Skip to content

Commit

Permalink
fix profile.py page_offset_base breakage (iovisor#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendangregg authored and 4ast committed Oct 19, 2016
1 parent 5845ef9 commit ac297c1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
16 changes: 6 additions & 10 deletions man/man8/profile.8
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ kernel context, rather than passing each stack to user space for frequency
counting there. Only the unique stacks and counts are passed to user space
at the end of the profile, greatly reducing the kernel<->user transfer.

Note: if another perf-based sampling session is active, the output may become
polluted with their events. On older kernels, the ouptut may also become
polluted with tracing sessions (when the kprobe is used instead of the
tracepoint). This may be filtered in a future version if it becomes a problem.
Note: if another perf-based sampling or tracing session is active, the output
may become polluted with their events. This will be fixed for Linux 4.9.
.SH REQUIREMENTS
CONFIG_BPF and bcc.

This also requires Linux 4.6+ (BPF_MAP_TYPE_STACK_TRACE support), and the
perf:perf_hrtimer tracepoint (currently a kernel patch). If the latter is
unavailable, this will try to use kprobes as a fallback (of perf_misc_flags()),
which may work or
may not, depending on your kernel build. If the kprobe doesn't work, this tool
will either error on instrumentation, or, instrument successfully but
generate no output.
perf_misc_flags() function symbol to exist. The latter may or may not
exist depending on your kernel build, and if it doesn't exist, this tool
will not work. Linux 4.9 provides a proper solution to this (this tool will
be updated).
.SH OPTIONS
.TP
\-h
Expand Down
35 changes: 19 additions & 16 deletions tools/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
# tracepoint). If this becomes a problem, logic can be added to filter events.
#
# REQUIRES: Linux 4.6+ (BPF_MAP_TYPE_STACK_TRACE support), and the
# perf:perf_hrtimer tracepoint (currently a kernel patch). If the latter is
# unavailable, this will try to use kprobes as a fallback, which may work or
# may instrument nothing, depending on your kernel build.
# perf_misc_flags() function symbol to exist. The latter may or may not
# exist depending on your kernel build. Linux 4.9 provides a proper solution
# to this (this tool will be updated).
#
# Copyright 2016 Netflix, Inc.
# Licensed under the Apache License, Version 2.0 (the "License")
Expand Down Expand Up @@ -162,8 +162,13 @@ def positive_nonzero_int(val):
struct pt_regs regs = {};
bpf_probe_read(&regs, sizeof(regs), (void *)REGS_LOCATION);
u64 ip = PT_REGS_IP(&regs);
// if ip isn't sane, leave key ips as zero for later checking
#ifdef CONFIG_RANDOMIZE_MEMORY
if (ip > __PAGE_OFFSET_BASE) {
#else
if (ip > PAGE_OFFSET) {
#endif
key.kernel_ip = ip;
if (DO_KERNEL_RIP) {
/*
Expand Down Expand Up @@ -232,23 +237,21 @@ def positive_nonzero_int(val):
else:
print("... Hit Ctrl-C to end.")

# use perf tracepoint if it exists, else kprobe
if os.path.exists("/sys/kernel/debug/tracing/events/perf/perf_hrtimer"):
bpf_text = bpf_text.replace('PERF_TRACE_EVENT',
'TRACEPOINT_PROBE(perf, perf_hrtimer)')
bpf_text = bpf_text.replace('REGS_LOCATION', 'args->regs')
else:
if not args.folded:
print("Tracepoint perf:perf_hrtimer missing. "
"Trying kprobe of perf_misc_flags()...")
bpf_text = bpf_text.replace('PERF_TRACE_EVENT',
'int kprobe__perf_misc_flags(struct pt_regs *args)')
bpf_text = bpf_text.replace('REGS_LOCATION', 'PT_REGS_PARM1(args)')
# kprobe perf_misc_flags()
bpf_text = bpf_text.replace('PERF_TRACE_EVENT',
'int kprobe__perf_misc_flags(struct pt_regs *args)')
bpf_text = bpf_text.replace('REGS_LOCATION', 'PT_REGS_PARM1(args)')
if debug:
print(bpf_text)

# initialize BPF
b = BPF(text=bpf_text)
try:
b = BPF(text=bpf_text)
except:
print("BPF initialization failed. perf_misc_flags() may be inlined in " +
"your kernel build.\nThis tool will be updated in the future to " +
"support Linux 4.9, which has reliable profiling support. Exiting.")
exit()

# signal handler
def signal_ignore(signal, frame):
Expand Down

0 comments on commit ac297c1

Please sign in to comment.