int main(int argc, char** argv) { struct timeval start_tv = {0}; struct timeval end_tv = {0}; long usec_diff; long nr_ctx_switches; void *join_ret; if (argc > 1) nr_switch_loops = strtol(argv[1], 0, 10); printf("Making 2 threads of %d switches each\n", nr_switch_loops); pthread_can_vcore_request(FALSE); /* 2LS won't manage vcores */ pthread_need_tls(FALSE); pthread_lib_init(); /* gives us one vcore */ /* each is passed the other's pthread_t. th1 starts the switching. */ if (pthread_create(&th1, NULL, &switch_thread, &th2)) perror("pth_create 1 failed"); /* thread 2 is created, but not put on the runnable list */ if (__pthread_create(&th2, NULL, &switch_thread, &th1)) perror("pth_create 2 failed"); if (gettimeofday(&start_tv, 0)) perror("Start time error..."); ready = TRUE; /* signal to any spinning uthreads to start */ pthread_join(th1, &join_ret); pthread_join(th2, &join_ret); if (gettimeofday(&end_tv, 0)) perror("End time error..."); nr_ctx_switches = 2 * nr_switch_loops; usec_diff = (end_tv.tv_sec - start_tv.tv_sec) * 1000000 + (end_tv.tv_usec - start_tv.tv_usec); printf("Done: %d loops\n", nr_switch_loops); printf("Nr context switches: %ld\n", nr_ctx_switches); printf("Time to run: %ld usec\n", usec_diff); printf("Context switch latency: %d nsec\n", (int)(1000LL*usec_diff / nr_ctx_switches)); printf("Context switches / sec: %d\n\n", (int)(1000000LL*nr_ctx_switches / usec_diff)); }
int main(int argc, char** argv) { struct timeval start_tv = {0}; struct timeval end_tv = {0}; long usec_diff; long nr_ctx_switches; if (argc > 1) nr_yield_threads = strtol(argv[1], 0, 10); if (argc > 2) nr_yield_loops = strtol(argv[2], 0, 10); if (argc > 3) nr_vcores = strtol(argv[3], 0, 10); if (argc > 4) amt_fake_work = strtol(argv[4], 0, 10); nr_yield_threads = MIN(nr_yield_threads, MAX_NR_TEST_THREADS); printf("Making %d threads of %d loops each, on %d vcore(s), %d work\n", nr_yield_threads, nr_yield_loops, nr_vcores, amt_fake_work); /* OS dependent prep work */ if (nr_vcores) { /* Only do the vcore trickery if requested */ pthread_can_vcore_request(FALSE); /* 2LS won't manage vcores */ pthread_need_tls(FALSE); pthread_lib_init(); /* gives us one vcore */ vcore_request(nr_vcores - 1); /* ghetto incremental interface */ for (int i = 0; i < nr_vcores; i++) { printd("Vcore %d mapped to pcore %d\n", i, __procinfo.vcoremap[i].pcoreid); } } /* create and join on yield */ for (int i = 0; i < nr_yield_threads; i++) { printf_safe("[A] About to create thread %d\n", i); if (pthread_create(&my_threads[i], NULL, &yield_thread, NULL)) perror("pth_create failed"); } if (gettimeofday(&start_tv, 0)) perror("Start time error..."); ready = TRUE; /* signal to any spinning uthreads to start */ for (int i = 0; i < nr_yield_threads; i++) { printf_safe("[A] About to join on thread %d(%p)\n", i, my_threads[i]); pthread_join(my_threads[i], &my_retvals[i]); printf_safe("[A] Successfully joined on thread %d (retval: %p)\n", i, my_retvals[i]); } if (gettimeofday(&end_tv, 0)) perror("End time error..."); nr_ctx_switches = nr_yield_threads * nr_yield_loops; usec_diff = (end_tv.tv_sec - start_tv.tv_sec) * 1000000 + (end_tv.tv_usec - start_tv.tv_usec); printf("Done: %d uthreads, %d loops, %d vcores, %d work\n", nr_yield_threads, nr_yield_loops, nr_vcores, amt_fake_work); printf("Nr context switches: %d\n", nr_ctx_switches); printf("Time to run: %d usec\n", usec_diff); if (nr_vcores == 1) printf("Context switch latency: %d nsec\n", (int)(1000LL*usec_diff / nr_ctx_switches)); printf("Context switches / sec: %d\n\n", (int)(1000000LL*nr_ctx_switches / usec_diff)); }