• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <signal.h>
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 static void setup_sigusr_handler(void); // sigusr1 and 2 sigaction setup.
16 
gettid()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));
whoami(char * msg)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 
do_burn()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;
signal_ready(void)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)
sleeper_or_burner(void * v)70 static void *sleeper_or_burner(void *v)
71 {
72    int i = 0;
73    struct spec* s = (struct spec*)v;
74    int ret;
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          ret = select (0, NULL, NULL, NULL, &t[s->t]);
85          /* We only expect a timeout result from the above. */
86          if (ret != 0)
87             perror("unexpected result from select");
88       }
89       if (burn > 0 && s->burn)
90          do_burn();
91    }
92    if (report_finished) {
93       fprintf(stderr, "%s finished to sleep and/or burn\n", s->name);
94       fflush (stderr);
95    }
96    return NULL;
97 }
98 
99 // wait till a thread signals it is ready
wait_ready(void)100 static void wait_ready(void)
101 {
102    int rc;
103    rc = pthread_mutex_lock(&ready_mutex);
104    if (rc != 0)
105       fprintf(stderr, "wait_ready lock error %d_n", rc);
106    while (! thread_ready && rc == 0) {
107       rc = pthread_cond_wait(&ready, &ready_mutex);
108       if (rc != 0)
109          fprintf(stderr, "wait_ready wait error %d_n", rc);
110    }
111    thread_ready = 0;
112    rc = pthread_mutex_unlock(&ready_mutex);
113    if (rc != 0)
114       fprintf(stderr, "wait_ready unlock error %d_n", rc);
115 }
116 
117 // We will lock ourselves on one single cpu.
118 // This bypasses the unfairness of the Valgrind scheduler
119 // when a multi-cpu machine has enough cpu to run all the
120 // threads wanting to burn cpu.
setaffinity(void)121 static void setaffinity(void)
122 {
123 #ifdef VGO_linux
124    cpu_set_t single_cpu;
125    CPU_ZERO(&single_cpu);
126    CPU_SET(1, &single_cpu);
127    (void) sched_setaffinity(0, sizeof(single_cpu), &single_cpu);
128 #endif
129    // GDBTD: equivalent for Darwin ?
130 }
131 
main(int argc,char * argv[])132 int main (int argc, char *argv[])
133 {
134   char *threads_spec;
135   pthread_t ebbr, egll, zzzz;
136   struct spec b, l, p, m;
137   char *some_mem __attribute__((unused)) = malloc(100);
138   setaffinity();
139   setup_sigusr_handler();
140   if (argc > 1)
141      loops = atoi(argv[1]);
142 
143   if (argc > 2)
144      sleepms = atoi(argv[2]);
145 
146   if (argc > 3)
147      burn = atoll(argv[3]);
148 
149   if (argc > 4)
150      threads_spec = argv[4];
151   else
152      threads_spec = "BSBSBSBS";
153 
154   fprintf(stderr, "loops/sleep_ms/burn/threads_spec:  %d %d %d %s\n",
155           loops, sleepms, burn, threads_spec);
156   fflush(stderr);
157 
158   b.name = "Brussels";
159   b.burn = *threads_spec++ == 'B';
160   b.sleep = *threads_spec++ == 'S';
161   b.t = -1;
162   if (b.burn || b.sleep) {
163      b.t = 1;
164      pthread_create(&ebbr, NULL, sleeper_or_burner, &b);
165      wait_ready();
166   }
167 
168   l.name = "London";
169   l.burn = *threads_spec++ == 'B';
170   l.sleep = *threads_spec++ == 'S';
171   l.t = -1;
172   if (l.burn || l.sleep) {
173      l.t = 2;
174      pthread_create(&egll, NULL, sleeper_or_burner, &l);
175      wait_ready();
176   }
177 
178   p.name = "Petaouchnok";
179   p.burn = *threads_spec++ == 'B';
180   p.sleep = *threads_spec++ == 'S';
181   p.t = -1;
182   if (p.burn || p.sleep) {
183      p.t = 3;
184      pthread_create(&zzzz, NULL, sleeper_or_burner, &p);
185      wait_ready();
186   }
187 
188   m.name = "main";
189   m.burn = *threads_spec++ == 'B';
190   m.sleep = *threads_spec++ == 'S';
191   m.t = 0;
192   sleeper_or_burner(&m);
193 
194   if (b.t != -1) pthread_join(ebbr, NULL);
195   if (l.t != -1) pthread_join(egll, NULL);
196   if (p.t != -1) pthread_join(zzzz, NULL);
197 
198   return 0;
199 }
200 
201 static int sigusr1_received = 0;
sigusr1_handler(int signr)202 static void sigusr1_handler(int signr)
203 {
204    sigusr1_received++;
205 }
setup_sigusr_handler(void)206 static void setup_sigusr_handler(void)
207 {
208    struct sigaction sa;
209    sa.sa_handler = sigusr1_handler;
210    sigemptyset(&sa.sa_mask);
211    sa.sa_flags = 0;
212 
213    if (sigaction (SIGUSR1, &sa, NULL) != 0)
214       perror("sigaction SIGUSR1");
215 
216    sa.sa_handler = SIG_IGN;
217    if (sigaction (SIGUSR2, &sa, NULL) != 0)
218       perror("sigaction SIGUSR2");
219 }
220 
221