builtin-test.c (a23e1966932464e1c5226cb9ac4ce1d5fc10ba22) builtin-test.c (3656e566cf03ab0f959b2bd6f8274ee9799641e6)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * builtin-test.c
4 *
5 * Builtin regression testing command: ever growing number of sanity tests
6 */
7#include <fcntl.h>
8#include <errno.h>

--- 25 unchanged lines hidden (view full) ---

34
35#include "tests-scripts.h"
36
37/*
38 * Command line option to not fork the test running in the same process and
39 * making them easier to debug.
40 */
41static bool dont_fork;
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * builtin-test.c
4 *
5 * Builtin regression testing command: ever growing number of sanity tests
6 */
7#include <fcntl.h>
8#include <errno.h>

--- 25 unchanged lines hidden (view full) ---

34
35#include "tests-scripts.h"
36
37/*
38 * Command line option to not fork the test running in the same process and
39 * making them easier to debug.
40 */
41static bool dont_fork;
42/* Fork the tests in parallel and then wait for their completion. */
42/* Don't fork the tests in parallel and wait for their completion. */
43static bool sequential = true;
44/* Do it in parallel, lacks infrastructure to avoid running tests that clash for resources,
45 * So leave it as the developers choice to enable while working on the needed infra */
43static bool parallel;
44const char *dso_to_test;
45const char *test_objdump_path = "objdump";
46
47/*
48 * List of architecture specific tests. Not a weak symbol as the array length is
49 * dependent on the initialization, as such GCC with LTO complains of
50 * conflicting definitions with a weak symbol.

--- 93 unchanged lines hidden (view full) ---

144
145static struct test_workload *workloads[] = {
146 &workload__noploop,
147 &workload__thloop,
148 &workload__leafloop,
149 &workload__sqrtloop,
150 &workload__brstack,
151 &workload__datasym,
46static bool parallel;
47const char *dso_to_test;
48const char *test_objdump_path = "objdump";
49
50/*
51 * List of architecture specific tests. Not a weak symbol as the array length is
52 * dependent on the initialization, as such GCC with LTO complains of
53 * conflicting definitions with a weak symbol.

--- 93 unchanged lines hidden (view full) ---

147
148static struct test_workload *workloads[] = {
149 &workload__noploop,
150 &workload__thloop,
151 &workload__leafloop,
152 &workload__sqrtloop,
153 &workload__brstack,
154 &workload__datasym,
155 &workload__landlock,
152};
153
154static int num_subtests(const struct test_suite *t)
155{
156 int num;
157
158 if (!t->test_cases)
159 return 0;

--- 109 unchanged lines hidden (view full) ---

269 return 0;
270}
271
272static int finish_test(struct child_test *child_test, int width)
273{
274 struct test_suite *t = child_test->test;
275 int i = child_test->test_num;
276 int subi = child_test->subtest;
156};
157
158static int num_subtests(const struct test_suite *t)
159{
160 int num;
161
162 if (!t->test_cases)
163 return 0;

--- 109 unchanged lines hidden (view full) ---

273 return 0;
274}
275
276static int finish_test(struct child_test *child_test, int width)
277{
278 struct test_suite *t = child_test->test;
279 int i = child_test->test_num;
280 int subi = child_test->subtest;
277 int out = child_test->process.out;
278 int err = child_test->process.err;
281 int err = child_test->process.err;
279 bool out_done = out <= 0;
280 bool err_done = err <= 0;
282 bool err_done = err <= 0;
281 struct strbuf out_output = STRBUF_INIT;
282 struct strbuf err_output = STRBUF_INIT;
283 int ret;
284
285 /*
286 * For test suites with subtests, display the suite name ahead of the
287 * sub test names.
288 */
289 if (has_subtests(t) && subi == 0)
290 pr_info("%3d: %-*s:\n", i + 1, width, test_description(t, -1));
291
292 /*
283 struct strbuf err_output = STRBUF_INIT;
284 int ret;
285
286 /*
287 * For test suites with subtests, display the suite name ahead of the
288 * sub test names.
289 */
290 if (has_subtests(t) && subi == 0)
291 pr_info("%3d: %-*s:\n", i + 1, width, test_description(t, -1));
292
293 /*
293 * Busy loop reading from the child's stdout and stderr that are set to
294 * be non-blocking until EOF.
294 * Busy loop reading from the child's stdout/stderr that are set to be
295 * non-blocking until EOF.
295 */
296 */
296 if (!out_done)
297 fcntl(out, F_SETFL, O_NONBLOCK);
298 if (!err_done)
299 fcntl(err, F_SETFL, O_NONBLOCK);
300 if (verbose > 1) {
301 if (has_subtests(t))
302 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));
303 else
304 pr_info("%3d: %s:\n", i + 1, test_description(t, -1));
305 }
297 if (!err_done)
298 fcntl(err, F_SETFL, O_NONBLOCK);
299 if (verbose > 1) {
300 if (has_subtests(t))
301 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));
302 else
303 pr_info("%3d: %s:\n", i + 1, test_description(t, -1));
304 }
306 while (!out_done || !err_done) {
307 struct pollfd pfds[2] = {
308 { .fd = out,
309 .events = POLLIN | POLLERR | POLLHUP | POLLNVAL,
310 },
305 while (!err_done) {
306 struct pollfd pfds[1] = {
311 { .fd = err,
312 .events = POLLIN | POLLERR | POLLHUP | POLLNVAL,
313 },
314 };
315 char buf[512];
316 ssize_t len;
317
307 { .fd = err,
308 .events = POLLIN | POLLERR | POLLHUP | POLLNVAL,
309 },
310 };
311 char buf[512];
312 ssize_t len;
313
318 /* Poll to avoid excessive spinning, timeout set for 1000ms. */
319 poll(pfds, ARRAY_SIZE(pfds), /*timeout=*/1000);
320 if (!out_done && pfds[0].revents) {
314 /* Poll to avoid excessive spinning, timeout set for 100ms. */
315 poll(pfds, ARRAY_SIZE(pfds), /*timeout=*/100);
316 if (!err_done && pfds[0].revents) {
321 errno = 0;
317 errno = 0;
322 len = read(out, buf, sizeof(buf) - 1);
323
324 if (len <= 0) {
325 out_done = errno != EAGAIN;
326 } else {
327 buf[len] = '\0';
328 if (verbose > 1)
329 fprintf(stdout, "%s", buf);
330 else
331 strbuf_addstr(&out_output, buf);
332 }
333 }
334 if (!err_done && pfds[1].revents) {
335 errno = 0;
336 len = read(err, buf, sizeof(buf) - 1);
337
338 if (len <= 0) {
339 err_done = errno != EAGAIN;
340 } else {
341 buf[len] = '\0';
342 if (verbose > 1)
343 fprintf(stdout, "%s", buf);

--- 5 unchanged lines hidden (view full) ---

349 /* Clean up child process. */
350 ret = finish_command(&child_test->process);
351 if (verbose == 1 && ret == TEST_FAIL) {
352 /* Add header for test that was skipped above. */
353 if (has_subtests(t))
354 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));
355 else
356 pr_info("%3d: %s:\n", i + 1, test_description(t, -1));
318 len = read(err, buf, sizeof(buf) - 1);
319
320 if (len <= 0) {
321 err_done = errno != EAGAIN;
322 } else {
323 buf[len] = '\0';
324 if (verbose > 1)
325 fprintf(stdout, "%s", buf);

--- 5 unchanged lines hidden (view full) ---

331 /* Clean up child process. */
332 ret = finish_command(&child_test->process);
333 if (verbose == 1 && ret == TEST_FAIL) {
334 /* Add header for test that was skipped above. */
335 if (has_subtests(t))
336 pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));
337 else
338 pr_info("%3d: %s:\n", i + 1, test_description(t, -1));
357 fprintf(stdout, "%s", out_output.buf);
358 fprintf(stderr, "%s", err_output.buf);
359 }
339 fprintf(stderr, "%s", err_output.buf);
340 }
360 strbuf_release(&out_output);
361 strbuf_release(&err_output);
362 print_test_result(t, i, subi, ret, width);
341 strbuf_release(&err_output);
342 print_test_result(t, i, subi, ret, width);
363 if (out > 0)
364 close(out);
365 if (err > 0)
366 close(err);
367 return 0;
368}
369
370static int start_test(struct test_suite *test, int i, int subi, struct child_test **child,
371 int width)
372{

--- 16 unchanged lines hidden (view full) ---

389 (*child)->test_num = i;
390 (*child)->subtest = subi;
391 (*child)->process.pid = -1;
392 (*child)->process.no_stdin = 1;
393 if (verbose <= 0) {
394 (*child)->process.no_stdout = 1;
395 (*child)->process.no_stderr = 1;
396 } else {
343 if (err > 0)
344 close(err);
345 return 0;
346}
347
348static int start_test(struct test_suite *test, int i, int subi, struct child_test **child,
349 int width)
350{

--- 16 unchanged lines hidden (view full) ---

367 (*child)->test_num = i;
368 (*child)->subtest = subi;
369 (*child)->process.pid = -1;
370 (*child)->process.no_stdin = 1;
371 if (verbose <= 0) {
372 (*child)->process.no_stdout = 1;
373 (*child)->process.no_stderr = 1;
374 } else {
375 (*child)->process.stdout_to_stderr = 1;
397 (*child)->process.out = -1;
398 (*child)->process.err = -1;
399 }
400 (*child)->process.no_exec_cmd = run_test_child;
401 err = start_command(&(*child)->process);
376 (*child)->process.out = -1;
377 (*child)->process.err = -1;
378 }
379 (*child)->process.no_exec_cmd = run_test_child;
380 err = start_command(&(*child)->process);
402 if (err || parallel)
381 if (err || !sequential)
403 return err;
404 return finish_test(*child, width);
405}
406
407#define for_each_test(j, k, t) \
408 for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \
409 while ((t = tests[j][k++]) != NULL)
410

--- 49 unchanged lines hidden (view full) ---

460 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
461 continue;
462 }
463
464 if (!has_subtests(t)) {
465 int err = start_test(t, curr, -1, &child_tests[child_test_num++], width);
466
467 if (err) {
382 return err;
383 return finish_test(*child, width);
384}
385
386#define for_each_test(j, k, t) \
387 for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \
388 while ((t = tests[j][k++]) != NULL)
389

--- 49 unchanged lines hidden (view full) ---

439 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
440 continue;
441 }
442
443 if (!has_subtests(t)) {
444 int err = start_test(t, curr, -1, &child_tests[child_test_num++], width);
445
446 if (err) {
468 /* TODO: if parallel waitpid the already forked children. */
447 /* TODO: if !sequential waitpid the already forked children. */
469 free(child_tests);
470 return err;
471 }
472 } else {
473 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {
474 int err;
475
476 if (!perf_test__matches(test_description(t, subi),
477 curr, argc, argv))
478 continue;
479
480 err = start_test(t, curr, subi, &child_tests[child_test_num++],
481 width);
482 if (err)
483 return err;
484 }
485 }
486 }
487 for (i = 0; i < child_test_num; i++) {
448 free(child_tests);
449 return err;
450 }
451 } else {
452 for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {
453 int err;
454
455 if (!perf_test__matches(test_description(t, subi),
456 curr, argc, argv))
457 continue;
458
459 err = start_test(t, curr, subi, &child_tests[child_test_num++],
460 width);
461 if (err)
462 return err;
463 }
464 }
465 }
466 for (i = 0; i < child_test_num; i++) {
488 if (parallel) {
467 if (!sequential) {
489 int ret = finish_test(child_tests[i], width);
490
491 if (ret)
492 return ret;
493 }
494 free(child_tests[i]);
495 }
496 free(child_tests);

--- 59 unchanged lines hidden (view full) ---

556 const char *skip = NULL;
557 const char *workload = NULL;
558 const struct option test_options[] = {
559 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
560 OPT_INCR('v', "verbose", &verbose,
561 "be more verbose (show symbol address, etc)"),
562 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
563 "Do not fork for testcase"),
468 int ret = finish_test(child_tests[i], width);
469
470 if (ret)
471 return ret;
472 }
473 free(child_tests[i]);
474 }
475 free(child_tests);

