aboutsummaryrefslogtreecommitdiff
path: root/include/tst_timer.h
blob: 8751149f02a30448183d17874b1cf861928487ca (plain)
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
/*
 * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Further, this software is distributed without any warranty that it is
 * free of the rightful claim of any third person regarding infringement
 * or the like.  Any license provided herein, whether implied or
 * otherwise, applies only to this software file.  Patent licenses, if
 * any, provided herein do not apply to combinations of this program with
 * other software, or any other product whatsoever.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

 /*

   Timer - struct timespec conversion runtimes and easy to use functions to
           measure elapsed time.

  */

#ifndef TST_TIMER
#define TST_TIMER

#include <sys/time.h>
#include <time.h>

static inline long long tst_timespec_to_ns(struct timespec t)
{
	return t.tv_sec * 1000000000 + t.tv_nsec;
}

/*
 * Converts timespec to microseconds.
 */
static inline long long tst_timespec_to_us(struct timespec t)
{
	return t.tv_sec * 1000000 + (t.tv_nsec + 500) / 1000;
}

/*
 * Converts timespec to miliseconds.
 */
static inline long long tst_timespec_to_ms(struct timespec t)
{
	return t.tv_sec * 1000 + (t.tv_nsec + 500000) / 1000000;
}

/*
 * Converts timeval to microseconds.
 */
static inline long long tst_timeval_to_us(struct timeval t)
{
	return t.tv_sec * 1000000 + t.tv_usec;
}

/*
 * Converts timeval to miliseconds.
 */
static inline long long tst_timeval_to_ms(struct timeval t)
{
	return t.tv_sec * 1000 + (t.tv_usec + 500) / 1000;
}

/*
 * Converts ms to struct timeval
 */
static inline struct timeval tst_ms_to_timeval(long long ms)
{
	struct timeval ret;

	ret.tv_sec = ms / 1000;
	ret.tv_usec = (ms % 1000) * 1000;

	return ret;
}

/*
 * Converts us to struct timeval
 */
static inline struct timeval tst_us_to_timeval(long long us)
{
	struct timeval ret;

	ret.tv_sec = us / 1000000;
	ret.tv_usec = us % 1000000;

	return ret;
}

/*
 * Converts ms to struct timespec
 */
static inline struct timespec tst_ms_to_timespec(long long ms)
{
	struct timespec ret;

	ret.tv_sec = ms / 1000;
	ret.tv_nsec = (ms % 1000) * 1000000;

	return ret;
}

/*
 * Converts us to struct timespec
 */
static inline struct timespec tst_us_to_timespec(long long us)
{
	struct timespec ret;

	ret.tv_sec = us / 1000000;
	ret.tv_nsec = (us % 1000000) * 1000;

	return ret;
}

/*
 * Comparsions
 */
static inline int tst_timespec_lt(struct timespec t1, struct timespec t2)
{
	if (t1.tv_sec == t2.tv_sec)
		return t1.tv_nsec < t2.tv_nsec;

	return t1.tv_sec < t2.tv_sec;
}

/*
 * Adds us microseconds to t.
 */
static inline struct timespec tst_timespec_add_us(struct timespec t,
                                                  long long us)
{
	t.tv_sec += us / 1000000;
	t.tv_nsec += (us % 1000000) * 1000;

	if (t.tv_nsec >= 1000000000) {
		t.tv_sec++;
		t.tv_nsec -= 1000000000;
	}

	return t;
}

/*
 * Adds two timespec structures.
 */
static inline struct timespec tst_timespec_add(struct timespec t1,
                                               struct timespec t2)
{
	struct timespec res;

	res.tv_sec = t1.tv_sec + t2.tv_sec;
	res.tv_nsec = t1.tv_nsec + t2.tv_nsec;

	if (res.tv_nsec >= 1000000000) {
		res.tv_sec++;
		res.tv_nsec -= 1000000000;
	}

	return res;
}

/*
 * Subtracts us microseconds from t.
 */
static inline struct timespec tst_timespec_sub_us(struct timespec t,
                                                  long long us)
{
	t.tv_sec -= us / 1000000;
	t.tv_nsec -= (us % 1000000) * 1000;

	if (t.tv_nsec < 0) {
		t.tv_sec--;
		t.tv_nsec += 1000000000;
	}

	return t;
}

