Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/memtrack/src/ebpf/c/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
submit_block; \
}

/* free() carries no stack: the flamegraph only attributes allocations, so a
* copy here would double the capture cost for nothing. */
#define UPROBE_RET(name, arg_expr, submit_block) \
SEC(UPROBE_SEC) \
int uprobe_##name(struct pt_regs* ctx) { \
__u64 arg0 = arg_expr; \
if (arg0 == 0) { \
return 0; \
} \
__u64 stack_hash = capture_stack(ctx); \
submit_block; \
}

Expand Down Expand Up @@ -86,7 +87,7 @@
UPROBE_ARG_RET(malloc, PT_REGS_PARM1(ctx),
{ return submit_alloc_event(arg0, ret_val, stack_hash); })

UPROBE_RET(free, PT_REGS_PARM1(ctx), { return submit_free_event(arg0, stack_hash); })
UPROBE_RET(free, PT_REGS_PARM1(ctx), { return submit_free_event(arg0); })

UPROBE_ARG_RET(calloc, PT_REGS_PARM1(ctx) * PT_REGS_PARM2(ctx),
{ return submit_calloc_event(arg0, ret_val, stack_hash); })
Expand Down
3 changes: 1 addition & 2 deletions crates/memtrack/src/ebpf/c/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ struct event {

/* Deallocation event (free) */
struct {
uint64_t addr; /* address to free */
uint64_t stack_hash; /* caller stack identity; 0 = not captured */
uint64_t addr; /* address to free */
} free;

/* Reallocation event - includes both old and new addresses */
Expand Down
7 changes: 2 additions & 5 deletions crates/memtrack/src/ebpf/c/utils/event_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,8 @@ static __always_inline int submit_calloc_event(__u64 size, __u64 addr, __u64 sta
});
}

static __always_inline int submit_free_event(__u64 addr, __u64 stack_hash) {
SUBMIT_GATED_EVENT(EVENT_TYPE_FREE, {
e->data.free.addr = addr;
e->data.free.stack_hash = stack_hash;
});
static __always_inline int submit_free_event(__u64 addr) {
SUBMIT_GATED_EVENT(EVENT_TYPE_FREE, { e->data.free.addr = addr; });
}

static __always_inline int submit_realloc_event(__u64 old_addr, __u64 new_addr, __u64 size,
Expand Down
7 changes: 1 addition & 6 deletions crates/memtrack/src/ebpf/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ pub fn parse_event(data: &[u8]) -> Option<MemtrackEvent> {
stack_hash: event.data.alloc.stack_hash,
},
),
EVENT_TYPE_FREE => (
event.data.free.addr,
MemtrackEventKind::Free {
stack_hash: event.data.free.stack_hash,
},
),
EVENT_TYPE_FREE => (event.data.free.addr, MemtrackEventKind::Free),
EVENT_TYPE_CALLOC => (
event.data.alloc.addr,
MemtrackEventKind::Calloc {
Expand Down
2 changes: 1 addition & 1 deletion crates/memtrack/tests/c_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn test_track_allocators_disabled_skips_allocations() -> Result<(), Box<dyn std:
matches!(
e.kind,
MemtrackEventKind::Malloc { .. }
| MemtrackEventKind::Free { .. }
| MemtrackEventKind::Free
| MemtrackEventKind::Calloc { .. }
| MemtrackEventKind::Realloc { .. }
| MemtrackEventKind::AlignedAlloc { .. }
Expand Down
3 changes: 1 addition & 2 deletions crates/memtrack/tests/dlopen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ fn test_dlopen_allocator() -> Result<(), Box<dyn std::error::Error>> {
let free_count = events
.iter()
.filter(|e| {
matches!(e.kind, MemtrackEventKind::Free { .. })
&& malloc_addrs.contains(&e.addr)
matches!(e.kind, MemtrackEventKind::Free) && malloc_addrs.contains(&e.addr)
})
.count();

Expand Down
6 changes: 3 additions & 3 deletions crates/memtrack/tests/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ macro_rules! assert_events_snapshot {
matches!(
e.kind,
MemtrackEventKind::Malloc { .. }
| MemtrackEventKind::Free { .. }
| MemtrackEventKind::Free
| MemtrackEventKind::Calloc { .. }
| MemtrackEventKind::Realloc { .. }
| MemtrackEventKind::AlignedAlloc { .. }
Expand Down Expand Up @@ -97,7 +97,7 @@ macro_rules! assert_events_with_marker_for_each_variant {
/// differ between runs of the same workload.
pub fn describe_kind(kind: &MemtrackEventKind) -> String {
match kind {
MemtrackEventKind::Free { .. } => "Free".to_string(),
MemtrackEventKind::Free => "Free".to_string(),
MemtrackEventKind::Malloc { size, .. } => format!("Malloc {{ size: {size} }}"),
MemtrackEventKind::Calloc { size, .. } => format!("Calloc {{ size: {size} }}"),
MemtrackEventKind::AlignedAlloc { size, .. } => format!("AlignedAlloc {{ size: {size} }}"),
Expand Down Expand Up @@ -280,7 +280,7 @@ fn event_profile(events: &[Event]) -> EventProfile {
// Only allocator events are comparable across variants: RSS and
// lifecycle values (sizes, pids) are per-run.
match event.kind {
MemtrackEventKind::Free { .. } => {
MemtrackEventKind::Free => {
if !live.remove(&event.addr) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: format_events(&events)
"Malloc { size: 1024, has_stack: true }",
"Malloc { size: 2048, has_stack: true }",
"Malloc { size: 4096, has_stack: true }",
"Free { has_stack: true }",
"Free { has_stack: true }",
"Free { has_stack: true }",
"Free",
"Free",
"Free",
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: format_events(&events)
"Malloc { size: 1024, has_stack: true }",
"Malloc { size: 2048, has_stack: true }",
"Malloc { size: 4096, has_stack: true }",
"Free { has_stack: true }",
"Free { has_stack: true }",
"Free { has_stack: true }",
"Free",
"Free",
"Free",
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: format_events(&events)
"Malloc { size: 1024, has_stack: false }",
"Malloc { size: 2048, has_stack: false }",
"Malloc { size: 4096, has_stack: false }",
"Free { has_stack: false }",
"Free { has_stack: false }",
"Free { has_stack: false }",
"Free",
"Free",
"Free",
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ expression: format_events(&events)
"Malloc { size: 1024, has_stack: false }",
"Malloc { size: 2048, has_stack: false }",
"Malloc { size: 4096, has_stack: false }",
"Free { has_stack: false }",
"Free { has_stack: false }",
"Free { has_stack: false }",
"Free",
"Free",
"Free",
]
Loading
Loading