Ported to NetBSD (1/2): Replaced Linux-specific timer call
CLOCK_THREAD_CPUTIME_ID is a linux-specific timer. On NetBSD, the best way to get timer information appears to be getrusage, which happens to be fairly cross-platform.
This commit is contained in:
parent
5270484839
commit
426a3f8468
1 changed files with 18 additions and 0 deletions
|
|
@ -10,7 +10,13 @@
|
||||||
#include <mach/mach.h>
|
#include <mach/mach.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef __NetBSD__
|
||||||
|
#include <sys/resource.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
void h_benchmark_clock_gettime(struct timespec *ts) {
|
void h_benchmark_clock_gettime(struct timespec *ts) {
|
||||||
|
if (ts == NULL)
|
||||||
|
return;
|
||||||
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
|
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
|
||||||
/*
|
/*
|
||||||
* This returns real time, not CPU time. See http://stackoverflow.com/a/6725161
|
* This returns real time, not CPU time. See http://stackoverflow.com/a/6725161
|
||||||
|
|
@ -23,6 +29,18 @@ void h_benchmark_clock_gettime(struct timespec *ts) {
|
||||||
mach_port_deallocate(mach_task_self(), cclock);
|
mach_port_deallocate(mach_task_self(), cclock);
|
||||||
ts->tv_sec = mts.tv_sec;
|
ts->tv_sec = mts.tv_sec;
|
||||||
ts->tv_nsec = mts.tv_nsec;
|
ts->tv_nsec = mts.tv_nsec;
|
||||||
|
#elif defined(__NetBSD__)
|
||||||
|
// NetBSD doesn't have CLOCK_THREAD_CPUTIME_ID. We'll use getrusage instead
|
||||||
|
struct rusage rusage;
|
||||||
|
getrusage(RUSAGE_SELF, &rusage);
|
||||||
|
ts->tv_nsec = (rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec) * 1000;
|
||||||
|
// not going to overflow; can be at most 2e9-2
|
||||||
|
ts->tv_sec = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_sec;
|
||||||
|
if (ts->tv_nsec >= 1000000000) {
|
||||||
|
ts->tv_nsec -= 1000000000; // subtract a second
|
||||||
|
ts->tv_sec += 1; // add it back.
|
||||||
|
}
|
||||||
|
assert (ts->tv_nsec <= 1000000000);
|
||||||
#else
|
#else
|
||||||
clock_gettime(CLOCK_THREAD_CPUTIME_ID, ts);
|
clock_gettime(CLOCK_THREAD_CPUTIME_ID, ts);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue