diff options
author | Nick Fitzgerald <fitzgen@gmail.com> | 2016-04-16 11:48:23 -0700 |
---|---|---|
committer | Nick Fitzgerald <fitzgen@gmail.com> | 2016-04-27 18:35:17 -0700 |
commit | 9fbb5c720eb89947720fa745aa08fa4fed7143b4 (patch) | |
tree | de0dbc67ce18b2dbb6b2960a90fccbb69b31ffb7 /components/profile/trace_dump.rs | |
parent | 311dd0f930b9e8e90d08151f1956e2da25737d8a (diff) | |
download | servo-9fbb5c720eb89947720fa745aa08fa4fed7143b4.tar.gz servo-9fbb5c720eb89947720fa745aa08fa4fed7143b4.zip |
Add a method for dumping self-contained HTML timeline profiles
This commit adds the `--profiler-trace-path` flag. When combined with `-p` to
enable profiling, it dumps a profile as a self-contained HTML file to the given
path. The profile visualizes the traced operations as a gant-chart style
timeline.
Diffstat (limited to 'components/profile/trace_dump.rs')
-rw-r--r-- | components/profile/trace_dump.rs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/components/profile/trace_dump.rs b/components/profile/trace_dump.rs new file mode 100644 index 00000000000..8f53d4a9e37 --- /dev/null +++ b/components/profile/trace_dump.rs @@ -0,0 +1,79 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +//! A module for writing time profiler traces out to a self contained HTML file. + +use profile_traits::time::{ProfilerCategory, TimerMetadata}; +use serde_json::{self}; +use std::fs; +use std::io::Write; + +/// An RAII class for writing the HTML trace dump. +pub struct TraceDump { + file: fs::File, +} + +#[derive(Debug, Serialize)] +struct TraceEntry { + category: ProfilerCategory, + metadata: Option<TimerMetadata>, + + #[serde(rename = "startTime")] + start_time: u64, + + #[serde(rename = "endTime")] + end_time: u64, + + #[serde(rename = "startEnergy")] + start_energy: u64, + + #[serde(rename = "endEnergy")] + end_energy: u64, +} + +impl TraceDump { + /// Create a new TraceDump and write the prologue of the HTML file out to + /// disk. + pub fn new(mut file: fs::File) -> TraceDump { + write_prologue(&mut file); + TraceDump { file: file } + } + + /// Write one trace to the trace dump file. + pub fn write_one(&mut self, + category: &(ProfilerCategory, Option<TimerMetadata>), + time: (u64, u64), + energy: (u64, u64)) { + let entry = TraceEntry { + category: category.0, + metadata: category.1.clone(), + start_time: time.0, + end_time: time.1, + start_energy: energy.0, + end_energy: energy.1, + }; + serde_json::to_writer(&mut self.file, &entry).unwrap(); + writeln!(&mut self.file, ",").unwrap(); + } +} + +impl Drop for TraceDump { + /// Write the epilogue of the trace dump HTML file out to disk on + /// destruction. + fn drop(&mut self) { + write_epilogue(&mut self.file); + } +} + +fn write_prologue(file: &mut fs::File) { + writeln!(file, "{}", include_str!("./trace-dump-prologue-1.html")).unwrap(); + writeln!(file, "{}", include_str!("./trace-dump.css")).unwrap(); + writeln!(file, "{}", include_str!("./trace-dump-prologue-2.html")).unwrap(); +} + +fn write_epilogue(file: &mut fs::File) { + writeln!(file, "{}", include_str!("./trace-dump-epilogue-1.html")).unwrap(); + writeln!(file, "{}", include_str!("./trace-dump.js")).unwrap(); + writeln!(file, "{}", include_str!("./trace-dump-epilogue-2.html")).unwrap(); +} |