void * second_thread(void *args) { struct second_thread_args *secargs = (struct second_thread_args *)args; int res; uint64_t i; kern_return_t kret; uint64_t wake_time; int cpuno; /* Set scheduling policy */ res = thread_setup(secargs->pol); if (res != 0) { printf("Couldn't set thread policy.\n"); exit(1); } /* * Repeatedly pick a random timer length and * try to sleep exactly that long */ for (i = 0; i < secargs->iterations; i++) { /* Wake up when poked by main thread */ kret = semaphore_wait(secargs->wakeup_semaphore); if (kret != KERN_SUCCESS) { errx(1, "semaphore_wait %d", kret); } wake_time = mach_absolute_time(); cpuno = cpu_number(); if (wake_time < secargs->last_poke_time) { /* Woke in past, unsynchronized mach_absolute_time()? */ errx(1, "woke in past %llu (%d) < %llu (%d)", wake_time, cpuno, secargs->last_poke_time, secargs->cpuno); } if (cpuno == secargs->cpuno) { secargs->woke_on_same_cpu++; } secargs->wakeup_second_jitter_arr[i] = (double)(wake_time - secargs->last_poke_time); /* Too much: cut a tracepoint for a debugger */ if (secargs->wakeup_second_jitter_arr[i] >= secargs->too_much) { kdebug_trace(0xeeeee4 | DBG_FUNC_NONE, 0, 0, 0, 0); } kret = semaphore_signal(secargs->return_semaphore); if (kret != KERN_SUCCESS) { errx(1, "semaphore_signal %d", kret); } } return NULL; }
static void loop_kdebug_trace(dt_stat_time_t s) { do { dt_stat_token start = dt_stat_time_begin(s); for (uint32_t i = 0; i<100; i++) { kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); kdebug_trace(0x97000000 | DBG_FUNC_NONE, i, i, i, i); } dt_stat_time_end_batch(s, 1000, start); } while (!dt_stat_stable(s)); }
int main(int argc, char **argv) { uint64_t iterations, i; double *jitter_arr, *fraction_arr; double *wakeup_second_jitter_arr; uint64_t target_time; uint64_t sleep_length_abs; uint64_t min_sleep_ns = 0; uint64_t max_sleep_ns = DEFAULT_MAX_SLEEP_NS; uint64_t wake_time; unsigned random_seed; boolean_t need_seed = TRUE; char ch; int res; kern_return_t kret; my_policy_type_t pol; boolean_t wakeup_second_thread = FALSE; semaphore_t wakeup_semaphore, return_semaphore; double avg, stddev, max, min; double avg_fract, stddev_fract, max_fract, min_fract; uint64_t too_much; struct second_thread_args secargs; pthread_t secthread; mach_timebase_info(&g_mti); /* Seed random */ opterr = 0; while ((ch = getopt(argc, argv, "m:n:hs:w")) != -1 && ch != '?') { switch (ch) { case 's': /* Specified seed for random)() */ random_seed = (unsigned)atoi(optarg); srandom(random_seed); need_seed = FALSE; break; case 'm': /* How long per timer? */ max_sleep_ns = strtoull(optarg, NULL, 10); break; case 'n': /* How long per timer? */ min_sleep_ns = strtoull(optarg, NULL, 10); break; case 'w': /* After each timed wait, wakeup another thread */ wakeup_second_thread = TRUE; break; case 'h': print_usage(); exit(0); break; default: fprintf(stderr, "Got unexpected result from getopt().\n"); exit(1); break; } } argc -= optind; argv += optind; if (argc != 3) { print_usage(); exit(1); } if (min_sleep_ns >= max_sleep_ns) { print_usage(); exit(1); } if (need_seed) { srandom(time(NULL)); } /* What scheduling policy? */ pol = parse_thread_policy(argv[0]); /* How many timers? */ iterations = strtoull(argv[1], NULL, 10); /* How much jitter is so extreme that we should cut a trace point */ too_much = strtoull(argv[2], NULL, 10); /* Array for data */ jitter_arr = (double*)malloc(sizeof(*jitter_arr) * iterations); if (jitter_arr == NULL) { printf("Couldn't allocate array to store results.\n"); exit(1); } fraction_arr = (double*)malloc(sizeof(*fraction_arr) * iterations); if (fraction_arr == NULL) { printf("Couldn't allocate array to store results.\n"); exit(1); } if (wakeup_second_thread) { /* Array for data */ wakeup_second_jitter_arr = (double*)malloc(sizeof(*jitter_arr) * iterations); if (wakeup_second_jitter_arr == NULL) { printf("Couldn't allocate array to store results.\n"); exit(1); } kret = semaphore_create(mach_task_self(), &wakeup_semaphore, SYNC_POLICY_FIFO, 0); if (kret != KERN_SUCCESS) { printf("Couldn't allocate semaphore %d\n", kret); exit(1); } kret = semaphore_create(mach_task_self(), &return_semaphore, SYNC_POLICY_FIFO, 0); if (kret != KERN_SUCCESS) { printf("Couldn't allocate semaphore %d\n", kret); exit(1); } secargs.wakeup_semaphore = wakeup_semaphore; secargs.return_semaphore = return_semaphore; secargs.iterations = iterations; secargs.pol = pol; secargs.wakeup_second_jitter_arr = wakeup_second_jitter_arr; secargs.woke_on_same_cpu = 0; secargs.too_much = too_much; secargs.last_poke_time = 0ULL; secargs.cpuno = 0; res = pthread_create(§hread, NULL, second_thread, &secargs); if (res) { err(1, "pthread_create"); } sleep(1); /* Time for other thread to start up */ } /* Set scheduling policy */ res = thread_setup(pol); if (res != 0) { printf("Couldn't set thread policy.\n"); exit(1); } /* * Repeatedly pick a random timer length and * try to sleep exactly that long */ for (i = 0; i < iterations; i++) { sleep_length_abs = (uint64_t) (get_random_sleep_length_abs_ns(min_sleep_ns, max_sleep_ns) * (((double)g_mti.denom) / ((double)g_mti.numer))); target_time = mach_absolute_time() + sleep_length_abs; /* Sleep */ kret = mach_wait_until(target_time); wake_time = mach_absolute_time(); jitter_arr[i] = (double)(wake_time - target_time); fraction_arr[i] = jitter_arr[i] / ((double)sleep_length_abs); /* Too much: cut a tracepoint for a debugger */ if (jitter_arr[i] >= too_much) { kdebug_trace(0xeeeee0 | DBG_FUNC_NONE, 0, 0, 0, 0); } if (wakeup_second_thread) { secargs.last_poke_time = mach_absolute_time(); secargs.cpuno = cpu_number(); OSMemoryBarrier(); kret = semaphore_signal(wakeup_semaphore); if (kret != KERN_SUCCESS) { errx(1, "semaphore_signal"); } kret = semaphore_wait(return_semaphore); if (kret != KERN_SUCCESS) { errx(1, "semaphore_wait"); } } } /* * Compute statistics and output results. */ compute_stats(jitter_arr, iterations, &avg, &max, &min, &stddev); compute_stats(fraction_arr, iterations, &avg_fract, &max_fract, &min_fract, &stddev_fract); putchar('\n'); print_stats_us("jitter", avg, max, min, stddev); print_stats_fract("%", avg_fract, max_fract, min_fract, stddev_fract); if (wakeup_second_thread) { res = pthread_join(secthread, NULL); if (res) { err(1, "pthread_join"); } compute_stats(wakeup_second_jitter_arr, iterations, &avg, &max, &min, &stddev); putchar('\n'); print_stats_us("second jitter", avg, max, min, stddev); putchar('\n'); printf("%llu/%llu (%.1f%%) wakeups on same CPU\n", secargs.woke_on_same_cpu, iterations, 100.0*((double)secargs.woke_on_same_cpu)/iterations); } return 0; }