• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
28 #include "lapi/posix_clocks.h"
29 
30 static struct timespec start_time, stop_time;
31 static clockid_t clock_id;
32 
clock_name(clockid_t clk_id)33 static const char *clock_name(clockid_t clk_id)
34 {
35 	switch (clk_id) {
36 	case CLOCK_REALTIME:
37 		return "CLOCK_REALTIME";
38 	case CLOCK_REALTIME_COARSE:
39 		return "CLOCK_REALTIME_COARSE";
40 	case CLOCK_MONOTONIC:
41 		return "CLOCK_MONOTONIC";
42 	case CLOCK_MONOTONIC_COARSE:
43 		return "CLOCK_MONOTONIC_COARSE";
44 	case CLOCK_MONOTONIC_RAW:
45 		return "CLOCK_MONOTONIC_RAW";
46 	case CLOCK_BOOTTIME:
47 		return "CLOCK_BOOTTIME";
48 	case CLOCK_PROCESS_CPUTIME_ID:
49 		return "CLOCK_PROCESS_CPUTIME_ID";
50 	case CLOCK_THREAD_CPUTIME_ID:
51 		return "CLOCK_THREAD_CPUTIME_ID";
52 	default:
53 		return "UNKNOWN/INVALID";
54 	}
55 }
56 
tst_timer_check(clockid_t clk_id)57 void tst_timer_check(clockid_t clk_id)
58 {
59 	if (clock_gettime(clk_id, &start_time)) {
60 		if (errno == EINVAL) {
61 			tst_brkm(TCONF, NULL,
62 			         "Clock id %s(%u) not supported by kernel",
63 				 clock_name(clk_id), clk_id);
64 			return;
65 		}
66 
67 		tst_brkm(TBROK | TERRNO, NULL, "clock_gettime() failed");
68 	}
69 }
70 
tst_timer_start(clockid_t clk_id)71 void tst_timer_start(clockid_t clk_id)
72 {
73 	clock_id = clk_id;
74 
75 	if (clock_gettime(clock_id, &start_time))
76 		tst_resm(TWARN | TERRNO, "clock_gettime() failed");
77 }
78 
tst_timer_stop(void)79 void tst_timer_stop(void)
80 {
81 	if (clock_gettime(clock_id, &stop_time))
82 		tst_resm(TWARN | TERRNO, "clock_gettime() failed");
83 }
84 
tst_timer_elapsed(void)85 struct timespec tst_timer_elapsed(void)
86 {
87 	return tst_timespec_diff(stop_time, start_time);
88 }
89