1 // SPDX-License-Identifier: GPL-2.0
3 * Basic resctrl file system operations
5 * Copyright (C) 2018 Intel Corporation
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 * remount_resctrlfs - Remount resctrl FS at /sys/fs/resctrl
52 * @mum_resctrlfs: Should the resctrl FS be remounted?
54 * If not mounted, mount it.
55 * If mounted and mum_resctrlfs then remount resctrl FS.
56 * If mounted and !mum_resctrlfs then noop
58 * Return: 0 on success, non-zero on failure
60 int remount_resctrlfs(bool mum_resctrlfs)
65 ret = find_resctrl_mount(mountpoint);
67 strcpy(mountpoint, RESCTRL_PATH);
69 if (!ret && mum_resctrlfs && umount(mountpoint))
70 ksft_print_msg("Fail: unmounting \"%s\"\n", mountpoint);
72 if (!ret && !mum_resctrlfs)
75 ksft_print_msg("Mounting resctrl to \"%s\"\n", RESCTRL_PATH);
76 ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
83 int umount_resctrlfs(void)
85 if (find_resctrl_mount(NULL))
88 if (umount(RESCTRL_PATH)) {
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 * run_benchmark - Run a specified benchmark or fill_buf (default benchmark)
299 * in specified signal. Direct benchmark stdio to /dev/null.
300 * @signum: signal number
302 * @ucontext: user context in signal handling
306 void run_benchmark(int signum, siginfo_t *info, void *ucontext)
308 int operation, ret, malloc_and_init_memory, memflush;
309 unsigned long span, buffer_span;
310 char **benchmark_cmd;
311 char resctrl_val[64];
314 benchmark_cmd = info->si_ptr;
317 * Direct stdio of child to /dev/null, so that only parent writes to
320 fp = freopen("/dev/null", "w", stdout);
322 PARENT_EXIT("Unable to direct benchmark status to /dev/null");
324 if (strcmp(benchmark_cmd[0], "fill_buf") == 0) {
325 /* Execute default fill_buf benchmark */
326 span = strtoul(benchmark_cmd[1], NULL, 10);
327 malloc_and_init_memory = atoi(benchmark_cmd[2]);
328 memflush = atoi(benchmark_cmd[3]);
329 operation = atoi(benchmark_cmd[4]);
330 sprintf(resctrl_val, "%s", benchmark_cmd[5]);
332 if (strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
333 buffer_span = span * MB;
337 if (run_fill_buf(buffer_span, malloc_and_init_memory, memflush,
338 operation, resctrl_val))
339 fprintf(stderr, "Error in running fill buffer\n");
341 /* Execute specified benchmark */
342 ret = execvp(benchmark_cmd[0], benchmark_cmd);
348 PARENT_EXIT("Unable to run specified benchmark");
352 * create_grp - Create a group only if one doesn't exist
353 * @grp_name: Name of the group
354 * @grp: Full path and name of the group
355 * @parent_grp: Full path and name of the parent group
357 * Return: 0 on success, non-zero on failure
359 static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
366 * At this point, we are guaranteed to have resctrl FS mounted and if
367 * length of grp_name == 0, it means, user wants to use root con_mon
370 if (strlen(grp_name) == 0)
373 /* Check if requested grp exists or not */
374 dp = opendir(parent_grp);
376 while ((ep = readdir(dp)) != NULL) {
377 if (strcmp(ep->d_name, grp_name) == 0)
382 perror("Unable to open resctrl for group");
387 /* Requested grp doesn't exist, hence create it */
388 if (found_grp == 0) {
389 if (mkdir(grp, 0) == -1) {
390 perror("Unable to create group");
399 static int write_pid_to_tasks(char *tasks, pid_t pid)
403 fp = fopen(tasks, "w");
405 perror("Failed to open tasks file");
409 if (fprintf(fp, "%d\n", pid) < 0) {
410 perror("Failed to wr pid to tasks file");
421 * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
422 * @bm_pid: PID that should be written
423 * @ctrlgrp: Name of the control monitor group (con_mon grp)
424 * @mongrp: Name of the monitor group (mon grp)
425 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
427 * If a con_mon grp is requested, create it and write pid to it, otherwise
428 * write pid to root con_mon grp.
429 * If a mon grp is requested, create it and write pid to it, otherwise
430 * pid is not written, this means that pid is in con_mon grp and hence
431 * should consult con_mon grp's mon_data directory for results.
433 * Return: 0 on success, non-zero on failure
435 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
438 char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
443 sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
445 sprintf(controlgroup, "%s", RESCTRL_PATH);
447 /* Create control and monitoring group and write pid into it */
448 ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
451 sprintf(tasks, "%s/tasks", controlgroup);
452 ret = write_pid_to_tasks(tasks, bm_pid);
456 /* Create mon grp and write pid into it for "mbm" and "cmt" test */
457 if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)) ||
458 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
459 if (strlen(mongrp)) {
460 sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
461 sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
462 ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
466 sprintf(tasks, "%s/mon_groups/%s/tasks",
467 controlgroup, mongrp);
468 ret = write_pid_to_tasks(tasks, bm_pid);
475 ksft_print_msg("Writing benchmark parameters to resctrl FS\n");
477 perror("# writing to resctrlfs");
483 * write_schemata - Update schemata of a con_mon grp
484 * @ctrlgrp: Name of the con_mon grp
485 * @schemata: Schemata that should be updated to
486 * @cpu_no: CPU number that the benchmark PID is binded to
487 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
489 * Update schemata of a con_mon grp *only* if requested resctrl feature is
492 * Return: 0 on success, non-zero on failure
494 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
496 char controlgroup[1024], schema[1024], reason[64];
497 int resource_id, ret = 0;
500 if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
501 strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) &&
502 strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) &&
503 strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
507 ksft_print_msg("Skipping empty schemata update\n");
512 if (get_resource_id(cpu_no, &resource_id) < 0) {
513 sprintf(reason, "Failed to get resource id");
519 if (strlen(ctrlgrp) != 0)
520 sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
522 sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
524 if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
525 !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
526 sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
527 if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
528 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)))
529 sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
531 fp = fopen(controlgroup, "w");
533 sprintf(reason, "Failed to open control group");
539 if (fprintf(fp, "%s\n", schema) < 0) {
540 sprintf(reason, "Failed to write schemata in control group");
549 ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
550 schema, ret ? " # " : "",
556 bool check_resctrlfs_support(void)
558 FILE *inf = fopen("/proc/filesystems", "r");
566 res = fgrep(inf, "nodev\tresctrl\n");
575 ksft_print_msg("%s Check kernel supports resctrl filesystem\n",
576 ret ? "Pass:" : "Fail:");
581 dp = opendir(RESCTRL_PATH);
582 ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n",
583 dp ? "Pass:" : "Fail:", RESCTRL_PATH);
587 ksft_print_msg("resctrl filesystem %s mounted\n",
588 find_resctrl_mount(NULL) ? "not" : "is");
593 char *fgrep(FILE *inf, const char *str)
596 int slen = strlen(str);
599 if (!fgets(line, 256, inf))
601 if (strncmp(line, str, slen))
611 * validate_resctrl_feature_request - Check if requested feature is valid.
612 * @resctrl_val: Requested feature
614 * Return: True if the feature is supported, else false
616 bool validate_resctrl_feature_request(const char *resctrl_val)
626 if (remount_resctrlfs(false))
629 if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) {
630 if (!stat(L3_PATH, &statbuf))
632 } else if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
633 if (!stat(MB_PATH, &statbuf))
635 } else if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
636 !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) {
637 if (!stat(L3_MON_PATH, &statbuf)) {
638 inf = fopen(L3_MON_FEATURES_PATH, "r");
642 if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) {
643 res = fgrep(inf, "llc_occupancy");
650 if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
651 res = fgrep(inf, "mbm_total_bytes");
654 res = fgrep(inf, "mbm_local_bytes");
668 int filter_dmesg(void)
685 dup2(pipefds[1], STDOUT_FILENO);
686 execlp("dmesg", "dmesg", NULL);
687 perror("executing dmesg");
691 fp = fdopen(pipefds[0], "r");
693 perror("fdopen(pipe)");
699 while (fgets(line, 1024, fp)) {
700 if (strstr(line, "intel_rdt:"))
701 ksft_print_msg("dmesg: %s", line);
702 if (strstr(line, "resctrl:"))
703 ksft_print_msg("dmesg: %s", line);
706 waitpid(pid, NULL, 0);
711 int validate_bw_report_request(char *bw_report)
713 if (strcmp(bw_report, "reads") == 0)
715 if (strcmp(bw_report, "writes") == 0)
717 if (strcmp(bw_report, "nt-writes") == 0) {
718 strcpy(bw_report, "writes");
721 if (strcmp(bw_report, "total") == 0)
724 fprintf(stderr, "Requested iMC B/W report type unavailable\n");
729 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
730 int group_fd, unsigned long flags)
734 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
739 unsigned int count_bits(unsigned long n)
741 unsigned int count = 0;