GNU Linux-libre 4.9.317-gnu1
[releases.git] / tools / testing / selftests / powerpc / benchmarks / context_switch.c
1 /*
2  * Context switch microbenchmark.
3  *
4  * Copyright (C) 2015 Anton Blanchard <anton@au.ibm.com>, IBM
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #define _GNU_SOURCE
13 #include <sched.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <getopt.h>
19 #include <signal.h>
20 #include <assert.h>
21 #include <pthread.h>
22 #include <limits.h>
23 #include <sys/time.h>
24 #include <sys/syscall.h>
25 #include <sys/sysinfo.h>
26 #include <sys/types.h>
27 #include <sys/shm.h>
28 #include <linux/futex.h>
29 #ifdef __powerpc__
30 #include <altivec.h>
31 #endif
32 #include "../utils.h"
33
34 static unsigned int timeout = 30;
35
36 static int touch_vdso;
37 struct timeval tv;
38
39 static int touch_fp = 1;
40 double fp;
41
42 static int touch_vector = 1;
43 vector int a, b, c;
44
45 #ifdef __powerpc__
46 static int touch_altivec = 1;
47
48 /*
49  * Note: LTO (Link Time Optimisation) doesn't play well with this function
50  * attribute. Be very careful enabling LTO for this test.
51  */
52 static void __attribute__((__target__("no-vsx"))) altivec_touch_fn(void)
53 {
54         c = a + b;
55 }
56 #endif
57
58 static void touch(void)
59 {
60         if (touch_vdso)
61                 gettimeofday(&tv, NULL);
62
63         if (touch_fp)
64                 fp += 0.1;
65
66 #ifdef __powerpc__
67         if (touch_altivec)
68                 altivec_touch_fn();
69 #endif
70
71         if (touch_vector)
72                 c = a + b;
73
74         asm volatile("# %0 %1 %2": : "r"(&tv), "r"(&fp), "r"(&c));
75 }
76
77 static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu)
78 {
79         pthread_t tid;
80         cpu_set_t cpuset;
81         pthread_attr_t attr;
82
83         CPU_ZERO(&cpuset);
84         CPU_SET(cpu, &cpuset);
85
86         pthread_attr_init(&attr);
87
88         if (pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset)) {
89                 perror("pthread_attr_setaffinity_np");
90                 exit(1);
91         }
92
93         if (pthread_create(&tid, &attr, fn, arg)) {
94                 perror("pthread_create");
95                 exit(1);
96         }
97 }
98
99 static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
100 {
101         int pid, ncpus;
102         cpu_set_t *cpuset;
103         size_t size;
104
105         pid = fork();
106         if (pid == -1) {
107                 perror("fork");
108                 exit(1);
109         }
110
111         if (pid)
112                 return;
113
114         ncpus = get_nprocs();
115         size = CPU_ALLOC_SIZE(ncpus);
116         cpuset = CPU_ALLOC(ncpus);
117         if (!cpuset) {
118                 perror("malloc");
119                 exit(1);
120         }
121         CPU_ZERO_S(size, cpuset);
122         CPU_SET_S(cpu, size, cpuset);
123
124         if (sched_setaffinity(0, size, cpuset)) {
125                 perror("sched_setaffinity");
126                 CPU_FREE(cpuset);
127                 exit(1);
128         }
129
130         CPU_FREE(cpuset);
131         fn(arg);
132
133         exit(0);
134 }
135
136 static unsigned long iterations;
137 static unsigned long iterations_prev;
138
139 static void sigalrm_handler(int junk)
140 {
141         unsigned long i = iterations;
142
143         printf("%ld\n", i - iterations_prev);
144         iterations_prev = i;
145
146         if (--timeout == 0)
147                 kill(0, SIGUSR1);
148
149         alarm(1);
150 }
151
152 static void sigusr1_handler(int junk)
153 {
154         exit(0);
155 }
156
157 struct actions {
158         void (*setup)(int, int);
159         void *(*thread1)(void *);
160         void *(*thread2)(void *);
161 };
162
163 #define READ 0
164 #define WRITE 1
165
166 static int pipe_fd1[2];
167 static int pipe_fd2[2];
168
169 static void pipe_setup(int cpu1, int cpu2)
170 {
171         if (pipe(pipe_fd1) || pipe(pipe_fd2))
172                 exit(1);
173 }
174
175 static void *pipe_thread1(void *arg)
176 {
177         signal(SIGALRM, sigalrm_handler);
178         alarm(1);
179
180         while (1) {
181                 assert(read(pipe_fd1[READ], &c, 1) == 1);
182                 touch();
183
184                 assert(write(pipe_fd2[WRITE], &c, 1) == 1);
185                 touch();
186
187                 iterations += 2;
188         }
189
190         return NULL;
191 }
192
193 static void *pipe_thread2(void *arg)
194 {
195         while (1) {
196                 assert(write(pipe_fd1[WRITE], &c, 1) == 1);
197                 touch();
198
199                 assert(read(pipe_fd2[READ], &c, 1) == 1);
200                 touch();
201         }
202
203         return NULL;
204 }
205
206 static struct actions pipe_actions = {
207         .setup = pipe_setup,
208         .thread1 = pipe_thread1,
209         .thread2 = pipe_thread2,
210 };
211
212 static void yield_setup(int cpu1, int cpu2)
213 {
214         if (cpu1 != cpu2) {
215                 fprintf(stderr, "Both threads must be on the same CPU for yield test\n");
216                 exit(1);
217         }
218 }
219
220 static void *yield_thread1(void *arg)
221 {
222         signal(SIGALRM, sigalrm_handler);
223         alarm(1);
224
225         while (1) {
226                 sched_yield();
227                 touch();
228
229                 iterations += 2;
230         }
231
232         return NULL;
233 }
234
235 static void *yield_thread2(void *arg)
236 {
237         while (1) {
238                 sched_yield();
239                 touch();
240         }
241
242         return NULL;
243 }
244
245 static struct actions yield_actions = {
246         .setup = yield_setup,
247         .thread1 = yield_thread1,
248         .thread2 = yield_thread2,
249 };
250
251 static long sys_futex(void *addr1, int op, int val1, struct timespec *timeout,
252                       void *addr2, int val3)
253 {
254         return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
255 }
256
257 static unsigned long cmpxchg(unsigned long *p, unsigned long expected,
258                              unsigned long desired)
259 {
260         unsigned long exp = expected;
261
262         __atomic_compare_exchange_n(p, &exp, desired, 0,
263                                     __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
264         return exp;
265 }
266
267 static unsigned long xchg(unsigned long *p, unsigned long val)
268 {
269         return __atomic_exchange_n(p, val, __ATOMIC_SEQ_CST);
270 }
271
272 static int mutex_lock(unsigned long *m)
273 {
274         int c;
275
276         c = cmpxchg(m, 0, 1);
277         if (!c)
278                 return 0;
279
280         if (c == 1)
281                 c = xchg(m, 2);
282
283         while (c) {
284                 sys_futex(m, FUTEX_WAIT, 2, NULL, NULL, 0);
285                 c = xchg(m, 2);
286         }
287
288         return 0;
289 }
290
291 static int mutex_unlock(unsigned long *m)
292 {
293         if (*m == 2)
294                 *m = 0;
295         else if (xchg(m, 0) == 1)
296                 return 0;
297
298         sys_futex(m, FUTEX_WAKE, 1, NULL, NULL, 0);
299
300         return 0;
301 }
302
303 static unsigned long *m1, *m2;
304
305 static void futex_setup(int cpu1, int cpu2)
306 {
307         int shmid;
308         void *shmaddr;
309
310         shmid = shmget(IPC_PRIVATE, getpagesize(), SHM_R | SHM_W);
311         if (shmid < 0) {
312                 perror("shmget");
313                 exit(1);
314         }
315
316         shmaddr = shmat(shmid, NULL, 0);
317         if (shmaddr == (char *)-1) {
318                 perror("shmat");
319                 shmctl(shmid, IPC_RMID, NULL);
320                 exit(1);
321         }
322
323         shmctl(shmid, IPC_RMID, NULL);
324
325         m1 = shmaddr;
326         m2 = shmaddr + sizeof(*m1);
327
328         *m1 = 0;
329         *m2 = 0;
330
331         mutex_lock(m1);
332         mutex_lock(m2);
333 }
334
335 static void *futex_thread1(void *arg)
336 {
337         signal(SIGALRM, sigalrm_handler);
338         alarm(1);
339
340         while (1) {
341                 mutex_lock(m2);
342                 mutex_unlock(m1);
343
344                 iterations += 2;
345         }
346
347         return NULL;
348 }
349
350 static void *futex_thread2(void *arg)
351 {
352         while (1) {
353                 mutex_unlock(m2);
354                 mutex_lock(m1);
355         }
356
357         return NULL;
358 }
359
360 static struct actions futex_actions = {
361         .setup = futex_setup,
362         .thread1 = futex_thread1,
363         .thread2 = futex_thread2,
364 };
365
366 static int processes;
367
368 static struct option options[] = {
369         { "test", required_argument, 0, 't' },
370         { "process", no_argument, &processes, 1 },
371         { "timeout", required_argument, 0, 's' },
372         { "vdso", no_argument, &touch_vdso, 1 },
373         { "no-fp", no_argument, &touch_fp, 0 },
374 #ifdef __powerpc__
375         { "no-altivec", no_argument, &touch_altivec, 0 },
376 #endif
377         { "no-vector", no_argument, &touch_vector, 0 },
378         { 0, },
379 };
380
381 static void usage(void)
382 {
383         fprintf(stderr, "Usage: context_switch2 <options> CPU1 CPU2\n\n");
384         fprintf(stderr, "\t\t--test=X\tpipe, futex or yield (default)\n");
385         fprintf(stderr, "\t\t--process\tUse processes (default threads)\n");
386         fprintf(stderr, "\t\t--timeout=X\tDuration in seconds to run (default 30)\n");
387         fprintf(stderr, "\t\t--vdso\t\ttouch VDSO\n");
388         fprintf(stderr, "\t\t--no-fp\t\tDon't touch FP\n");
389 #ifdef __powerpc__
390         fprintf(stderr, "\t\t--no-altivec\tDon't touch altivec\n");
391 #endif
392         fprintf(stderr, "\t\t--no-vector\tDon't touch vector\n");
393 }
394
395 int main(int argc, char *argv[])
396 {
397         signed char c;
398         struct actions *actions = &yield_actions;
399         int cpu1;
400         int cpu2;
401         static void (*start_fn)(void *(*fn)(void *), void *arg, unsigned long cpu);
402
403         while (1) {
404                 int option_index = 0;
405
406                 c = getopt_long(argc, argv, "", options, &option_index);
407
408                 if (c == -1)
409                         break;
410
411                 switch (c) {
412                 case 0:
413                         if (options[option_index].flag != 0)
414                                 break;
415
416                         usage();
417                         exit(1);
418                         break;
419
420                 case 't':
421                         if (!strcmp(optarg, "pipe")) {
422                                 actions = &pipe_actions;
423                         } else if (!strcmp(optarg, "yield")) {
424                                 actions = &yield_actions;
425                         } else if (!strcmp(optarg, "futex")) {
426                                 actions = &futex_actions;
427                         } else {
428                                 usage();
429                                 exit(1);
430                         }
431                         break;
432
433                 case 's':
434                         timeout = atoi(optarg);
435                         break;
436
437                 default:
438                         usage();
439                         exit(1);
440                 }
441         }
442
443         if (processes)
444                 start_fn = start_process_on;
445         else
446                 start_fn = start_thread_on;
447
448         if (((argc - optind) != 2)) {
449                 cpu1 = cpu2 = pick_online_cpu();
450         } else {
451                 cpu1 = atoi(argv[optind++]);
452                 cpu2 = atoi(argv[optind++]);
453         }
454
455         printf("Using %s with ", processes ? "processes" : "threads");
456
457         if (actions == &pipe_actions)
458                 printf("pipe");
459         else if (actions == &yield_actions)
460                 printf("yield");
461         else
462                 printf("futex");
463
464         printf(" on cpus %d/%d touching FP:%s altivec:%s vector:%s vdso:%s\n",
465                cpu1, cpu2, touch_fp ?  "yes" : "no", touch_altivec ? "yes" : "no",
466                touch_vector ? "yes" : "no", touch_vdso ? "yes" : "no");
467
468         /* Create a new process group so we can signal everyone for exit */
469         setpgid(getpid(), getpid());
470
471         signal(SIGUSR1, sigusr1_handler);
472
473         actions->setup(cpu1, cpu2);
474
475         start_fn(actions->thread1, NULL, cpu1);
476         start_fn(actions->thread2, NULL, cpu2);
477
478         while (1)
479                 sleep(3600);
480
481         return 0;
482 }