timing.c 8.65 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
9
10
11
12
13
 */

/**
 * @file
 * @brief   platform neutral timing utilities
 */
#include <stdio.h>
#include <string.h>

#include "timing.h"
14
#include "xmalloc.h"
Matthias Braun's avatar
Matthias Braun committed
15
#include "panic.h"
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>

/* Win32 timer value. */
typedef union {
	unsigned lo_prec;       /**< 32bit low precision time in milli seconds */
	LARGE_INTEGER hi_prec;  /**< 64bit high precision time in micro seconds */
} ir_timer_val_t;

#else

30
#include <unistd.h>
31
32
33
34
35
36
37
38
39
#define HAVE_GETTIMEOFDAY

/*
 * Just, if we have gettimeofday()
 * Someday, we will have a check here.
 */
#ifndef __USE_BSD
#define __USE_BSD
#include <sys/time.h>
40
41
42
43
#undef __USE_BSD
#else
#include <sys/time.h>
#endif
44
45
46
47
48
49
50
51

/* Linux timer value. */
typedef struct timeval ir_timer_val_t;

#endif /* _Win32 */

#include <stddef.h>

52
static inline void _time_reset(ir_timer_val_t *val);
53
54
55
56

/**
 * A timer.
 */
57
struct ir_timer_t {
58
59
	ir_timer_val_t elapsed;     /**< the elapsed time so far */
	ir_timer_val_t start;       /**< the start value of the timer */
Matthias Braun's avatar
Matthias Braun committed
60
61
	ir_timer_t     *parent;     /**< parent of a timer */
	ir_timer_t     *displaced;  /**< former timer in case of timer_push */
62
63
64
65
66
67
	unsigned       running : 1; /**< set if this timer is running */
};

/** The top of the timer stack */
static ir_timer_t *timer_stack;

68
ir_timer_t *ir_timer_new(void)
69
{
70
71
72
73
74
	ir_timer_t *timer = XMALLOCZ(ir_timer_t);
	_time_reset(&timer->elapsed);
	_time_reset(&timer->start);
	return timer;
}
75

76
77
void ir_timer_free(ir_timer_t *timer)
{
78
	free(timer);
79
80
81
82
}

#ifdef HAVE_GETTIMEOFDAY

83
static inline void _time_get(ir_timer_val_t *val)
84
85
86
87
{
	gettimeofday(val, NULL);
}

88
static inline void _time_reset(ir_timer_val_t *val)
89
90
91
92
{
	timerclear(val);
}

93
static inline unsigned long _time_to_msec(const ir_timer_val_t *elapsed)
94
95
96
97
98
{
	return (unsigned long) elapsed->tv_sec * 1000UL
		+ (unsigned long) elapsed->tv_usec / 1000UL;
}

99
static inline unsigned long _time_to_usec(const ir_timer_val_t *elapsed)
100
101
102
103
104
{
	return (unsigned long) elapsed->tv_sec * 1000000UL
		+ (unsigned long) elapsed->tv_usec;
}

Matthias Braun's avatar
Matthias Braun committed
105
106
107
108
109
static inline double _time_to_sec(const ir_timer_val_t *elapsed)
{
	return (double)elapsed->tv_sec + (double)elapsed->tv_usec / 1000000.0;
}

110
static inline ir_timer_val_t *_time_add(ir_timer_val_t *res,
111
112
113
114
115
116
		const ir_timer_val_t *lhs, const ir_timer_val_t *rhs)
{
	timeradd(lhs, rhs, res);
		return res;
}

117
static inline ir_timer_val_t *_time_sub(ir_timer_val_t *res,
118
119
120
121
122
123
124
125
		const ir_timer_val_t *lhs, const ir_timer_val_t *rhs)
{
	timersub(lhs, rhs, res);
	return res;
}

#elif defined(_WIN32)

126
static inline void _time_get(ir_timer_val_t *val)
127
{
128
	if (!QueryPerformanceCounter(&val->hi_prec))
129
130
131
		val->lo_prec = timeGetTime();
}

132
static inline void _time_reset(ir_timer_val_t *val)
133
134
135
136
{
	memset(val, 0, sizeof(val[0]));
}

137
static inline unsigned long _time_to_msec(const ir_timer_val_t *elapsed)
138
139
140
{
	LARGE_INTEGER freq;

141
	if (!QueryPerformanceFrequency(&freq))
142
143
144
145
146
		return (unsigned long) elapsed->lo_prec;

	return (unsigned long) ((elapsed->hi_prec.QuadPart * 1000) / freq.QuadPart);
}

