blob: 3c7e63a26f107850efd8083a371d97bce518cb5f [file] [log] [blame]
Jason Evansb67ec3c2014-09-07 19:57:24 -07001#include "test/jemalloc_test.h"
2
3void
Daniel Micayc3bfe952014-09-08 00:46:12 -04004timer_start(timedelta_t *timer)
Jason Evansb67ec3c2014-09-07 19:57:24 -07005{
6
Jason Evans9bad0792016-02-21 11:25:02 -08007 nstime_init(&timer->t0, 0);
8 nstime_update(&timer->t0);
Jason Evansb67ec3c2014-09-07 19:57:24 -07009}
10
11void
Daniel Micayc3bfe952014-09-08 00:46:12 -040012timer_stop(timedelta_t *timer)
Jason Evansb67ec3c2014-09-07 19:57:24 -070013{
14
Jason Evans9bad0792016-02-21 11:25:02 -080015 nstime_copy(&timer->t1, &timer->t0);
16 nstime_update(&timer->t1);
Jason Evansb67ec3c2014-09-07 19:57:24 -070017}
18
19uint64_t
Daniel Micayc3bfe952014-09-08 00:46:12 -040020timer_usec(const timedelta_t *timer)
Jason Evansb67ec3c2014-09-07 19:57:24 -070021{
Jason Evans9bad0792016-02-21 11:25:02 -080022 nstime_t delta;
Jason Evansb67ec3c2014-09-07 19:57:24 -070023
Jason Evans9bad0792016-02-21 11:25:02 -080024 nstime_copy(&delta, &timer->t1);
25 nstime_subtract(&delta, &timer->t0);
26 return (nstime_ns(&delta) / 1000);
Jason Evansb67ec3c2014-09-07 19:57:24 -070027}
28
29void
Daniel Micayc3bfe952014-09-08 00:46:12 -040030timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen)
Jason Evansb67ec3c2014-09-07 19:57:24 -070031{
32 uint64_t t0 = timer_usec(a);
33 uint64_t t1 = timer_usec(b);
34 uint64_t mult;
Jason Evans22af74e2016-03-15 09:35:14 -070035 size_t i = 0;
36 size_t j, n;
Jason Evansb67ec3c2014-09-07 19:57:24 -070037
38 /* Whole. */
Jason Evans5fae7dc2015-07-23 13:56:25 -070039 n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1);
Jason Evansb67ec3c2014-09-07 19:57:24 -070040 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,
Jason Evans5fae7dc2015-07-23 13:56:25 -070056 "%"FMTu64, (t0 * mult / t1) % 10 + round);
Jason Evansb67ec3c2014-09-07 19:57:24 -070057 i += n;
58 mult *= 10;
59 }
60}