Linux 4.8-rc8
[linux/fpc-iii.git] / tools / testing / selftests / powerpc / benchmarks / context_switch.c
bloba36883ad48a451799f3345e048e6e1f5d52ed33e
1 /*
2 * Context switch microbenchmark.
4 * Copyright (C) 2015 Anton Blanchard <anton@au.ibm.com>, IBM
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.
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/types.h>
26 #include <sys/shm.h>
27 #include <linux/futex.h>
28 #ifdef __powerpc__
29 #include <altivec.h>
30 #endif
31 #include "../utils.h"
33 static unsigned int timeout = 30;
35 static int touch_vdso;
36 struct timeval tv;
38 static int touch_fp = 1;
39 double fp;
41 static int touch_vector = 1;
42 vector int a, b, c;
44 #ifdef __powerpc__
45 static int touch_altivec = 1;
48 * Note: LTO (Link Time Optimisation) doesn't play well with this function
49 * attribute. Be very careful enabling LTO for this test.
51 static void __attribute__((__target__("no-vsx"))) altivec_touch_fn(void)
53 c = a + b;
55 #endif
57 static void touch(void)
59 if (touch_vdso)
60 gettimeofday(&tv, NULL);
62 if (touch_fp)
63 fp += 0.1;
65 #ifdef __powerpc__
66 if (touch_altivec)
67 altivec_touch_fn();
68 #endif
70 if (touch_vector)
71 c = a + b;
73 asm volatile("# %0 %1 %2": : "r"(&tv), "r"(&fp), "r"(&c));
76 static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu)
78 pthread_t tid;
79 cpu_set_t cpuset;
80 pthread_attr_t attr;
82 CPU_ZERO(&cpuset);
83 CPU_SET(cpu, &cpuset);
85 pthread_attr_init(&attr);
87 if (pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset)) {
88 perror("pthread_attr_setaffinity_np");
89 exit(1);
92 if (pthread_create(&tid, &attr, fn, arg)) {
93 perror("pthread_create");
94 exit(1);
98 static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
100 int pid;
101 cpu_set_t cpuset;
103 pid = fork();
104 if (pid == -1) {
105 perror("fork");
106 exit(1);
109 if (pid)
110 return;
112 CPU_ZERO(&cpuset);
113 CPU_SET(cpu, &cpuset);
115 if (sched_setaffinity(0, sizeof(cpuset), &cpuset)) {
116 perror("sched_setaffinity");
117 exit(1);
120 fn(arg);
122 exit(0);
125 static unsigned long iterations;
126 static unsigned long iterations_prev;
128 static void sigalrm_handler(int junk)
130 unsigned long i = iterations;
132 printf("%ld\n", i - iterations_prev);
133 iterations_prev = i;
135 if (--timeout == 0)
136 kill(0, SIGUSR1);
138 alarm(1);
141 static void sigusr1_handler(int junk)
143 exit(0);
146 struct actions {
147 void (*setup)(int, int);
148 void *(*thread1)(void *);
149 void *(*thread2)(void *);
152 #define READ 0
153 #define WRITE 1
155 static int pipe_fd1[2];
156 static int pipe_fd2[2];
158 static void pipe_setup(int cpu1, int cpu2)
160 if (pipe(pipe_fd1) || pipe(pipe_fd2))
161 exit(1);
164 static void *pipe_thread1(void *arg)
166 signal(SIGALRM, sigalrm_handler);
167 alarm(1);
169 while (1) {
170 assert(read(pipe_fd1[READ], &c, 1) == 1);
171 touch();
173 assert(write(pipe_fd2[WRITE], &c, 1) == 1);
174 touch();
176 iterations += 2;
179 return NULL;
182 static void *pipe_thread2(void *arg)
184 while (1) {
185 assert(write(pipe_fd1[WRITE], &c, 1) == 1);
186 touch();
188 assert(read(pipe_fd2[READ], &c, 1) == 1);
189 touch();
192 return NULL;
195 static struct actions pipe_actions = {
196 .setup = pipe_setup,
197 .thread1 = pipe_thread1,
198 .thread2 = pipe_thread2,
201 static void yield_setup(int cpu1, int cpu2)
203 if (cpu1 != cpu2) {
204 fprintf(stderr, "Both threads must be on the same CPU for yield test\n");
205 exit(1);
209 static void *yield_thread1(void *arg)
211 signal(SIGALRM, sigalrm_handler);
212 alarm(1);
214 while (1) {
215 sched_yield();
216 touch();
218 iterations += 2;
221 return NULL;
224 static void *yield_thread2(void *arg)
226 while (1) {
227 sched_yield();
228 touch();
231 return NULL;
234 static struct actions yield_actions = {
235 .setup = yield_setup,
236 .thread1 = yield_thread1,
237 .thread2 = yield_thread2,
240 static long sys_futex(void *addr1, int op, int val1, struct timespec *timeout,
241 void *addr2, int val3)
243 return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
246 static unsigned long cmpxchg(unsigned long *p, unsigned long expected,
247 unsigned long desired)
249 unsigned long exp = expected;
251 __atomic_compare_exchange_n(p, &exp, desired, 0,
252 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
253 return exp;
256 static unsigned long xchg(unsigned long *p, unsigned long val)
258 return __atomic_exchange_n(p, val, __ATOMIC_SEQ_CST);
261 static int mutex_lock(unsigned long *m)
263 int c;
265 c = cmpxchg(m, 0, 1);
266 if (!c)
267 return 0;
269 if (c == 1)
270 c = xchg(m, 2);
272 while (c) {
273 sys_futex(m, FUTEX_WAIT, 2, NULL, NULL, 0);
274 c = xchg(m, 2);
277 return 0;
280 static int mutex_unlock(unsigned long *m)
282 if (*m == 2)
283 *m = 0;
284 else if (xchg(m, 0) == 1)
285 return 0;
287 sys_futex(m, FUTEX_WAKE, 1, NULL, NULL, 0);
289 return 0;
292 static unsigned long *m1, *m2;
294 static void futex_setup(int cpu1, int cpu2)
296 int shmid;
297 void *shmaddr;
299 shmid = shmget(IPC_PRIVATE, getpagesize(), SHM_R | SHM_W);
300 if (shmid < 0) {
301 perror("shmget");
302 exit(1);
305 shmaddr = shmat(shmid, NULL, 0);
306 if (shmaddr == (char *)-1) {
307 perror("shmat");
308 shmctl(shmid, IPC_RMID, NULL);
309 exit(1);
312 shmctl(shmid, IPC_RMID, NULL);
314 m1 = shmaddr;
315 m2 = shmaddr + sizeof(*m1);
317 *m1 = 0;
318 *m2 = 0;
320 mutex_lock(m1);
321 mutex_lock(m2);
324 static void *futex_thread1(void *arg)
326 signal(SIGALRM, sigalrm_handler);
327 alarm(1);
329 while (1) {
330 mutex_lock(m2);
331 mutex_unlock(m1);
333 iterations += 2;
336 return NULL;
339 static void *futex_thread2(void *arg)
341 while (1) {
342 mutex_unlock(m2);
343 mutex_lock(m1);
346 return NULL;
349 static struct actions futex_actions = {
350 .setup = futex_setup,
351 .thread1 = futex_thread1,
352 .thread2 = futex_thread2,
355 static int processes;
357 static struct option options[] = {
358 { "test", required_argument, 0, 't' },
359 { "process", no_argument, &processes, 1 },
360 { "timeout", required_argument, 0, 's' },
361 { "vdso", no_argument, &touch_vdso, 1 },
362 { "no-fp", no_argument, &touch_fp, 0 },
363 #ifdef __powerpc__
364 { "no-altivec", no_argument, &touch_altivec, 0 },
365 #endif
366 { "no-vector", no_argument, &touch_vector, 0 },
367 { 0, },
370 static void usage(void)
372 fprintf(stderr, "Usage: context_switch2 <options> CPU1 CPU2\n\n");
373 fprintf(stderr, "\t\t--test=X\tpipe, futex or yield (default)\n");
374 fprintf(stderr, "\t\t--process\tUse processes (default threads)\n");
375 fprintf(stderr, "\t\t--timeout=X\tDuration in seconds to run (default 30)\n");
376 fprintf(stderr, "\t\t--vdso\t\ttouch VDSO\n");
377 fprintf(stderr, "\t\t--no-fp\t\tDon't touch FP\n");
378 #ifdef __powerpc__
379 fprintf(stderr, "\t\t--no-altivec\tDon't touch altivec\n");
380 #endif
381 fprintf(stderr, "\t\t--no-vector\tDon't touch vector\n");
384 int main(int argc, char *argv[])
386 signed char c;
387 struct actions *actions = &yield_actions;
388 int cpu1;
389 int cpu2;
390 static void (*start_fn)(void *(*fn)(void *), void *arg, unsigned long cpu);
392 while (1) {
393 int option_index = 0;
395 c = getopt_long(argc, argv, "", options, &option_index);
397 if (c == -1)
398 break;
400 switch (c) {
401 case 0:
402 if (options[option_index].flag != 0)
403 break;
405 usage();
406 exit(1);
407 break;
409 case 't':
410 if (!strcmp(optarg, "pipe")) {
411 actions = &pipe_actions;
412 } else if (!strcmp(optarg, "yield")) {
413 actions = &yield_actions;
414 } else if (!strcmp(optarg, "futex")) {
415 actions = &futex_actions;
416 } else {
417 usage();
418 exit(1);
420 break;
422 case 's':
423 timeout = atoi(optarg);
424 break;
426 default:
427 usage();
428 exit(1);
432 if (processes)
433 start_fn = start_process_on;
434 else
435 start_fn = start_thread_on;
437 if (((argc - optind) != 2)) {
438 cpu1 = cpu2 = pick_online_cpu();
439 } else {
440 cpu1 = atoi(argv[optind++]);
441 cpu2 = atoi(argv[optind++]);
444 printf("Using %s with ", processes ? "processes" : "threads");
446 if (actions == &pipe_actions)
447 printf("pipe");
448 else if (actions == &yield_actions)
449 printf("yield");
450 else
451 printf("futex");
453 printf(" on cpus %d/%d touching FP:%s altivec:%s vector:%s vdso:%s\n",
454 cpu1, cpu2, touch_fp ? "yes" : "no", touch_altivec ? "yes" : "no",
455 touch_vector ? "yes" : "no", touch_vdso ? "yes" : "no");
457 /* Create a new process group so we can signal everyone for exit */
458 setpgid(getpid(), getpid());
460 signal(SIGUSR1, sigusr1_handler);
462 actions->setup(cpu1, cpu2);
464 start_fn(actions->thread1, NULL, cpu1);
465 start_fn(actions->thread2, NULL, cpu2);
467 while (1)
468 sleep(3600);
470 return 0;