Skip to content

Commit

Permalink
--max-function-trace-depth flag (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 authored May 22, 2024
1 parent 5c9ed93 commit 1cc351a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `--max-function-trace-depth` allowing to specify maximum depth of the function tree in function level profiling

## [0.3.0] - 2024-04-20

### Added
Expand Down
8 changes: 8 additions & 0 deletions crates/cairo-profiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ struct Cli {
/// Show contract addresses and function selectors in a trace tree
#[arg(long)]
show_details: bool,

/// Specify maximum depth of function tree in function level profiling.
/// The is applied per entrypoint - each entrypoint function tree is treated separately.
/// Keep in mind recursive functions are also taken into account even though they are later
/// aggregated to one function call.
#[arg(long, default_value_t = 100)]
max_function_trace_depth: usize,
}

fn main() -> Result<()> {
Expand All @@ -47,6 +54,7 @@ fn main() -> Result<()> {
&serialized_trace,
&compiled_artifacts_path_map,
cli.show_details,
cli.max_function_trace_depth,
)?;

let profile = build_profile(&samples);
Expand Down
5 changes: 5 additions & 0 deletions crates/cairo-profiler/src/trace_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub fn collect_samples_from_trace(
trace: &CallTrace,
compiled_artifacts_path_map: &CompiledArtifactsPathMap,
show_details: bool,
max_function_trace_depth: usize,
) -> Result<Vec<ContractCallSample>> {
let mut samples = vec![];
let mut current_path = vec![];
Expand All @@ -166,6 +167,7 @@ pub fn collect_samples_from_trace(
trace,
compiled_artifacts_path_map,
show_details,
max_function_trace_depth,
)?;
Ok(samples)
}
Expand All @@ -176,6 +178,7 @@ fn collect_samples<'a>(
trace: &'a CallTrace,
compiled_artifacts_path_map: &CompiledArtifactsPathMap,
show_details: bool,
max_function_trace_depth: usize,
) -> Result<&'a ExecutionResources> {
current_call_stack.push(EntryPointId::from(
trace.entry_point.contract_name.clone(),
Expand Down Expand Up @@ -204,6 +207,7 @@ fn collect_samples<'a>(
compiled_artifacts.sierra.get_program_artifact(),
&compiled_artifacts.casm_debug_info,
compiled_artifacts.sierra.was_run_with_header(),
max_function_trace_depth,
)?;

for mut function_stack_trace in profiling_info.functions_stack_traces {
Expand Down Expand Up @@ -236,6 +240,7 @@ fn collect_samples<'a>(
sub_trace,
compiled_artifacts_path_map,
show_details,
max_function_trace_depth,
)?;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use std::collections::HashMap;
use std::ops::AddAssign;
use trace_data::TraceEntry;

const MAX_TRACE_DEPTH: u8 = 100;

pub struct ProfilingInfo {
pub functions_stack_traces: Vec<FunctionStackTrace>,
pub header_steps: Steps,
Expand Down Expand Up @@ -52,6 +50,7 @@ pub fn collect_profiling_info(
program_artifact: &ProgramArtifact,
casm_debug_info: &CairoProgramDebugInfo,
was_run_with_header: bool,
max_function_trace_depth: usize,
) -> Result<ProfilingInfo> {
let program = &program_artifact.program;
let sierra_program_registry = &ProgramRegistry::<CoreType, CoreLibfunc>::new(program).unwrap();
Expand Down Expand Up @@ -137,7 +136,7 @@ pub fn collect_profiling_info(
Ok(CoreConcreteLibfunc::FunctionCall(_))
) {
// Push to the stack.
if function_stack_depth < MAX_TRACE_DEPTH as usize {
if function_stack_depth < max_function_trace_depth {
function_stack.push((user_function_idx, current_function_steps));
current_function_steps = Steps(0);
}
Expand All @@ -146,7 +145,7 @@ pub fn collect_profiling_info(
}
GenStatement::Return(_) => {
// Pop from the stack.
if function_stack_depth <= MAX_TRACE_DEPTH as usize {
if function_stack_depth <= max_function_trace_depth {
let current_stack =
chain!(function_stack.iter().map(|f| f.0), [user_function_idx])
.collect_vec();
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-profiler/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn output_path() {
assert!(temp_dir.join("my/output/dir/my_file.pb.gz").exists());
}

#[test_case(&["call.json", "--max-function-trace-depth", "5"]; "with max function trace depth")]
#[test_case(&["call.json", "--show-details"]; "with details")]
#[test_case(&["call.json"]; "without details")]
fn simple_package(args: &[&str]) {
Expand Down

0 comments on commit 1cc351a

Please sign in to comment.