Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 1 | #include "test/jemalloc_test.h" |
| 2 | |
| 3 | void |
Daniel Micay | c3bfe95 | 2014-09-08 00:46:12 -0400 | [diff] [blame] | 4 | timer_start(timedelta_t *timer) |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 5 | { |
| 6 | |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 7 | nstime_init(&timer->t0, 0); |
| 8 | nstime_update(&timer->t0); |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 9 | } |
| 10 | |
| 11 | void |
Daniel Micay | c3bfe95 | 2014-09-08 00:46:12 -0400 | [diff] [blame] | 12 | timer_stop(timedelta_t *timer) |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 13 | { |
| 14 | |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 15 | nstime_copy(&timer->t1, &timer->t0); |
| 16 | nstime_update(&timer->t1); |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | uint64_t |
Daniel Micay | c3bfe95 | 2014-09-08 00:46:12 -0400 | [diff] [blame] | 20 | timer_usec(const timedelta_t *timer) |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 21 | { |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 22 | nstime_t delta; |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 23 | |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 24 | nstime_copy(&delta, &timer->t1); |
| 25 | nstime_subtract(&delta, &timer->t0); |
| 26 | return (nstime_ns(&delta) / 1000); |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void |
Daniel Micay | c3bfe95 | 2014-09-08 00:46:12 -0400 | [diff] [blame] | 30 | timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen) |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 31 | { |
| 32 | uint64_t t0 = timer_usec(a); |
| 33 | uint64_t t1 = timer_usec(b); |
| 34 | uint64_t mult; |
Jason Evans | 22af74e | 2016-03-15 09:35:14 -0700 | [diff] [blame] | 35 | size_t i = 0; |
| 36 | size_t j, n; |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 37 | |
| 38 | /* Whole. */ |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 39 | n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1); |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 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, |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 56 | "%"FMTu64, (t0 * mult / t1) % 10 + round); |
Jason Evans | b67ec3c | 2014-09-07 19:57:24 -0700 | [diff] [blame] | 57 | i += n; |
| 58 | mult *= 10; |
| 59 | } |
| 60 | } |