• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2002 Andi Kleen
4  * Copyright (C) 2017 Cyril Hrubis <chrubis@suse.cz>
5  */
6 
7 /*
8  * DESCRIPTION
9  *	Check if gettimeofday is monotonous
10  *
11  * ALGORITHM
12  *	Call gettimeofday() to get a t1 (fist value)
13  *	call it again to get t2, see if t2 < t1, set t2 = t1, repeat for 10 sec
14  */
15 
16 #include <stdint.h>
17 #include <sys/time.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <time.h>
21 #include <errno.h>
22 
23 #include "tst_test.h"
24 #include "tst_timer.h"
25 #include "lapi/syscalls.h"
26 
27 static volatile sig_atomic_t done;
28 static char *str_rtime;
29 static int rtime = 10;
30 
breakout(int sig)31 static void breakout(int sig)
32 {
33 	done = sig;
34 }
35 
verify_gettimeofday(void)36 static void verify_gettimeofday(void)
37 {
38 	struct __kernel_old_timeval tv1, tv2;
39 	unsigned long long cnt = 0;
40 
41 	done = 0;
42 
43 	alarm(rtime);
44 
45 	if (tst_syscall(__NR_gettimeofday, &tv1, NULL))
46 		tst_brk(TFAIL | TERRNO, "gettimeofday() failed");
47 
48 	while (!done) {
49 		if (tst_syscall(__NR_gettimeofday, &tv2, NULL))
50 			tst_brk(TFAIL | TERRNO, "gettimeofday() failed");
51 
52 		if (tv2.tv_sec < tv1.tv_sec ||
53 		    (tv2.tv_sec == tv1.tv_sec && tv2.tv_usec < tv1.tv_usec)) {
54 			tst_res(TFAIL,
55 				"Time is going backwards: old %jd.%jd vs new %jd.%jd!",
56 				(intmax_t) tv1.tv_sec, (intmax_t) tv1.tv_usec,
57 				(intmax_t) tv2.tv_sec, (intmax_t) tv2.tv_usec);
58 			return;
59 		}
60 
61 		tv1 = tv2;
62 		cnt++;
63 	}
64 
65 	tst_res(TINFO, "gettimeofday() called %llu times", cnt);
66 	tst_res(TPASS, "gettimeofday() monotonous in %i seconds", rtime);
67 }
68 
setup(void)69 static void setup(void)
70 {
71 	if (str_rtime) {
72 		rtime = atoi(str_rtime);
73 		if (rtime <= 0)
74 			tst_brk(TBROK, "Invalid runtime '%s'", str_rtime);
75 		tst_set_timeout(rtime + 60);
76 	}
77 
78 	SAFE_SIGNAL(SIGALRM, breakout);
79 }
80 
81 static struct tst_test test = {
82 	.setup = setup,
83 	.options = (struct tst_option[]) {
84 		{"T:", &str_rtime, "Test iteration runtime in seconds"},
85 		{},
86 	},
87 	.test_all = verify_gettimeofday,
88 };
89