1353944Sdim// Mini-benchmark for creating a lot of threads. 2353944Sdim// 3353944Sdim// Some facts: 4353944Sdim// a) clang -O1 takes <15ms to start N=500 threads, 5353944Sdim// consuming ~4MB more RAM than N=1. 6353944Sdim// b) clang -O1 -ftsan takes ~26s to start N=500 threads, 7353944Sdim// eats 5GB more RAM than N=1 (which is somewhat expected but still a lot) 8353944Sdim// but then it consumes ~4GB of extra memory when the threads shut down! 9353944Sdim// (definitely not in the barrier_wait interceptor) 10353944Sdim// Also, it takes 26s to run with N=500 vs just 1.1s to run with N=1. 11353944Sdim#include <assert.h> 12353944Sdim#include <pthread.h> 13353944Sdim#include <stdio.h> 14353944Sdim#include <stdlib.h> 15353944Sdim#include <unistd.h> 16353944Sdim 17353944Sdimpthread_barrier_t all_threads_ready; 18353944Sdim 19353944Sdimvoid* Thread(void *unused) { 20353944Sdim pthread_barrier_wait(&all_threads_ready); 21353944Sdim return 0; 22353944Sdim} 23353944Sdim 24353944Sdimint main(int argc, char **argv) { 25353944Sdim int n_threads; 26353944Sdim if (argc == 1) { 27353944Sdim n_threads = 100; 28353944Sdim } else if (argc == 2) { 29353944Sdim n_threads = atoi(argv[1]); 30353944Sdim } else { 31353944Sdim printf("Usage: %s n_threads\n", argv[0]); 32353944Sdim return 1; 33353944Sdim } 34353944Sdim printf("%s: n_threads=%d\n", __FILE__, n_threads); 35353944Sdim 36353944Sdim pthread_barrier_init(&all_threads_ready, NULL, n_threads + 1); 37353944Sdim 38353944Sdim pthread_t *t = new pthread_t[n_threads]; 39353944Sdim for (int i = 0; i < n_threads; i++) { 40353944Sdim int status = pthread_create(&t[i], 0, Thread, (void*)i); 41353944Sdim assert(status == 0); 42353944Sdim } 43353944Sdim // sleep(5); // FIXME: simplify measuring the memory usage. 44353944Sdim pthread_barrier_wait(&all_threads_ready); 45353944Sdim for (int i = 0; i < n_threads; i++) { 46353944Sdim pthread_join(t[i], 0); 47353944Sdim } 48353944Sdim // sleep(5); // FIXME: simplify measuring the memory usage. 49353944Sdim delete [] t; 50353944Sdim 51353944Sdim return 0; 52353944Sdim} 53