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	8
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(struct resctrl_val_param *p)
26 {
27 	static int runs_per_allocation, allocation = 100;
28 	char allocation_str[64];
29 	int ret;
30 
31 	if (runs_per_allocation >= NUM_OF_RUNS)
32 		runs_per_allocation = 0;
33 
34 	/* Only set up schemata once every NUM_OF_RUNS of allocations */
35 	if (runs_per_allocation++ != 0)
36 		return 0;
37 
38 	if (allocation < ALLOCATION_MIN || allocation > ALLOCATION_MAX)
39 		return END_OF_TESTS;
40 
41 	sprintf(allocation_str, "%d", allocation);
42 
43 	ret = write_schemata(p->ctrlgrp, allocation_str, p->cpu_no,
44 			     p->resctrl_val);
45 	if (ret < 0)
46 		return ret;
47 
48 	allocation -= ALLOCATION_STEP;
49 
50 	return 0;
51 }
52 
53 static bool show_mba_info(unsigned long *bw_imc, unsigned long *bw_resc)
54 {
55 	int allocation, runs;
56 	bool ret = false;
57 
58 	ksft_print_msg("Results are displayed in (MB)\n");
59 	/* Memory bandwidth from 100% down to 10% */
60 	for (allocation = 0; allocation < ALLOCATION_MAX / ALLOCATION_STEP;
61 	     allocation++) {
62 		unsigned long avg_bw_imc, avg_bw_resc;
63 		unsigned long sum_bw_imc = 0, sum_bw_resc = 0;
64 		int avg_diff_per;
65 		float avg_diff;
66 
67 		/*
68 		 * The first run is discarded due to inaccurate value from
69 		 * phase transition.
70 		 */
71 		for (runs = NUM_OF_RUNS * allocation + 1;
72 		     runs < NUM_OF_RUNS * allocation + NUM_OF_RUNS ; runs++) {
73 			sum_bw_imc += bw_imc[runs];
74 			sum_bw_resc += bw_resc[runs];
75 		}
76 
77 		avg_bw_imc = sum_bw_imc / (NUM_OF_RUNS - 1);
78 		avg_bw_resc = sum_bw_resc / (NUM_OF_RUNS - 1);
79 		avg_diff = (float)labs(avg_bw_resc - avg_bw_imc) / avg_bw_imc;
80 		avg_diff_per = (int)(avg_diff * 100);
81 
82 		ksft_print_msg("%s Check MBA diff within %d%% for schemata %u\n",
83 			       avg_diff_per > MAX_DIFF_PERCENT ?
84 			       "Fail:" : "Pass:",
85 			       MAX_DIFF_PERCENT,
86 			       ALLOCATION_MAX - ALLOCATION_STEP * allocation);
87 
88 		ksft_print_msg("avg_diff_per: %d%%\n", avg_diff_per);
89 		ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc);
90 		ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc);
91 		if (avg_diff_per > MAX_DIFF_PERCENT)
92 			ret = true;
93 	}
94 
95 	ksft_print_msg("%s Check schemata change using MBA\n",
96 		       ret ? "Fail:" : "Pass:");
97 	if (ret)
98 		ksft_print_msg("At least one test failed\n");
99 
100 	return ret;
101 }
102 
103 static int check_results(void)
104 {
105 	char *token_array[8], output[] = RESULT_FILE_NAME, temp[512];
106 	unsigned long bw_imc[1024], bw_resc[1024];
107 	int runs;
108 	FILE *fp;
109 
110 	fp = fopen(output, "r");
111 	if (!fp) {
112 		perror(output);
113 
114 		return errno;
115 	}
116 
117 	runs = 0;
118 	while (fgets(temp, sizeof(temp), fp)) {
119 		char *token = strtok(temp, ":\t");
120 		int fields = 0;
121 
122 		while (token) {
123 			token_array[fields++] = token;
124 			token = strtok(NULL, ":\t");
125 		}
126 
127 		/* Field 3 is perf imc value */
128 		bw_imc[runs] = strtoul(token_array[3], NULL, 0);
129 		/* Field 5 is resctrl value */
130 		bw_resc[runs] = strtoul(token_array[5], NULL, 0);
131 		runs++;
132 	}
133 
134 	fclose(fp);
135 
136 	return show_mba_info(bw_imc, bw_resc);
137 }
138 
139 void mba_test_cleanup(void)
140 {
141 	remove(RESULT_FILE_NAME);
142 }
143 
144 int mba_schemata_change(int cpu_no, const char * const *benchmark_cmd)
145 {
146 	struct resctrl_val_param param = {
147 		.resctrl_val	= MBA_STR,
148 		.ctrlgrp	= "c1",
149 		.mongrp		= "m1",
150 		.cpu_no		= cpu_no,
151 		.filename	= RESULT_FILE_NAME,
152 		.bw_report	= "reads",
153 		.setup		= mba_setup
154 	};
155 	int ret;
156 
157 	remove(RESULT_FILE_NAME);
158 
159 	ret = resctrl_val(benchmark_cmd, &param);
160 	if (ret)
161 		goto out;
162 
163 	ret = check_results();
164 
165 out:
166 	mba_test_cleanup();
167 
168 	return ret;
169 }
170