]>
Commit | Line | Data |
---|---|---|
45b81c9c MK |
1 | .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com> |
2 | .\" | |
f0008367 | 3 | .\" %%%LICENSE_START(GPLv2+_SW_3_PARA) |
45b81c9c MK |
4 | .\" This program is free software; you can redistribute it and/or modify |
5 | .\" it under the terms of the GNU General Public License as published by | |
6 | .\" the Free Software Foundation; either version 2 of the License, or | |
7 | .\" (at your option) any later version. | |
8 | .\" | |
9 | .\" This program is distributed in the hope that it will be useful, | |
10 | .\" but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | .\" GNU General Public License for more details. | |
13 | .\" | |
68fa4398 MK |
14 | .\" You should have received a copy of the GNU General Public |
15 | .\" License along with this manual; if not, see | |
16 | .\" <http://www.gnu.org/licenses/>. | |
8ff7380d | 17 | .\" %%%LICENSE_END |
45b81c9c | 18 | .\" |
bea08fec MK |
19 | .\" FIXME Linux 3.0: timerfd_settime() adds a TFD_TIMER_CANCEL_ON_SET flag; |
20 | .\" This flag needs to documented. | |
d09de287 | 21 | .\" |
9ba01802 | 22 | .TH TIMERFD_CREATE 2 2019-03-06 Linux "Linux Programmer's Manual" |
45b81c9c MK |
23 | .SH NAME |
24 | timerfd_create, timerfd_settime, timerfd_gettime \- | |
25 | timers that notify via file descriptors | |
26 | .SH SYNOPSIS | |
45b81c9c MK |
27 | .nf |
28 | .B #include <sys/timerfd.h> | |
68e4db0a | 29 | .PP |
45b81c9c | 30 | .BI "int timerfd_create(int " clockid ", int " flags ); |
68e4db0a | 31 | .PP |
45b81c9c MK |
32 | .BI "int timerfd_settime(int " fd ", int " flags , |
33 | .BI " const struct itimerspec *" new_value , | |
5c902597 | 34 | .BI " struct itimerspec *" old_value ); |
68e4db0a | 35 | .PP |
45b81c9c MK |
36 | .BI "int timerfd_gettime(int " fd ", struct itimerspec *" curr_value ); |
37 | .fi | |
38 | .SH DESCRIPTION | |
39 | These system calls create and operate on a timer | |
40 | that delivers timer expiration notifications via a file descriptor. | |
41 | They provide an alternative to the use of | |
42 | .BR setitimer (2) | |
43 | or | |
804f03e6 | 44 | .BR timer_create (2), |
45b81c9c MK |
45 | with the advantage that the file descriptor may be monitored by |
46 | .BR select (2), | |
47 | .BR poll (2), | |
48 | and | |
49 | .BR epoll (7). | |
efeece04 | 50 | .PP |
45b81c9c | 51 | The use of these three system calls is analogous to the use of |
804f03e6 MK |
52 | .BR timer_create (2), |
53 | .BR timer_settime (2), | |
45b81c9c | 54 | and |
804f03e6 | 55 | .BR timer_gettime (2). |
45b81c9c | 56 | (There is no analog of |
804f03e6 | 57 | .BR timer_getoverrun (2), |
45b81c9c MK |
58 | since that functionality is provided by |
59 | .BR read (2), | |
60 | as described below.) | |
61 | .\" | |
62 | .SS timerfd_create() | |
63 | .BR timerfd_create () | |
64 | creates a new timer object, | |
65 | and returns a file descriptor that refers to that timer. | |
66 | The | |
67 | .I clockid | |
68 | argument specifies the clock that is used to mark the progress | |
205b1bce | 69 | of the timer, and must be one of the following: |
4b76fffb | 70 | .TP |
45b81c9c | 71 | .B CLOCK_REALTIME |
4b76fffb MK |
72 | A settable system-wide real-time clock. |
73 | .TP | |
45b81c9c | 74 | .B CLOCK_MONOTONIC |
4b76fffb MK |
75 | A nonsettable monotonically increasing clock that measures time |
76 | from some unspecified point in the past that does not change | |
77 | after system startup. | |
4c747119 MK |
78 | .TP |
79 | .BR CLOCK_BOOTTIME " (Since Linux 3.15)" | |
80 | .\" commit 4a2378a943f09907fb1ae35c15de917f60289c14 | |
81 | Like | |
82 | .BR CLOCK_MONOTONIC , | |
83 | this is a monotonically increasing clock. | |
84 | However, whereas the | |
85 | .BR CLOCK_MONOTONIC | |
86 | clock does not measure the time while a system is suspended, the | |
87 | .BR CLOCK_BOOTTIME | |
88 | clock does include the time during which the system is suspended. | |
89 | This is useful for applications that need to be suspend-aware. | |
90 | .BR CLOCK_REALTIME | |
91 | is not suitable for such applications, since that clock is affected | |
92 | by discontinuous changes to the system clock. | |
93 | .TP | |
94 | .BR CLOCK_REALTIME_ALARM " (since Linux 3.11)" | |
95 | .\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8 | |
96 | This clock is like | |
97 | .BR CLOCK_REALTIME , | |
98 | but will wake the system if it is suspended. | |
99 | The caller must have the | |
100 | .B CAP_WAKE_ALARM | |
101 | capability in order to set a timer against this clock. | |
102 | .TP | |
103 | .BR CLOCK_BOOTTIME_ALARM " (since Linux 3.11)" | |
104 | .\" commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8 | |
105 | This clock is like | |
106 | .BR CLOCK_BOOTTIME , | |
107 | but will wake the system if it is suspended. | |
108 | The caller must have the | |
109 | .B CAP_WAKE_ALARM | |
110 | capability in order to set a timer against this clock. | |
4b76fffb | 111 | .PP |
45b81c9c | 112 | The current value of each of these clocks can be retrieved using |
0eb44391 | 113 | .BR clock_gettime (2). |
efeece04 | 114 | .PP |
3df1b973 MK |
115 | Starting with Linux 2.6.27, the following values may be bitwise ORed in |
116 | .IR flags | |
117 | to change the behavior of | |
118 | .BR timerfd_create (): | |
119 | .TP 14 | |
120 | .B TFD_NONBLOCK | |
121 | Set the | |
122 | .BR O_NONBLOCK | |
7f11e32c MK |
123 | file status flag on the open file description (see |
124 | .BR open (2)) | |
125 | referred to by the new file descriptor. | |
3df1b973 | 126 | Using this flag saves extra calls to |
ef7db4f5 | 127 | .BR fcntl (2) |
3df1b973 MK |
128 | to achieve the same result. |
129 | .TP | |
130 | .B TFD_CLOEXEC | |
131 | Set the close-on-exec | |
132 | .RB ( FD_CLOEXEC ) | |
133 | flag on the new file descriptor. | |
c5571b61 | 134 | See the description of the |
3df1b973 MK |
135 | .B O_CLOEXEC |
136 | flag in | |
137 | .BR open (2) | |
138 | for reasons why this may be useful. | |
139 | .PP | |
140 | In Linux versions up to and including 2.6.26, | |
45b81c9c | 141 | .I flags |
3df1b973 | 142 | must be specified as zero. |
45b81c9c MK |
143 | .SS timerfd_settime() |
144 | .BR timerfd_settime () | |
145 | arms (starts) or disarms (stops) | |
146 | the timer referred to by the file descriptor | |
147 | .IR fd . | |
efeece04 | 148 | .PP |
45b81c9c MK |
149 | The |
150 | .I new_value | |
151 | argument specifies the initial expiration and interval for the timer. | |
152 | The | |
251ebc1e | 153 | .I itimerspec |
45b81c9c MK |
154 | structure used for this argument contains two fields, |
155 | each of which is in turn a structure of type | |
156 | .IR timespec : | |
e646a1ba | 157 | .PP |
45b81c9c | 158 | .in +4n |
e646a1ba | 159 | .EX |
45b81c9c MK |
160 | struct timespec { |
161 | time_t tv_sec; /* Seconds */ | |
162 | long tv_nsec; /* Nanoseconds */ | |
163 | }; | |
164 | ||
165 | struct itimerspec { | |
166 | struct timespec it_interval; /* Interval for periodic timer */ | |
167 | struct timespec it_value; /* Initial expiration */ | |
168 | }; | |
b8302363 | 169 | .EE |
45b81c9c MK |
170 | .in |
171 | .PP | |
172 | .I new_value.it_value | |
173 | specifies the initial expiration of the timer, | |
174 | in seconds and nanoseconds. | |
175 | Setting either field of | |
176 | .I new_value.it_value | |
c7094399 | 177 | to a nonzero value arms the timer. |
45b81c9c MK |
178 | Setting both fields of |
179 | .I new_value.it_value | |
180 | to zero disarms the timer. | |
efeece04 | 181 | .PP |
45b81c9c MK |
182 | Setting one or both fields of |
183 | .I new_value.it_interval | |
c7094399 | 184 | to nonzero values specifies the period, in seconds and nanoseconds, |
45b81c9c MK |
185 | for repeated timer expirations after the initial expiration. |
186 | If both fields of | |
187 | .I new_value.it_interval | |
188 | are zero, the timer expires just once, at the time specified by | |
189 | .IR new_value.it_value . | |
efeece04 | 190 | .PP |
51062576 MK |
191 | By default, |
192 | the initial expiration time specified in | |
193 | .I new_value | |
194 | is interpreted relative to the current time | |
195 | on the timer's clock at the time of the call (i.e., | |
196 | .I new_value.it_value | |
197 | specifies a time relative to the current value of the clock specified by | |
198 | .IR clockid ). | |
199 | An absolute timeout can be selected via the | |
200 | .I flags | |
201 | argument. | |
efeece04 | 202 | .PP |
45b81c9c MK |
203 | The |
204 | .I flags | |
51062576 MK |
205 | argument is a bit mask that can include the following values: |
206 | .TP | |
207 | .B TFD_TIMER_ABSTIME | |
208 | Interpret | |
209 | .I new_value.it_value | |
210 | as an absolute value on the timer's clock. | |
211 | The timer will expire when the value of the timer's | |
45b81c9c | 212 | clock reaches the value specified in |
51062576 | 213 | .IR new_value.it_value . |
e9477548 MK |
214 | .TP |
215 | .BR TFD_TIMER_CANCEL_ON_SET | |
216 | If this flag is specified along with | |
217 | .B TFD_TIMER_ABSTIME | |
218 | and the clock for this timer is | |
219 | .BR CLOCK_REALTIME | |
220 | or | |
221 | .BR CLOCK_REALTIME_ALARM , | |
222 | then mark this timer as cancelable if the real-time clock | |
223 | undergoes a discontinuous change | |
224 | .RB ( settimeofday (2), | |
225 | .BR clock_settime (2), | |
226 | or similar). | |
227 | When such changes occur, a current or future | |
228 | .BR read (2) | |
229 | from the file descriptor will fail with the error | |
230 | .BR ECANCELED . | |
51062576 | 231 | .PP |
009cdc22 | 232 | If the |
5c902597 | 233 | .I old_value |
009cdc22 MK |
234 | argument is not NULL, then the |
235 | .I itimerspec | |
236 | structure that it points to is used to return the setting of the timer | |
237 | that was current at the time of the call; | |
238 | see the description of | |
45b81c9c MK |
239 | .BR timerfd_gettime () |
240 | following. | |
241 | .\" | |
242 | .SS timerfd_gettime() | |
243 | .BR timerfd_gettime () | |
244 | returns, in | |
245 | .IR curr_value , | |
246 | an | |
247 | .IR itimerspec | |
dc55661b | 248 | structure that contains the current setting of the timer |
45b81c9c MK |
249 | referred to by the file descriptor |
250 | .IR fd . | |
efeece04 | 251 | .PP |
45b81c9c MK |
252 | The |
253 | .I it_value | |
254 | field returns the amount of time | |
255 | until the timer will next expire. | |
256 | If both fields of this structure are zero, | |
257 | then the timer is currently disarmed. | |
258 | This field always contains a relative value, regardless of whether the | |
259 | .BR TFD_TIMER_ABSTIME | |
260 | flag was specified when setting the timer. | |
efeece04 | 261 | .PP |
45b81c9c MK |
262 | The |
263 | .I it_interval | |
264 | field returns the interval of the timer. | |
265 | If both fields of this structure are zero, | |
266 | then the timer is set to expire just once, at the time specified by | |
267 | .IR curr_value.it_value . | |
268 | .SS Operating on a timer file descriptor | |
269 | The file descriptor returned by | |
270 | .BR timerfd_create () | |
271 | supports the following operations: | |
272 | .TP | |
273 | .BR read (2) | |
274 | If the timer has already expired one or more times since | |
275 | its settings were last modified using | |
276 | .BR timerfd_settime (), | |
277 | or since the last successful | |
278 | .BR read (2), | |
279 | then the buffer given to | |
280 | .BR read (2) | |
281 | returns an unsigned 8-byte integer | |
282 | .RI ( uint64_t ) | |
283 | containing the number of expirations that have occurred. | |
88879aeb MK |
284 | (The returned value is in host byte order\(emthat is, |
285 | the native byte order for integers on the host machine.) | |
45b81c9c MK |
286 | .IP |
287 | If no timer expirations have occurred at the time of the | |
288 | .BR read (2), | |
289 | then the call either blocks until the next timer expiration, | |
290 | or fails with the error | |
291 | .B EAGAIN | |
ff40dbb3 | 292 | if the file descriptor has been made nonblocking |
45b81c9c MK |
293 | (via the use of the |
294 | .BR fcntl (2) | |
295 | .B F_SETFL | |
296 | operation to set the | |
297 | .B O_NONBLOCK | |
298 | flag). | |
299 | .IP | |
300 | A | |
301 | .BR read (2) | |
26cd31fd | 302 | fails with the error |
45b81c9c MK |
303 | .B EINVAL |
304 | if the size of the supplied buffer is less than 8 bytes. | |
efeece04 | 305 | .IP |
e9477548 MK |
306 | If the associated clock is either |
307 | .BR CLOCK_REALTIME | |
308 | or | |
309 | .BR CLOCK_REALTIME_ALARM , | |
310 | the timer is absolute | |
311 | .RB ( TFD_TIMER_ABSTIME ), | |
312 | and the flag | |
313 | .BR TFD_TIMER_CANCEL_ON_SET | |
314 | was specified when calling | |
315 | .BR timerfd_settime (), | |
316 | then | |
317 | .BR read (2) | |
26cd31fd | 318 | fails with the error |
e9477548 MK |
319 | .BR ECANCELED |
320 | if the real-time clock undergoes a discontinuous change. | |
321 | (This allows the reading application to discover | |
322 | such discontinuous changes to the clock.) | |
45b81c9c MK |
323 | .TP |
324 | .BR poll "(2), " select "(2) (and similar)" | |
325 | The file descriptor is readable | |
326 | (the | |
327 | .BR select (2) | |
328 | .I readfds | |
329 | argument; the | |
330 | .BR poll (2) | |
331 | .B POLLIN | |
332 | flag) | |
333 | if one or more timer expirations have occurred. | |
334 | .IP | |
335 | The file descriptor also supports the other file-descriptor | |
336 | multiplexing APIs: | |
337 | .BR pselect (2), | |
338 | .BR ppoll (2), | |
339 | and | |
340 | .BR epoll (7). | |
341 | .TP | |
6614e292 | 342 | .BR ioctl (2) |
80d27367 MK |
343 | The following timerfd-specific command is supported: |
344 | .RS | |
345 | .TP | |
40f6b649 MK |
346 | .BR TFD_IOC_SET_TICKS " (since Linux 3.17)" |
347 | .\" commit 5442e9fbd7c23172a1c9bc736629cd123a9923f0 | |
80d27367 MK |
348 | Adjust the number of timer expirations that have occurred. |
349 | The argument is a pointer to a nonzero 8-byte integer | |
5a3eb9cd CG |
350 | .RI ( uint64_t *) |
351 | containing the new number of expirations. | |
80d27367 | 352 | Once the number is set, any waiter on the timer is woken up. |
5a3eb9cd | 353 | The only purpose of this command is to restore the expirations |
80d27367 MK |
354 | for the purpose of checkpoint/restore. |
355 | This operation is available only if the kernel was configured with the | |
5a3eb9cd | 356 | .BR CONFIG_CHECKPOINT_RESTORE |
80d27367 MK |
357 | option. |
358 | .RE | |
5a3eb9cd | 359 | .TP |
45b81c9c MK |
360 | .BR close (2) |
361 | When the file descriptor is no longer required it should be closed. | |
362 | When all file descriptors associated with the same timer object | |
363 | have been closed, | |
364 | the timer is disarmed and its resources are freed by the kernel. | |
365 | .\" | |
366 | .SS fork(2) semantics | |
367 | After a | |
368 | .BR fork (2), | |
369 | the child inherits a copy of the file descriptor created by | |
370 | .BR timerfd_create (). | |
371 | The file descriptor refers to the same underlying | |
372 | timer object as the corresponding file descriptor in the parent, | |
373 | and | |
374 | .BR read (2)s | |
375 | in the child will return information about | |
376 | expirations of the timer. | |
377 | .\" | |
378 | .SS execve(2) semantics | |
379 | A file descriptor created by | |
380 | .BR timerfd_create () | |
381 | is preserved across | |
382 | .BR execve (2), | |
383 | and continues to generate timer expirations if the timer was armed. | |
47297adb | 384 | .SH RETURN VALUE |
45b81c9c MK |
385 | On success, |
386 | .BR timerfd_create () | |
387 | returns a new file descriptor. | |
388 | On error, \-1 is returned and | |
389 | .I errno | |
390 | is set to indicate the error. | |
efeece04 | 391 | .PP |
45b81c9c MK |
392 | .BR timerfd_settime () |
393 | and | |
394 | .BR timerfd_gettime () | |
395 | return 0 on success; | |
396 | on error they return \-1, and set | |
397 | .I errno | |
398 | to indicate the error. | |
399 | .SH ERRORS | |
400 | .BR timerfd_create () | |
401 | can fail with the following errors: | |
402 | .TP | |
403 | .B EINVAL | |
404 | The | |
405 | .I clockid | |
406 | argument is neither | |
407 | .B CLOCK_MONOTONIC | |
408 | nor | |
409 | .BR CLOCK_REALTIME ; | |
3df1b973 MK |
410 | .TP |
411 | .B EINVAL | |
412 | .I flags | |
413 | is invalid; | |
414 | or, in Linux 2.6.26 or earlier, | |
45b81c9c | 415 | .I flags |
c7094399 | 416 | is nonzero. |
45b81c9c MK |
417 | .TP |
418 | .B EMFILE | |
26c32fab | 419 | The per-process limit on the number of open file descriptors has been reached. |
45b81c9c MK |
420 | .TP |
421 | .B ENFILE | |
422 | The system-wide limit on the total number of open files has been | |
423 | reached. | |
424 | .TP | |
425 | .B ENODEV | |
426 | Could not mount (internal) anonymous inode device. | |
427 | .TP | |
428 | .B ENOMEM | |
429 | There was insufficient kernel memory to create the timer. | |
430 | .PP | |
431 | .BR timerfd_settime () | |
432 | and | |
433 | .BR timerfd_gettime () | |
434 | can fail with the following errors: | |
435 | .TP | |
436 | .B EBADF | |
437 | .I fd | |
438 | is not a valid file descriptor. | |
439 | .TP | |
2bbb3907 MK |
440 | .B EFAULT |
441 | .IR new_value , | |
442 | .IR old_value , | |
443 | or | |
444 | .I curr_value | |
445 | is not valid a pointer. | |
446 | .TP | |
45b81c9c MK |
447 | .B EINVAL |
448 | .I fd | |
449 | is not a valid timerfd file descriptor. | |
c71b054d MK |
450 | .PP |
451 | .BR timerfd_settime () | |
452 | can also fail with the following errors: | |
453 | .TP | |
454 | .B EINVAL | |
45b81c9c MK |
455 | .I new_value |
456 | is not properly initialized (one of the | |
457 | .I tv_nsec | |
458 | falls outside the range zero to 999,999,999). | |
86d95cf2 MK |
459 | .TP |
460 | .B EINVAL | |
461 | .\" This case only checked since 2.6.29, and 2.2.2[78].some-stable-version. | |
462 | .\" In older kernel versions, no check was made for invalid flags. | |
463 | .I flags | |
464 | is invalid. | |
45b81c9c MK |
465 | .SH VERSIONS |
466 | These system calls are available on Linux since kernel 2.6.25. | |
d6a54d42 | 467 | Library support is provided by glibc since version 2.8. |
45b81c9c MK |
468 | .SH CONFORMING TO |
469 | These system calls are Linux-specific. | |
7484d5a7 MK |
470 | .SH BUGS |
471 | Currently, | |
472 | .\" 2.6.29 | |
473 | .BR timerfd_create () | |
474 | supports fewer types of clock IDs than | |
475 | .BR timer_create (2). | |
45b81c9c MK |
476 | .SH EXAMPLE |
477 | The following program creates a timer and then monitors its progress. | |
478 | The program accepts up to three command-line arguments. | |
479 | The first argument specifies the number of seconds for | |
480 | the initial expiration of the timer. | |
481 | The second argument specifies the interval for the timer, in seconds. | |
482 | The third argument specifies the number of times the program should | |
483 | allow the timer to expire before terminating. | |
484 | The second and third command-line arguments are optional. | |
efeece04 | 485 | .PP |
45b81c9c | 486 | The following shell session demonstrates the use of the program: |
e646a1ba | 487 | .PP |
45b81c9c | 488 | .in +4n |
e646a1ba | 489 | .EX |
b43a3b30 | 490 | .RB "$" " a.out 3 1 100" |
45b81c9c MK |
491 | 0.000: timer started |
492 | 3.000: read: 1; total=1 | |
493 | 4.000: read: 1; total=2 | |
b43a3b30 | 494 | .BR "^Z " " # type control-Z to suspend the program" |
45b81c9c | 495 | [1]+ Stopped ./timerfd3_demo 3 1 100 |
b43a3b30 | 496 | .RB "$ " "fg" " # Resume execution after a few seconds" |
45b81c9c MK |
497 | a.out 3 1 100 |
498 | 9.660: read: 5; total=7 | |
499 | 10.000: read: 1; total=8 | |
500 | 11.000: read: 1; total=9 | |
b43a3b30 | 501 | .BR "^C " " # type control-C to suspend the program" |
b8302363 | 502 | .EE |
45b81c9c | 503 | .in |
9c330504 | 504 | .SS Program source |
d84d0300 | 505 | \& |
e7d0bb47 | 506 | .EX |
45b81c9c MK |
507 | .\" The commented out code here is what we currently need until |
508 | .\" the required stuff is in glibc | |
509 | .\" | |
510 | .\" | |
511 | .\"/* Link with -lrt */ | |
512 | .\"#define _GNU_SOURCE | |
513 | .\"#include <sys/syscall.h> | |
514 | .\"#include <unistd.h> | |
515 | .\"#include <time.h> | |
516 | .\"#if defined(__i386__) | |
517 | .\"#define __NR_timerfd_create 322 | |
518 | .\"#define __NR_timerfd_settime 325 | |
519 | .\"#define __NR_timerfd_gettime 326 | |
520 | .\"#endif | |
521 | .\" | |
522 | .\"static int | |
523 | .\"timerfd_create(int clockid, int flags) | |
524 | .\"{ | |
525 | .\" return syscall(__NR_timerfd_create, clockid, flags); | |
526 | .\"} | |
527 | .\" | |
528 | .\"static int | |
529 | .\"timerfd_settime(int fd, int flags, struct itimerspec *new_value, | |
530 | .\" struct itimerspec *curr_value) | |
531 | .\"{ | |
532 | .\" return syscall(__NR_timerfd_settime, fd, flags, new_value, | |
533 | .\" curr_value); | |
534 | .\"} | |
535 | .\" | |
536 | .\"static int | |
537 | .\"timerfd_gettime(int fd, struct itimerspec *curr_value) | |
538 | .\"{ | |
539 | .\" return syscall(__NR_timerfd_gettime, fd, curr_value); | |
540 | .\"} | |
541 | .\" | |
542 | .\"#define TFD_TIMER_ABSTIME (1 << 0) | |
543 | .\" | |
544 | .\"//////////////////////////////////////////////////////////// | |
45b81c9c MK |
545 | #include <sys/timerfd.h> |
546 | #include <time.h> | |
547 | #include <unistd.h> | |
548 | #include <stdlib.h> | |
549 | #include <stdio.h> | |
550 | #include <stdint.h> /* Definition of uint64_t */ | |
551 | ||
d1a71985 | 552 | #define handle_error(msg) \e |
45b81c9c MK |
553 | do { perror(msg); exit(EXIT_FAILURE); } while (0) |
554 | ||
555 | static void | |
556 | print_elapsed_time(void) | |
557 | { | |
558 | static struct timespec start; | |
559 | struct timespec curr; | |
560 | static int first_call = 1; | |
561 | int secs, nsecs; | |
562 | ||
563 | if (first_call) { | |
564 | first_call = 0; | |
565 | if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1) | |
566 | handle_error("clock_gettime"); | |
567 | } | |
568 | ||
569 | if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1) | |
570 | handle_error("clock_gettime"); | |
571 | ||
572 | secs = curr.tv_sec \- start.tv_sec; | |
573 | nsecs = curr.tv_nsec \- start.tv_nsec; | |
574 | if (nsecs < 0) { | |
575 | secs\-\-; | |
576 | nsecs += 1000000000; | |
577 | } | |
578 | printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000); | |
579 | } | |
580 | ||
581 | int | |
582 | main(int argc, char *argv[]) | |
583 | { | |
584 | struct itimerspec new_value; | |
585 | int max_exp, fd; | |
586 | struct timespec now; | |
587 | uint64_t exp, tot_exp; | |
588 | ssize_t s; | |
589 | ||
590 | if ((argc != 2) && (argc != 4)) { | |
d1a71985 | 591 | fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\en", |
45b81c9c MK |
592 | argv[0]); |
593 | exit(EXIT_FAILURE); | |
594 | } | |
595 | ||
596 | if (clock_gettime(CLOCK_REALTIME, &now) == \-1) | |
597 | handle_error("clock_gettime"); | |
598 | ||
599 | /* Create a CLOCK_REALTIME absolute timer with initial | |
600 | expiration and interval as specified in command line */ | |
601 | ||
602 | new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]); | |
603 | new_value.it_value.tv_nsec = now.tv_nsec; | |
604 | if (argc == 2) { | |
605 | new_value.it_interval.tv_sec = 0; | |
606 | max_exp = 1; | |
607 | } else { | |
608 | new_value.it_interval.tv_sec = atoi(argv[2]); | |
609 | max_exp = atoi(argv[3]); | |
610 | } | |
611 | new_value.it_interval.tv_nsec = 0; | |
612 | ||
613 | fd = timerfd_create(CLOCK_REALTIME, 0); | |
614 | if (fd == \-1) | |
615 | handle_error("timerfd_create"); | |
616 | ||
a279595b | 617 | if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1) |
45b81c9c MK |
618 | handle_error("timerfd_settime"); |
619 | ||
620 | print_elapsed_time(); | |
d1a71985 | 621 | printf("timer started\en"); |
45b81c9c MK |
622 | |
623 | for (tot_exp = 0; tot_exp < max_exp;) { | |
624 | s = read(fd, &exp, sizeof(uint64_t)); | |
625 | if (s != sizeof(uint64_t)) | |
626 | handle_error("read"); | |
627 | ||
628 | tot_exp += exp; | |
629 | print_elapsed_time(); | |
d1a71985 | 630 | printf("read: %llu; total=%llu\en", |
45b81c9c MK |
631 | (unsigned long long) exp, |
632 | (unsigned long long) tot_exp); | |
633 | } | |
634 | ||
635 | exit(EXIT_SUCCESS); | |
636 | } | |
e7d0bb47 | 637 | .EE |
47297adb | 638 | .SH SEE ALSO |
45b81c9c MK |
639 | .BR eventfd (2), |
640 | .BR poll (2), | |
641 | .BR read (2), | |
642 | .BR select (2), | |
643 | .BR setitimer (2), | |
644 | .BR signalfd (2), | |
804f03e6 MK |
645 | .BR timer_create (2), |
646 | .BR timer_gettime (2), | |
647 | .BR timer_settime (2), | |
45b81c9c MK |
648 | .BR epoll (7), |
649 | .BR time (7) |