Skip to content

Commit d56e5da

Browse files
ij-intelshuahkh
authored andcommitted
selftests/resctrl: Refactor feature check to use resource and feature name
Feature check in validate_resctrl_feature_request() takes in the test name string and maps that to what to check per test. Pass resource and feature names to validate_resctrl_feature_request() directly rather than deriving them from the test name inside the function which makes the feature check easier to extend for new test cases. Use !! in the return statement to make the boolean conversion more obvious even if it is not strictly necessary from correctness point of view (to avoid it looking like the function is returning a freed pointer). Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Tested-by: Shaopeng Tan <tan.shaopeng@jp.fujitsu.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Reviewed-by: Shaopeng Tan <tan.shaopeng@jp.fujitsu.com> Cc: <stable@vger.kernel.org> # selftests/resctrl: Remove duplicate feature check from CMT test Cc: <stable@vger.kernel.org> # selftests/resctrl: Move _GNU_SOURCE define into Makefile Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 3a1e4a9 commit d56e5da

File tree

3 files changed

+34
-51
lines changed

3 files changed

+34
-51
lines changed

tools/testing/selftests/resctrl/resctrl.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
#define RESCTRL_PATH "/sys/fs/resctrl"
2828
#define PHYS_ID_PATH "/sys/devices/system/cpu/cpu"
2929
#define INFO_PATH "/sys/fs/resctrl/info"
30-
#define L3_PATH "/sys/fs/resctrl/info/L3"
31-
#define MB_PATH "/sys/fs/resctrl/info/MB"
32-
#define L3_MON_PATH "/sys/fs/resctrl/info/L3_MON"
33-
#define L3_MON_FEATURES_PATH "/sys/fs/resctrl/info/L3_MON/mon_features"
3430

3531
#define ARCH_INTEL 1
3632
#define ARCH_AMD 2
@@ -87,7 +83,7 @@ int get_resource_id(int cpu_no, int *resource_id);
8783
int mount_resctrlfs(void);
8884
int umount_resctrlfs(void);
8985
int validate_bw_report_request(char *bw_report);
90-
bool validate_resctrl_feature_request(const char *resctrl_val);
86+
bool validate_resctrl_feature_request(const char *resource, const char *feature);
9187
char *fgrep(FILE *inf, const char *str);
9288
int taskset_benchmark(pid_t bm_pid, int cpu_no);
9389
void run_benchmark(int signum, siginfo_t *info, void *ucontext);

tools/testing/selftests/resctrl/resctrl_tests.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ static void run_mbm_test(const char * const *benchmark_cmd, int cpu_no)
103103
return;
104104
}
105105

106-
if (!validate_resctrl_feature_request(MBM_STR) || (get_vendor() != ARCH_INTEL)) {
106+
if (!validate_resctrl_feature_request("L3_MON", "mbm_total_bytes") ||
107+
!validate_resctrl_feature_request("L3_MON", "mbm_local_bytes") ||
108+
(get_vendor() != ARCH_INTEL)) {
107109
ksft_test_result_skip("Hardware does not support MBM or MBM is disabled\n");
108110
goto cleanup;
109111
}
@@ -128,7 +130,7 @@ static void run_mba_test(const char * const *benchmark_cmd, int cpu_no)
128130
return;
129131
}
130132

131-
if (!validate_resctrl_feature_request(MBA_STR) || (get_vendor() != ARCH_INTEL)) {
133+
if (!validate_resctrl_feature_request("MB", NULL) || (get_vendor() != ARCH_INTEL)) {
132134
ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n");
133135
goto cleanup;
134136
}
@@ -151,7 +153,7 @@ static void run_cmt_test(const char * const *benchmark_cmd, int cpu_no)
151153
return;
152154
}
153155

154-
if (!validate_resctrl_feature_request(CMT_STR)) {
156+
if (!validate_resctrl_feature_request("L3_MON", "llc_occupancy")) {
155157
ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n");
156158
goto cleanup;
157159
}
@@ -176,7 +178,7 @@ static void run_cat_test(int cpu_no, int no_of_bits)
176178
return;
177179
}
178180

179-
if (!validate_resctrl_feature_request(CAT_STR)) {
181+
if (!validate_resctrl_feature_request("L3", NULL)) {
180182
ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n");
181183
goto cleanup;
182184
}

tools/testing/selftests/resctrl/resctrlfs.c

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
99
* Fenghua Yu <fenghua.yu@intel.com>
1010
*/
11+
#include <limits.h>
12+
1113
#include "resctrl.h"
1214

1315
static int find_resctrl_mount(char *buffer)
@@ -604,63 +606,46 @@ char *fgrep(FILE *inf, const char *str)
604606

605607
/*
606608
* validate_resctrl_feature_request - Check if requested feature is valid.
607-
* @resctrl_val: Requested feature
609+
* @resource: Required resource (e.g., MB, L3, L2, L3_MON, etc.)
610+
* @feature: Required monitor feature (in mon_features file). Can only be
611+
* set for L3_MON. Must be NULL for all other resources.
608612
*
609-
* Return: True if the feature is supported, else false. False is also
610-
* returned if resctrl FS is not mounted.
613+
* Return: True if the resource/feature is supported, else false. False is
614+
* also returned if resctrl FS is not mounted.
611615
*/
612-
bool validate_resctrl_feature_request(const char *resctrl_val)
616+
bool validate_resctrl_feature_request(const char *resource, const char *feature)
613617
{
618+
char res_path[PATH_MAX];
614619
struct stat statbuf;
615-
bool found = false;
616620
char *res;
617621
FILE *inf;
618622
int ret;
619623

620-
if (!resctrl_val)
624+
if (!resource)
621625
return false;
622626

623627
ret = find_resctrl_mount(NULL);
624628
if (ret)
625629
return false;
626630

627-
if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) {
628-
if (!stat(L3_PATH, &statbuf))
629-
return true;
630-
} else if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
631-
if (!stat(MB_PATH, &statbuf))
632-
return true;
633-
} else if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
634-
!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) {
635-
if (!stat(L3_MON_PATH, &statbuf)) {
636-
inf = fopen(L3_MON_FEATURES_PATH, "r");
637-
if (!inf)
638-
return false;
639-
640-
if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) {
641-
res = fgrep(inf, "llc_occupancy");
642-
if (res) {
643-
found = true;
644-
free(res);
645-
}
646-
}
647-
648-
if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
649-
res = fgrep(inf, "mbm_total_bytes");
650-
if (res) {
651-
free(res);
652-
res = fgrep(inf, "mbm_local_bytes");
653-
if (res) {
654-
found = true;
655-
free(res);
656-
}
657-
}
658-
}
659-
fclose(inf);
660-
}
661-
}
631+
snprintf(res_path, sizeof(res_path), "%s/%s", INFO_PATH, resource);
632+
633+
if (stat(res_path, &statbuf))
634+
return false;
635+
636+
if (!feature)
637+
return true;
638+
639+
snprintf(res_path, sizeof(res_path), "%s/%s/mon_features", INFO_PATH, resource);
640+
inf = fopen(res_path, "r");
641+
if (!inf)
642+
return false;
643+
644+
res = fgrep(inf, feature);
645+
free(res);
646+
fclose(inf);
662647

663-
return found;
648+
return !!res;
664649
}
665650

666651
int filter_dmesg(void)

0 commit comments

Comments
 (0)