1 #define _GNU_SOURCE 2 #include <string.h> 3 #include <pthread.h> 4 #include <stdlib.h> 5 #include <stdio.h> 6 #include <fcntl.h> 7 #include <unistd.h> 8 #include <sys/types.h> 9 #include <sys/syscall.h> 10 #include <sched.h> 11 12 static int loops = 15; // each thread+main will do this amount of loop 13 static int sleepms = 1000; // in each loop, will sleep "sleepms" milliseconds 14 static int burn = 0; // after each sleep, will burn cpu in a tight 'burn' loop 15 16 17 static pid_t gettid() 18 { 19 #ifdef __NR_gettid 20 return syscall(__NR_gettid); 21 #else 22 return getpid(); 23 #endif 24 } 25 // will be invoked from gdb. 26 static void whoami(char *msg) __attribute__((unused)); 27 static void whoami(char *msg) 28 { 29 fprintf(stderr, "pid %d Thread %d %s\n", getpid(), gettid(), msg); 30 fflush(stderr); 31 } 32 33 34 static void do_burn () 35 { 36 int i; 37 int loopnr = 0; 38 // one single line for the below, to ensure interrupt on this line. 39 for (i = 0; i < burn; i++) loopnr++; 40 } 41 42 static int thread_ready = 0; 43 static pthread_cond_t ready = PTHREAD_COND_INITIALIZER; 44 static pthread_mutex_t ready_mutex = PTHREAD_MUTEX_INITIALIZER; 45 static void signal_ready (void) 46 { 47 int rc; 48 rc = pthread_mutex_lock(&ready_mutex); 49 if (rc != 0) 50 fprintf(stderr, "signal_ready lock error %d_n", rc); 51 thread_ready = 1; 52 rc = pthread_cond_signal(&ready); 53 if (rc != 0) 54 fprintf(stderr, "signal_ready signal error %d_n", rc); 55 rc = pthread_mutex_unlock(&ready_mutex); 56 if (rc != 0) 57 fprintf(stderr, "signal_ready unlock error %d_n", rc); 58 } 59 60 struct spec { 61 char *name; 62 int sleep; 63 int burn; 64 int t; 65 }; 66 static struct timeval t[4]; 67 static int nr_sleeper_or_burner = 0; 68 static volatile int report_finished = 1; 69 // set to 0 to have no finish msg (as order is non-deterministic) 70 static void *sleeper_or_burner(void *v) 71 { 72 int i = 0; 73 struct spec* s = (struct spec*)v; 74 75 fprintf(stderr, "%s ready to sleep and/or burn\n", s->name); 76 fflush (stderr); 77 signal_ready(); 78 nr_sleeper_or_burner++; 79 80 for (i = 0; i < loops; i++) { 81 if (sleepms > 0 && s->sleep) { 82 t[s->t].tv_sec = sleepms / 1000; 83 t[s->t].tv_usec = (sleepms % 1000) * 1000; 84 select (0, NULL, NULL, NULL, &t[s->t]); 85 } 86 if (burn > 0 && s->burn) 87 do_burn(); 88 } 89 if (report_finished) { 90 fprintf(stderr, "%s finished to sleep and/or burn\n", s->name); 91 fflush (stderr); 92 } 93 return NULL; 94 } 95 96 // wait till a thread signals it is ready 97 static void wait_ready(void) 98 { 99 int rc; 100 rc = pthread_mutex_lock(&ready_mutex); 101 if (rc != 0) 102 fprintf(stderr, "wait_ready lock error %d_n", rc); 103 while (! thread_ready && rc == 0) { 104 rc = pthread_cond_wait(&ready, &ready_mutex); 105 if (rc != 0) 106 fprintf(stderr, "wait_ready wait error %d_n", rc); 107 } 108 thread_ready = 0; 109 rc = pthread_mutex_unlock(&ready_mutex); 110 if (rc != 0) 111 fprintf(stderr, "wait_ready unlock error %d_n", rc); 112 } 113 114 // We will lock ourselves on one single cpu. 115 // This bypasses the unfairness of the Valgrind scheduler 116 // when a multi-cpu machine has enough cpu to run all the 117 // threads wanting to burn cpu. 118 static void setaffinity(void) 119 { 120 #ifdef VGO_linux 121 cpu_set_t single_cpu; 122 CPU_ZERO(&single_cpu); 123 CPU_SET(1, &single_cpu); 124 (void) sched_setaffinity(0, sizeof(single_cpu), &single_cpu); 125 #endif 126 // GDBTD: equivalent for Darwin ? 127 } 128 129 int main (int argc, char *argv[]) 130 { 131 char *threads_spec; 132 pthread_t ebbr, egll, zzzz; 133 struct spec b, l, p, m; 134 char *some_mem __attribute__((unused)) = malloc(100); 135 setaffinity(); 136 137 if (argc > 1) 138 loops = atoi(argv[1]); 139 140 if (argc > 2) 141 sleepms = atoi(argv[2]); 142 143 if (argc > 3) 144 burn = atoll(argv[3]); 145 146 if (argc > 4) 147 threads_spec = argv[4]; 148 else 149 threads_spec = "BSBSBSBS"; 150 151 fprintf(stderr, "loops/sleep_ms/burn/threads_spec: %d %d %d %s\n", 152 loops, sleepms, burn, threads_spec); 153 fflush(stderr); 154 155 b.name = "Brussels"; 156 b.burn = *threads_spec++ == 'B'; 157 b.sleep = *threads_spec++ == 'S'; 158 b.t = -1; 159 if (b.burn || b.sleep) { 160 b.t = 1; 161 pthread_create(&ebbr, NULL, sleeper_or_burner, &b); 162 wait_ready(); 163 } 164 165 l.name = "London"; 166 l.burn = *threads_spec++ == 'B'; 167 l.sleep = *threads_spec++ == 'S'; 168 l.t = -1; 169 if (l.burn || l.sleep) { 170 l.t = 2; 171 pthread_create(&egll, NULL, sleeper_or_burner, &l); 172 wait_ready(); 173 } 174 175 p.name = "Petaouchnok"; 176 p.burn = *threads_spec++ == 'B'; 177 p.sleep = *threads_spec++ == 'S'; 178 p.t = -1; 179 if (p.burn || p.sleep) { 180 p.t = 3; 181 pthread_create(&zzzz, NULL, sleeper_or_burner, &p); 182 wait_ready(); 183 } 184 185 m.name = "main"; 186 m.burn = *threads_spec++ == 'B'; 187 m.sleep = *threads_spec++ == 'S'; 188 m.t = 0; 189 sleeper_or_burner(&m); 190 191 if (b.t != -1) pthread_join(ebbr, NULL); 192 if (l.t != -1) pthread_join(egll, NULL); 193 if (p.t != -1) pthread_join(zzzz, NULL); 194 195 return 0; 196 } 197