--- 59 unchanged lines hidden (view full) ---

535 const char *skip = NULL;
536 const char *workload = NULL;
537 const struct option test_options[] = {
538 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
539 OPT_INCR('v', "verbose", &verbose,
540 "be more verbose (show symbol address, etc)"),
541 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
542 "Do not fork for testcase"),
564 OPT_BOOLEAN('p', "parallel", &parallel,
565 "Run the tests altogether in parallel"),
543 OPT_BOOLEAN('p', "parallel", &parallel, "Run the tests in parallel"),
544 OPT_BOOLEAN('S', "sequential", &sequential,
545 "Run the tests one after another rather than in parallel"),
566 OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"),
567 OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
568 OPT_STRING(0, "objdump", &test_objdump_path, "path",
569 "objdump binary to use for disassembly and annotations"),
570 OPT_END()
571 };
572 const char * const test_subcommands[] = { "list", NULL };
573 struct intlist *skiplist = NULL;

--- 10 unchanged lines hidden (view full) ---

584 tests[2] = create_script_test_suites();
585 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
586 if (argc >= 1 && !strcmp(argv[0], "list"))
587 return perf_test__list(argc - 1, argv + 1);
588
589 if (workload)
590 return run_workload(workload, argc, argv);
591
546 OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"),
547 OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
548 OPT_STRING(0, "objdump", &test_objdump_path, "path",
549 "objdump binary to use for disassembly and annotations"),
550 OPT_END()
551 };
552 const char * const test_subcommands[] = { "list", NULL };
553 struct intlist *skiplist = NULL;

