-
Notifications
You must be signed in to change notification settings - Fork 31
feat(memtrack): pause producers under ring pressure #543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c6f76e4
c0b2ba2
c0c4c19
a87e1f1
ebfbdef
38ea912
c023567
e594058
db9cfa5
4953b6c
3d58b73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #ifndef __PRESSURE_BPF_H__ | ||
| #define __PRESSURE_BPF_H__ | ||
|
|
||
| #include <bpf/bpf_helpers.h> | ||
|
|
||
| #include "map_helpers.h" | ||
| #include "process_tracking.h" | ||
| #include "stopped.h" | ||
|
|
||
| /* Ring pressure stop. Call only after submit/discard or a failed reserve: | ||
| * stopping with a live reservation would wedge the ring. A tracked producer | ||
| * that writes while the ring is over the watermark is stopped and recorded, | ||
| * so processes that do not write keep running. Userspace resumes the | ||
| * recorded producers once it has flushed the ring. */ | ||
|
|
||
| #define MEMTRACK_PRESSURE_HEADROOM_FRAC 4 /* stop at (FRAC-1)/FRAC = 75% used */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I'm missing context, but this |
||
|
|
||
| static __always_inline int memtrack_ring_over_watermark(void* ring) { | ||
| __u64 size = bpf_ringbuf_query(ring, BPF_RB_RING_SIZE); | ||
| __u64 avail = bpf_ringbuf_query(ring, BPF_RB_AVAIL_DATA); | ||
| return avail >= size - size / MEMTRACK_PRESSURE_HEADROOM_FRAC; | ||
| } | ||
|
|
||
| static __always_inline void memtrack_check_ring_pressure(void* ring, __u32 current_tgid) { | ||
| if (!memtrack_ring_over_watermark(ring)) { | ||
| return; | ||
| } | ||
|
|
||
| /* Never stop an untracked process that happens to trigger a probe. */ | ||
| if (!is_tracked(current_tgid)) { | ||
| return; | ||
| } | ||
|
|
||
| memtrack_stop_current(&pressure_stopped, current_tgid); | ||
| } | ||
|
|
||
| #endif /* __PRESSURE_BPF_H__ */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #ifndef __STOPPED_H__ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this filename could be a bit better than |
||
| #define __STOPPED_H__ | ||
|
|
||
| #include <bpf/bpf_helpers.h> | ||
|
|
||
| #include "map_helpers.h" | ||
|
|
||
| #define MEMTRACK_SIGCONT 18 | ||
| #define MEMTRACK_SIGSTOP 19 | ||
|
|
||
| /* tgid -> 1 for every process BPF stopped, one map per reason. A process is | ||
| * recorded before its stop can take effect, and userspace resumes only | ||
| * recorded processes, so a process stopped for both reasons resumes once | ||
| * neither map holds it. Sized like tracked_pids. */ | ||
| BPF_HASH_MAP(pressure_stopped, __u32, __u8, 10000); | ||
| BPF_HASH_MAP(attach_stopped, __u32, __u8, 10000); | ||
|
|
||
| /* Stop the current process and record it in `map`. SIGSTOP is queued before | ||
| * the record is written: it only takes effect on return to user mode, so any | ||
| * SIGCONT sent after userspace sees the record cancels or ends the stop. | ||
| * Requires task context with IRQs enabled, where the signal is queued | ||
| * synchronously rather than via irq_work. */ | ||
| static __always_inline void memtrack_stop_current(void* map, __u32 tgid) { | ||
| if (bpf_send_signal(MEMTRACK_SIGSTOP) != 0) { | ||
| return; | ||
| } | ||
|
|
||
| __u8 marker = 1; | ||
| if (bpf_map_update_elem(map, &tgid, &marker, BPF_ANY) != 0) { | ||
| /* Unrecorded, so nothing would resume it. */ | ||
| bpf_send_signal(MEMTRACK_SIGCONT); | ||
|
Comment on lines
+29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If this stop-record map is full while the process is also held for attach work or ring pressure, the failed update sends Knowledge Base Used: eBPF memory tracker Prompt To Fix With AIThis is a comment left during a code review.
Path: crates/memtrack/src/ebpf/c/utils/stopped.h
Line: 29-31
Comment:
**Failed Update Releases Another Stop**
If this stop-record map is full while the process is also held for attach work or ring pressure, the failed update sends `SIGCONT` without checking that other hold. The process can then run while it is supposed to remain stopped, allowing events during an attach or ring drain.
**Knowledge Base Used:** [eBPF memory tracker](https://app.greptile.com/codspeed/-/custom-context/knowledge-base/codspeedhq/codspeed/-/docs/ebpf-memory-tracker.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @not-matthias is this somethign we want to handle? At least have a big warning or so? 10000 pids is quite large but not irrealistic either |
||
| } | ||
| } | ||
|
|
||
| #endif /* __STOPPED_H__ */ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should make this closer to the seen_stack hashmap, and focus the comment on the fact that we are using the hashmap as a hashset, then have a small sentence to explain what's explained above, else it's a bit hard to follow