2ace464b96d1fdb6a388f9ea746c54b1b805b1eb
[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 int main(int argc, char **argv)
58 {
59         bool has_ben = false, mbm_test = true, mba_test = true, cmt_test = true;
60         int res, c, cpu_no = 1, span = 250, argc_new = argc, i, no_of_bits = 0;
61         char *benchmark_cmd[BENCHMARK_ARGS], bw_report[64], bm_type[64];
62         char benchmark_cmd_area[BENCHMARK_ARGS][BENCHMARK_ARG_SIZE];
63         int ben_ind, ben_count, tests = 0;
64         bool cat_test = true;
65
66         for (i = 0; i < argc; i++) {
67                 if (strcmp(argv[i], "-b") == 0) {
68                         ben_ind = i + 1;
69                         ben_count = argc - ben_ind;
70                         argc_new = ben_ind - 1;
71                         has_ben = true;
72                         break;
73                 }
74         }
75
76         while ((c = getopt(argc_new, argv, "ht:b:n:p:")) != -1) {
77                 char *token;
78
79                 switch (c) {
80                 case 't':
81                         token = strtok(optarg, ",");
82
83                         mbm_test = false;
84                         mba_test = false;
85                         cmt_test = false;
86                         cat_test = false;
87                         while (token) {
88                                 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
89                                         mbm_test = true;
90                                         tests++;
91                                 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
92                                         mba_test = true;
93                                         tests++;
94                                 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
95                                         cmt_test = true;
96                                         tests++;
97                                 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
98                                         cat_test = true;
99                                         tests++;
100                                 } else {
101                                         printf("invalid argument\n");
102
103                                         return -1;
104                                 }
105                                 token = strtok(NULL, ":\t");
106                         }
107                         break;
108                 case 'p':
109                         cpu_no = atoi(optarg);
110                         break;
111                 case 'n':
112                         no_of_bits = atoi(optarg);
113                         if (no_of_bits <= 0) {
114                                 printf("Bail out! invalid argument for no_of_bits\n");
115                                 return -1;
116                         }
117                         break;
118                 case 'h':
119                         cmd_help();
120
121                         return 0;
122                 default:
123                         printf("invalid argument\n");
124
125                         return -1;
126                 }
127         }
128
129         ksft_print_header();
130
131         /*
132          * Typically we need root privileges, because:
133          * 1. We write to resctrl FS
134          * 2. We execute perf commands
135          */
136         if (geteuid() != 0)
137                 return ksft_exit_fail_msg("Not running as root, abort testing.\n");
138
139         /* Detect AMD vendor */
140         detect_amd();
141
142         if (has_ben) {
143                 /* Extract benchmark command from command line. */
144                 for (i = ben_ind; i < argc; i++) {
145                         benchmark_cmd[i - ben_ind] = benchmark_cmd_area[i];
146                         sprintf(benchmark_cmd[i - ben_ind], "%s", argv[i]);
147                 }
148                 benchmark_cmd[ben_count] = NULL;
149         } else {
150                 /* If no benchmark is given by "-b" argument, use fill_buf. */
151                 for (i = 0; i < 6; i++)
152                         benchmark_cmd[i] = benchmark_cmd_area[i];
153
154                 strcpy(benchmark_cmd[0], "fill_buf");
155                 sprintf(benchmark_cmd[1], "%d", span);
156                 strcpy(benchmark_cmd[2], "1");
157                 strcpy(benchmark_cmd[3], "1");
158                 strcpy(benchmark_cmd[4], "0");
159                 strcpy(benchmark_cmd[5], "");
160                 benchmark_cmd[6] = NULL;
161         }
162
163         sprintf(bw_report, "reads");
164         sprintf(bm_type, "fill_buf");
165
166         if (!check_resctrlfs_support())
167                 return ksft_exit_fail_msg("resctrl FS does not exist\n");
168
169         filter_dmesg();
170
171         ksft_set_plan(tests ? : 4);
172
173         if (!is_amd && mbm_test) {
174                 ksft_print_msg("Starting MBM BW change ...\n");
175                 if (!has_ben)
176                         sprintf(benchmark_cmd[5], "%s", MBA_STR);
177                 res = mbm_bw_change(span, cpu_no, bw_report, benchmark_cmd);
178                 ksft_test_result(!res, "MBM: bw change\n");
179                 mbm_test_cleanup();
180         }
181
182         if (!is_amd && mba_test) {
183                 ksft_print_msg("Starting MBA Schemata change ...\n");
184                 if (!has_ben)
185                         sprintf(benchmark_cmd[1], "%d", span);
186                 res = mba_schemata_change(cpu_no, bw_report, benchmark_cmd);
187                 ksft_test_result(!res, "MBA: schemata change\n");
188                 mba_test_cleanup();
189         }
190
191         if (cmt_test) {
192                 ksft_print_msg("Starting CMT test ...\n");
193                 if (!has_ben)
194                         sprintf(benchmark_cmd[5], "%s", CMT_STR);
195                 res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd);
196                 ksft_test_result(!res, "CMT: test\n");
197                 cmt_test_cleanup();
198         }
199
200         if (cat_test) {
201                 ksft_print_msg("Starting CAT test ...\n");
202                 res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
203                 ksft_test_result(!res, "CAT: test\n");
204                 cat_test_cleanup();
205         }
206
207         return ksft_exit_pass();
208 }