147
static inline unsigned long _time_to_usec(const ir_timer_val_t *elapsed)
148
149
150
{
	LARGE_INTEGER freq;

151
	if (!QueryPerformanceFrequency(&freq))
Matthias Braun's avatar
Matthias Braun committed
152
		return (unsigned long) elapsed->lo_prec * 1000;
153
154
155
156

	return (unsigned long) ((elapsed->hi_prec.QuadPart * 1000000) / freq.QuadPart);
}

Matthias Braun's avatar
Matthias Braun committed
157
158
159
160
161
162
163
164
165
166
static inline double _time_to_sec(const ir_timer_val_t *elapsed)
{
	LARGE_INTEGER freq;

	if (!QueryPerformanceFrequency(&freq))
		return (double) elapsed->lo_prec / 1000.;

	return (double)elapsed->hi_prec.QuadPart / (double)freq.QuadPart;
}

167
static inline ir_timer_val_t *_time_add(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs)
168
169
{
	LARGE_INTEGER dummy;
170
	if (QueryPerformanceFrequency(&dummy))
171
172
173
174
175
176
177
		res->hi_prec.QuadPart = lhs->hi_prec.QuadPart + rhs->hi_prec.QuadPart;
	else
		res->lo_prec = lhs->lo_prec + rhs->lo_prec;

	return res;
}

178
static inline ir_timer_val_t *_time_sub(ir_timer_val_t *res, const ir_timer_val_t *lhs, const ir_timer_val_t *rhs)
179
180
{
	LARGE_INTEGER dummy;
181
	if (QueryPerformanceFrequency(&dummy))
182
183
184
185
186
187
188
189
190
		res->hi_prec.QuadPart = lhs->hi_prec.QuadPart - rhs->hi_prec.QuadPart;
	else
		res->lo_prec = lhs->lo_prec - rhs->lo_prec;

	return res;
}

#endif /* _WIN32 */

Michael Beck's avatar
Michael Beck committed
191
#if defined(_XOPEN_REALTIME) && _XOPEN_REALTIME != -1
192
193
194
195
196
197
198
199
200
201
202
203
204
205

#include <sys/types.h>
#include <sched.h>

static struct sched_param std_sched_param;
static int std_sched_param_init = 0;

int ir_timer_enter_high_priority(void)
{
	pid_t pid = getpid();

	struct sched_param p;
	int res, max, algo;

206
	if (!std_sched_param_init) {
207
		res = sched_getparam(pid, &std_sched_param);
208
209
		if (res != 0)
			return res;
210
211
212
213
214
215
		std_sched_param_init = 1;
	}

	algo = sched_getscheduler(pid);
	max  = sched_get_priority_max(algo);

216
	p = std_sched_param;
217
218
219
220
221
222
223
224
225
226
227
	p.sched_priority = max;
	res = sched_setparam(pid, &p);

	return res;
}

int ir_timer_leave_high_priority(void)
{
	int res   = 0;
	pid_t pid = getpid();

228
	if (std_sched_param_init)
229
230
231
232
233
234
235
236
237
238
239
240
		res = sched_setparam(pid, &std_sched_param);

	return res;
}

#elif defined(_WIN32)

static int initial_priority = THREAD_PRIORITY_NORMAL;

int ir_timer_leave_high_priority(void)
{
	int res = 0;
241
	if (!SetThreadPriority(GetCurrentThread(), initial_priority)) {
242
243
244
245
246
247
248
249
250
251
252
		fprintf(stderr, "Failed to leave high priority (%d)\n", GetLastError());
		res = GetLastError();
	}

	return res;
}

int ir_timer_enter_high_priority(void)
{
	int res = 0;
	initial_priority = GetThreadPriority(GetCurrentThread());
253
	if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST)) {
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
		fprintf(stderr, "Failed to enter high priority (%d)\n", GetLastError());
		res = GetLastError();
	}

	return res;
}


#else

int ir_timer_enter_high_priority(void)
{
	fprintf(stderr, "POSIX scheduling API not present\n");
	return 0;
}

int ir_timer_leave_high_priority(void)
{
	return 0;
}

275
#endif
276
277
278
279
280
281
282
283
284
285
286
287

/* reset a timer */
void ir_timer_reset(ir_timer_t *timer)
{
	_time_reset(&timer->elapsed);
	_time_reset(&timer->start);
	timer->running = 0;
}

