• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
4  */
5 
6 #include <errno.h>
7 
8 #define TST_NO_DEFAULT_MAIN
9 
10 #include "tst_test.h"
11 #include "tst_timer.h"
12 #include "tst_clocks.h"
13 #include "lapi/posix_clocks.h"
14 
15 static struct timespec start_time, stop_time;
16 static clockid_t clock_id;
17 
clock_name(clockid_t clk_id)18 static const char *clock_name(clockid_t clk_id)
19 {
20 	switch (clk_id) {
21 	case CLOCK_REALTIME:
22 		return "CLOCK_REALTIME";
23 	case CLOCK_REALTIME_COARSE:
24 		return "CLOCK_REALTIME_COARSE";
25 	case CLOCK_MONOTONIC:
26 		return "CLOCK_MONOTONIC";
27 	case CLOCK_MONOTONIC_COARSE:
28 		return "CLOCK_MONOTONIC_COARSE";
29 	case CLOCK_MONOTONIC_RAW:
30 		return "CLOCK_MONOTONIC_RAW";
31 	case CLOCK_BOOTTIME:
32 		return "CLOCK_BOOTTIME";
33 	case CLOCK_PROCESS_CPUTIME_ID:
34 		return "CLOCK_PROCESS_CPUTIME_ID";
35 	case CLOCK_THREAD_CPUTIME_ID:
36 		return "CLOCK_THREAD_CPUTIME_ID";
37 	default:
38 		return "UNKNOWN/INVALID";
39 	}
40 }
41 
tst_timer_check(clockid_t clk_id)42 void tst_timer_check(clockid_t clk_id)
43 {
44 	if (tst_clock_gettime(clk_id, &start_time)) {
45 		if (errno == EINVAL) {
46 			tst_brk(TCONF,
47 			         "Clock id %s(%u) not supported by kernel",
48 				 clock_name(clk_id), clk_id);
49 			return;
50 		}
51 
52 		tst_brk(TBROK | TERRNO, "tst_clock_gettime() failed");
53 	}
54 }
55 
tst_timer_start(clockid_t clk_id)56 void tst_timer_start(clockid_t clk_id)
57 {
58 	clock_id = clk_id;
59 
60 	if (tst_clock_gettime(clock_id, &start_time))
61 		tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
62 }
63 
tst_timer_expired_ms(long long ms)64 int tst_timer_expired_ms(long long ms)
65 {
66 	struct timespec cur_time;
67 
68 	if (tst_clock_gettime(clock_id, &cur_time))
69 		tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
70 
71 	return tst_timespec_diff_ms(cur_time, start_time) >= ms;
72 }
73 
tst_timer_stop(void)74 void tst_timer_stop(void)
75 {
76 	if (tst_clock_gettime(clock_id, &stop_time))
77 		tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
78 }
79 
tst_timer_elapsed(void)80 struct timespec tst_timer_elapsed(void)
81 {
82 	return tst_timespec_diff(stop_time, start_time);
83 }
84