Skip to content

Commit fa1116d

Browse files
ij-intelshuahkh
authored andcommitted
selftests/resctrl: Simplify bandwidth report type handling
bw_report is only needed for selecting the correct value from the values IMC measured. It is a member in the resctrl_val_param struct and is always set to "reads". The value is then checked in resctrl_val() using validate_bw_report_request() that besides validating the input, assumes it can mutate the string which is questionable programming practice. Simplify handling bw_report: - Convert validate_bw_report_request() into get_bw_report_type() that inputs and returns const char *. Use NULL to indicate error. - Validate the report types inside measure_mem_bw(), not in resctrl_val(). - Pass bw_report to measure_mem_bw() from ->measure() hook because resctrl_val() no longer needs bw_report for anything. Signed-off-by: Ilpo Järvinen <[email protected]> Tested-by: Babu Moger <[email protected]> Reviewed-by: Reinette Chatre <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent aef5efa commit fa1116d

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

tools/testing/selftests/resctrl/mba_test.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static int mba_setup(const struct resctrl_test *test,
6767
static int mba_measure(const struct user_params *uparams,
6868
struct resctrl_val_param *param, pid_t bm_pid)
6969
{
70-
return measure_mem_bw(uparams, param, bm_pid);
70+
return measure_mem_bw(uparams, param, bm_pid, "reads");
7171
}
7272

7373
static bool show_mba_info(unsigned long *bw_imc, unsigned long *bw_resc)
@@ -168,7 +168,6 @@ static int mba_run_test(const struct resctrl_test *test, const struct user_param
168168
.ctrlgrp = "c1",
169169
.mongrp = "m1",
170170
.filename = RESULT_FILE_NAME,
171-
.bw_report = "reads",
172171
.init = mba_init,
173172
.setup = mba_setup,
174173
.measure = mba_measure,

tools/testing/selftests/resctrl/mbm_test.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static int mbm_setup(const struct resctrl_test *test,
121121
static int mbm_measure(const struct user_params *uparams,
122122
struct resctrl_val_param *param, pid_t bm_pid)
123123
{
124-
return measure_mem_bw(uparams, param, bm_pid);
124+
return measure_mem_bw(uparams, param, bm_pid, "reads");
125125
}
126126

127127
static void mbm_test_cleanup(void)
@@ -135,7 +135,6 @@ static int mbm_run_test(const struct resctrl_test *test, const struct user_param
135135
.resctrl_val = MBM_STR,
136136
.ctrlgrp = "c1",
137137
.filename = RESULT_FILE_NAME,
138-
.bw_report = "reads",
139138
.init = mbm_init,
140139
.setup = mbm_setup,
141140
.measure = mbm_measure,

tools/testing/selftests/resctrl/resctrl.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ struct resctrl_test {
8585
* @ctrlgrp: Name of the control monitor group (con_mon grp)
8686
* @mongrp: Name of the monitor group (mon grp)
8787
* @filename: Name of file to which the o/p should be written
88-
* @bw_report: Bandwidth report type (reads vs writes)
8988
* @init: Callback function to initialize test environment
9089
* @setup: Callback function to setup per test run environment
9190
* @measure: Callback that performs the measurement (a single test)
@@ -95,7 +94,6 @@ struct resctrl_val_param {
9594
char ctrlgrp[64];
9695
char mongrp[64];
9796
char filename[64];
98-
char *bw_report;
9997
unsigned long mask;
10098
int num_of_runs;
10199
int (*init)(const struct resctrl_val_param *param,
@@ -135,7 +133,7 @@ int filter_dmesg(void);
135133
int get_domain_id(const char *resource, int cpu_no, int *domain_id);
136134
int mount_resctrlfs(void);
137135
int umount_resctrlfs(void);
138-
int validate_bw_report_request(char *bw_report);
136+
const char *get_bw_report_type(const char *bw_report);
139137
bool resctrl_resource_exists(const char *resource);
140138
bool resctrl_mon_feature_exists(const char *resource, const char *feature);
141139
bool resource_info_file_exists(const char *resource, const char *file);
@@ -154,7 +152,8 @@ void fill_cache_read(unsigned char *buf, size_t buf_size, bool once);
154152
int run_fill_buf(size_t buf_size, int memflush, int op, bool once);
155153
int initialize_mem_bw_imc(void);
156154
int measure_mem_bw(const struct user_params *uparams,
157-
struct resctrl_val_param *param, pid_t bm_pid);
155+
struct resctrl_val_param *param, pid_t bm_pid,
156+
const char *bw_report);
158157
void initialize_mem_bw_resctrl(const struct resctrl_val_param *param,
159158
int domain_id);
160159
int resctrl_val(const struct resctrl_test *test,

tools/testing/selftests/resctrl/resctrl_val.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ static void do_imc_mem_bw_test(void)
349349
*
350350
* Return: = 0 on success. < 0 on failure.
351351
*/
352-
static int get_mem_bw_imc(char *bw_report, float *bw_imc)
352+
static int get_mem_bw_imc(const char *bw_report, float *bw_imc)
353353
{
354354
float reads, writes, of_mul_read, of_mul_write;
355355
int imc;
@@ -556,20 +556,26 @@ static int print_results_bw(char *filename, pid_t bm_pid, float bw_imc,
556556
* @uparams: User supplied parameters
557557
* @param: Parameters passed to resctrl_val()
558558
* @bm_pid: PID that runs the benchmark
559+
* @bw_report: Bandwidth report type (reads, writes)
559560
*
560561
* Measure memory bandwidth from resctrl and from another source which is
561562
* perf imc value or could be something else if perf imc event is not
562563
* available. Compare the two values to validate resctrl value. It takes
563564
* 1 sec to measure the data.
564565
*/
565566
int measure_mem_bw(const struct user_params *uparams,
566-
struct resctrl_val_param *param, pid_t bm_pid)
567+
struct resctrl_val_param *param, pid_t bm_pid,
568+
const char *bw_report)
567569
{
568570
unsigned long bw_resc, bw_resc_start, bw_resc_end;
569571
FILE *mem_bw_fp;
570572
float bw_imc;
571573
int ret;
572574

575+
bw_report = get_bw_report_type(bw_report);
576+
if (!bw_report)
577+
return -1;
578+
573579
mem_bw_fp = open_mem_bw_resctrl(mbm_total_path);
574580
if (!mem_bw_fp)
575581
return -1;
@@ -590,7 +596,7 @@ int measure_mem_bw(const struct user_params *uparams,
590596
if (ret < 0)
591597
goto close_imc;
592598

593-
ret = get_mem_bw_imc(param->bw_report, &bw_imc);
599+
ret = get_mem_bw_imc(bw_report, &bw_imc);
594600
if (ret < 0)
595601
goto close_imc;
596602

@@ -694,13 +700,6 @@ int resctrl_val(const struct resctrl_test *test,
694700
return ret;
695701
}
696702

697-
if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
698-
!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
699-
ret = validate_bw_report_request(param->bw_report);
700-
if (ret)
701-
return ret;
702-
}
703-
704703
/*
705704
* If benchmark wasn't successfully started by child, then child should
706705
* kill parent, so save parent's pid

tools/testing/selftests/resctrl/resctrlfs.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -837,22 +837,21 @@ int filter_dmesg(void)
837837
return 0;
838838
}
839839

840-
int validate_bw_report_request(char *bw_report)
840+
const char *get_bw_report_type(const char *bw_report)
841841
{
842842
if (strcmp(bw_report, "reads") == 0)
843-
return 0;
843+
return bw_report;
844844
if (strcmp(bw_report, "writes") == 0)
845-
return 0;
845+
return bw_report;
846846
if (strcmp(bw_report, "nt-writes") == 0) {
847-
strcpy(bw_report, "writes");
848-
return 0;
847+
return "writes";
849848
}
850849
if (strcmp(bw_report, "total") == 0)
851-
return 0;
850+
return bw_report;
852851

853852
fprintf(stderr, "Requested iMC bandwidth report type unavailable\n");
854853

855-
return -1;
854+
return NULL;
856855
}
857856

858857
int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,

0 commit comments

Comments
 (0)