1 /* Copyright (c) 2016 Facebook
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
8 #include <linux/version.h>
9 #include <bpf/bpf_helpers.h>
10 #include <bpf/bpf_tracing.h>
11 #include <bpf/bpf_core_read.h>
13 #ifndef PERF_MAX_STACK_DEPTH
14 #define PERF_MAX_STACK_DEPTH 127
18 #define MAX_ENTRIES 10000
21 char waker[TASK_COMM_LEN];
22 char target[TASK_COMM_LEN];
28 __uint(type, BPF_MAP_TYPE_HASH);
29 __type(key, struct key_t);
31 __uint(max_entries, MAX_ENTRIES);
32 } counts SEC(".maps");
35 __uint(type, BPF_MAP_TYPE_HASH);
38 __uint(max_entries, MAX_ENTRIES);
42 char name[TASK_COMM_LEN];
47 __uint(type, BPF_MAP_TYPE_HASH);
49 __type(value, struct wokeby_t);
50 __uint(max_entries, MAX_ENTRIES);
51 } wokeby SEC(".maps");
54 __uint(type, BPF_MAP_TYPE_STACK_TRACE);
55 __uint(key_size, sizeof(u32));
56 __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
57 __uint(max_entries, MAX_ENTRIES);
58 } stackmap SEC(".maps");
60 #define STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
62 SEC("kprobe/try_to_wake_up")
63 int waker(struct pt_regs *ctx)
65 struct task_struct *p = (void *)PT_REGS_PARM1_CORE(ctx);
66 u32 pid = BPF_CORE_READ(p, pid);
69 bpf_get_current_comm(&woke.name, sizeof(woke.name));
70 woke.ret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
72 bpf_map_update_elem(&wokeby, &pid, &woke, BPF_ANY);
76 static inline int update_counts(void *ctx, u32 pid, u64 delta)
78 struct wokeby_t *woke;
82 __builtin_memset(&key.waker, 0, sizeof(key.waker));
83 bpf_get_current_comm(&key.target, sizeof(key.target));
84 key.tret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
87 woke = bpf_map_lookup_elem(&wokeby, &pid);
90 __builtin_memcpy(&key.waker, woke->name, sizeof(key.waker));
91 bpf_map_delete_elem(&wokeby, &pid);
94 val = bpf_map_lookup_elem(&counts, &key);
96 bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
97 val = bpf_map_lookup_elem(&counts, &key);
106 /* taken from /sys/kernel/tracing/events/sched/sched_switch/format */
107 SEC("tracepoint/sched/sched_switch")
108 int oncpu(struct trace_event_raw_sched_switch *ctx)
110 /* record previous thread sleep time */
111 u32 pid = ctx->prev_pid;
113 SEC("kprobe.multi/finish_task_switch*")
114 int oncpu(struct pt_regs *ctx)
116 struct task_struct *p = (void *)PT_REGS_PARM1_CORE(ctx);
117 /* record previous thread sleep time */
118 u32 pid = BPF_CORE_READ(p, pid);
122 ts = bpf_ktime_get_ns();
123 bpf_map_update_elem(&start, &pid, &ts, BPF_ANY);
125 /* calculate current thread's delta time */
126 pid = bpf_get_current_pid_tgid();
127 tsp = bpf_map_lookup_elem(&start, &pid);
129 /* missed start or filtered */
132 delta = bpf_ktime_get_ns() - *tsp;
133 bpf_map_delete_elem(&start, &pid);
134 delta = delta / 1000;
135 if (delta < MINBLOCK_US)
138 return update_counts(ctx, pid, delta);
140 char _license[] SEC("license") = "GPL";
141 u32 _version SEC("version") = LINUX_VERSION_CODE;