e63e0d8764ef00bda519c8f56ac7c98b1969618c
[linux-2.6-microblaze.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 bool is_amd;
17
18 void detect_amd(void)
19 {
20         FILE *inf = fopen("/proc/cpuinfo", "r");
21         char *res;
22
23         if (!inf)
24                 return;
25
26         res = fgrep(inf, "vendor_id");
27
28         if (res) {
29                 char *s = strchr(res, ':');
30
31                 is_amd = s && !strcmp(s, ": AuthenticAMD\n");
32                 free(res);
33         }
34         fclose(inf);
35 }
36
37 static void cmd_help(void)
38 {
39         printf("usage: resctrl_tests [-h] [-b \"benchmark_cmd [options]\"] [-t test list] [-n no_of_bits]\n");
40         printf("\t-b benchmark_cmd [options]: run specified benchmark for MBM, MBA and CMT\n");
41         printf("\t   default benchmark is builtin fill_buf\n");
42         printf("\t-t test list: run tests specified in the test list, ");
43         printf("e.g. -t mbm, mba, cmt, cat\n");
44         printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n");
45         printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n");
46         printf("\t-h: help\n");
47 }
48
49 void tests_cleanup(void)
50 {
51         mbm_test_cleanup();
52         mba_test_cleanup();
53         cmt_test_cleanup();
54         cat_test_cleanup();
55 }
56
57 static void run_mbm_test(bool has_ben, char **benchmark_cmd, int span,
58                          int cpu_no, char *bw_report)
59 {
60         int res;
61
62         ksft_print_msg("Starting MBM BW change ...\n");
63         if (!has_ben)
64                 sprintf(benchmark_cmd[5], "%s", MBA_STR);
65         res = mbm_bw_change(span, cpu_no, bw_report, benchmark_cmd);
66         ksft_test_result(!res, "MBM: bw change\n");
67         mbm_test_cleanup();
68 }
69
70 static void run_mba_test(bool has_ben, char **benchmark_cmd, int span,
71                          int cpu_no, char *bw_report)
72 {
73         int res;
74
75         ksft_print_msg("Starting MBA Schemata change ...\n");
76         if (!has_ben)
77                 sprintf(benchmark_cmd[1], "%d", span);
78         res = mba_schemata_change(cpu_no, bw_report, benchmark_cmd);
79         ksft_test_result(!res, "MBA: schemata change\n");
80         mba_test_cleanup();
81 }
82
83 static void run_cmt_test(bool has_ben, char **benchmark_cmd, int cpu_no)
84 {
85         int res;
86
87         ksft_print_msg("Starting CMT test ...\n");
88         if (!has_ben)
89                 sprintf(benchmark_cmd[5], "%s", CMT_STR);
90         res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd);
91         ksft_test_result(!res, "CMT: test\n");
92         cmt_test_cleanup();
93 }
94
95 static void run_cat_test(int cpu_no, int no_of_bits)
96 {
97         int res;
98
99         ksft_print_msg("Starting CAT test ...\n");
100         res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
101         ksft_test_result(!res, "CAT: test\n");
102         cat_test_cleanup();
103 }
104
105 int main(int argc, char **argv)
106 {
107         bool has_ben = false, mbm_test = true, mba_test = true, cmt_test = true;
108         int c, cpu_no = 1, span = 250, argc_new = argc, i, no_of_bits = 0;
109         char *benchmark_cmd[BENCHMARK_ARGS], bw_report[64], bm_type[64];
110         char benchmark_cmd_area[BENCHMARK_ARGS][BENCHMARK_ARG_SIZE];
111         int ben_ind, ben_count, tests = 0;
112         bool cat_test = true;
113
114         for (i = 0; i < argc; i++) {
115                 if (strcmp(argv[i], "-b") == 0) {
116                         ben_ind = i + 1;
117                         ben_count = argc - ben_ind;
118                         argc_new = ben_ind - 1;
119                         has_ben = true;
120                         break;
121                 }
122         }
123
124         while ((c = getopt(argc_new, argv, "ht:b:n:p:")) != -1) {
125                 char *token;
126
127                 switch (c) {
128                 case 't':
129                         token = strtok(optarg, ",");
130
131                         mbm_test = false;
132                         mba_test = false;
133                         cmt_test = false;
134                         cat_test = false;
135                         while (token) {
136                                 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
137                                         mbm_test = true;
138                                         tests++;
139                                 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
140                                         mba_test = true;
141                                         tests++;
142                                 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
143                                         cmt_test = true;
144                                         tests++;
145                                 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
146                                         cat_test = true;
147                                         tests++;
148                                 } else {
149                                         printf("invalid argument\n");
150
151                                         return -1;
152                                 }
153                                 token = strtok(NULL, ":\t");
154                         }
155                         break;
156                 case 'p':
157                         cpu_no = atoi(optarg);
158                         break;
159                 case 'n':
160                         no_of_bits = atoi(optarg);
161                         if (no_of_bits <= 0) {
162                                 printf("Bail out! invalid argument for no_of_bits\n");
163                                 return -1;
164                         }
165                         break;
166                 case 'h':
167                         cmd_help();
168
169                         return 0;
170                 default:
171                         printf("invalid argument\n");
172
173                         return -1;
174                 }
175         }
176
177         ksft_print_header();
178
179         /*
180          * Typically we need root privileges, because:
181          * 1. We write to resctrl FS
182          * 2. We execute perf commands
183          */
184         if (geteuid() != 0)
185                 return ksft_exit_fail_msg("Not running as root, abort testing.\n");
186
187         /* Detect AMD vendor */
188         detect_amd();
189
190         if (has_ben) {
191                 /* Extract benchmark command from command line. */
192                 for (i = ben_ind; i < argc; i++) {
193                         benchmark_cmd[i - ben_ind] = benchmark_cmd_area[i];
194                         sprintf(benchmark_cmd[i - ben_ind], "%s", argv[i]);
195                 }
196                 benchmark_cmd[ben_count] = NULL;
197         } else {
198                 /* If no benchmark is given by "-b" argument, use fill_buf. */
199                 for (i = 0; i < 6; i++)
200                         benchmark_cmd[i] = benchmark_cmd_area[i];
201
202                 strcpy(benchmark_cmd[0], "fill_buf");
203                 sprintf(benchmark_cmd[1], "%d", span);
204                 strcpy(benchmark_cmd[2], "1");
205                 strcpy(benchmark_cmd[3], "1");
206                 strcpy(benchmark_cmd[4], "0");
207                 strcpy(benchmark_cmd[5], "");
208                 benchmark_cmd[6] = NULL;
209         }
210
211         sprintf(bw_report, "reads");
212         sprintf(bm_type, "fill_buf");
213
214         if (!check_resctrlfs_support())
215                 return ksft_exit_fail_msg("resctrl FS does not exist\n");
216
217         filter_dmesg();
218
219         ksft_set_plan(tests ? : 4);
220
221         if (!is_amd && mbm_test)
222                 run_mbm_test(has_ben, benchmark_cmd, span, cpu_no, bw_report);
223
224         if (!is_amd && mba_test)
225                 run_mba_test(has_ben, benchmark_cmd, span, cpu_no, bw_report);
226
227         if (cmt_test)
228                 run_cmt_test(has_ben, benchmark_cmd, cpu_no);
229
230         if (cat_test)
231                 run_cat_test(cpu_no, no_of_bits);
232
233         return ksft_exit_pass();
234 }