-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtimer.c
298 lines (269 loc) · 8.35 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
/* Copyright (c) 2013 The Squash Authors
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Authors:
* Evan Nemerson <[email protected]>
*/
#define _POSIX_C_SOURCE 199309L
#include "timer.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define SQUASH_TIMER_METHOD_CLOCK_GETTIME 0
#define SQUASH_TIMER_METHOD_GETRUSAGE 1
#define SQUASH_TIMER_METHOD_CLOCK 2
#define SQUASH_TIMER_METHOD_GETPROCESSTIMES 3
#ifdef _WIN32
# define SQUASH_TIMER_METHOD SQUASH_TIMER_METHOD_GETPROCESSTIMES
# include <windows.h>
#endif
#if !defined(SQUASH_TIMER_METHOD) && (_POSIX_TIMERS > 0)
# ifdef _POSIX_CPUTIME
# define SQUASH_TIMER_CPUTIME CLOCK_PROCESS_CPUTIME_ID
# define SQUASH_TIMER_METHOD SQUASH_TIMER_METHOD_CLOCK_GETTIME
# include <time.h>
# elif defined(CLOCK_VIRTUAL)
# define SQUASH_TIMER_CPUTIME CLOCK_VIRTUAL
# define SQUASH_TIMER_METHOD SQUASH_TIMER_METHOD_CLOCK_GETTIME
# include <time.h>
# endif
#endif
#if !defined(SQUASH_TIMER_METHOD)
# define SQUASH_TIMER_METHOD SQUASH_TIMER_METHOD_GETRUSAGE
# include <sys/time.h>
# include <sys/resource.h>
#endif
struct SquashTimer_s {
double elapsed_cpu;
double elapsed_wall;
#if SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_CLOCK_GETTIME
struct timespec start_wall;
struct timespec end_wall;
struct timespec start_cpu;
struct timespec end_cpu;
#elif SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_GETRUSAGE
struct timeval start_wall;
struct timeval end_wall;
struct rusage start_cpu;
struct rusage end_cpu;
#elif SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_GETPROCESSTIMES
ULONGLONG start_wall;
ULONGLONG end_wall;
FILETIME start_cpu;
FILETIME end_cpu;
#endif
};
/**
* @defgroup SquashTimer SquashTimer
* @brief A cross-platform timer
*
* @{
*/
/**
* @struct _SquashTimer
* @brief Simple cross-platform timer
*
* Provides a timer for measuring the elapsed time between events, in
* both wall-clock and CPU time.
*/
/**
* @brief Create a new timer.
*
* Note that this function does not actually start timing.
*
* @return A new timer instance, or *NULL* on failure.
* @see squash_timer_free
*/
SquashTimer*
squash_timer_new (void) {
SquashTimer* timer = (SquashTimer*) malloc (sizeof (SquashTimer));
squash_timer_reset (timer);
return timer;
}
/**
* @brief Free a timer.
*
* @param timer The timer.
*/
void
squash_timer_free (SquashTimer* timer) {
free (timer);
}
/**
* @brief Begin or continue timing.
*
* @param timer The timer.
*/
void
squash_timer_start (SquashTimer* timer) {
#if SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_CLOCK_GETTIME
if (clock_gettime (CLOCK_REALTIME, &(timer->start_wall)) != 0) {
fputs ("Unable to get wall clock time\n", stderr);
exit (-1);
}
if (clock_gettime (SQUASH_TIMER_CPUTIME, &(timer->start_cpu)) != 0) {
fputs ("Unable to get CPU clock time\n", stderr);
exit (-1);
}
#elif SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_GETRUSAGE
if (gettimeofday(&(timer->start_wall), NULL) != 0) {
fputs ("Unable to get time\n", stderr);
}
if (getrusage(RUSAGE_SELF, &(timer->start_cpu)) != 0) {
fputs ("Unable to get CPU clock time\n", stderr);
exit (-1);
}
#elif SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_GETPROCESSTIMES
#if _WIN32_WINNT >= 0x0600
timer->start_wall = GetTickCount64 ();
#else
timer->start_wall = GetTickCount ();
#endif
FILETIME CreationTime, ExitTime, KernelTime;
if (!GetProcessTimes (GetCurrentProcess(), &CreationTime, &ExitTime, &KernelTime, &(timer->start_cpu))) {
fputs ("Unable to get CPU clock time\n", stderr);
}
#endif
}
#if SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_CLOCK_GETTIME
static double
squash_timer_timespec_elapsed (struct timespec* start, struct timespec* end) {
return
(double) (end->tv_sec - start->tv_sec) +
(((double) (end->tv_nsec - start->tv_nsec)) / 1000000000);
}
#elif SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_GETRUSAGE
static double
squash_timer_timeval_elapsed (struct timeval* start, struct timeval* end) {
return
(double) (end->tv_sec - start->tv_sec) +
(((double) (end->tv_usec - start->tv_usec)) / 1000000);
}
static double
squash_timer_rusage_elapsed (struct rusage* start, struct rusage* end) {
return
(double) ((end->ru_utime.tv_sec + end->ru_stime.tv_sec) - (start->ru_utime.tv_sec + start->ru_stime.tv_sec)) +
(((double) ((end->ru_utime.tv_usec + end->ru_stime.tv_usec) - (start->ru_utime.tv_usec + start->ru_stime.tv_usec))) / 1000000);
}
#endif
/**
* @brief Stop timing.
*
* @param timer The timer.
*/
void
squash_timer_stop (SquashTimer* timer) {
#if SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_CLOCK_GETTIME
if (clock_gettime (SQUASH_TIMER_CPUTIME, &(timer->end_cpu)) != 0) {
fputs ("Unable to get CPU clock time\n", stderr);
exit (-1);
}
if (clock_gettime (CLOCK_REALTIME, &(timer->end_wall)) != 0) {
fputs ("Unable to get wall clock time\n", stderr);
exit (-1);
}
timer->elapsed_cpu += squash_timer_timespec_elapsed (&(timer->start_cpu), &(timer->end_cpu));
timer->elapsed_wall += squash_timer_timespec_elapsed (&(timer->start_wall), &(timer->end_wall));
#elif SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_GETRUSAGE
if (getrusage(RUSAGE_SELF, &(timer->end_cpu)) != 0) {
fputs ("Unable to get CPU clock time\n", stderr);
exit (-1);
}
if (gettimeofday(&(timer->end_wall), NULL) != 0) {
fputs ("Unable to get time\n", stderr);
exit (-1);
}
timer->elapsed_cpu += squash_timer_rusage_elapsed (&(timer->start_cpu), &(timer->end_cpu));
timer->elapsed_wall += squash_timer_timeval_elapsed (&(timer->start_wall), &(timer->end_wall));
#elif SQUASH_TIMER_METHOD == SQUASH_TIMER_METHOD_GETPROCESSTIMES
FILETIME CreationTime, ExitTime, KernelTime;
if (!GetProcessTimes (GetCurrentProcess(), &CreationTime, &ExitTime, &KernelTime, &(timer->end_cpu))) {
fputs ("Unable to get CPU clock time\n", stderr);
}
#if _WIN32_WINNT >= 0x0600
timer->end_wall = GetTickCount64 ();
#else
timer->end_wall = GetTickCount ();
if (timer->end_wall < timer->start_wall) {
timer->end_wall += 0xffffffff;
}
#endif
uint64_t start_cpu, end_cpu;
start_cpu = timer->start_cpu.dwHighDateTime;
start_cpu <<= sizeof (DWORD) * 8;
start_cpu |= timer->start_cpu.dwLowDateTime;
end_cpu = timer->end_cpu.dwHighDateTime;
end_cpu <<= sizeof (DWORD) * 8;
end_cpu |= timer->end_cpu.dwLowDateTime;
timer->elapsed_cpu += ((double) (end_cpu - start_cpu)) / 10000000;
timer->elapsed_wall += ((double) (timer->end_wall - timer->start_wall)) / 1000;
#endif
}
/**
* @brief Reset the timer.
*
* Resets the elapsed time to 0.
*
* @param timer The timer.
*/
void
squash_timer_reset (SquashTimer* timer) {
timer->elapsed_cpu = 0.0;
timer->elapsed_wall = 0.0;
}
/**
* @brief Restart the timer.
*
* This is a convenience wrapper for resetting the timer then starting
* it.
*
* @param timer The timer.
*/
void
squash_timer_restart (SquashTimer* timer) {
squash_timer_reset (timer);
squash_timer_start (timer);
}
/**
* @brief Get the elapsed CPU time.
*
* @param timer The timer.
* @return Number of seconds (CPU time) elapsed.
*/
double
squash_timer_get_elapsed_cpu (SquashTimer* timer) {
return timer->elapsed_cpu;
}
/**
* @brief Get the elapsed wall-clock time.
*
* @param timer The timer.
* @return Number of seconds (wall-clock time) elapsed.
*/
double
squash_timer_get_elapsed_wall (SquashTimer* timer) {
return timer->elapsed_wall;
}
/**
* @}
*/