Skip to content

Commit

Permalink
Build inlined functions in pprof
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 committed Jun 10, 2024
1 parent 18a1596 commit 174a8bb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
85 changes: 50 additions & 35 deletions crates/cairo-profiler/src/profile_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ impl From<FunctionId> for u64 {
struct ProfilerContext {
strings: HashMap<String, StringId>,
functions: HashMap<FunctionName, pprof::Function>,
locations: HashMap<FunctionName, pprof::Location>,
locations: Vec<pprof::Location>,
}

impl ProfilerContext {
fn new() -> Self {
ProfilerContext {
strings: vec![(String::new(), StringId(0))].into_iter().collect(),
functions: HashMap::new(),
locations: HashMap::new(),
locations: vec![],
}
}

Expand All @@ -66,35 +66,52 @@ impl ProfilerContext {
}
}

fn location_id(&mut self, location: &Function) -> LocationId {
let location = match location {
Function::Entrypoint(function_name)
| Function::InternalFunction(InternalFunction::NonInlined(function_name)) => {
function_name
// TODO: optimize with map
fn locations_ids(&mut self, locations: &[Function]) -> Vec<LocationId> {
let mut pprof_locations = vec![];
for (index, loc) in locations.iter().enumerate() {
match loc {
Function::InternalFunction(InternalFunction::NonInlined(function_name))
| Function::Entrypoint(function_name) => {
let line = pprof::Line {
function_id: self.function_id(function_name).into(),
line: 0,
};
let location_data = pprof::Location {
id: (self.locations.len() + 1 + index) as u64,
mapping_id: 0,
address: 0,
line: vec![line],
is_folded: true,
};
pprof_locations.push(location_data);
}
Function::InternalFunction(InternalFunction::Inlined(function_name)) => {
let line = pprof::Line {
function_id: self.function_id(function_name).into(),
line: 0,
};
let location_data = pprof_locations
.last_mut()
.expect("Inlined function was on top of the call trace, but shouldn't");

location_data.line.push(line);
}
}
Function::InternalFunction(InternalFunction::_Inlined(_)) => {
todo!("Unused, logic for it will be added in the next PR")
}
};

if let Some(loc) = self.locations.get(location) {
LocationId(loc.id)
} else {
let line = pprof::Line {
function_id: self.function_id(location).into(),
line: 0,
};
let location_data = pprof::Location {
id: (self.locations.len() + 1) as u64,
mapping_id: 0,
address: 0,
line: vec![line],
is_folded: true,
};
}

self.locations.insert(location.clone(), location_data);
LocationId(self.locations.len() as u64)
for location in &mut pprof_locations {
// pprof format represents callstack from the least meaningful elements
location.line.reverse();
}

let locations_ids = pprof_locations
.iter()
.map(|loc| LocationId(loc.id))
.collect();
self.locations.append(&mut pprof_locations);

locations_ids
}

fn function_id(&mut self, function_name: &FunctionName) -> FunctionId {
Expand All @@ -121,9 +138,7 @@ impl ProfilerContext {

let functions = self.functions.into_values().collect();

let locations = self.locations.into_values().collect();

(string_table, functions, locations)
(string_table, functions, self.locations)
}
}

Expand Down Expand Up @@ -158,10 +173,10 @@ fn build_samples(
let samples = samples
.iter()
.map(|s| pprof::Sample {
location_id: s
.call_stack
.iter()
.map(|loc| context.location_id(loc).into())
location_id: context
.locations_ids(&s.call_stack)
.into_iter()
.map(Into::into)
.rev() // pprof format represents callstack from the least meaningful element
.collect(),
value: all_measurements_units
Expand Down
3 changes: 2 additions & 1 deletion crates/cairo-profiler/src/trace_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ pub enum Function {

#[derive(PartialEq, Eq, Hash, Clone)]
pub enum InternalFunction {
_Inlined(FunctionName),
#[allow(dead_code)]
Inlined(FunctionName),
NonInlined(FunctionName),
}

Expand Down

0 comments on commit 174a8bb

Please sign in to comment.