akaros/tests/pthread_barrier_test.c
<<
>>
Prefs
   1#include <parlib/stdio.h>
   2#include <pthread.h>
   3#include <stdlib.h>
   4#include <parlib/parlib.h>
   5#include <unistd.h>
   6#include <sys/time.h>
   7
   8pthread_barrier_t barrier;
   9
  10#define MAX_NR_TEST_THREADS 100000
  11int nr_threads = 100;
  12int nr_loops = 10000;
  13int nr_vcores = 0;
  14
  15pthread_t *my_threads;
  16void **my_retvals;
  17bool run_barriertest = FALSE;
  18
  19void *thread(void *arg)
  20{       
  21        while (!run_barriertest)
  22                cpu_relax();
  23        for (int i = 0; i < nr_loops; i++) {
  24                pthread_barrier_wait(&barrier);
  25        }
  26        return (void*)(long)pthread_self()->id;
  27}
  28
  29int main(int argc, char** argv) 
  30{
  31        struct timeval start_tv = {0};
  32        struct timeval end_tv = {0};
  33        long usec_diff;
  34
  35        if (argc > 1)
  36                nr_threads = strtol(argv[1], 0, 10);
  37        if (argc > 2)
  38                nr_loops = strtol(argv[2], 0, 10);
  39        if (argc > 3)
  40                nr_vcores = strtol(argv[3], 0, 10);
  41        printf("Running %d threads for %d iterations on %d vcores\n",
  42               nr_threads, nr_loops, nr_vcores);
  43        nr_threads = MIN(nr_threads, MAX_NR_TEST_THREADS);
  44        my_threads = malloc(sizeof(pthread_t) * nr_threads);
  45        my_retvals = malloc(sizeof(void*) * nr_threads);
  46        if (!(my_retvals && my_threads))
  47                perror("Init threads/malloc");
  48        if (nr_vcores) {
  49                /* Only do the vcore trickery if requested */
  50                parlib_never_yield = TRUE;
  51                pthread_mcp_init();             /* gives us one vcore */
  52                vcore_request_total(nr_vcores);
  53                parlib_never_vc_request = TRUE;
  54                for (int i = 0; i < nr_vcores; i++) {
  55                        printd("Vcore %d mapped to pcore %d\n", i,
  56                               __procinfo.vcoremap[i].pcoreid);
  57                }
  58        }
  59        pthread_barrier_init(&barrier, NULL, nr_threads);
  60        for (int i = 0; i < nr_threads; i++) {
  61                pthread_create(&my_threads[i], NULL, &thread, NULL);
  62        }
  63        if (gettimeofday(&start_tv, 0))
  64                perror("Start time error...");
  65        run_barriertest = TRUE;
  66        for (int i = 0; i < nr_threads; i++) {
  67                pthread_join(my_threads[i], &my_retvals[i]);
  68        }
  69        if (gettimeofday(&end_tv, 0))
  70                perror("End time error...");
  71        pthread_barrier_destroy(&barrier);
  72        usec_diff = (end_tv.tv_sec - start_tv.tv_sec) * 1000000 +
  73                    (end_tv.tv_usec - start_tv.tv_usec);
  74        printf("Done: %d threads, %d loops, %d vcores\n",
  75               nr_threads, nr_loops, nr_vcores);
  76        printf("Time to run: %d usec, %f usec per barrier\n", usec_diff,
  77               (float)usec_diff / nr_loops);
  78        pthread_barrier_destroy(&barrier);
  79} 
  80