xref: /linux/tools/testing/selftests/resctrl/mba_test.c (revision 91db4fd9019a08251a500157cf0d88de457e1a96)
101fee6b4SFenghua Yu // SPDX-License-Identifier: GPL-2.0
201fee6b4SFenghua Yu /*
301fee6b4SFenghua Yu  * Memory Bandwidth Allocation (MBA) test
401fee6b4SFenghua Yu  *
501fee6b4SFenghua Yu  * Copyright (C) 2018 Intel Corporation
601fee6b4SFenghua Yu  *
701fee6b4SFenghua Yu  * Authors:
801fee6b4SFenghua Yu  *    Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
901fee6b4SFenghua Yu  *    Fenghua Yu <fenghua.yu@intel.com>
1001fee6b4SFenghua Yu  */
1101fee6b4SFenghua Yu #include "resctrl.h"
1201fee6b4SFenghua Yu 
1301fee6b4SFenghua Yu #define RESULT_FILE_NAME	"result_mba"
1401fee6b4SFenghua Yu #define NUM_OF_RUNS		5
1506bd03a5SFenghua Yu #define MAX_DIFF_PERCENT	5
1601fee6b4SFenghua Yu #define ALLOCATION_MAX		100
1701fee6b4SFenghua Yu #define ALLOCATION_MIN		10
1801fee6b4SFenghua Yu #define ALLOCATION_STEP		10
1901fee6b4SFenghua Yu 
2001fee6b4SFenghua Yu /*
2101fee6b4SFenghua Yu  * Change schemata percentage from 100 to 10%. Write schemata to specified
2201fee6b4SFenghua Yu  * con_mon grp, mon_grp in resctrl FS.
2301fee6b4SFenghua Yu  * For each allocation, run 5 times in order to get average values.
2401fee6b4SFenghua Yu  */
2501fee6b4SFenghua Yu static int mba_setup(int num, ...)
2601fee6b4SFenghua Yu {
2701fee6b4SFenghua Yu 	static int runs_per_allocation, allocation = 100;
2801fee6b4SFenghua Yu 	struct resctrl_val_param *p;
2901fee6b4SFenghua Yu 	char allocation_str[64];
3001fee6b4SFenghua Yu 	va_list param;
310d45c83bSIlpo Järvinen 	int ret;
3201fee6b4SFenghua Yu 
3301fee6b4SFenghua Yu 	va_start(param, num);
3401fee6b4SFenghua Yu 	p = va_arg(param, struct resctrl_val_param *);
3501fee6b4SFenghua Yu 	va_end(param);
3601fee6b4SFenghua Yu 
3701fee6b4SFenghua Yu 	if (runs_per_allocation >= NUM_OF_RUNS)
3801fee6b4SFenghua Yu 		runs_per_allocation = 0;
3901fee6b4SFenghua Yu 
4001fee6b4SFenghua Yu 	/* Only set up schemata once every NUM_OF_RUNS of allocations */
4101fee6b4SFenghua Yu 	if (runs_per_allocation++ != 0)
4201fee6b4SFenghua Yu 		return 0;
4301fee6b4SFenghua Yu 
4401fee6b4SFenghua Yu 	if (allocation < ALLOCATION_MIN || allocation > ALLOCATION_MAX)
45fa10366cSIlpo Järvinen 		return END_OF_TESTS;
4601fee6b4SFenghua Yu 
4701fee6b4SFenghua Yu 	sprintf(allocation_str, "%d", allocation);
4801fee6b4SFenghua Yu 
490d45c83bSIlpo Järvinen 	ret = write_schemata(p->ctrlgrp, allocation_str, p->cpu_no,
500d45c83bSIlpo Järvinen 			     p->resctrl_val);
510d45c83bSIlpo Järvinen 	if (ret < 0)
520d45c83bSIlpo Järvinen 		return ret;
530d45c83bSIlpo Järvinen 
5401fee6b4SFenghua Yu 	allocation -= ALLOCATION_STEP;
5501fee6b4SFenghua Yu 
5601fee6b4SFenghua Yu 	return 0;
5701fee6b4SFenghua Yu }
5801fee6b4SFenghua Yu 
591e359b6aSShaopeng Tan static bool show_mba_info(unsigned long *bw_imc, unsigned long *bw_resc)
6001fee6b4SFenghua Yu {
6101fee6b4SFenghua Yu 	int allocation, runs;
621e359b6aSShaopeng Tan 	bool ret = false;
6301fee6b4SFenghua Yu 
64ca2f4214SFenghua Yu 	ksft_print_msg("Results are displayed in (MB)\n");
6501fee6b4SFenghua Yu 	/* Memory bandwidth from 100% down to 10% */
6601fee6b4SFenghua Yu 	for (allocation = 0; allocation < ALLOCATION_MAX / ALLOCATION_STEP;
6701fee6b4SFenghua Yu 	     allocation++) {
6801fee6b4SFenghua Yu 		unsigned long avg_bw_imc, avg_bw_resc;
6901fee6b4SFenghua Yu 		unsigned long sum_bw_imc = 0, sum_bw_resc = 0;
7006bd03a5SFenghua Yu 		int avg_diff_per;
7106bd03a5SFenghua Yu 		float avg_diff;
7201fee6b4SFenghua Yu 
7301fee6b4SFenghua Yu 		/*
7401fee6b4SFenghua Yu 		 * The first run is discarded due to inaccurate value from
7501fee6b4SFenghua Yu 		 * phase transition.
7601fee6b4SFenghua Yu 		 */
7701fee6b4SFenghua Yu 		for (runs = NUM_OF_RUNS * allocation + 1;
7801fee6b4SFenghua Yu 		     runs < NUM_OF_RUNS * allocation + NUM_OF_RUNS ; runs++) {
7901fee6b4SFenghua Yu 			sum_bw_imc += bw_imc[runs];
8001fee6b4SFenghua Yu 			sum_bw_resc += bw_resc[runs];
8101fee6b4SFenghua Yu 		}
8201fee6b4SFenghua Yu 
8301fee6b4SFenghua Yu 		avg_bw_imc = sum_bw_imc / (NUM_OF_RUNS - 1);
8401fee6b4SFenghua Yu 		avg_bw_resc = sum_bw_resc / (NUM_OF_RUNS - 1);
8506bd03a5SFenghua Yu 		avg_diff = (float)labs(avg_bw_resc - avg_bw_imc) / avg_bw_imc;
8606bd03a5SFenghua Yu 		avg_diff_per = (int)(avg_diff * 100);
8701fee6b4SFenghua Yu 
88e7507478SFenghua Yu 		ksft_print_msg("%s Check MBA diff within %d%% for schemata %u\n",
8906bd03a5SFenghua Yu 			       avg_diff_per > MAX_DIFF_PERCENT ?
9006bd03a5SFenghua Yu 			       "Fail:" : "Pass:",
9106bd03a5SFenghua Yu 			       MAX_DIFF_PERCENT,
9206bd03a5SFenghua Yu 			       ALLOCATION_MAX - ALLOCATION_STEP * allocation);
9306bd03a5SFenghua Yu 
9406bd03a5SFenghua Yu 		ksft_print_msg("avg_diff_per: %d%%\n", avg_diff_per);
95ca2f4214SFenghua Yu 		ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc);
96ca2f4214SFenghua Yu 		ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc);
9706bd03a5SFenghua Yu 		if (avg_diff_per > MAX_DIFF_PERCENT)
981e359b6aSShaopeng Tan 			ret = true;
9901fee6b4SFenghua Yu 	}
10001fee6b4SFenghua Yu 
101e7507478SFenghua Yu 	ksft_print_msg("%s Check schemata change using MBA\n",
1021e359b6aSShaopeng Tan 		       ret ? "Fail:" : "Pass:");
1031e359b6aSShaopeng Tan 	if (ret)
104e7507478SFenghua Yu 		ksft_print_msg("At least one test failed\n");
1051e359b6aSShaopeng Tan 
1061e359b6aSShaopeng Tan 	return ret;
10701fee6b4SFenghua Yu }
10801fee6b4SFenghua Yu 
10901fee6b4SFenghua Yu static int check_results(void)
11001fee6b4SFenghua Yu {
11101fee6b4SFenghua Yu 	char *token_array[8], output[] = RESULT_FILE_NAME, temp[512];
11201fee6b4SFenghua Yu 	unsigned long bw_imc[1024], bw_resc[1024];
11301fee6b4SFenghua Yu 	int runs;
11401fee6b4SFenghua Yu 	FILE *fp;
11501fee6b4SFenghua Yu 
11601fee6b4SFenghua Yu 	fp = fopen(output, "r");
11701fee6b4SFenghua Yu 	if (!fp) {
11801fee6b4SFenghua Yu 		perror(output);
11901fee6b4SFenghua Yu 
12001fee6b4SFenghua Yu 		return errno;
12101fee6b4SFenghua Yu 	}
12201fee6b4SFenghua Yu 
12301fee6b4SFenghua Yu 	runs = 0;
12401fee6b4SFenghua Yu 	while (fgets(temp, sizeof(temp), fp)) {
12501fee6b4SFenghua Yu 		char *token = strtok(temp, ":\t");
12601fee6b4SFenghua Yu 		int fields = 0;
12701fee6b4SFenghua Yu 
12801fee6b4SFenghua Yu 		while (token) {
12901fee6b4SFenghua Yu 			token_array[fields++] = token;
13001fee6b4SFenghua Yu 			token = strtok(NULL, ":\t");
13101fee6b4SFenghua Yu 		}
13201fee6b4SFenghua Yu 
13301fee6b4SFenghua Yu 		/* Field 3 is perf imc value */
13401fee6b4SFenghua Yu 		bw_imc[runs] = strtoul(token_array[3], NULL, 0);
13501fee6b4SFenghua Yu 		/* Field 5 is resctrl value */
13601fee6b4SFenghua Yu 		bw_resc[runs] = strtoul(token_array[5], NULL, 0);
13701fee6b4SFenghua Yu 		runs++;
13801fee6b4SFenghua Yu 	}
13901fee6b4SFenghua Yu 
14001fee6b4SFenghua Yu 	fclose(fp);
14101fee6b4SFenghua Yu 
1421e359b6aSShaopeng Tan 	return show_mba_info(bw_imc, bw_resc);
14301fee6b4SFenghua Yu }
14401fee6b4SFenghua Yu 
14501fee6b4SFenghua Yu void mba_test_cleanup(void)
14601fee6b4SFenghua Yu {
14701fee6b4SFenghua Yu 	remove(RESULT_FILE_NAME);
14801fee6b4SFenghua Yu }
14901fee6b4SFenghua Yu 
15001fee6b4SFenghua Yu int mba_schemata_change(int cpu_no, char *bw_report, char **benchmark_cmd)
15101fee6b4SFenghua Yu {
15201fee6b4SFenghua Yu 	struct resctrl_val_param param = {
15324286736SFenghua Yu 		.resctrl_val	= MBA_STR,
15401fee6b4SFenghua Yu 		.ctrlgrp	= "c1",
15501fee6b4SFenghua Yu 		.mongrp		= "m1",
15601fee6b4SFenghua Yu 		.cpu_no		= cpu_no,
157a967e17fSIlpo Järvinen 		.mum_resctrlfs	= true,
15801fee6b4SFenghua Yu 		.filename	= RESULT_FILE_NAME,
15901fee6b4SFenghua Yu 		.bw_report	= bw_report,
16001fee6b4SFenghua Yu 		.setup		= mba_setup
16101fee6b4SFenghua Yu 	};
16201fee6b4SFenghua Yu 	int ret;
16301fee6b4SFenghua Yu 
16401fee6b4SFenghua Yu 	remove(RESULT_FILE_NAME);
16501fee6b4SFenghua Yu 
16601fee6b4SFenghua Yu 	ret = resctrl_val(benchmark_cmd, &param);
16701fee6b4SFenghua Yu 	if (ret)
168*91db4fd9SShaopeng Tan 		goto out;
16901fee6b4SFenghua Yu 
17001fee6b4SFenghua Yu 	ret = check_results();
17101fee6b4SFenghua Yu 
172*91db4fd9SShaopeng Tan out:
17301fee6b4SFenghua Yu 	mba_test_cleanup();
17401fee6b4SFenghua Yu 
175*91db4fd9SShaopeng Tan 	return ret;
17601fee6b4SFenghua Yu }
177