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 mutex_lock(unsigned long *m)
276 c = cmpxchg(m, 0, 1);
284 sys_futex(m, FUTEX_WAIT, 2, NULL, NULL, 0);
291 static int mutex_unlock(unsigned long *m)
295 else if (xchg(m, 0) == 1)
298 sys_futex(m, FUTEX_WAKE, 1, NULL, NULL, 0);
303 static unsigned long *m1, *m2;
305 static void futex_setup(int cpu1, int cpu2)
310 shmid = shmget(IPC_PRIVATE, getpagesize(), SHM_R | SHM_W);
316 shmaddr = shmat(shmid, NULL, 0);
317 if (shmaddr == (char *)-1) {
319 shmctl(shmid, IPC_RMID, NULL);
323 shmctl(shmid, IPC_RMID, NULL);
326 m2 = shmaddr + sizeof(*m1);
335 static void *futex_thread1(void *arg)
337 signal(SIGALRM, sigalrm_handler);
350 static void *futex_thread2(void *arg)
360 static struct actions futex_actions = {
361 .setup = futex_setup,
362 .thread1 = futex_thread1,
363 .thread2 = futex_thread2,
366 static int processes;
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 },
375 { "no-altivec", no_argument, &touch_altivec, 0 },
377 { "no-vector", no_argument, &touch_vector, 0 },
381 static void usage(void)
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");
390 fprintf(stderr, "\t\t--no-altivec\tDon't touch altivec\n");
392 fprintf(stderr, "\t\t--no-vector\tDon't touch vector\n");
395 int main(int argc, char *argv[])
398 struct actions *actions = &yield_actions;
401 static void (*start_fn)(void *(*fn)(void *), void *arg, unsigned long cpu);
404 int option_index = 0;
406 c = getopt_long(argc, argv, "", options, &option_index);
413 if (options[option_index].flag != 0)
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;
434 timeout = atoi(optarg);
444 start_fn = start_process_on;
446 start_fn = start_thread_on;
448 if (((argc - optind) != 2)) {
449 cpu1 = cpu2 = pick_online_cpu();
451 cpu1 = atoi(argv[optind++]);
452 cpu2 = atoi(argv[optind++]);
455 printf("Using %s with ", processes ? "processes" : "threads");
457 if (actions == &pipe_actions)
459 else if (actions == &yield_actions)
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");
468 /* Create a new process group so we can signal everyone for exit */
469 setpgid(getpid(), getpid());
471 signal(SIGUSR1, sigusr1_handler);
473 actions->setup(cpu1, cpu2);
475 start_fn(actions->thread1, NULL, cpu1);
476 start_fn(actions->thread2, NULL, cpu2);