Skip to content

Commit 9fde4ab

Browse files
committed
Allow post-processing of the callstack.
1 parent c7cef77 commit 9fde4ab

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

memapi/src/memorytracking.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ impl Allocation {
352352
}
353353
}
354354

355+
fn same_callstack(callstack: &Callstack) -> Cow<Callstack> {
356+
Cow::Borrowed(callstack)
357+
}
358+
355359
/// The main data structure tracking everything.
356360
pub struct AllocationTracker<FL: FunctionLocations> {
357361
// malloc()/calloc():
@@ -624,18 +628,22 @@ impl<FL: FunctionLocations> AllocationTracker<FL> {
624628
self.dump_to_flamegraph(path, true, "peak-memory", "Peak Tracked Memory Usage", true);
625629
}
626630

627-
pub fn to_lines(
628-
&self,
631+
pub fn to_lines<'a, F>(
632+
&'a self,
629633
peak: bool,
630634
to_be_post_processed: bool,
631-
) -> impl ExactSizeIterator<Item = String> + '_ {
635+
update_callstack: F,
636+
) -> impl ExactSizeIterator<Item = String> + '_
637+
where
638+
F: Fn(&Callstack) -> Cow<Callstack> + 'a,
639+
{
632640
let by_call = self.combine_callstacks(peak).into_iter();
633641
let id_to_callstack = self.interner.get_reverse_map();
634642
let mut linecache = LineCacher::default();
635643
by_call.map(move |(callstack_id, size)| {
636644
format!(
637645
"{} {}",
638-
id_to_callstack.get(&callstack_id).unwrap().as_string(
646+
update_callstack(id_to_callstack.get(&callstack_id).unwrap()).as_string(
639647
to_be_post_processed,
640648
&self.functions,
641649
";",
@@ -692,7 +700,7 @@ impl<FL: FunctionLocations> AllocationTracker<FL> {
692700
subtitle,
693701
"bytes",
694702
to_be_post_processed,
695-
|tbpp| self.to_lines(peak, tbpp),
703+
|tbpp| self.to_lines(peak, tbpp, same_callstack),
696704
)
697705
}
698706

@@ -763,7 +771,7 @@ impl<FL: FunctionLocations> AllocationTracker<FL> {
763771

764772
#[cfg(test)]
765773
mod tests {
766-
use crate::memorytracking::{ProcessUid, PARENT_PROCESS};
774+
use crate::memorytracking::{same_callstack, ProcessUid, PARENT_PROCESS};
767775

768776
use super::LineNumberInfo::LineNumber;
769777
use super::{
@@ -1223,7 +1231,7 @@ mod tests {
12231231
"c:3 (cf) 234",
12241232
"a:7 (af);b:2 (bf) 6000",
12251233
];
1226-
let mut result2: Vec<String> = tracker.to_lines(true, false).collect();
1234+
let mut result2: Vec<String> = tracker.to_lines(true, false, same_callstack).collect();
12271235
result2.sort();
12281236
expected2.sort();
12291237
assert_eq!(expected2, result2);

0 commit comments

Comments
 (0)