1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2015 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
11*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
12*49cdfc7eSAndroid Build Coastguard Worker #include "tst_clocks.h"
13*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/posix_clocks.h"
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker static struct timespec start_time, stop_time;
16*49cdfc7eSAndroid Build Coastguard Worker static clockid_t clock_id;
17*49cdfc7eSAndroid Build Coastguard Worker
tst_timer_check(clockid_t clk_id)18*49cdfc7eSAndroid Build Coastguard Worker void tst_timer_check(clockid_t clk_id)
19*49cdfc7eSAndroid Build Coastguard Worker {
20*49cdfc7eSAndroid Build Coastguard Worker if (tst_clock_gettime(clk_id, &start_time)) {
21*49cdfc7eSAndroid Build Coastguard Worker if (errno == EINVAL) {
22*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF,
23*49cdfc7eSAndroid Build Coastguard Worker "Clock id %s(%u) not supported by kernel",
24*49cdfc7eSAndroid Build Coastguard Worker tst_clock_name(clk_id), clk_id);
25*49cdfc7eSAndroid Build Coastguard Worker return;
26*49cdfc7eSAndroid Build Coastguard Worker }
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "tst_clock_gettime() failed");
29*49cdfc7eSAndroid Build Coastguard Worker }
30*49cdfc7eSAndroid Build Coastguard Worker }
31*49cdfc7eSAndroid Build Coastguard Worker
tst_timer_start(clockid_t clk_id)32*49cdfc7eSAndroid Build Coastguard Worker void tst_timer_start(clockid_t clk_id)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker clock_id = clk_id;
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker if (tst_clock_gettime(clock_id, &start_time))
37*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker
tst_timer_expired_ms(long long ms)40*49cdfc7eSAndroid Build Coastguard Worker int tst_timer_expired_ms(long long ms)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker struct timespec cur_time;
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker if (tst_clock_gettime(clock_id, &cur_time))
45*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker return tst_timespec_diff_ms(cur_time, start_time) >= ms;
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker
tst_timer_stop(void)50*49cdfc7eSAndroid Build Coastguard Worker void tst_timer_stop(void)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker if (tst_clock_gettime(clock_id, &stop_time))
53*49cdfc7eSAndroid Build Coastguard Worker tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker
tst_timer_elapsed(void)56*49cdfc7eSAndroid Build Coastguard Worker struct timespec tst_timer_elapsed(void)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker return tst_timespec_diff(stop_time, start_time);
59*49cdfc7eSAndroid Build Coastguard Worker }
60