Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2018 Facebook
0003 
0004 #include <vmlinux.h>
0005 #include <bpf/bpf_helpers.h>
0006 
0007 #ifndef PERF_MAX_STACK_DEPTH
0008 #define PERF_MAX_STACK_DEPTH         127
0009 #endif
0010 
0011 struct {
0012     __uint(type, BPF_MAP_TYPE_ARRAY);
0013     __uint(max_entries, 1);
0014     __type(key, __u32);
0015     __type(value, __u32);
0016 } control_map SEC(".maps");
0017 
0018 struct {
0019     __uint(type, BPF_MAP_TYPE_HASH);
0020     __uint(max_entries, 16384);
0021     __type(key, __u32);
0022     __type(value, __u32);
0023 } stackid_hmap SEC(".maps");
0024 
0025 typedef __u64 stack_trace_t[PERF_MAX_STACK_DEPTH];
0026 
0027 struct {
0028     __uint(type, BPF_MAP_TYPE_STACK_TRACE);
0029     __uint(max_entries, 16384);
0030     __type(key, __u32);
0031     __type(value, stack_trace_t);
0032 } stackmap SEC(".maps");
0033 
0034 struct {
0035     __uint(type, BPF_MAP_TYPE_ARRAY);
0036     __uint(max_entries, 16384);
0037     __type(key, __u32);
0038     __type(value, stack_trace_t);
0039 } stack_amap SEC(".maps");
0040 
0041 /* taken from /sys/kernel/debug/tracing/events/sched/sched_switch/format */
0042 struct sched_switch_args {
0043     unsigned long long pad;
0044     char prev_comm[TASK_COMM_LEN];
0045     int prev_pid;
0046     int prev_prio;
0047     long long prev_state;
0048     char next_comm[TASK_COMM_LEN];
0049     int next_pid;
0050     int next_prio;
0051 };
0052 
0053 SEC("tracepoint/sched/sched_switch")
0054 int oncpu(struct sched_switch_args *ctx)
0055 {
0056     __u32 max_len = PERF_MAX_STACK_DEPTH * sizeof(__u64);
0057     __u32 key = 0, val = 0, *value_p;
0058     void *stack_p;
0059 
0060     value_p = bpf_map_lookup_elem(&control_map, &key);
0061     if (value_p && *value_p)
0062         return 0; /* skip if non-zero *value_p */
0063 
0064     /* The size of stackmap and stackid_hmap should be the same */
0065     key = bpf_get_stackid(ctx, &stackmap, 0);
0066     if ((int)key >= 0) {
0067         bpf_map_update_elem(&stackid_hmap, &key, &val, 0);
0068         stack_p = bpf_map_lookup_elem(&stack_amap, &key);
0069         if (stack_p)
0070             bpf_get_stack(ctx, stack_p, max_len, 0);
0071     }
0072 
0073     return 0;
0074 }
0075 
0076 char _license[] SEC("license") = "GPL";