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