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

source code of linux/tools/testing/selftests/resctrl/mba_test.c