resctrl_val.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Memory bandwidth monitoring and allocation library
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. *
  7. * Authors:
  8. * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
  9. * Fenghua Yu <fenghua.yu@intel.com>
  10. */
  11. #include "resctrl.h"
  12. #define UNCORE_IMC "uncore_imc"
  13. #define READ_FILE_NAME "events/cas_count_read"
  14. #define WRITE_FILE_NAME "events/cas_count_write"
  15. #define DYN_PMU_PATH "/sys/bus/event_source/devices"
  16. #define SCALE 0.00006103515625
  17. #define MAX_IMCS 20
  18. #define MAX_TOKENS 5
  19. #define READ 0
  20. #define WRITE 1
  21. #define CON_MBM_LOCAL_BYTES_PATH \
  22. "%s/%s/mon_data/mon_L3_%02d/mbm_local_bytes"
  23. struct membw_read_format {
  24. __u64 value; /* The value of the event */
  25. __u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
  26. __u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
  27. __u64 id; /* if PERF_FORMAT_ID */
  28. };
  29. struct imc_counter_config {
  30. __u32 type;
  31. __u64 event;
  32. __u64 umask;
  33. struct perf_event_attr pe;
  34. struct membw_read_format return_value;
  35. int fd;
  36. };
  37. static char mbm_total_path[1024];
  38. static int imcs;
  39. static struct imc_counter_config imc_counters_config[MAX_IMCS][2];
  40. static const struct resctrl_test *current_test;
  41. void membw_initialize_perf_event_attr(int i, int j)
  42. {
  43. memset(&imc_counters_config[i][j].pe, 0,
  44. sizeof(struct perf_event_attr));
  45. imc_counters_config[i][j].pe.type = imc_counters_config[i][j].type;
  46. imc_counters_config[i][j].pe.size = sizeof(struct perf_event_attr);
  47. imc_counters_config[i][j].pe.disabled = 1;
  48. imc_counters_config[i][j].pe.inherit = 1;
  49. imc_counters_config[i][j].pe.exclude_guest = 0;
  50. imc_counters_config[i][j].pe.config =
  51. imc_counters_config[i][j].umask << 8 |
  52. imc_counters_config[i][j].event;
  53. imc_counters_config[i][j].pe.sample_type = PERF_SAMPLE_IDENTIFIER;
  54. imc_counters_config[i][j].pe.read_format =
  55. PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
  56. }
  57. void membw_ioctl_perf_event_ioc_reset_enable(int i, int j)
  58. {
  59. ioctl(imc_counters_config[i][j].fd, PERF_EVENT_IOC_RESET, 0);
  60. ioctl(imc_counters_config[i][j].fd, PERF_EVENT_IOC_ENABLE, 0);
  61. }
  62. void membw_ioctl_perf_event_ioc_disable(int i, int j)
  63. {
  64. ioctl(imc_counters_config[i][j].fd, PERF_EVENT_IOC_DISABLE, 0);
  65. }
  66. /*
  67. * get_event_and_umask: Parse config into event and umask
  68. * @cas_count_cfg: Config
  69. * @count: iMC number
  70. * @op: Operation (read/write)
  71. */
  72. void get_event_and_umask(char *cas_count_cfg, int count, bool op)
  73. {
  74. char *token[MAX_TOKENS];
  75. int i = 0;
  76. token[0] = strtok(cas_count_cfg, "=,");
  77. for (i = 1; i < MAX_TOKENS; i++)
  78. token[i] = strtok(NULL, "=,");
  79. for (i = 0; i < MAX_TOKENS - 1; i++) {
  80. if (!token[i])
  81. break;
  82. if (strcmp(token[i], "event") == 0) {
  83. if (op == READ)
  84. imc_counters_config[count][READ].event =
  85. strtol(token[i + 1], NULL, 16);
  86. else
  87. imc_counters_config[count][WRITE].event =
  88. strtol(token[i + 1], NULL, 16);
  89. }
  90. if (strcmp(token[i], "umask") == 0) {
  91. if (op == READ)
  92. imc_counters_config[count][READ].umask =
  93. strtol(token[i + 1], NULL, 16);
  94. else
  95. imc_counters_config[count][WRITE].umask =
  96. strtol(token[i + 1], NULL, 16);
  97. }
  98. }
  99. }
  100. static int open_perf_event(int i, int cpu_no, int j)
  101. {
  102. imc_counters_config[i][j].fd =
  103. perf_event_open(&imc_counters_config[i][j].pe, -1, cpu_no, -1,
  104. PERF_FLAG_FD_CLOEXEC);
  105. if (imc_counters_config[i][j].fd == -1) {
  106. fprintf(stderr, "Error opening leader %llx\n",
  107. imc_counters_config[i][j].pe.config);
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. /* Get type and config (read and write) of an iMC counter */
  113. static int read_from_imc_dir(char *imc_dir, int count)
  114. {
  115. char cas_count_cfg[1024], imc_counter_cfg[1024], imc_counter_type[1024];
  116. FILE *fp;
  117. /* Get type of iMC counter */
  118. sprintf(imc_counter_type, "%s%s", imc_dir, "type");
  119. fp = fopen(imc_counter_type, "r");
  120. if (!fp) {
  121. ksft_perror("Failed to open iMC counter type file");
  122. return -1;
  123. }
  124. if (fscanf(fp, "%u", &imc_counters_config[count][READ].type) <= 0) {
  125. ksft_perror("Could not get iMC type");
  126. fclose(fp);
  127. return -1;
  128. }
  129. fclose(fp);
  130. imc_counters_config[count][WRITE].type =
  131. imc_counters_config[count][READ].type;
  132. /* Get read config */
  133. sprintf(imc_counter_cfg, "%s%s", imc_dir, READ_FILE_NAME);
  134. fp = fopen(imc_counter_cfg, "r");
  135. if (!fp) {
  136. ksft_perror("Failed to open iMC config file");
  137. return -1;
  138. }
  139. if (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
  140. ksft_perror("Could not get iMC cas count read");
  141. fclose(fp);
  142. return -1;
  143. }
  144. fclose(fp);
  145. get_event_and_umask(cas_count_cfg, count, READ);
  146. /* Get write config */
  147. sprintf(imc_counter_cfg, "%s%s", imc_dir, WRITE_FILE_NAME);
  148. fp = fopen(imc_counter_cfg, "r");
  149. if (!fp) {
  150. ksft_perror("Failed to open iMC config file");
  151. return -1;
  152. }
  153. if (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
  154. ksft_perror("Could not get iMC cas count write");
  155. fclose(fp);
  156. return -1;
  157. }
  158. fclose(fp);
  159. get_event_and_umask(cas_count_cfg, count, WRITE);
  160. return 0;
  161. }
  162. /*
  163. * A system can have 'n' number of iMC (Integrated Memory Controller)
  164. * counters, get that 'n'. For each iMC counter get it's type and config.
  165. * Also, each counter has two configs, one for read and the other for write.
  166. * A config again has two parts, event and umask.
  167. * Enumerate all these details into an array of structures.
  168. *
  169. * Return: >= 0 on success. < 0 on failure.
  170. */
  171. static int num_of_imcs(void)
  172. {
  173. char imc_dir[512], *temp;
  174. unsigned int count = 0;
  175. struct dirent *ep;
  176. int ret;
  177. DIR *dp;
  178. dp = opendir(DYN_PMU_PATH);
  179. if (dp) {
  180. while ((ep = readdir(dp))) {
  181. temp = strstr(ep->d_name, UNCORE_IMC);
  182. if (!temp)
  183. continue;
  184. /*
  185. * imc counters are named as "uncore_imc_<n>", hence
  186. * increment the pointer to point to <n>. Note that
  187. * sizeof(UNCORE_IMC) would count for null character as
  188. * well and hence the last underscore character in
  189. * uncore_imc'_' need not be counted.
  190. */
  191. temp = temp + sizeof(UNCORE_IMC);
  192. /*
  193. * Some directories under "DYN_PMU_PATH" could have
  194. * names like "uncore_imc_free_running", hence, check if
  195. * first character is a numerical digit or not.
  196. */
  197. if (temp[0] >= '0' && temp[0] <= '9') {
  198. sprintf(imc_dir, "%s/%s/", DYN_PMU_PATH,
  199. ep->d_name);
  200. ret = read_from_imc_dir(imc_dir, count);
  201. if (ret) {
  202. closedir(dp);
  203. return ret;
  204. }
  205. count++;
  206. }
  207. }
  208. closedir(dp);
  209. if (count == 0) {
  210. ksft_print_msg("Unable to find iMC counters\n");
  211. return -1;
  212. }
  213. } else {
  214. ksft_perror("Unable to open PMU directory");
  215. return -1;
  216. }
  217. return count;
  218. }
  219. int initialize_mem_bw_imc(void)
  220. {
  221. int imc, j;
  222. imcs = num_of_imcs();
  223. if (imcs <= 0)
  224. return imcs;
  225. /* Initialize perf_event_attr structures for all iMC's */
  226. for (imc = 0; imc < imcs; imc++) {
  227. for (j = 0; j < 2; j++)
  228. membw_initialize_perf_event_attr(imc, j);
  229. }
  230. return 0;
  231. }
  232. static void perf_close_imc_mem_bw(void)
  233. {
  234. int mc;
  235. for (mc = 0; mc < imcs; mc++) {
  236. if (imc_counters_config[mc][READ].fd != -1)
  237. close(imc_counters_config[mc][READ].fd);
  238. if (imc_counters_config[mc][WRITE].fd != -1)
  239. close(imc_counters_config[mc][WRITE].fd);
  240. }
  241. }
  242. /*
  243. * perf_open_imc_mem_bw - Open perf fds for IMCs
  244. * @cpu_no: CPU number that the benchmark PID is bound to
  245. *
  246. * Return: = 0 on success. < 0 on failure.
  247. */
  248. static int perf_open_imc_mem_bw(int cpu_no)
  249. {
  250. int imc, ret;
  251. for (imc = 0; imc < imcs; imc++) {
  252. imc_counters_config[imc][READ].fd = -1;
  253. imc_counters_config[imc][WRITE].fd = -1;
  254. }
  255. for (imc = 0; imc < imcs; imc++) {
  256. ret = open_perf_event(imc, cpu_no, READ);
  257. if (ret)
  258. goto close_fds;
  259. ret = open_perf_event(imc, cpu_no, WRITE);
  260. if (ret)
  261. goto close_fds;
  262. }
  263. return 0;
  264. close_fds:
  265. perf_close_imc_mem_bw();
  266. return -1;
  267. }
  268. /*
  269. * do_mem_bw_test - Perform memory bandwidth test
  270. *
  271. * Runs memory bandwidth test over one second period. Also, handles starting
  272. * and stopping of the IMC perf counters around the test.
  273. */
  274. static void do_imc_mem_bw_test(void)
  275. {
  276. int imc;
  277. for (imc = 0; imc < imcs; imc++) {
  278. membw_ioctl_perf_event_ioc_reset_enable(imc, READ);
  279. membw_ioctl_perf_event_ioc_reset_enable(imc, WRITE);
  280. }
  281. sleep(1);
  282. /* Stop counters after a second to get results (both read and write) */
  283. for (imc = 0; imc < imcs; imc++) {
  284. membw_ioctl_perf_event_ioc_disable(imc, READ);
  285. membw_ioctl_perf_event_ioc_disable(imc, WRITE);
  286. }
  287. }
  288. /*
  289. * get_mem_bw_imc - Memory bandwidth as reported by iMC counters
  290. * @bw_report: Bandwidth report type (reads, writes)
  291. *
  292. * Memory bandwidth utilized by a process on a socket can be calculated
  293. * using iMC counters. Perf events are used to read these counters.
  294. *
  295. * Return: = 0 on success. < 0 on failure.
  296. */
  297. static int get_mem_bw_imc(const char *bw_report, float *bw_imc)
  298. {
  299. float reads, writes, of_mul_read, of_mul_write;
  300. int imc;
  301. /* Start all iMC counters to log values (both read and write) */
  302. reads = 0, writes = 0, of_mul_read = 1, of_mul_write = 1;
  303. /*
  304. * Get results which are stored in struct type imc_counter_config
  305. * Take overflow into consideration before calculating total bandwidth.
  306. */
  307. for (imc = 0; imc < imcs; imc++) {
  308. struct imc_counter_config *r =
  309. &imc_counters_config[imc][READ];
  310. struct imc_counter_config *w =
  311. &imc_counters_config[imc][WRITE];
  312. if (read(r->fd, &r->return_value,
  313. sizeof(struct membw_read_format)) == -1) {
  314. ksft_perror("Couldn't get read bandwidth through iMC");
  315. return -1;
  316. }
  317. if (read(w->fd, &w->return_value,
  318. sizeof(struct membw_read_format)) == -1) {
  319. ksft_perror("Couldn't get write bandwidth through iMC");
  320. return -1;
  321. }
  322. __u64 r_time_enabled = r->return_value.time_enabled;
  323. __u64 r_time_running = r->return_value.time_running;
  324. if (r_time_enabled != r_time_running)
  325. of_mul_read = (float)r_time_enabled /
  326. (float)r_time_running;
  327. __u64 w_time_enabled = w->return_value.time_enabled;
  328. __u64 w_time_running = w->return_value.time_running;
  329. if (w_time_enabled != w_time_running)
  330. of_mul_write = (float)w_time_enabled /
  331. (float)w_time_running;
  332. reads += r->return_value.value * of_mul_read * SCALE;
  333. writes += w->return_value.value * of_mul_write * SCALE;
  334. }
  335. if (strcmp(bw_report, "reads") == 0) {
  336. *bw_imc = reads;
  337. return 0;
  338. }
  339. if (strcmp(bw_report, "writes") == 0) {
  340. *bw_imc = writes;
  341. return 0;
  342. }
  343. *bw_imc = reads + writes;
  344. return 0;
  345. }
  346. /*
  347. * initialize_mem_bw_resctrl: Appropriately populate "mbm_total_path"
  348. * @param: Parameters passed to resctrl_val()
  349. * @domain_id: Domain ID (cache ID; for MB, L3 cache ID)
  350. */
  351. void initialize_mem_bw_resctrl(const struct resctrl_val_param *param,
  352. int domain_id)
  353. {
  354. sprintf(mbm_total_path, CON_MBM_LOCAL_BYTES_PATH, RESCTRL_PATH,
  355. param->ctrlgrp, domain_id);
  356. }
  357. /*
  358. * Open file to read MBM local bytes from resctrl FS
  359. */
  360. static FILE *open_mem_bw_resctrl(const char *mbm_bw_file)
  361. {
  362. FILE *fp;
  363. fp = fopen(mbm_bw_file, "r");
  364. if (!fp)
  365. ksft_perror("Failed to open total memory bandwidth file");
  366. return fp;
  367. }
  368. /*
  369. * Get MBM Local bytes as reported by resctrl FS
  370. */
  371. static int get_mem_bw_resctrl(FILE *fp, unsigned long *mbm_total)
  372. {
  373. if (fscanf(fp, "%lu\n", mbm_total) <= 0) {
  374. ksft_perror("Could not get MBM local bytes");
  375. return -1;
  376. }
  377. return 0;
  378. }
  379. static pid_t bm_pid, ppid;
  380. void ctrlc_handler(int signum, siginfo_t *info, void *ptr)
  381. {
  382. /* Only kill child after bm_pid is set after fork() */
  383. if (bm_pid)
  384. kill(bm_pid, SIGKILL);
  385. umount_resctrlfs();
  386. if (current_test && current_test->cleanup)
  387. current_test->cleanup();
  388. ksft_print_msg("Ending\n\n");
  389. exit(EXIT_SUCCESS);
  390. }
  391. /*
  392. * Register CTRL-C handler for parent, as it has to kill
  393. * child process before exiting.
  394. */
  395. int signal_handler_register(const struct resctrl_test *test)
  396. {
  397. struct sigaction sigact = {};
  398. int ret = 0;
  399. bm_pid = 0;
  400. current_test = test;
  401. sigact.sa_sigaction = ctrlc_handler;
  402. sigemptyset(&sigact.sa_mask);
  403. sigact.sa_flags = SA_SIGINFO;
  404. if (sigaction(SIGINT, &sigact, NULL) ||
  405. sigaction(SIGTERM, &sigact, NULL) ||
  406. sigaction(SIGHUP, &sigact, NULL)) {
  407. ksft_perror("sigaction");
  408. ret = -1;
  409. }
  410. return ret;
  411. }
  412. /*
  413. * Reset signal handler to SIG_DFL.
  414. * Non-Value return because the caller should keep
  415. * the error code of other path even if sigaction fails.
  416. */
  417. void signal_handler_unregister(void)
  418. {
  419. struct sigaction sigact = {};
  420. current_test = NULL;
  421. sigact.sa_handler = SIG_DFL;
  422. sigemptyset(&sigact.sa_mask);
  423. if (sigaction(SIGINT, &sigact, NULL) ||
  424. sigaction(SIGTERM, &sigact, NULL) ||
  425. sigaction(SIGHUP, &sigact, NULL)) {
  426. ksft_perror("sigaction");
  427. }
  428. }
  429. static void parent_exit(pid_t ppid)
  430. {
  431. kill(ppid, SIGKILL);
  432. umount_resctrlfs();
  433. exit(EXIT_FAILURE);
  434. }
  435. /*
  436. * print_results_bw: the memory bandwidth results are stored in a file
  437. * @filename: file that stores the results
  438. * @bm_pid: child pid that runs benchmark
  439. * @bw_imc: perf imc counter value
  440. * @bw_resc: memory bandwidth value
  441. *
  442. * Return: 0 on success, < 0 on error.
  443. */
  444. static int print_results_bw(char *filename, pid_t bm_pid, float bw_imc,
  445. unsigned long bw_resc)
  446. {
  447. unsigned long diff = fabs(bw_imc - bw_resc);
  448. FILE *fp;
  449. if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
  450. printf("Pid: %d \t Mem_BW_iMC: %f \t ", (int)bm_pid, bw_imc);
  451. printf("Mem_BW_resc: %lu \t Difference: %lu\n", bw_resc, diff);
  452. } else {
  453. fp = fopen(filename, "a");
  454. if (!fp) {
  455. ksft_perror("Cannot open results file");
  456. return -1;
  457. }
  458. if (fprintf(fp, "Pid: %d \t Mem_BW_iMC: %f \t Mem_BW_resc: %lu \t Difference: %lu\n",
  459. (int)bm_pid, bw_imc, bw_resc, diff) <= 0) {
  460. ksft_print_msg("Could not log results\n");
  461. fclose(fp);
  462. return -1;
  463. }
  464. fclose(fp);
  465. }
  466. return 0;
  467. }
  468. /*
  469. * measure_mem_bw - Measures memory bandwidth numbers while benchmark runs
  470. * @uparams: User supplied parameters
  471. * @param: Parameters passed to resctrl_val()
  472. * @bm_pid: PID that runs the benchmark
  473. * @bw_report: Bandwidth report type (reads, writes)
  474. *
  475. * Measure memory bandwidth from resctrl and from another source which is
  476. * perf imc value or could be something else if perf imc event is not
  477. * available. Compare the two values to validate resctrl value. It takes
  478. * 1 sec to measure the data.
  479. */
  480. int measure_mem_bw(const struct user_params *uparams,
  481. struct resctrl_val_param *param, pid_t bm_pid,
  482. const char *bw_report)
  483. {
  484. unsigned long bw_resc, bw_resc_start, bw_resc_end;
  485. FILE *mem_bw_fp;
  486. float bw_imc;
  487. int ret;
  488. bw_report = get_bw_report_type(bw_report);
  489. if (!bw_report)
  490. return -1;
  491. mem_bw_fp = open_mem_bw_resctrl(mbm_total_path);
  492. if (!mem_bw_fp)
  493. return -1;
  494. ret = perf_open_imc_mem_bw(uparams->cpu);
  495. if (ret < 0)
  496. goto close_fp;
  497. ret = get_mem_bw_resctrl(mem_bw_fp, &bw_resc_start);
  498. if (ret < 0)
  499. goto close_imc;
  500. rewind(mem_bw_fp);
  501. do_imc_mem_bw_test();
  502. ret = get_mem_bw_resctrl(mem_bw_fp, &bw_resc_end);
  503. if (ret < 0)
  504. goto close_imc;
  505. ret = get_mem_bw_imc(bw_report, &bw_imc);
  506. if (ret < 0)
  507. goto close_imc;
  508. perf_close_imc_mem_bw();
  509. fclose(mem_bw_fp);
  510. bw_resc = (bw_resc_end - bw_resc_start) / MB;
  511. return print_results_bw(param->filename, bm_pid, bw_imc, bw_resc);
  512. close_imc:
  513. perf_close_imc_mem_bw();
  514. close_fp:
  515. fclose(mem_bw_fp);
  516. return ret;
  517. }
  518. /*
  519. * run_benchmark - Run a specified benchmark or fill_buf (default benchmark)
  520. * in specified signal. Direct benchmark stdio to /dev/null.
  521. * @signum: signal number
  522. * @info: signal info
  523. * @ucontext: user context in signal handling
  524. */
  525. static void run_benchmark(int signum, siginfo_t *info, void *ucontext)
  526. {
  527. int operation, ret, memflush;
  528. char **benchmark_cmd;
  529. size_t span;
  530. bool once;
  531. FILE *fp;
  532. benchmark_cmd = info->si_ptr;
  533. /*
  534. * Direct stdio of child to /dev/null, so that only parent writes to
  535. * stdio (console)
  536. */
  537. fp = freopen("/dev/null", "w", stdout);
  538. if (!fp) {
  539. ksft_perror("Unable to direct benchmark status to /dev/null");
  540. parent_exit(ppid);
  541. }
  542. if (strcmp(benchmark_cmd[0], "fill_buf") == 0) {
  543. /* Execute default fill_buf benchmark */
  544. span = strtoul(benchmark_cmd[1], NULL, 10);
  545. memflush = atoi(benchmark_cmd[2]);
  546. operation = atoi(benchmark_cmd[3]);
  547. if (!strcmp(benchmark_cmd[4], "true")) {
  548. once = true;
  549. } else if (!strcmp(benchmark_cmd[4], "false")) {
  550. once = false;
  551. } else {
  552. ksft_print_msg("Invalid once parameter\n");
  553. parent_exit(ppid);
  554. }
  555. if (run_fill_buf(span, memflush, operation, once))
  556. fprintf(stderr, "Error in running fill buffer\n");
  557. } else {
  558. /* Execute specified benchmark */
  559. ret = execvp(benchmark_cmd[0], benchmark_cmd);
  560. if (ret)
  561. ksft_perror("execvp");
  562. }
  563. fclose(stdout);
  564. ksft_print_msg("Unable to run specified benchmark\n");
  565. parent_exit(ppid);
  566. }
  567. /*
  568. * resctrl_val: execute benchmark and measure memory bandwidth on
  569. * the benchmark
  570. * @test: test information structure
  571. * @uparams: user supplied parameters
  572. * @benchmark_cmd: benchmark command and its arguments
  573. * @param: parameters passed to resctrl_val()
  574. *
  575. * Return: 0 when the test was run, < 0 on error.
  576. */
  577. int resctrl_val(const struct resctrl_test *test,
  578. const struct user_params *uparams,
  579. const char * const *benchmark_cmd,
  580. struct resctrl_val_param *param)
  581. {
  582. struct sigaction sigact;
  583. int ret = 0, pipefd[2];
  584. char pipe_message = 0;
  585. union sigval value;
  586. int domain_id;
  587. if (strcmp(param->filename, "") == 0)
  588. sprintf(param->filename, "stdio");
  589. ret = get_domain_id(test->resource, uparams->cpu, &domain_id);
  590. if (ret < 0) {
  591. ksft_print_msg("Could not get domain ID\n");
  592. return ret;
  593. }
  594. /*
  595. * If benchmark wasn't successfully started by child, then child should
  596. * kill parent, so save parent's pid
  597. */
  598. ppid = getpid();
  599. if (pipe(pipefd)) {
  600. ksft_perror("Unable to create pipe");
  601. return -1;
  602. }
  603. /*
  604. * Fork to start benchmark, save child's pid so that it can be killed
  605. * when needed
  606. */
  607. fflush(stdout);
  608. bm_pid = fork();
  609. if (bm_pid == -1) {
  610. ksft_perror("Unable to fork");
  611. return -1;
  612. }
  613. if (bm_pid == 0) {
  614. /*
  615. * Mask all signals except SIGUSR1, parent uses SIGUSR1 to
  616. * start benchmark
  617. */
  618. sigfillset(&sigact.sa_mask);
  619. sigdelset(&sigact.sa_mask, SIGUSR1);
  620. sigact.sa_sigaction = run_benchmark;
  621. sigact.sa_flags = SA_SIGINFO;
  622. /* Register for "SIGUSR1" signal from parent */
  623. if (sigaction(SIGUSR1, &sigact, NULL)) {
  624. ksft_perror("Can't register child for signal");
  625. parent_exit(ppid);
  626. }
  627. /* Tell parent that child is ready */
  628. close(pipefd[0]);
  629. pipe_message = 1;
  630. if (write(pipefd[1], &pipe_message, sizeof(pipe_message)) <
  631. sizeof(pipe_message)) {
  632. ksft_perror("Failed signaling parent process");
  633. close(pipefd[1]);
  634. return -1;
  635. }
  636. close(pipefd[1]);
  637. /* Suspend child until delivery of "SIGUSR1" from parent */
  638. sigsuspend(&sigact.sa_mask);
  639. ksft_perror("Child is done");
  640. parent_exit(ppid);
  641. }
  642. ksft_print_msg("Benchmark PID: %d\n", (int)bm_pid);
  643. /*
  644. * The cast removes constness but nothing mutates benchmark_cmd within
  645. * the context of this process. At the receiving process, it becomes
  646. * argv, which is mutable, on exec() but that's after fork() so it
  647. * doesn't matter for the process running the tests.
  648. */
  649. value.sival_ptr = (void *)benchmark_cmd;
  650. /* Taskset benchmark to specified cpu */
  651. ret = taskset_benchmark(bm_pid, uparams->cpu, NULL);
  652. if (ret)
  653. goto out;
  654. /* Write benchmark to specified control&monitoring grp in resctrl FS */
  655. ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp);
  656. if (ret)
  657. goto out;
  658. if (param->init) {
  659. ret = param->init(param, domain_id);
  660. if (ret)
  661. goto out;
  662. }
  663. /* Parent waits for child to be ready. */
  664. close(pipefd[1]);
  665. while (pipe_message != 1) {
  666. if (read(pipefd[0], &pipe_message, sizeof(pipe_message)) <
  667. sizeof(pipe_message)) {
  668. ksft_perror("Failed reading message from child process");
  669. close(pipefd[0]);
  670. goto out;
  671. }
  672. }
  673. close(pipefd[0]);
  674. /* Signal child to start benchmark */
  675. if (sigqueue(bm_pid, SIGUSR1, value) == -1) {
  676. ksft_perror("sigqueue SIGUSR1 to child");
  677. ret = -1;
  678. goto out;
  679. }
  680. /* Give benchmark enough time to fully run */
  681. sleep(1);
  682. /* Test runs until the callback setup() tells the test to stop. */
  683. while (1) {
  684. ret = param->setup(test, uparams, param);
  685. if (ret == END_OF_TESTS) {
  686. ret = 0;
  687. break;
  688. }
  689. if (ret < 0)
  690. break;
  691. ret = param->measure(uparams, param, bm_pid);
  692. if (ret)
  693. break;
  694. }
  695. out:
  696. kill(bm_pid, SIGKILL);
  697. return ret;
  698. }