Skip to content

Commit 4fc9887

Browse files
committed
Remove custom labels hash and write directly to Trace
1 parent 6fb7635 commit 4fc9887

File tree

10 files changed

+53
-147
lines changed

10 files changed

+53
-147
lines changed

support/ebpf/go_labels.ebpf.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "bpfdefs.h"
44
#include "util.h"
5-
#include "hash.h"
65
#include "kernel.h"
76
#include "tracemgmt.h"
87
#include "tsd.h"
@@ -16,25 +15,25 @@ void process_value(GoMapBucket *map_value, CustomLabelsArray *out, unsigned i) {
1615
return;
1716
CustomLabel *lbl = &out->labels[out->len];
1817
if (map_value->keys[i].str != NULL) {
19-
long res = bpf_probe_read_user(lbl->key.key_bytes, CUSTOM_LABEL_MAX_KEY_LEN, map_value->keys[i].str);
18+
unsigned klen = MIN(map_value->keys[i].len, CUSTOM_LABEL_MAX_KEY_LEN-1);
19+
long res = bpf_probe_read_user(lbl->key, klen, map_value->keys[i].str);
2020
if (res) {
2121
DEBUG_PRINT("cl: failed to read key for custom label (%lx): %ld", (unsigned long) map_value->keys[i].str, res);
2222
return;
2323
}
24-
res = bpf_probe_read_user(lbl->val.val_bytes, CUSTOM_LABEL_MAX_VAL_LEN, map_value->values[i].str);
24+
unsigned vlen = MIN(map_value->values[i].len, CUSTOM_LABEL_MAX_VAL_LEN-1);
25+
res = bpf_probe_read_user(lbl->val, vlen, map_value->values[i].str);
2526
if (res) {
2627
DEBUG_PRINT("cl: failed to read value for custom label: %ld", res);
2728
return;
2829
}
29-
lbl->key_len = map_value->keys[i].len;
30-
lbl->val_len = map_value->values[i].len;
3130
}
3231
out->len++;
3332
}
3433

