Skip to content

Commit

Permalink
Added option to print used resources for tests.
Browse files Browse the repository at this point in the history
commit-id:008454fd
  • Loading branch information
orizi committed Mar 17, 2024
1 parent c99f89e commit f903e0f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 40 deletions.
4 changes: 4 additions & 0 deletions crates/bin/cairo-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ struct Args {
/// [cairo_lang_test_runner::RunProfilerConfig]
#[clap(short, long, default_value_t, value_enum)]
run_profiler: RunProfilerConfigArg,
/// Whether to print resource usage after each test.
#[arg(long, default_value_t = false)]
print_resource_usage: bool,
}

fn main() -> anyhow::Result<()> {
Expand All @@ -69,6 +72,7 @@ fn main() -> anyhow::Result<()> {
ignored: args.ignored,
include_ignored: args.include_ignored,
run_profiler: args.run_profiler.into(),
print_resource_usage: args.print_resource_usage,
};

let runner = TestRunner::new(&args.path, args.starknet, args.allow_warnings, config)?;
Expand Down
34 changes: 22 additions & 12 deletions crates/cairo-lang-runner/src/casm_run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
use cairo_vm::vm::errors::hint_errors::HintError;
use cairo_vm::vm::errors::memory_errors::MemoryError;
use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
use cairo_vm::vm::runners::cairo_runner::{CairoRunner, ResourceTracker, RunResources};
use cairo_vm::vm::runners::cairo_runner::{
CairoRunner, ExecutionResources, ResourceTracker, RunResources,
};
use cairo_vm::vm::vm_core::VirtualMachine;
use dict_manager::DictManagerExecScope;
use itertools::Itertools;
Expand All @@ -41,7 +43,7 @@ use {ark_secp256k1 as secp256k1, ark_secp256r1 as secp256r1};
use self::contract_address::calculate_contract_address;
use self::dict_manager::DictSquashExecScope;
use crate::short_string::{as_cairo_short_string, as_cairo_short_string_ex};
use crate::{Arg, RunResultValue, SierraCasmRunner};
use crate::{Arg, RunResultValue, SierraCasmRunner, UsedResources};

#[cfg(test)]
mod test;
Expand Down Expand Up @@ -92,14 +94,15 @@ struct Secp256r1ExecutionScope {
/// HintProcessor for Cairo compiler hints.
pub struct CairoHintProcessor<'a> {
/// The Cairo runner.
#[allow(dead_code)]
pub runner: Option<&'a SierraCasmRunner>,
// A mapping from a string that represents a hint to the hint object.
/// A mapping from a string that represents a hint to the hint object.
pub string_to_hint: HashMap<String, Hint>,
// The starknet state.
/// The starknet state.
pub starknet_state: StarknetState,
// Maintains the resources of the run.
/// Maintains the resources of the run.
pub run_resources: RunResources,
/// Resources used during syscalls.
pub syscalls_used_resources: UsedResources,
}

pub fn cell_ref_to_relocatable(cell_ref: &CellRef, vm: &VirtualMachine) -> Relocatable {
Expand Down Expand Up @@ -131,7 +134,6 @@ pub struct StarknetState {
/// The values of addresses in the simulated storage per contract.
storage: HashMap<Felt252, HashMap<Felt252, Felt252>>,
/// A mapping from contract address to class hash.
#[allow(dead_code)]
deployed_contracts: HashMap<Felt252, Felt252>,
/// A mapping from contract address to logs.
logs: HashMap<Felt252, ContractLogs>,
Expand Down Expand Up @@ -651,7 +653,9 @@ impl<'a> CairoHintProcessor<'a> {
}
Ok(())
};
match std::str::from_utf8(&selector).unwrap() {
let selector = std::str::from_utf8(&selector).unwrap();
*self.syscalls_used_resources.syscalls.entry(selector.into()).or_default() += 1;
match selector {
"StorageWrite" => execute_handle_helper(&mut |system_buffer, gas_counter| {
self.storage_write(
gas_counter,
Expand Down Expand Up @@ -1117,7 +1121,7 @@ impl<'a> CairoHintProcessor<'a> {
self.starknet_state.clone(),
)
.expect("Internal runner error.");

self.syscalls_used_resources.add(res.used_resources);
*gas_counter = res.gas_counter.unwrap().to_usize().unwrap();
match res.value {
RunResultValue::Success(value) => {
Expand Down Expand Up @@ -2085,7 +2089,7 @@ pub struct RunFunctionContext<'a> {
pub data_len: usize,
}

type RunFunctionRes = (Vec<Option<Felt252>>, usize);
type RunFunctionRes = (Vec<Option<Felt252>>, usize, ExecutionResources);

/// Runs CairoRunner on layout with prime.
/// Allows injecting custom CairoRunner.
Expand Down Expand Up @@ -2146,8 +2150,14 @@ pub fn run_function<'a, 'b: 'a>(
let mut runner = build_cairo_runner(data, builtins, hints_dict)?;

run_function_with_runner(vm, data_len, additional_initialization, hint_processor, &mut runner)?;

Ok((runner.relocated_memory, vm.get_relocated_trace().unwrap().last().unwrap().ap))
let used_resources = runner
.get_execution_resources(vm)
.expect("Failed to get execution resources, but the run was successful.");
Ok((
runner.relocated_memory,
vm.get_relocated_trace().unwrap().last().unwrap().ap,
used_resources,
))
}

/// Formats the given felts as a debug string.
Expand Down
6 changes: 4 additions & 2 deletions crates/cairo-lang-runner/src/casm_run/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ fn test_runner(function: CasmContext, n_returns: usize, expected: &[i128]) {
string_to_hint,
starknet_state: StarknetState::default(),
run_resources: RunResources::default(),
syscalls_used_resources: Default::default(),
};
let bytecode: Vec<BigInt> = function
.instructions
.iter()
.flat_map(|instruction| instruction.assemble().encode())
.collect();

let (cells, ap) = run_function(
let (cells, ap, _) = run_function(
&mut VirtualMachine::new(true),
bytecode.iter(),
vec![],
Expand Down Expand Up @@ -154,11 +155,12 @@ fn test_allocate_segment() {
string_to_hint,
starknet_state: StarknetState::default(),
run_resources: RunResources::default(),
syscalls_used_resources: Default::default(),
};
let bytecode: Vec<BigInt> =
casm.instructions.iter().flat_map(|instruction| instruction.assemble().encode()).collect();

let (memory, ap) = run_function(
let (memory, ap, _) = run_function(
&mut VirtualMachine::new(true),
bytecode.iter(),
vec![],
Expand Down
45 changes: 35 additions & 10 deletions crates/cairo-lang-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use cairo_vm::hint_processor::hint_processor_definition::HintProcessor;
use cairo_vm::serde::deserialize_program::{BuiltinName, HintParams};
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
use cairo_vm::vm::runners::cairo_runner::RunResources;
use cairo_vm::vm::runners::cairo_runner::{ExecutionResources, RunResources};
use cairo_vm::vm::trace::trace_entry::TraceEntry;
use cairo_vm::vm::vm_core::VirtualMachine;
use casm_run::hint_to_hint_params;
Expand Down Expand Up @@ -85,6 +85,7 @@ pub struct RunResultStarknet {
pub memory: Vec<Option<Felt252>>,
pub value: RunResultValue,
pub starknet_state: StarknetState,
pub used_resources: UsedResources,
/// The profiling info of the run, if requested.
pub profiling_info: Option<ProfilingInfo>,
}
Expand All @@ -95,10 +96,29 @@ pub struct RunResult {
pub gas_counter: Option<Felt252>,
pub memory: Vec<Option<Felt252>>,
pub value: RunResultValue,
pub used_resources: ExecutionResources,
/// The profiling info of the run, if requested.
pub profiling_info: Option<ProfilingInfo>,
}

/// The used resources in a run.
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct UsedResources {
/// The basic execution resources.
pub execution_resources: ExecutionResources,
/// The used syscalls.
pub syscalls: HashMap<String, usize>,
}
impl UsedResources {
/// Adds the resources of `other` to `self`.
pub fn add(&mut self, other: Self) {
self.execution_resources += &other.execution_resources;
for (k, v) in other.syscalls {
*self.syscalls.entry(k).or_default() += v;
}
}
}

/// The ran function return value.
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum RunResultValue {
Expand Down Expand Up @@ -226,19 +246,24 @@ impl SierraCasmRunner {
starknet_state,
string_to_hint,
run_resources: RunResources::default(),
syscalls_used_resources: Default::default(),
};
let RunResult { gas_counter, memory, value, profiling_info } = self.run_function(
func,
&mut hint_processor,
hints_dict,
assembled_program.bytecode.iter(),
builtins,
)?;
let RunResult { gas_counter, memory, value, used_resources, profiling_info } = self
.run_function(
func,
&mut hint_processor,
hints_dict,
assembled_program.bytecode.iter(),
builtins,
)?;
let mut all_used_resources = hint_processor.syscalls_used_resources;
all_used_resources.execution_resources += &used_resources;
Ok(RunResultStarknet {
gas_counter,
memory,
value,
starknet_state: hint_processor.starknet_state,
used_resources: all_used_resources,
profiling_info,
})
}
Expand All @@ -262,7 +287,7 @@ impl SierraCasmRunner {
{
let return_types = self.generic_id_and_size_from_concrete(&func.signature.ret_types);

let (cells, ap) = casm_run::run_function(
let (cells, ap, used_resources) = casm_run::run_function(
vm,
bytecode,
builtins,
Expand All @@ -287,7 +312,7 @@ impl SierraCasmRunner {
self.collect_profiling_info(vm.get_relocated_trace().unwrap(), config.clone())
});

Ok(RunResult { gas_counter, memory: cells, value, profiling_info })
Ok(RunResult { gas_counter, memory: cells, value, used_resources, profiling_info })
}

/// Collects profiling info of the current run using the trace.
Expand Down
Loading

0 comments on commit f903e0f

Please sign in to comment.