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.
25 #include <sys/syscall.h>
26 #include <sys/sysinfo.h>
27 #include <sys/types.h>
29 #include <linux/futex.h>
35 static unsigned int timeout = 30;
37 static int touch_vdso;
40 static int touch_fp = 1;
43 static int touch_vector = 1;
47 static int touch_altivec = 1;
50 * Note: LTO (Link Time Optimisation) doesn't play well with this function
51 * attribute. Be very careful enabling LTO for this test.
53 static void __attribute__((__target__("no-vsx"))) altivec_touch_fn(void)
59 static void touch(void)
62 gettimeofday(&tv, NULL);
75 asm volatile("# %0 %1 %2": : "r"(&tv), "r"(&fp), "r"(&c));
78 static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu)
86 CPU_SET(cpu, &cpuset);
88 rc = pthread_attr_init(&attr);
91 perror("pthread_attr_init");
95 rc = pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
98 perror("pthread_attr_setaffinity_np");
102 rc = pthread_create(&tid, &attr, fn, arg);
105 perror("pthread_create");
110 static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
125 ncpus = get_nprocs();
126 size = CPU_ALLOC_SIZE(ncpus);
127 cpuset = CPU_ALLOC(ncpus);
132 CPU_ZERO_S(size, cpuset);
133 CPU_SET_S(cpu, size, cpuset);
135 if (sched_setaffinity(0, size, cpuset)) {
136 perror("sched_setaffinity");
147 static unsigned long iterations;
148 static unsigned long iterations_prev;
150 static void sigalrm_handler(int junk)
152 unsigned long i = iterations;
154 printf("%ld\n", i - iterations_prev);
163 static void sigusr1_handler(int junk)
169 void (*setup)(int, int);
170 void *(*thread1)(void *);
171 void *(*thread2)(void *);
177 static int pipe_fd1[2];
178 static int pipe_fd2[2];
180 static void pipe_setup(int cpu1, int cpu2)
182 if (pipe(pipe_fd1) || pipe(pipe_fd2))
186 static void *pipe_thread1(void *arg)
188 signal(SIGALRM, sigalrm_handler);
192 assert(read(pipe_fd1[READ], &c, 1) == 1);
195 assert(write(pipe_fd2[WRITE], &c, 1) == 1);
204 static void *pipe_thread2(void *arg)
207 assert(write(pipe_fd1[WRITE], &c, 1) == 1);
210 assert(read(pipe_fd2[READ], &c, 1) == 1);
217 static struct actions pipe_actions = {
219 .thread1 = pipe_thread1,
220 .thread2 = pipe_thread2,
223 static void yield_setup(int cpu1, int cpu2)
226 fprintf(stderr, "Both threads must be on the same CPU for yield test\n");
231 static void *yield_thread1(void *arg)
233 signal(SIGALRM, sigalrm_handler);
246 static void *yield_thread2(void *arg)
256 static struct actions yield_actions = {
257 .setup = yield_setup,
258 .thread1 = yield_thread1,
259 .thread2 = yield_thread2,
262 static long sys_futex(void *addr1, int op, int val1, struct timespec *timeout,
263 void *addr2, int val3)
265 return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
268 static unsigned long cmpxchg(unsigned long *p, unsigned long expected,
269 unsigned long desired)
271 unsigned long exp = expected;
273 __atomic_compare_exchange_n(p, &exp, desired, 0,
274 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
278 static unsigned long xchg(unsigned long *p, unsigned long val)
280 return __atomic_exchange_n(p, val, __ATOMIC_SEQ_CST);
283 static int processes;
285 static int mutex_lock(unsigned long *m)
288 int flags = FUTEX_WAIT;
290 flags |= FUTEX_PRIVATE_FLAG;
292 c = cmpxchg(m, 0, 1);
300 sys_futex(m, flags, 2, NULL, NULL, 0);
307 static int mutex_unlock(unsigned long *m)
309 int flags = FUTEX_WAKE;
311 flags |= FUTEX_PRIVATE_FLAG;
315 else if (xchg(m, 0) == 1)
318 sys_futex(m, flags, 1, NULL, NULL, 0);
323 static unsigned long *m1, *m2;
325 static void futex_setup(int cpu1, int cpu2)
328 static unsigned long _m1, _m2;
335 shmid = shmget(IPC_PRIVATE, getpagesize(), SHM_R | SHM_W);
341 shmaddr = shmat(shmid, NULL, 0);
342 if (shmaddr == (char *)-1) {
344 shmctl(shmid, IPC_RMID, NULL);
348 shmctl(shmid, IPC_RMID, NULL);
351 m2 = shmaddr + sizeof(*m1);
361 static void *futex_thread1(void *arg)
363 signal(SIGALRM, sigalrm_handler);
376 static void *futex_thread2(void *arg)
386 static struct actions futex_actions = {
387 .setup = futex_setup,
388 .thread1 = futex_thread1,
389 .thread2 = futex_thread2,
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 },
399 { "no-altivec", no_argument, &touch_altivec, 0 },
401 { "no-vector", no_argument, &touch_vector, 0 },
405 static void usage(void)
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");
414 fprintf(stderr, "\t\t--no-altivec\tDon't touch altivec\n");
416 fprintf(stderr, "\t\t--no-vector\tDon't touch vector\n");
419 int main(int argc, char *argv[])
422 struct actions *actions = &yield_actions;
425 static void (*start_fn)(void *(*fn)(void *), void *arg, unsigned long cpu);
428 int option_index = 0;
430 c = getopt_long(argc, argv, "", options, &option_index);
437 if (options[option_index].flag != 0)
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;
458 timeout = atoi(optarg);
468 start_fn = start_process_on;
470 start_fn = start_thread_on;
472 if (((argc - optind) != 2)) {
473 cpu1 = cpu2 = pick_online_cpu();
475 cpu1 = atoi(argv[optind++]);
476 cpu2 = atoi(argv[optind++]);
479 printf("Using %s with ", processes ? "processes" : "threads");
481 if (actions == &pipe_actions)
483 else if (actions == &yield_actions)
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");
492 /* Create a new process group so we can signal everyone for exit */
493 setpgid(getpid(), getpid());
495 signal(SIGUSR1, sigusr1_handler);
497 actions->setup(cpu1, cpu2);
499 start_fn(actions->thread1, NULL, cpu1);
500 start_fn(actions->thread2, NULL, cpu2);