selftests/resctrl: Refactor feature check to use resource and feature name
[platform/kernel/linux-starfive.git] / tools / testing / selftests / resctrl / resctrl_tests.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Resctrl tests
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 BENCHMARK_ARGS          64
14 #define BENCHMARK_ARG_SIZE      64
15
16 static int detect_vendor(void)
17 {
18         FILE *inf = fopen("/proc/cpuinfo", "r");
19         int vendor_id = 0;
20         char *s = NULL;
21         char *res;
22
23         if (!inf)
24                 return vendor_id;
25
26         res = fgrep(inf, "vendor_id");
27
28         if (res)
29                 s = strchr(res, ':');
30
31         if (s && !strcmp(s, ": GenuineIntel\n"))
32                 vendor_id = ARCH_INTEL;
33         else if (s && !strcmp(s, ": AuthenticAMD\n"))
34                 vendor_id = ARCH_AMD;
35
36         fclose(inf);
37         free(res);
38         return vendor_id;
39 }
40
41 int get_vendor(void)
42 {
43         static int vendor = -1;
44
45         if (vendor == -1)
46                 vendor = detect_vendor();
47         if (vendor == 0)
48                 ksft_print_msg("Can not get vendor info...\n");
49
50         return vendor;
51 }
52
53 static void cmd_help(void)
54 {
55         printf("usage: resctrl_tests [-h] [-b \"benchmark_cmd [options]\"] [-t test list] [-n no_of_bits]\n");
56         printf("\t-b benchmark_cmd [options]: run specified benchmark for MBM, MBA and CMT\n");
57         printf("\t   default benchmark is builtin fill_buf\n");
58         printf("\t-t test list: run tests specified in the test list, ");
59         printf("e.g. -t mbm,mba,cmt,cat\n");
60         printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n");
61         printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n");
62         printf("\t-h: help\n");
63 }
64
65 void tests_cleanup(void)
66 {
67         mbm_test_cleanup();
68         mba_test_cleanup();
69         cmt_test_cleanup();
70         cat_test_cleanup();
71 }
72
73 static void run_mbm_test(char **benchmark_cmd, size_t span,
74                          int cpu_no, char *bw_report)
75 {
76         int res;
77
78         ksft_print_msg("Starting MBM BW change ...\n");
79
80         res = mount_resctrlfs();
81         if (res) {
82                 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
83                 return;
84         }
85
86         if (!validate_resctrl_feature_request("L3_MON", "mbm_total_bytes") ||
87             !validate_resctrl_feature_request("L3_MON", "mbm_local_bytes") ||
88             (get_vendor() != ARCH_INTEL)) {
89                 ksft_test_result_skip("Hardware does not support MBM or MBM is disabled\n");
90                 goto umount;
91         }
92
93         res = mbm_bw_change(span, cpu_no, bw_report, benchmark_cmd);
94         ksft_test_result(!res, "MBM: bw change\n");
95         if ((get_vendor() == ARCH_INTEL) && res)
96                 ksft_print_msg("Intel MBM may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
97
98 umount:
99         umount_resctrlfs();
100 }
101
102 static void run_mba_test(char **benchmark_cmd, int cpu_no, char *bw_report)
103 {
104         int res;
105
106         ksft_print_msg("Starting MBA Schemata change ...\n");
107
108         res = mount_resctrlfs();
109         if (res) {
110                 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
111                 return;
112         }
113
114         if (!validate_resctrl_feature_request("MB", NULL) || (get_vendor() != ARCH_INTEL)) {
115                 ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n");
116                 goto umount;
117         }
118
119         res = mba_schemata_change(cpu_no, bw_report, benchmark_cmd);
120         ksft_test_result(!res, "MBA: schemata change\n");
121
122 umount:
123         umount_resctrlfs();
124 }
125
126 static void run_cmt_test(char **benchmark_cmd, int cpu_no)
127 {
128         int res;
129
130         ksft_print_msg("Starting CMT test ...\n");
131
132         res = mount_resctrlfs();
133         if (res) {
134                 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
135                 return;
136         }
137
138         if (!validate_resctrl_feature_request("L3_MON", "llc_occupancy")) {
139                 ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n");
140                 goto umount;
141         }
142
143         res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd);
144         ksft_test_result(!res, "CMT: test\n");
145         if ((get_vendor() == ARCH_INTEL) && res)
146                 ksft_print_msg("Intel CMT may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
147
148 umount:
149         umount_resctrlfs();
150 }
151
152 static void run_cat_test(int cpu_no, int no_of_bits)
153 {
154         int res;
155
156         ksft_print_msg("Starting CAT test ...\n");
157
158         res = mount_resctrlfs();
159         if (res) {
160                 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
161                 return;
162         }
163
164         if (!validate_resctrl_feature_request("L3", NULL)) {
165                 ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n");
166                 goto umount;
167         }
168
169         res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
170         ksft_test_result(!res, "CAT: test\n");
171
172 umount:
173         umount_resctrlfs();
174 }
175
176 int main(int argc, char **argv)
177 {
178         bool has_ben = false, mbm_test = true, mba_test = true, cmt_test = true;
179         char *benchmark_cmd[BENCHMARK_ARGS], bw_report[64], bm_type[64];
180         char benchmark_cmd_area[BENCHMARK_ARGS][BENCHMARK_ARG_SIZE];
181         int c, cpu_no = 1, argc_new = argc, i, no_of_bits = 0;
182         int ben_ind, ben_count, tests = 0;
183         size_t span = 250 * MB;
184         bool cat_test = true;
185
186         for (i = 0; i < argc; i++) {
187                 if (strcmp(argv[i], "-b") == 0) {
188                         ben_ind = i + 1;
189                         ben_count = argc - ben_ind;
190                         argc_new = ben_ind - 1;
191                         has_ben = true;
192                         break;
193                 }
194         }
195
196         while ((c = getopt(argc_new, argv, "ht:b:n:p:")) != -1) {
197                 char *token;
198
199                 switch (c) {
200                 case 't':
201                         token = strtok(optarg, ",");
202
203                         mbm_test = false;
204                         mba_test = false;
205                         cmt_test = false;
206                         cat_test = false;
207                         while (token) {
208                                 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
209                                         mbm_test = true;
210                                         tests++;
211                                 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
212                                         mba_test = true;
213                                         tests++;
214                                 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
215                                         cmt_test = true;
216                                         tests++;
217                                 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
218                                         cat_test = true;
219                                         tests++;
220                                 } else {
221                                         printf("invalid argument\n");
222
223                                         return -1;
224                                 }
225                                 token = strtok(NULL, ",");
226                         }
227                         break;
228                 case 'p':
229                         cpu_no = atoi(optarg);
230                         break;
231                 case 'n':
232                         no_of_bits = atoi(optarg);
233                         if (no_of_bits <= 0) {
234                                 printf("Bail out! invalid argument for no_of_bits\n");
235                                 return -1;
236                         }
237                         break;
238                 case 'h':
239                         cmd_help();
240
241                         return 0;
242                 default:
243                         printf("invalid argument\n");
244
245                         return -1;
246                 }
247         }
248
249         ksft_print_header();
250
251         /*
252          * Typically we need root privileges, because:
253          * 1. We write to resctrl FS
254          * 2. We execute perf commands
255          */
256         if (geteuid() != 0)
257                 return ksft_exit_skip("Not running as root. Skipping...\n");
258
259         if (has_ben) {
260                 if (argc - ben_ind >= BENCHMARK_ARGS)
261                         ksft_exit_fail_msg("Too long benchmark command.\n");
262
263                 /* Extract benchmark command from command line. */
264                 for (i = ben_ind; i < argc; i++) {
265                         benchmark_cmd[i - ben_ind] = benchmark_cmd_area[i];
266                         if (strlen(argv[i]) >= BENCHMARK_ARG_SIZE)
267                                 ksft_exit_fail_msg("Too long benchmark command argument.\n");
268                         sprintf(benchmark_cmd[i - ben_ind], "%s", argv[i]);
269                 }
270                 benchmark_cmd[ben_count] = NULL;
271         } else {
272                 /* If no benchmark is given by "-b" argument, use fill_buf. */
273                 for (i = 0; i < 5; i++)
274                         benchmark_cmd[i] = benchmark_cmd_area[i];
275
276                 strcpy(benchmark_cmd[0], "fill_buf");
277                 sprintf(benchmark_cmd[1], "%zu", span);
278                 strcpy(benchmark_cmd[2], "1");
279                 strcpy(benchmark_cmd[3], "0");
280                 strcpy(benchmark_cmd[4], "false");
281                 benchmark_cmd[5] = NULL;
282         }
283
284         sprintf(bw_report, "reads");
285         sprintf(bm_type, "fill_buf");
286
287         if (!check_resctrlfs_support())
288                 return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
289
290         if (umount_resctrlfs())
291                 return ksft_exit_skip("resctrl FS unmount failed.\n");
292
293         filter_dmesg();
294
295         ksft_set_plan(tests ? : 4);
296
297         if (mbm_test)
298                 run_mbm_test(benchmark_cmd, span, cpu_no, bw_report);
299
300         if (mba_test)
301                 run_mba_test(benchmark_cmd, cpu_no, bw_report);
302
303         if (cmt_test)
304                 run_cmt_test(benchmark_cmd, cpu_no);
305
306         if (cat_test)
307                 run_cat_test(cpu_no, no_of_bits);
308
309         ksft_finished();
310 }