~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/offwaketime.bpf.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 "vmlinux.h"
  8 #include <linux/version.h>
  9 #include <bpf/bpf_helpers.h>
 10 #include <bpf/bpf_tracing.h>
 11 #include <bpf/bpf_core_read.h>
 12 
 13 #ifndef PERF_MAX_STACK_DEPTH
 14 #define PERF_MAX_STACK_DEPTH         127
 15 #endif
 16 
 17 #define MINBLOCK_US     1
 18 #define MAX_ENTRIES     10000
 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 
 27 struct {
 28         __uint(type, BPF_MAP_TYPE_HASH);
 29         __type(key, struct key_t);
 30         __type(value, u64);
 31         __uint(max_entries, MAX_ENTRIES);
 32 } counts SEC(".maps");
 33 
 34 struct {
 35         __uint(type, BPF_MAP_TYPE_HASH);
 36         __type(key, u32);
 37         __type(value, u64);
 38         __uint(max_entries, MAX_ENTRIES);
 39 } start SEC(".maps");
 40 
 41 struct wokeby_t {
 42         char name[TASK_COMM_LEN];
 43         u32 ret;
 44 };
 45 
 46 struct {
 47         __uint(type, BPF_MAP_TYPE_HASH);
 48         __type(key, u32);
 49         __type(value, struct wokeby_t);
 50         __uint(max_entries, MAX_ENTRIES);
 51 } wokeby SEC(".maps");
 52 
 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));
 57         __uint(max_entries, MAX_ENTRIES);
 58 } stackmap SEC(".maps");
 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 {
 65         struct task_struct *p = (void *)PT_REGS_PARM1_CORE(ctx);
 66         u32 pid = BPF_CORE_READ(p, pid);
 67         struct wokeby_t woke;
 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 
 76 static inline int update_counts(void *ctx, u32 pid, u64 delta)
 77 {
 78         struct wokeby_t *woke;
 79         u64 zero = 0, *val;
 80         struct key_t key;
 81 
 82         __builtin_memset(&key.waker, 0, sizeof(key.waker));
 83         bpf_get_current_comm(&key.target, sizeof(key.target));
 84         key.tret = bpf_get_stackid(ctx, &stackmap, STACKID_FLAGS);
 85         key.wret = 0;
 86 
 87         woke = bpf_map_lookup_elem(&wokeby, &pid);
 88         if (woke) {
 89                 key.wret = woke->ret;
 90                 __builtin_memcpy(&key.waker, woke->name, sizeof(key.waker));
 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 
105 #if 1
106 /* taken from /sys/kernel/tracing/events/sched/sched_switch/format */
107 SEC("tracepoint/sched/sched_switch")
108 int oncpu(struct trace_event_raw_sched_switch *ctx)
109 {
110         /* record previous thread sleep time */
111         u32 pid = ctx->prev_pid;
112 #else
113 SEC("kprobe.multi/finish_task_switch*")
114 int oncpu(struct pt_regs *ctx)
115 {
116         struct task_struct *p = (void *)PT_REGS_PARM1_CORE(ctx);
117         /* record previous thread sleep time */
118         u32 pid = BPF_CORE_READ(p, pid);
119 #endif
120         u64 delta, ts, *tsp;
121 
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;
142 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php