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