-
Notifications
You must be signed in to change notification settings - Fork 3
/
timer.c
351 lines (326 loc) · 8.45 KB
/
timer.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include "timer.h"
/** Functions **/
/* Function
* internal function to return a human understandable meaning for the
* status returns.
*
* @param: the return status values
*
* @return: the human understandable string meaning of the status value
*/
inline
char * error_num(int status)
{
char * tmp;
switch(status)
{
case OK:
tmp = (char *) "status is OK";
break;
case NOT_ALLOCATED:
tmp = (char *) "variable not allocated!";
break;
case CLOCK_FAILED:
tmp = (char *) "clock not available!";
break;
default:
tmp = (char *) "Unknown status number!";
break;
}
return tmp;
}
/* Function
* internal function that interprets the clock enum and returns the
* appropriate system clock.
*
* @param ck: clock enum
*
* @return: clockid_t system clock
*/
inline
clockid_t set_clock(clock_e ck)
{
clockid_t clock;
switch(ck)
{
case rtc:
clock = CLOCK_REALTIME_COARSE;
break;
case mono:
clock = CLOCK_MONOTONIC;
break;
case monoc:
clock = CLOCK_MONOTONIC_COARSE;
break;
case monor:
clock = CLOCK_MONOTONIC_RAW;
break;
case cpup:
clock = CLOCK_PROCESS_CPUTIME_ID;
break;
case cput:
clock = CLOCK_THREAD_CPUTIME_ID;
break;
case monob:
#ifdef CLOCK_BOOTTIME
clock = CLOCK_BOOTTIME;
break;
#endif
default:
ERROR("Invalid CLOCK value, using CLOCK_REALTIME");
/* Fall-through */
case rt:
clock = CLOCK_REALTIME;
break;
}
return clock;
}
/* Function
* internal function that calculates the difference between two struct
* timespecs and returns a struct timespec containing the difference.
*
* @param start: the fist measured time point
* @param end: the final or last measured time point
*
* @return: struct timespec The difference between start and end
*/
inline
struct timespec diff_timespec(struct timespec end, struct timespec begin)
{
struct timespec result = begin;
/* Perform the carry for the later subtraction. */
if (end.tv_nsec < begin.tv_nsec) {
int nsec = (begin.tv_nsec - end.tv_nsec) / 1000000000 + 1;
result.tv_nsec -= 1000000000 * nsec;
result.tv_sec += nsec;
}
if (end.tv_nsec - begin.tv_nsec > 1000000000) {
int nsec = (end.tv_nsec - begin.tv_nsec) / 1000000000;
result.tv_nsec += 1000000000 * nsec;
result.tv_sec -= nsec;
}
/* Compute the time remaining to wait.
* tv_usec is certainly positive. */
result.tv_sec = end.tv_sec - result.tv_sec;
result.tv_nsec = end.tv_nsec - result.tv_nsec;
return result;
}
/*
* XXX: This is a bit of unnecessary toil, however it does the job...
* An inline solution would be more practical... but I can't
* remember how to do it with a struct...
*/
int set_timespec(struct timespec ** tims, long sec, long nsec)
{
*tims = (struct timespec *) malloc(sizeof(struct timespec));
CHECK(!*tims, "Failed to allocate timespec!");
(*tims)->tv_sec = sec;
(*tims)->tv_nsec = nsec;
return OK;
error:
return NOT_ALLOCATED;
}
/* Function
* internal function to interpret the unit enum and return the
* appropriate time unit.
*
* @param unit: enum time unit
*
* @return: string The time unit
*/
inline
char * print_unit(unit_e unit)
{
switch(unit)
{
default:
ERROR("Invalid UNIT value, using seconds (s)");
/* Fall-through */
case 0:
return (char *) "s";
break;
case 1:
return (char *) "ms";
break;
case 2:
return (char *) "us";
break;
case 3:
return (char *) "ns";
break;
}
return (char *) "null";
}
/* Function
* create interval variable, which means to allocate the underlying
* structure.
*
* @param tmp: the address of the interval to be allocated
* @param name: the name of the interval
* @param ck: clock enum
* @param ut: unit enum
*
* @return: status code
*/
int create_interval(interval_t ** tmp, char * name, clock_e ck, unit_e ut)
{
*tmp = (interval_t *) malloc(sizeof(interval_t));
CHECK(!*tmp, "Unable to create interval %s!", name);
(*tmp)->name = name;
(*tmp)->clock = ck;
(*tmp)->unit = ut;
return OK;
error:
if(*tmp) free(*tmp);
return NOT_ALLOCATED;
}
/* Function
* get the current time and save its value in the appropriate pointer
*
* @param clock: the system clock to be used
* @param time: the timespec structure that holds the time
*
* @return: either OK, or error status from clock_gettime
*/
inline
int get_time(clockid_t clock, struct timespec * time)
{
int ret;
ret = clock_gettime(clock, time);
CHECK(ret, "Failed to get start time!");
return OK;
error:
return ret;
}
/* Function
* set the start field of the interval with the current time
*
* @param tmp: the interval
*
* @return: either OK, or error status from clock_gettime
*/
int start(interval_t * tmp)
{
return get_time(set_clock(tmp->clock), &(tmp->start));
}
/* Function
* set the stop field of the interval with the current time
*
* @param tmp: the interval
*
* @return: either OK, or error status from clock_gettime
*/
int stop(interval_t * tmp)
{
return get_time(set_clock(tmp->clock), &(tmp->stop));
}
/* Function
* compute the elapsed time from the interval
*
* @param tmp: the interval
* @param ut: unit enum
*
* @return: the elapsed time in the global time unit
*/
inline
double elapsed_interval(interval_t * tmp, unit_e ut)
{
double time = 0.0;
unit_e unit;
struct timespec diff;
diff = diff_timespec(tmp->stop, tmp->start);
unit = 0 <= ut && ut < unit_check ? ut : tmp->unit;
switch(unit)
{
default:
ERROR("Invalid UNIT value, using seconds (s)");
/* Fall-through */
case s:
time = (double) diff.tv_sec + NANO_TO_SEC(diff.tv_nsec);
break;
case ms:
time = (double) SEC_TO_MSEC(diff.tv_sec) + NANO_TO_MSEC(diff.tv_nsec);
break;
case us:
time = (double) SEC_TO_MCSEC(diff.tv_sec) + NANO_TO_MCSEC(diff.tv_nsec);
break;
case ns:
time = (double) SEC_TO_NSEC(diff.tv_sec) + diff.tv_nsec;
break;
}
return time;
}
/* Function
* this function prints out the elapsed time(s) from the given interval(s). It supports
* several different print formats.
*
* @param num: the number of intervals to be printed
* @param ...: the interval(s)
*/
inline
void print_results(int num, ...)
{
va_list vl;
char * names[num];
double values[num];
unit_e units[num];
va_start(vl, num);
for(int i = 0; i < num; i++)
{
interval_t * time = va_arg(vl, interval_t *);
units[i] = time->unit;
names[i] = (time->name == NULL) ? NULL : strdup(time->name);
values[i] = elapsed_interval(time, none);
}
va_end(vl);
for(int i = 0; i < num; i++)
{
printf("%s: %.3f %s\n", names[i], values[i], print_unit(units[i]));
free(names[i]);
}
}
/* Function
* this function prints out the elapsed time(s) from the given interval(s).
* Print out is in a CSV compatible format.
*
* @param comment: comment character that precedes the headers
* @param num: the number of intervals to be printed
* @param ...: the interval(s)
*/
inline
void print_results_csv(char * comment, int num, ...)
{
va_list vl;
char * names[num];
double values[num];
unit_e units[num];
va_start(vl, num);
for(int i = 0; i < num; i++)
{
interval_t * time = va_arg(vl, interval_t *);
units[i] = time->unit;
names[i] = (time->name == NULL) ? NULL : strdup(time->name);
values[i] = elapsed_interval(time, none);
}
va_end(vl);
printf("%s ", comment);
for(int i = 0; i < num; i++)
{
printf("%s (%s)", names[i], print_unit(units[i]));
free(names[i]);
if(i < num - 1) printf(", ");
}
printf("\n");
for(int i = 0; i < num; i++)
{
printf("%.3f", values[i]);
if(i < num - 1) printf(", ");
}
printf("\n");
}