xref: /aosp_15_r20/external/ltp/lib/tst_wallclock.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2019 Linaro Limited. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Rafael David Tinoco <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
10*49cdfc7eSAndroid Build Coastguard Worker 
11*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
12*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_clocks.h"
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_rtctime.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_wallclock.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/posix_clocks.h"
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static struct timespec real_begin, mono_begin;
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static struct rtc_time rtc_begin;
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker static int clock_saved;
23*49cdfc7eSAndroid Build Coastguard Worker 
tst_wallclock_save(void)24*49cdfc7eSAndroid Build Coastguard Worker void tst_wallclock_save(void)
25*49cdfc7eSAndroid Build Coastguard Worker {
26*49cdfc7eSAndroid Build Coastguard Worker 	/* save initial monotonic time to restore it when needed */
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clock_gettime(CLOCK_REALTIME, &real_begin))
29*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tst_clock_gettime() realtime failed");
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin)) {
32*49cdfc7eSAndroid Build Coastguard Worker 		if (errno == EINVAL) {
33*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF | TERRNO,
34*49cdfc7eSAndroid Build Coastguard Worker 				"tst_clock_gettime() didn't support CLOCK_MONOTONIC_RAW");
35*49cdfc7eSAndroid Build Coastguard Worker 		}
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
38*49cdfc7eSAndroid Build Coastguard Worker 	}
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	clock_saved = 1;
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker 
tst_wallclock_restore(void)43*49cdfc7eSAndroid Build Coastguard Worker void tst_wallclock_restore(void)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	static const char *localtime = "/etc/localtime";
46*49cdfc7eSAndroid Build Coastguard Worker 	static struct timespec mono_end, elapsed, adjust;
47*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	if (!clock_saved)
50*49cdfc7eSAndroid Build Coastguard Worker 		return;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	clock_saved = 0;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end))
55*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	elapsed = tst_timespec_diff(mono_end, mono_begin);
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	adjust = tst_timespec_add(real_begin, elapsed);
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	/* restore realtime clock based on monotonic delta */
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clock_settime(CLOCK_REALTIME, &adjust))
64*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tst_clock_settime() realtime failed");
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	/*
67*49cdfc7eSAndroid Build Coastguard Worker 	 * Fix access time of /etc/localtime because adjusting the wallclock
68*49cdfc7eSAndroid Build Coastguard Worker 	 * might have changed it to a time value which lies far ahead
69*49cdfc7eSAndroid Build Coastguard Worker 	 * in the future.
70*49cdfc7eSAndroid Build Coastguard Worker 	 * The access time of a file only changes if the new one is past
71*49cdfc7eSAndroid Build Coastguard Worker 	 * the current one, therefore, just opening a file and reading it
72*49cdfc7eSAndroid Build Coastguard Worker 	 * might not be enough because the current access time might be far
73*49cdfc7eSAndroid Build Coastguard Worker 	 * in the future.
74*49cdfc7eSAndroid Build Coastguard Worker 	 */
75*49cdfc7eSAndroid Build Coastguard Worker 	ret = access(localtime, F_OK | W_OK);
76*49cdfc7eSAndroid Build Coastguard Worker 	if (!ret)
77*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_TOUCH(localtime, 0, NULL);
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker 
tst_rtc_clock_save(const char * rtc_dev)80*49cdfc7eSAndroid Build Coastguard Worker void tst_rtc_clock_save(const char *rtc_dev)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	/* save initial monotonic time to restore it when needed */
83*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_rtc_gettime(rtc_dev, &rtc_begin))
84*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tst_rtc_gettime() realtime failed");
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_begin))
87*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	clock_saved = 1;
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker 
tst_rtc_clock_restore(const char * rtc_dev)92*49cdfc7eSAndroid Build Coastguard Worker void tst_rtc_clock_restore(const char *rtc_dev)
93*49cdfc7eSAndroid Build Coastguard Worker {
94*49cdfc7eSAndroid Build Coastguard Worker 	static struct timespec mono_end, elapsed;
95*49cdfc7eSAndroid Build Coastguard Worker 	static struct timespec rtc_begin_tm, rtc_adjust;
96*49cdfc7eSAndroid Build Coastguard Worker 	static struct rtc_time rtc_restore;
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	if (!clock_saved)
99*49cdfc7eSAndroid Build Coastguard Worker 		return;
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	clock_saved = 0;
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_clock_gettime(CLOCK_MONOTONIC_RAW, &mono_end))
104*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tst_clock_gettime() monotonic failed");
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	elapsed = tst_timespec_diff(mono_end, mono_begin);
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	rtc_begin_tm.tv_nsec = 0;
109*49cdfc7eSAndroid Build Coastguard Worker 	rtc_begin_tm.tv_sec = tst_rtc_tm_to_time(&rtc_begin);
110*49cdfc7eSAndroid Build Coastguard Worker 
111*49cdfc7eSAndroid Build Coastguard Worker 	rtc_adjust = tst_timespec_add(rtc_begin_tm, elapsed);
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	tst_rtc_time_to_tm(rtc_adjust.tv_sec, &rtc_restore);
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 	/* restore realtime clock based on monotonic delta */
116*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_rtc_settime(rtc_dev, &rtc_restore))
117*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "tst_rtc_settime() realtime failed");
118*49cdfc7eSAndroid Build Coastguard Worker }
119