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