#include #include #include #include #include #define RDTSC(pu64) \ __asm__ __volatile__("rdtsc" \ :"=a" (((unsigned long *)pu64)[0]), \ "=d" (((unsigned long *)pu64)[1])) #define BENCHMARK_CREATE(name, operator, dt) \ dt run_bench_##name(dt lval, dt rval, char *l, char *r) \ { \ int ret; \ \ RDTSC(&before); \ ret = (lval operator rval); \ RDTSC(&after); \ printf("%s: %s %s %s: %lld cycles\n", #name, l, \ #operator, r, (after - before) - bias); \ \ return ret; \ } #define BENCHMARK_RUN(name, lval, rval) \ run_bench_##name(lval, rval, #lval, #rval) long long before, after, bias; BENCHMARK_CREATE(gt, >, int) BENCHMARK_CREATE(lt, <, int) BENCHMARK_CREATE(eq, ==, int) BENCHMARK_CREATE(ne, !=, int) BENCHMARK_CREATE(fpgt, >, float) void dummy(int); int main(int argc, char *argv[]) { struct sched_param p; struct timespec ts; // int x; // void *gah; if (sched_rr_get_interval(getpid(), &ts) < 0) { perror("sched_rr_get_interval"); return 0; } printf("maximum time quantum: %ld.%9ld seconds\n", ts.tv_sec, ts.tv_nsec); memset(&p, 0, sizeof(p)); p.sched_priority = 99; if (sched_setscheduler(getpid(), SCHED_RR, &p) < 0) perror("sched_setscheduler"); // bah. // so I thought this would give me more consistent results with the // tests below, perhaps due to scheduler weirdness. boy, was I ever wrong. // for (x = 0; x < 10000000; x++) // dummy(x); RDTSC(&before); RDTSC(&after); bias = after - before; printf("rdtsc bias: %lld cycles\n", bias); #if 0 RDTSC(&before); gah = main; RDTSC(&after); printf("test bias: %d cycles\n", after - before); bias += after - before; #endif BENCHMARK_RUN(gt, 50, 20); BENCHMARK_RUN(gt, 20, 50); BENCHMARK_RUN(gt, 1, 0); BENCHMARK_RUN(lt, 50, 20); BENCHMARK_RUN(eq, 50, 20); BENCHMARK_RUN(eq, 50, 50); BENCHMARK_RUN(eq, 1, 0); BENCHMARK_RUN(fpgt, 50.0, 20.0); BENCHMARK_RUN(fpgt, 50.0, 20.0); BENCHMARK_RUN(fpgt, 50.0, 20.0); return 0; } void dummy(int x) { }