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");
114 static int get_resource_cache_level(const char *resource
)
116 /* "MB" use L3 (LLC) as resource */
117 if (!strcmp(resource
, "MB"))
119 return get_cache_level(resource
);
123 * get_domain_id - Get resctrl domain ID for a specified CPU
124 * @resource: resource name
125 * @cpu_no: CPU number
126 * @domain_id: domain ID (cache ID; for MB, L3 cache ID)
128 * Return: >= 0 on success, < 0 on failure.
130 int get_domain_id(const char *resource
, int cpu_no
, int *domain_id
)
132 char phys_pkg_path
[1024];
136 cache_num
= get_resource_cache_level(resource
);
140 sprintf(phys_pkg_path
, "%s%d/cache/index%d/id", PHYS_ID_PATH
, cpu_no
, cache_num
);
142 fp
= fopen(phys_pkg_path
, "r");
144 ksft_perror("Failed to open cache id file");
148 if (fscanf(fp
, "%d", domain_id
) <= 0) {
149 ksft_perror("Could not get domain ID");
160 * get_cache_size - Get cache size for a specified CPU
161 * @cpu_no: CPU number
162 * @cache_type: Cache level L2/L3
163 * @cache_size: pointer to cache_size
165 * Return: = 0 on success, < 0 on failure.
167 int get_cache_size(int cpu_no
, const char *cache_type
, unsigned long *cache_size
)
169 char cache_path
[1024], cache_str
[64];
170 int length
, i
, cache_num
;
173 cache_num
= get_cache_level(cache_type
);
177 sprintf(cache_path
, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
179 fp
= fopen(cache_path
, "r");
181 ksft_perror("Failed to open cache size");
185 if (fscanf(fp
, "%63s", cache_str
) <= 0) {
186 ksft_perror("Could not get cache_size");
193 length
= (int)strlen(cache_str
);
197 for (i
= 0; i
< length
; i
++) {
198 if ((cache_str
[i
] >= '0') && (cache_str
[i
] <= '9'))
200 *cache_size
= *cache_size
* 10 + (cache_str
[i
] - '0');
202 else if (cache_str
[i
] == 'K')
204 *cache_size
= *cache_size
* 1024;
206 else if (cache_str
[i
] == 'M')
208 *cache_size
= *cache_size
* 1024 * 1024;
217 #define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu"
220 * get_bit_mask - Get bit mask from given file
221 * @filename: File containing the mask
222 * @mask: The bit mask returned as unsigned long
224 * Return: = 0 on success, < 0 on failure.
226 static int get_bit_mask(const char *filename
, unsigned long *mask
)
230 if (!filename
|| !mask
)
233 fp
= fopen(filename
, "r");
235 ksft_print_msg("Failed to open bit mask file '%s': %s\n",
236 filename
, strerror(errno
));
240 if (fscanf(fp
, "%lx", mask
) <= 0) {
241 ksft_print_msg("Could not read bit mask file '%s': %s\n",
242 filename
, strerror(errno
));
253 * resource_info_unsigned_get - Read an unsigned value from
254 * /sys/fs/resctrl/info/@resource/@filename
255 * @resource: Resource name that matches directory name in
256 * /sys/fs/resctrl/info
257 * @filename: File in /sys/fs/resctrl/info/@resource
258 * @val: Contains read value on success.
260 * Return: = 0 on success, < 0 on failure. On success the read
261 * value is saved into @val.
263 int resource_info_unsigned_get(const char *resource
, const char *filename
,
266 char file_path
[PATH_MAX
];
269 snprintf(file_path
, sizeof(file_path
), "%s/%s/%s", INFO_PATH
, resource
,
272 fp
= fopen(file_path
, "r");
274 ksft_print_msg("Error opening %s: %m\n", file_path
);
278 if (fscanf(fp
, "%u", val
) <= 0) {
279 ksft_print_msg("Could not get contents of %s: %m\n", file_path
);
289 * create_bit_mask- Create bit mask from start, len pair
290 * @start: LSB of the mask
291 * @len Number of bits in the mask
293 unsigned long create_bit_mask(unsigned int start
, unsigned int len
)
295 return ((1UL << len
) - 1UL) << start
;
299 * count_contiguous_bits - Returns the longest train of bits in a bit mask
301 * @start The location of the least-significant bit of the longest train
303 * Return: The length of the contiguous bits in the longest train of bits
305 unsigned int count_contiguous_bits(unsigned long val
, unsigned int *start
)
307 unsigned long last_val
;
308 unsigned int count
= 0;
318 *start
= ffsl(last_val
) - 1;
327 * get_full_cbm - Get full Cache Bit Mask (CBM)
328 * @cache_type: Cache type as "L2" or "L3"
329 * @mask: Full cache bit mask representing the maximal portion of cache
330 * available for allocation, returned as unsigned long.
332 * Return: = 0 on success, < 0 on failure.
334 int get_full_cbm(const char *cache_type
, unsigned long *mask
)
336 char cbm_path
[PATH_MAX
];
342 snprintf(cbm_path
, sizeof(cbm_path
), "%s/%s/cbm_mask",
343 INFO_PATH
, cache_type
);
345 ret
= get_bit_mask(cbm_path
, mask
);
353 * get_shareable_mask - Get shareable mask from shareable_bits
354 * @cache_type: Cache type as "L2" or "L3"
355 * @shareable_mask: Shareable mask returned as unsigned long
357 * Return: = 0 on success, < 0 on failure.
359 static int get_shareable_mask(const char *cache_type
, unsigned long *shareable_mask
)
361 char mask_path
[PATH_MAX
];
366 snprintf(mask_path
, sizeof(mask_path
), "%s/%s/shareable_bits",
367 INFO_PATH
, cache_type
);
369 return get_bit_mask(mask_path
, shareable_mask
);
373 * get_mask_no_shareable - Get Cache Bit Mask (CBM) without shareable bits
374 * @cache_type: Cache type as "L2" or "L3"
375 * @mask: The largest exclusive portion of the cache out of the
376 * full CBM, returned as unsigned long
378 * Parts of a cache may be shared with other devices such as GPU. This function
379 * calculates the largest exclusive portion of the cache where no other devices
380 * besides CPU have access to the cache portion.
382 * Return: = 0 on success, < 0 on failure.
384 int get_mask_no_shareable(const char *cache_type
, unsigned long *mask
)
386 unsigned long full_mask
, shareable_mask
;
387 unsigned int start
, len
;
389 if (get_full_cbm(cache_type
, &full_mask
) < 0)
391 if (get_shareable_mask(cache_type
, &shareable_mask
) < 0)
394 len
= count_contiguous_bits(full_mask
& ~shareable_mask
, &start
);
398 *mask
= create_bit_mask(start
, len
);
404 * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
405 * @bm_pid: PID that should be binded
406 * @cpu_no: CPU number at which the PID would be binded
407 * @old_affinity: When not NULL, set to old CPU affinity
409 * Return: 0 on success, < 0 on error.
411 int taskset_benchmark(pid_t bm_pid
, int cpu_no
, cpu_set_t
*old_affinity
)
416 CPU_ZERO(old_affinity
);
417 if (sched_getaffinity(bm_pid
, sizeof(*old_affinity
),
419 ksft_perror("Unable to read CPU affinity");
425 CPU_SET(cpu_no
, &my_set
);
427 if (sched_setaffinity(bm_pid
, sizeof(cpu_set_t
), &my_set
)) {
428 ksft_perror("Unable to taskset benchmark");
437 * taskset_restore - Taskset PID to the earlier CPU affinity
438 * @bm_pid: PID that should be reset
439 * @old_affinity: The old CPU affinity to restore
441 * Return: 0 on success, < 0 on error.
443 int taskset_restore(pid_t bm_pid
, cpu_set_t
*old_affinity
)
445 if (sched_setaffinity(bm_pid
, sizeof(*old_affinity
), old_affinity
)) {
446 ksft_perror("Unable to restore CPU affinity");
454 * create_grp - Create a group only if one doesn't exist
455 * @grp_name: Name of the group
456 * @grp: Full path and name of the group
457 * @parent_grp: Full path and name of the parent group
459 * Creates a group @grp_name if it does not exist yet. If @grp_name is NULL,
460 * it is interpreted as the root group which always results in success.
462 * Return: 0 on success, < 0 on error.
464 static int create_grp(const char *grp_name
, char *grp
, const char *parent_grp
)
473 /* Check if requested grp exists or not */
474 dp
= opendir(parent_grp
);
476 while ((ep
= readdir(dp
)) != NULL
) {
477 if (strcmp(ep
->d_name
, grp_name
) == 0)
482 ksft_perror("Unable to open resctrl for group");
487 /* Requested grp doesn't exist, hence create it */
488 if (found_grp
== 0) {
489 if (mkdir(grp
, 0) == -1) {
490 ksft_perror("Unable to create group");
499 static int write_pid_to_tasks(char *tasks
, pid_t pid
)
503 fp
= fopen(tasks
, "w");
505 ksft_perror("Failed to open tasks file");
509 if (fprintf(fp
, "%d\n", (int)pid
) < 0) {
510 ksft_print_msg("Failed to write pid to tasks file\n");
521 * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
522 * @bm_pid: PID that should be written
523 * @ctrlgrp: Name of the control monitor group (con_mon grp)
524 * @mongrp: Name of the monitor group (mon grp)
526 * If a con_mon grp is requested, create it and write pid to it, otherwise
527 * write pid to root con_mon grp.
528 * If a mon grp is requested, create it and write pid to it, otherwise
529 * pid is not written, this means that pid is in con_mon grp and hence
530 * should consult con_mon grp's mon_data directory for results.
532 * Return: 0 on success, < 0 on error.
534 int write_bm_pid_to_resctrl(pid_t bm_pid
, const char *ctrlgrp
, const char *mongrp
)
536 char controlgroup
[128], monitorgroup
[512], monitorgroup_p
[256];
541 sprintf(controlgroup
, "%s/%s", RESCTRL_PATH
, ctrlgrp
);
543 sprintf(controlgroup
, "%s", RESCTRL_PATH
);
545 /* Create control and monitoring group and write pid into it */
546 ret
= create_grp(ctrlgrp
, controlgroup
, RESCTRL_PATH
);
549 sprintf(tasks
, "%s/tasks", controlgroup
);
550 ret
= write_pid_to_tasks(tasks
, bm_pid
);
554 /* Create monitor group and write pid into if it is used */
556 sprintf(monitorgroup_p
, "%s/mon_groups", controlgroup
);
557 sprintf(monitorgroup
, "%s/%s", monitorgroup_p
, mongrp
);
558 ret
= create_grp(mongrp
, monitorgroup
, monitorgroup_p
);
562 sprintf(tasks
, "%s/mon_groups/%s/tasks",
563 controlgroup
, mongrp
);
564 ret
= write_pid_to_tasks(tasks
, bm_pid
);
570 ksft_print_msg("Writing benchmark parameters to resctrl FS\n");
572 ksft_print_msg("Failed writing to resctrlfs\n");
578 * write_schemata - Update schemata of a con_mon grp
579 * @ctrlgrp: Name of the con_mon grp
580 * @schemata: Schemata that should be updated to
581 * @cpu_no: CPU number that the benchmark PID is binded to
582 * @resource: Resctrl resource (Eg: MB, L3, L2, etc.)
584 * Update schemata of a con_mon grp *only* if requested resctrl resource is
587 * Return: 0 on success, < 0 on error.
589 int write_schemata(const char *ctrlgrp
, char *schemata
, int cpu_no
,
590 const char *resource
)
592 char controlgroup
[1024], reason
[128], schema
[1024] = {};
593 int domain_id
, fd
, schema_len
, ret
= 0;
596 ksft_print_msg("Skipping empty schemata update\n");
601 if (get_domain_id(resource
, cpu_no
, &domain_id
) < 0) {
602 sprintf(reason
, "Failed to get domain ID");
609 sprintf(controlgroup
, "%s/%s/schemata", RESCTRL_PATH
, ctrlgrp
);
611 sprintf(controlgroup
, "%s/schemata", RESCTRL_PATH
);
613 schema_len
= snprintf(schema
, sizeof(schema
), "%s:%d=%s\n",
614 resource
, domain_id
, schemata
);
615 if (schema_len
< 0 || schema_len
>= sizeof(schema
)) {
616 snprintf(reason
, sizeof(reason
),
617 "snprintf() failed with return value : %d", schema_len
);
622 fd
= open(controlgroup
, O_WRONLY
);
624 snprintf(reason
, sizeof(reason
),
625 "open() failed : %s", strerror(errno
));
628 goto err_schema_not_empty
;
630 if (write(fd
, schema
, schema_len
) < 0) {
631 snprintf(reason
, sizeof(reason
),
632 "write() failed : %s", strerror(errno
));
636 goto err_schema_not_empty
;
640 err_schema_not_empty
:
641 schema
[schema_len
- 1] = 0;
643 ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
644 schema
, ret
? " # " : "",
650 bool check_resctrlfs_support(void)
652 FILE *inf
= fopen("/proc/filesystems", "r");
660 res
= fgrep(inf
, "nodev\tresctrl\n");
669 ksft_print_msg("%s Check kernel supports resctrl filesystem\n",
670 ret
? "Pass:" : "Fail:");
675 dp
= opendir(RESCTRL_PATH
);
676 ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n",
677 dp
? "Pass:" : "Fail:", RESCTRL_PATH
);
681 ksft_print_msg("resctrl filesystem %s mounted\n",
682 find_resctrl_mount(NULL
) ? "not" : "is");
687 char *fgrep(FILE *inf
, const char *str
)
690 int slen
= strlen(str
);
693 if (!fgets(line
, 256, inf
))
695 if (strncmp(line
, str
, slen
))
705 * resctrl_resource_exists - Check if a resource is supported.
706 * @resource: Resctrl resource (e.g., MB, L3, L2, L3_MON, etc.)
708 * Return: True if the resource is supported, else false. False is
709 * also returned if resctrl FS is not mounted.
711 bool resctrl_resource_exists(const char *resource
)
713 char res_path
[PATH_MAX
];
720 ret
= find_resctrl_mount(NULL
);
724 snprintf(res_path
, sizeof(res_path
), "%s/%s", INFO_PATH
, resource
);
726 if (stat(res_path
, &statbuf
))
733 * resctrl_mon_feature_exists - Check if requested monitoring feature is valid.
734 * @resource: Resource that uses the mon_features file. Currently only L3_MON
736 * @feature: Required monitor feature (in mon_features file).
738 * Return: True if the feature is supported, else false.
740 bool resctrl_mon_feature_exists(const char *resource
, const char *feature
)
742 char res_path
[PATH_MAX
];
746 if (!feature
|| !resource
)
749 snprintf(res_path
, sizeof(res_path
), "%s/%s/mon_features", INFO_PATH
, resource
);
750 inf
= fopen(res_path
, "r");
754 res
= fgrep(inf
, feature
);
762 * resource_info_file_exists - Check if a file is present inside
763 * /sys/fs/resctrl/info/@resource.
764 * @resource: Required resource (Eg: MB, L3, L2, etc.)
765 * @file: Required file.
767 * Return: True if the /sys/fs/resctrl/info/@resource/@file exists, else false.
769 bool resource_info_file_exists(const char *resource
, const char *file
)
771 char res_path
[PATH_MAX
];
774 if (!file
|| !resource
)
777 snprintf(res_path
, sizeof(res_path
), "%s/%s/%s", INFO_PATH
, resource
,
780 if (stat(res_path
, &statbuf
))
786 bool test_resource_feature_check(const struct resctrl_test
*test
)
788 return resctrl_resource_exists(test
->resource
);
791 int filter_dmesg(void)
808 dup2(pipefds
[1], STDOUT_FILENO
);
809 execlp("dmesg", "dmesg", NULL
);
810 ksft_perror("Executing dmesg");
814 fp
= fdopen(pipefds
[0], "r");
816 ksft_perror("fdopen(pipe)");
822 while (fgets(line
, 1024, fp
)) {
823 if (strstr(line
, "intel_rdt:"))
824 ksft_print_msg("dmesg: %s", line
);
825 if (strstr(line
, "resctrl:"))
826 ksft_print_msg("dmesg: %s", line
);
829 waitpid(pid
, NULL
, 0);
834 int perf_event_open(struct perf_event_attr
*hw_event
, pid_t pid
, int cpu
,
835 int group_fd
, unsigned long flags
)
839 ret
= syscall(__NR_perf_event_open
, hw_event
, pid
, cpu
,
844 unsigned int count_bits(unsigned long n
)
846 unsigned int count
= 0;