-
Notifications
You must be signed in to change notification settings - Fork 109
/
mod.rs
605 lines (544 loc) · 20.3 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
mod attrs;
mod color;
mod merge;
mod svg;
pub use attrs::FuncFrameAttrsMap;
pub use color::BackgroundColor;
pub use color::Palette;
use num_format::Locale;
use quick_xml::{
events::{BytesEnd, BytesStart, BytesText, Event},
Writer,
};
use std::io;
use std::io::prelude::*;
use str_stack::StrStack;
use svg::StyleOptions;
const IMAGEWIDTH: usize = 1200; // max width, pixels
const FRAMEHEIGHT: usize = 16; // max height is dynamic
const FONTSIZE: usize = 12; // base text size
const FONTWIDTH: f64 = 0.59; // avg width relative to FONTSIZE
const MINWIDTH: f64 = 0.1; // min function width, pixels
const YPAD1: usize = FONTSIZE * 3; // pad top, include title
const YPAD2: usize = FONTSIZE * 2 + 10; // pad bottom, include labels
const XPAD: usize = 10; // pad lefm and right
const FRAMEPAD: usize = 1; // vertical padding for frames
/// Configure the flame graph.
#[derive(Debug)]
pub struct Options {
/// The color palette to use when plotting.
pub colors: color::Palette,
/// The background color for the plot.
///
/// If `None`, the background color will be selected based on the value of `colors`.
pub bgcolors: Option<color::BackgroundColor>,
/// Choose names based on the hashes of function names.
///
/// This will cause similar functions to be colored similarly.
pub hash: bool,
/// Store the choice of color for each function so that later invocations use the same colors.
///
/// With this option enabled, a file called `palette.map` will be created the first time a
/// flame graph is generated, and the color chosen for each function will be written into it.
/// On subsequent invocations, functions that already have a color registered in that file will
/// be given the stored color rather than be assigned a new one. New functions will have their
/// colors persisted for future runs.
///
/// This feature was first implemented [by Shawn
/// Sterling](https://github.com/brendangregg/FlameGraph/pull/25).
pub consistent_palette: bool,
/// If `consistent_palette` is set to `true` the palette will be stored in this file.
///
/// Defaults to "palette.map"
pub palette_file: String,
/// Assign extra attributes to particular functions.
///
/// In particular, if a function appears in the given map, it will have extra attributes set in
/// the resulting SVG based on its value in the map.
pub func_frameattrs: FuncFrameAttrsMap,
/// Whether to plot a plot that grows top-to-bottom or bottom-up (the default).
pub direction: Direction,
/// The title for the flame chart.
///
/// Defaults to "Flame Graph".
pub title: String,
/// By default, if [differential] samples are included in the provided stacks, the resulting
/// flame graph will compute and show differentials as `sample#2 - sample#1`. If this option is
/// set, the differential is instead computed using `sample#1 - sample#2`.
///
/// [differential]: http://www.brendangregg.com/blog/2014-11-09/differential-flame-graphs.html
pub negate_differentials: bool,
/// Factor to scale sample counts by in the flame graph.
///
/// This option can be useful if the sample data has fractional sample counts since the fractional
/// parts are stripped off when creating the flame graph. To work around this you can scale up the
/// sample counts to be integers, then scale them back down in the graph with the `factor` option.
///
/// For example, if you have `23.4` as a sample count you can upscale it to `234`, then set `factor`
/// to `0.1`.
///
/// Defaults to 1.0.
pub factor: f64,
}
impl Default for Options {
fn default() -> Self {
Options {
title: "Flame Graph".to_string(),
palette_file: "palette.map".to_string(),
factor: 1.0,
colors: Default::default(),
bgcolors: Default::default(),
hash: Default::default(),
consistent_palette: Default::default(),
func_frameattrs: Default::default(),
direction: Default::default(),
negate_differentials: Default::default(),
}
}
}
/// The direction the plot should grow.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Direction {
/// Stacks grow from the bottom to the top.
///
/// The `(all)` meta frame will be at the bottom.
Straight,
/// Stacks grow from the top to the bottom.
///
/// The `(all)` meta frame will be at the top.
Inverted,
}
impl Default for Direction {
fn default() -> Self {
Direction::Straight
}
}
macro_rules! args {
($($key:expr => $value:expr),*) => {{
[$(($key, $value),)*].into_iter().map(|(k, v): &(&str, &str)| (*k, *v))
}};
}
struct FrameAttributes<'a> {
title: &'a str,
class: &'a str,
onmouseover: &'a str,
onmouseout: &'a str,
onclick: &'a str,
style: Option<&'a str>,
g_extra: Option<&'a Vec<(String, String)>>,
href: Option<&'a str>,
target: &'a str,
a_extra: Option<&'a Vec<(String, String)>>,
}
fn override_or_add_attributes<'a>(
title: &'a str,
attributes: Option<&'a attrs::FrameAttrs>,
) -> FrameAttributes<'a> {
let mut title = title;
let mut class = "func_g";
let mut onmouseover = "s(this)";
let mut onmouseout = "c()";
let mut onclick = "zoom(this)";
let mut style = None;
let mut g_extra = None;
let mut href = None;
let mut target = "_top";
let mut a_extra = None;
// Handle any overridden or extra attributes.
if let Some(attrs) = attributes {
if let Some(ref c) = attrs.g.class {
class = c.as_str();
}
if let Some(ref c) = attrs.g.style {
style = Some(c.as_str());
}
if let Some(ref o) = attrs.g.onmouseover {
onmouseover = o.as_str();
}
if let Some(ref o) = attrs.g.onmouseout {
onmouseout = o.as_str();
}
if let Some(ref o) = attrs.g.onclick {
onclick = o.as_str();
}
if let Some(ref t) = attrs.title {
title = t.as_str();
}
g_extra = Some(&attrs.g.extra);
if let Some(ref h) = attrs.a.href {
href = Some(h.as_str());
}
if let Some(ref t) = attrs.a.target {
target = t.as_str();
}
a_extra = Some(&attrs.a.extra);
}
FrameAttributes {
title,
class,
onmouseover,
onmouseout,
onclick,
style,
g_extra,
href,
target,
a_extra,
}
}
struct Rectangle {
x1: usize,
y1: usize,
x2: usize,
y2: usize,
}
impl Rectangle {
fn width(&self) -> usize {
self.x2 - self.x1
}
fn height(&self) -> usize {
self.y2 - self.y1
}
}
/// Produce a flame graph from a sorted iterator over folded stack lines.
///
/// This function expects each folded stack to contain the following whitespace-separated fields:
///
/// - A semicolon-separated list of frame names (e.g., `main;foo;bar;baz`).
/// - A sample count for the given stack.
/// - An optional second sample count.
///
/// If two sample counts are provided, a [differential flame graph] is produced. In this mode, the
/// flame graph uses the difference between the two sample counts to show how the sample counts for
/// each stack has changed between the first and second profiling.
///
/// The resulting flame graph will be written out to `writer` in SVG format.
///
/// [differential flame graph]: http://www.brendangregg.com/blog/2014-11-09/differential-flame-graphs.html
#[allow(clippy::cyclomatic_complexity)]
pub fn from_sorted_lines<'a, I, W>(opt: Options, lines: I, writer: W) -> quick_xml::Result<()>
where
I: IntoIterator<Item = &'a str>,
W: Write,
{
let mut palette_map = if opt.consistent_palette {
Some(color::PaletteMap::load(&opt.palette_file)?)
} else {
None
};
let (bgcolor1, bgcolor2) = color::bgcolor_for(opt.bgcolors, opt.colors);
let mut buffer = StrStack::new();
let (mut frames, time, ignored, delta_max) = merge::frames(lines);
if ignored != 0 {
warn!("Ignored {} lines with invalid format", ignored);
}
// let's start writing the svg!
let mut svg = Writer::new(writer);
if time == 0 {
error!("No stack counts found");
// emit an error message SVG, for tools automating flamegraph use
let imageheight = FONTSIZE * 5;
svg::write_header(&mut svg, imageheight)?;
svg::write_str(
&mut svg,
&mut buffer,
svg::TextItem {
color: "black",
size: FONTSIZE + 2,
x: (IMAGEWIDTH / 2) as f64,
y: (FONTSIZE * 2) as f64,
text: "ERROR: No valid input provided to flamegraph".into(),
location: Some("middle"),
extra: None,
},
)?;
svg.write_event(Event::End(BytesEnd::borrowed(b"svg")))?;
svg.write_event(Event::Eof)?;
return Err(quick_xml::Error::Io(io::Error::new(
io::ErrorKind::InvalidData,
"No stack counts found",
)));
}
let timemax = time;
let widthpertime = (IMAGEWIDTH - 2 * XPAD) as f64 / timemax as f64;
let minwidth_time = MINWIDTH / widthpertime;
// prune blocks that are too narrow
let mut depthmax = 0;
frames.retain(|frame| {
if ((frame.end_time - frame.start_time) as f64) < minwidth_time {
false
} else {
depthmax = std::cmp::max(depthmax, frame.location.depth);
true
}
});
// draw canvas, and embed interactive JavaScript program
let imageheight = ((depthmax + 1) * FRAMEHEIGHT) + YPAD1 + YPAD2;
svg::write_header(&mut svg, imageheight)?;
let style_options = StyleOptions {
imageheight,
bgcolor1,
bgcolor2,
};
svg::write_prelude(&mut svg, &style_options, &opt)?;
// Used when picking color parameters at random, when no option determines how to pick these
// parameters. We instanciate it here because it may be called once for each iteration in the
// frames loop.
let mut thread_rng = rand::thread_rng();
// structs to reuse accross loops to avoid allocations
let mut cache_g = Event::Start({ BytesStart::owned_name("g") });
let mut cache_a = Event::Start({ BytesStart::owned_name("a") });
let mut cache_rect = Event::Empty(BytesStart::owned_name("rect"));
// draw frames
let mut samples_txt_buffer = num_format::Buffer::default();
for frame in frames {
let x1 = XPAD + (frame.start_time as f64 * widthpertime) as usize;
let x2 = XPAD + (frame.end_time as f64 * widthpertime) as usize;
let (y1, y2) = match opt.direction {
Direction::Straight => {
let y1 = imageheight - YPAD2 - (frame.location.depth + 1) * FRAMEHEIGHT + FRAMEPAD;
let y2 = imageheight - YPAD2 - frame.location.depth * FRAMEHEIGHT;
(y1, y2)
}
Direction::Inverted => {
let y1 = YPAD1 + frame.location.depth * FRAMEHEIGHT;
let y2 = YPAD1 + (frame.location.depth + 1) * FRAMEHEIGHT - FRAMEPAD;
(y1, y2)
}
};
let rect = Rectangle { x1, y1, x2, y2 };
// The rounding here can differ from the Perl version when the fractional part is `0.5`.
// The Perl version does `my $samples = sprintf "%.0f", ($etime - $stime) * $factor;`,
// but this can format in strange ways as shown in these examples:
// `sprintf "%.0f", 1.5` produces "2"
// `sprintf "%.0f", 2.5` produces "2"
// `sprintf "%.0f", 3.5` produces "4"
let samples = ((frame.end_time - frame.start_time) as f64 * opt.factor).round() as usize;
// add thousands separators to `samples`
let _ = samples_txt_buffer.write_formatted(&samples, &Locale::en);
let samples_txt = samples_txt_buffer.as_str();
let info = if frame.location.function.is_empty() && frame.location.depth == 0 {
write!(buffer, "all ({} samples, 100%)", samples_txt)
} else {
let pct = (100 * samples) as f64 / (timemax as f64 * opt.factor);
let function = deannotate(&frame.location.function);
match frame.delta {
None => write!(
buffer,
"{} ({} samples, {:.2}%)",
function, samples_txt, pct
),
// Special case delta == 0 so we don't format percentage with a + sign.
Some(delta) if delta == 0 => write!(
buffer,
"{} ({} samples, {:.2}%; 0.00%)",
function, samples_txt, pct,
),
Some(mut delta) => {
if opt.negate_differentials {
delta = -delta;
}
let delta_pct = (100 * delta) as f64 / (timemax as f64 * opt.factor);
write!(
buffer,
"{} ({} samples, {:.2}%; {:+.2}%)",
function, samples_txt, pct, delta_pct
)
}
}
};
let frame_attributes = opt
.func_frameattrs
.frameattrs_for_func(frame.location.function);
let frame_attributes = override_or_add_attributes(&buffer[info], frame_attributes);
let href_is_some = frame_attributes.href.is_some();
if let Event::Start(ref mut g) = cache_g {
// clear the BytesStart
g.clear_attributes();
g.extend_attributes(args!(
"class" => frame_attributes.class,
"onmouseover" => frame_attributes.onmouseover,
"onmouseout" => frame_attributes.onmouseout,
"onclick" => frame_attributes.onclick
));
// add optional attributes
if let Some(style) = frame_attributes.style {
g.extend_attributes(std::iter::once(("style", style)));
}
if let Some(extra) = frame_attributes.g_extra {
g.extend_attributes(extra.iter().map(|(k, v)| (k.as_str(), v.as_str())));
}
} else {
unreachable!("cache wrapper was of wrong type: {:?}", cache_g);
}
svg.write_event(&cache_g)?;
svg.write_event(Event::Start(BytesStart::borrowed_name(b"title")))?;
svg.write_event(Event::Text(BytesText::from_plain_str(
frame_attributes.title,
)))?;
svg.write_event(Event::End(BytesEnd::borrowed(b"title")))?;
if let Some(href) = frame_attributes.href {
if let Event::Start(ref mut a) = cache_a {
// clear the BytesStart
a.clear_attributes();
a.extend_attributes(args!(
"xlink:href" => href,
"target" => frame_attributes.target
));
if let Some(extra) = frame_attributes.a_extra {
a.extend_attributes(extra.iter().map(|(k, v)| (k.as_str(), v.as_str())));
}
} else {
unreachable!("cache wrapper was of wrong type: {:?}", cache_a);
}
svg.write_event(&cache_a)?;
}
// select the color of the rectangle
let color = if frame.location.function == "--" {
color::VDGREY
} else if frame.location.function == "-" {
color::DGREY
} else if let Some(mut delta) = frame.delta {
if opt.negate_differentials {
delta = -delta;
}
color::color_scale(delta, delta_max)
} else if let Some(ref mut palette_map) = palette_map {
palette_map.find_color_for(&frame.location.function, |name| {
color::color(opt.colors, opt.hash, name, &mut thread_rng)
})
} else {
color::color(
opt.colors,
opt.hash,
frame.location.function,
&mut thread_rng,
)
};
filled_rectangle(&mut svg, &mut buffer, &rect, color, &mut cache_rect)?;
let fitchars = (rect.width() as f64 / (FONTSIZE as f64 * FONTWIDTH)).trunc() as usize;
let text: svg::TextArgument = if fitchars >= 3 {
// room for one char plus two dots
let f = deannotate(&frame.location.function);
// TODO: use Unicode grapheme clusters instead
if f.len() < fitchars {
// no need to truncate
f.into()
} else {
// need to truncate :'(
use std::fmt::Write;
let mut w = buffer.writer();
for c in f.chars().take(fitchars - 2) {
w.write_char(c).expect("writing to buffer shouldn't fail");
}
w.write_str("..").expect("writing to buffer shouldn't fail");
w.finish().into()
}
} else {
// don't show the function name
"".into()
};
// write the text
svg::write_str(
&mut svg,
&mut buffer,
svg::TextItem {
color: "rgb(0, 0, 0)",
size: FONTSIZE,
x: rect.x1 as f64 + 3.0,
y: 3.0 + (rect.y1 + rect.y2) as f64 / 2.0,
text,
location: None,
extra: None,
},
)?;
buffer.clear();
if href_is_some {
svg.write_event(Event::End(BytesEnd::borrowed(b"a")))?;
}
svg.write_event(Event::End(BytesEnd::borrowed(b"g")))?;
}
svg.write_event(Event::End(BytesEnd::borrowed(b"svg")))?;
svg.write_event(Event::Eof)?;
if let Some(palette_map) = palette_map {
palette_map
.save(&opt.palette_file)
.map_err(quick_xml::Error::Io)?;
}
Ok(())
}
/// Produce a flame graph from a reader that contains a sorted sequence of folded stack lines.
///
/// See [`from_sorted_lines`] for the expected format of each line.
///
/// The resulting flame graph will be written out to `writer` in SVG format.
pub fn from_reader<R, W>(opt: Options, mut reader: R, writer: W) -> quick_xml::Result<()>
where
R: Read,
W: Write,
{
let mut input = String::new();
reader
.read_to_string(&mut input)
.map_err(quick_xml::Error::Io)?;
from_sorted_lines(opt, input.lines(), writer)
}
/// Produce a flame graph from a set of readers that contain folded stack lines.
///
/// This function sorts all the read lines before processing them.
/// See [`from_sorted_lines`] for the expected format of each line.
///
/// The resulting flame graph will be written out to `writer` in SVG format.
pub fn from_readers<R, W>(opt: Options, readers: R, writer: W) -> quick_xml::Result<()>
where
R: IntoIterator,
R::Item: Read,
W: Write,
{
let mut input = String::new();
for mut reader in readers {
reader
.read_to_string(&mut input)
.map_err(quick_xml::Error::Io)?;
}
let mut lines: Vec<&str> = input.lines().collect();
lines.sort_unstable();
from_sorted_lines(opt, lines, writer)
}
fn deannotate(f: &str) -> &str {
if f.ends_with(']') {
if let Some(ai) = f.rfind("_[") {
if f[ai..].len() == 4 && "kwij".contains(&f[ai + 2..ai + 3]) {
return &f[..ai];
}
}
}
f
}
fn filled_rectangle<W: Write>(
svg: &mut Writer<W>,
buffer: &mut StrStack,
rect: &Rectangle,
color: (u8, u8, u8),
cache_rect: &mut Event,
) -> quick_xml::Result<usize> {
let x = write!(buffer, "{}", rect.x1);
let y = write!(buffer, "{}", rect.y1);
let width = write!(buffer, "{}", rect.width());
let height = write!(buffer, "{}", rect.height());
let color = write!(buffer, "rgb({},{},{})", color.0, color.1, color.2);
if let Event::Empty(bytes_start) = cache_rect {
// clear the state
bytes_start.clear_attributes();
bytes_start.extend_attributes(args!(
"x" => &buffer[x],
"y" => &buffer[y],
"width" => &buffer[width],
"height" => &buffer[height],
"fill" => &buffer[color]
));
} else {
unreachable!("cache wrapper was of wrong type: {:?}", cache_rect);
}
svg.write_event(&cache_rect)
}