/* start a timer */
void ir_timer_start(ir_timer_t *timer)
{
Matthias Braun's avatar
Matthias Braun committed
288
289
290
	if (timer->running)
		panic("timer started twice");

291
292
293
	_time_reset(&timer->start);
	_time_get(&timer->start);
	timer->running = 1;
Matthias Braun's avatar
Matthias Braun committed
294
295
296
297
298
299
300

	if (timer->parent == NULL) {
		timer->parent = timer_stack;
	} else if (timer->parent != timer_stack) {
		panic("timer used at different stack positions");
	}
	timer_stack = timer;
301
302
303
304
305
306
307
308
309
310
}

void ir_timer_reset_and_start(ir_timer_t *timer)
{
  _time_reset(&timer->elapsed);
  ir_timer_start(timer);
}

void ir_timer_stop(ir_timer_t *timer)
{
Matthias Braun's avatar
Matthias Braun committed
311
312
313
314
315
316
317
318
319
320
321
322
323
	if (!timer->running)
		panic("attempting to stop stopped timer");
	if (timer != timer_stack)
		panic("timer stack error");
	timer_stack = timer->parent;

	ir_timer_val_t val;
	ir_timer_val_t tgt;

	_time_get(&val);
	timer->running = 0;
	_time_add(&timer->elapsed, &timer->elapsed, _time_sub(&tgt, &val, &timer->start));
}
324

Matthias Braun's avatar
Matthias Braun committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
void ir_timer_init_parent(ir_timer_t *timer)
{
	if (timer == NULL)
		return;
	if (timer->parent != NULL && timer->parent != timer_stack)
		panic("timer parent mismatch");
	timer->parent = timer_stack;
}

void ir_timer_push(ir_timer_t *timer)
{
	if (timer->running)
		panic("timer started twice");

	ir_timer_t *parent = timer->parent;
	if (timer->parent == NULL)
		panic("pushing timer with unknown parent");

	timer->displaced = timer_stack;
	for (ir_timer_t *t = timer_stack; t != parent; t = t->parent) {
		if (t == NULL)
			panic("parent timer not on stack");
		ir_timer_stop(t);
348
	}
Matthias Braun's avatar
Matthias Braun committed
349
350
351
	timer_stack = parent;

	ir_timer_start(timer);
352
353
}

Matthias Braun's avatar
Matthias Braun committed
354
static void start_stack(ir_timer_t *timer, ir_timer_t *stop)
355
{
Matthias Braun's avatar
Matthias Braun committed
356
357
358
	if (timer == stop)
		return;
	start_stack(timer->parent, stop);
359
360
361
	ir_timer_start(timer);
}

Matthias Braun's avatar
Matthias Braun committed
362
void ir_timer_pop(ir_timer_t *timer)
363
{
Matthias Braun's avatar
Matthias Braun committed
364
365
366
367
368
369
370
371
372
373
374
	if (!timer->running)
		panic("attempting to stop stopped timer");
	ir_timer_t *displaced = timer->displaced;
	if (displaced == NULL)
		panic("timer start/stop/push/pop mismatch");

	ir_timer_t *parent = timer->parent;
	timer->displaced = NULL;

	ir_timer_stop(timer);
	start_stack(displaced, parent);
375
376
377
378
379
380
381
}

unsigned long ir_timer_elapsed_msec(const ir_timer_t *timer)
{
	ir_timer_val_t v;
	const ir_timer_val_t *elapsed = &timer->elapsed;

382
	if (timer->running) {
383
384
385
386
387
388
389
390
391
392
393
394
		elapsed = &v;
		_time_get(&v);
		_time_add(&v, &timer->elapsed, _time_sub(&v, &v, &timer->start));
	}
	return _time_to_msec(elapsed);
}

unsigned long ir_timer_elapsed_usec(const ir_timer_t *timer)
{
	ir_timer_val_t v;
	const ir_timer_val_t *elapsed = &timer->elapsed;

395
	if (timer->running) {
396
397
398
399
400
401
		elapsed = &v;
		_time_get(&v);
		_time_add(&v, &timer->elapsed, _time_sub(&v, &v, &timer->start));
	}
	return _time_to_usec(elapsed);
}
Matthias Braun's avatar
Matthias Braun committed
402
403
404
405
406
407
408
409
410
411
412
413
414

double ir_timer_elapsed_sec(const ir_timer_t *timer)
{
	ir_timer_val_t v;
	const ir_timer_val_t *elapsed = &timer->elapsed;

	if (timer->running) {
		elapsed = &v;
		_time_get(&v);
		_time_add(&v, &timer->elapsed, _time_sub(&v, &v, &timer->start));
	}
	return _time_to_sec(elapsed);
}