From 7d93b9e5a2a92f366719a9c471c6bf926915e5c0 Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Thu, 13 Mar 2025 18:17:56 -0400 Subject: pktmr: BREAKING more denominations --- pktmr.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'pktmr.h') diff --git a/pktmr.h b/pktmr.h index 9245373..b0bcdee 100644 --- a/pktmr.h +++ b/pktmr.h @@ -10,7 +10,6 @@ * CLOCK_PROCESS_CPUTIME_ID consistently elapsed thousands of nanoseconds, * even with no work between sequential _start() and _stop() calls. * Meanwhile, the same test with _MONOTONIC elapsed only tens of nanoseconds. - * Consider replacing explicit usage with a define for more user control. */ /* struct pk_tmr */ @@ -19,9 +18,16 @@ struct pk_tmr { struct timespec e; // end }; -#define pk_tmr_start(tmr) { clock_gettime(CLOCK_MONOTONIC, &tmr.b); } -#define pk_tmr_stop(tmr) { clock_gettime(CLOCK_MONOTONIC, &tmr.e); } -#define pk_tmr_duration_double(tmr) ((1000.0 * tmr.e.tv_sec + 1e-6 * tmr.e.tv_nsec) - (1000.0 * tmr.b.tv_sec + 1e-6 * tmr.b.tv_nsec)) -#define pk_tmr_duration_nano(tmr) ((((uint64_t)tmr.e.tv_sec * (uint64_t)1000000000) + tmr.e.tv_nsec) - (((uint64_t)tmr.b.tv_sec * (uint64_t)1000000000) + (uint64_t)tmr.b.tv_nsec)) +#ifndef PK_TMR_CLOCK + #define PK_TMR_CLOCK CLOCK_MONOTONIC +#endif + +#define pk_tmr_start(tmr) { clock_gettime(PK_TMR_CLOCK, &tmr.b); } +#define pk_tmr_stop(tmr) { clock_gettime(PK_TMR_CLOCK, &tmr.e); } +#define pk_tmr_duration_u64_nano(tmr) ((((unsigned long long int)tmr.e.tv_sec * 1000000000llu) + tmr.e.tv_nsec) - (((unsigned long long int)tmr.b.tv_sec * 1000000000llu) + (unsigned long long int)tmr.b.tv_nsec)) +#define pk_tmr_duration_dbl_nano(tmr) ((1e+9 * tmr.e.tv_sec + tmr.e.tv_nsec) - (1e+9 * tmr.b.tv_sec + tmr.b.tv_nsec)) +#define pk_tmr_duration_dbl_micro(tmr) ((1e+6 * tmr.e.tv_sec + 1e-3 * tmr.e.tv_nsec) - (1e+6 * tmr.b.tv_sec + 1e-3 * tmr.b.tv_nsec)) +#define pk_tmr_duration_dbl_mili(tmr) ((1e+3 * tmr.e.tv_sec + 1e-6 * tmr.e.tv_nsec) - (1e+3 * tmr.b.tv_sec + 1e-6 * tmr.b.tv_nsec)) +#define pk_tmr_duration_dbl_scnd(tmr) ((tmr.e.tv_sec + 1e-9 * tmr.e.tv_nsec) - (tmr.b.tv_sec + 1e-9 * tmr.b.tv_nsec)) #endif /* PK_PKTMR_H */ -- cgit v1.2.3