1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018 Intel Corporation
8 * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
9 * Fenghua Yu <fenghua.yu@intel.com>
13 #define BENCHMARK_ARGS 64
14 #define BENCHMARK_ARG_SIZE 64
16 static int detect_vendor(void)
18 FILE *inf = fopen("/proc/cpuinfo", "r");
26 res = fgrep(inf, "vendor_id");
31 if (s && !strcmp(s, ": GenuineIntel\n"))
32 vendor_id = ARCH_INTEL;
33 else if (s && !strcmp(s, ": AuthenticAMD\n"))
43 static int vendor = -1;
46 vendor = detect_vendor();
48 ksft_print_msg("Can not get vendor info...\n");
53 static void cmd_help(void)
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");
65 void tests_cleanup(void)
73 static void run_mbm_test(char **benchmark_cmd, size_t span,
74 int cpu_no, char *bw_report)
78 ksft_print_msg("Starting MBM BW change ...\n");
80 res = mount_resctrlfs();
82 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
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");
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");
102 static void run_mba_test(char **benchmark_cmd, int cpu_no, char *bw_report)
106 ksft_print_msg("Starting MBA Schemata change ...\n");
108 res = mount_resctrlfs();
110 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
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");
119 res = mba_schemata_change(cpu_no, bw_report, benchmark_cmd);
120 ksft_test_result(!res, "MBA: schemata change\n");
126 static void run_cmt_test(char **benchmark_cmd, int cpu_no)
130 ksft_print_msg("Starting CMT test ...\n");
132 res = mount_resctrlfs();
134 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
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");
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");
152 static void run_cat_test(int cpu_no, int no_of_bits)
156 ksft_print_msg("Starting CAT test ...\n");
158 res = mount_resctrlfs();
160 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
164 if (!validate_resctrl_feature_request("L3", NULL)) {
165 ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n");
169 res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
170 ksft_test_result(!res, "CAT: test\n");
176 int main(int argc, char **argv)
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;
186 for (i = 0; i < argc; i++) {
187 if (strcmp(argv[i], "-b") == 0) {
189 ben_count = argc - ben_ind;
190 argc_new = ben_ind - 1;
196 while ((c = getopt(argc_new, argv, "ht:b:n:p:")) != -1) {
201 token = strtok(optarg, ",");
208 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
211 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
214 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
217 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
221 printf("invalid argument\n");
225 token = strtok(NULL, ",");
229 cpu_no = atoi(optarg);
232 no_of_bits = atoi(optarg);
233 if (no_of_bits <= 0) {
234 printf("Bail out! invalid argument for no_of_bits\n");
243 printf("invalid argument\n");
252 * Typically we need root privileges, because:
253 * 1. We write to resctrl FS
254 * 2. We execute perf commands
257 return ksft_exit_skip("Not running as root. Skipping...\n");
260 if (argc - ben_ind >= BENCHMARK_ARGS)
261 ksft_exit_fail_msg("Too long benchmark command.\n");
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]);
270 benchmark_cmd[ben_count] = NULL;
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];
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;
284 sprintf(bw_report, "reads");
285 sprintf(bm_type, "fill_buf");
287 if (!check_resctrlfs_support())
288 return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
290 if (umount_resctrlfs())
291 return ksft_exit_skip("resctrl FS unmount failed.\n");
295 ksft_set_plan(tests ? : 4);
298 run_mbm_test(benchmark_cmd, span, cpu_no, bw_report);
301 run_mba_test(benchmark_cmd, cpu_no, bw_report);
304 run_cmt_test(benchmark_cmd, cpu_no);
307 run_cat_test(cpu_no, no_of_bits);