Skip to content

Commit

Permalink
tools/wakeuptime: Switch to using tracepoints if available
Browse files Browse the repository at this point in the history
wakeuptime currently uses kprobes to trace schedule(). Switch to using
tracepoints if available.

Also, in some builds, try_to_wake_up() may get optimized away causing
the script to fail altogether.

With this switch, we however see stack trace entries related to BPF too
in the end for each trace. While correct, it takes up screen real estate
for each backtrace.

    target:          InputThread
    ffffffffaf000107 secondary_startup_64_no_verify
    ffffffffb12495f1 start_kernel
    ffffffffaf10e609 cpu_startup_entry
    ffffffffaf10e40b do_idle
    ffffffffaf946849 cpuidle_enter
    ffffffffaf946577 cpuidle_enter_state
    ffffffffafc00d02 asm_sysvec_apic_timer_interrupt
    ffffffffafbbbc86 sysvec_apic_timer_interrupt
    ffffffffaf0dcc32 irq_exit_rcu
    ffffffffaf02bc47 do_softirq_own_stack
    ffffffffafc01112 asm_call_irq_on_stack
    ffffffffafe000ca __softirqentry_text_start
    ffffffffaf163a66 run_timer_softirq
    ffffffffaf1639df __run_timers.part.0
    ffffffffaf162f09 call_timer_fn
    ffffffffaf8c46c3 input_repeat_key
    ffffffffaf8c318e input_pass_values.part.0
    ffffffffaf8c2085 input_to_handler
    ffffffffaf8c9e81 evdev_events
    ffffffffaf124a1c __wake_up_common_lock
    ffffffffaf1248b0 __wake_up_common
    ffffffffaf375e49 ep_poll_callback
    ffffffffaf124a1c __wake_up_common_lock
    ffffffffaf1248b0 __wake_up_common
    ffffffffaf375e0d ep_poll_callback
    ffffffffaf124a1c __wake_up_common_lock
    ffffffffaf1248b0 __wake_up_common
    ffffffffaf124681 autoremove_wake_function
    ffffffffaf108e57 try_to_wake_up
    ffffffffaf107dcc ttwu_do_wakeup
    ffffffffaf1f48a2 bpf_trace_run1
    ffffffffc16879d3 ftrace_trampoline
    ffffffffaf1f51c9 bpf_get_stackid_raw_tp
    waker:           swapper/0
        169325
  • Loading branch information
Ananth N Mavinakayanahalli authored and yonghong-song committed Feb 6, 2021
1 parent 777b9be commit bdd7337
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions tools/wakeuptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def signal_ignore(signal, frame):
BPF_HASH(start, u32);
BPF_STACK_TRACE(stack_traces, STACK_STORAGE_SIZE);
int offcpu(struct pt_regs *ctx) {
static int offcpu_sched_switch() {
u32 pid = bpf_get_current_pid_tgid();
struct task_struct *p = (struct task_struct *) bpf_get_current_task();
u64 ts;
Expand All @@ -115,7 +115,7 @@ def signal_ignore(signal, frame):
return 0;
}
int waker(struct pt_regs *ctx, struct task_struct *p) {
static int wakeup(ARG0, struct task_struct *p) {
u32 pid = p->pid;
u64 delta, *tsp, ts;
Expand Down Expand Up @@ -143,6 +143,38 @@ def signal_ignore(signal, frame):
return 0;
}
"""

bpf_text_kprobe = """
int offcpu(struct pt_regs *ctx) {
return offcpu_sched_switch();
}
int waker(struct pt_regs *ctx, struct task_struct *p) {
return wakeup(ctx, p);
}
"""

bpf_text_raw_tp = """
RAW_TRACEPOINT_PROBE(sched_switch)
{
// TP_PROTO(bool preempt, struct task_struct *prev, struct task_struct *next)
return offcpu_sched_switch();
}
RAW_TRACEPOINT_PROBE(sched_wakeup)
{
// TP_PROTO(struct task_struct *p)
struct task_struct *p = (struct task_struct *) bpf_get_current_task();
return wakeup(ctx, p);
}
"""

is_supported_raw_tp = BPF.support_raw_tracepoint()
if is_supported_raw_tp:
bpf_text += bpf_text_raw_tp
else:
bpf_text += bpf_text_kprobe

if args.pid:
filter = 'pid != %s' % args.pid
elif args.useronly:
Expand All @@ -151,6 +183,12 @@ def signal_ignore(signal, frame):
filter = '0'
bpf_text = bpf_text.replace('FILTER', filter)

if is_supported_raw_tp:
arg0 = 'struct bpf_raw_tracepoint_args *ctx'
else:
arg0 = 'struct pt_regs *ctx'
bpf_text = bpf_text.replace('ARG0', arg0)

# set stack storage size
bpf_text = bpf_text.replace('STACK_STORAGE_SIZE', str(args.stack_storage_size))
bpf_text = bpf_text.replace('MINBLOCK_US_VALUE', str(args.min_block_time))
Expand All @@ -163,12 +201,13 @@ def signal_ignore(signal, frame):

# initialize BPF
b = BPF(text=bpf_text)
b.attach_kprobe(event="schedule", fn_name="offcpu")
b.attach_kprobe(event="try_to_wake_up", fn_name="waker")
matched = b.num_open_kprobes()
if matched == 0:
print("0 functions traced. Exiting.")
exit()
if not is_supported_raw_tp:
b.attach_kprobe(event="schedule", fn_name="offcpu")
b.attach_kprobe(event="try_to_wake_up", fn_name="waker")
matched = b.num_open_kprobes()
if matched == 0:
print("0 functions traced. Exiting.")
exit()

# header
if not folded:
Expand Down

0 comments on commit bdd7337

Please sign in to comment.