1 // SPDX-License-Identifier: GPL-2.0
3 * Basic resctrl file system operations
5 * Copyright (C) 2018 Intel Corporation
8 * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
9 * Fenghua Yu <fenghua.yu@intel.com>
13 static int find_resctrl_mount(char *buffer)
16 char line[256], *fs, *mntpoint;
18 mounts = fopen("/proc/mounts", "r");
20 perror("/proc/mounts");
23 while (!feof(mounts)) {
24 if (!fgets(line, 256, mounts))
26 fs = strtok(line, " \t");
29 mntpoint = strtok(NULL, " \t");
32 fs = strtok(NULL, " \t");
35 if (strcmp(fs, "resctrl"))
40 strncpy(buffer, mntpoint, 256);
51 * mount_resctrlfs - Mount resctrl FS at /sys/fs/resctrl
53 * Mounts resctrl FS. Fails if resctrl FS is already mounted to avoid
54 * pre-existing settings interfering with the test results.
56 * Return: 0 on success, non-zero on failure
58 int mount_resctrlfs(void)
62 ret = find_resctrl_mount(NULL);
66 ksft_print_msg("Mounting resctrl to \"%s\"\n", RESCTRL_PATH);
67 ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
74 int umount_resctrlfs(void)
79 ret = find_resctrl_mount(mountpoint);
85 if (umount(mountpoint)) {
86 perror("# Unable to umount resctrl");
95 * get_resource_id - Get socket number/l3 id for a specified CPU
97 * @resource_id: Socket number or l3_id
99 * Return: >= 0 on success, < 0 on failure.
101 int get_resource_id(int cpu_no, int *resource_id)
103 char phys_pkg_path[1024];
106 if (get_vendor() == ARCH_AMD)
107 sprintf(phys_pkg_path, "%s%d/cache/index3/id",
108 PHYS_ID_PATH, cpu_no);
110 sprintf(phys_pkg_path, "%s%d/topology/physical_package_id",
111 PHYS_ID_PATH, cpu_no);
113 fp = fopen(phys_pkg_path, "r");
115 perror("Failed to open physical_package_id");
119 if (fscanf(fp, "%d", resource_id) <= 0) {
120 perror("Could not get socket number or l3 id");
131 * get_cache_size - Get cache size for a specified CPU
132 * @cpu_no: CPU number
133 * @cache_type: Cache level L2/L3
134 * @cache_size: pointer to cache_size
136 * Return: = 0 on success, < 0 on failure.
138 int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size)
140 char cache_path[1024], cache_str[64];
141 int length, i, cache_num;
144 if (!strcmp(cache_type, "L3")) {
146 } else if (!strcmp(cache_type, "L2")) {
149 perror("Invalid cache level");
153 sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
155 fp = fopen(cache_path, "r");
157 perror("Failed to open cache size");
161 if (fscanf(fp, "%s", cache_str) <= 0) {
162 perror("Could not get cache_size");
169 length = (int)strlen(cache_str);
173 for (i = 0; i < length; i++) {
174 if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
176 *cache_size = *cache_size * 10 + (cache_str[i] - '0');
178 else if (cache_str[i] == 'K')
180 *cache_size = *cache_size * 1024;
182 else if (cache_str[i] == 'M')
184 *cache_size = *cache_size * 1024 * 1024;
193 #define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu"
196 * get_cbm_mask - Get cbm mask for given cache
197 * @cache_type: Cache level L2/L3
198 * @cbm_mask: cbm_mask returned as a string
200 * Return: = 0 on success, < 0 on failure.
202 int get_cbm_mask(char *cache_type, char *cbm_mask)
204 char cbm_mask_path[1024];
210 sprintf(cbm_mask_path, "%s/%s/cbm_mask", INFO_PATH, cache_type);
212 fp = fopen(cbm_mask_path, "r");
214 perror("Failed to open cache level");
218 if (fscanf(fp, "%s", cbm_mask) <= 0) {
219 perror("Could not get max cbm_mask");
230 * get_core_sibling - Get sibling core id from the same socket for given CPU
231 * @cpu_no: CPU number
233 * Return: > 0 on success, < 0 on failure.
235 int get_core_sibling(int cpu_no)
237 char core_siblings_path[1024], cpu_list_str[64];
238 int sibling_cpu_no = -1;
241 sprintf(core_siblings_path, "%s%d/topology/core_siblings_list",
242 CORE_SIBLINGS_PATH, cpu_no);
244 fp = fopen(core_siblings_path, "r");
246 perror("Failed to open core siblings path");
250 if (fscanf(fp, "%s", cpu_list_str) <= 0) {
251 perror("Could not get core_siblings list");
258 char *token = strtok(cpu_list_str, "-,");
261 sibling_cpu_no = atoi(token);
262 /* Skipping core 0 as we don't want to run test on core 0 */
263 if (sibling_cpu_no != 0 && sibling_cpu_no != cpu_no)
265 token = strtok(NULL, "-,");
268 return sibling_cpu_no;
272 * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
273 * @bm_pid: PID that should be binded
274 * @cpu_no: CPU number at which the PID would be binded
276 * Return: 0 on success, non-zero on failure
278 int taskset_benchmark(pid_t bm_pid, int cpu_no)
283 CPU_SET(cpu_no, &my_set);
285 if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
286 perror("Unable to taskset benchmark");
295 * run_benchmark - Run a specified benchmark or fill_buf (default benchmark)
296 * in specified signal. Direct benchmark stdio to /dev/null.
297 * @signum: signal number
299 * @ucontext: user context in signal handling
303 void run_benchmark(int signum, siginfo_t *info, void *ucontext)
305 int operation, ret, memflush;
306 char **benchmark_cmd;
311 benchmark_cmd = info->si_ptr;
314 * Direct stdio of child to /dev/null, so that only parent writes to
317 fp = freopen("/dev/null", "w", stdout);
319 PARENT_EXIT("Unable to direct benchmark status to /dev/null");
321 if (strcmp(benchmark_cmd[0], "fill_buf") == 0) {
322 /* Execute default fill_buf benchmark */
323 span = strtoul(benchmark_cmd[1], NULL, 10);
324 memflush = atoi(benchmark_cmd[2]);
325 operation = atoi(benchmark_cmd[3]);
326 if (!strcmp(benchmark_cmd[4], "true"))
328 else if (!strcmp(benchmark_cmd[4], "false"))
331 PARENT_EXIT("Invalid once parameter");
333 if (run_fill_buf(span, memflush, operation, once))
334 fprintf(stderr, "Error in running fill buffer\n");
336 /* Execute specified benchmark */
337 ret = execvp(benchmark_cmd[0], benchmark_cmd);
343 PARENT_EXIT("Unable to run specified benchmark");
347 * create_grp - Create a group only if one doesn't exist
348 * @grp_name: Name of the group
349 * @grp: Full path and name of the group
350 * @parent_grp: Full path and name of the parent group
352 * Return: 0 on success, non-zero on failure
354 static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
361 * At this point, we are guaranteed to have resctrl FS mounted and if
362 * length of grp_name == 0, it means, user wants to use root con_mon
365 if (strlen(grp_name) == 0)
368 /* Check if requested grp exists or not */
369 dp = opendir(parent_grp);
371 while ((ep = readdir(dp)) != NULL) {
372 if (strcmp(ep->d_name, grp_name) == 0)
377 perror("Unable to open resctrl for group");
382 /* Requested grp doesn't exist, hence create it */
383 if (found_grp == 0) {
384 if (mkdir(grp, 0) == -1) {
385 perror("Unable to create group");
394 static int write_pid_to_tasks(char *tasks, pid_t pid)
398 fp = fopen(tasks, "w");
400 perror("Failed to open tasks file");
404 if (fprintf(fp, "%d\n", pid) < 0) {
405 perror("Failed to wr pid to tasks file");
416 * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
417 * @bm_pid: PID that should be written
418 * @ctrlgrp: Name of the control monitor group (con_mon grp)
419 * @mongrp: Name of the monitor group (mon grp)
420 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
422 * If a con_mon grp is requested, create it and write pid to it, otherwise
423 * write pid to root con_mon grp.
424 * If a mon grp is requested, create it and write pid to it, otherwise
425 * pid is not written, this means that pid is in con_mon grp and hence
426 * should consult con_mon grp's mon_data directory for results.
428 * Return: 0 on success, non-zero on failure
430 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
433 char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
438 sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
440 sprintf(controlgroup, "%s", RESCTRL_PATH);
442 /* Create control and monitoring group and write pid into it */
443 ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
446 sprintf(tasks, "%s/tasks", controlgroup);
447 ret = write_pid_to_tasks(tasks, bm_pid);
451 /* Create mon grp and write pid into it for "mbm" and "cmt" test */
452 if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)) ||
453 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
454 if (strlen(mongrp)) {
455 sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
456 sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
457 ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
461 sprintf(tasks, "%s/mon_groups/%s/tasks",
462 controlgroup, mongrp);
463 ret = write_pid_to_tasks(tasks, bm_pid);
470 ksft_print_msg("Writing benchmark parameters to resctrl FS\n");
472 perror("# writing to resctrlfs");
478 * write_schemata - Update schemata of a con_mon grp
479 * @ctrlgrp: Name of the con_mon grp
480 * @schemata: Schemata that should be updated to
481 * @cpu_no: CPU number that the benchmark PID is binded to
482 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
484 * Update schemata of a con_mon grp *only* if requested resctrl feature is
487 * Return: 0 on success, non-zero on failure
489 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
491 char controlgroup[1024], schema[1024], reason[64];
492 int resource_id, ret = 0;
495 if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
496 strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) &&
497 strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) &&
498 strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
502 ksft_print_msg("Skipping empty schemata update\n");
507 if (get_resource_id(cpu_no, &resource_id) < 0) {
508 sprintf(reason, "Failed to get resource id");
514 if (strlen(ctrlgrp) != 0)
515 sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
517 sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
519 if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
520 !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
521 sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
522 if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
523 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)))
524 sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
526 fp = fopen(controlgroup, "w");
528 sprintf(reason, "Failed to open control group");
534 if (fprintf(fp, "%s\n", schema) < 0) {
535 sprintf(reason, "Failed to write schemata in control group");
544 ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
545 schema, ret ? " # " : "",
551 bool check_resctrlfs_support(void)
553 FILE *inf = fopen("/proc/filesystems", "r");
561 res = fgrep(inf, "nodev\tresctrl\n");
570 ksft_print_msg("%s Check kernel supports resctrl filesystem\n",
571 ret ? "Pass:" : "Fail:");
576 dp = opendir(RESCTRL_PATH);
577 ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n",
578 dp ? "Pass:" : "Fail:", RESCTRL_PATH);
582 ksft_print_msg("resctrl filesystem %s mounted\n",
583 find_resctrl_mount(NULL) ? "not" : "is");
588 char *fgrep(FILE *inf, const char *str)
591 int slen = strlen(str);
594 if (!fgets(line, 256, inf))
596 if (strncmp(line, str, slen))
606 * validate_resctrl_feature_request - Check if requested feature is valid.
607 * @resctrl_val: Requested feature
609 * Return: True if the feature is supported, else false. False is also
610 * returned if resctrl FS is not mounted.
612 bool validate_resctrl_feature_request(const char *resctrl_val)
623 ret = find_resctrl_mount(NULL);
627 if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) {
628 if (!stat(L3_PATH, &statbuf))
630 } else if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
631 if (!stat(MB_PATH, &statbuf))
633 } else if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
634 !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) {
635 if (!stat(L3_MON_PATH, &statbuf)) {
636 inf = fopen(L3_MON_FEATURES_PATH, "r");
640 if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) {
641 res = fgrep(inf, "llc_occupancy");
648 if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
649 res = fgrep(inf, "mbm_total_bytes");
652 res = fgrep(inf, "mbm_local_bytes");
666 int filter_dmesg(void)
683 dup2(pipefds[1], STDOUT_FILENO);
684 execlp("dmesg", "dmesg", NULL);
685 perror("executing dmesg");
689 fp = fdopen(pipefds[0], "r");
691 perror("fdopen(pipe)");
697 while (fgets(line, 1024, fp)) {
698 if (strstr(line, "intel_rdt:"))
699 ksft_print_msg("dmesg: %s", line);
700 if (strstr(line, "resctrl:"))
701 ksft_print_msg("dmesg: %s", line);
704 waitpid(pid, NULL, 0);
709 int validate_bw_report_request(char *bw_report)
711 if (strcmp(bw_report, "reads") == 0)
713 if (strcmp(bw_report, "writes") == 0)
715 if (strcmp(bw_report, "nt-writes") == 0) {
716 strcpy(bw_report, "writes");
719 if (strcmp(bw_report, "total") == 0)
722 fprintf(stderr, "Requested iMC B/W report type unavailable\n");
727 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
728 int group_fd, unsigned long flags)
732 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
737 unsigned int count_bits(unsigned long n)
739 unsigned int count = 0;