Skip to content

Commit

Permalink
Delay map cleanup in bpf (#682)
Browse files Browse the repository at this point in the history
* delay map cleanup in bpf

* add changelog entry
  • Loading branch information
grcevski committed Feb 21, 2024
1 parent cf4ecc3 commit 795ffba
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
- Clean up warn in otelglobal `SetStatus()` when grabbing the status code. ([#675](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/675))
- Reset `proc` offset after a failed iteration. ([#681](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/681))
- Avoid using runtime.NumCPU to get the number of CPUs on the system before remote mmap ([#680](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/680))
- Cleanup eBPF maps only when we stop using the memory ([#682](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/682))

## [v0.10.1-alpha] - 2024-01-10

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,11 @@ int uprobe_Start_Returns(struct pt_regs *ctx) {
if (span_name == NULL) {
return 0;
}
bpf_map_delete_elem(&span_name_by_context, &key);

u32 zero_span_key = 0;
struct otel_span_t *zero_span = bpf_map_lookup_elem(&otel_span_storage_map, &zero_span_key);
if (zero_span == NULL) {
return 0;
goto done;
}

u32 otel_span_key = 1;
Expand All @@ -134,7 +133,7 @@ int uprobe_Start_Returns(struct pt_regs *ctx) {
// Get a pointer to the zeroed span
struct otel_span_t *otel_span = bpf_map_lookup_elem(&otel_span_storage_map, &otel_span_key);
if (otel_span == NULL) {
return 0;
goto done;
}

otel_span->start_time = bpf_ktime_get_ns();
Expand All @@ -156,6 +155,9 @@ int uprobe_Start_Returns(struct pt_regs *ctx) {

bpf_map_update_elem(&active_spans_by_span_ptr, &span_ptr_val, otel_span, 0);
start_tracking_span(ret_context_ptr_val, &otel_span->sc);

done:
bpf_map_delete_elem(&span_name_by_context, &key);
return 0;
}

Expand Down Expand Up @@ -253,9 +255,10 @@ int uprobe_End(struct pt_regs *ctx) {
return 0;
}
span->end_time = bpf_ktime_get_ns();
bpf_map_delete_elem(&active_spans_by_span_ptr, &non_recording_span_ptr);
stop_tracking_span(&span->sc, &span->psc);

bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, span, sizeof(*span));

bpf_map_delete_elem(&active_spans_by_span_ptr, &non_recording_span_ptr);
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ int uprobe_LoopyWriter_HeaderHandler(struct pt_regs *ctx)
return 0;
}

bpf_map_delete_elem(&streamid_to_span_contexts, &stream_id);
struct go_slice slice = {};
struct go_slice_user_ptr slice_user_ptr = {};
slice_user_ptr.array = (void *)(headerFrame_ptr + (headerFrame_hf_pos));
Expand All @@ -157,7 +156,7 @@ int uprobe_LoopyWriter_HeaderHandler(struct pt_regs *ctx)
struct go_string key_str = write_user_go_string(tp_key, sizeof(tp_key));
if (key_str.len == 0) {
bpf_printk("key write failed, aborting ebpf probe");
return 0;
goto done;
}

// Write headers
Expand All @@ -166,12 +165,15 @@ int uprobe_LoopyWriter_HeaderHandler(struct pt_regs *ctx)
struct go_string val_str = write_user_go_string(val, sizeof(val));
if (val_str.len == 0) {
bpf_printk("val write failed, aborting ebpf probe");
return 0;
goto done;
}
struct hpack_header_field hf = {};
hf.name = key_str;
hf.value = val_str;
append_item_to_slice(&slice, &hf, sizeof(hf), &slice_user_ptr);
done:
bpf_map_delete_elem(&streamid_to_span_contexts, &stream_id);

return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ int uprobe_Transport_roundTrip_Returns(struct pt_regs *ctx) {
bpf_printk("probe_Transport_roundTrip_Returns: entry_state is NULL");
return 0;
}
bpf_map_delete_elem(&http_events, &key);

if (is_register_abi()) {
// Getting the returned response
Expand All @@ -249,5 +248,7 @@ int uprobe_Transport_roundTrip_Returns(struct pt_regs *ctx) {

bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, http_req_span, sizeof(*http_req_span));
stop_tracking_span(&http_req_span->sc, &http_req_span->psc);

bpf_map_delete_elem(&http_events, &key);
return 0;
}

0 comments on commit 795ffba

Please sign in to comment.