xref: /llvm-project/compiler-rt/test/tsan/bench.h (revision c4cb9b64dd350f0d675e12d38ea54f4e0c3ceb37)
1 #include "test.h"
2 #include <time.h>
3 
4 int bench_nthread;
5 int bench_niter;
6 int bench_mode;
7 
8 void bench();  // defined by user
9 void start_thread_group(int nth, void(*f)(int tid));
10 
main(int argc,char ** argv)11 int main(int argc, char **argv) {
12   bench_nthread = 2;
13   if (argc > 1)
14     bench_nthread = atoi(argv[1]);
15   bench_niter = 100;
16   if (argc > 2)
17     bench_niter = atoi(argv[2]);
18   if (argc > 3)
19     bench_mode = atoi(argv[3]);
20 
21   timespec tp0;
22   clock_gettime(CLOCK_MONOTONIC, &tp0);
23   bench();
24   timespec tp1;
25   clock_gettime(CLOCK_MONOTONIC, &tp1);
26   unsigned long long t =
27       (tp1.tv_sec * 1000000000ULL + tp1.tv_nsec) -
28       (tp0.tv_sec * 1000000000ULL + tp0.tv_nsec);
29   fprintf(stderr, "%llu ns/iter\n", t / bench_niter);
30   fprintf(stderr, "DONE\n");
31 }
32 
start_thread_group(int nth,void (* f)(int tid))33 void start_thread_group(int nth, void(*f)(int tid)) {
34   pthread_t *th = (pthread_t*)malloc(nth * sizeof(pthread_t));
35   for (int i = 0; i < nth; i++)
36     pthread_create(&th[i], 0, (void*(*)(void*))f, (void*)(long)i);
37   for (int i = 0; i < nth; i++)
38     pthread_join(th[i], 0);
39 }
40