xref: /third_party/ltp/lib/tst_timer.c (revision f08c3bdf)
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
15static struct timespec start_time, stop_time;
16static clockid_t clock_id;
17
18void tst_timer_check(clockid_t clk_id)
19{
20	if (tst_clock_gettime(clk_id, &start_time)) {
21		if (errno == EINVAL) {
22			tst_brk(TCONF,
23			         "Clock id %s(%u) not supported by kernel",
24				 tst_clock_name(clk_id), clk_id);
25			return;
26		}
27
28		tst_brk(TBROK | TERRNO, "tst_clock_gettime() failed");
29	}
30}
31
32void tst_timer_start(clockid_t clk_id)
33{
34	clock_id = clk_id;
35
36	if (tst_clock_gettime(clock_id, &start_time))
37		tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
38}
39
40int tst_timer_expired_ms(long long ms)
41{
42	struct timespec cur_time;
43
44	if (tst_clock_gettime(clock_id, &cur_time))
45		tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
46
47	return tst_timespec_diff_ms(cur_time, start_time) >= ms;
48}
49
50void tst_timer_stop(void)
51{
52	if (tst_clock_gettime(clock_id, &stop_time))
53		tst_res(TWARN | TERRNO, "tst_clock_gettime() failed");
54}
55
56struct timespec tst_timer_elapsed(void)
57{
58	return tst_timespec_diff(stop_time, start_time);
59}
60