xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/timerfd/timerfd01.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  *  timerfd() test by Davide Libenzi (test app for timerfd)
4*49cdfc7eSAndroid Build Coastguard Worker  *  Copyright (C) 2007  Davide Libenzi
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  *  Davide Libenzi <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  *  Description:
9*49cdfc7eSAndroid Build Coastguard Worker  *	Test timerfd with the flags:
10*49cdfc7eSAndroid Build Coastguard Worker  *		1) CLOCK_MONOTONIC
11*49cdfc7eSAndroid Build Coastguard Worker  *		2) CLOCK_REALTIME
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
14*49cdfc7eSAndroid Build Coastguard Worker  *  28/05/2008 Initial contribution by Davide Libenzi <[email protected]>
15*49cdfc7eSAndroid Build Coastguard Worker  *  28/05/2008 Integrated to LTP by Subrata Modak <[email protected]>
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
19*49cdfc7eSAndroid Build Coastguard Worker #include <poll.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "time64_variants.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_timer.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_timerfd.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
25*49cdfc7eSAndroid Build Coastguard Worker 	int id;
26*49cdfc7eSAndroid Build Coastguard Worker 	char const *name;
27*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
28*49cdfc7eSAndroid Build Coastguard Worker 	{CLOCK_MONOTONIC, "CLOCK MONOTONIC"},
29*49cdfc7eSAndroid Build Coastguard Worker 	{CLOCK_REALTIME, "CLOCK REALTIME"},
30*49cdfc7eSAndroid Build Coastguard Worker };
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static struct time64_variants variants[] = {
33*49cdfc7eSAndroid Build Coastguard Worker #if (__NR_timerfd_gettime != __LTP__NR_INVALID_SYSCALL)
34*49cdfc7eSAndroid Build Coastguard Worker 	{ .clock_gettime = sys_clock_gettime, .tfd_gettime = sys_timerfd_gettime, .tfd_settime = sys_timerfd_settime, .ts_type = TST_KERN_OLD_TIMESPEC, .desc = "syscall with old kernel spec"},
35*49cdfc7eSAndroid Build Coastguard Worker #endif
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker #if (__NR_timerfd_gettime64 != __LTP__NR_INVALID_SYSCALL)
38*49cdfc7eSAndroid Build Coastguard Worker 	{ .clock_gettime = sys_clock_gettime64, .tfd_gettime = sys_timerfd_gettime64, .tfd_settime = sys_timerfd_settime64, .ts_type = TST_KERN_TIMESPEC, .desc = "syscall time64 with kernel spec"},
39*49cdfc7eSAndroid Build Coastguard Worker #endif
40*49cdfc7eSAndroid Build Coastguard Worker };
41*49cdfc7eSAndroid Build Coastguard Worker 
getustime(int clockid)42*49cdfc7eSAndroid Build Coastguard Worker static unsigned long long getustime(int clockid)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	struct time64_variants *tv = &variants[tst_variant];
45*49cdfc7eSAndroid Build Coastguard Worker 	struct tst_ts tp = {.type = tv->ts_type, };
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	if (tv->clock_gettime((clockid_t) clockid, tst_ts_get(&tp))) {
48*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "clock_gettime() failed");
49*49cdfc7eSAndroid Build Coastguard Worker 		return 0;
50*49cdfc7eSAndroid Build Coastguard Worker 	}
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	return 1000000ULL * tst_ts_get_sec(tp) + tst_ts_get_nsec(tp) / 1000;
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker 
settime(int tfd,struct tst_its * tmr,int tflags,unsigned long long tvalue,int tinterval)55*49cdfc7eSAndroid Build Coastguard Worker static void settime(int tfd, struct tst_its *tmr, int tflags,
56*49cdfc7eSAndroid Build Coastguard Worker                     unsigned long long tvalue, int tinterval)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker 	struct time64_variants *tv = &variants[tst_variant];
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	tst_its_set_value_from_us(tmr, tvalue);
61*49cdfc7eSAndroid Build Coastguard Worker 	tst_its_set_interval_from_us(tmr, tinterval);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	if (tv->tfd_settime(tfd, tflags, tst_its_get(tmr), NULL))
64*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "timerfd_settime() failed");
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
waittmr(int tfd,unsigned int exp_ticks)67*49cdfc7eSAndroid Build Coastguard Worker static void waittmr(int tfd, unsigned int exp_ticks)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker 	uint64_t ticks;
70*49cdfc7eSAndroid Build Coastguard Worker 	struct pollfd pfd;
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	pfd.fd = tfd;
73*49cdfc7eSAndroid Build Coastguard Worker 	pfd.events = POLLIN;
74*49cdfc7eSAndroid Build Coastguard Worker 	pfd.revents = 0;
75*49cdfc7eSAndroid Build Coastguard Worker 	if (poll(&pfd, 1, -1) < 0) {
76*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "poll() failed");
77*49cdfc7eSAndroid Build Coastguard Worker 		return;
78*49cdfc7eSAndroid Build Coastguard Worker 	}
79*49cdfc7eSAndroid Build Coastguard Worker 	if ((pfd.revents & POLLIN) == 0) {
80*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "no ticks happened");
81*49cdfc7eSAndroid Build Coastguard Worker 		return;
82*49cdfc7eSAndroid Build Coastguard Worker 	}
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_READ(0, tfd, &ticks, sizeof(ticks));
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	if (ticks != exp_ticks) {
86*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "got %u tick(s) expected %u",
87*49cdfc7eSAndroid Build Coastguard Worker 		        (unsigned int)ticks, exp_ticks);
88*49cdfc7eSAndroid Build Coastguard Worker 	} else {
89*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "got %u tick(s)", exp_ticks);
90*49cdfc7eSAndroid Build Coastguard Worker 	}
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)93*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
94*49cdfc7eSAndroid Build Coastguard Worker {
95*49cdfc7eSAndroid Build Coastguard Worker 	struct time64_variants *tv = &variants[tst_variant];
96*49cdfc7eSAndroid Build Coastguard Worker 	int tfd;
97*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long long tnow;
98*49cdfc7eSAndroid Build Coastguard Worker 	uint64_t uticks;
99*49cdfc7eSAndroid Build Coastguard Worker 	struct tst_its tmr = {.type = tv->ts_type, };
100*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *clks = &tcases[n];
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "testing %s", clks->name);
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	tfd = SAFE_TIMERFD_CREATE(clks->id, 0);
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "relative timer (100 ms)");
107*49cdfc7eSAndroid Build Coastguard Worker 	settime(tfd, &tmr, 0, 100 * 1000, 0);
108*49cdfc7eSAndroid Build Coastguard Worker 	waittmr(tfd, 1);
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "absolute timer (100 ms)");
111*49cdfc7eSAndroid Build Coastguard Worker 	tnow = getustime(clks->id);
112*49cdfc7eSAndroid Build Coastguard Worker 	settime(tfd, &tmr, TFD_TIMER_ABSTIME, tnow + 100 * 1000, 0);
113*49cdfc7eSAndroid Build Coastguard Worker 	waittmr(tfd, 1);
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "sequential timer (50 ms)");
116*49cdfc7eSAndroid Build Coastguard Worker 	tnow = getustime(clks->id);
117*49cdfc7eSAndroid Build Coastguard Worker 	settime(tfd, &tmr, TFD_TIMER_ABSTIME, tnow + 50 * 1000, 50 * 1000);
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	memset(&tmr, 0, sizeof(tmr));
120*49cdfc7eSAndroid Build Coastguard Worker 	tmr.type = tv->ts_type;
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 	if (tv->tfd_gettime(tfd, tst_its_get(&tmr)))
123*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "timerfd_gettime() failed");
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_its_get_value_sec(tmr) != 0 || tst_its_get_value_nsec(tmr) > 50 * 1000000)
126*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Timer read back value not relative");
127*49cdfc7eSAndroid Build Coastguard Worker 	else
128*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Timer read back value is relative");
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker 	usleep(160000);
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker 	waittmr(tfd, 3);
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "testing with O_NONBLOCK");
135*49cdfc7eSAndroid Build Coastguard Worker 	settime(tfd, &tmr, 0, 100 * 1000, 0);
136*49cdfc7eSAndroid Build Coastguard Worker 	waittmr(tfd, 1);
137*49cdfc7eSAndroid Build Coastguard Worker 
138*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FCNTL(tfd, F_SETFL, fcntl(tfd, F_GETFL, 0) | O_NONBLOCK);
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	TEST(read(tfd, &uticks, sizeof(uticks)));
141*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET > 0)
142*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "timer ticks not zero");
143*49cdfc7eSAndroid Build Coastguard Worker 	else if (TST_ERR != EAGAIN)
144*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "read should fail with EAGAIN got");
145*49cdfc7eSAndroid Build Coastguard Worker 	else
146*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TERRNO, "read failed with");
147*49cdfc7eSAndroid Build Coastguard Worker 
148*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(tfd);
149*49cdfc7eSAndroid Build Coastguard Worker }
150*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)151*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
152*49cdfc7eSAndroid Build Coastguard Worker {
153*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Testing variant: %s", variants[tst_variant].desc);
154*49cdfc7eSAndroid Build Coastguard Worker }
155*49cdfc7eSAndroid Build Coastguard Worker 
156*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
157*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
158*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
159*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = ARRAY_SIZE(variants),
160*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
161*49cdfc7eSAndroid Build Coastguard Worker };
162