xref: /aosp_15_r20/external/bcc/examples/tracing/task_switch.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 #include <uapi/linux/ptrace.h>
2 #include <linux/sched.h>
3 
4 struct key_t {
5     u32 prev_pid;
6     u32 curr_pid;
7 };
8 
9 BPF_HASH(stats, struct key_t, u64, 1024);
count_sched(struct pt_regs * ctx,struct task_struct * prev)10 int count_sched(struct pt_regs *ctx, struct task_struct *prev) {
11     struct key_t key = {};
12     u64 zero = 0, *val;
13 
14     key.curr_pid = bpf_get_current_pid_tgid();
15     key.prev_pid = prev->pid;
16 
17     // could also use `stats.increment(key);`
18     val = stats.lookup_or_try_init(&key, &zero);
19     if (val) {
20         (*val)++;
21     }
22     return 0;
23 }
24