Skip to content

Wrap analyzeme 0.7.0 and 9.0.0 under a single API #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 28 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion site/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ async-trait = "0.1"
database = { path = "../database" }
bytes = "0.5.6"
url = "2"
analyzeme = { git = "https://github.com/rust-lang/measureme" }
tar = "0.4"
inferno = { version="0.10", default-features = false }
mime = "0.3"
analyzeme-7 = { package = "analyzeme", git = "https://github.com/rust-lang/measureme", rev = "de3edc5cfb6961580dc04b720de963510c209fc4" }
analyzeme-9 = { package = "analyzeme", git = "https://github.com/rust-lang/measureme", rev = "2c45b109a98ff143be39bc0c14ae2153fa0ca8ca" }

[dependencies.collector]
path = "../collector"
Expand Down
51 changes: 3 additions & 48 deletions site/src/self_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use anyhow::Context;
use bytes::buf::BufExt;
use hyper::StatusCode;
use std::collections::HashMap;
use std::fmt;
use std::io::Read;

type Response = http::Response<hyper::Body>;

mod versioning;
pub mod crox;
pub mod flamegraph;

use versioning::Pieces;

pub struct Output {
pub data: Vec<u8>,
pub filename: &'static str,
Expand Down Expand Up @@ -49,22 +50,6 @@ pub fn generate(
}
}

pub struct Pieces {
pub string_data: Vec<u8>,
pub string_index: Vec<u8>,
pub events: Vec<u8>,
}

impl fmt::Debug for Pieces {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Pieces")
.field("string_data", &self.string_data.len())
.field("string_index", &self.string_index.len())
.field("events", &self.events.len())
.finish()
}
}

pub async fn get_pieces(
body: crate::api::self_profile_raw::Request,
data: &InputData,
Expand Down Expand Up @@ -118,33 +103,3 @@ pub async fn get_pieces(
};
Ok(pieces)
}

impl Pieces {
fn from_tarball<R: std::io::Read>(mut tarball: tar::Archive<R>) -> anyhow::Result<Pieces> {
let mut pieces = Pieces {
string_data: Vec::new(),
string_index: Vec::new(),
events: Vec::new(),
};

for entry in tarball.entries().context("entries")? {
let mut entry = entry.context("tarball entry")?;
let path = entry.path_bytes();
if *path == *b"self-profile.string_index" {
entry
.read_to_end(&mut pieces.string_index)
.context("reading string index")?;
} else if *path == *b"self-profile.string_data" {
entry
.read_to_end(&mut pieces.string_data)
.context("reading string data")?;
} else if *path == *b"self-profile.events" {
entry
.read_to_end(&mut pieces.events)
.context("reading events")?;
}
}

Ok(pieces)
}
}
37 changes: 5 additions & 32 deletions site/src/self_profile/crox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use hashbrown::HashMap;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use analyzeme::{ProfilingData, Timestamp};
use super::versioning::ProfilingData;

use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};
Expand Down Expand Up @@ -65,11 +65,11 @@ fn generate_thread_to_collapsed_thread_mapping(
thread_start_and_end
.entry(event.thread_id)
.and_modify(|(thread_start, thread_end)| {
let (event_min, event_max) = timestamp_to_min_max(event.timestamp);
let (event_min, event_max) = event.timestamp.to_min_max();
*thread_start = cmp::min(*thread_start, event_min);
*thread_end = cmp::max(*thread_end, event_max);
})
.or_insert_with(|| timestamp_to_min_max(event.timestamp));
.or_insert_with(|| event.timestamp.to_min_max());
}
// collect the the threads in order of the end time
let mut end_and_thread = thread_start_and_end
Expand Down Expand Up @@ -109,29 +109,13 @@ fn generate_thread_to_collapsed_thread_mapping(
thread_to_collapsed_thread
}

fn get_args(full_event: &analyzeme::Event) -> Option<HashMap<String, String>> {
if !full_event.additional_data.is_empty() {
Some(
full_event
.additional_data
.iter()
.enumerate()
.map(|(i, arg)| (format!("arg{}", i).to_string(), arg.to_string()))
.collect(),
)
} else {
None
}
}

/// Returns JSON blob fit, `chrome_profiler.json`.
pub fn generate(pieces: super::Pieces, opt: Opt) -> anyhow::Result<Vec<u8>> {
let mut serializer = serde_json::Serializer::new(Vec::new());

let mut seq = serializer.serialize_seq(None)?;

let data = ProfilingData::from_buffers(pieces.string_data, pieces.string_index, pieces.events)
.map_err(|e| anyhow::format_err!("{:?}", e))?;
let data = pieces.into_profiling_data()?;

let thread_to_collapsed_thread =
generate_thread_to_collapsed_thread_mapping(opt.collapse_threads, &data);
Expand All @@ -156,7 +140,7 @@ pub fn generate(pieces: super::Pieces, opt: Opt) -> anyhow::Result<Vec<u8>> {
thread_id: *thread_to_collapsed_thread
.get(&event.thread_id)
.unwrap_or(&event.thread_id),
args: get_args(&full_event),
args: full_event.get_args(),
};
seq.serialize_element(&crox_event)?;
}
Expand Down Expand Up @@ -201,14 +185,3 @@ pub fn generate(pieces: super::Pieces, opt: Opt) -> anyhow::Result<Vec<u8>> {

Ok(serializer.into_inner())
}

fn timestamp_to_min_max(timestamp: Timestamp) -> (SystemTime, SystemTime) {
match timestamp {
Timestamp::Instant(t) => (t, t),
Timestamp::Interval { start, end } => {
// Usually start should always be greater than end, but let's not
// choke on invalid data here.
(cmp::min(start, end), cmp::max(start, end))
}
}
}
10 changes: 1 addition & 9 deletions site/src/self_profile/flamegraph.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
use analyzeme::{collapse_stacks, ProfilingData};
use anyhow::Context;
use inferno::flamegraph::{from_lines, Options as FlamegraphOptions};

#[derive(serde::Deserialize, Debug)]
pub struct Opt {}

pub fn generate(title: &str, pieces: super::Pieces, _: Opt) -> anyhow::Result<Vec<u8>> {
let profiling_data =
ProfilingData::from_buffers(pieces.string_data, pieces.string_index, pieces.events)
.map_err(|e| anyhow::format_err!("{:?}", e))?;

let recorded_stacks = collapse_stacks(&profiling_data)
.iter()
.map(|(unique_stack, count)| format!("{} {}", unique_stack, count))
.collect::<Vec<_>>();
let recorded_stacks = pieces.into_collapsed_stacks()?;

let mut file = Vec::new();
let mut flamegraph_options = FlamegraphOptions::default();
Expand Down
Loading