1 #include "test/jemalloc_test.h" 2 3 void 4 timer_start(timedelta_t *timer) 5 { 6 7 nstime_init(&timer->t0, 0); 8 nstime_update(&timer->t0); 9 } 10 11 void 12 timer_stop(timedelta_t *timer) 13 { 14 15 nstime_copy(&timer->t1, &timer->t0); 16 nstime_update(&timer->t1); 17 } 18 19 uint64_t 20 timer_usec(const timedelta_t *timer) 21 { 22 nstime_t delta; 23 24 nstime_copy(&delta, &timer->t1); 25 nstime_subtract(&delta, &timer->t0); 26 return (nstime_ns(&delta) / 1000); 27 } 28 29 void 30 timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen) 31 { 32 uint64_t t0 = timer_usec(a); 33 uint64_t t1 = timer_usec(b); 34 uint64_t mult; 35 unsigned i = 0; 36 unsigned j; 37 int n; 38 39 /* Whole. */ 40 n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1); 41 i += n; 42 if (i >= buflen) 43 return; 44 mult = 1; 45 for (j = 0; j < n; j++) 46 mult *= 10; 47 48 /* Decimal. */ 49 n = malloc_snprintf(&buf[i], buflen-i, "."); 50 i += n; 51 52 /* Fraction. */ 53 while (i < buflen-1) { 54 uint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10 55 >= 5)) ? 1 : 0; 56 n = malloc_snprintf(&buf[i], buflen-i, 57 "%"FMTu64, (t0 * mult / t1) % 10 + round); 58 i += n; 59 mult *= 10; 60 } 61 } 62