resctrl_val.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 DYN_PMU_PATH "/sys/bus/event_source/devices"
  15. #define SCALE 0.00006103515625
  16. #define MAX_IMCS 20
  17. #define MAX_TOKENS 5
  18. #define CON_MBM_LOCAL_BYTES_PATH \
  19. "%s/%s/mon_data/mon_L3_%02d/mbm_local_bytes"
  20. struct membw_read_format {
  21. __u64 value; /* The value of the event */
  22. __u64 time_enabled; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
  23. __u64 time_running; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
  24. __u64 id; /* if PERF_FORMAT_ID */
  25. };
  26. struct imc_counter_config {
  27. __u32 type;
  28. __u64 event;
  29. __u64 umask;
  30. struct perf_event_attr pe;
  31. struct membw_read_format return_value;
  32. int fd;
  33. };
  34. static char mbm_total_path[1024];
  35. static int imcs;
  36. static struct imc_counter_config imc_counters_config[MAX_IMCS];
  37. static const struct resctrl_test *current_test;
  38. static void read_mem_bw_initialize_perf_event_attr(int i)
  39. {
  40. memset(&imc_counters_config[i].pe, 0,
  41. sizeof(struct perf_event_attr));
  42. imc_counters_config[i].pe.type = imc_counters_config[i].type;
  43. imc_counters_config[i].pe.size = sizeof(struct perf_event_attr);
  44. imc_counters_config[i].pe.disabled = 1;
  45. imc_counters_config[i].pe.inherit = 1;
  46. imc_counters_config[i].pe.exclude_guest = 0;
  47. imc_counters_config[i].pe.config =
  48. imc_counters_config[i].umask << 8 |
  49. imc_counters_config[i].event;
  50. imc_counters_config[i].pe.sample_type = PERF_SAMPLE_IDENTIFIER;
  51. imc_counters_config[i].pe.read_format =
  52. PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
  53. }
  54. static void read_mem_bw_ioctl_perf_event_ioc_reset_enable(int i)
  55. {
  56. ioctl(imc_counters_config[i].fd, PERF_EVENT_IOC_RESET, 0);
  57. ioctl(imc_counters_config[i].fd, PERF_EVENT_IOC_ENABLE, 0);
  58. }
  59. static void read_mem_bw_ioctl_perf_event_ioc_disable(int i)
  60. {
  61. ioctl(imc_counters_config[i].fd, PERF_EVENT_IOC_DISABLE, 0);
  62. }
  63. /*
  64. * get_read_event_and_umask: Parse config into event and umask
  65. * @cas_count_cfg: Config
  66. * @count: iMC number
  67. */
  68. static void get_read_event_and_umask(char *cas_count_cfg, int count)
  69. {
  70. char *token[MAX_TOKENS];
  71. int i = 0;
  72. token[0] = strtok(cas_count_cfg, "=,");
  73. for (i = 1; i < MAX_TOKENS; i++)
  74. token[i] = strtok(NULL, "=,");
  75. for (i = 0; i < MAX_TOKENS - 1; i++) {
  76. if (!token[i])
  77. break;
  78. if (strcmp(token[i], "event") == 0)
  79. imc_counters_config[count].event = strtol(token[i + 1], NULL, 16);
  80. if (strcmp(token[i], "umask") == 0)
  81. imc_counters_config[count].umask = strtol(token[i + 1], NULL, 16);
  82. }
  83. }
  84. static int open_perf_read_event(int i, int cpu_no)
  85. {
  86. imc_counters_config[i].fd =
  87. perf_event_open(&imc_counters_config[i].pe, -1, cpu_no, -1,
  88. PERF_FLAG_FD_CLOEXEC);
  89. if (imc_counters_config[i].fd == -1) {
  90. fprintf(stderr, "Error opening leader %llx\n",
  91. imc_counters_config[i].pe.config);
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. /* Get type and config of an iMC counter's read event. */
  97. static int read_from_imc_dir(char *imc_dir, int count)
  98. {
  99. char cas_count_cfg[1024], imc_counter_cfg[1024], imc_counter_type[1024];
  100. FILE *fp;
  101. /* Get type of iMC counter */
  102. sprintf(imc_counter_type, "%s%s", imc_dir, "type");
  103. fp = fopen(imc_counter_type, "r");
  104. if (!fp) {
  105. ksft_perror("Failed to open iMC counter type file");
  106. return -1;
  107. }
  108. if (fscanf(fp, "%u", &imc_counters_config[count].type) <= 0) {
  109. ksft_perror("Could not get iMC type");
  110. fclose(fp);
  111. return -1;
  112. }
  113. fclose(fp);
  114. /* Get read config */
  115. sprintf(imc_counter_cfg, "%s%s", imc_dir, READ_FILE_NAME);
  116. fp = fopen(imc_counter_cfg, "r");
  117. if (!fp) {
  118. ksft_perror("Failed to open iMC config file");
  119. return -1;
  120. }
  121. if (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
  122. ksft_perror("Could not get iMC cas count read");
  123. fclose(fp);
  124. return -1;
  125. }
  126. fclose(fp);
  127. get_read_event_and_umask(cas_count_cfg, count);
  128. return 0;
  129. }
  130. /*
  131. * A system can have 'n' number of iMC (Integrated Memory Controller)
  132. * counters, get that 'n'. Discover the properties of the available
  133. * counters in support of needed performance measurement via perf.
  134. * For each iMC counter get it's type and config. Also obtain each
  135. * counter's event and umask for the memory read events that will be
  136. * measured.
  137. *
  138. * Enumerate all these details into an array of structures.
  139. *
  140. * Return: >= 0 on success. < 0 on failure.
  141. */
  142. static int num_of_imcs(void)
  143. {
  144. char imc_dir[512], *temp;
  145. unsigned int count = 0;
  146. struct dirent *ep;
  147. int ret;
  148. DIR *dp;
  149. dp = opendir(DYN_PMU_PATH);
  150. if (dp) {
  151. while ((ep = readdir(dp))) {
  152. temp = strstr(ep->d_name, UNCORE_IMC);
  153. if (!temp)
  154. continue;
  155. /*
  156. * imc counters are named as "uncore_imc_<n>", hence
  157. * increment the pointer to point to <n>. Note that
  158. * sizeof(UNCORE_IMC) would count for null character as
  159. * well and hence the last underscore character in
  160. * uncore_imc'_' need not be counted.
  161. */
  162. temp = temp + sizeof(UNCORE_IMC);
  163. /*
  164. * Some directories under "DYN_PMU_PATH" could have
  165. * names like "uncore_imc_free_running", hence, check if
  166. * first character is a numerical digit or not.
  167. */
  168. if (temp[0] >= '0' && temp[0] <= '9') {
  169. sprintf(imc_dir, "%s/%s/", DYN_PMU_PATH,
  170. ep->d_name);
  171. ret = read_from_imc_dir(imc_dir, count);
  172. if (ret) {
  173. closedir(dp);
  174. return ret;
  175. }
  176. count++;
  177. }
  178. }
  179. closedir(dp);
  180. if (count == 0) {
  181. ksft_print_msg("Unable to find iMC counters\n");
  182. return -1;
  183. }
  184. } else {
  185. ksft_perror("Unable to open PMU directory");
  186. return -1;
  187. }
  188. return count;
  189. }
  190. int initialize_read_mem_bw_imc(void)
  191. {
  192. int imc;
  193. imcs = num_of_imcs();
  194. if (imcs <= 0)
  195. return imcs;
  196. /* Initialize perf_event_attr structures for all iMC's */
  197. for (imc = 0; imc < imcs; imc++)
  198. read_mem_bw_initialize_perf_event_attr(imc);
  199. return 0;
  200. }
  201. static void perf_close_imc_read_mem_bw(void)
  202. {
  203. int mc;
  204. for (mc = 0; mc < imcs; mc++) {
  205. if (imc_counters_config[mc].fd != -1)
  206. close(imc_counters_config[mc].fd);
  207. }
  208. }
  209. /*
  210. * perf_open_imc_read_mem_bw - Open perf fds for IMCs
  211. * @cpu_no: CPU number that the benchmark PID is bound to
  212. *
  213. * Return: = 0 on success. < 0 on failure.
  214. */
  215. static int perf_open_imc_read_mem_bw(int cpu_no)
  216. {
  217. int imc, ret;
  218. for (imc = 0; imc < imcs; imc++)
  219. imc_counters_config[imc].fd = -1;
  220. for (imc = 0; imc < imcs; imc++) {
  221. ret = open_perf_read_event(imc, cpu_no);
  222. if (ret)
  223. goto close_fds;
  224. }
  225. return 0;
  226. close_fds:
  227. perf_close_imc_read_mem_bw();
  228. return -1;
  229. }
  230. /*
  231. * do_imc_read_mem_bw_test - Perform memory bandwidth test
  232. *
  233. * Runs memory bandwidth test over one second period. Also, handles starting
  234. * and stopping of the IMC perf counters around the test.
  235. */
  236. static void do_imc_read_mem_bw_test(void)
  237. {
  238. int imc;
  239. for (imc = 0; imc < imcs; imc++)
  240. read_mem_bw_ioctl_perf_event_ioc_reset_enable(imc);
  241. sleep(1);
  242. /* Stop counters after a second to get results. */
  243. for (imc = 0; imc < imcs; imc++)
  244. read_mem_bw_ioctl_perf_event_ioc_disable(imc);
  245. }
  246. /*
  247. * get_read_mem_bw_imc - Memory read bandwidth as reported by iMC counters
  248. *
  249. * Memory read bandwidth utilized by a process on a socket can be calculated
  250. * using iMC counters' read events. Perf events are used to read these
  251. * counters.
  252. *
  253. * Return: = 0 on success. < 0 on failure.
  254. */
  255. static int get_read_mem_bw_imc(float *bw_imc)
  256. {
  257. float reads = 0, of_mul_read = 1;
  258. int imc;
  259. /*
  260. * Log read event values from all iMC counters into
  261. * struct imc_counter_config.
  262. * Take overflow into consideration before calculating total bandwidth.
  263. */
  264. for (imc = 0; imc < imcs; imc++) {
  265. struct imc_counter_config *r =
  266. &imc_counters_config[imc];
  267. if (read(r->fd, &r->return_value,
  268. sizeof(struct membw_read_format)) == -1) {
  269. ksft_perror("Couldn't get read bandwidth through iMC");
  270. return -1;
  271. }
  272. __u64 r_time_enabled = r->return_value.time_enabled;
  273. __u64 r_time_running = r->return_value.time_running;
  274. if (r_time_enabled != r_time_running)
  275. of_mul_read = (float)r_time_enabled /
  276. (float)r_time_running;
  277. reads += r->return_value.value * of_mul_read * SCALE;
  278. }
  279. *bw_imc = reads;
  280. return 0;
  281. }
  282. /*
  283. * initialize_mem_bw_resctrl: Appropriately populate "mbm_total_path"
  284. * @param: Parameters passed to resctrl_val()
  285. * @domain_id: Domain ID (cache ID; for MB, L3 cache ID)
  286. */
  287. void initialize_mem_bw_resctrl(const struct resctrl_val_param *param,
  288. int domain_id)
  289. {
  290. sprintf(mbm_total_path, CON_MBM_LOCAL_BYTES_PATH, RESCTRL_PATH,
  291. param->ctrlgrp, domain_id);
  292. }
  293. /*
  294. * Open file to read MBM local bytes from resctrl FS
  295. */
  296. static FILE *open_mem_bw_resctrl(const char *mbm_bw_file)
  297. {
  298. FILE *fp;
  299. fp = fopen(mbm_bw_file, "r");
  300. if (!fp)
  301. ksft_perror("Failed to open total memory bandwidth file");
  302. return fp;
  303. }
  304. /*
  305. * Get MBM Local bytes as reported by resctrl FS
  306. */
  307. static int get_mem_bw_resctrl(FILE *fp, unsigned long *mbm_total)
  308. {
  309. if (fscanf(fp, "%lu\n", mbm_total) <= 0) {
  310. ksft_perror("Could not get MBM local bytes");
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. static pid_t bm_pid;
  316. void ctrlc_handler(int signum, siginfo_t *info, void *ptr)
  317. {
  318. /* Only kill child after bm_pid is set after fork() */
  319. if (bm_pid)
  320. kill(bm_pid, SIGKILL);
  321. umount_resctrlfs();
  322. if (current_test && current_test->cleanup)
  323. current_test->cleanup();
  324. ksft_print_msg("Ending\n\n");
  325. exit(EXIT_SUCCESS);
  326. }
  327. /*
  328. * Register CTRL-C handler for parent, as it has to kill
  329. * child process before exiting.
  330. */
  331. int signal_handler_register(const struct resctrl_test *test)
  332. {
  333. struct sigaction sigact = {};
  334. int ret = 0;
  335. bm_pid = 0;
  336. current_test = test;
  337. sigact.sa_sigaction = ctrlc_handler;
  338. sigemptyset(&sigact.sa_mask);
  339. sigact.sa_flags = SA_SIGINFO;
  340. if (sigaction(SIGINT, &sigact, NULL) ||
  341. sigaction(SIGTERM, &sigact, NULL) ||
  342. sigaction(SIGHUP, &sigact, NULL)) {
  343. ksft_perror("sigaction");
  344. ret = -1;
  345. }
  346. return ret;
  347. }
  348. /*
  349. * Reset signal handler to SIG_DFL.
  350. * Non-Value return because the caller should keep
  351. * the error code of other path even if sigaction fails.
  352. */
  353. void signal_handler_unregister(void)
  354. {
  355. struct sigaction sigact = {};
  356. current_test = NULL;
  357. sigact.sa_handler = SIG_DFL;
  358. sigemptyset(&sigact.sa_mask);
  359. if (sigaction(SIGINT, &sigact, NULL) ||
  360. sigaction(SIGTERM, &sigact, NULL) ||
  361. sigaction(SIGHUP, &sigact, NULL)) {
  362. ksft_perror("sigaction");
  363. }
  364. }
  365. /*
  366. * print_results_bw: the memory bandwidth results are stored in a file
  367. * @filename: file that stores the results
  368. * @bm_pid: child pid that runs benchmark
  369. * @bw_imc: perf imc counter value
  370. * @bw_resc: memory bandwidth value
  371. *
  372. * Return: 0 on success, < 0 on error.
  373. */
  374. static int print_results_bw(char *filename, pid_t bm_pid, float bw_imc,
  375. unsigned long bw_resc)
  376. {
  377. unsigned long diff = fabs(bw_imc - bw_resc);
  378. FILE *fp;
  379. if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
  380. printf("Pid: %d \t Mem_BW_iMC: %f \t ", (int)bm_pid, bw_imc);
  381. printf("Mem_BW_resc: %lu \t Difference: %lu\n", bw_resc, diff);
  382. } else {
  383. fp = fopen(filename, "a");
  384. if (!fp) {
  385. ksft_perror("Cannot open results file");
  386. return -1;
  387. }
  388. if (fprintf(fp, "Pid: %d \t Mem_BW_iMC: %f \t Mem_BW_resc: %lu \t Difference: %lu\n",
  389. (int)bm_pid, bw_imc, bw_resc, diff) <= 0) {
  390. ksft_print_msg("Could not log results\n");
  391. fclose(fp);
  392. return -1;
  393. }
  394. fclose(fp);
  395. }
  396. return 0;
  397. }
  398. /*
  399. * measure_read_mem_bw - Measures read memory bandwidth numbers while benchmark runs
  400. * @uparams: User supplied parameters
  401. * @param: Parameters passed to resctrl_val()
  402. * @bm_pid: PID that runs the benchmark
  403. *
  404. * Measure memory bandwidth from resctrl and from another source which is
  405. * perf imc value or could be something else if perf imc event is not
  406. * available. Compare the two values to validate resctrl value. It takes
  407. * 1 sec to measure the data.
  408. * resctrl does not distinguish between read and write operations so
  409. * its data includes all memory operations.
  410. */
  411. int measure_read_mem_bw(const struct user_params *uparams,
  412. struct resctrl_val_param *param, pid_t bm_pid)
  413. {
  414. unsigned long bw_resc, bw_resc_start, bw_resc_end;
  415. FILE *mem_bw_fp;
  416. float bw_imc;
  417. int ret;
  418. mem_bw_fp = open_mem_bw_resctrl(mbm_total_path);
  419. if (!mem_bw_fp)
  420. return -1;
  421. ret = perf_open_imc_read_mem_bw(uparams->cpu);
  422. if (ret < 0)
  423. goto close_fp;
  424. ret = get_mem_bw_resctrl(mem_bw_fp, &bw_resc_start);
  425. if (ret < 0)
  426. goto close_imc;
  427. rewind(mem_bw_fp);
  428. do_imc_read_mem_bw_test();
  429. ret = get_mem_bw_resctrl(mem_bw_fp, &bw_resc_end);
  430. if (ret < 0)
  431. goto close_imc;
  432. ret = get_read_mem_bw_imc(&bw_imc);
  433. if (ret < 0)
  434. goto close_imc;
  435. perf_close_imc_read_mem_bw();
  436. fclose(mem_bw_fp);
  437. bw_resc = (bw_resc_end - bw_resc_start) / MB;
  438. return print_results_bw(param->filename, bm_pid, bw_imc, bw_resc);
  439. close_imc:
  440. perf_close_imc_read_mem_bw();
  441. close_fp:
  442. fclose(mem_bw_fp);
  443. return ret;
  444. }
  445. /*
  446. * resctrl_val: execute benchmark and measure memory bandwidth on
  447. * the benchmark
  448. * @test: test information structure
  449. * @uparams: user supplied parameters
  450. * @param: parameters passed to resctrl_val()
  451. *
  452. * Return: 0 when the test was run, < 0 on error.
  453. */
  454. int resctrl_val(const struct resctrl_test *test,
  455. const struct user_params *uparams,
  456. struct resctrl_val_param *param)
  457. {
  458. unsigned char *buf = NULL;
  459. cpu_set_t old_affinity;
  460. int domain_id;
  461. int ret = 0;
  462. pid_t ppid;
  463. if (strcmp(param->filename, "") == 0)
  464. sprintf(param->filename, "stdio");
  465. ret = get_domain_id(test->resource, uparams->cpu, &domain_id);
  466. if (ret < 0) {
  467. ksft_print_msg("Could not get domain ID\n");
  468. return ret;
  469. }
  470. ppid = getpid();
  471. /* Taskset test to specified CPU. */
  472. ret = taskset_benchmark(ppid, uparams->cpu, &old_affinity);
  473. if (ret)
  474. return ret;
  475. /* Write test to specified control & monitoring group in resctrl FS. */
  476. ret = write_bm_pid_to_resctrl(ppid, param->ctrlgrp, param->mongrp);
  477. if (ret)
  478. goto reset_affinity;
  479. if (param->init) {
  480. ret = param->init(param, domain_id);
  481. if (ret)
  482. goto reset_affinity;
  483. }
  484. /*
  485. * If not running user provided benchmark, run the default
  486. * "fill_buf". First phase of "fill_buf" is to prepare the
  487. * buffer that the benchmark will operate on. No measurements
  488. * are needed during this phase and prepared memory will be
  489. * passed to next part of benchmark via copy-on-write thus
  490. * no impact on the benchmark that relies on reading from
  491. * memory only.
  492. */
  493. if (param->fill_buf) {
  494. buf = alloc_buffer(param->fill_buf->buf_size,
  495. param->fill_buf->memflush);
  496. if (!buf) {
  497. ret = -ENOMEM;
  498. goto reset_affinity;
  499. }
  500. }
  501. fflush(stdout);
  502. bm_pid = fork();
  503. if (bm_pid == -1) {
  504. ret = -errno;
  505. ksft_perror("Unable to fork");
  506. goto free_buf;
  507. }
  508. /*
  509. * What needs to be measured runs in separate process until
  510. * terminated.
  511. */
  512. if (bm_pid == 0) {
  513. if (param->fill_buf)
  514. fill_cache_read(buf, param->fill_buf->buf_size, false);
  515. else if (uparams->benchmark_cmd[0])
  516. execvp(uparams->benchmark_cmd[0], (char **)uparams->benchmark_cmd);
  517. exit(EXIT_SUCCESS);
  518. }
  519. ksft_print_msg("Benchmark PID: %d\n", (int)bm_pid);
  520. /* Give benchmark enough time to fully run. */
  521. sleep(1);
  522. /* Test runs until the callback setup() tells the test to stop. */
  523. while (1) {
  524. ret = param->setup(test, uparams, param);
  525. if (ret == END_OF_TESTS) {
  526. ret = 0;
  527. break;
  528. }
  529. if (ret < 0)
  530. break;
  531. ret = param->measure(uparams, param, bm_pid);
  532. if (ret)
  533. break;
  534. }
  535. kill(bm_pid, SIGKILL);
  536. free_buf:
  537. free(buf);
  538. reset_affinity:
  539. taskset_restore(ppid, &old_affinity);
  540. return ret;
  541. }