xref: /freebsd/contrib/llvm-project/compiler-rt/lib/tsan/benchmarks/start_many_threads.cpp (revision 5b27928474e6a4103d65b347544705c40c9618fd)
1*68d75effSDimitry Andric // Mini-benchmark for creating a lot of threads.
2*68d75effSDimitry Andric //
3*68d75effSDimitry Andric // Some facts:
4*68d75effSDimitry Andric // a) clang -O1 takes <15ms to start N=500 threads,
5*68d75effSDimitry Andric //    consuming ~4MB more RAM than N=1.
6*68d75effSDimitry Andric // b) clang -O1 -ftsan takes ~26s to start N=500 threads,
7*68d75effSDimitry Andric //    eats 5GB more RAM than N=1 (which is somewhat expected but still a lot)
8*68d75effSDimitry Andric //    but then it consumes ~4GB of extra memory when the threads shut down!
9*68d75effSDimitry Andric //        (definitely not in the barrier_wait interceptor)
10*68d75effSDimitry Andric //    Also, it takes 26s to run with N=500 vs just 1.1s to run with N=1.
11*68d75effSDimitry Andric #include <assert.h>
12*68d75effSDimitry Andric #include <pthread.h>
13*68d75effSDimitry Andric #include <stdio.h>
14*68d75effSDimitry Andric #include <stdlib.h>
15*68d75effSDimitry Andric #include <unistd.h>
16*68d75effSDimitry Andric 
17*68d75effSDimitry Andric pthread_barrier_t all_threads_ready;
18*68d75effSDimitry Andric 
Thread(void * unused)19*68d75effSDimitry Andric void* Thread(void *unused) {
20*68d75effSDimitry Andric   pthread_barrier_wait(&all_threads_ready);
21*68d75effSDimitry Andric   return 0;
22*68d75effSDimitry Andric }
23*68d75effSDimitry Andric 
main(int argc,char ** argv)24*68d75effSDimitry Andric int main(int argc, char **argv) {
25*68d75effSDimitry Andric   int n_threads;
26*68d75effSDimitry Andric   if (argc == 1) {
27*68d75effSDimitry Andric     n_threads = 100;
28*68d75effSDimitry Andric   } else if (argc == 2) {
29*68d75effSDimitry Andric     n_threads = atoi(argv[1]);
30*68d75effSDimitry Andric   } else {
31*68d75effSDimitry Andric     printf("Usage: %s n_threads\n", argv[0]);
32*68d75effSDimitry Andric     return 1;
33*68d75effSDimitry Andric   }
34*68d75effSDimitry Andric   printf("%s: n_threads=%d\n", __FILE__, n_threads);
35*68d75effSDimitry Andric 
36*68d75effSDimitry Andric   pthread_barrier_init(&all_threads_ready, NULL, n_threads + 1);
37*68d75effSDimitry Andric 
38*68d75effSDimitry Andric   pthread_t *t = new pthread_t[n_threads];
39*68d75effSDimitry Andric   for (int i = 0; i < n_threads; i++) {
40*68d75effSDimitry Andric     int status = pthread_create(&t[i], 0, Thread, (void*)i);
41*68d75effSDimitry Andric     assert(status == 0);
42*68d75effSDimitry Andric   }
43*68d75effSDimitry Andric   // sleep(5);  // FIXME: simplify measuring the memory usage.
44*68d75effSDimitry Andric   pthread_barrier_wait(&all_threads_ready);
45*68d75effSDimitry Andric   for (int i = 0; i < n_threads; i++) {
46*68d75effSDimitry Andric     pthread_join(t[i], 0);
47*68d75effSDimitry Andric   }
48*68d75effSDimitry Andric   // sleep(5);  // FIXME: simplify measuring the memory usage.
49*68d75effSDimitry Andric   delete [] t;
50*68d75effSDimitry Andric 
51*68d75effSDimitry Andric   return 0;
52*68d75effSDimitry Andric }
53