FreeBSD Bugzilla – Attachment 234088 Details for
Bug 264131
2nd and subsequent events on a repeating EVFILT_TIMER are delivered with twice the requested timer period
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
reproducing test program
repro_timer.c (text/plain), 2.33 KB, created by
Conor Hughes
on 2022-05-21 19:21:41 UTC
(
hide
)
Description:
reproducing test program
Filename:
MIME Type:
Creator:
Conor Hughes
Created:
2022-05-21 19:21:41 UTC
Size:
2.33 KB
patch
obsolete
>#include <sys/event.h> >#include <sys/types.h> >#include <stdio.h> >#include <err.h> >#include <sysexits.h> >#include <stdbool.h> >#include <time.h> > >#ifndef NSEC_PER_SEC >#define NSEC_PER_SEC 1000000000ULL >#endif // ndef NSEC_PER_SEC > >int cmh_timespec_cmp(struct timespec lhs, struct timespec rhs) >{ > if ( lhs.tv_sec > rhs.tv_sec ) { > return 1; > } else if ( lhs.tv_sec < rhs.tv_sec ) { > return -1; > } else if ( lhs.tv_nsec > rhs.tv_nsec ) { > return 1; > } else if ( lhs.tv_nsec < rhs.tv_nsec ) { > return -1; > } else { > return 0; > } >} > >bool cmh_timespec_sub(struct timespec lhs, struct timespec rhs, > struct timespec *out) >{ > bool negative = cmh_timespec_cmp(lhs, rhs) < 0; > if ( negative ) { > struct timespec tmp = lhs; > lhs = rhs; > rhs = tmp; > } > > if ( out ) { > if ( lhs.tv_nsec < rhs.tv_nsec ) { > // borrow > lhs.tv_sec -= 1; > // This is safe for a normalized timespec. > lhs.tv_nsec += NSEC_PER_SEC; > } > > out->tv_nsec = lhs.tv_nsec - rhs.tv_nsec; > out->tv_sec = lhs.tv_sec - rhs.tv_sec; > } > > return negative; >} > > >int main(void) >{ > static const int64_t milliseconds_wait = 500; > struct kevent kev; > EV_SET(&kev, 1, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, milliseconds_wait, NULL); > int kq = kqueue(); > if ( kq < 0 ) { > err(EX_OSERR, "kqueue"); > } > > struct timespec last_fire; > if ( clock_gettime(CLOCK_MONOTONIC, &last_fire) ) { > err(EX_OSERR, "clock_gettime"); > } > > // Add timer. > if ( kevent(kq, &kev, 1, NULL, 0, NULL) < 0 ) { > err(EX_OSERR, "kevent (add)"); > } > > for ( ; ; ) { > // Wait for events. > int nevents = kevent(kq, NULL, 0, &kev, 1, NULL); > if ( nevents < 0 ) { > err(EX_OSERR, "kevent (wait)"); > } else if ( nevents != 1 ) { > fprintf(stderr, "bogus number of events %d?\n", nevents); > continue; > } > > struct timespec now; > if ( clock_gettime(CLOCK_MONOTONIC, &now) ) { > err(EX_OSERR, "clock_gettime"); > } > struct timespec diff; > if ( cmh_timespec_sub(now, last_fire, &diff) ) { > errx(EX_OSERR, "clock_gettime(CLOCK_MONOTONIC) went backwards?"); > } > > fprintf(stderr, "got event, ident %llu, filter %d, data %lld; last fire was %ld ms ago\n", > (unsigned long long)kev.ident, kev.filter, (long long)kev.data, (long)((diff.tv_sec * 1000) + (diff.tv_nsec / 1000000))); > > last_fire = now; > } > > return 0; >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 264131
: 234088