context_switch.c 8.9 KB

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