|
| 1 | +#include <linux/version.h> |
| 2 | +#include <uapi/linux/bpf.h> |
| 3 | + |
| 4 | +#include <asm/page_types.h> |
| 5 | + |
| 6 | +/* asm/fpu/types.h assumes __packed is defined */ |
| 7 | +#define __packed __attribute__((packed)) |
| 8 | +#include <asm/fpu/types.h> |
| 9 | + |
| 10 | +#define SEC(NAME) __attribute__((section(NAME), used)) |
| 11 | + |
| 12 | +#define BUF_SIZE_MAP_NS 256 |
| 13 | + |
| 14 | +typedef struct bpf_map_def { |
| 15 | + unsigned int type; |
| 16 | + unsigned int key_size; |
| 17 | + unsigned int value_size; |
| 18 | + unsigned int max_entries; |
| 19 | + unsigned int map_flags; |
| 20 | + unsigned int pinning; |
| 21 | + char namespace[BUF_SIZE_MAP_NS]; |
| 22 | +} bpf_map_def; |
| 23 | + |
| 24 | +static int (*bpf_probe_read)(void *dst, u64 size, const void *unsafe_ptr) = |
| 25 | + (void *)BPF_FUNC_probe_read; |
| 26 | + |
| 27 | +static u64 (*bpf_get_current_cgroup_id)(void) = (void *) |
| 28 | + BPF_FUNC_get_current_cgroup_id; |
| 29 | + |
| 30 | +static int (*bpf_map_update_elem)(void *map, void *key, void *value, |
| 31 | + u64 flags) = (void *)BPF_FUNC_map_update_elem; |
| 32 | + |
| 33 | +static void *(*bpf_map_lookup_elem)(void *map, void *key) = (void *) |
| 34 | + BPF_FUNC_map_lookup_elem; |
| 35 | + |
| 36 | +#define bpf_printk(fmt, ...) \ |
| 37 | + ({ \ |
| 38 | + char ____fmt[] = fmt; \ |
| 39 | + bpf_trace_printk(____fmt, sizeof(____fmt), ##__VA_ARGS__); \ |
| 40 | + }) |
| 41 | +static int (*bpf_trace_printk)(const char *fmt, int fmt_size, |
| 42 | + ...) = (void *)BPF_FUNC_trace_printk; |
| 43 | + |
| 44 | +struct bpf_map_def |
| 45 | + SEC("maps/all_context_switch_count") all_context_switch_count_hash = { |
| 46 | + .type = BPF_MAP_TYPE_HASH, |
| 47 | + .key_size = sizeof(u64), |
| 48 | + .value_size = sizeof(u32), |
| 49 | + .max_entries = 1024, |
| 50 | + }; |
| 51 | + |
| 52 | +struct bpf_map_def |
| 53 | + SEC("maps/avx_context_switch_count") avx_context_switch_count_hash = { |
| 54 | + .type = BPF_MAP_TYPE_HASH, |
| 55 | + .key_size = sizeof(u64), |
| 56 | + .value_size = sizeof(u32), |
| 57 | + .max_entries = 1024, |
| 58 | + }; |
| 59 | + |
| 60 | +struct bpf_map_def |
| 61 | + SEC("maps/avx_timestamp") avx_timestamp_hash = { |
| 62 | + .type = BPF_MAP_TYPE_HASH, |
| 63 | + .key_size = sizeof(u64), |
| 64 | + .value_size = sizeof(u32), |
| 65 | + .max_entries = 1024, |
| 66 | + }; |
| 67 | + |
| 68 | +struct bpf_map_def SEC("maps/cpu") cpu_hash = { |
| 69 | + .type = BPF_MAP_TYPE_HASH, |
| 70 | + .key_size = sizeof(unsigned int), |
| 71 | + .value_size = sizeof(u32), |
| 72 | + .max_entries = 128, |
| 73 | +}; |
| 74 | + |
| 75 | +struct sched_switch_args { |
| 76 | + u64 pad; |
| 77 | + char prev_comm[16]; |
| 78 | + int prev_pid; |
| 79 | + int prev_prio; |
| 80 | + long long prev_state; |
| 81 | + char next_comm[16]; |
| 82 | + int next_pid; |
| 83 | + int next_prio; |
| 84 | +}; |
| 85 | + |
| 86 | +SEC("tracepoint/sched/sched_switch") |
| 87 | +int tracepoint__sched_switch(struct sched_switch_args *args) |
| 88 | +{ |
| 89 | + u64 cgroup_id = bpf_get_current_cgroup_id(); |
| 90 | + u32 *count; |
| 91 | + u32 new_count = 1; |
| 92 | + |
| 93 | + count = bpf_map_lookup_elem(&all_context_switch_count_hash, &cgroup_id); |
| 94 | + if (count) { |
| 95 | + __sync_fetch_and_add(count, 1); |
| 96 | + } else { |
| 97 | + bpf_map_update_elem(&all_context_switch_count_hash, &cgroup_id, |
| 98 | + &new_count, BPF_ANY); |
| 99 | + } |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +struct x86_fpu_args { |
| 104 | + u64 pad; |
| 105 | + struct fpu *fpu; |
| 106 | + bool load_fpu; |
| 107 | + u64 xfeatures; |
| 108 | + u64 xcomp_bv; |
| 109 | +}; |
| 110 | + |
| 111 | +SEC("tracepoint/x86_fpu/x86_fpu_regs_deactivated") |
| 112 | +int tracepoint__x86_fpu_regs_deactivated(struct x86_fpu_args *args) |
| 113 | +{ |
| 114 | + u32 *counter; |
| 115 | + u32 ts; |
| 116 | + bpf_probe_read(&ts, sizeof(u32), (void *)&args->fpu->avx512_timestamp); |
| 117 | + |
| 118 | + if (ts == 0) { |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | + u64 cgroup_id = bpf_get_current_cgroup_id(); |
| 123 | + |
| 124 | + u32 ts_prev; |
| 125 | + u32 *tsp; |
| 126 | + tsp = bpf_map_lookup_elem(&avx_timestamp_hash, &cgroup_id); |
| 127 | + |
| 128 | + ts_prev = tsp ? *tsp : 0; |
| 129 | + |
| 130 | + if (ts == ts_prev) { |
| 131 | + return 0; |
| 132 | + } |
| 133 | + bpf_map_update_elem(&avx_timestamp_hash, &cgroup_id, &ts, BPF_ANY); |
| 134 | + |
| 135 | + unsigned int last_cpu; |
| 136 | + bpf_probe_read(&last_cpu, sizeof(last_cpu), |
| 137 | + (void *)&args->fpu->last_cpu); |
| 138 | + |
| 139 | + u32 count = 1; |
| 140 | + counter = bpf_map_lookup_elem(&cpu_hash, &last_cpu); |
| 141 | + if (counter) { |
| 142 | + __sync_fetch_and_add(counter, 1); |
| 143 | + } else { |
| 144 | + bpf_map_update_elem(&cpu_hash, &last_cpu, &count, BPF_ANY); |
| 145 | + } |
| 146 | + |
| 147 | + counter = bpf_map_lookup_elem(&avx_context_switch_count_hash, &cgroup_id); |
| 148 | + if (counter) { |
| 149 | + __sync_fetch_and_add(counter, 1); |
| 150 | + } else { |
| 151 | + bpf_map_update_elem(&avx_context_switch_count_hash, &cgroup_id, |
| 152 | + &count, BPF_ANY); |
| 153 | + } |
| 154 | + |
| 155 | + bpf_printk("AVX512 detected in cgroup %llu\n", cgroup_id); |
| 156 | + return 0; |
| 157 | +} |
| 158 | + |
| 159 | +char _license[] SEC("license") = "GPL"; |
| 160 | + |
| 161 | +unsigned int _version SEC("version") = LINUX_VERSION_CODE; |
0 commit comments