1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (c) 2020 Facebook
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
7 struct bpf_perf_event_value___local {
11 } __attribute__((preserve_access_index));
13 /* map of perf event fds, num_cpu * num_metric entries */
15 __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
16 __uint(key_size, sizeof(u32));
17 __uint(value_size, sizeof(int));
18 } events SEC(".maps");
20 /* readings at fentry */
22 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
23 __uint(key_size, sizeof(u32));
24 __uint(value_size, sizeof(struct bpf_perf_event_value___local));
25 } fentry_readings SEC(".maps");
27 /* accumulated readings */
29 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
30 __uint(key_size, sizeof(u32));
31 __uint(value_size, sizeof(struct bpf_perf_event_value___local));
32 } accum_readings SEC(".maps");
34 /* sample counts, one per cpu */
36 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
37 __uint(key_size, sizeof(u32));
38 __uint(value_size, sizeof(u64));
39 } counts SEC(".maps");
41 const volatile __u32 num_cpu = 1;
42 const volatile __u32 num_metric = 1;
43 #define MAX_NUM_MATRICS 4
46 int BPF_PROG(fentry_XXX)
48 struct bpf_perf_event_value___local *ptrs[MAX_NUM_MATRICS];
49 u32 key = bpf_get_smp_processor_id();
52 /* look up before reading, to reduce error */
53 for (i = 0; i < num_metric && i < MAX_NUM_MATRICS; i++) {
56 ptrs[i] = bpf_map_lookup_elem(&fentry_readings, &flag);
61 for (i = 0; i < num_metric && i < MAX_NUM_MATRICS; i++) {
62 struct bpf_perf_event_value___local reading;
65 err = bpf_perf_event_read_value(&events, key, (void *)&reading,
77 fexit_update_maps(u32 id, struct bpf_perf_event_value___local *after)
79 struct bpf_perf_event_value___local *before, diff;
81 before = bpf_map_lookup_elem(&fentry_readings, &id);
82 /* only account samples with a valid fentry_reading */
83 if (before && before->counter) {
84 struct bpf_perf_event_value___local *accum;
86 diff.counter = after->counter - before->counter;
87 diff.enabled = after->enabled - before->enabled;
88 diff.running = after->running - before->running;
90 accum = bpf_map_lookup_elem(&accum_readings, &id);
92 accum->counter += diff.counter;
93 accum->enabled += diff.enabled;
94 accum->running += diff.running;
100 int BPF_PROG(fexit_XXX)
102 struct bpf_perf_event_value___local readings[MAX_NUM_MATRICS];
103 u32 cpu = bpf_get_smp_processor_id();
108 /* read all events before updating the maps, to reduce error */
109 for (i = 0; i < num_metric && i < MAX_NUM_MATRICS; i++) {
110 err = bpf_perf_event_read_value(&events, cpu + i * num_cpu,
111 (void *)(readings + i),
116 count = bpf_map_lookup_elem(&counts, &zero);
119 for (i = 0; i < num_metric && i < MAX_NUM_MATRICS; i++)
120 fexit_update_maps(i, &readings[i]);
125 char LICENSE[] SEC("license") = "Dual BSD/GPL";