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_cache_level - Convert cache level from string to integer
99 * @cache_type: Cache level as string
101 * Return: cache level as integer or -1 if @cache_type is invalid.
103 static int get_cache_level(const char *cache_type)
105 if (!strcmp(cache_type, "L3"))
107 if (!strcmp(cache_type, "L2"))
110 ksft_print_msg("Invalid cache level\n");
115 * get_domain_id - Get resctrl domain ID for a specified CPU
116 * @cpu_no: CPU number
117 * @domain_id: domain ID (cache ID; for MB, L3 cache ID)
119 * Return: >= 0 on success, < 0 on failure.
121 int get_domain_id(int cpu_no, int *domain_id)
123 char phys_pkg_path[1024];
126 if (get_vendor() == ARCH_AMD)
127 sprintf(phys_pkg_path, "%s%d/cache/index3/id",
128 PHYS_ID_PATH, cpu_no);
130 sprintf(phys_pkg_path, "%s%d/topology/physical_package_id",
131 PHYS_ID_PATH, cpu_no);
133 fp = fopen(phys_pkg_path, "r");
135 ksft_perror("Failed to open physical_package_id");
139 if (fscanf(fp, "%d", domain_id) <= 0) {
140 ksft_perror("Could not get domain ID");
151 * get_cache_size - Get cache size for a specified CPU
152 * @cpu_no: CPU number
153 * @cache_type: Cache level L2/L3
154 * @cache_size: pointer to cache_size
156 * Return: = 0 on success, < 0 on failure.
158 int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size)
160 char cache_path[1024], cache_str[64];
161 int length, i, cache_num;
164 cache_num = get_cache_level(cache_type);
168 sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
170 fp = fopen(cache_path, "r");
172 ksft_perror("Failed to open cache size");
176 if (fscanf(fp, "%s", cache_str) <= 0) {
177 ksft_perror("Could not get cache_size");
184 length = (int)strlen(cache_str);
188 for (i = 0; i < length; i++) {
189 if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
191 *cache_size = *cache_size * 10 + (cache_str[i] - '0');
193 else if (cache_str[i] == 'K')
195 *cache_size = *cache_size * 1024;
197 else if (cache_str[i] == 'M')
199 *cache_size = *cache_size * 1024 * 1024;
208 #define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu"
211 * get_bit_mask - Get bit mask from given file
212 * @filename: File containing the mask
213 * @mask: The bit mask returned as unsigned long
215 * Return: = 0 on success, < 0 on failure.
217 static int get_bit_mask(const char *filename, unsigned long *mask)
221 if (!filename || !mask)
224 fp = fopen(filename, "r");
226 ksft_print_msg("Failed to open bit mask file '%s': %s\n",
227 filename, strerror(errno));
231 if (fscanf(fp, "%lx", mask) <= 0) {
232 ksft_print_msg("Could not read bit mask file '%s': %s\n",
233 filename, strerror(errno));
244 * create_bit_mask- Create bit mask from start, len pair
245 * @start: LSB of the mask
246 * @len Number of bits in the mask
248 unsigned long create_bit_mask(unsigned int start, unsigned int len)
250 return ((1UL << len) - 1UL) << start;
254 * count_contiguous_bits - Returns the longest train of bits in a bit mask
256 * @start The location of the least-significant bit of the longest train
258 * Return: The length of the contiguous bits in the longest train of bits
260 unsigned int count_contiguous_bits(unsigned long val, unsigned int *start)
262 unsigned long last_val;
263 unsigned int count = 0;
273 *start = ffsl(last_val) - 1;
282 * get_full_cbm - Get full Cache Bit Mask (CBM)
283 * @cache_type: Cache type as "L2" or "L3"
284 * @mask: Full cache bit mask representing the maximal portion of cache
285 * available for allocation, returned as unsigned long.
287 * Return: = 0 on success, < 0 on failure.
289 int get_full_cbm(const char *cache_type, unsigned long *mask)
291 char cbm_path[PATH_MAX];
297 snprintf(cbm_path, sizeof(cbm_path), "%s/%s/cbm_mask",
298 INFO_PATH, cache_type);
300 ret = get_bit_mask(cbm_path, mask);
308 * get_shareable_mask - Get shareable mask from shareable_bits
309 * @cache_type: Cache type as "L2" or "L3"
310 * @shareable_mask: Shareable mask returned as unsigned long
312 * Return: = 0 on success, < 0 on failure.
314 static int get_shareable_mask(const char *cache_type, unsigned long *shareable_mask)
316 char mask_path[PATH_MAX];
321 snprintf(mask_path, sizeof(mask_path), "%s/%s/shareable_bits",
322 INFO_PATH, cache_type);
324 return get_bit_mask(mask_path, shareable_mask);
328 * get_mask_no_shareable - Get Cache Bit Mask (CBM) without shareable bits
329 * @cache_type: Cache type as "L2" or "L3"
330 * @mask: The largest exclusive portion of the cache out of the
331 * full CBM, returned as unsigned long
333 * Parts of a cache may be shared with other devices such as GPU. This function
334 * calculates the largest exclusive portion of the cache where no other devices
335 * besides CPU have access to the cache portion.
337 * Return: = 0 on success, < 0 on failure.
339 int get_mask_no_shareable(const char *cache_type, unsigned long *mask)
341 unsigned long full_mask, shareable_mask;
342 unsigned int start, len;
344 if (get_full_cbm(cache_type, &full_mask) < 0)
346 if (get_shareable_mask(cache_type, &shareable_mask) < 0)
349 len = count_contiguous_bits(full_mask & ~shareable_mask, &start);
353 *mask = create_bit_mask(start, len);
359 * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
360 * @bm_pid: PID that should be binded
361 * @cpu_no: CPU number at which the PID would be binded
362 * @old_affinity: When not NULL, set to old CPU affinity
364 * Return: 0 on success, < 0 on error.
366 int taskset_benchmark(pid_t bm_pid, int cpu_no, cpu_set_t *old_affinity)
371 CPU_ZERO(old_affinity);
372 if (sched_getaffinity(bm_pid, sizeof(*old_affinity),
374 ksft_perror("Unable to read CPU affinity");
380 CPU_SET(cpu_no, &my_set);
382 if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
383 ksft_perror("Unable to taskset benchmark");
392 * taskset_restore - Taskset PID to the earlier CPU affinity
393 * @bm_pid: PID that should be reset
394 * @old_affinity: The old CPU affinity to restore
396 * Return: 0 on success, < 0 on error.
398 int taskset_restore(pid_t bm_pid, cpu_set_t *old_affinity)
400 if (sched_setaffinity(bm_pid, sizeof(*old_affinity), old_affinity)) {
401 ksft_perror("Unable to restore CPU affinity");
409 * create_grp - Create a group only if one doesn't exist
410 * @grp_name: Name of the group
411 * @grp: Full path and name of the group
412 * @parent_grp: Full path and name of the parent group
414 * Return: 0 on success, < 0 on error.
416 static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
423 * At this point, we are guaranteed to have resctrl FS mounted and if
424 * length of grp_name == 0, it means, user wants to use root con_mon
427 if (strlen(grp_name) == 0)
430 /* Check if requested grp exists or not */
431 dp = opendir(parent_grp);
433 while ((ep = readdir(dp)) != NULL) {
434 if (strcmp(ep->d_name, grp_name) == 0)
439 ksft_perror("Unable to open resctrl for group");
444 /* Requested grp doesn't exist, hence create it */
445 if (found_grp == 0) {
446 if (mkdir(grp, 0) == -1) {
447 ksft_perror("Unable to create group");
456 static int write_pid_to_tasks(char *tasks, pid_t pid)
460 fp = fopen(tasks, "w");
462 ksft_perror("Failed to open tasks file");
466 if (fprintf(fp, "%d\n", pid) < 0) {
467 ksft_print_msg("Failed to write pid to tasks file\n");
478 * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
479 * @bm_pid: PID that should be written
480 * @ctrlgrp: Name of the control monitor group (con_mon grp)
481 * @mongrp: Name of the monitor group (mon grp)
482 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
484 * If a con_mon grp is requested, create it and write pid to it, otherwise
485 * write pid to root con_mon grp.
486 * If a mon grp is requested, create it and write pid to it, otherwise
487 * pid is not written, this means that pid is in con_mon grp and hence
488 * should consult con_mon grp's mon_data directory for results.
490 * Return: 0 on success, < 0 on error.
492 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
495 char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
500 sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
502 sprintf(controlgroup, "%s", RESCTRL_PATH);
504 /* Create control and monitoring group and write pid into it */
505 ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
508 sprintf(tasks, "%s/tasks", controlgroup);
509 ret = write_pid_to_tasks(tasks, bm_pid);
513 /* Create mon grp and write pid into it for "mbm" and "cmt" test */
514 if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)) ||
515 !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
516 if (strlen(mongrp)) {
517 sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
518 sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
519 ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
523 sprintf(tasks, "%s/mon_groups/%s/tasks",
524 controlgroup, mongrp);
525 ret = write_pid_to_tasks(tasks, bm_pid);
532 ksft_print_msg("Writing benchmark parameters to resctrl FS\n");
534 ksft_print_msg("Failed writing to resctrlfs\n");
540 * write_schemata - Update schemata of a con_mon grp
541 * @ctrlgrp: Name of the con_mon grp
542 * @schemata: Schemata that should be updated to
543 * @cpu_no: CPU number that the benchmark PID is binded to
544 * @resource: Resctrl resource (Eg: MB, L3, L2, etc.)
546 * Update schemata of a con_mon grp *only* if requested resctrl resource is
549 * Return: 0 on success, < 0 on error.
551 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, const char *resource)
553 char controlgroup[1024], reason[128], schema[1024] = {};
554 int domain_id, fd, schema_len, ret = 0;
557 ksft_print_msg("Skipping empty schemata update\n");
562 if (get_domain_id(cpu_no, &domain_id) < 0) {
563 sprintf(reason, "Failed to get domain ID");
569 if (strlen(ctrlgrp) != 0)
570 sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
572 sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
574 schema_len = snprintf(schema, sizeof(schema), "%s:%d=%s\n",
575 resource, domain_id, schemata);
576 if (schema_len < 0 || schema_len >= sizeof(schema)) {
577 snprintf(reason, sizeof(reason),
578 "snprintf() failed with return value : %d", schema_len);
583 fd = open(controlgroup, O_WRONLY);
585 snprintf(reason, sizeof(reason),
586 "open() failed : %s", strerror(errno));
589 goto err_schema_not_empty;
591 if (write(fd, schema, schema_len) < 0) {
592 snprintf(reason, sizeof(reason),
593 "write() failed : %s", strerror(errno));
597 goto err_schema_not_empty;
601 err_schema_not_empty:
602 schema[schema_len - 1] = 0;
604 ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
605 schema, ret ? " # " : "",
611 bool check_resctrlfs_support(void)
613 FILE *inf = fopen("/proc/filesystems", "r");
621 res = fgrep(inf, "nodev\tresctrl\n");
630 ksft_print_msg("%s Check kernel supports resctrl filesystem\n",
631 ret ? "Pass:" : "Fail:");
636 dp = opendir(RESCTRL_PATH);
637 ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n",
638 dp ? "Pass:" : "Fail:", RESCTRL_PATH);
642 ksft_print_msg("resctrl filesystem %s mounted\n",
643 find_resctrl_mount(NULL) ? "not" : "is");
648 char *fgrep(FILE *inf, const char *str)
651 int slen = strlen(str);
654 if (!fgets(line, 256, inf))
656 if (strncmp(line, str, slen))
666 * validate_resctrl_feature_request - Check if requested feature is valid.
667 * @resource: Required resource (e.g., MB, L3, L2, L3_MON, etc.)
668 * @feature: Required monitor feature (in mon_features file). Can only be
669 * set for L3_MON. Must be NULL for all other resources.
671 * Return: True if the resource/feature is supported, else false. False is
672 * also returned if resctrl FS is not mounted.
674 bool validate_resctrl_feature_request(const char *resource, const char *feature)
676 char res_path[PATH_MAX];
685 ret = find_resctrl_mount(NULL);
689 snprintf(res_path, sizeof(res_path), "%s/%s", INFO_PATH, resource);
691 if (stat(res_path, &statbuf))
697 snprintf(res_path, sizeof(res_path), "%s/%s/mon_features", INFO_PATH, resource);
698 inf = fopen(res_path, "r");
702 res = fgrep(inf, feature);
709 bool test_resource_feature_check(const struct resctrl_test *test)
711 return validate_resctrl_feature_request(test->resource, NULL);
714 int filter_dmesg(void)
731 dup2(pipefds[1], STDOUT_FILENO);
732 execlp("dmesg", "dmesg", NULL);
733 ksft_perror("Executing dmesg");
737 fp = fdopen(pipefds[0], "r");
739 ksft_perror("fdopen(pipe)");
745 while (fgets(line, 1024, fp)) {
746 if (strstr(line, "intel_rdt:"))
747 ksft_print_msg("dmesg: %s", line);
748 if (strstr(line, "resctrl:"))
749 ksft_print_msg("dmesg: %s", line);
752 waitpid(pid, NULL, 0);
757 int validate_bw_report_request(char *bw_report)
759 if (strcmp(bw_report, "reads") == 0)
761 if (strcmp(bw_report, "writes") == 0)
763 if (strcmp(bw_report, "nt-writes") == 0) {
764 strcpy(bw_report, "writes");
767 if (strcmp(bw_report, "total") == 0)
770 fprintf(stderr, "Requested iMC B/W report type unavailable\n");
775 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
776 int group_fd, unsigned long flags)
780 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
785 unsigned int count_bits(unsigned long n)
787 unsigned int count = 0;