1 #include "test/jemalloc_test.h"
2 
3 void
timer_start(timedelta_t * timer)4 timer_start(timedelta_t *timer)
5 {
6 
7 	nstime_init(&timer->t0, 0);
8 	nstime_update(&timer->t0);
9 }
10 
11 void
timer_stop(timedelta_t * timer)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
timer_usec(const timedelta_t * timer)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
timer_ratio(timedelta_t * a,timedelta_t * b,char * buf,size_t buflen)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 	size_t i = 0;
36 	size_t j, n;
37 
38 	/* Whole. */
39 	n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1);
40 	i += n;
41 	if (i >= buflen)
42 		return;
43 	mult = 1;
44 	for (j = 0; j < n; j++)
45 		mult *= 10;
46 
47 	/* Decimal. */
48 	n = malloc_snprintf(&buf[i], buflen-i, ".");
49 	i += n;
50 
51 	/* Fraction. */
52 	while (i < buflen-1) {
53 		uint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10
54 		    >= 5)) ? 1 : 0;
55 		n = malloc_snprintf(&buf[i], buflen-i,
56 		    "%"FMTu64, (t0 * mult / t1) % 10 + round);
57 		i += n;
58 		mult *= 10;
59 	}
60 }
61