blob: 41e3cf6bb66c68e80320ce96df70ae6e228e5638 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03002/*
3 * builtin-test.c
4 *
5 * Builtin regression testing command: ever growing number of sanity tests
6 */
Arnaldo Carvalho de Meloc23c2a02017-09-11 10:50:26 -03007#include <fcntl.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03008#include <errno.h>
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +02009#include <unistd.h>
10#include <string.h>
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030011#include <stdlib.h>
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -030012#include <sys/types.h>
13#include <dirent.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030014#include <sys/wait.h>
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -030015#include <sys/stat.h>
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030016#include "builtin.h"
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -030017#include "hist.h"
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -030018#include "intlist.h"
Jiri Olsa0a4e1ae2012-11-10 01:46:41 +010019#include "tests.h"
Jiri Olsac81251e2012-11-10 01:46:51 +010020#include "debug.h"
21#include "color.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060022#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -030023#include "string2.h"
Jiri Olsac81251e2012-11-10 01:46:51 +010024#include "symbol.h"
Arnaldo Carvalho de Melod3280ce2019-07-09 16:27:01 -030025#include "util/rlimit.h"
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030026#include <linux/kernel.h>
Arnaldo Carvalho de Melo3ca43b62019-06-26 12:06:20 -030027#include <linux/string.h>
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -030028#include <subcmd/exec-cmd.h>
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -020029
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +020030static bool dont_fork;
31
Matt Fleming31b67532015-10-05 15:40:19 +010032struct test __weak arch_tests[] = {
33 {
34 .func = NULL,
35 },
36};
37
38static struct test generic_tests[] = {
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030039 {
40 .desc = "vmlinux symtab matches kallsyms",
41 .func = test__vmlinux_matches_kallsyms,
42 },
43 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030044 .desc = "Detect openat syscall event",
Riku Voipio43f322b2015-04-16 16:52:53 +030045 .func = test__openat_syscall_event,
Arnaldo Carvalho de Melod8548612011-01-04 00:16:20 -020046 },
47 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030048 .desc = "Detect openat syscall event on all cpus",
Riku Voipio43f322b2015-04-16 16:52:53 +030049 .func = test__openat_syscall_event_on_all_cpus,
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -020050 },
51 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030052 .desc = "Read samples using the mmap interface",
Arnaldo Carvalho de Melode5fa3a2011-01-15 10:42:46 -020053 .func = test__basic_mmap,
54 },
55 {
Andi Kleen3067eaa2017-08-16 15:21:56 -070056 .desc = "Test data source output",
57 .func = test__mem,
58 },
59 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030060 .desc = "Parse event definition strings",
Jiri Olsac81251e2012-11-10 01:46:51 +010061 .func = test__parse_events,
Jiri Olsa13b62562011-07-14 11:25:33 +020062 },
63 {
Andi Kleen07516732017-03-20 13:17:05 -070064 .desc = "Simple expression parser",
65 .func = test__expr,
66 },
67 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030068 .desc = "PERF_RECORD_* events & perf_sample fields",
Arnaldo Carvalho de Melo3e7c4392011-12-02 11:13:50 -020069 .func = test__PERF_RECORD,
70 },
71 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030072 .desc = "Parse perf pmu format",
Jiri Olsacff7f952012-11-10 01:46:50 +010073 .func = test__pmu,
Jiri Olsacd82a322012-03-15 20:09:17 +010074 },
75 {
John Garrya6c925f2020-03-17 19:02:16 +080076 .desc = "PMU events",
77 .func = test__pmu_events,
Ian Rogers06392aa2020-05-13 14:29:33 -070078 .subtest = {
79 .skip_if_fail = false,
80 .get_nr = test__pmu_events_subtest_get_nr,
81 .get_desc = test__pmu_events_subtest_get_desc,
82 .skip_reason = test__pmu_events_subtest_skip_reason,
83 },
84
John Garrya6c925f2020-03-17 19:02:16 +080085 },
86 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030087 .desc = "DSO data read",
Jiri Olsac81251e2012-11-10 01:46:51 +010088 .func = test__dso_data,
Jiri Olsaf7add552012-07-22 14:14:40 +020089 },
90 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030091 .desc = "DSO data cache",
Jiri Olsa4ebbcb82014-05-12 14:43:53 +020092 .func = test__dso_data_cache,
93 },
94 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030095 .desc = "DSO data reopen",
Jiri Olsa45dc1bb52014-05-12 14:50:03 +020096 .func = test__dso_data_reopen,
97 },
98 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -030099 .desc = "Roundtrip evsel->name",
Jiri Olsacfffae22012-11-10 01:46:47 +0100100 .func = test__perf_evsel__roundtrip_name_test,
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300101 },
102 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300103 .desc = "Parse sched tracepoints fields",
Jiri Olsa5e24a092012-11-10 01:46:48 +0100104 .func = test__perf_evsel__tp_sched_test,
Arnaldo Carvalho de Melo6a6cd112012-09-18 11:56:28 -0300105 },
106 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300107 .desc = "syscalls:sys_enter_openat event fields",
Riku Voipio43f322b2015-04-16 16:52:53 +0300108 .func = test__syscall_openat_tp_fields,
Arnaldo Carvalho de Meloeb2f2702012-09-26 13:23:10 -0300109 },
110 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300111 .desc = "Setup struct perf_event_attr",
Jiri Olsac81251e2012-11-10 01:46:51 +0100112 .func = test__attr,
Jiri Olsad898b242012-10-30 23:02:05 +0100113 },
114 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300115 .desc = "Match and link multiple hists",
Namhyung Kimf8ebb0c2012-12-10 17:29:57 +0900116 .func = test__hists_link,
117 },
118 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300119 .desc = "'import perf' in python",
Arnaldo Carvalho de Melo54359d32012-12-14 13:06:13 -0300120 .func = test__python_use,
121 },
122 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300123 .desc = "Breakpoint overflow signal handler",
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100124 .func = test__bp_signal,
Jiri Olsa598762c2017-06-01 22:54:50 +0200125 .is_supported = test__bp_signal_is_supported,
Jiri Olsa5a6bef42013-03-10 19:41:10 +0100126 },
127 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300128 .desc = "Breakpoint overflow sampling",
Jiri Olsa06933e32013-03-10 19:41:11 +0100129 .func = test__bp_signal_overflow,
Jiri Olsa598762c2017-06-01 22:54:50 +0200130 .is_supported = test__bp_signal_is_supported,
Jiri Olsa06933e32013-03-10 19:41:11 +0100131 },
132 {
Jiri Olsa032db282018-03-12 14:45:48 +0100133 .desc = "Breakpoint accounting",
134 .func = test__bp_accounting,
Leo Yane533ead2019-10-18 16:55:30 +0800135 .is_supported = test__bp_account_is_supported,
Jiri Olsa032db282018-03-12 14:45:48 +0100136 },
137 {
Ravi Bangoria2e85d592018-09-12 11:42:29 +0530138 .desc = "Watchpoint",
139 .func = test__wp,
Thomas Richter0e241472018-09-28 12:53:35 +0200140 .is_supported = test__wp_is_supported,
Ravi Bangoria2e85d592018-09-12 11:42:29 +0530141 .subtest = {
142 .skip_if_fail = false,
143 .get_nr = test__wp_subtest_get_nr,
144 .get_desc = test__wp_subtest_get_desc,
Tommi Rantalacc3b9642020-10-16 16:16:50 +0300145 .skip_reason = test__wp_subtest_skip_reason,
Ravi Bangoria2e85d592018-09-12 11:42:29 +0530146 },
147 },
148 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300149 .desc = "Number of exit events of a simple workload",
Namhyung Kimd723a552013-03-15 14:58:11 +0900150 .func = test__task_exit,
151 },
152 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300153 .desc = "Software clock events period values",
Namhyung Kimbc96b362013-03-18 11:41:47 +0900154 .func = test__sw_clock_freq,
155 },
156 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300157 .desc = "Object code reading",
Adrian Hunterb55ae0a2013-08-07 14:38:45 +0300158 .func = test__code_reading,
159 },
160 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300161 .desc = "Sample parsing",
Adrian Hunter045f8cd82013-08-27 11:23:13 +0300162 .func = test__sample_parsing,
163 },
164 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300165 .desc = "Use a dummy software event to keep tracking",
Adrian Hunter395c3072013-08-31 21:50:53 +0300166 .func = test__keep_tracking,
167 },
168 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300169 .desc = "Parse with no sample_id_all bit set",
Adrian Hunter53a277e2013-09-04 23:18:16 +0300170 .func = test__parse_no_sample_id_all,
171 },
172 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300173 .desc = "Filter hist entries",
Namhyung Kim3c3cfd92014-04-25 12:28:14 +0900174 .func = test__hists_filter,
175 },
176 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300177 .desc = "Lookup mmap thread",
Jiri Olsa4e85edf2014-03-05 17:20:31 +0100178 .func = test__mmap_thread_lookup,
179 },
180 {
Arnaldo Carvalho de Melo6d382672019-11-25 22:29:18 -0300181 .desc = "Share thread maps",
182 .func = test__thread_maps_share,
Jiri Olsafabf012382014-03-17 14:39:00 +0100183 },
184 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300185 .desc = "Sort output of hist entries",
Namhyung Kimf21d1812014-05-12 14:43:18 +0900186 .func = test__hists_output,
187 },
188 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300189 .desc = "Cumulate child hist entries",
Namhyung Kim0506aec2014-05-23 18:04:42 +0900190 .func = test__hists_cumulate,
191 },
192 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300193 .desc = "Track with sched_switch",
Adrian Hunterd44bc552014-08-15 22:08:36 +0300194 .func = test__switch_tracking,
195 },
196 {
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300197 .desc = "Filter fds with revents mask in a fdarray",
198 .func = test__fdarray__filter,
Arnaldo Carvalho de Melo54dbfae2014-08-12 23:34:06 -0300199 },
200 {
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300201 .desc = "Add fd to a fdarray, making it autogrow",
202 .func = test__fdarray__add,
Arnaldo Carvalho de Melo9ae28032014-08-18 16:49:00 -0300203 },
204 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300205 .desc = "kmod_path__parse",
Jiri Olsa3c8a67f2015-02-05 15:40:25 +0100206 .func = test__kmod_path__parse,
207 },
208 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300209 .desc = "Thread map",
Jiri Olsa134aa442015-06-26 11:29:08 +0200210 .func = test__thread_map,
211 },
212 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300213 .desc = "LLVM search and compile",
Wang Nan9bc898c2015-07-08 10:04:02 +0000214 .func = test__llvm,
Wang Nane8c6d502015-11-17 08:32:48 +0000215 .subtest = {
216 .skip_if_fail = true,
217 .get_nr = test__llvm_subtest_get_nr,
218 .get_desc = test__llvm_subtest_get_desc,
219 },
Wang Nan9bc898c2015-07-08 10:04:02 +0000220 },
221 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300222 .desc = "Session topology",
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300223 .func = test__session_topology,
Kan Liangc84974e2015-09-04 04:58:31 -0400224 },
225 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300226 .desc = "BPF filter",
Wang Nanba1fae42015-11-06 13:49:43 +0000227 .func = test__bpf,
Wang Nan77a0cf62015-11-17 08:32:49 +0000228 .subtest = {
229 .skip_if_fail = true,
230 .get_nr = test__bpf_subtest_get_nr,
231 .get_desc = test__bpf_subtest_get_desc,
232 },
Wang Nanba1fae42015-11-06 13:49:43 +0000233 },
234 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300235 .desc = "Synthesize thread map",
Jiri Olsa99471c92015-10-25 15:51:20 +0100236 .func = test__thread_map_synthesize,
237 },
238 {
Jiri Olsa38af91f2016-12-12 11:35:41 +0100239 .desc = "Remove thread map",
240 .func = test__thread_map_remove,
241 },
242 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300243 .desc = "Synthesize cpu map",
Jiri Olsa6c872902015-10-25 15:51:24 +0100244 .func = test__cpu_map_synthesize,
245 },
246 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300247 .desc = "Synthesize stat config",
Jiri Olsa67424342015-10-25 15:51:28 +0100248 .func = test__synthesize_stat_config,
249 },
250 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300251 .desc = "Synthesize stat",
Jiri Olsa5796f8f2015-10-25 15:51:31 +0100252 .func = test__synthesize_stat,
253 },
254 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300255 .desc = "Synthesize stat round",
Jiri Olsad4c22592015-10-25 15:51:34 +0100256 .func = test__synthesize_stat_round,
257 },
258 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300259 .desc = "Synthesize attr update",
Jiri Olsaa6e52812015-10-25 15:51:37 +0100260 .func = test__event_update,
261 },
262 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300263 .desc = "Event times",
Jiri Olsab31d6602016-03-24 13:52:20 +0100264 .func = test__event_times,
265 },
266 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300267 .desc = "Read backward ring buffer",
Wang Nanee747012016-05-09 01:47:51 +0000268 .func = test__backward_ring_buffer,
269 },
270 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300271 .desc = "Print cpu map",
Jiri Olsaa24020e2016-06-28 13:29:04 +0200272 .func = test__cpu_map_print,
273 },
274 {
Andi Kleena2408a72019-11-20 16:15:14 -0800275 .desc = "Merge cpu map",
276 .func = test__cpu_map_merge,
277 },
278
279 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300280 .desc = "Probe SDT events",
Masami Hiramatsu8e5dc842016-07-12 19:06:05 +0900281 .func = test__sdt_event,
282 },
283 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300284 .desc = "is_printable_array",
Jiri Olsa988dd772016-07-16 18:11:20 +0200285 .func = test__is_printable_array,
286 },
287 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300288 .desc = "Print bitmap",
Jiri Olsaff3e33b2016-08-01 20:02:32 +0200289 .func = test__bitmap_print,
290 },
291 {
Arnaldo Carvalho de Melo030910c2016-11-29 12:38:14 -0300292 .desc = "perf hooks",
Wang Nana0748652016-11-26 07:03:28 +0000293 .func = test__perf_hooks,
294 },
295 {
Wang Nan00b86692016-11-26 07:03:34 +0000296 .desc = "builtin clang support",
297 .func = test__clang,
298 .subtest = {
299 .skip_if_fail = true,
300 .get_nr = test__clang_subtest_get_nr,
301 .get_desc = test__clang_subtest_get_desc,
302 }
303 },
304 {
Jiri Olsa98081432017-01-09 10:51:55 +0100305 .desc = "unit_number__scnprintf",
306 .func = test__unit_number__scnprint,
307 },
308 {
Jiri Olsa81858502018-03-09 11:14:36 +0100309 .desc = "mem2node",
310 .func = test__mem2node,
311 },
312 {
Adrian Huntere39a12c2019-06-04 16:00:16 +0300313 .desc = "time utils",
314 .func = test__time_utils,
315 },
316 {
Ian Rogersfa7f7e72019-11-26 15:59:13 -0800317 .desc = "Test jit_write_elf",
318 .func = test__jit_write_elf,
319 },
320 {
Stephane Eranian70943492020-05-05 11:29:43 -0700321 .desc = "Test libpfm4 support",
322 .func = test__pfm,
323 .subtest = {
324 .skip_if_fail = true,
325 .get_nr = test__pfm_subtest_get_nr,
326 .get_desc = test__pfm_subtest_get_desc,
327 }
328 },
329 {
Ian Rogerse95770a2020-04-14 22:40:49 -0700330 .desc = "Test api io",
331 .func = test__api_io,
332 },
333 {
Arnaldo Carvalho de Meloa5732682019-11-25 22:33:02 -0300334 .desc = "maps__merge_in",
335 .func = test__maps__merge_in,
Jiri Olsa4f600bc2019-05-08 15:20:07 +0200336 },
337 {
Nick Gasson525c8212020-04-27 14:15:16 +0800338 .desc = "Demangle Java",
339 .func = test__demangle_java,
340 },
341 {
Fabian Hemmercef7af22021-02-03 16:15:37 -0500342 .desc = "Demangle OCaml",
343 .func = test__demangle_ocaml,
344 },
345 {
Jiri Olsa0a507af2020-06-02 23:47:40 +0200346 .desc = "Parse and process metrics",
347 .func = test__parse_metric,
348 },
349 {
Remi Bernoned21d6d2020-08-21 18:52:38 +0200350 .desc = "PE file support",
351 .func = test__pe_file_parsing,
352 },
353 {
Namhyung Kim40b74c302020-09-24 21:44:55 +0900354 .desc = "Event expansion for cgroups",
355 .func = test__expand_cgroup_events,
356 },
357 {
Leo Yan3989bbf2020-10-19 18:02:35 +0800358 .desc = "Convert perf time to TSC",
359 .func = test__perf_time_to_tsc,
Leo Yan248dd9b2020-10-19 18:02:36 +0800360 .is_supported = test__tsc_is_supported,
Leo Yan3989bbf2020-10-19 18:02:35 +0800361 },
362 {
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300363 .func = NULL,
364 },
365};
366
Matt Fleming31b67532015-10-05 15:40:19 +0100367static struct test *tests[] = {
368 generic_tests,
369 arch_tests,
370};
371
Jiri Olsad685e6c2020-05-25 00:42:06 +0200372static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200373{
374 int i;
375
376 if (argc == 0)
377 return true;
378
379 for (i = 0; i < argc; ++i) {
380 char *end;
381 long nr = strtoul(argv[i], &end, 10);
382
383 if (*end == '\0') {
384 if (nr == curr + 1)
385 return true;
386 continue;
387 }
388
Jiri Olsad685e6c2020-05-25 00:42:06 +0200389 if (strcasestr(desc, argv[i]))
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200390 return true;
391 }
392
393 return false;
394}
395
Arnaldo Carvalho de Melo721a1f52015-11-19 12:01:48 -0300396static int run_test(struct test *test, int subtest)
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200397{
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200398 int status, err = -1, child = dont_fork ? 0 : fork();
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +0000399 char sbuf[STRERR_BUFSIZE];
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200400
401 if (child < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +0000402 pr_err("failed to fork test: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300403 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200404 return -1;
405 }
406
407 if (!child) {
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200408 if (!dont_fork) {
409 pr_debug("test child forked, pid %d\n", getpid());
Wang Nan5bcf2fe2015-11-17 08:32:50 +0000410
Namhyung Kimbb963e12017-02-17 17:17:38 +0900411 if (verbose <= 0) {
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200412 int nullfd = open("/dev/null", O_WRONLY);
413
414 if (nullfd >= 0) {
415 close(STDERR_FILENO);
416 close(STDOUT_FILENO);
417
418 dup2(nullfd, STDOUT_FILENO);
419 dup2(STDOUT_FILENO, STDERR_FILENO);
420 close(nullfd);
421 }
422 } else {
423 signal(SIGSEGV, sighandler_dump_stack);
424 signal(SIGFPE, sighandler_dump_stack);
Wang Nan5bcf2fe2015-11-17 08:32:50 +0000425 }
426 }
427
Arnaldo Carvalho de Melo81f17c92017-08-03 15:16:31 -0300428 err = test->func(test, subtest);
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200429 if (!dont_fork)
430 exit(err);
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200431 }
432
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200433 if (!dont_fork) {
434 wait(&status);
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200435
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200436 if (WIFEXITED(status)) {
437 err = (signed char)WEXITSTATUS(status);
438 pr_debug("test child finished with %d\n", err);
439 } else if (WIFSIGNALED(status)) {
440 err = -1;
441 pr_debug("test child interrupted\n");
442 }
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200443 }
444
445 return err;
446}
447
Matt Fleming31b67532015-10-05 15:40:19 +0100448#define for_each_test(j, t) \
449 for (j = 0; j < ARRAY_SIZE(tests); j++) \
450 for (t = &tests[j][0]; t->func; t++)
Matt Fleminge8210ce2015-09-05 20:02:20 +0100451
Wang Nane8c6d502015-11-17 08:32:48 +0000452static int test_and_print(struct test *t, bool force_skip, int subtest)
453{
454 int err;
455
456 if (!force_skip) {
457 pr_debug("\n--- start ---\n");
458 err = run_test(t, subtest);
459 pr_debug("---- end ----\n");
460 } else {
461 pr_debug("\n--- force skipped ---\n");
462 err = TEST_SKIP;
463 }
464
465 if (!t->subtest.get_nr)
466 pr_debug("%s:", t->desc);
467 else
Sandipan Dasaa90f9f2018-07-26 22:47:33 +0530468 pr_debug("%s subtest %d:", t->desc, subtest + 1);
Wang Nane8c6d502015-11-17 08:32:48 +0000469
470 switch (err) {
471 case TEST_OK:
472 pr_info(" Ok\n");
473 break;
Ian Rogers3b536652020-05-13 14:29:33 -0700474 case TEST_SKIP: {
475 const char *skip_reason = NULL;
476 if (t->subtest.skip_reason)
477 skip_reason = t->subtest.skip_reason(subtest);
478 if (skip_reason)
479 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", skip_reason);
480 else
481 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
482 }
Wang Nane8c6d502015-11-17 08:32:48 +0000483 break;
484 case TEST_FAIL:
485 default:
486 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
487 break;
488 }
489
490 return err;
491}
492
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300493static const char *shell_test__description(char *description, size_t size,
494 const char *path, const char *name)
495{
496 FILE *fp;
497 char filename[PATH_MAX];
498
499 path__join(filename, sizeof(filename), path, name);
500 fp = fopen(filename, "r");
501 if (!fp)
502 return NULL;
503
Michael Petlan4eaf97e2018-11-21 16:39:18 -0500504 /* Skip shebang */
505 while (fgetc(fp) != '\n');
506
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300507 description = fgets(description, size, fp);
508 fclose(fp);
509
Arnaldo Carvalho de Melo3ca43b62019-06-26 12:06:20 -0300510 return description ? strim(description + 1) : NULL;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300511}
512
Riccardo Mancinida963832021-05-26 01:05:17 +0200513#define for_each_shell_test(entlist, nr, base, ent) \
514 for (int __i = 0; __i < nr && (ent = entlist[__i]); __i++) \
Kim Phillipsdb8fec52018-06-29 12:46:58 -0500515 if (!is_directory(base, ent) && ent->d_name[0] != '.')
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300516
517static const char *shell_tests__dir(char *path, size_t size)
518{
519 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
520 char *exec_path;
521 unsigned int i;
522
523 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
524 struct stat st;
525 if (!lstat(devel_dirs[i], &st)) {
526 scnprintf(path, size, "%s/shell", devel_dirs[i]);
527 if (!lstat(devel_dirs[i], &st))
528 return path;
529 }
530 }
531
532 /* Then installed path. */
533 exec_path = get_argv_exec_path();
534 scnprintf(path, size, "%s/tests/shell", exec_path);
535 free(exec_path);
536 return path;
537}
538
539static int shell_tests__max_desc_width(void)
540{
Riccardo Mancinida963832021-05-26 01:05:17 +0200541 struct dirent **entlist;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300542 struct dirent *ent;
Riccardo Mancinida963832021-05-26 01:05:17 +0200543 int n_dirs;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300544 char path_dir[PATH_MAX];
545 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
546 int width = 0;
547
548 if (path == NULL)
549 return -1;
550
Riccardo Mancinida963832021-05-26 01:05:17 +0200551 n_dirs = scandir(path, &entlist, NULL, alphasort);
552 if (n_dirs == -1)
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300553 return -1;
554
Riccardo Mancinida963832021-05-26 01:05:17 +0200555 for_each_shell_test(entlist, n_dirs, path, ent) {
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300556 char bf[256];
557 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
558
559 if (desc) {
560 int len = strlen(desc);
561
562 if (width < len)
563 width = len;
564 }
565 }
566
Riccardo Mancinida963832021-05-26 01:05:17 +0200567 free(entlist);
568
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300569 return width;
570}
571
572struct shell_test {
573 const char *dir;
574 const char *file;
575};
576
577static int shell_test__run(struct test *test, int subdir __maybe_unused)
578{
579 int err;
580 char script[PATH_MAX];
581 struct shell_test *st = test->priv;
582
Ian Rogerse3c9cfd2021-06-21 14:56:46 -0700583 path__join(script, sizeof(script) - 3, st->dir, st->file);
584
585 if (verbose)
586 strncat(script, " -v", sizeof(script) - strlen(script) - 1);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300587
588 err = system(script);
589 if (!err)
590 return TEST_OK;
591
592 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
593}
594
595static int run_shell_tests(int argc, const char *argv[], int i, int width)
596{
Riccardo Mancinida963832021-05-26 01:05:17 +0200597 struct dirent **entlist;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300598 struct dirent *ent;
Riccardo Mancinida963832021-05-26 01:05:17 +0200599 int n_dirs;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300600 char path_dir[PATH_MAX];
601 struct shell_test st = {
602 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
603 };
604
605 if (st.dir == NULL)
606 return -1;
607
Riccardo Mancinida963832021-05-26 01:05:17 +0200608 n_dirs = scandir(st.dir, &entlist, NULL, alphasort);
609 if (n_dirs == -1) {
Ian Rogersb2bf6662020-03-12 17:56:02 -0700610 pr_err("failed to open shell test directory: %s\n",
611 st.dir);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300612 return -1;
Ian Rogersb2bf6662020-03-12 17:56:02 -0700613 }
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300614
Riccardo Mancinida963832021-05-26 01:05:17 +0200615 for_each_shell_test(entlist, n_dirs, st.dir, ent) {
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300616 int curr = i++;
617 char desc[256];
618 struct test test = {
619 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name),
620 .func = shell_test__run,
621 .priv = &st,
622 };
623
Jiri Olsad685e6c2020-05-25 00:42:06 +0200624 if (!perf_test__matches(test.desc, curr, argc, argv))
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300625 continue;
626
627 st.file = ent->d_name;
628 pr_info("%2d: %-*s:", i, width, test.desc);
629 test_and_print(&test, false, -1);
630 }
631
Riccardo Mancinida963832021-05-26 01:05:17 +0200632 free(entlist);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300633 return 0;
634}
635
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300636static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300637{
Matt Fleminge8210ce2015-09-05 20:02:20 +0100638 struct test *t;
Matt Fleming31b67532015-10-05 15:40:19 +0100639 unsigned int j;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300640 int i = 0;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300641 int width = shell_tests__max_desc_width();
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300642
Matt Fleming31b67532015-10-05 15:40:19 +0100643 for_each_test(j, t) {
Matt Fleminge8210ce2015-09-05 20:02:20 +0100644 int len = strlen(t->desc);
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200645
646 if (width < len)
647 width = len;
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200648 }
Jiri Olsa945aea22012-10-30 23:01:43 +0100649
Matt Fleming31b67532015-10-05 15:40:19 +0100650 for_each_test(j, t) {
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200651 int curr = i++, err;
Jiri Olsad685e6c2020-05-25 00:42:06 +0200652 int subi;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200653
Jiri Olsad685e6c2020-05-25 00:42:06 +0200654 if (!perf_test__matches(t->desc, curr, argc, argv)) {
655 bool skip = true;
656 int subn;
657
658 if (!t->subtest.get_nr)
659 continue;
660
661 subn = t->subtest.get_nr();
662
663 for (subi = 0; subi < subn; subi++) {
664 if (perf_test__matches(t->subtest.get_desc(subi), curr, argc, argv))
665 skip = false;
666 }
667
668 if (skip)
669 continue;
670 }
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200671
Jiri Olsa598762c2017-06-01 22:54:50 +0200672 if (t->is_supported && !t->is_supported()) {
673 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
674 continue;
675 }
676
Matt Fleminge8210ce2015-09-05 20:02:20 +0100677 pr_info("%2d: %-*s:", i, width, t->desc);
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300678
679 if (intlist__find(skiplist, i)) {
680 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
681 continue;
682 }
683
Wang Nane8c6d502015-11-17 08:32:48 +0000684 if (!t->subtest.get_nr) {
685 test_and_print(t, false, -1);
686 } else {
687 int subn = t->subtest.get_nr();
688 /*
689 * minus 2 to align with normal testcases.
690 * For subtest we print additional '.x' in number.
691 * for example:
692 *
693 * 35: Test LLVM searching and compiling :
694 * 35.1: Basic BPF llvm compiling test : Ok
695 */
696 int subw = width > 2 ? width - 2 : width;
697 bool skip = false;
Jiri Olsaf4c1ea52012-12-19 11:33:39 -0300698
Wang Nane8c6d502015-11-17 08:32:48 +0000699 if (subn <= 0) {
700 color_fprintf(stderr, PERF_COLOR_YELLOW,
701 " Skip (not compiled in)\n");
702 continue;
703 }
704 pr_info("\n");
705
706 for (subi = 0; subi < subn; subi++) {
707 int len = strlen(t->subtest.get_desc(subi));
708
709 if (subw < len)
710 subw = len;
711 }
712
713 for (subi = 0; subi < subn; subi++) {
Jiri Olsad685e6c2020-05-25 00:42:06 +0200714 if (!perf_test__matches(t->subtest.get_desc(subi), curr, argc, argv))
715 continue;
716
Wang Nane8c6d502015-11-17 08:32:48 +0000717 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
718 t->subtest.get_desc(subi));
Sandipan Dasaa90f9f2018-07-26 22:47:33 +0530719 err = test_and_print(t, skip, subi);
Wang Nane8c6d502015-11-17 08:32:48 +0000720 if (err != TEST_OK && t->subtest.skip_if_fail)
721 skip = true;
722 }
Jiri Olsaf4c1ea52012-12-19 11:33:39 -0300723 }
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300724 }
725
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300726 return run_shell_tests(argc, argv, i, width);
727}
728
729static int perf_test__list_shell(int argc, const char **argv, int i)
730{
Riccardo Mancinida963832021-05-26 01:05:17 +0200731 struct dirent **entlist;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300732 struct dirent *ent;
Riccardo Mancinida963832021-05-26 01:05:17 +0200733 int n_dirs;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300734 char path_dir[PATH_MAX];
735 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
736
737 if (path == NULL)
738 return -1;
739
Riccardo Mancinida963832021-05-26 01:05:17 +0200740 n_dirs = scandir(path, &entlist, NULL, alphasort);
741 if (n_dirs == -1)
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300742 return -1;
743
Riccardo Mancinida963832021-05-26 01:05:17 +0200744 for_each_shell_test(entlist, n_dirs, path, ent) {
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300745 int curr = i++;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300746 char bf[256];
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300747 struct test t = {
748 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
749 };
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300750
Jiri Olsad685e6c2020-05-25 00:42:06 +0200751 if (!perf_test__matches(t.desc, curr, argc, argv))
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300752 continue;
753
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300754 pr_info("%2d: %s\n", i, t.desc);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300755 }
756
Riccardo Mancinida963832021-05-26 01:05:17 +0200757 free(entlist);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300758 return 0;
759}
760
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200761static int perf_test__list(int argc, const char **argv)
762{
Matt Fleming31b67532015-10-05 15:40:19 +0100763 unsigned int j;
Matt Fleminge8210ce2015-09-05 20:02:20 +0100764 struct test *t;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200765 int i = 0;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300766
Matt Fleming31b67532015-10-05 15:40:19 +0100767 for_each_test(j, t) {
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300768 int curr = i++;
Arnaldo Carvalho de Melo28765bf2017-08-04 11:16:40 -0300769
Jiri Olsad685e6c2020-05-25 00:42:06 +0200770 if (!perf_test__matches(t->desc, curr, argc, argv) ||
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300771 (t->is_supported && !t->is_supported()))
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200772 continue;
773
Arnaldo Carvalho de Melo28765bf2017-08-04 11:16:40 -0300774 pr_info("%2d: %s\n", i, t->desc);
Hendrik Bruecknerea40b6d2018-04-13 09:42:23 +0200775
776 if (t->subtest.get_nr) {
777 int subn = t->subtest.get_nr();
778 int subi;
779
780 for (subi = 0; subi < subn; subi++)
781 pr_info("%2d:%1d: %s\n", i, subi + 1,
782 t->subtest.get_desc(subi));
783 }
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200784 }
785
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300786 perf_test__list_shell(argc, argv, i);
787
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200788 return 0;
789}
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300790
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300791int cmd_test(int argc, const char **argv)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300792{
Yunlong Song1f9975f2015-03-18 21:35:55 +0800793 const char *test_usage[] = {
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200794 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
795 NULL,
796 };
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300797 const char *skip = NULL;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200798 const struct option test_options[] = {
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300799 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
Namhyung Kimc30ab8a2012-01-08 02:25:26 +0900800 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200801 "be more verbose (show symbol address, etc)"),
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200802 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
803 "Do not fork for testcase"),
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200804 OPT_END()
805 };
Yunlong Song1f9975f2015-03-18 21:35:55 +0800806 const char * const test_subcommands[] = { "list", NULL };
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300807 struct intlist *skiplist = NULL;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -0300808 int ret = hists__init();
809
810 if (ret < 0)
811 return ret;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200812
Yunlong Song1f9975f2015-03-18 21:35:55 +0800813 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200814 if (argc >= 1 && !strcmp(argv[0], "list"))
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300815 return perf_test__list(argc - 1, argv + 1);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300816
817 symbol_conf.priv_size = sizeof(int);
818 symbol_conf.sort_by_name = true;
819 symbol_conf.try_vmlinux_path = true;
820
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900821 if (symbol__init(NULL) < 0)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300822 return -1;
823
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300824 if (skip != NULL)
825 skiplist = intlist__new(skip);
Arnaldo Carvalho de Melod3280ce2019-07-09 16:27:01 -0300826 /*
827 * Tests that create BPF maps, for instance, need more than the 64K
828 * default:
829 */
830 rlimit__bump_memlock();
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300831
832 return __cmd_test(argc, argv, skiplist);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300833}