2014-09-08 10:57:24 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
timer_start(timedelta_t *timer) {
|
2019-12-17 04:41:06 +08:00
|
|
|
nstime_init_update(&timer->t0);
|
2014-09-08 10:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
timer_stop(timedelta_t *timer) {
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_copy(&timer->t1, &timer->t0);
|
|
|
|
nstime_update(&timer->t1);
|
2014-09-08 10:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2017-01-16 08:56:30 +08:00
|
|
|
timer_usec(const timedelta_t *timer) {
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_t delta;
|
2014-09-08 10:57:24 +08:00
|
|
|
|
2016-02-22 03:25:02 +08:00
|
|
|
nstime_copy(&delta, &timer->t1);
|
|
|
|
nstime_subtract(&delta, &timer->t0);
|
2017-01-20 10:15:45 +08:00
|
|
|
return nstime_ns(&delta) / 1000;
|
2014-09-08 10:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 08:56:30 +08:00
|
|
|
timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen) {
|
2014-09-08 10:57:24 +08:00
|
|
|
uint64_t t0 = timer_usec(a);
|
|
|
|
uint64_t t1 = timer_usec(b);
|
|
|
|
uint64_t mult;
|
2016-03-16 00:35:14 +08:00
|
|
|
size_t i = 0;
|
|
|
|
size_t j, n;
|
2014-09-08 10:57:24 +08:00
|
|
|
|
|
|
|
/* Whole. */
|
2015-07-24 04:56:25 +08:00
|
|
|
n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1);
|
2014-09-08 10:57:24 +08:00
|
|
|
i += n;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (i >= buflen) {
|
2014-09-08 10:57:24 +08:00
|
|
|
return;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-09-08 10:57:24 +08:00
|
|
|
mult = 1;
|
2017-01-16 08:56:30 +08:00
|
|
|
for (j = 0; j < n; j++) {
|
2014-09-08 10:57:24 +08:00
|
|
|
mult *= 10;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2014-09-08 10:57:24 +08:00
|
|
|
|
|
|
|
/* Decimal. */
|
|
|
|
n = malloc_snprintf(&buf[i], buflen-i, ".");
|
|
|
|
i += n;
|
|
|
|
|
|
|
|
/* Fraction. */
|
|
|
|
while (i < buflen-1) {
|
|
|
|
uint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10
|
|
|
|
>= 5)) ? 1 : 0;
|
|
|
|
n = malloc_snprintf(&buf[i], buflen-i,
|
2015-07-24 04:56:25 +08:00
|
|
|
"%"FMTu64, (t0 * mult / t1) % 10 + round);
|
2014-09-08 10:57:24 +08:00
|
|
|
i += n;
|
|
|
|
mult *= 10;
|
|
|
|
}
|
|
|
|
}
|