context_switch.c 8.8 KB

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