Loading...
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 | #include <sys/types.h> #include <sys/event.h> #include <sys/time.h> #include <assert.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> int kq, passed, failed; /* * Wait for given kevent, which should return in 'expected' usecs. */ int do_simple_kevent(struct kevent64_s *kev, uint64_t expected) { int ret; uint64_t elapsed_usecs, delta_usecs; struct timespec timeout; struct timeval before, after; /* time out after 1 sec extra delay */ timeout.tv_sec = (expected / (1000 * 1000)) + 1; timeout.tv_nsec = (expected % (1000 * 1000)) * 1000; /* measure time for the kevent */ gettimeofday(&before, NULL); ret = kevent64(kq, kev, 1, kev, 1, 0, &timeout); gettimeofday(&after, NULL); if (ret < 1 || (kev->flags & EV_ERROR)) { printf("\tfailure: kevent returned %d, error %d\n", ret, (ret == -1 ? errno : (int) kev->data)); return 0; } /* did it work? */ elapsed_usecs = (after.tv_sec - before.tv_sec) * (1000 * 1000) + (after.tv_usec - before.tv_usec); delta_usecs = abs(elapsed_usecs - (expected)); /* failure if we're 30% off, or 50 mics late */ if (delta_usecs > (30 * expected / 100.0) && delta_usecs > 50) { printf("\tfailure: expected %lld usec, measured %lld usec.\n", expected, elapsed_usecs); return 0; } else { printf("\tsuccess.\n"); return 1; } } void test_absolute_kevent(int time, int scale) { struct timeval tv; struct kevent64_s kev; uint64_t nowus, expected, deadline; int ret; int timescale = 0; gettimeofday(&tv, NULL); nowus = tv.tv_sec * (1000 * 1000LL) + tv.tv_usec; switch (scale) { case NOTE_SECONDS: printf("Testing %d sec absolute timer...\n", time); timescale = 1000 * 1000; break; case NOTE_USECONDS: printf("Testing %d usec absolute timer...\n", time); timescale = 1; break; case 0: printf("Testing %d msec absolute timer...\n", time); timescale = 1000; break; default: printf("Failure: scale 0x%x not recognized.\n", scale); return; } expected = time * timescale; deadline = nowus / timescale + time; /* deadlines in the past should fire immediately */ if (time < 0) expected = 0; EV_SET64(&kev, 1, EVFILT_TIMER, EV_ADD, NOTE_ABSOLUTE | scale, deadline, 0,0,0); ret = do_simple_kevent(&kev, expected); if (ret) passed++; else failed++; } void test_oneshot_kevent(int time, int scale) { int ret; uint64_t expected = 0; struct kevent64_s kev; switch (scale) { case NOTE_SECONDS: printf("Testing %d sec interval timer...\n", time); expected = time * (1000 * 1000); break; case NOTE_USECONDS: printf("Testing %d usec interval timer...\n", time); expected = time; break; case NOTE_NSECONDS: printf("Testing %d nsec interval timer...\n", time); expected = time / 1000; break; case 0: printf("Testing %d msec interval timer...\n", time); expected = time * 1000; break; default: printf("Failure: scale 0x%x not recognized.\n", scale); return; } /* deadlines in the past should fire immediately */ if (time < 0) expected = 0; EV_SET64(&kev, 2, EVFILT_TIMER, EV_ADD | EV_ONESHOT, scale, time, 0, 0, 0); ret = do_simple_kevent(&kev, expected); if (ret) passed++; else failed++; } void test_repeating_kevent(int usec) { struct kevent64_s kev; int expected_pops, ret; expected_pops = 1000 * 1000 / usec; printf("Testing repeating kevent for %d pops in a second...\n", expected_pops); EV_SET64(&kev, 3, EVFILT_TIMER, EV_ADD, NOTE_USECONDS, usec, 0, 0, 0); ret = kevent64(kq, &kev, 1, NULL, 0, 0, NULL); if (ret != 0) { printf("\tfailure: kevent64 returned %d\n", ret); failed++; return; } /* sleep 1 second */ usleep(1000 * 1000); ret = kevent64(kq, NULL, 0, &kev, 1, 0, NULL); if (ret != 1 || (kev.flags & EV_ERROR)) { printf("\tfailure: kevent64 returned %d\n", ret); failed++; return; } /* check how many times the timer fired: within 5%? */ if (kev.data > expected_pops + (expected_pops / 20) || kev.data < expected_pops - (expected_pops / 20)) { printf("\tfailure: saw %lld pops.\n", kev.data); failed++; } else { printf("\tsuccess: saw %lld pops.\n", kev.data); passed++; } EV_SET64(&kev, 3, EVFILT_TIMER, EV_DELETE, 0, 0, 0, 0, 0); ret = kevent64(kq, &kev, 1, NULL, 0, 0, NULL); if (ret != 0) { printf("\tfailed to stop repeating timer: %d\n", ret); } } test_updated_kevent(int first, int second) { struct kevent64_s kev; int ret; printf("Testing update from %d to %d msecs...\n", first, second); EV_SET64(&kev, 4, EVFILT_TIMER, EV_ADD|EV_ONESHOT, 0, first, 0, 0, 0); ret = kevent64(kq, &kev, 1, NULL, 0, 0, NULL); if (ret != 0) { printf("\tfailure: initial kevent returned %d\n", ret); failed++; return; } EV_SET64(&kev, 4, EVFILT_TIMER, EV_ONESHOT, 0, second, 0, 0, 0); if (second < 0) second = 0; ret = do_simple_kevent(&kev, second * 1000); if (ret) passed++; else failed++; } int main(void) { struct timeval tv; struct kevent64_s kev; uint64_t nowms, deadline; kq = kqueue(); assert(kq > 0); passed = 0; failed = 0; test_absolute_kevent(100, 0); test_absolute_kevent(200, 0); test_absolute_kevent(300, 0); test_absolute_kevent(1000, 0); test_absolute_kevent(500, NOTE_USECONDS); test_absolute_kevent(100, NOTE_USECONDS); test_absolute_kevent(5, NOTE_SECONDS); test_absolute_kevent(-1000, 0); test_oneshot_kevent(1, NOTE_SECONDS); test_oneshot_kevent(10, 0); test_oneshot_kevent(200, NOTE_USECONDS); test_oneshot_kevent(300000, NOTE_NSECONDS); test_oneshot_kevent(-1, NOTE_SECONDS); test_repeating_kevent(100 * 1000); test_repeating_kevent(5 * 1000); test_repeating_kevent(200); test_repeating_kevent(50); test_repeating_kevent(10); test_updated_kevent(1000, 2000); test_updated_kevent(2000, 1000); test_updated_kevent(1000, -1); printf("\nFinished: %d tests passed, %d failed.\n", passed, failed); exit(EXIT_SUCCESS); } |