1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
// SPDX-License-Identifier: GPL-2.0
/*
* Resctrl tests
*
* Copyright (C) 2018 Intel Corporation
*
* Authors:
* Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
* Fenghua Yu <fenghua.yu@intel.com>
*/
#include "resctrl.h"
/* Volatile memory sink to prevent compiler optimizations */
static volatile int sink_target;
volatile int *value_sink = &sink_target;
static struct resctrl_test *resctrl_tests[] = {
&mbm_test,
&mba_test,
&cmt_test,
&l3_cat_test,
&l3_noncont_cat_test,
&l2_noncont_cat_test,
};
static int detect_vendor(void)
{
FILE *inf = fopen("/proc/cpuinfo", "r");
int vendor_id = 0;
char *s = NULL;
char *res;
if (!inf)
return vendor_id;
res = fgrep(inf, "vendor_id");
if (res)
s = strchr(res, ':');
if (s && !strcmp(s, ": GenuineIntel\n"))
vendor_id = ARCH_INTEL;
else if (s && !strcmp(s, ": AuthenticAMD\n"))
vendor_id = ARCH_AMD;
fclose(inf);
free(res);
return vendor_id;
}
int get_vendor(void)
{
static int vendor = -1;
if (vendor == -1)
vendor = detect_vendor();
if (vendor == 0)
ksft_print_msg("Can not get vendor info...\n");
return vendor;
}
static void cmd_help(void)
{
int i;
printf("usage: resctrl_tests [-h] [-t test list] [-n no_of_bits] [-b benchmark_cmd [option]...]\n");
printf("\t-b benchmark_cmd [option]...: run specified benchmark for MBM, MBA and CMT\n");
printf("\t default benchmark is builtin fill_buf\n");
printf("\t-t test list: run tests/groups specified by the list, ");
printf("e.g. -t mbm,mba,cmt,cat\n");
printf("\t\tSupported tests (group):\n");
for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) {
if (resctrl_tests[i]->group)
printf("\t\t\t%s (%s)\n", resctrl_tests[i]->name, resctrl_tests[i]->group);
else
printf("\t\t\t%s\n", resctrl_tests[i]->name);
}
printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n");
printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n");
printf("\t-h: help\n");
}
static int test_prepare(const struct resctrl_test *test)
{
int res;
res = signal_handler_register(test);
if (res) {
ksft_print_msg("Failed to register signal handler\n");
return res;
}
res = mount_resctrlfs();
if (res) {
signal_handler_unregister();
ksft_print_msg("Failed to mount resctrl FS\n");
return res;
}
return 0;
}
static void test_cleanup(const struct resctrl_test *test)
{
if (test->cleanup)
test->cleanup();
umount_resctrlfs();
signal_handler_unregister();
}
static bool test_vendor_specific_check(const struct resctrl_test *test)
{
if (!test->vendor_specific)
return true;
return get_vendor() & test->vendor_specific;
}
static void run_single_test(const struct resctrl_test *test, const struct user_params *uparams)
{
int ret, snc_mode;
if (test->disabled)
return;
if (!test_vendor_specific_check(test)) {
ksft_test_result_skip("Hardware does not support %s\n", test->name);
return;
}
snc_mode = snc_nodes_per_l3_cache();
ksft_print_msg("Starting %s test ...\n", test->name);
if (snc_mode == 1 && snc_unreliable && get_vendor() == ARCH_INTEL) {
ksft_test_result_skip("SNC detection unreliable due to offline CPUs. Test results may not be accurate if SNC enabled.\n");
return;
}
if (test_prepare(test)) {
ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
return;
}
if (!test->feature_check(test)) {
ksft_test_result_skip("Hardware does not support %s or %s is disabled\n",
test->name, test->name);
goto cleanup;
}
ret = test->run_test(test, uparams);
ksft_test_result(!ret, "%s: test\n", test->name);
cleanup:
test_cleanup(test);
}
/*
* Allocate and initialize a struct fill_buf_param with user provided
* (via "-b fill_buf <fill_buf parameters>") parameters.
*
* Use defaults (that may not be appropriate for all tests) for any
* fill_buf parameters omitted by the user.
*
* Historically it may have been possible for user space to provide
* additional parameters, "operation" ("read" vs "write") in
* benchmark_cmd[3] and "once" (run "once" or until terminated) in
* benchmark_cmd[4]. Changing these parameters have never been
* supported with the default of "read" operation and running until
* terminated built into the tests. Any unsupported values for
* (original) "fill_buf" parameters are treated as failure.
*
* Return: On failure, forcibly exits the test on any parsing failure,
* returns NULL if no parsing needed (user did not actually provide
* "-b fill_buf").
* On success, returns pointer to newly allocated and fully
* initialized struct fill_buf_param that caller must free.
*/
static struct fill_buf_param *alloc_fill_buf_param(struct user_params *uparams)
{
struct fill_buf_param *fill_param = NULL;
char *endptr = NULL;
if (!uparams->benchmark_cmd[0] || strcmp(uparams->benchmark_cmd[0], "fill_buf"))
return NULL;
fill_param = malloc(sizeof(*fill_param));
if (!fill_param)
ksft_exit_skip("Unable to allocate memory for fill_buf parameters.\n");
if (uparams->benchmark_cmd[1] && *uparams->benchmark_cmd[1] != '\0') {
errno = 0;
fill_param->buf_size = strtoul(uparams->benchmark_cmd[1], &endptr, 10);
if (errno || *endptr != '\0') {
free(fill_param);
ksft_exit_skip("Unable to parse benchmark buffer size.\n");
}
} else {
fill_param->buf_size = MINIMUM_SPAN;
}
if (uparams->benchmark_cmd[2] && *uparams->benchmark_cmd[2] != '\0') {
errno = 0;
fill_param->memflush = strtol(uparams->benchmark_cmd[2], &endptr, 10) != 0;
if (errno || *endptr != '\0') {
free(fill_param);
ksft_exit_skip("Unable to parse benchmark memflush parameter.\n");
}
} else {
fill_param->memflush = true;
}
if (uparams->benchmark_cmd[3] && *uparams->benchmark_cmd[3] != '\0') {
if (strcmp(uparams->benchmark_cmd[3], "0")) {
free(fill_param);
ksft_exit_skip("Only read operations supported.\n");
}
}
if (uparams->benchmark_cmd[4] && *uparams->benchmark_cmd[4] != '\0') {
if (strcmp(uparams->benchmark_cmd[4], "false")) {
free(fill_param);
ksft_exit_skip("fill_buf is required to run until termination.\n");
}
}
return fill_param;
}
static void init_user_params(struct user_params *uparams)
{
memset(uparams, 0, sizeof(*uparams));
uparams->cpu = 1;
uparams->bits = 0;
}
int main(int argc, char **argv)
{
struct fill_buf_param *fill_param = NULL;
int tests = ARRAY_SIZE(resctrl_tests);
bool test_param_seen = false;
struct user_params uparams;
int c, i;
init_user_params(&uparams);
while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) {
char *token;
switch (c) {
case 'b':
/*
* First move optind back to the (first) optarg and
* then build the benchmark command using the
* remaining arguments.
*/
optind--;
if (argc - optind >= BENCHMARK_ARGS)
ksft_exit_fail_msg("Too long benchmark command");
/* Extract benchmark command from command line. */
for (i = 0; i < argc - optind; i++)
uparams.benchmark_cmd[i] = argv[i + optind];
uparams.benchmark_cmd[i] = NULL;
goto last_arg;
case 't':
token = strtok(optarg, ",");
if (!test_param_seen) {
for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++)
resctrl_tests[i]->disabled = true;
tests = 0;
test_param_seen = true;
}
while (token) {
bool found = false;
for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) {
if (!strcasecmp(token, resctrl_tests[i]->name) ||
(resctrl_tests[i]->group &&
!strcasecmp(token, resctrl_tests[i]->group))) {
if (resctrl_tests[i]->disabled)
tests++;
resctrl_tests[i]->disabled = false;
found = true;
}
}
if (!found) {
printf("invalid test: %s\n", token);
return -1;
}
token = strtok(NULL, ",");
}
break;
case 'p':
uparams.cpu = atoi(optarg);
break;
case 'n':
uparams.bits = atoi(optarg);
if (uparams.bits <= 0) {
printf("Bail out! invalid argument for no_of_bits\n");
return -1;
}
break;
case 'h':
cmd_help();
return 0;
default:
printf("invalid argument\n");
return -1;
}
}
last_arg:
fill_param = alloc_fill_buf_param(&uparams);
if (fill_param)
uparams.fill_buf = fill_param;
ksft_print_header();
/*
* Typically we need root privileges, because:
* 1. We write to resctrl FS
* 2. We execute perf commands
*/
if (geteuid() != 0)
ksft_exit_skip("Not running as root. Skipping...\n");
if (!check_resctrlfs_support())
ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
if (umount_resctrlfs())
ksft_exit_skip("resctrl FS unmount failed.\n");
filter_dmesg();
ksft_set_plan(tests);
for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++)
run_single_test(resctrl_tests[i], &uparams);
free(fill_param);
ksft_finished();
}
|