1 #include "test/jemalloc_test.h" 2 3 void 4 timer_start(timedelta_t *timer) 5 { 6 7 #ifdef _WIN32 8 GetSystemTimeAsFileTime(&timer->ft0); 9 #elif JEMALLOC_CLOCK_GETTIME 10 if (sysconf(_SC_MONOTONIC_CLOCK) <= 0) 11 timer->clock_id = CLOCK_REALTIME; 12 else 13 timer->clock_id = CLOCK_MONOTONIC; 14 clock_gettime(timer->clock_id, &timer->ts0); 15 #else 16 gettimeofday(&timer->tv0, NULL); 17 #endif 18 } 19 20 void 21 timer_stop(timedelta_t *timer) 22 { 23 24 #ifdef _WIN32 25 GetSystemTimeAsFileTime(&timer->ft0); 26 #elif JEMALLOC_CLOCK_GETTIME 27 clock_gettime(timer->clock_id, &timer->ts1); 28 #else 29 gettimeofday(&timer->tv1, NULL); 30 #endif 31 } 32 33 uint64_t 34 timer_usec(const timedelta_t *timer) 35 { 36 37 #ifdef _WIN32 38 uint64_t t0, t1; 39 t0 = (((uint64_t)timer->ft0.dwHighDateTime) << 32) | 40 timer->ft0.dwLowDateTime; 41 t1 = (((uint64_t)timer->ft1.dwHighDateTime) << 32) | 42 timer->ft1.dwLowDateTime; 43 return ((t1 - t0) / 10); 44 #elif JEMALLOC_CLOCK_GETTIME 45 return (((timer->ts1.tv_sec - timer->ts0.tv_sec) * 1000000) + 46 (timer->ts1.tv_nsec - timer->ts0.tv_nsec) / 1000); 47 #else 48 return (((timer->tv1.tv_sec - timer->tv0.tv_sec) * 1000000) + 49 timer->tv1.tv_usec - timer->tv0.tv_usec); 50 #endif 51 } 52 53 void 54 timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen) 55 { 56 uint64_t t0 = timer_usec(a); 57 uint64_t t1 = timer_usec(b); 58 uint64_t mult; 59 unsigned i = 0; 60 unsigned j; 61 int n; 62 63 /* Whole. */ 64 n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1); 65 i += n; 66 if (i >= buflen) 67 return; 68 mult = 1; 69 for (j = 0; j < n; j++) 70 mult *= 10; 71 72 /* Decimal. */ 73 n = malloc_snprintf(&buf[i], buflen-i, "."); 74 i += n; 75 76 /* Fraction. */ 77 while (i < buflen-1) { 78 uint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10 79 >= 5)) ? 1 : 0; 80 n = malloc_snprintf(&buf[i], buflen-i, 81 "%"FMTu64, (t0 * mult / t1) % 10 + round); 82 i += n; 83 mult *= 10; 84 } 85 } 86