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 ksft_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, < 0 on error.
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 ksft_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 ksft_perror("Failed to open physical_package_id");
122 if (fscanf(fp, "%d", resource_id) <= 0) {
123 ksft_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, const 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 ksft_print_msg("Invalid cache level\n");
156 sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
158 fp = fopen(cache_path, "r");
160 ksft_perror("Failed to open cache size");
164 if (fscanf(fp, "%s", cache_str) <= 0) {
165 ksft_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_bit_mask - Get bit mask from given file
200 * @filename: File containing the mask
201 * @mask: The bit mask returned as unsigned long
203 * Return: = 0 on success, < 0 on failure.
205 static int get_bit_mask(const char *filename, unsigned long *mask)
209 if (!filename || !mask)
212 fp = fopen(filename, "r");
214 ksft_print_msg("Failed to open bit mask file '%s': %s\n",
215 filename, strerror(errno));
219 if (fscanf(fp, "%lx", mask) <= 0) {
220 ksft_print_msg("Could not read bit mask file '%s': %s\n",
221 filename, strerror(errno));
232 * get_full_cbm - Get full Cache Bit Mask (CBM)
233 * @cache_type: Cache type as "L2" or "L3"
234 * @mask: Full cache bit mask representing the maximal portion of cache
235 * available for allocation, returned as unsigned long.
237 * Return: = 0 on success, < 0 on failure.
239 int get_full_cbm(const char *cache_type, unsigned long *mask)
241 char cbm_path[PATH_MAX];
247 snprintf(cbm_path, sizeof(cbm_path), "%s/%s/cbm_mask",
248 INFO_PATH, cache_type);
250 ret = get_bit_mask(cbm_path, mask);
258 * get_core_sibling - Get sibling core id from the same socket for given CPU
259 * @cpu_no: CPU number
261 * Return: > 0 on success, < 0 on failure.
263 int get_core_sibling(int cpu_no)
265 char core_siblings_path[1024], cpu_list_str[64];
266 int sibling_cpu_no = -1;
269 sprintf(core_siblings_path, "%s%d/topology/core_siblings_list",
270 CORE_SIBLINGS_PATH, cpu_no);
272 fp = fopen(core_siblings_path, "r");
274 ksft_perror("Failed to open core siblings path");
278 if (fscanf(fp, "%s", cpu_list_str) <= 0) {
279 ksft_perror("Could not get core_siblings list");
286 char *token = strtok(cpu_list_str, "-,");
289 sibling_cpu_no = atoi(token);
290 /* Skipping core 0 as we don't want to run test on core 0 */
291 if (sibling_cpu_no != 0 && sibling_cpu_no != cpu_no)
293 token = strtok(NULL, "-,");
296 return sibling_cpu_no;
300 * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
301 * @bm_pid: PID that should be binded
302 * @cpu_no: CPU number at which the PID would be binded
304 * Return: 0 on success, < 0 on error.
306 int taskset_benchmark(pid_t bm_pid, int cpu_no)
311 CPU_SET(cpu_no, &my_set);
313 if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
314 ksft_perror("Unable to taskset benchmark");
323 * create_grp - Create a group only if one doesn't exist
324 * @grp_name: Name of the group
325 * @grp: Full path and name of the group
326 * @parent_grp: Full path and name of the parent group
328 * Return: 0 on success, < 0 on error.
330 static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
337 * At this point, we are guaranteed to have resctrl FS mounted and if
338 * length of grp_name == 0, it means, user wants to use root con_mon
341 if (strlen(grp_name) == 0)
344 /* Check if requested grp exists or not */
345 dp = opendir(parent_grp);
347 while ((ep = readdir(dp)) != NULL) {
348 if (strcmp(ep->d_name, grp_name) == 0)
353 ksft_perror("Unable to open resctrl for group");
358 /* Requested grp doesn't exist, hence create it */
359 if (found_grp == 0) {
360 if (mkdir(grp, 0) == -1) {
361 ksft_perror("Unable to create group");
370 static int write_pid_to_tasks(char *tasks, pid_t pid)
374 fp = fopen(tasks, "w");
376 ksft_perror("Failed to open tasks file");
380 if (fprintf(fp, "%d\n", pid) < 0) {
381 ksft_print_msg("Failed to write pid to tasks file\n");
392 * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
393 * @bm_pid: PID that should be written
394 * @ctrlgrp: Name of the control monitor group (con_mon grp)
395 * @mongrp: Name of the monitor group (mon grp)
396 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
398 * If a con_mon grp is requested, create it and write pid to it, otherwise
399 * write pid to root con_mon grp.
400 * If a mon grp is requested, create it and write pid to it, otherwise
401 * pid is not written, this means that pid is in con_mon grp and hence
402 * should consult con_mon grp's mon_data directory for results.
404 * Return: 0 on success, < 0 on error.
406 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
409 char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
414 sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
416 sprintf(controlgroup, "%s", RESCTRL_PATH);
418 /* Create control and monitoring group and write pid into it */
419 ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
422 sprintf(tasks, "%s/tasks", controlgroup);
423 ret = write_pid_to_tasks(tasks, bm_pid);
427 /* Create mon grp and write pid into it for "mbm" and "cmt" test */
428 if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)) ||
429 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
430 if (strlen(mongrp)) {
431 sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
432 sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
433 ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
437 sprintf(tasks, "%s/mon_groups/%s/tasks",
438 controlgroup, mongrp);
439 ret = write_pid_to_tasks(tasks, bm_pid);
446 ksft_print_msg("Writing benchmark parameters to resctrl FS\n");
448 ksft_print_msg("Failed writing to resctrlfs\n");
454 * write_schemata - Update schemata of a con_mon grp
455 * @ctrlgrp: Name of the con_mon grp
456 * @schemata: Schemata that should be updated to
457 * @cpu_no: CPU number that the benchmark PID is binded to
458 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
460 * Update schemata of a con_mon grp *only* if requested resctrl feature is
463 * Return: 0 on success, < 0 on error.
465 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
467 char controlgroup[1024], reason[128], schema[1024] = {};
468 int resource_id, fd, schema_len = -1, ret = 0;
470 if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
471 strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) &&
472 strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) &&
473 strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
477 ksft_print_msg("Skipping empty schemata update\n");
482 if (get_resource_id(cpu_no, &resource_id) < 0) {
483 sprintf(reason, "Failed to get resource id");
489 if (strlen(ctrlgrp) != 0)
490 sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
492 sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
494 if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
495 !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
496 schema_len = snprintf(schema, sizeof(schema), "%s%d%c%s\n",
497 "L3:", resource_id, '=', schemata);
498 if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
499 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)))
500 schema_len = snprintf(schema, sizeof(schema), "%s%d%c%s\n",
501 "MB:", resource_id, '=', schemata);
502 if (schema_len < 0 || schema_len >= sizeof(schema)) {
503 snprintf(reason, sizeof(reason),
504 "snprintf() failed with return value : %d", schema_len);
509 fd = open(controlgroup, O_WRONLY);
511 snprintf(reason, sizeof(reason),
512 "open() failed : %s", strerror(errno));
515 goto err_schema_not_empty;
517 if (write(fd, schema, schema_len) < 0) {
518 snprintf(reason, sizeof(reason),
519 "write() failed : %s", strerror(errno));
523 goto err_schema_not_empty;
527 err_schema_not_empty:
528 schema[schema_len - 1] = 0;
530 ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
531 schema, ret ? " # " : "",
537 bool check_resctrlfs_support(void)
539 FILE *inf = fopen("/proc/filesystems", "r");
547 res = fgrep(inf, "nodev\tresctrl\n");
556 ksft_print_msg("%s Check kernel supports resctrl filesystem\n",
557 ret ? "Pass:" : "Fail:");
562 dp = opendir(RESCTRL_PATH);
563 ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n",
564 dp ? "Pass:" : "Fail:", RESCTRL_PATH);
568 ksft_print_msg("resctrl filesystem %s mounted\n",
569 find_resctrl_mount(NULL) ? "not" : "is");
574 char *fgrep(FILE *inf, const char *str)
577 int slen = strlen(str);
580 if (!fgets(line, 256, inf))
582 if (strncmp(line, str, slen))
592 * validate_resctrl_feature_request - Check if requested feature is valid.
593 * @resource: Required resource (e.g., MB, L3, L2, L3_MON, etc.)
594 * @feature: Required monitor feature (in mon_features file). Can only be
595 * set for L3_MON. Must be NULL for all other resources.
597 * Return: True if the resource/feature is supported, else false. False is
598 * also returned if resctrl FS is not mounted.
600 bool validate_resctrl_feature_request(const char *resource, const char *feature)
602 char res_path[PATH_MAX];
611 ret = find_resctrl_mount(NULL);
615 snprintf(res_path, sizeof(res_path), "%s/%s", INFO_PATH, resource);
617 if (stat(res_path, &statbuf))
623 snprintf(res_path, sizeof(res_path), "%s/%s/mon_features", INFO_PATH, resource);
624 inf = fopen(res_path, "r");
628 res = fgrep(inf, feature);
635 int filter_dmesg(void)
652 dup2(pipefds[1], STDOUT_FILENO);
653 execlp("dmesg", "dmesg", NULL);
654 ksft_perror("Executing dmesg");
658 fp = fdopen(pipefds[0], "r");
660 ksft_perror("fdopen(pipe)");
666 while (fgets(line, 1024, fp)) {
667 if (strstr(line, "intel_rdt:"))
668 ksft_print_msg("dmesg: %s", line);
669 if (strstr(line, "resctrl:"))
670 ksft_print_msg("dmesg: %s", line);
673 waitpid(pid, NULL, 0);
678 int validate_bw_report_request(char *bw_report)
680 if (strcmp(bw_report, "reads") == 0)
682 if (strcmp(bw_report, "writes") == 0)
684 if (strcmp(bw_report, "nt-writes") == 0) {
685 strcpy(bw_report, "writes");
688 if (strcmp(bw_report, "total") == 0)
691 fprintf(stderr, "Requested iMC B/W report type unavailable\n");
696 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
697 int group_fd, unsigned long flags)
701 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
706 unsigned int count_bits(unsigned long n)
708 unsigned int count = 0;