]> git.proxmox.com Git - rustc.git/blob - src/jemalloc/test/src/timer.c
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / jemalloc / test / src / timer.c
1 #include "test/jemalloc_test.h"
2
3 void
4 timer_start(timedelta_t *timer)
5 {
6
7 gettimeofday(&timer->tv0, NULL);
8 }
9
10 void
11 timer_stop(timedelta_t *timer)
12 {
13
14 gettimeofday(&timer->tv1, NULL);
15 }
16
17 uint64_t
18 timer_usec(const timedelta_t *timer)
19 {
20
21 return (((timer->tv1.tv_sec - timer->tv0.tv_sec) * 1000000) +
22 timer->tv1.tv_usec - timer->tv0.tv_usec);
23 }
24
25 void
26 timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen)
27 {
28 uint64_t t0 = timer_usec(a);
29 uint64_t t1 = timer_usec(b);
30 uint64_t mult;
31 unsigned i = 0;
32 unsigned j;
33 int n;
34
35 /* Whole. */
36 n = malloc_snprintf(&buf[i], buflen-i, "%"PRIu64, t0 / t1);
37 i += n;
38 if (i >= buflen)
39 return;
40 mult = 1;
41 for (j = 0; j < n; j++)
42 mult *= 10;
43
44 /* Decimal. */
45 n = malloc_snprintf(&buf[i], buflen-i, ".");
46 i += n;
47
48 /* Fraction. */
49 while (i < buflen-1) {
50 uint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10
51 >= 5)) ? 1 : 0;
52 n = malloc_snprintf(&buf[i], buflen-i,
53 "%"PRIu64, (t0 * mult / t1) % 10 + round);
54 i += n;
55 mult *= 10;
56 }
57 }