--- 10 unchanged lines hidden (view full) ---

564 tests[2] = create_script_test_suites();
565 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
566 if (argc >= 1 && !strcmp(argv[0], "list"))
567 return perf_test__list(argc - 1, argv + 1);
568
569 if (workload)
570 return run_workload(workload, argc, argv);
571
572 if (dont_fork)
573 sequential = true;
574 else if (parallel)
575 sequential = false;
576
592 symbol_conf.priv_size = sizeof(int);
593 symbol_conf.try_vmlinux_path = true;
594
595 if (symbol__init(NULL) < 0)
596 return -1;
597
598 if (skip != NULL)
599 skiplist = intlist__new(skip);
600 /*
601 * Tests that create BPF maps, for instance, need more than the 64K
602 * default:
603 */
604 rlimit__bump_memlock();
605
606 return __cmd_test(argc, argv, skiplist);
607}
577 symbol_conf.priv_size = sizeof(int);
578 symbol_conf.try_vmlinux_path = true;
579
580 if (symbol__init(NULL) < 0)
581 return -1;
582
583 if (skip != NULL)
584 skiplist = intlist__new(skip);
585 /*
586 * Tests that create BPF maps, for instance, need more than the 64K
587 * default:
588 */
589 rlimit__bump_memlock();
590
591 return __cmd_test(argc, argv, skiplist);
592}