File size: 4,837 Bytes
8be366a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | // Evaluate cost of calling different clocks under Linux. On one machine at a
// point in time, we got these values:
//
// time : 3 cycles
// ftime : 54 cycles
// gettimeofday : 42 cycles
//
// CLOCK_MONOTONIC_COARSE : 9 cycles
// CLOCK_REALTIME_COARSE : 9 cycles
// CLOCK_MONOTONIC : 42 cycles
// CLOCK_REALTIME : 42 cycles
// CLOCK_MONOTONIC_RAW : 173 cycles
// CLOCK_BOOTTIME : 179 cycles
// CLOCK_THREAD_CPUTIME_ID : 349 cycles
// CLOCK_PROCESS_CPUTIME_ID : 370 cycles
//
// Numbers above generated on Intel Core i7-4771 CPU @ 3.50GHz on Linux 4.0.
//
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <time.h>
#include "tsc.h"
#define N 100000
/* measure the cost to call `time`. */
void time_overhead()
{
int i;
uint64_t t0, t1, tsc_overhead;
uint64_t min, max, avg;
uint64_t times[N];
tsc_overhead = measure_tsc_overhead();
// we run N times and take the min
for (i = 0; i < N; i++) {
t0 = bench_start();
time(NULL);
t1 = bench_end();
times[i] = t1 - t0 - tsc_overhead;
}
min = ~0, max = 0, avg = 0;
for (i = 0; i < N; i++) {
avg += times[i];
if (times[i] < min) { min = times[i]; }
if (times[i] > max) { max = times[i]; }
}
avg /= N;
printf("\n- TIME -\n");
printf("time_getres: 1s\n");
printf("Cost (min): %" PRIu64 " cycles\n", min);
printf("Cost (avg): %" PRIu64 " cycles\n", avg);
printf("Cost (max): %" PRIu64 " cycles\n", max);
}
/* measure the cost to call `ftime`. */
void ftime_overhead()
{
int i;
struct timeb t;
uint64_t t0, t1, tsc_overhead;
uint64_t min, max, avg;
uint64_t times[N];
tsc_overhead = measure_tsc_overhead();
// we run N times and take the min
for (i = 0; i < N; i++) {
t0 = bench_start();
ftime(&t);
t1 = bench_end();
times[i] = t1 - t0 - tsc_overhead;
}
min = ~0, max = 0, avg = 0;
for (i = 0; i < N; i++) {
avg += times[i];
if (times[i] < min) { min = times[i]; }
if (times[i] > max) { max = times[i]; }
}
avg /= N;
printf("\n- FTIME -\n");
printf("ftime_getres: 1ms\n");
printf("Cost (min): %" PRIu64 " cycles\n", min);
printf("Cost (avg): %" PRIu64 " cycles\n", avg);
printf("Cost (max): %" PRIu64 " cycles\n", max);
}
/* measure the cost to call `gettimeofday`. */
void gettimeofday_overhead()
{
int i;
struct timeval t;
uint64_t t0, t1, tsc_overhead;
uint64_t min, max, avg;
uint64_t times[N];
tsc_overhead = measure_tsc_overhead();
// we run N times and take the min
for (i = 0; i < N; i++) {
t0 = bench_start();
gettimeofday(&t, NULL);
t1 = bench_end();
times[i] = t1 - t0 - tsc_overhead;
}
min = ~0, max = 0, avg = 0;
for (i = 0; i < N; i++) {
avg += times[i];
if (times[i] < min) { min = times[i]; }
if (times[i] > max) { max = times[i]; }
}
avg /= N;
printf("\n- GETTIMEOFDAY -\n");
printf("gettimeofday_getres: 1us (?)\n");
printf("Cost (min): %" PRIu64 " cycles\n", min);
printf("Cost (avg): %" PRIu64 " cycles\n", avg);
printf("Cost (max): %" PRIu64 " cycles\n", max);
}
/* measure the cost to call `clock_gettime` for the specified clock. */
void clock_overhead(clockid_t clock)
{
int i;
struct timespec t;
uint64_t t0, t1, tsc_overhead;
uint64_t min, max, avg;
uint64_t times[N];
tsc_overhead = measure_tsc_overhead();
// we run N times and take the min
for (i = 0; i < N; i++) {
t0 = bench_start();
clock_gettime(clock, &t);
t1 = bench_end();
times[i] = t1 - t0 - tsc_overhead;
}
min = ~0, max = 0, avg = 0;
for (i = 0; i < N; i++) {
avg += times[i];
if (times[i] < min) { min = times[i]; }
if (times[i] > max) { max = times[i]; }
}
avg /= N;
printf("Cost (min): %" PRIu64 " cycles\n", min);
printf("Cost (avg): %" PRIu64 " cycles\n", avg);
printf("Cost (max): %" PRIu64 " cycles\n", max);
}
/* measure + print the cost to call `clock_gettime` for the specified clock. */
void measure_clock(const char * ctype, clockid_t clock)
{
struct timespec t;
printf("\n- %s -\n", ctype);
clock_getres(clock, &t);
printf("clock_getres: %ld seconds | %ld nanoseconds\n", t.tv_sec, t.tv_nsec);
clock_overhead(clock);
}
#define eval_clock(clk) \
measure_clock(#clk, clk)
/* benchmark various clock sources */
int main(void)
{
printf("=> Testing Clock...\n");
time_overhead();
ftime_overhead();
gettimeofday_overhead();
eval_clock(CLOCK_REALTIME);
eval_clock(CLOCK_REALTIME_COARSE);
eval_clock(CLOCK_MONOTONIC);
eval_clock(CLOCK_MONOTONIC_COARSE);
eval_clock(CLOCK_MONOTONIC_RAW);
eval_clock(CLOCK_BOOTTIME);
eval_clock(CLOCK_PROCESS_CPUTIME_ID);
eval_clock(CLOCK_THREAD_CPUTIME_ID);
return 0;
}
|