Skip to content

Instantly share code, notes, and snippets.

@jmmlmendes
Last active April 10, 2016 18:42
Show Gist options
  • Save jmmlmendes/69fb09c6f71ae02af42bf8253d0188aa to your computer and use it in GitHub Desktop.
Save jmmlmendes/69fb09c6f71ae02af42bf8253d0188aa to your computer and use it in GitHub Desktop.
measure rumprun sched_yield() time in infinite loop
#include <err.h>
#include <stdio.h>
#include <limits.h>
#include <sys/time.h>
#include <sched.h>
#define ITERATIONS 10000000
/*
* Steps:
* 1) Measure time for clock_gettime()
* 2) Measure time for sched_yield()
* 3) Present final number for sched_yield(), subtracting time for clock_gettime()
*/
int main(int argc, char *argv[])
{
struct timespec first_ts, second_ts;
long max_clk_t = 0;
long min_clk_t = LONG_MAX;
long long avg_clk_t = 0LL;
long max_yield_t = 0;
long min_yield_t = LONG_MAX;
long long avg_yield_t = 0LL;
long time_sysc;
int iter;
/* Measure time to execute clock_gettime() */
for (;;)
{
if (clock_gettime(CLOCK_REALTIME, &first_ts))
err(1, "clock_gettime");
if (clock_gettime(CLOCK_REALTIME, &second_ts))
err(1, "clock_gettime");
/* Just ignore this situation, simplifies the math */
if (first_ts.tv_sec != second_ts.tv_sec)
continue;
time_sysc = (long)second_ts.tv_nsec - (long)first_ts.tv_nsec;
if (time_sysc > max_clk_t)
max_clk_t = time_sysc;
if (time_sysc < min_clk_t)
min_clk_t = time_sysc;
avg_clk_t += time_sysc;
if (++iter >= ITERATIONS)
break;
}
avg_clk_t /= ITERATIONS;
printf("------------------ clock_gettime() stats ------------------\n");
printf("avg = %lldns; max = %ldns; min = %ldns\n", avg_clk_t, max_clk_t, min_clk_t);
printf("==========================================\n");
/****************************************/
/* Measure time to execute sched_yield() */
iter = 0;
for (;;)
{
if (clock_gettime(CLOCK_REALTIME, &first_ts))
err(1, "clock_gettime");
sched_yield();
if (clock_gettime(CLOCK_REALTIME, &second_ts))
err(1, "clock_gettime");
/* Just ignore this situation, simplifies the math */
if (first_ts.tv_sec != second_ts.tv_sec)
continue;
time_sysc = (long)second_ts.tv_nsec - (long)first_ts.tv_nsec;
if (time_sysc > max_yield_t)
max_yield_t = time_sysc;
if (time_sysc < min_yield_t)
min_yield_t = time_sysc;
avg_yield_t += time_sysc;
if (++iter >= ITERATIONS)
break;
}
avg_yield_t /= ITERATIONS;
printf("------------------ sched_yield() stats ------------------\n");
printf("avg = %lldns; max = %ldns; min = %ldns\n", avg_yield_t, max_yield_t, min_yield_t);
printf("sched_yield() overhead : %ldns\n", min_yield_t - min_clk_t);
printf("==========================================\n");
/****************************************/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment