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>
16 static int find_resctrl_mount(char *buffer)
19 char line[256], *fs, *mntpoint;
21 mounts = fopen("/proc/mounts", "r");
23 perror("/proc/mounts");
26 while (!feof(mounts)) {
27 if (!fgets(line, 256, mounts))
29 fs = strtok(line, " \t");
32 mntpoint = strtok(NULL, " \t");
35 fs = strtok(NULL, " \t");
38 if (strcmp(fs, "resctrl"))
43 strncpy(buffer, mntpoint, 256);
54 * mount_resctrlfs - Mount resctrl FS at /sys/fs/resctrl
56 * Mounts resctrl FS. Fails if resctrl FS is already mounted to avoid
57 * pre-existing settings interfering with the test results.
59 * Return: 0 on success, non-zero on failure
61 int mount_resctrlfs(void)
65 ret = find_resctrl_mount(NULL);
69 ksft_print_msg("Mounting resctrl to \"%s\"\n", RESCTRL_PATH);
70 ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
77 int umount_resctrlfs(void)
82 ret = find_resctrl_mount(mountpoint);
88 if (umount(mountpoint)) {
89 perror("# Unable to umount resctrl");
98 * get_resource_id - Get socket number/l3 id for a specified CPU
100 * @resource_id: Socket number or l3_id
102 * Return: >= 0 on success, < 0 on failure.
104 int get_resource_id(int cpu_no, int *resource_id)
106 char phys_pkg_path[1024];
109 if (get_vendor() == ARCH_AMD)
110 sprintf(phys_pkg_path, "%s%d/cache/index3/id",
111 PHYS_ID_PATH, cpu_no);
113 sprintf(phys_pkg_path, "%s%d/topology/physical_package_id",
114 PHYS_ID_PATH, cpu_no);
116 fp = fopen(phys_pkg_path, "r");
118 perror("Failed to open physical_package_id");
122 if (fscanf(fp, "%d", resource_id) <= 0) {
123 perror("Could not get socket number or l3 id");
134 * get_cache_size - Get cache size for a specified CPU
135 * @cpu_no: CPU number
136 * @cache_type: Cache level L2/L3
137 * @cache_size: pointer to cache_size
139 * Return: = 0 on success, < 0 on failure.
141 int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size)
143 char cache_path[1024], cache_str[64];
144 int length, i, cache_num;
147 if (!strcmp(cache_type, "L3")) {
149 } else if (!strcmp(cache_type, "L2")) {
152 perror("Invalid cache level");
156 sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
158 fp = fopen(cache_path, "r");
160 perror("Failed to open cache size");
164 if (fscanf(fp, "%s", cache_str) <= 0) {
165 perror("Could not get cache_size");
172 length = (int)strlen(cache_str);
176 for (i = 0; i < length; i++) {
177 if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
179 *cache_size = *cache_size * 10 + (cache_str[i] - '0');
181 else if (cache_str[i] == 'K')
183 *cache_size = *cache_size * 1024;
185 else if (cache_str[i] == 'M')
187 *cache_size = *cache_size * 1024 * 1024;
196 #define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu"
199 * get_cbm_mask - Get cbm mask for given cache
200 * @cache_type: Cache level L2/L3
201 * @cbm_mask: cbm_mask returned as a string
203 * Return: = 0 on success, < 0 on failure.
205 int get_cbm_mask(char *cache_type, char *cbm_mask)
207 char cbm_mask_path[1024];
213 sprintf(cbm_mask_path, "%s/%s/cbm_mask", INFO_PATH, cache_type);
215 fp = fopen(cbm_mask_path, "r");
217 perror("Failed to open cache level");
221 if (fscanf(fp, "%s", cbm_mask) <= 0) {
222 perror("Could not get max cbm_mask");
233 * get_core_sibling - Get sibling core id from the same socket for given CPU
234 * @cpu_no: CPU number
236 * Return: > 0 on success, < 0 on failure.
238 int get_core_sibling(int cpu_no)
240 char core_siblings_path[1024], cpu_list_str[64];
241 int sibling_cpu_no = -1;
244 sprintf(core_siblings_path, "%s%d/topology/core_siblings_list",
245 CORE_SIBLINGS_PATH, cpu_no);
247 fp = fopen(core_siblings_path, "r");
249 perror("Failed to open core siblings path");
253 if (fscanf(fp, "%s", cpu_list_str) <= 0) {
254 perror("Could not get core_siblings list");
261 char *token = strtok(cpu_list_str, "-,");
264 sibling_cpu_no = atoi(token);
265 /* Skipping core 0 as we don't want to run test on core 0 */
266 if (sibling_cpu_no != 0 && sibling_cpu_no != cpu_no)
268 token = strtok(NULL, "-,");
271 return sibling_cpu_no;
275 * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
276 * @bm_pid: PID that should be binded
277 * @cpu_no: CPU number at which the PID would be binded
279 * Return: 0 on success, non-zero on failure
281 int taskset_benchmark(pid_t bm_pid, int cpu_no)
286 CPU_SET(cpu_no, &my_set);
288 if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
289 perror("Unable to taskset benchmark");
298 * create_grp - Create a group only if one doesn't exist
299 * @grp_name: Name of the group
300 * @grp: Full path and name of the group
301 * @parent_grp: Full path and name of the parent group
303 * Return: 0 on success, non-zero on failure
305 static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
312 * At this point, we are guaranteed to have resctrl FS mounted and if
313 * length of grp_name == 0, it means, user wants to use root con_mon
316 if (strlen(grp_name) == 0)
319 /* Check if requested grp exists or not */
320 dp = opendir(parent_grp);
322 while ((ep = readdir(dp)) != NULL) {
323 if (strcmp(ep->d_name, grp_name) == 0)
328 perror("Unable to open resctrl for group");
333 /* Requested grp doesn't exist, hence create it */
334 if (found_grp == 0) {
335 if (mkdir(grp, 0) == -1) {
336 perror("Unable to create group");
345 static int write_pid_to_tasks(char *tasks, pid_t pid)
349 fp = fopen(tasks, "w");
351 perror("Failed to open tasks file");
355 if (fprintf(fp, "%d\n", pid) < 0) {
356 perror("Failed to wr pid to tasks file");
367 * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
368 * @bm_pid: PID that should be written
369 * @ctrlgrp: Name of the control monitor group (con_mon grp)
370 * @mongrp: Name of the monitor group (mon grp)
371 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
373 * If a con_mon grp is requested, create it and write pid to it, otherwise
374 * write pid to root con_mon grp.
375 * If a mon grp is requested, create it and write pid to it, otherwise
376 * pid is not written, this means that pid is in con_mon grp and hence
377 * should consult con_mon grp's mon_data directory for results.
379 * Return: 0 on success, non-zero on failure
381 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
384 char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
389 sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
391 sprintf(controlgroup, "%s", RESCTRL_PATH);
393 /* Create control and monitoring group and write pid into it */
394 ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
397 sprintf(tasks, "%s/tasks", controlgroup);
398 ret = write_pid_to_tasks(tasks, bm_pid);
402 /* Create mon grp and write pid into it for "mbm" and "cmt" test */
403 if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)) ||
404 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
405 if (strlen(mongrp)) {
406 sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
407 sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
408 ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
412 sprintf(tasks, "%s/mon_groups/%s/tasks",
413 controlgroup, mongrp);
414 ret = write_pid_to_tasks(tasks, bm_pid);
421 ksft_print_msg("Writing benchmark parameters to resctrl FS\n");
423 perror("# writing to resctrlfs");
429 * write_schemata - Update schemata of a con_mon grp
430 * @ctrlgrp: Name of the con_mon grp
431 * @schemata: Schemata that should be updated to
432 * @cpu_no: CPU number that the benchmark PID is binded to
433 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
435 * Update schemata of a con_mon grp *only* if requested resctrl feature is
438 * Return: 0 on success, non-zero on failure
440 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
442 char controlgroup[1024], reason[128], schema[1024] = {};
443 int resource_id, fd, schema_len = -1, ret = 0;
445 if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
446 strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) &&
447 strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) &&
448 strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
452 ksft_print_msg("Skipping empty schemata update\n");
457 if (get_resource_id(cpu_no, &resource_id) < 0) {
458 sprintf(reason, "Failed to get resource id");
464 if (strlen(ctrlgrp) != 0)
465 sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
467 sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
469 if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
470 !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
471 schema_len = snprintf(schema, sizeof(schema), "%s%d%c%s\n",
472 "L3:", resource_id, '=', schemata);
473 if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
474 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)))
475 schema_len = snprintf(schema, sizeof(schema), "%s%d%c%s\n",
476 "MB:", resource_id, '=', schemata);
477 if (schema_len < 0 || schema_len >= sizeof(schema)) {
478 snprintf(reason, sizeof(reason),
479 "snprintf() failed with return value : %d", schema_len);
484 fd = open(controlgroup, O_WRONLY);
486 snprintf(reason, sizeof(reason),
487 "open() failed : %s", strerror(errno));
490 goto err_schema_not_empty;
492 if (write(fd, schema, schema_len) < 0) {
493 snprintf(reason, sizeof(reason),
494 "write() failed : %s", strerror(errno));
498 goto err_schema_not_empty;
502 err_schema_not_empty:
503 schema[schema_len - 1] = 0;
505 ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
506 schema, ret ? " # " : "",
512 bool check_resctrlfs_support(void)
514 FILE *inf = fopen("/proc/filesystems", "r");
522 res = fgrep(inf, "nodev\tresctrl\n");
531 ksft_print_msg("%s Check kernel supports resctrl filesystem\n",
532 ret ? "Pass:" : "Fail:");
537 dp = opendir(RESCTRL_PATH);
538 ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n",
539 dp ? "Pass:" : "Fail:", RESCTRL_PATH);
543 ksft_print_msg("resctrl filesystem %s mounted\n",
544 find_resctrl_mount(NULL) ? "not" : "is");
549 char *fgrep(FILE *inf, const char *str)
552 int slen = strlen(str);
555 if (!fgets(line, 256, inf))
557 if (strncmp(line, str, slen))
567 * validate_resctrl_feature_request - Check if requested feature is valid.
568 * @resource: Required resource (e.g., MB, L3, L2, L3_MON, etc.)
569 * @feature: Required monitor feature (in mon_features file). Can only be
570 * set for L3_MON. Must be NULL for all other resources.
572 * Return: True if the resource/feature is supported, else false. False is
573 * also returned if resctrl FS is not mounted.
575 bool validate_resctrl_feature_request(const char *resource, const char *feature)
577 char res_path[PATH_MAX];
586 ret = find_resctrl_mount(NULL);
590 snprintf(res_path, sizeof(res_path), "%s/%s", INFO_PATH, resource);
592 if (stat(res_path, &statbuf))
598 snprintf(res_path, sizeof(res_path), "%s/%s/mon_features", INFO_PATH, resource);
599 inf = fopen(res_path, "r");
603 res = fgrep(inf, feature);
610 int filter_dmesg(void)
627 dup2(pipefds[1], STDOUT_FILENO);
628 execlp("dmesg", "dmesg", NULL);
629 perror("executing dmesg");
633 fp = fdopen(pipefds[0], "r");
635 perror("fdopen(pipe)");
641 while (fgets(line, 1024, fp)) {
642 if (strstr(line, "intel_rdt:"))
643 ksft_print_msg("dmesg: %s", line);
644 if (strstr(line, "resctrl:"))
645 ksft_print_msg("dmesg: %s", line);
648 waitpid(pid, NULL, 0);
653 int validate_bw_report_request(char *bw_report)
655 if (strcmp(bw_report, "reads") == 0)
657 if (strcmp(bw_report, "writes") == 0)
659 if (strcmp(bw_report, "nt-writes") == 0) {
660 strcpy(bw_report, "writes");
663 if (strcmp(bw_report, "total") == 0)
666 fprintf(stderr, "Requested iMC B/W report type unavailable\n");
671 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
672 int group_fd, unsigned long flags)
676 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
681 unsigned int count_bits(unsigned long n)
683 unsigned int count = 0;