1 /* Copied from the Linux manpage
2 * with the printed times rounded to
3 * seconds for reproducibility.
4 * And some errors added.
11 #include <sys/timerfd.h>
17 #include "../../memcheck.h"
20 print_elapsed_time(void)
23 static int first_call
= 1;
25 static struct timespec start
;
30 if (clock_gettime(CLOCK_MONOTONIC
, &start
) == -1)
31 err(EXIT_FAILURE
, "clock_gettime");
34 if (clock_gettime(CLOCK_MONOTONIC
, &curr
) == -1)
35 err(EXIT_FAILURE
, "clock_gettime");
37 secs
= curr
.tv_sec
- start
.tv_sec
;
38 nsecs
= curr
.tv_nsec
- start
.tv_nsec
;
43 round_secs
= round((secs
*1e9
+ nsecs
)/1e9
);
44 printf("%d: ", round_secs
);
48 main(int argc
, char *argv
[])
52 uint64_t exp
, tot_exp
, max_exp
;
54 struct itimerspec new_value
;
56 if (argc
!= 2 && argc
!= 4) {
57 fprintf(stderr
, "%s init-secs [interval-secs max-exp]\n",
62 if (clock_gettime(CLOCK_REALTIME
, &now
) == -1)
63 err(EXIT_FAILURE
, "clock_gettime");
65 /* Create a CLOCK_REALTIME absolute timer with initial
66 expiration and interval as specified in command line. */
68 new_value
.it_value
.tv_sec
= now
.tv_sec
+ atoi(argv
[1]);
69 new_value
.it_value
.tv_nsec
= now
.tv_nsec
;
71 new_value
.it_interval
.tv_sec
= 0;
74 new_value
.it_interval
.tv_sec
= atoi(argv
[2]);
75 max_exp
= atoi(argv
[3]);
77 new_value
.it_interval
.tv_nsec
= 0;
79 fd
= timerfd_create(CLOCK_REALTIME
, 0);
81 err(EXIT_FAILURE
, "timerfd_create");
83 if (timerfd_settime(fd
, TFD_TIMER_ABSTIME
, &new_value
, NULL
) == -1)
84 err(EXIT_FAILURE
, "timerfd_settime");
87 printf("timer started\n");
89 for (tot_exp
= 0; tot_exp
< max_exp
;) {
90 s
= read(fd
, &exp
, sizeof(uint64_t));
91 if (s
!= sizeof(uint64_t))
92 err(EXIT_FAILURE
, "read");
96 printf("read: %" PRIu64
"; total=%" PRIu64
"\n", exp
, tot_exp
);
102 int a
= CLOCK_REALTIME
;
104 int c
= TFD_TIMER_ABSTIME
;
105 VALGRIND_MAKE_MEM_UNDEFINED(&a
, sizeof(a
));
106 VALGRIND_MAKE_MEM_UNDEFINED(&b
, sizeof(b
));
107 VALGRIND_MAKE_MEM_UNDEFINED(&c
, sizeof(c
));
108 struct itimerspec
* get_ts
= malloc(sizeof(*get_ts
) - 2);
109 struct itimerspec
* set_ts
= malloc(sizeof(*set_ts
) - 2);
110 struct itimerspec ts
;
114 /* uninit clockid and flag but should work */
115 int fd2
= timerfd_create(a
, b
);
118 /* bad flag should fail */
119 timerfd_create(CLOCK_REALTIME
, 1000000);
120 /* bad clockid should fail */
121 timerfd_create(1000000, TFD_CLOEXEC
);
123 /* memory too small for requested get */
124 timerfd_gettime(fd2
, get_ts
);
126 timerfd_gettime(fd2
, &ts
);
127 ts
.it_interval
.tv_nsec
+= 100000;
130 timerfd_settime(fd2
, c
, &ts
, NULL
);
132 ts
.it_interval
.tv_nsec
+= 100000;
133 /* memory too small for requested old value */
134 retval
= timerfd_settime(fd2
, TFD_TIMER_ABSTIME
, &ts
, set_ts
);
138 VALGRIND_MAKE_MEM_UNDEFINED(&fd2
, sizeof(fd2
));
139 timerfd_gettime(fd2
, &ts
);
140 ts
.it_interval
.tv_nsec
+= 100000;
141 timerfd_settime(fd2
, TFD_TIMER_ABSTIME
, &ts
, NULL
);