~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/tests/builtin-test.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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>
  9 #include <poll.h>
 10 #include <unistd.h>
 11 #include <string.h>
 12 #include <stdlib.h>
 13 #include <sys/types.h>
 14 #include <dirent.h>
 15 #include <sys/wait.h>
 16 #include <sys/stat.h>
 17 #include "builtin.h"
 18 #include "config.h"
 19 #include "hist.h"
 20 #include "intlist.h"
 21 #include "tests.h"
 22 #include "debug.h"
 23 #include "color.h"
 24 #include <subcmd/parse-options.h>
 25 #include <subcmd/run-command.h>
 26 #include "string2.h"
 27 #include "symbol.h"
 28 #include "util/rlimit.h"
 29 #include "util/strbuf.h"
 30 #include <linux/kernel.h>
 31 #include <linux/string.h>
 32 #include <subcmd/exec-cmd.h>
 33 #include <linux/zalloc.h>
 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  */
 41 static bool dont_fork;
 42 /* Don't fork the tests in parallel and wait for their completion. */
 43 static 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 */
 46 static bool parallel;
 47 const char *dso_to_test;
 48 const 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.
 54  */
 55 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
 56 extern struct test_suite *arch_tests[];
 57 #else
 58 static struct test_suite *arch_tests[] = {
 59         NULL,
 60 };
 61 #endif
 62 
 63 static struct test_suite *generic_tests[] = {
 64         &suite__vmlinux_matches_kallsyms,
 65 #ifdef HAVE_LIBTRACEEVENT
 66         &suite__openat_syscall_event,
 67         &suite__openat_syscall_event_on_all_cpus,
 68         &suite__basic_mmap,
 69 #endif
 70         &suite__mem,
 71         &suite__parse_events,
 72         &suite__expr,
 73         &suite__PERF_RECORD,
 74         &suite__pmu,
 75         &suite__pmu_events,
 76         &suite__dso_data,
 77         &suite__perf_evsel__roundtrip_name_test,
 78 #ifdef HAVE_LIBTRACEEVENT
 79         &suite__perf_evsel__tp_sched_test,
 80         &suite__syscall_openat_tp_fields,
 81 #endif
 82         &suite__attr,
 83         &suite__hists_link,
 84         &suite__python_use,
 85         &suite__bp_signal,
 86         &suite__bp_signal_overflow,
 87         &suite__bp_accounting,
 88         &suite__wp,
 89         &suite__task_exit,
 90         &suite__sw_clock_freq,
 91         &suite__code_reading,
 92         &suite__sample_parsing,
 93         &suite__keep_tracking,
 94         &suite__parse_no_sample_id_all,
 95         &suite__hists_filter,
 96         &suite__mmap_thread_lookup,
 97         &suite__thread_maps_share,
 98         &suite__hists_output,
 99         &suite__hists_cumulate,
100 #ifdef HAVE_LIBTRACEEVENT
101         &suite__switch_tracking,
102 #endif
103         &suite__fdarray__filter,
104         &suite__fdarray__add,
105         &suite__kmod_path__parse,
106         &suite__thread_map,
107         &suite__session_topology,
108         &suite__thread_map_synthesize,
109         &suite__thread_map_remove,
110         &suite__cpu_map,
111         &suite__synthesize_stat_config,
112         &suite__synthesize_stat,
113         &suite__synthesize_stat_round,
114         &suite__event_update,
115         &suite__event_times,
116         &suite__backward_ring_buffer,
117         &suite__sdt_event,
118         &suite__is_printable_array,
119         &suite__bitmap_print,
120         &suite__perf_hooks,
121         &suite__unit_number__scnprint,
122         &suite__mem2node,
123         &suite__time_utils,
124         &suite__jit_write_elf,
125         &suite__pfm,
126         &suite__api_io,
127         &suite__maps__merge_in,
128         &suite__demangle_java,
129         &suite__demangle_ocaml,
130         &suite__parse_metric,
131         &suite__pe_file_parsing,
132         &suite__expand_cgroup_events,
133         &suite__perf_time_to_tsc,
134         &suite__dlfilter,
135         &suite__sigtrap,
136         &suite__event_groups,
137         &suite__symbols,
138         &suite__util,
139         NULL,
140 };
141 
142 static struct test_suite **tests[] = {
143         generic_tests,
144         arch_tests,
145         NULL, /* shell tests created at runtime. */
146 };
147 
148 static struct test_workload *workloads[] = {
149         &workload__noploop,
150         &workload__thloop,
151         &workload__leafloop,
152         &workload__sqrtloop,
153         &workload__brstack,
154         &workload__datasym,
155 };
156 
157 static int num_subtests(const struct test_suite *t)
158 {
159         int num;
160 
161         if (!t->test_cases)
162                 return 0;
163 
164         num = 0;
165         while (t->test_cases[num].name)
166                 num++;
167 
168         return num;
169 }
170 
171 static bool has_subtests(const struct test_suite *t)
172 {
173         return num_subtests(t) > 1;
174 }
175 
176 static const char *skip_reason(const struct test_suite *t, int subtest)
177 {
178         if (!t->test_cases)
179                 return NULL;
180 
181         return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason;
182 }
183 
184 static const char *test_description(const struct test_suite *t, int subtest)
185 {
186         if (t->test_cases && subtest >= 0)
187                 return t->test_cases[subtest].desc;
188 
189         return t->desc;
190 }
191 
192 static test_fnptr test_function(const struct test_suite *t, int subtest)
193 {
194         if (subtest <= 0)
195                 return t->test_cases[0].run_case;
196 
197         return t->test_cases[subtest].run_case;
198 }
199 
200 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
201 {
202         int i;
203 
204         if (argc == 0)
205                 return true;
206 
207         for (i = 0; i < argc; ++i) {
208                 char *end;
209                 long nr = strtoul(argv[i], &end, 10);
210 
211                 if (*end == '\0') {
212                         if (nr == curr + 1)
213                                 return true;
214                         continue;
215                 }
216 
217                 if (strcasestr(desc, argv[i]))
218                         return true;
219         }
220 
221         return false;
222 }
223 
224 struct child_test {
225         struct child_process process;
226         struct test_suite *test;
227         int test_num;
228         int subtest;
229 };
230 
231 static int run_test_child(struct child_process *process)
232 {
233         struct child_test *child = container_of(process, struct child_test, process);
234         int err;
235 
236         pr_debug("--- start ---\n");
237         pr_debug("test child forked, pid %d\n", getpid());
238         err = test_function(child->test, child->subtest)(child->test, child->subtest);
239         pr_debug("---- end(%d) ----\n", err);
240         fflush(NULL);
241         return -err;
242 }
243 
244 static int print_test_result(struct test_suite *t, int i, int subtest, int result, int width)
245 {
246         if (has_subtests(t)) {
247                 int subw = width > 2 ? width - 2 : width;
248 
249                 pr_info("%3d.%1d: %-*s:", i + 1, subtest + 1, subw, test_description(t, subtest));
250         } else
251                 pr_info("%3d: %-*s:", i + 1, width, test_description(t, subtest));
252 
253         switch (result) {
254         case TEST_OK:
255                 pr_info(" Ok\n");
256                 break;
257         case TEST_SKIP: {
258                 const char *reason = skip_reason(t, subtest);
259 
260                 if (reason)
261                         color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
262                 else
263                         color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
264         }
265                 break;
266         case TEST_FAIL:
267         default:
268                 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
269                 break;
270         }
271 
272         return 0;
273 }
274 
275 static int finish_test(struct child_test *child_test, int width)
276 {
277         struct test_suite *t = child_test->test;
278         int i = child_test->test_num;
279         int subi = child_test->subtest;
280         int err = child_test->process.err;
281         bool err_done = err <= 0;
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         /*
293          * Busy loop reading from the child's stdout/stderr that are set to be
294          * non-blocking until EOF.
295          */
296         if (!err_done)
297                 fcntl(err, F_SETFL, O_NONBLOCK);
298         if (verbose > 1) {
299                 if (has_subtests(t))
300                         pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));
301                 else
302                         pr_info("%3d: %s:\n", i + 1, test_description(t, -1));
303         }
304         while (!err_done) {
305                 struct pollfd pfds[1] = {
306                         { .fd = err,
307                           .events = POLLIN | POLLERR | POLLHUP | POLLNVAL,
308                         },
309                 };
310                 char buf[512];
311                 ssize_t len;
312 
313                 /* Poll to avoid excessive spinning, timeout set for 100ms. */
314                 poll(pfds, ARRAY_SIZE(pfds), /*timeout=*/100);
315                 if (!err_done && pfds[0].revents) {
316                         errno = 0;
317                         len = read(err, buf, sizeof(buf) - 1);
318 
319                         if (len <= 0) {
320                                 err_done = errno != EAGAIN;
321                         } else {
322                                 buf[len] = '\0';
323                                 if (verbose > 1)
324                                         fprintf(stdout, "%s", buf);
325                                 else
326                                         strbuf_addstr(&err_output, buf);
327                         }
328                 }
329         }
330         /* Clean up child process. */
331         ret = finish_command(&child_test->process);
332         if (verbose == 1 && ret == TEST_FAIL) {
333                 /* Add header for test that was skipped above. */
334                 if (has_subtests(t))
335                         pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi));
336                 else
337                         pr_info("%3d: %s:\n", i + 1, test_description(t, -1));
338                 fprintf(stderr, "%s", err_output.buf);
339         }
340         strbuf_release(&err_output);
341         print_test_result(t, i, subi, ret, width);
342         if (err > 0)
343                 close(err);
344         return 0;
345 }
346 
347 static int start_test(struct test_suite *test, int i, int subi, struct child_test **child,
348                       int width)
349 {
350         int err;
351 
352         *child = NULL;
353         if (dont_fork) {
354                 pr_debug("--- start ---\n");
355                 err = test_function(test, subi)(test, subi);
356                 pr_debug("---- end ----\n");
357                 print_test_result(test, i, subi, err, width);
358                 return 0;
359         }
360 
361         *child = zalloc(sizeof(**child));
362         if (!*child)
363                 return -ENOMEM;
364 
365         (*child)->test = test;
366         (*child)->test_num = i;
367         (*child)->subtest = subi;
368         (*child)->process.pid = -1;
369         (*child)->process.no_stdin = 1;
370         if (verbose <= 0) {
371                 (*child)->process.no_stdout = 1;
372                 (*child)->process.no_stderr = 1;
373         } else {
374                 (*child)->process.stdout_to_stderr = 1;
375                 (*child)->process.out = -1;
376                 (*child)->process.err = -1;
377         }
378         (*child)->process.no_exec_cmd = run_test_child;
379         err = start_command(&(*child)->process);
380         if (err || !sequential)
381                 return  err;
382         return finish_test(*child, width);
383 }
384 
385 #define for_each_test(j, k, t)                                  \
386         for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0)   \
387                 while ((t = tests[j][k++]) != NULL)
388 
389 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
390 {
391         struct test_suite *t;
392         unsigned int j, k;
393         int i = 0;
394         int width = 0;
395         size_t num_tests = 0;
396         struct child_test **child_tests;
397         int child_test_num = 0;
398 
399         for_each_test(j, k, t) {
400                 int len = strlen(test_description(t, -1));
401 
402                 if (width < len)
403                         width = len;
404 
405                 if (has_subtests(t)) {
406                         for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {
407                                 len = strlen(test_description(t, subi));
408                                 if (width < len)
409                                         width = len;
410                                 num_tests++;
411                         }
412                 } else {
413                         num_tests++;
414                 }
415         }
416         child_tests = calloc(num_tests, sizeof(*child_tests));
417         if (!child_tests)
418                 return -ENOMEM;
419 
420         for_each_test(j, k, t) {
421                 int curr = i++;
422 
423                 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
424                         bool skip = true;
425 
426                         for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {
427                                 if (perf_test__matches(test_description(t, subi),
428                                                         curr, argc, argv))
429                                         skip = false;
430                         }
431 
432                         if (skip)
433                                 continue;
434                 }
435 
436                 if (intlist__find(skiplist, i)) {
437                         pr_info("%3d: %-*s:", curr + 1, width, test_description(t, -1));
438                         color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
439                         continue;
440                 }
441 
442                 if (!has_subtests(t)) {
443                         int err = start_test(t, curr, -1, &child_tests[child_test_num++], width);
444 
445                         if (err) {
446                                 /* TODO: if !sequential waitpid the already forked children. */
447                                 free(child_tests);
448                                 return err;
449                         }
450                 } else {
451                         for (int subi = 0, subn = num_subtests(t); subi < subn; subi++) {
452                                 int err;
453 
454                                 if (!perf_test__matches(test_description(t, subi),
455                                                         curr, argc, argv))
456                                         continue;
457 
458                                 err = start_test(t, curr, subi, &child_tests[child_test_num++],
459                                                  width);
460                                 if (err)
461                                         return err;
462                         }
463                 }
464         }
465         for (i = 0; i < child_test_num; i++) {
466                 if (!sequential) {
467                         int ret  = finish_test(child_tests[i], width);
468 
469                         if (ret)
470                                 return ret;
471                 }
472                 free(child_tests[i]);
473         }
474         free(child_tests);
475         return 0;
476 }
477 
478 static int perf_test__list(int argc, const char **argv)
479 {
480         unsigned int j, k;
481         struct test_suite *t;
482         int i = 0;
483 
484         for_each_test(j, k, t) {
485                 int curr = i++;
486 
487                 if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
488                         continue;
489 
490                 pr_info("%3d: %s\n", i, test_description(t, -1));
491 
492                 if (has_subtests(t)) {
493                         int subn = num_subtests(t);
494                         int subi;
495 
496                         for (subi = 0; subi < subn; subi++)
497                                 pr_info("%3d:%1d: %s\n", i, subi + 1,
498                                         test_description(t, subi));
499                 }
500         }
501         return 0;
502 }
503 
504 static int run_workload(const char *work, int argc, const char **argv)
505 {
506         unsigned int i = 0;
507         struct test_workload *twl;
508 
509         for (i = 0; i < ARRAY_SIZE(workloads); i++) {
510                 twl = workloads[i];
511                 if (!strcmp(twl->name, work))
512                         return twl->func(argc, argv);
513         }
514 
515         pr_info("No workload found: %s\n", work);
516         return -1;
517 }
518 
519 static int perf_test__config(const char *var, const char *value,
520                              void *data __maybe_unused)
521 {
522         if (!strcmp(var, "annotate.objdump"))
523                 test_objdump_path = value;
524 
525         return 0;
526 }
527 
528 int cmd_test(int argc, const char **argv)
529 {
530         const char *test_usage[] = {
531         "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
532         NULL,
533         };
534         const char *skip = NULL;
535         const char *workload = NULL;
536         const struct option test_options[] = {
537         OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
538         OPT_INCR('v', "verbose", &verbose,
539                     "be more verbose (show symbol address, etc)"),
540         OPT_BOOLEAN('F', "dont-fork", &dont_fork,
541                     "Do not fork for testcase"),
542         OPT_BOOLEAN('p', "parallel", &parallel, "Run the tests in parallel"),
543         OPT_BOOLEAN('S', "sequential", &sequential,
544                     "Run the tests one after another rather than in parallel"),
545         OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"),
546         OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
547         OPT_STRING(0, "objdump", &test_objdump_path, "path",
548                    "objdump binary to use for disassembly and annotations"),
549         OPT_END()
550         };
551         const char * const test_subcommands[] = { "list", NULL };
552         struct intlist *skiplist = NULL;
553         int ret = hists__init();
554 
555         if (ret < 0)
556                 return ret;
557 
558         perf_config(perf_test__config, NULL);
559 
560         /* Unbuffered output */
561         setvbuf(stdout, NULL, _IONBF, 0);
562 
563         tests[2] = create_script_test_suites();
564         argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
565         if (argc >= 1 && !strcmp(argv[0], "list"))
566                 return perf_test__list(argc - 1, argv + 1);
567 
568         if (workload)
569                 return run_workload(workload, argc, argv);
570 
571         if (dont_fork)
572                 sequential = true;
573         else if (parallel)
574                 sequential = false;
575 
576         symbol_conf.priv_size = sizeof(int);
577         symbol_conf.try_vmlinux_path = true;
578 
579         if (symbol__init(NULL) < 0)
580                 return -1;
581 
582         if (skip != NULL)
583                 skiplist = intlist__new(skip);
584         /*
585          * Tests that create BPF maps, for instance, need more than the 64K
586          * default:
587          */
588         rlimit__bump_memlock();
589 
590         return __cmd_test(argc, argv, skiplist);
591 }
592 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php