Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 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 | */ |
| 7 | #include <uapi/linux/bpf.h> |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 8 | #include <uapi/linux/ptrace.h> |
| 9 | #include <uapi/linux/perf_event.h> |
| 10 | #include <linux/version.h> |
| 11 | #include <linux/sched.h> |
Yonghong Song | 03421a9 | 2020-05-13 11:02:23 -0700 | [diff] [blame] | 12 | #include <bpf/bpf_helpers.h> |
| 13 | #include <bpf/bpf_tracing.h> |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 14 | |
Ilya Leoshkevich | e4d9c23 | 2020-07-20 13:48:06 +0200 | [diff] [blame] | 15 | #define _(P) \ |
| 16 | ({ \ |
| 17 | typeof(P) val; \ |
| 18 | bpf_probe_read_kernel(&val, sizeof(val), &(P)); \ |
| 19 | val; \ |
| 20 | }) |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 21 | |
| 22 | #define MINBLOCK_US 1 |
| 23 | |
| 24 | struct key_t { |
| 25 | char waker[TASK_COMM_LEN]; |
| 26 | char target[TASK_COMM_LEN]; |
| 27 | u32 wret; |
| 28 | u32 tret; |
| 29 | }; |
| 30 | |
| 31 | struct bpf_map_def SEC("maps") counts = { |
| 32 | .type = BPF_MAP_TYPE_HASH, |
| 33 | .key_size = sizeof(struct key_t), |
| 34 | .value_size = sizeof(u64), |
| 35 | .max_entries = 10000, |
| 36 | }; |
| 37 | |
| 38 | struct bpf_map_def SEC("maps") start = { |
| 39 | .type = BPF_MAP_TYPE_HASH, |
| 40 | .key_size = sizeof(u32), |
| 41 | .value_size = sizeof(u64), |
| 42 | .max_entries = 10000, |
| 43 | }; |
| 44 | |
| 45 | struct wokeby_t { |
| 46 | char name[TASK_COMM_LEN]; |
| 47 | u32 ret; |
| 48 | }; |
| 49 | |
| 50 | struct bpf_map_def SEC("maps") wokeby = { |
| 51 | .type = BPF_MAP_TYPE_HASH, |
| 52 | .key_size = sizeof(u32), |
| 53 | .value_size = sizeof(struct wokeby_t), |
| 54 | .max_entries = 10000, |
| 55 | }; |
| 56 | |
| 57 | struct bpf_map_def SEC("maps") stackmap = { |
| 58 | .type = BPF_MAP_TYPE_STACK_TRACE, |
| 59 | .key_size = sizeof(u32), |
| 60 | .value_size = PERF_MAX_STACK_DEPTH * sizeof(u64), |
| 61 | .max_entries = 10000, |
| 62 | }; |
| 63 | |
| 64 | #define STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP) |
| 65 | |
| 66 | SEC("kprobe/try_to_wake_up") |
| 67 | int waker(struct pt_regs *ctx) |
| 68 | { |
| 69 | struct task_struct *p = (void *) PT_REGS_PARM1(ctx); |
Daniel Borkmann | 02413ca | 2016-04-13 00:10:53 +0200 | [diff] [blame] | 70 | struct wokeby_t woke; |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 71 | u32 pid; |
| 72 | |
| 73 | pid = _(p->pid); |
| 74 | |
| 75 | bpf_get_current_comm(&woke.name, sizeof(woke.name)); |
| 76 | woke.ret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS); |
| 77 | |
| 78 | bpf_map_update_elem(&wokeby, &pid, &woke, BPF_ANY); |
| 79 | return 0; |
| 80 | } |
| 81 | |
Alexei Starovoitov | 3c9b164 | 2016-04-06 18:43:30 -0700 | [diff] [blame] | 82 | static inline int update_counts(void *ctx, u32 pid, u64 delta) |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 83 | { |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 84 | struct wokeby_t *woke; |
| 85 | u64 zero = 0, *val; |
Daniel Borkmann | 02413ca | 2016-04-13 00:10:53 +0200 | [diff] [blame] | 86 | struct key_t key; |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 87 | |
Daniel Borkmann | 02413ca | 2016-04-13 00:10:53 +0200 | [diff] [blame] | 88 | __builtin_memset(&key.waker, 0, sizeof(key.waker)); |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 89 | bpf_get_current_comm(&key.target, sizeof(key.target)); |
| 90 | key.tret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS); |
Daniel Borkmann | 02413ca | 2016-04-13 00:10:53 +0200 | [diff] [blame] | 91 | key.wret = 0; |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 92 | |
| 93 | woke = bpf_map_lookup_elem(&wokeby, &pid); |
| 94 | if (woke) { |
| 95 | key.wret = woke->ret; |
Daniel Borkmann | 02413ca | 2016-04-13 00:10:53 +0200 | [diff] [blame] | 96 | __builtin_memcpy(&key.waker, woke->name, sizeof(key.waker)); |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 97 | bpf_map_delete_elem(&wokeby, &pid); |
| 98 | } |
| 99 | |
| 100 | val = bpf_map_lookup_elem(&counts, &key); |
| 101 | if (!val) { |
| 102 | bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST); |
| 103 | val = bpf_map_lookup_elem(&counts, &key); |
| 104 | if (!val) |
| 105 | return 0; |
| 106 | } |
| 107 | (*val) += delta; |
| 108 | return 0; |
| 109 | } |
| 110 | |
Alexei Starovoitov | 3c9b164 | 2016-04-06 18:43:30 -0700 | [diff] [blame] | 111 | #if 1 |
| 112 | /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */ |
| 113 | struct sched_switch_args { |
| 114 | unsigned long long pad; |
| 115 | char prev_comm[16]; |
| 116 | int prev_pid; |
| 117 | int prev_prio; |
| 118 | long long prev_state; |
| 119 | char next_comm[16]; |
| 120 | int next_pid; |
| 121 | int next_prio; |
| 122 | }; |
| 123 | SEC("tracepoint/sched/sched_switch") |
| 124 | int oncpu(struct sched_switch_args *ctx) |
| 125 | { |
| 126 | /* record previous thread sleep time */ |
| 127 | u32 pid = ctx->prev_pid; |
| 128 | #else |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 129 | SEC("kprobe/finish_task_switch") |
| 130 | int oncpu(struct pt_regs *ctx) |
| 131 | { |
| 132 | struct task_struct *p = (void *) PT_REGS_PARM1(ctx); |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 133 | /* record previous thread sleep time */ |
Alexei Starovoitov | 3c9b164 | 2016-04-06 18:43:30 -0700 | [diff] [blame] | 134 | u32 pid = _(p->pid); |
| 135 | #endif |
| 136 | u64 delta, ts, *tsp; |
| 137 | |
Alexei Starovoitov | a6ffe7b | 2016-02-17 19:58:59 -0800 | [diff] [blame] | 138 | ts = bpf_ktime_get_ns(); |
| 139 | bpf_map_update_elem(&start, &pid, &ts, BPF_ANY); |
| 140 | |
| 141 | /* calculate current thread's delta time */ |
| 142 | pid = bpf_get_current_pid_tgid(); |
| 143 | tsp = bpf_map_lookup_elem(&start, &pid); |
| 144 | if (!tsp) |
| 145 | /* missed start or filtered */ |
| 146 | return 0; |
| 147 | |
| 148 | delta = bpf_ktime_get_ns() - *tsp; |
| 149 | bpf_map_delete_elem(&start, &pid); |
| 150 | delta = delta / 1000; |
| 151 | if (delta < MINBLOCK_US) |
| 152 | return 0; |
| 153 | |
| 154 | return update_counts(ctx, pid, delta); |
| 155 | } |
| 156 | char _license[] SEC("license") = "GPL"; |
| 157 | u32 _version SEC("version") = LINUX_VERSION_CODE; |