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