Commit | Line | Data |
---|---|---|
a6ffe7b9 AS |
1 | /* Copyright (c) 2016 Facebook |
2 | * | |
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. | |
6 | */ | |
e7e6c774 | 7 | #include "vmlinux.h" |
a6ffe7b9 | 8 | #include <linux/version.h> |
03421a92 YS |
9 | #include <bpf/bpf_helpers.h> |
10 | #include <bpf/bpf_tracing.h> | |
a6ffe7b9 | 11 | |
e7e6c774 DL |
12 | #ifndef PERF_MAX_STACK_DEPTH |
13 | #define PERF_MAX_STACK_DEPTH 127 | |
14 | #endif | |
15 | ||
e4d9c232 IL |
16 | #define _(P) \ |
17 | ({ \ | |
18 | typeof(P) val; \ | |
19 | bpf_probe_read_kernel(&val, sizeof(val), &(P)); \ | |
20 | val; \ | |
21 | }) | |
a6ffe7b9 AS |
22 | |
23 | #define MINBLOCK_US 1 | |
d1bf7c4d | 24 | #define MAX_ENTRIES 10000 |
a6ffe7b9 AS |
25 | |
26 | struct key_t { | |
27 | char waker[TASK_COMM_LEN]; | |
28 | char target[TASK_COMM_LEN]; | |
29 | u32 wret; | |
30 | u32 tret; | |
31 | }; | |
32 | ||
f0c328f8 DL |
33 | struct { |
34 | __uint(type, BPF_MAP_TYPE_HASH); | |
35 | __type(key, struct key_t); | |
36 | __type(value, u64); | |
d1bf7c4d | 37 | __uint(max_entries, MAX_ENTRIES); |
f0c328f8 | 38 | } counts SEC(".maps"); |
a6ffe7b9 | 39 | |
f0c328f8 DL |
40 | struct { |
41 | __uint(type, BPF_MAP_TYPE_HASH); | |
42 | __type(key, u32); | |
43 | __type(value, u64); | |
d1bf7c4d | 44 | __uint(max_entries, MAX_ENTRIES); |
f0c328f8 | 45 | } start SEC(".maps"); |
a6ffe7b9 AS |
46 | |
47 | struct wokeby_t { | |
48 | char name[TASK_COMM_LEN]; | |
49 | u32 ret; | |
50 | }; | |
51 | ||
f0c328f8 DL |
52 | struct { |
53 | __uint(type, BPF_MAP_TYPE_HASH); | |
54 | __type(key, u32); | |
55 | __type(value, struct wokeby_t); | |
d1bf7c4d | 56 | __uint(max_entries, MAX_ENTRIES); |
f0c328f8 | 57 | } wokeby SEC(".maps"); |
a6ffe7b9 | 58 | |
f0c328f8 DL |
59 | struct { |
60 | __uint(type, BPF_MAP_TYPE_STACK_TRACE); | |
61 | __uint(key_size, sizeof(u32)); | |
62 | __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64)); | |
d1bf7c4d | 63 | __uint(max_entries, MAX_ENTRIES); |
f0c328f8 | 64 | } stackmap SEC(".maps"); |
a6ffe7b9 AS |
65 | |
66 | #define STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP) | |
67 | ||
68 | SEC("kprobe/try_to_wake_up") | |
69 | int waker(struct pt_regs *ctx) | |
70 | { | |
71 | struct task_struct *p = (void *) PT_REGS_PARM1(ctx); | |
02413cab | 72 | struct wokeby_t woke; |
a6ffe7b9 AS |
73 | u32 pid; |
74 | ||
75 | pid = _(p->pid); | |
76 | ||
77 | bpf_get_current_comm(&woke.name, sizeof(woke.name)); | |
78 | woke.ret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS); | |
79 | ||
80 | bpf_map_update_elem(&wokeby, &pid, &woke, BPF_ANY); | |
81 | return 0; | |
82 | } | |
83 | ||
3c9b1644 | 84 | static inline int update_counts(void *ctx, u32 pid, u64 delta) |
a6ffe7b9 | 85 | { |
a6ffe7b9 AS |
86 | struct wokeby_t *woke; |
87 | u64 zero = 0, *val; | |
02413cab | 88 | struct key_t key; |
a6ffe7b9 | 89 | |
02413cab | 90 | __builtin_memset(&key.waker, 0, sizeof(key.waker)); |
a6ffe7b9 AS |
91 | bpf_get_current_comm(&key.target, sizeof(key.target)); |
92 | key.tret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS); | |
02413cab | 93 | key.wret = 0; |
a6ffe7b9 AS |
94 | |
95 | woke = bpf_map_lookup_elem(&wokeby, &pid); | |
96 | if (woke) { | |
97 | key.wret = woke->ret; | |
02413cab | 98 | __builtin_memcpy(&key.waker, woke->name, sizeof(key.waker)); |
a6ffe7b9 AS |
99 | bpf_map_delete_elem(&wokeby, &pid); |
100 | } | |
101 | ||
102 | val = bpf_map_lookup_elem(&counts, &key); | |
103 | if (!val) { | |
104 | bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST); | |
105 | val = bpf_map_lookup_elem(&counts, &key); | |
106 | if (!val) | |
107 | return 0; | |
108 | } | |
109 | (*val) += delta; | |
110 | return 0; | |
111 | } | |
112 | ||
3c9b1644 | 113 | #if 1 |
27d7fdf0 | 114 | /* taken from /sys/kernel/tracing/events/sched/sched_switch/format */ |
3c9b1644 | 115 | SEC("tracepoint/sched/sched_switch") |
e7e6c774 | 116 | int oncpu(struct trace_event_raw_sched_switch *ctx) |
3c9b1644 AS |
117 | { |
118 | /* record previous thread sleep time */ | |
119 | u32 pid = ctx->prev_pid; | |
120 | #else | |
a6ffe7b9 AS |
121 | SEC("kprobe/finish_task_switch") |
122 | int oncpu(struct pt_regs *ctx) | |
123 | { | |
124 | struct task_struct *p = (void *) PT_REGS_PARM1(ctx); | |
3c9b1644 AS |
125 | /* record previous thread sleep time */ |
126 | u32 pid = _(p->pid); | |
127 | #endif | |
a6ffe7b9 | 128 | u64 delta, ts, *tsp; |
a6ffe7b9 | 129 | |
a6ffe7b9 AS |
130 | ts = bpf_ktime_get_ns(); |
131 | bpf_map_update_elem(&start, &pid, &ts, BPF_ANY); | |
132 | ||
133 | /* calculate current thread's delta time */ | |
134 | pid = bpf_get_current_pid_tgid(); | |
135 | tsp = bpf_map_lookup_elem(&start, &pid); | |
136 | if (!tsp) | |
137 | /* missed start or filtered */ | |
138 | return 0; | |
139 | ||
140 | delta = bpf_ktime_get_ns() - *tsp; | |
141 | bpf_map_delete_elem(&start, &pid); | |
142 | delta = delta / 1000; | |
143 | if (delta < MINBLOCK_US) | |
144 | return 0; | |
145 | ||
146 | return update_counts(ctx, pid, delta); | |
147 | } | |
148 | char _license[] SEC("license") = "GPL"; | |
149 | u32 _version SEC("version") = LINUX_VERSION_CODE; |