Home | History | Annotate | Download | only in src
      1 #include "test/jemalloc_test.h"
      2 
      3 void
      4 timer_start(timedelta_t *timer)
      5 {
      6 
      7 #if JEMALLOC_CLOCK_GETTIME
      8 	if (sysconf(_SC_MONOTONIC_CLOCK) <= 0)
      9 		timer->clock_id = CLOCK_REALTIME;
     10 	else
     11 		timer->clock_id = CLOCK_MONOTONIC;
     12 	clock_gettime(timer->clock_id, &timer->tv0);
     13 #else
     14 	gettimeofday(&timer->tv0, NULL);
     15 #endif
     16 }
     17 
     18 void
     19 timer_stop(timedelta_t *timer)
     20 {
     21 
     22 #if JEMALLOC_CLOCK_GETTIME
     23 	clock_gettime(timer->clock_id, &timer->tv1);
     24 #else
     25 	gettimeofday(&timer->tv1, NULL);
     26 #endif
     27 }
     28 
     29 uint64_t
     30 timer_usec(const timedelta_t *timer)
     31 {
     32 
     33 #if JEMALLOC_CLOCK_GETTIME
     34 	return (((timer->tv1.tv_sec - timer->tv0.tv_sec) * 1000000) +
     35 	    (timer->tv1.tv_nsec - timer->tv0.tv_nsec) / 1000);
     36 #else
     37 	return (((timer->tv1.tv_sec - timer->tv0.tv_sec) * 1000000) +
     38 	    timer->tv1.tv_usec - timer->tv0.tv_usec);
     39 #endif
     40 }
     41 
     42 void
     43 timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen)
     44 {
     45 	uint64_t t0 = timer_usec(a);
     46 	uint64_t t1 = timer_usec(b);
     47 	uint64_t mult;
     48 	unsigned i = 0;
     49 	unsigned j;
     50 	int n;
     51 
     52 	/* Whole. */
     53 	n = malloc_snprintf(&buf[i], buflen-i, "%"PRIu64, t0 / t1);
     54 	i += n;
     55 	if (i >= buflen)
     56 		return;
     57 	mult = 1;
     58 	for (j = 0; j < n; j++)
     59 		mult *= 10;
     60 
     61 	/* Decimal. */
     62 	n = malloc_snprintf(&buf[i], buflen-i, ".");
     63 	i += n;
     64 
     65 	/* Fraction. */
     66 	while (i < buflen-1) {
     67 		uint64_t round = (i+1 == buflen-1 && ((t0 * mult * 10 / t1) % 10
     68 		    >= 5)) ? 1 : 0;
     69 		n = malloc_snprintf(&buf[i], buflen-i,
     70 		    "%"PRIu64, (t0 * mult / t1) % 10 + round);
     71 		i += n;
     72 		mult *= 10;
     73 	}
     74 }
     75