1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/** Test Valgrind's interception of the Linux syscalls timerfd_create(),
* timerfd_gettime() and timerfd_settime().
*
* This is a modified version of
* timerfd-test2 by Davide Libenzi (test app for timerfd)
* Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
* Modified for inclusion in Valgrind.
* Copyright (C) 2008 Bart Van Assche <bvanassche@acm.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* See also http://www.xmailserver.org/timerfd-test2.c
*/
#define _GNU_SOURCE
#include "../../../config.h"
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#if defined(HAVE_SYS_SIGNAL_H)
#include <sys/signal.h>
#endif
#if defined(HAVE_SYS_SYSCALL_H)
#include <sys/syscall.h>
#endif
#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif
#if defined(HAVE_SYS_TYPES_H)
#include <sys/types.h>
#endif
/*
* timerfd_* system call numbers introduced in 2.6.23. These constants are
* not yet in the glibc 2.7 headers, that is why they are defined here.
*/
#ifndef __NR_timerfd_create
#if defined(__x86_64__)
#define __NR_timerfd_create 283
#elif defined(__i386__)
#define __NR_timerfd_create 322
#elif defined(__powerpc__)
#define __NR_timerfd_create 306
#elif defined(__s390x__)
#define __NR_timerfd_create 319
#else
#error Cannot detect your architecture!
#endif
#endif
#ifndef __NR_timerfd_settime
#if defined(__x86_64__)
#define __NR_timerfd_settime 286
#define __NR_timerfd_gettime 287
#elif defined(__i386__)
#define __NR_timerfd_settime 325
#define __NR_timerfd_gettime 326
#elif defined(__powerpc__)
#define __NR_timerfd_settime 311
#define __NR_timerfd_gettime 312
#elif defined(__s390x__)
#define __NR_timerfd_settime 320
#define __NR_timerfd_gettime 321
#else
#error Cannot detect your architecture!
#endif
#endif
/* Definitions from include/linux/timerfd.h */
#define TFD_TIMER_ABSTIME (1 << 0)
struct tmr_type
{
int id;
char const *name;
};
#if defined(HAVE_CLOCK_GETTIME)
unsigned long long getustime(int clockid)
{
struct timespec tp;
if (clock_gettime((clockid_t) clockid, &tp))
{
perror("clock_gettime");
return 0;
}
return 1000000ULL * tp.tv_sec + tp.tv_nsec / 1000;
}
#else
unsigned long long getustime(int clockid)
{
fprintf(stderr, "error: clock_gettime\n");
return 0;
}
#endif
void set_timespec(struct timespec *tmr, unsigned long long ustime)
{
tmr->tv_sec = (time_t) (ustime / 1000000ULL);
tmr->tv_nsec = (long) (1000ULL * (ustime % 1000000ULL));
}
int timerfd_create(int clockid, int flags)
{
return syscall(__NR_timerfd_create, clockid, flags);
}
int timerfd_settime(int ufc, int flags, const struct itimerspec *utmr,
struct itimerspec *otmr)
{
return syscall(__NR_timerfd_settime, ufc, flags, utmr, otmr);
}
int timerfd_gettime(int ufc, struct itimerspec *otmr)
{
return syscall(__NR_timerfd_gettime, ufc, otmr);
}
long waittmr(int tfd, int timeo)
{
u_int64_t ticks;
struct pollfd pfd;
pfd.fd = tfd;
pfd.events = POLLIN;
pfd.revents = 0;
if (poll(&pfd, 1, timeo) < 0)
{
perror("poll");
return -1;
}
if ((pfd.revents & POLLIN) == 0)
{
fprintf(stderr, "no ticks happened\n");
return -1;
}
if (read(tfd, &ticks, sizeof(ticks)) != sizeof(ticks))
{
perror("timerfd read");
return -1;
}
return ticks;
}
int main(int ac, char **av)
{
int i, tfd;
long ticks;
unsigned long long tnow, ttmr;
u_int64_t uticks;
struct itimerspec tmr;
struct tmr_type clks[] =
{
#if defined(HAVE_CLOCK_MONOTONIC)
{ CLOCK_MONOTONIC, "CLOCK MONOTONIC" },
#endif
{ CLOCK_REALTIME, "CLOCK REALTIME" },
};
for (i = 0; i < sizeof(clks) / sizeof(clks[0]); i++)
{
fprintf(stderr, "\n\n---------------------------------------\n");
fprintf(stderr, "| testing %s\n", clks[i].name);
fprintf(stderr, "---------------------------------------\n\n");
fprintf(stderr, "relative timer test (at 500 ms) ...\n");
set_timespec(&tmr.it_value, 500 * 1000);
set_timespec(&tmr.it_interval, 0);
tnow = getustime(clks[i].id);
if ((tfd = timerfd_create(clks[i].id, 0)) == -1)
{
perror("timerfd_create");
return 1;
}
if (timerfd_settime(tfd, 0, &tmr, NULL))
{
perror("timerfd_settime");
return 1;
}
fprintf(stderr, "waiting timer ...\n");
ticks = waittmr(tfd, -1);
ttmr = getustime(clks[i].id);
if (ticks <= 0)
fprintf(stderr, "whooops! no timer showed up!\n");
else
fprintf(stderr, "got timer ticks (%ld) after %.1f s\n",
ticks, (ttmr - tnow) * 1e-6);
fprintf(stderr, "absolute timer test (at 500 ms) ...\n");
tnow = getustime(clks[i].id);
set_timespec(&tmr.it_value, tnow + 500 * 1000);
set_timespec(&tmr.it_interval, 0);
if (timerfd_settime(tfd, TFD_TIMER_ABSTIME, &tmr, NULL))
{
perror("timerfd_settime");
return 1;
}
fprintf(stderr, "waiting timer ...\n");
ticks = waittmr(tfd, -1);
ttmr = getustime(clks[i].id);
if (ticks <= 0)
fprintf(stderr, "whooops! no timer showed up!\n");
else
fprintf(stderr, "got timer ticks (%ld) after %.1f s\n",
ticks, (ttmr - tnow) * 1e-6);
fprintf(stderr, "sequential timer test (100 ms clock) ...\n");
tnow = getustime(clks[i].id);
set_timespec(&tmr.it_value, tnow + 100 * 1000);
set_timespec(&tmr.it_interval, 100 * 1000);
if (timerfd_settime(tfd, TFD_TIMER_ABSTIME, &tmr, NULL))
{
perror("timerfd_settime");
return 1;
}
fprintf(stderr, "sleeping one second ...\n");
sleep(1);
if (timerfd_gettime(tfd, &tmr))
{
perror("timerfd_gettime");
return 1;
}
fprintf(stderr, "timerfd_gettime returned:\n"
"\tit_value = %.1f it_interval = %.1f\n",
tmr.it_value.tv_sec + 1e-9 * tmr.it_value.tv_nsec,
tmr.it_interval.tv_sec + 1e-9 * tmr.it_interval.tv_nsec);
fprintf(stderr, "sleeping 1 second ...\n");
sleep(1);
fprintf(stderr, "waiting timer ...\n");
ticks = waittmr(tfd, -1);
ttmr = getustime(clks[i].id);
if (ticks <= 0)
fprintf(stderr, "whooops! no timer showed up!\n");
else
{
const double delta = (ttmr - tnow) * 1e-6;
if (1.9 < delta && delta < 2.1)
fprintf(stderr, "got timer ticks (%ld) after about 2s\n", ticks);
else
fprintf(stderr, "got timer ticks (%ld) after %.1f s\n", ticks, delta);
}
fprintf(stderr, "O_NONBLOCK test ...\n");
tnow = getustime(clks[i].id);
set_timespec(&tmr.it_value, 100 * 1000);
set_timespec(&tmr.it_interval, 0);
if (timerfd_settime(tfd, 0, &tmr, NULL))
{
perror("timerfd_settime");
return 1;
}
#if 0
fprintf(stderr, "timerfd = %d\n", tfd);
#endif
fprintf(stderr, "waiting timer (flush the single tick) ...\n");
ticks = waittmr(tfd, -1);
ttmr = getustime(clks[i].id);
if (ticks <= 0)
fprintf(stderr, "whooops! no timer showed up!\n");
else
fprintf(stderr, "got timer ticks (%ld) after %.1f s\n",
ticks, (ttmr - tnow) * 1e-6);
fcntl(tfd, F_SETFL, fcntl(tfd, F_GETFL, 0) | O_NONBLOCK);
if (read(tfd, &uticks, sizeof(uticks)) > 0)
fprintf(stderr, "whooops! timer ticks not zero when should have been\n");
else if (errno != EAGAIN)
fprintf(stderr, "whooops! bad errno value (%d = '%s')!\n",
errno, strerror(errno));
else
fprintf(stderr, "success\n");
fcntl(tfd, F_SETFL, fcntl(tfd, F_GETFL, 0) & ~O_NONBLOCK);
close(tfd);
}
return 0;
}
|