blob: 13cc1e0d8b0f5516b7260948b14ba820135d9998 [file] [log] [blame]
sewardj3b290482011-05-06 21:02:55 +00001#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
12static int loops = 15; // each thread+main will do this amount of loop
13static int sleepms = 1000; // in each loop, will sleep "sleepms" milliseconds
14static int burn = 0; // after each sleep, will burn cpu in a tight 'burn' loop
15
16
17static pid_t gettid()
18{
19#ifdef __NR_gettid
20 return syscall(__NR_gettid);
21#else
22 return getpid();
23#endif
24}
sewardj3b290482011-05-06 21:02:55 +000025// will be invoked from gdb.
bart667c6772011-05-08 09:29:06 +000026static void whoami(char *msg) __attribute__((unused));
sewardj3b290482011-05-06 21:02:55 +000027static void whoami(char *msg)
28{
29 fprintf(stderr, "pid %d Thread %d %s\n", getpid(), gettid(), msg);
30 fflush(stderr);
31}
32
33
34static 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
42static int thread_ready = 0;
43static pthread_cond_t ready = PTHREAD_COND_INITIALIZER;
44static pthread_mutex_t ready_mutex = PTHREAD_MUTEX_INITIALIZER;
45static 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
60struct spec {
61 char *name;
62 int sleep;
63 int burn;
64 int t;
65};
66static struct timeval t[4];
67static int nr_sleeper_or_burner = 0;
68static volatile int report_finished = 1;
69// set to 0 to have no finish msg (as order is non-deterministic)
70static 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
97static 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.
118static 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
129int main (int argc, char *argv[])
130{
131 char *threads_spec;
132 pthread_t ebbr, egll, zzzz;
133 struct spec b, l, p, m;
bart667c6772011-05-08 09:29:06 +0000134 char *some_mem __attribute__((unused)) = malloc(100);
sewardj3b290482011-05-06 21:02:55 +0000135 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';
sewardjeefeeb72011-05-10 11:01:07 +0000158 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 }
sewardj3b290482011-05-06 21:02:55 +0000164
165 l.name = "London";
166 l.burn = *threads_spec++ == 'B';
167 l.sleep = *threads_spec++ == 'S';
sewardjeefeeb72011-05-10 11:01:07 +0000168 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 }
sewardj3b290482011-05-06 21:02:55 +0000174
175 p.name = "Petaouchnok";
176 p.burn = *threads_spec++ == 'B';
177 p.sleep = *threads_spec++ == 'S';
sewardjeefeeb72011-05-10 11:01:07 +0000178 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 }
sewardj3b290482011-05-06 21:02:55 +0000184
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
sewardjeefeeb72011-05-10 11:01:07 +0000191 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);
sewardj3b290482011-05-06 21:02:55 +0000194
195 return 0;
196}