Skip to content

Commit 4cb9344

Browse files
committed
perf tools: Set the maximum allowed stack from /proc/sys/kernel/perf_event_max_stack
There is an upper limit to what tooling considers a valid callchain, and it was tied to the hardcoded value in the kernel, PERF_MAX_STACK_DEPTH (127), now that this can be tuned via a sysctl, make it read it and use that as the upper limit, falling back to PERF_MAX_STACK_DEPTH for kernels where this sysctl isn't present. Cc: Adrian Hunter <[email protected]> Cc: Brendan Gregg <[email protected]> Cc: David Ahern <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Milian Wolff <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Wang Nan <[email protected]> Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent c5dfd78 commit 4cb9344

File tree

16 files changed

+28
-18
lines changed

16 files changed

+28
-18
lines changed

tools/perf/Documentation/perf-report.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ OPTIONS
248248
Note that when using the --itrace option the synthesized callchain size
249249
will override this value if the synthesized callchain size is bigger.
250250

251-
Default: 127
251+
Default: /proc/sys/kernel/perf_event_max_stack when present, 127 otherwise.
252252

253253
-G::
254254
--inverted::

tools/perf/Documentation/perf-script.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ include::itrace.txt[]
267267
Note that when using the --itrace option the synthesized callchain size
268268
will override this value if the synthesized callchain size is bigger.
269269

270-
Default: 127
270+
Default: /proc/sys/kernel/perf_event_max_stack when present, 127 otherwise.
271271

272272
--ns::
273273
Use 9 decimal places when displaying time (i.e. show the nanoseconds)

tools/perf/Documentation/perf-top.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ Default is to monitor all CPUS.
177177
between information loss and faster processing especially for
178178
workloads that can have a very long callchain stack.
179179

180-
Default: 127
180+
Default: /proc/sys/kernel/perf_event_max_stack when present, 127 otherwise.
181181

182182
--ignore-callees=<regex>::
183183
Ignore callees of the function(s) matching the given regex.

tools/perf/Documentation/perf-trace.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs.
143143
Implies '--call-graph dwarf' when --call-graph not present on the
144144
command line, on systems where DWARF unwinding was built in.
145145

146-
Default: 127
146+
Default: /proc/sys/kernel/perf_event_max_stack when present, 127 otherwise.
147147

148148
--min-stack::
149149
Set the stack depth limit when parsing the callchain, anything

tools/perf/builtin-report.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
691691
.ordered_events = true,
692692
.ordering_requires_timestamps = true,
693693
},
694-
.max_stack = PERF_MAX_STACK_DEPTH,
694+
.max_stack = sysctl_perf_event_max_stack,
695695
.pretty_printing_style = "normal",
696696
.socket_filter = -1,
697697
};
@@ -744,7 +744,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
744744
OPT_INTEGER(0, "max-stack", &report.max_stack,
745745
"Set the maximum stack depth when parsing the callchain, "
746746
"anything beyond the specified depth will be ignored. "
747-
"Default: " __stringify(PERF_MAX_STACK_DEPTH)),
747+
"Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
748748
OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
749749
"alias for inverted call graph"),
750750
OPT_CALLBACK(0, "ignore-callees", NULL, "regex",

tools/perf/builtin-script.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
20312031
OPT_UINTEGER(0, "max-stack", &scripting_max_stack,
20322032
"Set the maximum stack depth when parsing the callchain, "
20332033
"anything beyond the specified depth will be ignored. "
2034-
"Default: " __stringify(PERF_MAX_STACK_DEPTH)),
2034+
"Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
20352035
OPT_BOOLEAN('I', "show-info", &show_full_info,
20362036
"display extended information from perf.data file"),
20372037
OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
@@ -2067,6 +2067,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
20672067
NULL
20682068
};
20692069

2070+
scripting_max_stack = sysctl_perf_event_max_stack;
2071+
20702072
setup_scripting();
20712073

20722074
argc = parse_options_subcommand(argc, argv, options, script_subcommands, script_usage,

tools/perf/builtin-top.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
11031103
},
11041104
.proc_map_timeout = 500,
11051105
},
1106-
.max_stack = PERF_MAX_STACK_DEPTH,
1106+
.max_stack = sysctl_perf_event_max_stack,
11071107
.sym_pcnt_filter = 5,
11081108
};
11091109
struct record_opts *opts = &top.record_opts;
@@ -1171,7 +1171,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
11711171
"Accumulate callchains of children and show total overhead as well"),
11721172
OPT_INTEGER(0, "max-stack", &top.max_stack,
11731173
"Set the maximum stack depth when parsing the callchain. "
1174-
"Default: " __stringify(PERF_MAX_STACK_DEPTH)),
1174+
"Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
11751175
OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
11761176
"ignore callees of these functions in call graphs",
11771177
report_parse_ignore_callees_opt),

tools/perf/builtin-trace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3106,7 +3106,7 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
31063106
OPT_UINTEGER(0, "max-stack", &trace.max_stack,
31073107
"Set the maximum stack depth when parsing the callchain, "
31083108
"anything beyond the specified depth will be ignored. "
3109-
"Default: " __stringify(PERF_MAX_STACK_DEPTH)),
3109+
"Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
31103110
OPT_UINTEGER(0, "proc-map-timeout", &trace.opts.proc_map_timeout,
31113111
"per thread proc mmap processing timeout in ms"),
31123112
OPT_END()
@@ -3150,7 +3150,7 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
31503150
mmap_pages_user_set = false;
31513151