/*
 * Returns difference between two timespec structures.
 */
static inline struct timespec tst_timespec_diff(struct timespec t1,
                                                struct timespec t2)
{
	struct timespec res;

	res.tv_sec = t1.tv_sec - t2.tv_sec;

	if (t1.tv_nsec < t2.tv_nsec) {
		res.tv_sec--;
		res.tv_nsec = 1000000000 - (t2.tv_nsec - t1.tv_nsec);
	} else {
		res.tv_nsec = t1.tv_nsec - t2.tv_nsec;
	}

	return res;
}

static inline long long tst_timespec_diff_ns(struct timespec t1,
					     struct timespec t2)
{
	return t1.tv_nsec - t2.tv_nsec + 1000000000LL * (t1.tv_sec - t2.tv_sec);
}

static inline long long tst_timespec_diff_us(struct timespec t1,
                                             struct timespec t2)
{
	return tst_timespec_to_us(tst_timespec_diff(t1, t2));
}

static inline long long tst_timespec_diff_ms(struct timespec t1,
                                             struct timespec t2)
{
	return tst_timespec_to_ms(tst_timespec_diff(t1, t2));
}

/*
 * Returns difference between two timeval structures.
 */
static inline struct timeval tst_timeval_diff(struct timeval t1,
                                              struct timeval t2)
{
	struct timeval res;

	res.tv_sec = t1.tv_sec - t2.tv_sec;

	if (t1.tv_usec < t2.tv_usec) {
		res.tv_sec--;
		res.tv_usec = 1000000 - (t2.tv_usec - t1.tv_usec);
	} else {
		res.tv_usec = t1.tv_usec - t2.tv_usec;
	}

	return res;
}

static inline long long tst_timeval_diff_us(struct timeval t1,
                                            struct timeval t2)
{
	return tst_timeval_to_us(tst_timeval_diff(t1, t2));
}

static inline long long tst_timeval_diff_ms(struct timeval t1,
                                            struct timeval t2)
{
	return tst_timeval_to_ms(tst_timeval_diff(t1, t2));
}

/*
 * Returns absolute value of difference between two timespec structures.
 */
static inline struct timespec tst_timespec_abs_diff(struct timespec t1,
                                                    struct timespec t2)
{
	if (tst_timespec_lt(t1, t2))
		return tst_timespec_diff(t2, t1);
	else
		return tst_timespec_diff(t1, t2);
}

static inline long long tst_timespec_abs_diff_us(struct timespec t1,
                                                 struct timespec t2)
{
       return tst_timespec_to_us(tst_timespec_abs_diff(t1, t2));
}

static inline long long tst_timespec_abs_diff_ms(struct timespec t1,
                                                 struct timespec t2)
{
       return tst_timespec_to_ms(tst_timespec_abs_diff(t1, t2));
}

/*
 * Exits the test with TCONF if particular timer is not supported. This is
 * intended to be used in test setup. There is no cleanup callback parameter as
 * you are expected to call it before initializing any resources that has to be
 * cleaned up later.
 *
 * @clk_id: Posix clock to use.
 */
void tst_timer_check(clockid_t clk_id);

/*
 * Marks a start time for given clock type.
 *
 * @clk_id: Posix clock to use.
 */
void tst_timer_start(clockid_t clk_id);

/*
 * Returns true if timer started by tst_timer_start() has been running for
 * longer than ms seconds.
 *
 * @ms: Time interval in miliseconds.
 */
int tst_timer_expired_ms(long long ms);

/*
 * Marks timer end time.
 */
void tst_timer_stop(void);

/*
 * Retuns elapsed time in struct timespec.
 */
struct timespec tst_timer_elapsed(void);

/*
 * Returns elapsed time in miliseconds.
 */
static inline long long tst_timer_elapsed_ms(void)
{
	return tst_timespec_to_ms(tst_timer_elapsed());
}

/*
 * Returns elapsed time in microseconds.
 */
static inline long long tst_timer_elapsed_us(void)
{
	return tst_timespec_to_us(tst_timer_elapsed());
}

/*
 * Returns a string containing given clock type name
 */
const char *tst_clock_name(clockid_t);

#endif /* TST_TIMER */