-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimers.c
115 lines (106 loc) · 2.64 KB
/
timers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* timers.c */
#include <sys/times.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Timer Timer;
struct Timer {
char *id;
struct tms start;
clock_t total;
Timer *next;
};
Timer *timers;
void finalise_timers (void)
{
Timer *t;
const char *fname = "timers.times";
FILE *f;
fprintf (stderr, "=== timers ===\n");
for (t = timers; t; t = t->next)
{
fprintf (stderr, "Timer '%s': %d\n", t->id, t->total);
if (t->start.tms_utime != 0)
{
fprintf (stderr, "XXX Timer is still running XXX\n");
abort();
}
}
/* Read in timers */
f = fopen (fname, "r");
if (f)
{
while (!feof (f))
{
char buffer[BUFSIZ], buffer2[BUFSIZ];
char *c;
int n;
fgets (buffer, BUFSIZ, f);
if (feof (f))
break;
/* chomp */
fgets (buffer2, BUFSIZ, f);
n = atoi (buffer2);
for (c = buffer; *c; c++)
if (*c == '\n' || *c == '\r')
*c = '\0';
/* Find it in the timers */
for (t = timers; t; t = t->next)
{
if (!strcmp(t->id, buffer))
{
t->total += n;
break;
}
}
/* Add it to our list if it's not there */
if (!t)
{
t = alloca (sizeof *t);
t->next = timers;
timers = t;
t->id = alloca (strlen(buffer)+1);
strcpy (t->id, buffer);
t->total = n;
}
}
fclose (f);
fprintf (stderr, "=== cumulativive ===\n");
for (t = timers; t; t = t->next)
fprintf (stderr, "Timer '%s': %d\n", t->id, t->total);
}
f = fopen (fname, "w");
for (t = timers; t; t = t->next)
fprintf (f, "%s\n%d\n", t->id, t->total);
fclose (f);
}
void start_timer (Timer *t, char *id)
{
if (!t->id)
{
/* First call to start this timer. */
t->id = id;
/* Is this the first timer? */
if (!timers)
atexit (finalise_timers);
t->next = timers;
timers = t;
}
if (t->start.tms_utime != 0)
{
fprintf (stderr, "XXX counter '%s' restarted without finish\n", t->id);
abort();
}
times (& t->start);
}
void stop_timer (Timer *t)
{
struct tms stop;
times(&stop);
t->total += stop.tms_utime - t->start.tms_utime;
t->start.tms_utime = 0;
}
/* XXX header or c&p or #include */
#define BEGIN_TIMER(s) do { static Timer __t; start_timer (&__t, s);
#define END_TIMER stop_timer (&__t); } while (0);
#define NEXT_TIMER(s) END_TIMER BEGIN_TIMER(s)