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>
15 static int find_resctrl_mount(char *buffer)
18 char line[256], *fs, *mntpoint;
20 mounts = fopen("/proc/mounts", "r");
22 perror("/proc/mounts");
25 while (!feof(mounts)) {
26 if (!fgets(line, 256, mounts))
28 fs = strtok(line, " \t");
31 mntpoint = strtok(NULL, " \t");
34 fs = strtok(NULL, " \t");
37 if (strcmp(fs, "resctrl"))
42 strncpy(buffer, mntpoint, 256);
53 * mount_resctrlfs - Mount resctrl FS at /sys/fs/resctrl
55 * Mounts resctrl FS. Fails if resctrl FS is already mounted to avoid
56 * pre-existing settings interfering with the test results.
58 * Return: 0 on success, non-zero on failure
60 int mount_resctrlfs(void)
64 ret = find_resctrl_mount(NULL);
68 ksft_print_msg("Mounting resctrl to \"%s\"\n", RESCTRL_PATH);
69 ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
76 int umount_resctrlfs(void)
81 ret = find_resctrl_mount(mountpoint);
87 if (umount(mountpoint)) {
88 perror("# Unable to umount resctrl");
97 * get_resource_id - Get socket number/l3 id for a specified CPU
99 * @resource_id: Socket number or l3_id
101 * Return: >= 0 on success, < 0 on failure.
103 int get_resource_id(int cpu_no, int *resource_id)
105 char phys_pkg_path[1024];
108 if (get_vendor() == ARCH_AMD)
109 sprintf(phys_pkg_path, "%s%d/cache/index3/id",
110 PHYS_ID_PATH, cpu_no);
112 sprintf(phys_pkg_path, "%s%d/topology/physical_package_id",
113 PHYS_ID_PATH, cpu_no);
115 fp = fopen(phys_pkg_path, "r");
117 perror("Failed to open physical_package_id");
121 if (fscanf(fp, "%d", resource_id) <= 0) {
122 perror("Could not get socket number or l3 id");
133 * get_cache_size - Get cache size for a specified CPU
134 * @cpu_no: CPU number
135 * @cache_type: Cache level L2/L3
136 * @cache_size: pointer to cache_size
138 * Return: = 0 on success, < 0 on failure.
140 int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size)
142 char cache_path[1024], cache_str[64];
143 int length, i, cache_num;
146 if (!strcmp(cache_type, "L3")) {
148 } else if (!strcmp(cache_type, "L2")) {
151 perror("Invalid cache level");
155 sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
157 fp = fopen(cache_path, "r");
159 perror("Failed to open cache size");
163 if (fscanf(fp, "%s", cache_str) <= 0) {
164 perror("Could not get cache_size");
171 length = (int)strlen(cache_str);
175 for (i = 0; i < length; i++) {
176 if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
178 *cache_size = *cache_size * 10 + (cache_str[i] - '0');
180 else if (cache_str[i] == 'K')
182 *cache_size = *cache_size * 1024;
184 else if (cache_str[i] == 'M')
186 *cache_size = *cache_size * 1024 * 1024;
195 #define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu"
198 * get_cbm_mask - Get cbm mask for given cache
199 * @cache_type: Cache level L2/L3
200 * @cbm_mask: cbm_mask returned as a string
202 * Return: = 0 on success, < 0 on failure.
204 int get_cbm_mask(char *cache_type, char *cbm_mask)
206 char cbm_mask_path[1024];
212 sprintf(cbm_mask_path, "%s/%s/cbm_mask", INFO_PATH, cache_type);
214 fp = fopen(cbm_mask_path, "r");
216 perror("Failed to open cache level");
220 if (fscanf(fp, "%s", cbm_mask) <= 0) {
221 perror("Could not get max cbm_mask");
232 * get_core_sibling - Get sibling core id from the same socket for given CPU
233 * @cpu_no: CPU number
235 * Return: > 0 on success, < 0 on failure.
237 int get_core_sibling(int cpu_no)
239 char core_siblings_path[1024], cpu_list_str[64];
240 int sibling_cpu_no = -1;
243 sprintf(core_siblings_path, "%s%d/topology/core_siblings_list",
244 CORE_SIBLINGS_PATH, cpu_no);
246 fp = fopen(core_siblings_path, "r");
248 perror("Failed to open core siblings path");
252 if (fscanf(fp, "%s", cpu_list_str) <= 0) {
253 perror("Could not get core_siblings list");
260 char *token = strtok(cpu_list_str, "-,");
263 sibling_cpu_no = atoi(token);
264 /* Skipping core 0 as we don't want to run test on core 0 */
265 if (sibling_cpu_no != 0 && sibling_cpu_no != cpu_no)
267 token = strtok(NULL, "-,");
270 return sibling_cpu_no;
274 * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
275 * @bm_pid: PID that should be binded
276 * @cpu_no: CPU number at which the PID would be binded
278 * Return: 0 on success, non-zero on failure
280 int taskset_benchmark(pid_t bm_pid, int cpu_no)
285 CPU_SET(cpu_no, &my_set);
287 if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
288 perror("Unable to taskset benchmark");
297 * run_benchmark - Run a specified benchmark or fill_buf (default benchmark)
298 * in specified signal. Direct benchmark stdio to /dev/null.
299 * @signum: signal number
301 * @ucontext: user context in signal handling
305 void run_benchmark(int signum, siginfo_t *info, void *ucontext)
307 int operation, ret, memflush;
308 char **benchmark_cmd;
313 benchmark_cmd = info->si_ptr;
316 * Direct stdio of child to /dev/null, so that only parent writes to
319 fp = freopen("/dev/null", "w", stdout);
321 PARENT_EXIT("Unable to direct benchmark status to /dev/null");
323 if (strcmp(benchmark_cmd[0], "fill_buf") == 0) {
324 /* Execute default fill_buf benchmark */
325 span = strtoul(benchmark_cmd[1], NULL, 10);
326 memflush = atoi(benchmark_cmd[2]);
327 operation = atoi(benchmark_cmd[3]);
328 if (!strcmp(benchmark_cmd[4], "true"))
330 else if (!strcmp(benchmark_cmd[4], "false"))
333 PARENT_EXIT("Invalid once parameter");
335 if (run_fill_buf(span, memflush, operation, once))
336 fprintf(stderr, "Error in running fill buffer\n");
338 /* Execute specified benchmark */
339 ret = execvp(benchmark_cmd[0], benchmark_cmd);
345 PARENT_EXIT("Unable to run specified benchmark");
349 * create_grp - Create a group only if one doesn't exist
350 * @grp_name: Name of the group
351 * @grp: Full path and name of the group
352 * @parent_grp: Full path and name of the parent group
354 * Return: 0 on success, non-zero on failure
356 static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
363 * At this point, we are guaranteed to have resctrl FS mounted and if
364 * length of grp_name == 0, it means, user wants to use root con_mon
367 if (strlen(grp_name) == 0)
370 /* Check if requested grp exists or not */
371 dp = opendir(parent_grp);
373 while ((ep = readdir(dp)) != NULL) {
374 if (strcmp(ep->d_name, grp_name) == 0)
379 perror("Unable to open resctrl for group");
384 /* Requested grp doesn't exist, hence create it */
385 if (found_grp == 0) {
386 if (mkdir(grp, 0) == -1) {
387 perror("Unable to create group");
396 static int write_pid_to_tasks(char *tasks, pid_t pid)
400 fp = fopen(tasks, "w");
402 perror("Failed to open tasks file");
406 if (fprintf(fp, "%d\n", pid) < 0) {
407 perror("Failed to wr pid to tasks file");
418 * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
419 * @bm_pid: PID that should be written
420 * @ctrlgrp: Name of the control monitor group (con_mon grp)
421 * @mongrp: Name of the monitor group (mon grp)
422 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
424 * If a con_mon grp is requested, create it and write pid to it, otherwise
425 * write pid to root con_mon grp.
426 * If a mon grp is requested, create it and write pid to it, otherwise
427 * pid is not written, this means that pid is in con_mon grp and hence
428 * should consult con_mon grp's mon_data directory for results.
430 * Return: 0 on success, non-zero on failure
432 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
435 char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
440 sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
442 sprintf(controlgroup, "%s", RESCTRL_PATH);
444 /* Create control and monitoring group and write pid into it */
445 ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
448 sprintf(tasks, "%s/tasks", controlgroup);
449 ret = write_pid_to_tasks(tasks, bm_pid);
453 /* Create mon grp and write pid into it for "mbm" and "cmt" test */
454 if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)) ||
455 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
456 if (strlen(mongrp)) {
457 sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
458 sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
459 ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
463 sprintf(tasks, "%s/mon_groups/%s/tasks",
464 controlgroup, mongrp);
465 ret = write_pid_to_tasks(tasks, bm_pid);
472 ksft_print_msg("Writing benchmark parameters to resctrl FS\n");
474 perror("# writing to resctrlfs");
480 * write_schemata - Update schemata of a con_mon grp
481 * @ctrlgrp: Name of the con_mon grp
482 * @schemata: Schemata that should be updated to
483 * @cpu_no: CPU number that the benchmark PID is binded to
484 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
486 * Update schemata of a con_mon grp *only* if requested resctrl feature is
489 * Return: 0 on success, non-zero on failure
491 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
493 char controlgroup[1024], schema[1024], reason[64];
494 int resource_id, ret = 0;
497 if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
498 strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) &&
499 strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) &&
500 strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
504 ksft_print_msg("Skipping empty schemata update\n");
509 if (get_resource_id(cpu_no, &resource_id) < 0) {
510 sprintf(reason, "Failed to get resource id");
516 if (strlen(ctrlgrp) != 0)
517 sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
519 sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
521 if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
522 !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
523 sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
524 if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
525 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)))
526 sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
528 fp = fopen(controlgroup, "w");
530 sprintf(reason, "Failed to open control group");
536 if (fprintf(fp, "%s\n", schema) < 0) {
537 sprintf(reason, "Failed to write schemata in control group");
546 ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
547 schema, ret ? " # " : "",
553 bool check_resctrlfs_support(void)
555 FILE *inf = fopen("/proc/filesystems", "r");
563 res = fgrep(inf, "nodev\tresctrl\n");
572 ksft_print_msg("%s Check kernel supports resctrl filesystem\n",
573 ret ? "Pass:" : "Fail:");
578 dp = opendir(RESCTRL_PATH);
579 ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n",
580 dp ? "Pass:" : "Fail:", RESCTRL_PATH);
584 ksft_print_msg("resctrl filesystem %s mounted\n",
585 find_resctrl_mount(NULL) ? "not" : "is");
590 char *fgrep(FILE *inf, const char *str)
593 int slen = strlen(str);
596 if (!fgets(line, 256, inf))
598 if (strncmp(line, str, slen))
608 * validate_resctrl_feature_request - Check if requested feature is valid.
609 * @resource: Required resource (e.g., MB, L3, L2, L3_MON, etc.)
610 * @feature: Required monitor feature (in mon_features file). Can only be
611 * set for L3_MON. Must be NULL for all other resources.
613 * Return: True if the resource/feature is supported, else false. False is
614 * also returned if resctrl FS is not mounted.
616 bool validate_resctrl_feature_request(const char *resource, const char *feature)
618 char res_path[PATH_MAX];
627 ret = find_resctrl_mount(NULL);
631 snprintf(res_path, sizeof(res_path), "%s/%s", INFO_PATH, resource);
633 if (stat(res_path, &statbuf))
639 snprintf(res_path, sizeof(res_path), "%s/%s/mon_features", INFO_PATH, resource);
640 inf = fopen(res_path, "r");
644 res = fgrep(inf, feature);
651 int filter_dmesg(void)
668 dup2(pipefds[1], STDOUT_FILENO);
669 execlp("dmesg", "dmesg", NULL);
670 perror("executing dmesg");
674 fp = fdopen(pipefds[0], "r");
676 perror("fdopen(pipe)");
682 while (fgets(line, 1024, fp)) {
683 if (strstr(line, "intel_rdt:"))
684 ksft_print_msg("dmesg: %s", line);
685 if (strstr(line, "resctrl:"))
686 ksft_print_msg("dmesg: %s", line);
689 waitpid(pid, NULL, 0);
694 int validate_bw_report_request(char *bw_report)
696 if (strcmp(bw_report, "reads") == 0)
698 if (strcmp(bw_report, "writes") == 0)
700 if (strcmp(bw_report, "nt-writes") == 0) {
701 strcpy(bw_report, "writes");
704 if (strcmp(bw_report, "total") == 0)
707 fprintf(stderr, "Requested iMC B/W report type unavailable\n");
712 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
713 int group_fd, unsigned long flags)
717 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
722 unsigned int count_bits(unsigned long n)
724 unsigned int count = 0;