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