3534
static inline __attribute__((__always_inline__))
3635
bool process_bucket(PerCPURecord *record, void *label_buckets, int j) {
37-
CustomLabelsArray *out = &record->customLabelsState.cla;
36+
CustomLabelsArray *out = &record->trace.custom_labels;
3837
GoMapBucket *map_value = &record->goMapBucket;
3938
long res = bpf_probe_read(map_value, sizeof(GoMapBucket), label_buckets + (j * sizeof(GoMapBucket)));
4039
if (res < 0) {

support/ebpf/hash.h

Lines changed: 0 additions & 35 deletions
This file was deleted.

support/ebpf/interpreter_dispatcher.ebpf.c

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include "bpfdefs.h"
66
#include "util.h"
7-
#include "hash.h"
87
#include "kernel.h"
98
#include "tracemgmt.h"
109
#include "tsd.h"
@@ -142,32 +141,6 @@ bpf_map_def SEC("maps") cl_procs = {
142141
.max_entries = 128,
143142
};
144143

145-
#define MAX_BUCKETS 8
146-
#define MAX_CUSTOM_LABELS_ENTRIES 1000
147-
148-
bpf_map_def SEC("maps") custom_labels = {
149-
.type = BPF_MAP_TYPE_HASH,
150-
.key_size = sizeof(u64),
151-
.value_size = sizeof(CustomLabelsArray),
152-
.max_entries = MAX_CUSTOM_LABELS_ENTRIES,
153-
};
154-
155-
static inline __attribute__((__always_inline__))
156-
bool add_custom_labels(PerCPURecord *record) {
157-
CustomLabelsArray *lbls = &record->customLabelsState.cla;
158-
DEBUG_PRINT("cl: got %d custom labels", lbls->len);
159-
u64 hash = hash_custom_labels(lbls);
160-
int err = bpf_map_update_elem(&custom_labels, &hash, lbls, BPF_ANY);
161-
if (err) {
162-
DEBUG_PRINT("cl: failed to update custom labels with error %d\n", err);
163-
return false;
164-
} else {
165-
record->trace.custom_labels_hash = hash;
166-
DEBUG_PRINT("cl: successfully computed hash 0x%llx for %d custom labels", hash, lbls->len);
167-
return true;
168-
}
169-
}
170-
171144
static inline __attribute__((__always_inline__))
172145
void *get_m_ptr(struct GoCustomLabelsOffsets *offs, UnwindState *state) {
173146
long res;
@@ -224,15 +197,8 @@ void maybe_add_go_custom_labels(struct pt_regs *ctx, PerCPURecord *record) {
224197

225198
DEBUG_PRINT("cl: trace is within a process with Go custom labels enabled");
226199
increment_metric(metricID_UnwindGoCustomLabelsAttempts);
227-
clear_custom_labels(&record->customLabelsState.cla);
228200
record->state.processed_go_labels = true;
229201
tail_call(ctx, PROG_GO_LABELS);
230-
} else {
231-
if (record->customLabelsState.cla.len > 0) {
232-
if (!add_custom_labels(record)) {
233-
increment_metric(metricID_UnwindGoCustomLabelsFailures);
234-
}
235-
}
236202
}
237203
}
238204

@@ -260,7 +226,7 @@ bool get_native_custom_labels(PerCPURecord *record, NativeCustomLabelsProcInfo *
260226

261227
unsigned ct = 0;
262228
NativeCustomLabel *lbl_ptr;
263-
CustomLabelsArray *out = &record->customLabelsState.cla;
229+
CustomLabelsArray *out = &record->trace.custom_labels;
264230

265231
#pragma unroll
266232
for (int i = 0; i < MAX_CUSTOM_LABELS; i++) {
@@ -276,16 +242,14 @@ bool get_native_custom_labels(PerCPURecord *record, NativeCustomLabelsProcInfo *
276242
if (!lbl->key.buf)
277243
continue;
278244
CustomLabel *out_lbl = &out->labels[ct];
279-
unsigned len = MIN(lbl->key.len, CUSTOM_LABEL_MAX_KEY_LEN);
280-
out_lbl->key_len = len;
281-
if ((err = bpf_probe_read_user(out_lbl->key.key_bytes, len, (void *)lbl->key.buf))) {
245+
unsigned klen = MIN(lbl->key.len, CUSTOM_LABEL_MAX_KEY_LEN-1);
246+
if ((err = bpf_probe_read_user(out_lbl->key, klen, (void *)lbl->key.buf))) {
282247
increment_metric(metricID_UnwindNativeCustomLabelsErrReadKey);
283248
DEBUG_PRINT("cl: failed to read label key: %d", err);
284249
goto exit;
285250
}
286-
len = MIN(lbl->value.len, CUSTOM_LABEL_MAX_VAL_LEN);
287-
out_lbl->val_len = len;
288-
if ((err = bpf_probe_read_user(out_lbl->val.val_bytes, len, (void *)lbl->value.buf))) {
251+
unsigned vlen = MIN(lbl->value.len, CUSTOM_LABEL_MAX_VAL_LEN-1);
252+
if ((err = bpf_probe_read_user(out_lbl->val, vlen, (void *)lbl->value.buf))) {
289253
increment_metric(metricID_UnwindNativeCustomLabelsErrReadValue);
290254
DEBUG_PRINT("cl: failed to read label value: %d", err);
291255
goto exit;
@@ -306,12 +270,8 @@ void maybe_add_native_custom_labels(PerCPURecord *record) {
306270
DEBUG_PRINT("cl: %d does not support native custom labels", pid);
307271
return;
308272
}
309-
clear_custom_labels(&record->customLabelsState.cla);
310273
DEBUG_PRINT("cl: trace is within a process with native custom labels enabled");
311274
bool success = get_native_custom_labels(record, proc);
312-
if (success) {
313-
success = add_custom_labels(record);
314-
}
315275
if (success)
316276
increment_metric(metricID_UnwindNativeCustomLabelsAddSuccesses);
317277
else

support/ebpf/tracemgmt.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,9 @@ static inline PerCPURecord *get_pristine_per_cpu_record()
223223
record->tailCalls = 0;
224224
record->ratelimitAction = RATELIMIT_ACTION_DEFAULT;
225225
record->customLabelsState.go_m_ptr = NULL;
226-
// customLabelsState.cla is reset right before use.
227226

228227
Trace *trace = &record->trace;
229228
trace->kernel_stack_id = -1;
230-
trace->custom_labels_hash = 0;
231229
trace->stack_len = 0;
232230
trace->pid = 0;
233231
trace->tid = 0;
@@ -236,6 +234,12 @@ static inline PerCPURecord *get_pristine_per_cpu_record()
236234
trace->apm_trace_id.as_int.lo = 0;
237235
trace->apm_transaction_id.as_int = 0;
238236

237+
u64 *labels_space = (u64*)&trace->custom_labels;
238+
#pragma unroll
239+
for (int i=0; i < sizeof(CustomLabelsArray)/8; i++) {
240+
labels_space[i] = 0;
241+
}
242+
239243
return record;
240244
}
241245

-159 KB
Binary file not shown.
-159 KB
Binary file not shown.
-24.6 KB
Binary file not shown.
-24.6 KB
Binary file not shown.

support/ebpf/types.h

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -565,20 +565,21 @@ typedef struct __attribute__((packed)) ApmCorrelationBuf {
565565
ApmSpanID transaction_id;
566566
} ApmCorrelationBuf;
567567

568-
typedef struct NativeCustomLabelsString {
569-
size_t len;
570-
const unsigned char *buf;
571-
} NativeCustomLabelsString;
568+
#define CUSTOM_LABEL_MAX_KEY_LEN COMM_LEN
569+
// Big enough to hold UUIDs, etc.
570+
#define CUSTOM_LABEL_MAX_VAL_LEN 48
572571

573-
typedef struct NativeCustomLabel {
574-
NativeCustomLabelsString key;
575-
NativeCustomLabelsString value;
576-
} NativeCustomLabel;
572+
typedef struct CustomLabel {
573+
char key[CUSTOM_LABEL_MAX_KEY_LEN];
574+
char val[CUSTOM_LABEL_MAX_VAL_LEN];
575+
} CustomLabel;
577576

578-
typedef struct NativeCustomLabelsThreadLocalData {
579-
NativeCustomLabel *storage;
580-
size_t count;
581-
} NativeCustomLabelsThreadLocalData;
577+
#define MAX_CUSTOM_LABELS 10
578+
579+
typedef struct CustomLabelsArray {
580+
unsigned len;
581+
CustomLabel labels[MAX_CUSTOM_LABELS];
582+
} CustomLabelsArray;
582583

583584
// Container for a stack trace
584585
typedef struct Trace {
@@ -596,8 +597,8 @@ typedef struct Trace {
596597
ApmSpanID apm_transaction_id;
597598
// APM trace ID or all-zero if not present.
598599
ApmTraceID apm_trace_id;
599-
// custom labels hash or zero if not present
600-
u64 custom_labels_hash;
600+
// Custom Labels
601+
CustomLabelsArray custom_labels;
601602
// The kernel stack ID.
602603
s32 kernel_stack_id;
603604
// The number of frames in the stack.
@@ -791,7 +792,7 @@ typedef struct PythonUnwindScratchSpace {
791792

792793
struct GoString {
793794
char *str;
794-
s64 len;
795+
u64 len;
795796
};
796797

797798
struct GoSlice {
@@ -807,38 +808,25 @@ typedef struct GoMapBucket {
807808
void *overflow;
808809
} GoMapBucket;
809810

810-
811-
// These must be divisible by 8 and a power of 2
812-
#define CUSTOM_LABEL_MAX_KEY_LEN 64
813-
#define CUSTOM_LABEL_MAX_VAL_LEN 64
814-
815-
typedef struct CustomLabel {
816-
unsigned key_len;
817-
unsigned val_len;
818-
// If we use unaligned `unsigned char` instead of `u64`
819-
// buffers, the hash function becomes too complex to verify.
820-
union {
821-
u64 key_u64[CUSTOM_LABEL_MAX_KEY_LEN / 8];
822-
unsigned char key_bytes[CUSTOM_LABEL_MAX_KEY_LEN];
823-
} key;
824-
union {
825-
u64 val_u64[CUSTOM_LABEL_MAX_VAL_LEN / 8];
826-
unsigned char val_bytes[CUSTOM_LABEL_MAX_VAL_LEN];
827-
} val;
828-
} CustomLabel;
829-
830-
#define MAX_CUSTOM_LABELS 14
831-
832-
typedef struct CustomLabelsArray {
833-
unsigned len;
834-
struct CustomLabel labels[MAX_CUSTOM_LABELS];
835-
} CustomLabelsArray;
836-
837811
typedef struct CustomLabelsState {
838812
void *go_m_ptr;
839-
CustomLabelsArray cla;
840813
} CustomLabelsState;
841814

815+
typedef struct NativeCustomLabelsString {
816+
size_t len;
817+
const unsigned char *buf;
818+
} NativeCustomLabelsString;
819+
820+
typedef struct NativeCustomLabel {
821+
NativeCustomLabelsString key;
822+
NativeCustomLabelsString value;
823+
} NativeCustomLabel;
824+
825+
typedef struct NativeCustomLabelsThreadLocalData {
826+
NativeCustomLabel *storage;
827+
size_t count;
828+
} NativeCustomLabelsThreadLocalData;
829+
842830
// Per-CPU info for the stack being built. This contains the stack as well as
843831
// meta-data on the number of eBPF tail-calls used so far to construct it.
844832
typedef struct PerCPURecord {

tracer/tracer.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -901,22 +901,12 @@ func (t *Tracer) loadBpfTrace(raw []byte, cpu int) *host.Trace {
901901
}
902902
}
903903

904-
if ptr.custom_labels_hash != 0 {
905-
var lbls C.CustomLabelsArray
906-
907-
if err := t.ebpfMaps["custom_labels"].Lookup(
908-
unsafe.Pointer(&ptr.custom_labels_hash), unsafe.Pointer(&lbls),
909-
); err != nil {
910-
log.Warnf("Failed to read custom labels: %v", err)
911-
}
912-
913-
trace.CustomLabels = make(map[string]string, int(lbls.len))
914-
for i := 0; i < int(lbls.len); i++ {
915-
lbl := lbls.labels[i]
916-
klen := min(int(lbl.key_len), 64)
917-
key := string(lbl.key[0:klen])
918-
vlen := min(int(lbl.val_len), 64)
919-
val := string(lbl.val[0:vlen])
904+
if ptr.custom_labels.len > 0 {
905+
trace.CustomLabels = make(map[string]string, int(ptr.custom_labels.len))
906+
for i := 0; i < int(ptr.custom_labels.len); i++ {
907+
lbl := ptr.custom_labels.labels[i]
908+
key := C.GoString((*C.char)(unsafe.Pointer(&lbl.key)))
909+
val := C.GoString((*C.char)(unsafe.Pointer(&lbl.val)))
920910
trace.CustomLabels[key] = val
921911
}
922912
}

0 commit comments

Comments
 (0)