Skip to content

Commit c2adc0a

Browse files
committed
Make the functions public again.
1 parent 0b96f71 commit c2adc0a

File tree

1 file changed

+74
-44
lines changed

1 file changed

+74
-44
lines changed

memapi/src/flamegraph.rs

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -118,35 +118,12 @@ where
118118
pub fn get_flamegraph_with_options(
119119
&'a self,
120120
to_be_post_processed: bool,
121-
mut options: flamegraph::Options,
121+
options: flamegraph::Options,
122122
// special cased because it needs to be psot-processed:
123123
subtitle: Option<&str>,
124124
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
125125
let lines = self.to_lines(to_be_post_processed);
126-
let mut output = vec![];
127-
let lines: Vec<String> = lines.into_iter().collect();
128-
match flamegraph::from_lines(&mut options, lines.iter().map(|s| s.as_ref()), &mut output) {
129-
Err(e) => Err(format!("{}", e).into()),
130-
Ok(_) => {
131-
if to_be_post_processed {
132-
// Replace with real subtitle.
133-
let data = String::from_utf8(output)?;
134-
let data = if let Some(subtitle) = subtitle {
135-
data.replace("__FIL-SUBTITLE-HERE__", subtitle)
136-
} else {
137-
data
138-
};
139-
// Restore normal semi-colons.
140-
let data = data.replace('\u{ff1b}', ";");
141-
// Restore (non-breaking) spaces.
142-
let data = data.replace('\u{12e4}', "\u{00a0}");
143-
// Get rid of empty-line markers:
144-
let data = data.replace('\u{2800}', "");
145-
output = data.as_bytes().to_vec();
146-
}
147-
Ok(output)
148-
}
149-
}
126+
get_flamegraph_with_options(lines, to_be_post_processed, options, subtitle)
150127
}
151128

152129
/// Write a flamegraph SVG to disk, given lines in summarized format.
@@ -158,25 +135,14 @@ where
158135
count_name: &str,
159136
to_be_post_processed: bool,
160137
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
161-
let title = format!("{}{}", title, if reversed { ", Reversed" } else { "" },);
162-
let mut options = flamegraph::Options::default();
163-
options.title = title;
164-
options.count_name = count_name.to_string();
165-
options.font_size = 16;
166-
options.font_type = "monospace".to_string();
167-
options.frame_height = 22;
168-
options.reverse_stack_order = reversed;
169-
options.color_diffusion = true;
170-
options.direction = flamegraph::Direction::Inverted;
171-
options.min_width = 0.2;
172-
// Maybe disable this some day; but for now it makes debugging much
173-
// easier:
174-
options.pretty_xml = true;
175-
if to_be_post_processed {
176-
// Can't put structured text into subtitle, so have to do a hack.
177-
options.subtitle = Some("__FIL-SUBTITLE-HERE__".to_string());
178-
}
179-
self.get_flamegraph_with_options(to_be_post_processed, options, Some(subtitle))
138+
get_flamegraph(
139+
self.to_lines(to_be_post_processed),
140+
reversed,
141+
title,
142+
subtitle,
143+
count_name,
144+
to_be_post_processed,
145+
)
180146
}
181147

182148
/// Write a flamegraph SVG to disk.
@@ -273,6 +239,70 @@ where
273239
}
274240
}
275241

242+
/// Low-level interface for writing flamegraphs with post-processing:
243+
pub fn get_flamegraph_with_options<I: IntoIterator<Item = String>>(
244+
lines: I,
245+
to_be_post_processed: bool,
246+
mut options: flamegraph::Options,
247+
// special cased because it needs to be psot-processed:
248+
subtitle: Option<&str>,
249+
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
250+
let mut output = vec![];
251+
let lines: Vec<String> = lines.into_iter().collect();
252+
match flamegraph::from_lines(&mut options, lines.iter().map(|s| s.as_ref()), &mut output) {
253+
Err(e) => Err(format!("{}", e).into()),
254+
Ok(_) => {
255+
if to_be_post_processed {
256+
// Replace with real subtitle.
257+
let data = String::from_utf8(output)?;
258+
let data = if let Some(subtitle) = subtitle {
259+
data.replace("__FIL-SUBTITLE-HERE__", subtitle)
260+
} else {
261+
data
262+
};
263+
// Restore normal semi-colons.
264+
let data = data.replace('\u{ff1b}', ";");
265+
// Restore (non-breaking) spaces.
266+
let data = data.replace('\u{12e4}', "\u{00a0}");
267+
// Get rid of empty-line markers:
268+
let data = data.replace('\u{2800}', "");
269+
output = data.as_bytes().to_vec();
270+
}
271+
Ok(output)
272+
}
273+
}
274+
}
275+
276+
/// Write a flamegraph SVG to disk, given lines in summarized format.
277+
pub fn get_flamegraph<I: IntoIterator<Item = String>>(
278+
lines: I,
279+
reversed: bool,
280+
title: &str,
281+
subtitle: &str,
282+
count_name: &str,
283+
to_be_post_processed: bool,
284+
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
285+
let title = format!("{}{}", title, if reversed { ", Reversed" } else { "" },);
286+
let mut options = flamegraph::Options::default();
287+
options.title = title;
288+
options.count_name = count_name.to_string();
289+
options.font_size = 16;
290+
options.font_type = "monospace".to_string();
291+
options.frame_height = 22;
292+
options.reverse_stack_order = reversed;
293+
options.color_diffusion = true;
294+
options.direction = flamegraph::Direction::Inverted;
295+
options.min_width = 0.2;
296+
// Maybe disable this some day; but for now it makes debugging much
297+
// easier:
298+
options.pretty_xml = true;
299+
if to_be_post_processed {
300+
// Can't put structured text into subtitle, so have to do a hack.
301+
options.subtitle = Some("__FIL-SUBTITLE-HERE__".to_string());
302+
}
303+
get_flamegraph_with_options(lines, to_be_post_processed, options, Some(subtitle))
304+
}
305+
276306
#[cfg(test)]
277307
mod tests {
278308
use super::filter_to_useful_callstacks;

0 commit comments

Comments
 (0)