blob: 00bae884eb86614f3f17cb881cbbbbb3d1151513 [file] [log] [blame]
subrata_modakaf2c9ef2008-06-03 06:54:12 +00001/*
2 * timerfd() test by Davide Libenzi (test app for timerfd)
3 * Copyright (C) 2007 Davide Libenzi
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Davide Libenzi <davidel@xmailserver.org>
20 *
21 *
22 * $ gcc -o timerfd-test2 timerfd-test2.c -lrt
23 *
24 * NAME
25 * timerfd01.c
26 * HISTORY
27 * 28/05/2008 Initial contribution by Davide Libenzi <davidel@xmailserver.org>
28 * 28/05/2008 Integrated to LTP by Subrata Modak <subrata@linux.vnet.ibm.com>
29 */
30
31#define _GNU_SOURCE
32#include <sys/syscall.h>
33#include <sys/types.h>
34#include <sys/signal.h>
35#include <sys/time.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40#include <signal.h>
41#include <poll.h>
42#include <fcntl.h>
43#include <time.h>
44#include <errno.h>
45#include "test.h"
subrata_modakaf2c9ef2008-06-03 06:54:12 +000046#include "linux_syscall_numbers.h"
47
subrata_modak24fc2132008-11-11 05:57:38 +000048#define cleanup tst_exit
49
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020050char *TCID = "timerfd01";
subrata_modakaf2c9ef2008-06-03 06:54:12 +000051
52/*
53 * This were good at the time of 2.6.23-rc7 ...
subrata_modakea012ad2008-10-21 07:08:23 +000054 *
55 * #ifdef __NR_timerfd
56 *
57 * ... but is not now with 2.6.25
subrata_modakaf2c9ef2008-06-03 06:54:12 +000058 */
subrata_modakea012ad2008-10-21 07:08:23 +000059#ifdef __NR_timerfd_create
subrata_modakaf2c9ef2008-06-03 06:54:12 +000060
61/* Definitions from include/linux/timerfd.h */
62#define TFD_TIMER_ABSTIME (1 << 0)
63
subrata_modakaf2c9ef2008-06-03 06:54:12 +000064struct tmr_type {
65 int id;
66 char const *name;
67};
68
subrata_modak56207ce2009-03-23 13:35:39 +000069unsigned long long getustime(int clockid)
70{
subrata_modakaf2c9ef2008-06-03 06:54:12 +000071 struct timespec tp;
72
73 if (clock_gettime((clockid_t) clockid, &tp)) {
74 perror("clock_gettime");
75 return 0;
76 }
77
78 return 1000000ULL * tp.tv_sec + tp.tv_nsec / 1000;
79}
80
subrata_modak56207ce2009-03-23 13:35:39 +000081void set_timespec(struct timespec *tmr, unsigned long long ustime)
82{
subrata_modakaf2c9ef2008-06-03 06:54:12 +000083
84 tmr->tv_sec = (time_t) (ustime / 1000000ULL);
subrata_modak56207ce2009-03-23 13:35:39 +000085 tmr->tv_nsec = (long)(1000ULL * (ustime % 1000000ULL));
subrata_modakaf2c9ef2008-06-03 06:54:12 +000086}
87
subrata_modak56207ce2009-03-23 13:35:39 +000088int timerfd_create(int clockid, int flags)
89{
subrata_modakaf2c9ef2008-06-03 06:54:12 +000090
Jan Stancek359980f2013-02-15 10:16:05 +010091 return ltp_syscall(__NR_timerfd_create, clockid, flags);
subrata_modakaf2c9ef2008-06-03 06:54:12 +000092}
93
94int timerfd_settime(int ufc, int flags, const struct itimerspec *utmr,
subrata_modak56207ce2009-03-23 13:35:39 +000095 struct itimerspec *otmr)
96{
subrata_modakaf2c9ef2008-06-03 06:54:12 +000097
Jan Stancek359980f2013-02-15 10:16:05 +010098 return ltp_syscall(__NR_timerfd_settime, ufc, flags, utmr, otmr);
subrata_modakaf2c9ef2008-06-03 06:54:12 +000099}
100
subrata_modak56207ce2009-03-23 13:35:39 +0000101int timerfd_gettime(int ufc, struct itimerspec *otmr)
102{
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000103
Jan Stancek359980f2013-02-15 10:16:05 +0100104 return ltp_syscall(__NR_timerfd_gettime, ufc, otmr);
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000105}
106
subrata_modak56207ce2009-03-23 13:35:39 +0000107long waittmr(int tfd, int timeo)
108{
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000109 u_int64_t ticks;
110 struct pollfd pfd;
111
112 pfd.fd = tfd;
113 pfd.events = POLLIN;
114 pfd.revents = 0;
115 if (poll(&pfd, 1, timeo) < 0) {
116 perror("poll");
117 return -1;
118 }
119 if ((pfd.revents & POLLIN) == 0) {
120 fprintf(stdout, "no ticks happened\n");
121 return -1;
122 }
123 if (read(tfd, &ticks, sizeof(ticks)) != sizeof(ticks)) {
124 perror("timerfd read");
125 return -1;
126 }
127
128 return ticks;
129}
130
131int TST_TOTAL = 3;
132
subrata_modak56207ce2009-03-23 13:35:39 +0000133int main(int ac, char **av)
134{
subrata_modak424f2792008-11-11 04:55:32 +0000135 int i, tfd;
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000136 long ticks;
137 unsigned long long tnow, ttmr;
138 u_int64_t uticks;
139 struct itimerspec tmr;
140 struct tmr_type clks[] = {
subrata_modak56207ce2009-03-23 13:35:39 +0000141 {CLOCK_MONOTONIC, "CLOCK MONOTONIC"},
142 {CLOCK_REALTIME, "CLOCK REALTIME"},
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000143 };
144
subrata_modak56207ce2009-03-23 13:35:39 +0000145 if ((tst_kvercmp(2, 6, 25)) < 0) {
146 tst_resm(TCONF, "This test can only run on kernels that are ");
147 tst_resm(TCONF, "2.6.25 and higher");
148 exit(0);
149 }
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000150
151 for (i = 0; i < sizeof(clks) / sizeof(clks[0]); i++) {
subrata_modak56207ce2009-03-23 13:35:39 +0000152 fprintf(stdout,
153 "\n\n---------------------------------------\n");
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000154 fprintf(stdout, "| testing %s\n", clks[i].name);
155 fprintf(stdout, "---------------------------------------\n\n");
156
157 fprintf(stdout, "relative timer test (at 500 ms) ...\n");
158 set_timespec(&tmr.it_value, 500 * 1000);
159 set_timespec(&tmr.it_interval, 0);
160 tnow = getustime(clks[i].id);
161 if ((tfd = timerfd_create(clks[i].id, 0)) == -1) {
162 perror("timerfd");
163 return 1;
164 }
165 fprintf(stdout, "timerfd = %d\n", tfd);
166
167 if (timerfd_settime(tfd, 0, &tmr, NULL)) {
168 perror("timerfd_settime");
169 return 1;
170 }
171
172 fprintf(stdout, "wating timer ...\n");
173 ticks = waittmr(tfd, -1);
174 ttmr = getustime(clks[i].id);
175 if (ticks <= 0)
176 fprintf(stdout, "whooops! no timer showed up!\n");
177 else
178 fprintf(stdout, "got timer ticks (%ld) after %llu ms\n",
179 ticks, (ttmr - tnow) / 1000);
180
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000181 fprintf(stdout, "absolute timer test (at 500 ms) ...\n");
182 tnow = getustime(clks[i].id);
183 set_timespec(&tmr.it_value, tnow + 500 * 1000);
184 set_timespec(&tmr.it_interval, 0);
185 if (timerfd_settime(tfd, TFD_TIMER_ABSTIME, &tmr, NULL)) {
186 perror("timerfd_settime");
187 return 1;
188 }
189
190 fprintf(stdout, "wating timer ...\n");
191 ticks = waittmr(tfd, -1);
192 ttmr = getustime(clks[i].id);
193 if (ticks <= 0)
194 fprintf(stdout, "whooops! no timer showed up!\n");
195 else
196 fprintf(stdout, "got timer ticks (%ld) after %llu ms\n",
197 ticks, (ttmr - tnow) / 1000);
198
199 fprintf(stdout, "sequential timer test (100 ms clock) ...\n");
200 tnow = getustime(clks[i].id);
201 set_timespec(&tmr.it_value, tnow + 100 * 1000);
202 set_timespec(&tmr.it_interval, 100 * 1000);
203 if (timerfd_settime(tfd, TFD_TIMER_ABSTIME, &tmr, NULL)) {
204 perror("timerfd_settime");
205 return 1;
206 }
207
208 fprintf(stdout, "sleeping 1 second ...\n");
209 sleep(1);
210 if (timerfd_gettime(tfd, &tmr)) {
211 perror("timerfd_gettime");
212 return 1;
213 }
214 fprintf(stdout, "timerfd_gettime returned:\n"
215 "\tit_value = { %ld, %ld } it_interval = { %ld, %ld }\n",
subrata_modak56207ce2009-03-23 13:35:39 +0000216 (long)tmr.it_value.tv_sec, (long)tmr.it_value.tv_nsec,
217 (long)tmr.it_interval.tv_sec,
218 (long)tmr.it_interval.tv_nsec);
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000219 fprintf(stdout, "sleeping 1 second ...\n");
220 sleep(1);
221
222 fprintf(stdout, "wating timer ...\n");
223 ticks = waittmr(tfd, -1);
224 ttmr = getustime(clks[i].id);
225 if (ticks <= 0)
226 fprintf(stdout, "whooops! no timer showed up!\n");
227 else
228 fprintf(stdout, "got timer ticks (%ld) after %llu ms\n",
229 ticks, (ttmr - tnow) / 1000);
230
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000231 fprintf(stdout, "O_NONBLOCK test ...\n");
232 tnow = getustime(clks[i].id);
233 set_timespec(&tmr.it_value, 100 * 1000);
234 set_timespec(&tmr.it_interval, 0);
235 if (timerfd_settime(tfd, 0, &tmr, NULL)) {
236 perror("timerfd_settime");
237 return 1;
238 }
239 fprintf(stdout, "timerfd = %d\n", tfd);
240
241 fprintf(stdout, "wating timer (flush the single tick) ...\n");
242 ticks = waittmr(tfd, -1);
243 ttmr = getustime(clks[i].id);
244 if (ticks <= 0)
245 fprintf(stdout, "whooops! no timer showed up!\n");
246 else
247 fprintf(stdout, "got timer ticks (%ld) after %llu ms\n",
248 ticks, (ttmr - tnow) / 1000);
249
250 fcntl(tfd, F_SETFL, fcntl(tfd, F_GETFL, 0) | O_NONBLOCK);
251
252 if (read(tfd, &uticks, sizeof(uticks)) > 0)
subrata_modak56207ce2009-03-23 13:35:39 +0000253 fprintf(stdout,
254 "whooops! timer ticks not zero when should have been\n");
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000255 else if (errno != EAGAIN)
subrata_modak56207ce2009-03-23 13:35:39 +0000256 fprintf(stdout,
257 "whooops! bad errno value (%d = '%s')!\n",
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000258 errno, strerror(errno));
259 else
260 fprintf(stdout, "success\n");
261
262 fcntl(tfd, F_SETFL, fcntl(tfd, F_GETFL, 0) & ~O_NONBLOCK);
263
264 close(tfd);
265 }
266
Garrett Cooper2c282152010-12-16 00:55:50 -0800267 tst_exit();
subrata_modakaf2c9ef2008-06-03 06:54:12 +0000268}
269
subrata_modaka649d522008-06-04 10:30:57 +0000270#else
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200271int TST_TOTAL = 0;
subrata_modaka649d522008-06-04 10:30:57 +0000272
Mike Frysingerc57fba52014-04-09 18:56:30 -0400273int main(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000274{
subrata_modaka649d522008-06-04 10:30:57 +0000275
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100276 tst_brkm(TCONF, NULL,
277 "This test needs a kernel that has timerfd syscall.");
subrata_modaka649d522008-06-04 10:30:57 +0000278}
Chris Dearmanec6edca2012-10-17 19:54:01 -0700279#endif