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.
24 #include <sys/syscall.h>
25 #include <sys/sysinfo.h>
26 #include <sys/types.h>
28 #include <linux/futex.h>
34 static unsigned int timeout = 30;
36 static int touch_vdso;
39 static int touch_fp = 1;
42 static int touch_vector = 1;
46 static int touch_altivec = 1;
49 * Note: LTO (Link Time Optimisation) doesn't play well with this function
50 * attribute. Be very careful enabling LTO for this test.
52 static void __attribute__((__target__("no-vsx"))) altivec_touch_fn(void)
58 static void touch(void)
61 gettimeofday(&tv, NULL);
74 asm volatile("# %0 %1 %2": : "r"(&tv), "r"(&fp), "r"(&c));
77 static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu)
84 CPU_SET(cpu, &cpuset);
86 pthread_attr_init(&attr);
88 if (pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset)) {
89 perror("pthread_attr_setaffinity_np");
93 if (pthread_create(&tid, &attr, fn, arg)) {
94 perror("pthread_create");
99 static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
114 ncpus = get_nprocs();
115 size = CPU_ALLOC_SIZE(ncpus);
116 cpuset = CPU_ALLOC(ncpus);
121 CPU_ZERO_S(size, cpuset);
122 CPU_SET_S(cpu, size, cpuset);
124 if (sched_setaffinity(0, size, cpuset)) {
125 perror("sched_setaffinity");
136 static unsigned long iterations;
137 static unsigned long iterations_prev;
139 static void sigalrm_handler(int junk)
141 unsigned long i = iterations;
143 printf("%ld\n", i - iterations_prev);
152 static void sigusr1_handler(int junk)
158 void (*setup)(int, int);
159 void *(*thread1)(void *);
160 void *(*thread2)(void *);
166 static int pipe_fd1[2];
167 static int pipe_fd2[2];
169 static void pipe_setup(int cpu1, int cpu2)
171 if (pipe(pipe_fd1) || pipe(pipe_fd2))
175 static void *pipe_thread1(void *arg)
177 signal(SIGALRM, sigalrm_handler);
181 assert(read(pipe_fd1[READ], &c, 1) == 1);
184 assert(write(pipe_fd2[WRITE], &c, 1) == 1);
193 static void *pipe_thread2(void *arg)
196 assert(write(pipe_fd1[WRITE], &c, 1) == 1);
199 assert(read(pipe_fd2[READ], &c, 1) == 1);
206 static struct actions pipe_actions = {
208 .thread1 = pipe_thread1,
209 .thread2 = pipe_thread2,
212 static void yield_setup(int cpu1, int cpu2)
215 fprintf(stderr, "Both threads must be on the same CPU for yield test\n");
220 static void *yield_thread1(void *arg)
222 signal(SIGALRM, sigalrm_handler);
235 static void *yield_thread2(void *arg)
245 static struct actions yield_actions = {
246 .setup = yield_setup,
247 .thread1 = yield_thread1,
248 .thread2 = yield_thread2,
251 static long sys_futex(void *addr1, int op, int val1, struct timespec *timeout,
252 void *addr2, int val3)
254 return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
257 static unsigned long cmpxchg(unsigned long *p, unsigned long expected,
258 unsigned long desired)
260 unsigned long exp = expected;
262 __atomic_compare_exchange_n(p, &exp, desired, 0,
263 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
267 static unsigned long xchg(unsigned long *p, unsigned long val)
269 return __atomic_exchange_n(p, val, __ATOMIC_SEQ_CST);
272 static int processes;
274 static int mutex_lock(unsigned long *m)
277 int flags = FUTEX_WAIT;
279 flags |= FUTEX_PRIVATE_FLAG;
281 c = cmpxchg(m, 0, 1);
289 sys_futex(m, flags, 2, NULL, NULL, 0);
296 static int mutex_unlock(unsigned long *m)
298 int flags = FUTEX_WAKE;
300 flags |= FUTEX_PRIVATE_FLAG;
304 else if (xchg(m, 0) == 1)
307 sys_futex(m, flags, 1, NULL, NULL, 0);
312 static unsigned long *m1, *m2;
314 static void futex_setup(int cpu1, int cpu2)
317 static unsigned long _m1, _m2;
324 shmid = shmget(IPC_PRIVATE, getpagesize(), SHM_R | SHM_W);
330 shmaddr = shmat(shmid, NULL, 0);
331 if (shmaddr == (char *)-1) {
333 shmctl(shmid, IPC_RMID, NULL);
337 shmctl(shmid, IPC_RMID, NULL);
340 m2 = shmaddr + sizeof(*m1);
350 static void *futex_thread1(void *arg)
352 signal(SIGALRM, sigalrm_handler);
365 static void *futex_thread2(void *arg)
375 static struct actions futex_actions = {
376 .setup = futex_setup,
377 .thread1 = futex_thread1,
378 .thread2 = futex_thread2,
381 static struct option options[] = {
382 { "test", required_argument, 0, 't' },
383 { "process", no_argument, &processes, 1 },
384 { "timeout", required_argument, 0, 's' },
385 { "vdso", no_argument, &touch_vdso, 1 },
386 { "no-fp", no_argument, &touch_fp, 0 },
388 { "no-altivec", no_argument, &touch_altivec, 0 },
390 { "no-vector", no_argument, &touch_vector, 0 },
394 static void usage(void)
396 fprintf(stderr, "Usage: context_switch2 <options> CPU1 CPU2\n\n");
397 fprintf(stderr, "\t\t--test=X\tpipe, futex or yield (default)\n");
398 fprintf(stderr, "\t\t--process\tUse processes (default threads)\n");
399 fprintf(stderr, "\t\t--timeout=X\tDuration in seconds to run (default 30)\n");
400 fprintf(stderr, "\t\t--vdso\t\ttouch VDSO\n");
401 fprintf(stderr, "\t\t--no-fp\t\tDon't touch FP\n");
403 fprintf(stderr, "\t\t--no-altivec\tDon't touch altivec\n");
405 fprintf(stderr, "\t\t--no-vector\tDon't touch vector\n");
408 int main(int argc, char *argv[])
411 struct actions *actions = &yield_actions;
414 static void (*start_fn)(void *(*fn)(void *), void *arg, unsigned long cpu);
417 int option_index = 0;
419 c = getopt_long(argc, argv, "", options, &option_index);
426 if (options[option_index].flag != 0)
434 if (!strcmp(optarg, "pipe")) {
435 actions = &pipe_actions;
436 } else if (!strcmp(optarg, "yield")) {
437 actions = &yield_actions;
438 } else if (!strcmp(optarg, "futex")) {
439 actions = &futex_actions;
447 timeout = atoi(optarg);
457 start_fn = start_process_on;
459 start_fn = start_thread_on;
461 if (((argc - optind) != 2)) {
462 cpu1 = cpu2 = pick_online_cpu();
464 cpu1 = atoi(argv[optind++]);
465 cpu2 = atoi(argv[optind++]);
468 printf("Using %s with ", processes ? "processes" : "threads");
470 if (actions == &pipe_actions)
472 else if (actions == &yield_actions)
477 printf(" on cpus %d/%d touching FP:%s altivec:%s vector:%s vdso:%s\n",
478 cpu1, cpu2, touch_fp ? "yes" : "no", touch_altivec ? "yes" : "no",
479 touch_vector ? "yes" : "no", touch_vdso ? "yes" : "no");
481 /* Create a new process group so we can signal everyone for exit */
482 setpgid(getpid(), getpid());
484 signal(SIGUSR1, sigusr1_handler);
486 actions->setup(cpu1, cpu2);
488 start_fn(actions->thread1, NULL, cpu1);
489 start_fn(actions->thread2, NULL, cpu2);