blob: 4866afd054dab8eee087d269ee0ec4949e5c95b0 [file] [log] [blame]
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -08001/* 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 Starovoitova6ffe7b2016-02-17 19:58:59 -08008#include <uapi/linux/ptrace.h>
9#include <uapi/linux/perf_event.h>
10#include <linux/version.h>
11#include <linux/sched.h>
Yonghong Song03421a92020-05-13 11:02:23 -070012#include <bpf/bpf_helpers.h>
13#include <bpf/bpf_tracing.h>
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080014
Ilya Leoshkeviche4d9c232020-07-20 13:48:06 +020015#define _(P) \
16 ({ \
17 typeof(P) val; \
18 bpf_probe_read_kernel(&val, sizeof(val), &(P)); \
19 val; \
20 })
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080021
22#define MINBLOCK_US 1
Muhammad Falak R Wanid1bf7c42021-08-15 12:20:13 +053023#define MAX_ENTRIES 10000
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080024
25struct key_t {
26 char waker[TASK_COMM_LEN];
27 char target[TASK_COMM_LEN];
28 u32 wret;
29 u32 tret;
30};
31
Daniel T. Leef0c328f2020-08-23 17:53:34 +090032struct {
33 __uint(type, BPF_MAP_TYPE_HASH);
34 __type(key, struct key_t);
35 __type(value, u64);
Muhammad Falak R Wanid1bf7c42021-08-15 12:20:13 +053036 __uint(max_entries, MAX_ENTRIES);
Daniel T. Leef0c328f2020-08-23 17:53:34 +090037} counts SEC(".maps");
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080038
Daniel T. Leef0c328f2020-08-23 17:53:34 +090039struct {
40 __uint(type, BPF_MAP_TYPE_HASH);
41 __type(key, u32);
42 __type(value, u64);
Muhammad Falak R Wanid1bf7c42021-08-15 12:20:13 +053043 __uint(max_entries, MAX_ENTRIES);
Daniel T. Leef0c328f2020-08-23 17:53:34 +090044} start SEC(".maps");
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080045
46struct wokeby_t {
47 char name[TASK_COMM_LEN];
48 u32 ret;
49};
50
Daniel T. Leef0c328f2020-08-23 17:53:34 +090051struct {
52 __uint(type, BPF_MAP_TYPE_HASH);
53 __type(key, u32);
54 __type(value, struct wokeby_t);
Muhammad Falak R Wanid1bf7c42021-08-15 12:20:13 +053055 __uint(max_entries, MAX_ENTRIES);
Daniel T. Leef0c328f2020-08-23 17:53:34 +090056} wokeby SEC(".maps");
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080057
Daniel T. Leef0c328f2020-08-23 17:53:34 +090058struct {
59 __uint(type, BPF_MAP_TYPE_STACK_TRACE);
60 __uint(key_size, sizeof(u32));
61 __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
Muhammad Falak R Wanid1bf7c42021-08-15 12:20:13 +053062 __uint(max_entries, MAX_ENTRIES);
Daniel T. Leef0c328f2020-08-23 17:53:34 +090063} stackmap SEC(".maps");
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080064
65#define STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP)
66
67SEC("kprobe/try_to_wake_up")
68int waker(struct pt_regs *ctx)
69{
70 struct task_struct *p = (void *) PT_REGS_PARM1(ctx);
Daniel Borkmann02413ca2016-04-13 00:10:53 +020071 struct wokeby_t woke;
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080072 u32 pid;
73
74 pid = _(p->pid);
75
76 bpf_get_current_comm(&woke.name, sizeof(woke.name));
77 woke.ret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
78
79 bpf_map_update_elem(&wokeby, &pid, &woke, BPF_ANY);
80 return 0;
81}
82
Alexei Starovoitov3c9b1642016-04-06 18:43:30 -070083static inline int update_counts(void *ctx, u32 pid, u64 delta)
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080084{
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080085 struct wokeby_t *woke;
86 u64 zero = 0, *val;
Daniel Borkmann02413ca2016-04-13 00:10:53 +020087 struct key_t key;
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080088
Daniel Borkmann02413ca2016-04-13 00:10:53 +020089 __builtin_memset(&key.waker, 0, sizeof(key.waker));
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080090 bpf_get_current_comm(&key.target, sizeof(key.target));
91 key.tret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
Daniel Borkmann02413ca2016-04-13 00:10:53 +020092 key.wret = 0;
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080093
94 woke = bpf_map_lookup_elem(&wokeby, &pid);
95 if (woke) {
96 key.wret = woke->ret;
Daniel Borkmann02413ca2016-04-13 00:10:53 +020097 __builtin_memcpy(&key.waker, woke->name, sizeof(key.waker));
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -080098 bpf_map_delete_elem(&wokeby, &pid);
99 }
100
101 val = bpf_map_lookup_elem(&counts, &key);
102 if (!val) {
103 bpf_map_update_elem(&counts, &key, &zero, BPF_NOEXIST);
104 val = bpf_map_lookup_elem(&counts, &key);
105 if (!val)
106 return 0;
107 }
108 (*val) += delta;
109 return 0;
110}
111
Alexei Starovoitov3c9b1642016-04-06 18:43:30 -0700112#if 1
113/* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */
114struct sched_switch_args {
115 unsigned long long pad;
116 char prev_comm[16];
117 int prev_pid;
118 int prev_prio;
119 long long prev_state;
120 char next_comm[16];
121 int next_pid;
122 int next_prio;
123};
124SEC("tracepoint/sched/sched_switch")
125int oncpu(struct sched_switch_args *ctx)
126{
127 /* record previous thread sleep time */
128 u32 pid = ctx->prev_pid;
129#else
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -0800130SEC("kprobe/finish_task_switch")
131int oncpu(struct pt_regs *ctx)
132{
133 struct task_struct *p = (void *) PT_REGS_PARM1(ctx);
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -0800134 /* record previous thread sleep time */
Alexei Starovoitov3c9b1642016-04-06 18:43:30 -0700135 u32 pid = _(p->pid);
136#endif
137 u64 delta, ts, *tsp;
138
Alexei Starovoitova6ffe7b2016-02-17 19:58:59 -0800139 ts = bpf_ktime_get_ns();
140 bpf_map_update_elem(&start, &pid, &ts, BPF_ANY);
141
142 /* calculate current thread's delta time */
143 pid = bpf_get_current_pid_tgid();
144 tsp = bpf_map_lookup_elem(&start, &pid);
145 if (!tsp)
146 /* missed start or filtered */
147 return 0;
148
149 delta = bpf_ktime_get_ns() - *tsp;
150 bpf_map_delete_elem(&start, &pid);
151 delta = delta / 1000;
152 if (delta < MINBLOCK_US)
153 return 0;
154
155 return update_counts(ctx, pid, delta);
156}
157char _license[] SEC("license") = "GPL";
158u32 _version SEC("version") = LINUX_VERSION_CODE;