2014-09-08 10:57:24 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
void
|
2014-09-08 12:46:12 +08:00
|
|
|
timer_start(timedelta_t *timer)
|
2014-09-08 10:57:24 +08:00
|
|
|
{
|
|
|
|
|
2015-07-14 05:35:15 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
GetSystemTimeAsFileTime(&timer->ft0);
|
|
|
|
#elif JEMALLOC_CLOCK_GETTIME
|
2014-10-17 05:02:18 +08:00
|
|
|
if (sysconf(_SC_MONOTONIC_CLOCK) <= 0)
|
|
|
|
timer->clock_id = CLOCK_REALTIME;
|
|
|
|
else
|
|
|
|
timer->clock_id = CLOCK_MONOTONIC;
|
2015-07-14 05:35:15 +08:00
|
|
|
clock_gettime(timer->clock_id, &timer->ts0);
|
2014-10-17 05:02:18 +08:00
|
|
|
#else
|
2014-09-08 10:57:24 +08:00
|
|
|
gettimeofday(&timer->tv0, NULL);
|
2014-10-17 05:02:18 +08:00
|
|
|
#endif
|
2014-09-08 10:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-08 12:46:12 +08:00
|
|
|
timer_stop(timedelta_t *timer)
|
2014-09-08 10:57:24 +08:00
|
|
|
{
|
|
|
|
|
2015-07-14 05:35:15 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
GetSystemTimeAsFileTime(&timer->ft0);
|
|
|
|
#elif JEMALLOC_CLOCK_GETTIME
|
|
|
|
clock_gettime(timer->clock_id, &timer->ts1);
|
2014-10-17 05:02:18 +08:00
|
|
|
#else
|
2014-09-08 10:57:24 +08:00
|
|
|
gettimeofday(&timer->tv1, NULL);
|
2014-10-17 05:02:18 +08:00
|
|
|
#endif
|
2014-09-08 10:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2014-09-08 12:46:12 +08:00
|
|
|
timer_usec(const timedelta_t *timer)
|
2014-09-08 10:57:24 +08:00
|
|
|
{
|
|
|
|
|
2015-07-14 05:35:15 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
uint64_t t0, t1;
|
|
|
|
t0 = (((uint64_t)timer->ft0.dwHighDateTime) << 32) |
|
|
|
|
timer->ft0.dwLowDateTime;
|
|
|
|
t1 = (((uint64_t)timer->ft1.dwHighDateTime) << 32) |
|
|
|
|
timer->ft1.dwLowDateTime;
|
|
|
|
return ((t1 - t0) / 10);
|
|
|
|
#elif JEMALLOC_CLOCK_GETTIME
|
|
|
|
return (((timer->ts1.tv_sec - timer->ts0.tv_sec) * 1000000) +
|
|
|
|
(timer->ts1.tv_nsec - timer->ts0.tv_nsec) / 1000);
|
2014-10-17 05:02:18 +08:00
|
|
|
#else
|
2014-09-08 10:57:24 +08:00
|
|
|
return (((timer->tv1.tv_sec - timer->tv0.tv_sec) * 1000000) +
|
|
|
|
timer->tv1.tv_usec - timer->tv0.tv_usec);
|
2014-10-17 05:02:18 +08:00
|
|
|
#endif
|
2014-09-08 10:57:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-09-08 12:46:12 +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;
|
|
|
|
unsigned i = 0;
|
|
|
|
unsigned j;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
if (i >= buflen)
|
|
|
|
return;
|
|
|
|
mult = 1;
|
|
|
|
for (j = 0; j < n; j++)
|
|
|
|
mult *= 10;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|