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, int cpu_no)
77 ksft_print_msg("Starting MBM BW change ...\n");
79 res = mount_resctrlfs();
81 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
85 if (!validate_resctrl_feature_request("L3_MON", "mbm_total_bytes") ||
86 !validate_resctrl_feature_request("L3_MON", "mbm_local_bytes") ||
87 (get_vendor() != ARCH_INTEL)) {
88 ksft_test_result_skip("Hardware does not support MBM or MBM is disabled\n");
92 res = mbm_bw_change(span, cpu_no, benchmark_cmd);
93 ksft_test_result(!res, "MBM: bw change\n");
94 if ((get_vendor() == ARCH_INTEL) && res)
95 ksft_print_msg("Intel MBM may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
101 static void run_mba_test(char **benchmark_cmd, int cpu_no)
105 ksft_print_msg("Starting MBA Schemata change ...\n");
107 res = mount_resctrlfs();
109 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
113 if (!validate_resctrl_feature_request("MB", NULL) ||
114 !validate_resctrl_feature_request("L3_MON", "mbm_local_bytes") ||
115 (get_vendor() != ARCH_INTEL)) {
116 ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n");
120 res = mba_schemata_change(cpu_no, benchmark_cmd);
121 ksft_test_result(!res, "MBA: schemata change\n");
127 static void run_cmt_test(char **benchmark_cmd, int cpu_no)
131 ksft_print_msg("Starting CMT test ...\n");
133 res = mount_resctrlfs();
135 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
139 if (!validate_resctrl_feature_request("L3_MON", "llc_occupancy") ||
140 !validate_resctrl_feature_request("L3", NULL)) {
141 ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n");
145 res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd);
146 ksft_test_result(!res, "CMT: test\n");
147 if ((get_vendor() == ARCH_INTEL) && res)
148 ksft_print_msg("Intel CMT may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
154 static void run_cat_test(int cpu_no, int no_of_bits)
158 ksft_print_msg("Starting CAT test ...\n");
160 res = mount_resctrlfs();
162 ksft_exit_fail_msg("Failed to mount resctrl FS\n");
166 if (!validate_resctrl_feature_request("L3", NULL)) {
167 ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n");
171 res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
172 ksft_test_result(!res, "CAT: test\n");
178 int main(int argc, char **argv)
180 bool has_ben = false, mbm_test = true, mba_test = true, cmt_test = true;
181 char benchmark_cmd_area[BENCHMARK_ARGS][BENCHMARK_ARG_SIZE];
182 int c, cpu_no = 1, argc_new = argc, i, no_of_bits = 0;
183 char *benchmark_cmd[BENCHMARK_ARGS];
184 int ben_ind, ben_count, tests = 0;
185 size_t span = 250 * MB;
186 bool cat_test = true;
188 for (i = 0; i < argc; i++) {
189 if (strcmp(argv[i], "-b") == 0) {
191 ben_count = argc - ben_ind;
192 argc_new = ben_ind - 1;
198 while ((c = getopt(argc_new, argv, "ht:b:n:p:")) != -1) {
203 token = strtok(optarg, ",");
210 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
213 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
216 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
219 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
223 printf("invalid argument\n");
227 token = strtok(NULL, ",");
231 cpu_no = atoi(optarg);
234 no_of_bits = atoi(optarg);
235 if (no_of_bits <= 0) {
236 printf("Bail out! invalid argument for no_of_bits\n");
245 printf("invalid argument\n");
254 * Typically we need root privileges, because:
255 * 1. We write to resctrl FS
256 * 2. We execute perf commands
259 return ksft_exit_skip("Not running as root. Skipping...\n");
262 if (argc - ben_ind >= BENCHMARK_ARGS)
263 ksft_exit_fail_msg("Too long benchmark command.\n");
265 /* Extract benchmark command from command line. */
266 for (i = ben_ind; i < argc; i++) {
267 benchmark_cmd[i - ben_ind] = benchmark_cmd_area[i];
268 if (strlen(argv[i]) >= BENCHMARK_ARG_SIZE)
269 ksft_exit_fail_msg("Too long benchmark command argument.\n");
270 sprintf(benchmark_cmd[i - ben_ind], "%s", argv[i]);
272 benchmark_cmd[ben_count] = NULL;
274 /* If no benchmark is given by "-b" argument, use fill_buf. */
275 for (i = 0; i < 5; i++)
276 benchmark_cmd[i] = benchmark_cmd_area[i];
278 strcpy(benchmark_cmd[0], "fill_buf");
279 sprintf(benchmark_cmd[1], "%zu", span);
280 strcpy(benchmark_cmd[2], "1");
281 strcpy(benchmark_cmd[3], "0");
282 strcpy(benchmark_cmd[4], "false");
283 benchmark_cmd[5] = NULL;
286 if (!check_resctrlfs_support())
287 return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
289 if (umount_resctrlfs())
290 return ksft_exit_skip("resctrl FS unmount failed.\n");
294 ksft_set_plan(tests ? : 4);
297 run_mbm_test(benchmark_cmd, span, cpu_no);
300 run_mba_test(benchmark_cmd, cpu_no);
303 run_cmt_test(benchmark_cmd, cpu_no);
306 run_cat_test(cpu_no, no_of_bits);