context_switch.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #define _GNU_SOURCE
  12. #include <sched.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <getopt.h>
  18. #include <signal.h>
  19. #include <assert.h>
  20. #include <pthread.h>
  21. #include <limits.h>
  22. #include <sys/time.h>
  23. #include <sys/syscall.h>
  24. #include <sys/types.h>
  25. #include <sys/shm.h>
  26. #include <linux/futex.h>
  27. #include "../utils.h"
  28. static unsigned int timeout = 30;
  29. static int touch_vdso;
  30. struct timeval tv;
  31. static int touch_fp = 1;
  32. double fp;
  33. static int touch_vector = 1;
  34. typedef int v4si __attribute__ ((vector_size (16)));
  35. v4si a, b, c;
  36. #ifdef __powerpc__
  37. static int touch_altivec = 1;
  38. static void __attribute__((__target__("no-vsx"))) altivec_touch_fn(void)
  39. {
  40. c = a + b;
  41. }
  42. #endif
  43. static void touch(void)
  44. {
  45. if (touch_vdso)
  46. gettimeofday(&tv, NULL);
  47. if (touch_fp)
  48. fp += 0.1;
  49. #ifdef __powerpc__
  50. if (touch_altivec)
  51. altivec_touch_fn();
  52. #endif
  53. if (touch_vector)
  54. c = a + b;
  55. asm volatile("# %0 %1 %2": : "r"(&tv), "r"(&fp), "r"(&c));
  56. }
  57. static void start_thread_on(void *(*fn)(void *), void *arg, unsigned long cpu)
  58. {
  59. pthread_t tid;
  60. cpu_set_t cpuset;
  61. pthread_attr_t attr;
  62. CPU_ZERO(&cpuset);
  63. CPU_SET(cpu, &cpuset);
  64. pthread_attr_init(&attr);
  65. if (pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset)) {
  66. perror("pthread_attr_setaffinity_np");
  67. exit(1);
  68. }
  69. if (pthread_create(&tid, &attr, fn, arg)) {
  70. perror("pthread_create");
  71. exit(1);
  72. }
  73. }
  74. static void start_process_on(void *(*fn)(void *), void *arg, unsigned long cpu)
  75. {
  76. int pid;
  77. cpu_set_t cpuset;
  78. pid = fork();
  79. if (pid == -1) {
  80. perror("fork");
  81. exit(1);
  82. }
  83. if (pid)
  84. return;
  85. CPU_ZERO(&cpuset);
  86. CPU_SET(cpu, &cpuset);
  87. if (sched_setaffinity(0, sizeof(cpuset), &cpuset)) {
  88. perror("sched_setaffinity");
  89. exit(1);
  90. }
  91. fn(arg);
  92. exit(0);
  93. }
  94. static unsigned long iterations;
  95. static unsigned long iterations_prev;
  96. static void sigalrm_handler(int junk)
  97. {
  98. unsigned long i = iterations;
  99. printf("%ld\n", i - iterations_prev);
  100. iterations_prev = i;
  101. if (--timeout == 0)
  102. kill(0, SIGUSR1);
  103. alarm(1);
  104. }
  105. static void sigusr1_handler(int junk)
  106. {
  107. exit(0);
  108. }
  109. struct actions {
  110. void (*setup)(int, int);
  111. void *(*thread1)(void *);
  112. void *(*thread2)(void *);
  113. };
  114. #define READ 0
  115. #define WRITE 1
  116. static int pipe_fd1[2];
  117. static int pipe_fd2[2];
  118. static void pipe_setup(int cpu1, int cpu2)
  119. {
  120. if (pipe(pipe_fd1) || pipe(pipe_fd2))
  121. exit(1);
  122. }
  123. static void *pipe_thread1(void *arg)
  124. {
  125. signal(SIGALRM, sigalrm_handler);
  126. alarm(1);
  127. while (1) {
  128. assert(read(pipe_fd1[READ], &c, 1) == 1);
  129. touch();
  130. assert(write(pipe_fd2[WRITE], &c, 1) == 1);
  131. touch();
  132. iterations += 2;
  133. }
  134. return NULL;
  135. }
  136. static void *pipe_thread2(void *arg)
  137. {
  138. while (1) {
  139. assert(write(pipe_fd1[WRITE], &c, 1) == 1);
  140. touch();
  141. assert(read(pipe_fd2[READ], &c, 1) == 1);
  142. touch();
  143. }
  144. return NULL;
  145. }
  146. static struct actions pipe_actions = {
  147. .setup = pipe_setup,
  148. .thread1 = pipe_thread1,
  149. .thread2 = pipe_thread2,
  150. };
  151. static void yield_setup(int cpu1, int cpu2)
  152. {
  153. if (cpu1 != cpu2) {
  154. fprintf(stderr, "Both threads must be on the same CPU for yield test\n");
  155. exit(1);
  156. }
  157. }
  158. static void *yield_thread1(void *arg)
  159. {
  160. signal(SIGALRM, sigalrm_handler);
  161. alarm(1);
  162. while (1) {
  163. sched_yield();
  164. touch();
  165. iterations += 2;
  166. }
  167. return NULL;
  168. }
  169. static void *yield_thread2(void *arg)
  170. {
  171. while (1) {
  172. sched_yield();
  173. touch();
  174. }
  175. return NULL;
  176. }
  177. static struct actions yield_actions = {
  178. .setup = yield_setup,
  179. .thread1 = yield_thread1,
  180. .thread2 = yield_thread2,
  181. };
  182. static long sys_futex(void *addr1, int op, int val1, struct timespec *timeout,
  183. void *addr2, int val3)
  184. {
  185. return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
  186. }
  187. static unsigned long cmpxchg(unsigned long *p, unsigned long expected,
  188. unsigned long desired)
  189. {
  190. unsigned long exp = expected;
  191. __atomic_compare_exchange_n(p, &exp, desired, 0,
  192. __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
  193. return exp;
  194. }
  195. static unsigned long xchg(unsigned long *p, unsigned long val)
  196. {
  197. return __atomic_exchange_n(p, val, __ATOMIC_SEQ_CST);
  198. }
  199. static int mutex_lock(unsigned long *m)
  200. {
  201. int c;
  202. c = cmpxchg(m, 0, 1);
  203. if (!c)
  204. return 0;
  205. if (c == 1)
  206. c = xchg(m, 2);
  207. while (c) {
  208. sys_futex(m, FUTEX_WAIT, 2, NULL, NULL, 0);
  209. c = xchg(m, 2);
  210. }
  211. return 0;
  212. }
  213. static int mutex_unlock(unsigned long *m)
  214. {
  215. if (*m == 2)
  216. *m = 0;
  217. else if (xchg(m, 0) == 1)
  218. return 0;
  219. sys_futex(m, FUTEX_WAKE, 1, NULL, NULL, 0);
  220. return 0;
  221. }
  222. static unsigned long *m1, *m2;
  223. static void futex_setup(int cpu1, int cpu2)
  224. {
  225. int shmid;
  226. void *shmaddr;
  227. shmid = shmget(IPC_PRIVATE, getpagesize(), SHM_R | SHM_W);
  228. if (shmid < 0) {
  229. perror("shmget");
  230. exit(1);
  231. }
  232. shmaddr = shmat(shmid, NULL, 0);
  233. if (shmaddr == (char *)-1) {
  234. perror("shmat");
  235. shmctl(shmid, IPC_RMID, NULL);
  236. exit(1);
  237. }
  238. shmctl(shmid, IPC_RMID, NULL);
  239. m1 = shmaddr;
  240. m2 = shmaddr + sizeof(*m1);
  241. *m1 = 0;
  242. *m2 = 0;
  243. mutex_lock(m1);
  244. mutex_lock(m2);
  245. }
  246. static void *futex_thread1(void *arg)
  247. {
  248. signal(SIGALRM, sigalrm_handler);
  249. alarm(1);
  250. while (1) {
  251. mutex_lock(m2);
  252. mutex_unlock(m1);
  253. iterations += 2;
  254. }
  255. return NULL;
  256. }
  257. static void *futex_thread2(void *arg)
  258. {
  259. while (1) {
  260. mutex_unlock(m2);
  261. mutex_lock(m1);
  262. }
  263. return NULL;
  264. }
  265. static struct actions futex_actions = {
  266. .setup = futex_setup,
  267. .thread1 = futex_thread1,
  268. .thread2 = futex_thread2,
  269. };
  270. static int processes;
  271. static struct option options[] = {
  272. { "test", required_argument, 0, 't' },
  273. { "process", no_argument, &processes, 1 },
  274. { "timeout", required_argument, 0, 's' },
  275. { "vdso", no_argument, &touch_vdso, 1 },
  276. { "no-fp", no_argument, &touch_fp, 0 },
  277. #ifdef __powerpc__
  278. { "no-altivec", no_argument, &touch_altivec, 0 },
  279. #endif
  280. { "no-vector", no_argument, &touch_vector, 0 },
  281. { 0, },
  282. };
  283. static void usage(void)
  284. {
  285. fprintf(stderr, "Usage: context_switch2 <options> CPU1 CPU2\n\n");
  286. fprintf(stderr, "\t\t--test=X\tpipe, futex or yield (default)\n");
  287. fprintf(stderr, "\t\t--process\tUse processes (default threads)\n");
  288. fprintf(stderr, "\t\t--timeout=X\tDuration in seconds to run (default 30)\n");
  289. fprintf(stderr, "\t\t--vdso\t\ttouch VDSO\n");
  290. fprintf(stderr, "\t\t--fp\t\ttouch FP\n");
  291. #ifdef __powerpc__
  292. fprintf(stderr, "\t\t--altivec\ttouch altivec\n");
  293. #endif
  294. fprintf(stderr, "\t\t--vector\ttouch vector\n");
  295. }
  296. int main(int argc, char *argv[])
  297. {
  298. signed char c;
  299. struct actions *actions = &yield_actions;
  300. int cpu1;
  301. int cpu2;
  302. static void (*start_fn)(void *(*fn)(void *), void *arg, unsigned long cpu);
  303. while (1) {
  304. int option_index = 0;
  305. c = getopt_long(argc, argv, "", options, &option_index);
  306. if (c == -1)
  307. break;
  308. switch (c) {
  309. case 0:
  310. if (options[option_index].flag != 0)
  311. break;
  312. usage();
  313. exit(1);
  314. break;
  315. case 't':
  316. if (!strcmp(optarg, "pipe")) {
  317. actions = &pipe_actions;
  318. } else if (!strcmp(optarg, "yield")) {
  319. actions = &yield_actions;
  320. } else if (!strcmp(optarg, "futex")) {
  321. actions = &futex_actions;
  322. } else {
  323. usage();
  324. exit(1);
  325. }
  326. break;
  327. case 's':
  328. timeout = atoi(optarg);
  329. break;
  330. default:
  331. usage();
  332. exit(1);
  333. }
  334. }
  335. if (processes)
  336. start_fn = start_process_on;
  337. else
  338. start_fn = start_thread_on;
  339. if (((argc - optind) != 2)) {
  340. cpu1 = cpu2 = pick_online_cpu();
  341. } else {
  342. cpu1 = atoi(argv[optind++]);
  343. cpu2 = atoi(argv[optind++]);
  344. }
  345. printf("Using %s with ", processes ? "processes" : "threads");
  346. if (actions == &pipe_actions)
  347. printf("pipe");
  348. else if (actions == &yield_actions)
  349. printf("yield");
  350. else
  351. printf("futex");
  352. printf(" on cpus %d/%d touching FP:%s altivec:%s vector:%s vdso:%s\n",
  353. cpu1, cpu2, touch_fp ? "yes" : "no", touch_altivec ? "yes" : "no",
  354. touch_vector ? "yes" : "no", touch_vdso ? "yes" : "no");
  355. /* Create a new process group so we can signal everyone for exit */
  356. setpgid(getpid(), getpid());
  357. signal(SIGUSR1, sigusr1_handler);
  358. actions->setup(cpu1, cpu2);
  359. start_fn(actions->thread1, NULL, cpu1);
  360. start_fn(actions->thread2, NULL, cpu2);
  361. while (1)
  362. sleep(3600);
  363. return 0;
  364. }