-
Notifications
You must be signed in to change notification settings - Fork 240
/
maps.bpf.h
81 lines (69 loc) · 6.7 KB
/
maps.bpf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "bits.bpf.h"
#define lookup_or_zero_init_key(map, key, into) \
u64 zero = 0; \
\
into = bpf_map_lookup_elem(map, key); \
if (!into) { \
bpf_map_update_elem(map, key, &zero, BPF_NOEXIST); \
into = bpf_map_lookup_elem(map, key); \
if (!into) { \
return 0; \
} \
}
#define increment_variant(map, key, increment, variant) \
u64 *count; \
\
lookup_or_zero_init_key(map, key, count); \
\
variant; \
\
return *count;
static inline int increment_map(void *map, void *key, u64 increment)
{
increment_variant(map, key, increment, __sync_fetch_and_add(count, increment));
}
static inline int increment_map_nosync(void *map, void *key, u64 increment)
{
increment_variant(map, key, increment, *count += increment);
}
// Arrays are always preallocated, so this only fails if the key is missing
#define read_array_ptr(map, key, into) \
into = bpf_map_lookup_elem(map, key); \
if (!into) { \
return 0; \
}
#define _increment_histogram(map, key, increment, max_bucket, increment_fn) \
if (key.bucket > max_bucket) { \
key.bucket = max_bucket; \
} \
\
increment_fn(map, &key, 1); \
\
if (increment > 0) { \
key.bucket = max_bucket + 1; \
increment_fn(map, &key, increment); \
}
#define _increment_ex2_histogram(map, key, increment, max_bucket, increment_fn) \
key.bucket = log2l(increment); \
\
if (key.bucket > max_bucket) { \
key.bucket = max_bucket; \
} \
\
_increment_histogram(map, key, increment, max_bucket, increment_fn);
#define increment_exp2_histogram(map, key, increment, max_bucket) \
_increment_ex2_histogram(map, key, increment, max_bucket, increment_map)
#define increment_exp2_histogram_nosync(map, key, increment, max_bucket) \
_increment_ex2_histogram(map, key, increment, max_bucket, increment_map_nosync)
#define _increment_exp2zero_histogram(map, key, increment, max_bucket, increment_fn) \
if (increment == 0) { \
key.bucket = 0; \
} else { \
key.bucket = log2l(increment) + 1; \
} \
\
_increment_histogram(map, key, increment, max_bucket, increment_fn);
#define increment_exp2zero_histogram(map, key, increment, max_bucket) \
_increment_exp2zero_histogram(map, key, increment, max_bucket, increment_map)
#define increment_exp2zero_histogram_nosync(map, key, increment, max_bucket) \
_increment_exp2zero_histogram(map, key, increment, max_bucket, increment_map_nosync)