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