2
0
mirror of https://github.com/edk2-porting/linux-next.git synced 2024-12-17 17:53:56 +08:00

selftests/resctrl: Clean up resctrl features check

Checking resctrl features call strcmp() to compare feature strings
(e.g. "mba", "cat" etc). The checkings are error prone and don't have
good coding style. Define the constant strings in macros and call
strncmp() to solve the potential issues.

Suggested-by: Shuah Khan <skhan@linuxfoundation.org>
Tested-by: Babu Moger <babu.moger@amd.com>
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
This commit is contained in:
Fenghua Yu 2021-03-17 02:22:38 +00:00 committed by Shuah Khan
parent 896016d2ad
commit 2428673638
10 changed files with 41 additions and 35 deletions

View File

@ -182,7 +182,7 @@ int measure_cache_vals(struct resctrl_val_param *param, int bm_pid)
/*
* Measure cache miss from perf.
*/
if (!strcmp(param->resctrl_val, "cat")) {
if (!strncmp(param->resctrl_val, CAT_STR, sizeof(CAT_STR))) {
ret = get_llc_perf(&llc_perf_miss);
if (ret < 0)
return ret;
@ -192,7 +192,7 @@ int measure_cache_vals(struct resctrl_val_param *param, int bm_pid)
/*
* Measure llc occupancy from resctrl.
*/
if (!strcmp(param->resctrl_val, "cqm")) {
if (!strncmp(param->resctrl_val, CQM_STR, sizeof(CQM_STR))) {
ret = get_llc_occu_resctrl(&llc_occu_resc);
if (ret < 0)
return ret;
@ -234,7 +234,7 @@ int cat_val(struct resctrl_val_param *param)
if (ret)
return ret;
if ((strcmp(resctrl_val, "cat") == 0)) {
if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) {
ret = initialize_llc_perf();
if (ret)
return ret;
@ -242,7 +242,7 @@ int cat_val(struct resctrl_val_param *param)
/* Test runs until the callback setup() tells the test to stop. */
while (1) {
if (strcmp(resctrl_val, "cat") == 0) {
if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) {
ret = param->setup(1, param);
if (ret) {
ret = 0;

View File

@ -164,7 +164,7 @@ int cat_perf_miss_val(int cpu_no, int n, char *cache_type)
return -1;
struct resctrl_val_param param = {
.resctrl_val = "cat",
.resctrl_val = CAT_STR,
.cpu_no = cpu_no,
.mum_resctrlfs = 0,
.setup = cat_setup,

View File

@ -145,7 +145,7 @@ int cqm_resctrl_val(int cpu_no, int n, char **benchmark_cmd)
}
struct resctrl_val_param param = {
.resctrl_val = "cqm",
.resctrl_val = CQM_STR,
.ctrlgrp = "c1",
.mongrp = "m1",
.cpu_no = cpu_no,

View File

@ -115,7 +115,7 @@ static int fill_cache_read(unsigned char *start_ptr, unsigned char *end_ptr,
while (1) {
ret = fill_one_span_read(start_ptr, end_ptr);
if (!strcmp(resctrl_val, "cat"))
if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)))
break;
}
@ -134,7 +134,7 @@ static int fill_cache_write(unsigned char *start_ptr, unsigned char *end_ptr,
{
while (1) {
fill_one_span_write(start_ptr, end_ptr);
if (!strcmp(resctrl_val, "cat"))
if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)))
break;
}

View File

@ -141,7 +141,7 @@ void mba_test_cleanup(void)
int mba_schemata_change(int cpu_no, char *bw_report, char **benchmark_cmd)
{
struct resctrl_val_param param = {
.resctrl_val = "mba",
.resctrl_val = MBA_STR,
.ctrlgrp = "c1",
.mongrp = "m1",
.cpu_no = cpu_no,

View File

@ -114,7 +114,7 @@ void mbm_test_cleanup(void)
int mbm_bw_change(int span, int cpu_no, char *bw_report, char **benchmark_cmd)
{
struct resctrl_val_param param = {
.resctrl_val = "mbm",
.resctrl_val = MBM_STR,
.ctrlgrp = "c1",
.mongrp = "m1",
.span = span,

View File

@ -62,6 +62,11 @@ struct resctrl_val_param {
int (*setup)(int num, ...);
};
#define MBM_STR "mbm"
#define MBA_STR "mba"
#define CQM_STR "cqm"
#define CAT_STR "cat"
extern pid_t bm_pid, ppid;
extern int tests_run;

View File

@ -85,13 +85,13 @@ int main(int argc, char **argv)
cqm_test = false;
cat_test = false;
while (token) {
if (!strcmp(token, "mbm")) {
if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
mbm_test = true;
} else if (!strcmp(token, "mba")) {
} else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
mba_test = true;
} else if (!strcmp(token, "cqm")) {
} else if (!strncmp(token, CQM_STR, sizeof(CQM_STR))) {
cqm_test = true;
} else if (!strcmp(token, "cat")) {
} else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
cat_test = true;
} else {
printf("invalid argument\n");
@ -161,7 +161,7 @@ int main(int argc, char **argv)
if (!is_amd && mbm_test) {
printf("# Starting MBM BW change ...\n");
if (!has_ben)
sprintf(benchmark_cmd[5], "%s", "mba");
sprintf(benchmark_cmd[5], "%s", MBA_STR);
res = mbm_bw_change(span, cpu_no, bw_report, benchmark_cmd);
printf("%sok MBM: bw change\n", res ? "not " : "");
mbm_test_cleanup();
@ -181,7 +181,7 @@ int main(int argc, char **argv)
if (cqm_test) {
printf("# Starting CQM test ...\n");
if (!has_ben)
sprintf(benchmark_cmd[5], "%s", "cqm");
sprintf(benchmark_cmd[5], "%s", CQM_STR);
res = cqm_resctrl_val(cpu_no, no_of_bits, benchmark_cmd);
printf("%sok CQM: test\n", res ? "not " : "");
cqm_test_cleanup();

View File

@ -397,10 +397,10 @@ static void initialize_mem_bw_resctrl(const char *ctrlgrp, const char *mongrp,
return;
}
if (strcmp(resctrl_val, "mbm") == 0)
if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)))
set_mbm_path(ctrlgrp, mongrp, resource_id);
if ((strcmp(resctrl_val, "mba") == 0)) {
if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
if (ctrlgrp)
sprintf(mbm_total_path, CON_MBM_LOCAL_BYTES_PATH,
RESCTRL_PATH, ctrlgrp, resource_id);
@ -524,7 +524,7 @@ static void initialize_llc_occu_resctrl(const char *ctrlgrp, const char *mongrp,
return;
}
if (strcmp(resctrl_val, "cqm") == 0)
if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
set_cqm_path(ctrlgrp, mongrp, resource_id);
}
@ -579,8 +579,8 @@ int resctrl_val(char **benchmark_cmd, struct resctrl_val_param *param)
if (strcmp(param->filename, "") == 0)
sprintf(param->filename, "stdio");
if ((strcmp(resctrl_val, "mba")) == 0 ||
(strcmp(resctrl_val, "mbm")) == 0) {
if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
ret = validate_bw_report_request(param->bw_report);
if (ret)
return ret;
@ -674,15 +674,15 @@ int resctrl_val(char **benchmark_cmd, struct resctrl_val_param *param)
if (ret)
goto out;
if ((strcmp(resctrl_val, "mbm") == 0) ||
(strcmp(resctrl_val, "mba") == 0)) {
if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
ret = initialize_mem_bw_imc();
if (ret)
goto out;
initialize_mem_bw_resctrl(param->ctrlgrp, param->mongrp,
param->cpu_no, resctrl_val);
} else if (strcmp(resctrl_val, "cqm") == 0)
} else if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
initialize_llc_occu_resctrl(param->ctrlgrp, param->mongrp,
param->cpu_no, resctrl_val);
@ -710,8 +710,8 @@ int resctrl_val(char **benchmark_cmd, struct resctrl_val_param *param)
/* Test runs until the callback setup() tells the test to stop. */
while (1) {
if ((strcmp(resctrl_val, "mbm") == 0) ||
(strcmp(resctrl_val, "mba") == 0)) {
if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
ret = param->setup(1, param);
if (ret) {
ret = 0;
@ -721,7 +721,7 @@ int resctrl_val(char **benchmark_cmd, struct resctrl_val_param *param)
ret = measure_vals(param, &bw_resc_start);
if (ret)
break;
} else if (strcmp(resctrl_val, "cqm") == 0) {
} else if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR))) {
ret = param->setup(1, param);
if (ret) {
ret = 0;

View File

@ -334,7 +334,7 @@ void run_benchmark(int signum, siginfo_t *info, void *ucontext)
operation = atoi(benchmark_cmd[4]);
sprintf(resctrl_val, "%s", benchmark_cmd[5]);
if (strcmp(resctrl_val, "cqm") != 0)
if (strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
buffer_span = span * MB;
else
buffer_span = span;
@ -459,8 +459,8 @@ int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
goto out;
/* Create mon grp and write pid into it for "mbm" and "cqm" test */
if ((strcmp(resctrl_val, "cqm") == 0) ||
(strcmp(resctrl_val, "mbm") == 0)) {
if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)) ||
!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
if (strlen(mongrp)) {
sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
@ -505,9 +505,9 @@ int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
int resource_id, ret = 0;
FILE *fp;
if ((strcmp(resctrl_val, "mba") != 0) &&
(strcmp(resctrl_val, "cat") != 0) &&
(strcmp(resctrl_val, "cqm") != 0))
if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) &&
strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
return -ENOENT;
if (!schemata) {
@ -528,9 +528,10 @@ int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
else
sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
if (!strcmp(resctrl_val, "cat") || !strcmp(resctrl_val, "cqm"))
if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
if (strcmp(resctrl_val, "mba") == 0)
if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)))
sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
fp = fopen(controlgroup, "w");