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