31523152
if (trace.max_stack == UINT_MAX) {
3153-
trace.max_stack = PERF_MAX_STACK_DEPTH;
3153+
trace.max_stack = sysctl_perf_event_max_stack;
31543154
max_stack_user_set = false;
31553155
}
31563156

tools/perf/perf.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <subcmd/parse-options.h>
1818
#include "util/bpf-loader.h"
1919
#include "util/debug.h"
20+
#include <api/fs/fs.h>
2021
#include <api/fs/tracing_path.h>
2122
#include <pthread.h>
2223
#include <stdlib.h>
@@ -533,6 +534,7 @@ int main(int argc, const char **argv)
533534
{
534535
const char *cmd;
535536
char sbuf[STRERR_BUFSIZE];
537+
int value;
536538

537539
/* libsubcmd init */
538540
exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT);
@@ -542,6 +544,9 @@ int main(int argc, const char **argv)
542544
page_size = sysconf(_SC_PAGE_SIZE);
543545
cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
544546

547+
if (sysctl__read_int("kernel/perf_event_max_stack", &value) == 0)
548+
sysctl_perf_event_max_stack = value;
549+
545550
cmd = extract_argv0_path(argv[0]);
546551
if (!cmd)
547552
cmd = "perf-help";

tools/perf/tests/hists_cumulate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)
101101
if (machine__resolve(machine, &al, &sample) < 0)
102102
goto out;
103103

104-
if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH,
104+
if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack,
105105
NULL) < 0) {
106106
addr_location__put(&al);
107107
goto out;

tools/perf/tests/hists_filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int add_hist_entries(struct perf_evlist *evlist,
8181

8282
al.socket = fake_samples[i].socket;
8383
if (hist_entry_iter__add(&iter, &al,
84-
PERF_MAX_STACK_DEPTH, NULL) < 0) {
84+
sysctl_perf_event_max_stack, NULL) < 0) {
8585
addr_location__put(&al);
8686
goto out;
8787
}

tools/perf/tests/hists_output.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static int add_hist_entries(struct hists *hists, struct machine *machine)
6767
if (machine__resolve(machine, &al, &sample) < 0)
6868
goto out;
6969

70-
if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH,
70+
if (hist_entry_iter__add(&iter, &al, sysctl_perf_event_max_stack,
7171
NULL) < 0) {
7272
addr_location__put(&al);
7373
goto out;

tools/perf/util/machine.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,7 @@ static int resolve_lbr_callchain_sample(struct thread *thread,
17641764
*/
17651765
int mix_chain_nr = i + 1 + lbr_nr + 1;
17661766

1767-
if (mix_chain_nr > PERF_MAX_STACK_DEPTH + PERF_MAX_BRANCH_DEPTH) {
1767+
if (mix_chain_nr > (int)sysctl_perf_event_max_stack + PERF_MAX_BRANCH_DEPTH) {
17681768
pr_warning("corrupted callchain. skipping...\n");
17691769
return 0;
17701770
}
@@ -1825,7 +1825,7 @@ static int thread__resolve_callchain_sample(struct thread *thread,
18251825
* Based on DWARF debug information, some architectures skip
18261826
* a callchain entry saved by the kernel.
18271827
*/
1828-
if (chain->nr < PERF_MAX_STACK_DEPTH)
1828+
if (chain->nr < sysctl_perf_event_max_stack)
18291829
skip_idx = arch_skip_callchain_idx(thread, chain);
18301830

18311831
/*
@@ -1886,7 +1886,7 @@ static int thread__resolve_callchain_sample(struct thread *thread,
18861886
}
18871887

18881888
check_calls:
1889-
if (chain->nr > PERF_MAX_STACK_DEPTH && (int)chain->nr > max_stack) {
1889+
if (chain->nr > sysctl_perf_event_max_stack && (int)chain->nr > max_stack) {
18901890
pr_warning("corrupted callchain. skipping...\n");
18911891
return 0;
18921892
}

tools/perf/util/scripting-engines/trace-event-perl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static SV *perl_process_callchain(struct perf_sample *sample,
265265

266266
if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
267267
sample, NULL, NULL,
268-
PERF_MAX_STACK_DEPTH) != 0) {
268+
sysctl_perf_event_max_stack) != 0) {
269269
pr_err("Failed to resolve callchain. Skipping\n");
270270
goto exit;
271271
}

tools/perf/util/util.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct callchain_param callchain_param = {
3333
unsigned int page_size;
3434
int cacheline_size;
3535

36+
unsigned int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
37+
3638
bool test_attr__enabled;
3739

3840
bool perf_host = true;

tools/perf/util/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ void sighandler_dump_stack(int sig);
267267

268268
extern unsigned int page_size;
269269
extern int cacheline_size;
270+
extern unsigned int sysctl_perf_event_max_stack;
270271

271272
struct parse_tag {
272273
char tag;

0 commit comments

Comments
 (0)