1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Memory Bandwidth Allocation (MBA) test
4  *
5  * Copyright (C) 2018 Intel Corporation
6  *
7  * Authors:
8  *    Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
9  *    Fenghua Yu <fenghua.yu@intel.com>
10  */
11 #include "resctrl.h"
12 
13 #define RESULT_FILE_NAME	"result_mba"
14 #define NUM_OF_RUNS		5
15 #define MAX_DIFF_PERCENT	5
16 #define ALLOCATION_MAX		100
17 #define ALLOCATION_MIN		10
18 #define ALLOCATION_STEP		10
19 
20 /*
21  * Change schemata percentage from 100 to 10%. Write schemata to specified
22  * con_mon grp, mon_grp in resctrl FS.
23  * For each allocation, run 5 times in order to get average values.
24  */
25 static int mba_setup(int num, ...)
26 {
27 	static int runs_per_allocation, allocation = 100;
28 	struct resctrl_val_param *p;
29 	char allocation_str[64];
30 	va_list param;
31 	int ret;
32 
33 	va_start(param, num);
34 	p = va_arg(param, struct resctrl_val_param *);
35 	va_end(param);
36 
37 	if (runs_per_allocation >= NUM_OF_RUNS)
38 		runs_per_allocation = 0;
39 
40 	/* Only set up schemata once every NUM_OF_RUNS of allocations */
41 	if (runs_per_allocation++ != 0)
42 		return 0;
43 
44 	if (allocation < ALLOCATION_MIN || allocation > ALLOCATION_MAX)
45 		return END_OF_TESTS;
46 
47 	sprintf(allocation_str, "%d", allocation);
48 
49 	ret = write_schemata(p->ctrlgrp, allocation_str, p->cpu_no,
50 			     p->resctrl_val);
51 	if (ret < 0)
52 		return ret;
53 
54 	allocation -= ALLOCATION_STEP;
55 
56 	return 0;
57 }
58 
59 static bool show_mba_info(unsigned long *bw_imc, unsigned long *bw_resc)
60 {
61 	int allocation, runs;
62 	bool ret = false;
63 
64 	ksft_print_msg("Results are displayed in (MB)\n");
65 	/* Memory bandwidth from 100% down to 10% */
66 	for (allocation = 0; allocation < ALLOCATION_MAX / ALLOCATION_STEP;
67 	     allocation++) {
68 		unsigned long avg_bw_imc, avg_bw_resc;
69 		unsigned long sum_bw_imc = 0, sum_bw_resc = 0;
70 		int avg_diff_per;
71 		float avg_diff;
72 
73 		/*
74 		 * The first run is discarded due to inaccurate value from
75 		 * phase transition.
76 		 */
77 		for (runs = NUM_OF_RUNS * allocation + 1;
78 		     runs < NUM_OF_RUNS * allocation + NUM_OF_RUNS ; runs++) {
79 			sum_bw_imc += bw_imc[runs];
80 			sum_bw_resc += bw_resc[runs];
81 		}
82 
83 		avg_bw_imc = sum_bw_imc / (NUM_OF_RUNS - 1);
84 		avg_bw_resc = sum_bw_resc / (NUM_OF_RUNS - 1);
85 		avg_diff = (float)labs(avg_bw_resc - avg_bw_imc) / avg_bw_imc;
86 		avg_diff_per = (int)(avg_diff * 100);
87 
88 		ksft_print_msg("%s Check MBA diff within %d%% for schemata %u\n",
89 			       avg_diff_per > MAX_DIFF_PERCENT ?
90 			       "Fail:" : "Pass:",
91 			       MAX_DIFF_PERCENT,
92 			       ALLOCATION_MAX - ALLOCATION_STEP * allocation);
93 
94 		ksft_print_msg("avg_diff_per: %d%%\n", avg_diff_per);
95 		ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc);
96 		ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc);
97 		if (avg_diff_per > MAX_DIFF_PERCENT)
98 			ret = true;
99 	}
100 
101 	ksft_print_msg("%s Check schemata change using MBA\n",
102 		       ret ? "Fail:" : "Pass:");
103 	if (ret)
104 		ksft_print_msg("At least one test failed\n");
105 
106 	return ret;
107 }
108 
109 static int check_results(void)
110 {
111 	char *token_array[8], output[] = RESULT_FILE_NAME, temp[512];
112 	unsigned long bw_imc[1024], bw_resc[1024];
113 	int runs;
114 	FILE *fp;
115 
116 	fp = fopen(output, "r");
117 	if (!fp) {
118 		perror(output);
119 
120 		return errno;
121 	}
122 
123 	runs = 0;
124 	while (fgets(temp, sizeof(temp), fp)) {
125 		char *token = strtok(temp, ":\t");
126 		int fields = 0;
127 
128 		while (token) {
129 			token_array[fields++] = token;
130 			token = strtok(NULL, ":\t");
131 		}
132 
133 		/* Field 3 is perf imc value */
134 		bw_imc[runs] = strtoul(token_array[3], NULL, 0);
135 		/* Field 5 is resctrl value */
136 		bw_resc[runs] = strtoul(token_array[5], NULL, 0);
137 		runs++;
138 	}
139 
140 	fclose(fp);
141 
142 	return show_mba_info(bw_imc, bw_resc);
143 }
144 
145 void mba_test_cleanup(void)
146 {
147 	remove(RESULT_FILE_NAME);
148 }
149 
150 int mba_schemata_change(int cpu_no, char *bw_report, char **benchmark_cmd)
151 {
152 	struct resctrl_val_param param = {
153 		.resctrl_val	= MBA_STR,
154 		.ctrlgrp	= "c1",
155 		.mongrp		= "m1",
156 		.cpu_no		= cpu_no,
157 		.mum_resctrlfs	= true,
158 		.filename	= RESULT_FILE_NAME,
159 		.bw_report	= bw_report,
160 		.setup		= mba_setup
161 	};
162 	int ret;
163 
164 	remove(RESULT_FILE_NAME);
165 
166 	ret = resctrl_val(benchmark_cmd, &param);
167 	if (ret)
168 		goto out;
169 
170 	ret = check_results();
171 
172 out:
173 	mba_test_cleanup();
174 
175 	return ret;
176 }
177