fp-stress.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022 ARM Limited.
  4. */
  5. #define _GNU_SOURCE
  6. #define _POSIX_C_SOURCE 199309L
  7. #include <errno.h>
  8. #include <getopt.h>
  9. #include <poll.h>
  10. #include <signal.h>
  11. #include <stdbool.h>
  12. #include <stddef.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <sys/auxv.h>
  18. #include <sys/epoll.h>
  19. #include <sys/prctl.h>
  20. #include <sys/types.h>
  21. #include <sys/uio.h>
  22. #include <sys/wait.h>
  23. #include <asm/hwcap.h>
  24. #include "../../kselftest.h"
  25. #define MAX_VLS 16
  26. struct child_data {
  27. char *name, *output;
  28. pid_t pid;
  29. int stdout;
  30. bool output_seen;
  31. bool exited;
  32. int exit_status;
  33. };
  34. static int epoll_fd;
  35. static struct child_data *children;
  36. static int num_children;
  37. static bool terminate;
  38. static void drain_output(bool flush);
  39. static int num_processors(void)
  40. {
  41. long nproc = sysconf(_SC_NPROCESSORS_CONF);
  42. if (nproc < 0) {
  43. perror("Unable to read number of processors\n");
  44. exit(EXIT_FAILURE);
  45. }
  46. return nproc;
  47. }
  48. static void child_start(struct child_data *child, const char *program)
  49. {
  50. int ret, pipefd[2], i;
  51. struct epoll_event ev;
  52. ret = pipe(pipefd);
  53. if (ret != 0)
  54. ksft_exit_fail_msg("Failed to create stdout pipe: %s (%d)\n",
  55. strerror(errno), errno);
  56. child->pid = fork();
  57. if (child->pid == -1)
  58. ksft_exit_fail_msg("fork() failed: %s (%d)\n",
  59. strerror(errno), errno);
  60. if (!child->pid) {
  61. /*
  62. * In child, replace stdout with the pipe, errors to
  63. * stderr from here as kselftest prints to stdout.
  64. */
  65. ret = dup2(pipefd[1], 1);
  66. if (ret == -1) {
  67. fprintf(stderr, "dup2() %d\n", errno);
  68. exit(EXIT_FAILURE);
  69. }
  70. /*
  71. * Very dumb mechanism to clean open FDs other than
  72. * stdio. We don't want O_CLOEXEC for the pipes...
  73. */
  74. for (i = 3; i < 8192; i++)
  75. close(i);
  76. ret = execl(program, program, NULL);
  77. fprintf(stderr, "execl(%s) failed: %d (%s)\n",
  78. program, errno, strerror(errno));
  79. exit(EXIT_FAILURE);
  80. } else {
  81. /*
  82. * In parent, remember the child and close our copy of the
  83. * write side of stdout.
  84. */
  85. close(pipefd[1]);
  86. child->stdout = pipefd[0];
  87. child->output = NULL;
  88. child->exited = false;
  89. child->output_seen = false;
  90. ev.events = EPOLLIN | EPOLLHUP;
  91. ev.data.ptr = child;
  92. ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, child->stdout, &ev);
  93. if (ret < 0) {
  94. ksft_exit_fail_msg("%s EPOLL_CTL_ADD failed: %s (%d)\n",
  95. child->name, strerror(errno), errno);
  96. }
  97. /*
  98. * Keep output flowing during child startup so logs
  99. * are more timely, can help debugging.
  100. */
  101. drain_output(false);
  102. }
  103. }
  104. static bool child_output_read(struct child_data *child)
  105. {
  106. char read_data[1024];
  107. char work[1024];
  108. int ret, len, cur_work, cur_read;
  109. ret = read(child->stdout, read_data, sizeof(read_data));
  110. if (ret < 0) {
  111. if (errno == EINTR)
  112. return true;
  113. ksft_print_msg("%s: read() failed: %s (%d)\n",
  114. child->name, strerror(errno),
  115. errno);
  116. return false;
  117. }
  118. len = ret;
  119. child->output_seen = true;
  120. /* Pick up any partial read */
  121. if (child->output) {
  122. strncpy(work, child->output, sizeof(work) - 1);
  123. cur_work = strnlen(work, sizeof(work));
  124. free(child->output);
  125. child->output = NULL;
  126. } else {
  127. cur_work = 0;
  128. }
  129. cur_read = 0;
  130. while (cur_read < len) {
  131. work[cur_work] = read_data[cur_read++];
  132. if (work[cur_work] == '\n') {
  133. work[cur_work] = '\0';
  134. ksft_print_msg("%s: %s\n", child->name, work);
  135. cur_work = 0;
  136. } else {
  137. cur_work++;
  138. }
  139. }
  140. if (cur_work) {
  141. work[cur_work] = '\0';
  142. ret = asprintf(&child->output, "%s", work);
  143. if (ret == -1)
  144. ksft_exit_fail_msg("Out of memory\n");
  145. }
  146. return false;
  147. }
  148. static void child_output(struct child_data *child, uint32_t events,
  149. bool flush)
  150. {
  151. bool read_more;
  152. if (events & EPOLLIN) {
  153. do {
  154. read_more = child_output_read(child);
  155. } while (read_more);
  156. }
  157. if (events & EPOLLHUP) {
  158. close(child->stdout);
  159. child->stdout = -1;
  160. flush = true;
  161. }
  162. if (flush && child->output) {
  163. ksft_print_msg("%s: %s<EOF>\n", child->name, child->output);
  164. free(child->output);
  165. child->output = NULL;
  166. }
  167. }
  168. static void child_tickle(struct child_data *child)
  169. {
  170. if (child->output_seen && !child->exited)
  171. kill(child->pid, SIGUSR2);
  172. }
  173. static void child_stop(struct child_data *child)
  174. {
  175. if (!child->exited)
  176. kill(child->pid, SIGTERM);
  177. }
  178. static void child_cleanup(struct child_data *child)
  179. {
  180. pid_t ret;
  181. int status;
  182. bool fail = false;
  183. if (!child->exited) {
  184. do {
  185. ret = waitpid(child->pid, &status, 0);
  186. if (ret == -1 && errno == EINTR)
  187. continue;
  188. if (ret == -1) {
  189. ksft_print_msg("waitpid(%d) failed: %s (%d)\n",
  190. child->pid, strerror(errno),
  191. errno);
  192. fail = true;
  193. break;
  194. }
  195. } while (!WIFEXITED(status));
  196. child->exit_status = WEXITSTATUS(status);
  197. }
  198. if (!child->output_seen) {
  199. ksft_print_msg("%s no output seen\n", child->name);
  200. fail = true;
  201. }
  202. if (child->exit_status != 0) {
  203. ksft_print_msg("%s exited with error code %d\n",
  204. child->name, child->exit_status);
  205. fail = true;
  206. }
  207. ksft_test_result(!fail, "%s\n", child->name);
  208. }
  209. static void handle_child_signal(int sig, siginfo_t *info, void *context)
  210. {
  211. int i;
  212. bool found = false;
  213. for (i = 0; i < num_children; i++) {
  214. if (children[i].pid == info->si_pid) {
  215. children[i].exited = true;
  216. children[i].exit_status = info->si_status;
  217. found = true;
  218. break;
  219. }
  220. }
  221. if (!found)
  222. ksft_print_msg("SIGCHLD for unknown PID %d with status %d\n",
  223. info->si_pid, info->si_status);
  224. }
  225. static void handle_exit_signal(int sig, siginfo_t *info, void *context)
  226. {
  227. int i;
  228. /* If we're already exiting then don't signal again */
  229. if (terminate)
  230. return;
  231. ksft_print_msg("Got signal, exiting...\n");
  232. terminate = true;
  233. /*
  234. * This should be redundant, the main loop should clean up
  235. * after us, but for safety stop everything we can here.
  236. */
  237. for (i = 0; i < num_children; i++)
  238. child_stop(&children[i]);
  239. }
  240. static void start_fpsimd(struct child_data *child, int cpu, int copy)
  241. {
  242. int ret;
  243. child_start(child, "./fpsimd-test");
  244. ret = asprintf(&child->name, "FPSIMD-%d-%d", cpu, copy);
  245. if (ret == -1)
  246. ksft_exit_fail_msg("asprintf() failed\n");
  247. ksft_print_msg("Started %s\n", child->name);
  248. }
  249. static void start_sve(struct child_data *child, int vl, int cpu)
  250. {
  251. int ret;
  252. ret = prctl(PR_SVE_SET_VL, vl | PR_SVE_VL_INHERIT);
  253. if (ret < 0)
  254. ksft_exit_fail_msg("Failed to set SVE VL %d\n", vl);
  255. child_start(child, "./sve-test");
  256. ret = asprintf(&child->name, "SVE-VL-%d-%d", vl, cpu);
  257. if (ret == -1)
  258. ksft_exit_fail_msg("asprintf() failed\n");
  259. ksft_print_msg("Started %s\n", child->name);
  260. }
  261. static void start_ssve(struct child_data *child, int vl, int cpu)
  262. {
  263. int ret;
  264. ret = prctl(PR_SME_SET_VL, vl | PR_SME_VL_INHERIT);
  265. if (ret < 0)
  266. ksft_exit_fail_msg("Failed to set SME VL %d\n", ret);
  267. child_start(child, "./ssve-test");
  268. ret = asprintf(&child->name, "SSVE-VL-%d-%d", vl, cpu);
  269. if (ret == -1)
  270. ksft_exit_fail_msg("asprintf() failed\n");
  271. ksft_print_msg("Started %s\n", child->name);
  272. }
  273. static void start_za(struct child_data *child, int vl, int cpu)
  274. {
  275. int ret;
  276. ret = prctl(PR_SME_SET_VL, vl | PR_SVE_VL_INHERIT);
  277. if (ret < 0)
  278. ksft_exit_fail_msg("Failed to set SME VL %d\n", ret);
  279. child_start(child, "./za-test");
  280. ret = asprintf(&child->name, "ZA-VL-%d-%d", vl, cpu);
  281. if (ret == -1)
  282. ksft_exit_fail_msg("asprintf() failed\n");
  283. ksft_print_msg("Started %s\n", child->name);
  284. }
  285. static void probe_vls(int vls[], int *vl_count, int set_vl)
  286. {
  287. unsigned int vq;
  288. int vl;
  289. *vl_count = 0;
  290. for (vq = SVE_VQ_MAX; vq > 0; --vq) {
  291. vl = prctl(set_vl, vq * 16);
  292. if (vl == -1)
  293. ksft_exit_fail_msg("SET_VL failed: %s (%d)\n",
  294. strerror(errno), errno);
  295. vl &= PR_SVE_VL_LEN_MASK;
  296. vq = sve_vq_from_vl(vl);
  297. vls[*vl_count] = vl;
  298. *vl_count += 1;
  299. }
  300. }
  301. /* Handle any pending output without blocking */
  302. static void drain_output(bool flush)
  303. {
  304. struct epoll_event ev;
  305. int ret = 1;
  306. while (ret > 0) {
  307. ret = epoll_wait(epoll_fd, &ev, 1, 0);
  308. if (ret < 0) {
  309. if (errno == EINTR)
  310. continue;
  311. ksft_print_msg("epoll_wait() failed: %s (%d)\n",
  312. strerror(errno), errno);
  313. }
  314. if (ret == 1)
  315. child_output(ev.data.ptr, ev.events, flush);
  316. }
  317. }
  318. static const struct option options[] = {
  319. { "timeout", required_argument, NULL, 't' },
  320. { }
  321. };
  322. int main(int argc, char **argv)
  323. {
  324. int ret;
  325. int timeout = 10;
  326. int cpus, tests, i, j, c;
  327. int sve_vl_count, sme_vl_count, fpsimd_per_cpu;
  328. int sve_vls[MAX_VLS], sme_vls[MAX_VLS];
  329. struct epoll_event ev;
  330. struct sigaction sa;
  331. while ((c = getopt_long(argc, argv, "t:", options, NULL)) != -1) {
  332. switch (c) {
  333. case 't':
  334. ret = sscanf(optarg, "%d", &timeout);
  335. if (ret != 1)
  336. ksft_exit_fail_msg("Failed to parse timeout %s\n",
  337. optarg);
  338. break;
  339. default:
  340. ksft_exit_fail_msg("Unknown argument\n");
  341. }
  342. }
  343. cpus = num_processors();
  344. tests = 0;
  345. if (getauxval(AT_HWCAP) & HWCAP_SVE) {
  346. probe_vls(sve_vls, &sve_vl_count, PR_SVE_SET_VL);
  347. tests += sve_vl_count * cpus;
  348. } else {
  349. sve_vl_count = 0;
  350. }
  351. if (getauxval(AT_HWCAP2) & HWCAP2_SME) {
  352. probe_vls(sme_vls, &sme_vl_count, PR_SME_SET_VL);
  353. tests += sme_vl_count * cpus * 2;
  354. } else {
  355. sme_vl_count = 0;
  356. }
  357. /* Force context switching if we only have FPSIMD */
  358. if (!sve_vl_count && !sme_vl_count)
  359. fpsimd_per_cpu = 2;
  360. else
  361. fpsimd_per_cpu = 1;
  362. tests += cpus * fpsimd_per_cpu;
  363. ksft_print_header();
  364. ksft_set_plan(tests);
  365. ksft_print_msg("%d CPUs, %d SVE VLs, %d SME VLs\n",
  366. cpus, sve_vl_count, sme_vl_count);
  367. if (timeout > 0)
  368. ksft_print_msg("Will run for %ds\n", timeout);
  369. else
  370. ksft_print_msg("Will run until terminated\n");
  371. children = calloc(sizeof(*children), tests);
  372. if (!children)
  373. ksft_exit_fail_msg("Unable to allocate child data\n");
  374. ret = epoll_create1(EPOLL_CLOEXEC);
  375. if (ret < 0)
  376. ksft_exit_fail_msg("epoll_create1() failed: %s (%d)\n",
  377. strerror(errno), ret);
  378. epoll_fd = ret;
  379. /* Get signal handers ready before we start any children */
  380. memset(&sa, 0, sizeof(sa));
  381. sa.sa_sigaction = handle_exit_signal;
  382. sa.sa_flags = SA_RESTART | SA_SIGINFO;
  383. sigemptyset(&sa.sa_mask);
  384. ret = sigaction(SIGINT, &sa, NULL);
  385. if (ret < 0)
  386. ksft_print_msg("Failed to install SIGINT handler: %s (%d)\n",
  387. strerror(errno), errno);
  388. ret = sigaction(SIGTERM, &sa, NULL);
  389. if (ret < 0)
  390. ksft_print_msg("Failed to install SIGTERM handler: %s (%d)\n",
  391. strerror(errno), errno);
  392. sa.sa_sigaction = handle_child_signal;
  393. ret = sigaction(SIGCHLD, &sa, NULL);
  394. if (ret < 0)
  395. ksft_print_msg("Failed to install SIGCHLD handler: %s (%d)\n",
  396. strerror(errno), errno);
  397. for (i = 0; i < cpus; i++) {
  398. for (j = 0; j < fpsimd_per_cpu; j++)
  399. start_fpsimd(&children[num_children++], i, j);
  400. for (j = 0; j < sve_vl_count; j++)
  401. start_sve(&children[num_children++], sve_vls[j], i);
  402. for (j = 0; j < sme_vl_count; j++) {
  403. start_ssve(&children[num_children++], sme_vls[j], i);
  404. start_za(&children[num_children++], sme_vls[j], i);
  405. }
  406. }
  407. for (;;) {
  408. /* Did we get a signal asking us to exit? */
  409. if (terminate)
  410. break;
  411. /*
  412. * Timeout is counted in seconds with no output, the
  413. * tests print during startup then are silent when
  414. * running so this should ensure they all ran enough
  415. * to install the signal handler, this is especially
  416. * useful in emulation where we will both be slow and
  417. * likely to have a large set of VLs.
  418. */
  419. ret = epoll_wait(epoll_fd, &ev, 1, 1000);
  420. if (ret < 0) {
  421. if (errno == EINTR)
  422. continue;
  423. ksft_exit_fail_msg("epoll_wait() failed: %s (%d)\n",
  424. strerror(errno), errno);
  425. }
  426. /* Output? */
  427. if (ret == 1) {
  428. child_output(ev.data.ptr, ev.events, false);
  429. continue;
  430. }
  431. /* Otherwise epoll_wait() timed out */
  432. for (i = 0; i < num_children; i++)
  433. child_tickle(&children[i]);
  434. /* Negative timeout means run indefinitely */
  435. if (timeout < 0)
  436. continue;
  437. if (--timeout == 0)
  438. break;
  439. }
  440. ksft_print_msg("Finishing up...\n");
  441. terminate = true;
  442. for (i = 0; i < tests; i++)
  443. child_stop(&children[i]);
  444. drain_output(false);
  445. for (i = 0; i < tests; i++)
  446. child_cleanup(&children[i]);
  447. drain_output(true);
  448. ksft_print_cnts();
  449. return 0;
  450. }