blob: afdb441720a5fb4facd3a2ea0dce7b5293c6dcc7 [file] [log] [blame]
Cyril Hrubis9dee4052015-02-24 13:31:14 +01001/*
2 * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#include <errno.h>
25
26#include "test.h"
27#include "tst_timer.h"
Cyril Hrubis62fe7122017-06-20 15:42:17 +020028#include "tst_clocks.h"
Cyril Hrubis9dee4052015-02-24 13:31:14 +010029#include "lapi/posix_clocks.h"
30
31static struct timespec start_time, stop_time;
32static clockid_t clock_id;
33
34static const char *clock_name(clockid_t clk_id)
35{
36 switch (clk_id) {
37 case CLOCK_REALTIME:
38 return "CLOCK_REALTIME";
39 case CLOCK_REALTIME_COARSE:
40 return "CLOCK_REALTIME_COARSE";
41 case CLOCK_MONOTONIC:
42 return "CLOCK_MONOTONIC";
43 case CLOCK_MONOTONIC_COARSE:
44 return "CLOCK_MONOTONIC_COARSE";
45 case CLOCK_MONOTONIC_RAW:
46 return "CLOCK_MONOTONIC_RAW";
47 case CLOCK_BOOTTIME:
48 return "CLOCK_BOOTTIME";
49 case CLOCK_PROCESS_CPUTIME_ID:
50 return "CLOCK_PROCESS_CPUTIME_ID";
51 case CLOCK_THREAD_CPUTIME_ID:
52 return "CLOCK_THREAD_CPUTIME_ID";
53 default:
54 return "UNKNOWN/INVALID";
55 }
56}
57
58void tst_timer_check(clockid_t clk_id)
59{
Cyril Hrubis62fe7122017-06-20 15:42:17 +020060 if (tst_clock_gettime(clk_id, &start_time)) {
Cyril Hrubis9dee4052015-02-24 13:31:14 +010061 if (errno == EINVAL) {
62 tst_brkm(TCONF, NULL,
63 "Clock id %s(%u) not supported by kernel",
64 clock_name(clk_id), clk_id);
Cyril Hrubisd101cab2017-02-14 11:48:46 +010065 return;
Cyril Hrubis9dee4052015-02-24 13:31:14 +010066 }
67
Cyril Hrubis62fe7122017-06-20 15:42:17 +020068 tst_brkm(TBROK | TERRNO, NULL, "tst_clock_gettime() failed");
Cyril Hrubis9dee4052015-02-24 13:31:14 +010069 }
70}
71
72void tst_timer_start(clockid_t clk_id)
73{
74 clock_id = clk_id;
75
Cyril Hrubis62fe7122017-06-20 15:42:17 +020076 if (tst_clock_gettime(clock_id, &start_time))
77 tst_resm(TWARN | TERRNO, "tst_clock_gettime() failed");
Cyril Hrubis9dee4052015-02-24 13:31:14 +010078}
79
80void tst_timer_stop(void)
81{
Cyril Hrubis62fe7122017-06-20 15:42:17 +020082 if (tst_clock_gettime(clock_id, &stop_time))
83 tst_resm(TWARN | TERRNO, "tst_clock_gettime() failed");
Cyril Hrubis9dee4052015-02-24 13:31:14 +010084}
85
86struct timespec tst_timer_elapsed(void)
87{
88 return tst_timespec_diff(stop_time, start_time);
89}