Skip to content

Commit f5fd482

Browse files
committed
Get custom labels working with clang-17 and 4.19 kernel
1 parent ed07a3c commit f5fd482

18 files changed

+201
-470
lines changed

.github/workflows/unit-test-on-pull-request.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ jobs:
120120
# https://github.com/cilium/ci-kernels/pkgs/container/ci-kernels/versions?filters%5Bversion_type%5D=tagged
121121

122122
# AMD64
123+
- { target_arch: amd64, kernel: 4.19.314 }
123124
- { target_arch: amd64, kernel: 5.4.276 }
124125
- { target_arch: amd64, kernel: 5.10.217 }
125126
- { target_arch: amd64, kernel: 5.15.159 }

support/ebpf/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
SHELL ?= bash
2-
BPF_CLANG ?= clang-16
3-
BPF_LINK ?= llvm-link-16
4-
LLC ?= llc-16
2+
BPF_CLANG ?= clang-17
3+
BPF_LINK ?= llvm-link-17
4+
LLC ?= llc-17
55

66
DEBUG_FLAGS = -DOPTI_DEBUG -g
77

support/ebpf/bpfdefs.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,4 @@ static long (*bpf_perf_prog_read_value)(struct pt_regs *ctx, struct bpf_perf_eve
157157

158158
#endif // !TESTING_COREDUMP
159159

160-
// HACK: On failure, bpf_perf_prog_read_value() zeroes the buffer. We ensure that this always
161-
// fail with a compile time assert that ensures that the struct size is different to the size
162-
// of the expected structure.
163-
#define bpf_large_memzero(_d, _l) \
164-
({ \
165-
_Static_assert(_l != sizeof(struct bpf_perf_event_value), "stack size must be different to the valid argument"); \
166-
bpf_perf_prog_read_value(ctx, _d, _l); \
167-
})
168-
169-
170160
#endif // OPTI_BPFDEFS_H

support/ebpf/extmaps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern bpf_map_def inhibit_events;
1717
extern bpf_map_def interpreter_offsets;
1818
extern bpf_map_def system_config;
1919
extern bpf_map_def trace_events;
20+
extern bpf_map_def go_procs;
2021

2122
#if defined(TESTING_COREDUMP)
2223

support/ebpf/hash.h

Lines changed: 23 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,33 @@
33

44
#include "types.h"
55

6-
#define ROUNDUP_8(x) ((x + 7) & ~7)
7-
static inline __attribute__((__always_inline__))
8-
bool hash_custom_labels(CustomLabelsArray *lbls, int seed, u64 *out) {
9-
// apply murmurhash2 as though this is an array of
10-
// the number of labels (8 bytes), followed by all the key/val lengths,
11-
// followed by all the keys/vals.
12-
const u64 m = 0xc6a4a7935bd1e995LLU;
13-
const int r = 47;
14-
15-
int len = 8;
16-
for (int i = 0; i < MAX_CUSTOM_LABELS; ++i) {
17-
if (i >= lbls->len)
18-
break;
19-
len += 8;
20-
len += ROUNDUP_8(lbls->labels[i].key_len);
21-
len += ROUNDUP_8(lbls->labels[i].val_len);
22-
}
23-
24-
u64 h = seed ^ (len * m);
25-
26-
// hash the number of labels
27-
{
28-
u64 k = lbls->len;
29-
k *= m;
30-
k ^= k >> r;
31-
k *= m;
32-
33-
h ^= k;
34-
h *= m;
35-
}
36-
37-
// hash each k/v len
38-
for (int i = 0; i < MAX_CUSTOM_LABELS; ++i) {
39-
// force clang not to unroll the loop by hiding the value of i.
40-
// Unrolling this loop confuses the verifier.
41-
asm volatile("" : "=r"(i) : "0"(i));
42-
if (i >= lbls->len)
43-
break;
44-
u64 k = (((u64)lbls->labels[i].key_len) << 32) | ((u64)lbls->labels[i].val_len);
45-
k *= m;
46-
k ^= k >> r;
47-
k *= m;
6+
#define M 0xc6a4a7935bd1e995LLU
487

49-
h ^= k;
50-
h *= m;
8+
static inline __attribute__((__always_inline__))
9+
u64 clear_or_hash_custom_labels(CustomLabelsArray *lbls, bool clear) {
10+
u64 h = lbls->len * M;
11+
u64 *bits = (u64 *)lbls;
12+
#pragma unroll
13+
for (int i=0; i < sizeof(CustomLabelsArray)/8; i++) {
14+
if (clear) {
15+
bits[i] = 0;
16+
} else {
17+
h ^= bits[i];
18+
h *= M;
19+
}
5120
}
5221

53-
// hash each k/v
54-
for (int i = 0; i < MAX_CUSTOM_LABELS; ++i) {
55-
if (i >= lbls->len)
56-
break;
57-
CustomLabel *lbl = &lbls->labels[i];
58-
u64 kl = ROUNDUP_8(lbl->key_len);
59-
for (int j = 0; j < CUSTOM_LABEL_MAX_VAL_LEN / 8; ++j) {
60-
if (j >= kl)
61-
return false;
62-
u64 k = lbl->key.key_u64[j];
63-
k *= m;
64-
k ^= k >> r;
65-
k *= m;
66-
67-
h ^= k;
68-
h *= m;
69-
}
70-
u64 vl = ROUNDUP_8(lbl->val_len);
71-
for (int j = 0; j < CUSTOM_LABEL_MAX_VAL_LEN / 8; ++j) {
72-
if (j >= vl)
73-
return false;
74-
u64 k = lbl->val.val_u64[j];
75-
k *= m;
76-
k ^= k >> r;
77-
k *= m;
78-
79-
h ^= k;
80-
h *= m;
81-
}
82-
}
22+
return h;
23+
}
8324

84-
h ^= h >> r;
85-
h *= m;
86-
h ^= h >> r;
25+
static inline __attribute__((__always_inline__))
26+
void clear_custom_labels(CustomLabelsArray *lbls) {
27+
clear_or_hash_custom_labels(lbls, true);
28+
}
8729

88-
*out = h;
89-
return true;
30+
static inline __attribute__((__always_inline__))
31+
u64 hash_custom_labels(CustomLabelsArray *lbls) {
32+
return clear_or_hash_custom_labels(lbls, false);
9033
}
9134

92-
#endif
35+
#endif // OPTI_HASH_H

0 commit comments

Comments
 (0)