|
2 | 2 |
|
3 | 3 | // Produce values that are usable for prometheus. See: |
4 | 4 | // * https://github.com/cloudflare/ebpf_exporter/issues/488 |
5 | | -static __always_inline u64 log2l_histogram(u64 v) { |
6 | | - u64 rounded = log2l(v); |
| 5 | +static __always_inline u64 log2l_histogram(u64 v) |
| 6 | +{ |
| 7 | + u64 rounded = log2l(v); |
7 | 8 |
|
8 | | - if (rounded == 0) { |
9 | | - return 0; |
10 | | - } |
| 9 | + if (rounded == 0) { |
| 10 | + return 0; |
| 11 | + } |
11 | 12 |
|
12 | | - if (2ULL << (rounded - 1) == v) { |
13 | | - return rounded; |
14 | | - } else { |
15 | | - return rounded + 1; |
16 | | - } |
| 13 | + if (2ULL << (rounded - 1) == v) { |
| 14 | + return rounded; |
| 15 | + } else { |
| 16 | + return rounded + 1; |
| 17 | + } |
17 | 18 | } |
18 | 19 |
|
19 | | -#define lookup_or_zero_init_key(map, key, into) \ |
20 | | - u64 zero = 0; \ |
21 | | - \ |
22 | | - into = bpf_map_lookup_elem(map, key); \ |
23 | | - if (!into) { \ |
24 | | - bpf_map_update_elem(map, key, &zero, BPF_NOEXIST); \ |
25 | | - into = bpf_map_lookup_elem(map, key); \ |
26 | | - if (!into) { \ |
27 | | - return 0; \ |
28 | | - } \ |
29 | | - } |
| 20 | +#define lookup_or_zero_init_key(map, key, into) \ |
| 21 | + u64 zero = 0; \ |
| 22 | + \ |
| 23 | + into = bpf_map_lookup_elem(map, key); \ |
| 24 | + if (!into) { \ |
| 25 | + bpf_map_update_elem(map, key, &zero, BPF_NOEXIST); \ |
| 26 | + into = bpf_map_lookup_elem(map, key); \ |
| 27 | + if (!into) { \ |
| 28 | + return 0; \ |
| 29 | + } \ |
| 30 | + } |
30 | 31 |
|
31 | | -#define increment_variant(map, key, increment, variant) \ |
32 | | - u64 *count; \ |
33 | | - \ |
34 | | - lookup_or_zero_init_key(map, key, count); \ |
35 | | - \ |
36 | | - variant; \ |
37 | | - \ |
38 | | - return *count; |
| 32 | +#define increment_variant(map, key, increment, variant) \ |
| 33 | + u64 *count; \ |
| 34 | + \ |
| 35 | + lookup_or_zero_init_key(map, key, count); \ |
| 36 | + \ |
| 37 | + variant; \ |
| 38 | + \ |
| 39 | + return *count; |
39 | 40 |
|
40 | | -static inline int increment_map(void *map, void *key, u64 increment) { |
41 | | - increment_variant(map, key, increment, |
42 | | - __sync_fetch_and_add(count, increment)); |
| 41 | +static inline int increment_map(void *map, void *key, u64 increment) |
| 42 | +{ |
| 43 | + increment_variant(map, key, increment, __sync_fetch_and_add(count, increment)); |
43 | 44 | } |
44 | 45 |
|
45 | | -static inline int increment_map_nosync(void *map, void *key, u64 increment) { |
46 | | - increment_variant(map, key, increment, *count += increment); |
| 46 | +static inline int increment_map_nosync(void *map, void *key, u64 increment) |
| 47 | +{ |
| 48 | + increment_variant(map, key, increment, *count += increment); |
47 | 49 | } |
48 | 50 |
|
49 | 51 | // Arrays are always preallocated, so this only fails if the key is missing |
50 | | -#define read_array_ptr(map, key, into) \ |
51 | | - into = bpf_map_lookup_elem(map, key); \ |
52 | | - if (!into) { \ |
53 | | - return 0; \ |
54 | | - } |
| 52 | +#define read_array_ptr(map, key, into) \ |
| 53 | + into = bpf_map_lookup_elem(map, key); \ |
| 54 | + if (!into) { \ |
| 55 | + return 0; \ |
| 56 | + } |
55 | 57 |
|
56 | | -#define _increment_histogram(map, key, increment, max_bucket, increment_fn) \ |
57 | | - if (key.bucket > max_bucket) { \ |
58 | | - key.bucket = max_bucket; \ |
59 | | - } \ |
60 | | - \ |
61 | | - increment_fn(map, &key, 1); \ |
62 | | - \ |
63 | | - if (increment > 0) { \ |
64 | | - key.bucket = max_bucket + 1; \ |
65 | | - increment_fn(map, &key, increment); \ |
66 | | - } |
| 58 | +#define _increment_histogram(map, key, increment, max_bucket, increment_fn) \ |
| 59 | + if (key.bucket > max_bucket) { \ |
| 60 | + key.bucket = max_bucket; \ |
| 61 | + } \ |
| 62 | + \ |
| 63 | + increment_fn(map, &key, 1); \ |
| 64 | + \ |
| 65 | + if (increment > 0) { \ |
| 66 | + key.bucket = max_bucket + 1; \ |
| 67 | + increment_fn(map, &key, increment); \ |
| 68 | + } |
67 | 69 |
|
68 | | -#define _increment_ex2_histogram(map, key, increment, max_bucket, \ |
69 | | - increment_fn) \ |
70 | | - key.bucket = log2l_histogram(increment); \ |
71 | | - \ |
72 | | - _increment_histogram(map, key, increment, max_bucket, increment_fn); |
| 70 | +#define _increment_ex2_histogram(map, key, increment, max_bucket, increment_fn) \ |
| 71 | + key.bucket = log2l_histogram(increment); \ |
| 72 | + \ |
| 73 | + _increment_histogram(map, key, increment, max_bucket, increment_fn); |
73 | 74 |
|
74 | | -#define increment_exp2_histogram(map, key, increment, max_bucket) \ |
75 | | - _increment_ex2_histogram(map, key, increment, max_bucket, increment_map) |
| 75 | +#define increment_exp2_histogram(map, key, increment, max_bucket) \ |
| 76 | + _increment_ex2_histogram(map, key, increment, max_bucket, increment_map) |
76 | 77 |
|
77 | | -#define increment_exp2_histogram_nosync(map, key, increment, max_bucket) \ |
78 | | - _increment_ex2_histogram(map, key, increment, max_bucket, \ |
79 | | - increment_map_nosync) |
| 78 | +#define increment_exp2_histogram_nosync(map, key, increment, max_bucket) \ |
| 79 | + _increment_ex2_histogram(map, key, increment, max_bucket, increment_map_nosync) |
80 | 80 |
|
81 | | -#define _increment_exp2zero_histogram(map, key, increment, max_bucket, \ |
82 | | - increment_fn) \ |
83 | | - if (increment == 0) { \ |
84 | | - key.bucket = 0; \ |
85 | | - } else { \ |
86 | | - key.bucket = log2l_histogram(increment) + 1; \ |
87 | | - } \ |
88 | | - \ |
89 | | - _increment_histogram(map, key, increment, max_bucket, increment_fn); |
| 81 | +#define _increment_exp2zero_histogram(map, key, increment, max_bucket, increment_fn) \ |
| 82 | + if (increment == 0) { \ |
| 83 | + key.bucket = 0; \ |
| 84 | + } else { \ |
| 85 | + key.bucket = log2l_histogram(increment) + 1; \ |
| 86 | + } \ |
| 87 | + \ |
| 88 | + _increment_histogram(map, key, increment, max_bucket, increment_fn); |
90 | 89 |
|
91 | | -#define increment_exp2zero_histogram(map, key, increment, max_bucket) \ |
92 | | - _increment_exp2zero_histogram(map, key, increment, max_bucket, increment_map) |
| 90 | +#define increment_exp2zero_histogram(map, key, increment, max_bucket) \ |
| 91 | + _increment_exp2zero_histogram(map, key, increment, max_bucket, increment_map) |
93 | 92 |
|
94 | | -#define increment_exp2zero_histogram_nosync(map, key, increment, max_bucket) \ |
95 | | - _increment_exp2zero_histogram(map, key, increment, max_bucket, \ |
96 | | - increment_map_nosync) |
| 93 | +#define increment_exp2zero_histogram_nosync(map, key, increment, max_bucket) \ |
| 94 | + _increment_exp2zero_histogram(map, key, increment, max_bucket, increment_map_nosync) |
0 commit comments