#include #include #include struct timeval start, end; unsigned long long i, cnt; long utime, seconds, useconds; void enable_early() { system("echo 1 > /sys/devices/system/cpu/cpufreq/ondemand/early_demand"); } void disable_early() { system("echo 0 > /sys/devices/system/cpu/cpufreq/ondemand/early_demand"); } void calibrate() { sleep(1); gettimeofday(&start, NULL); for (i = 0; i < 1000000000; i++); gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; utime = seconds * 1000000 + useconds; printf("Calibrating\n"); printf("Elapsed time: %ld microseconds\n", utime); /* find the counter for 10ms */ cnt = i * 10000 / utime; printf("cnt: %ld\n", cnt); } long do_bench() { gettimeofday(&start, NULL); for (i = 0; i < cnt; i++); gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; utime = seconds * 1000000 + useconds; printf("Elapsed time: %ld microseconds\n", utime); return utime; } void benchmark() { const int iter = 20; long total_off = 0; long total_on = 0; int diff_perc; unsigned int i; /* calibrate(); */ cnt = 10000000; disable_early(); do_bench(); /* do a first benchmark but do not count in total */ sleep(5); for (i = 0; i < iter; i++) { total_off += do_bench(); usleep(500000); } total_off /= iter; printf("early_demand off - average time: %ld microseconds\n", total_off); enable_early(); do_bench(); /* do a first benchmark but do not count in total */ sleep(5); for (i = 0; i < iter; i++) { total_on += do_bench(); usleep(500000); } total_on /= iter; diff_perc = total_off - total_on; printf("early_demand on - average time: %ld microseconds\n", total_on); printf("diff: %d\n", diff_perc); } main () { printf("Starting benchmark\n"); benchmark(); }