subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 1 | /* |
| 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_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 46 | #include "linux_syscall_numbers.h" |
| 47 | |
subrata_modak | 24fc213 | 2008-11-11 05:57:38 +0000 | [diff] [blame] | 48 | #define cleanup tst_exit |
| 49 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 50 | char *TCID = "timerfd01"; |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * This were good at the time of 2.6.23-rc7 ... |
subrata_modak | ea012ad | 2008-10-21 07:08:23 +0000 | [diff] [blame] | 54 | * |
| 55 | * #ifdef __NR_timerfd |
| 56 | * |
| 57 | * ... but is not now with 2.6.25 |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 58 | */ |
subrata_modak | ea012ad | 2008-10-21 07:08:23 +0000 | [diff] [blame] | 59 | #ifdef __NR_timerfd_create |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 60 | |
| 61 | /* Definitions from include/linux/timerfd.h */ |
| 62 | #define TFD_TIMER_ABSTIME (1 << 0) |
| 63 | |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 64 | struct tmr_type { |
| 65 | int id; |
| 66 | char const *name; |
| 67 | }; |
| 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | unsigned long long getustime(int clockid) |
| 70 | { |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 71 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | void set_timespec(struct timespec *tmr, unsigned long long ustime) |
| 82 | { |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 83 | |
| 84 | tmr->tv_sec = (time_t) (ustime / 1000000ULL); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | tmr->tv_nsec = (long)(1000ULL * (ustime % 1000000ULL)); |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 86 | } |
| 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | int timerfd_create(int clockid, int flags) |
| 89 | { |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 90 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 91 | return ltp_syscall(__NR_timerfd_create, clockid, flags); |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | int timerfd_settime(int ufc, int flags, const struct itimerspec *utmr, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | struct itimerspec *otmr) |
| 96 | { |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 97 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 98 | return ltp_syscall(__NR_timerfd_settime, ufc, flags, utmr, otmr); |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 99 | } |
| 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | int timerfd_gettime(int ufc, struct itimerspec *otmr) |
| 102 | { |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 103 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame] | 104 | return ltp_syscall(__NR_timerfd_gettime, ufc, otmr); |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 105 | } |
| 106 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | long waittmr(int tfd, int timeo) |
| 108 | { |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 109 | 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 | |
| 131 | int TST_TOTAL = 3; |
| 132 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | int main(int ac, char **av) |
| 134 | { |
subrata_modak | 424f279 | 2008-11-11 04:55:32 +0000 | [diff] [blame] | 135 | int i, tfd; |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 136 | long ticks; |
| 137 | unsigned long long tnow, ttmr; |
| 138 | u_int64_t uticks; |
| 139 | struct itimerspec tmr; |
| 140 | struct tmr_type clks[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | {CLOCK_MONOTONIC, "CLOCK MONOTONIC"}, |
| 142 | {CLOCK_REALTIME, "CLOCK REALTIME"}, |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | 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_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 150 | |
| 151 | for (i = 0; i < sizeof(clks) / sizeof(clks[0]); i++) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | fprintf(stdout, |
| 153 | "\n\n---------------------------------------\n"); |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 154 | 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_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 181 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 216 | (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_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 219 | 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_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 231 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 253 | fprintf(stdout, |
| 254 | "whooops! timer ticks not zero when should have been\n"); |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 255 | else if (errno != EAGAIN) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 256 | fprintf(stdout, |
| 257 | "whooops! bad errno value (%d = '%s')!\n", |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 258 | 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 267 | tst_exit(); |
subrata_modak | af2c9ef | 2008-06-03 06:54:12 +0000 | [diff] [blame] | 268 | } |
| 269 | |
subrata_modak | a649d52 | 2008-06-04 10:30:57 +0000 | [diff] [blame] | 270 | #else |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 271 | int TST_TOTAL = 0; |
subrata_modak | a649d52 | 2008-06-04 10:30:57 +0000 | [diff] [blame] | 272 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 273 | int main(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 274 | { |
subrata_modak | a649d52 | 2008-06-04 10:30:57 +0000 | [diff] [blame] | 275 | |
Cyril Hrubis | 526fdf8 | 2014-12-04 14:35:01 +0100 | [diff] [blame] | 276 | tst_brkm(TCONF, NULL, |
| 277 | "This test needs a kernel that has timerfd syscall."); |
subrata_modak | a649d52 | 2008-06-04 10:30:57 +0000 | [diff] [blame] | 278 | } |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 279 | #endif |