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 static int detect_vendor(void)
15 FILE *inf = fopen("/proc/cpuinfo", "r");
23 res = fgrep(inf, "vendor_id");
28 if (s && !strcmp(s, ": GenuineIntel\n"))
29 vendor_id = ARCH_INTEL;
30 else if (s && !strcmp(s, ": AuthenticAMD\n"))
40 static int vendor = -1;
43 vendor = detect_vendor();
45 ksft_print_msg("Can not get vendor info...\n");
50 static void cmd_help(void)
52 printf("usage: resctrl_tests [-h] [-b \"benchmark_cmd [options]\"] [-t test list] [-n no_of_bits]\n");
53 printf("\t-b benchmark_cmd [options]: run specified benchmark for MBM, MBA and CMT\n");
54 printf("\t default benchmark is builtin fill_buf\n");
55 printf("\t-t test list: run tests specified in the test list, ");
56 printf("e.g. -t mbm,mba,cmt,cat\n");
57 printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n");
58 printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n");
59 printf("\t-h: help\n");
62 void tests_cleanup(void)
70 static int test_prepare(void)
74 res = signal_handler_register();
76 ksft_print_msg("Failed to register signal handler\n");
80 res = mount_resctrlfs();
82 signal_handler_unregister();
83 ksft_print_msg("Failed to mount resctrl FS\n");
89 static void test_cleanup(void)
92 signal_handler_unregister();
95 static void run_mbm_test(const char * const *benchmark_cmd, int cpu_no)
99 ksft_print_msg("Starting MBM BW change ...\n");
101 if (test_prepare()) {
102 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
106 if (!validate_resctrl_feature_request("L3_MON", "mbm_total_bytes") ||
107 !validate_resctrl_feature_request("L3_MON", "mbm_local_bytes") ||
108 (get_vendor() != ARCH_INTEL)) {
109 ksft_test_result_skip("Hardware does not support MBM or MBM is disabled\n");
113 res = mbm_bw_change(cpu_no, benchmark_cmd);
114 ksft_test_result(!res, "MBM: bw change\n");
115 if ((get_vendor() == ARCH_INTEL) && res)
116 ksft_print_msg("Intel MBM may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
122 static void run_mba_test(const char * const *benchmark_cmd, int cpu_no)
126 ksft_print_msg("Starting MBA Schemata change ...\n");
128 if (test_prepare()) {
129 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
133 if (!validate_resctrl_feature_request("MB", NULL) ||
134 !validate_resctrl_feature_request("L3_MON", "mbm_local_bytes") ||
135 (get_vendor() != ARCH_INTEL)) {
136 ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n");
140 res = mba_schemata_change(cpu_no, benchmark_cmd);
141 ksft_test_result(!res, "MBA: schemata change\n");
147 static void run_cmt_test(const char * const *benchmark_cmd, int cpu_no)
151 ksft_print_msg("Starting CMT test ...\n");
153 if (test_prepare()) {
154 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
158 if (!validate_resctrl_feature_request("L3_MON", "llc_occupancy") ||
159 !validate_resctrl_feature_request("L3", NULL)) {
160 ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n");
164 res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd);
165 ksft_test_result(!res, "CMT: test\n");
166 if ((get_vendor() == ARCH_INTEL) && res)
167 ksft_print_msg("Intel CMT may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
173 static void run_cat_test(int cpu_no, int no_of_bits)
177 ksft_print_msg("Starting CAT test ...\n");
179 if (test_prepare()) {
180 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
184 if (!validate_resctrl_feature_request("L3", NULL)) {
185 ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n");
189 res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
190 ksft_test_result(!res, "CAT: test\n");
196 int main(int argc, char **argv)
198 bool has_ben = false, mbm_test = true, mba_test = true, cmt_test = true;
199 int c, cpu_no = 1, argc_new = argc, i, no_of_bits = 0;
200 const char *benchmark_cmd[BENCHMARK_ARGS];
201 int ben_ind, ben_count, tests = 0;
202 char *span_str = NULL;
203 bool cat_test = true;
206 for (i = 0; i < argc; i++) {
207 if (strcmp(argv[i], "-b") == 0) {
209 ben_count = argc - ben_ind;
210 argc_new = ben_ind - 1;
216 while ((c = getopt(argc_new, argv, "ht:b:n:p:")) != -1) {
221 token = strtok(optarg, ",");
228 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
231 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
234 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
237 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
241 printf("invalid argument\n");
245 token = strtok(NULL, ",");
249 cpu_no = atoi(optarg);
252 no_of_bits = atoi(optarg);
253 if (no_of_bits <= 0) {
254 printf("Bail out! invalid argument for no_of_bits\n");
263 printf("invalid argument\n");
272 * Typically we need root privileges, because:
273 * 1. We write to resctrl FS
274 * 2. We execute perf commands
277 return ksft_exit_skip("Not running as root. Skipping...\n");
280 if (argc - ben_ind >= BENCHMARK_ARGS)
281 ksft_exit_fail_msg("Too long benchmark command.\n");
283 /* Extract benchmark command from command line. */
284 for (i = 0; i < argc - ben_ind; i++)
285 benchmark_cmd[i] = argv[i + ben_ind];
286 benchmark_cmd[ben_count] = NULL;
288 /* If no benchmark is given by "-b" argument, use fill_buf. */
289 benchmark_cmd[0] = "fill_buf";
290 ret = asprintf(&span_str, "%u", DEFAULT_SPAN);
292 ksft_exit_fail_msg("Out of memory!\n");
293 benchmark_cmd[1] = span_str;
294 benchmark_cmd[2] = "1";
295 benchmark_cmd[3] = "0";
296 benchmark_cmd[4] = "false";
297 benchmark_cmd[5] = NULL;
300 if (!check_resctrlfs_support())
301 return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
303 if (umount_resctrlfs())
304 return ksft_exit_skip("resctrl FS unmount failed.\n");
308 ksft_set_plan(tests ? : 4);
311 run_mbm_test(benchmark_cmd, cpu_no);
314 run_mba_test(benchmark_cmd, cpu_no);
317 run_cmt_test(benchmark_cmd, cpu_no);
320 run_cat_test(cpu_no, no_of_bits);