Skip to content
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

Add support for multiple contracts in profiler #960

Draft
wants to merge 1 commit into
base: add-libfunc-profiling
Choose a base branch
from
Draft
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
26 changes: 16 additions & 10 deletions src/bin/cairo-native-run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use cairo_native::{
starknet_stub::StubSyscallHandler,
};
use clap::{Parser, ValueEnum};
use starknet_types_core::felt::Felt;
use std::path::PathBuf;
use tracing_subscriber::{EnvFilter, FmtSubscriber};
use utils::{find_function, result_to_runresult};

mod utils;

#[derive(Clone, Debug, ValueEnum)]
Expand Down Expand Up @@ -63,6 +63,8 @@ fn main() -> anyhow::Result<()> {
let mut db = RootDatabase::builder().detect_corelib().build()?;
let main_crate_ids = setup_project(&mut db, &args.path)?;

cairo_native::metadata::profiler::ProfilerImpl::push_contract(Felt::ZERO);

let sierra_program = compile_prepared_db(
&db,
main_crate_ids,
Expand Down Expand Up @@ -149,15 +151,19 @@ fn main() -> anyhow::Result<()> {

let mut trace = HashMap::<ConcreteLibfuncId, (Vec<u64>, u64)>::new();

for (statement_idx, tick_delta) in cairo_native::metadata::profiler::ProfilerImpl::take() {
if let Statement::Invocation(invocation) = &sierra_program.statements[statement_idx.0] {
let (tick_deltas, extra_count) =
trace.entry(invocation.libfunc_id.clone()).or_default();

if tick_delta != u64::MAX {
tick_deltas.push(tick_delta);
} else {
*extra_count += 1;
for (_, contract_trace) in cairo_native::metadata::profiler::ProfilerImpl::take() {
for (statement_idx, tick_delta) in contract_trace {
if let Statement::Invocation(invocation) =
&sierra_program.statements[statement_idx.0]
{
let (tick_deltas, extra_count) =
trace.entry(invocation.libfunc_id.clone()).or_default();

if tick_delta != u64::MAX {
tick_deltas.push(tick_delta);
} else {
*extra_count += 1;
}
}
}
}
Expand Down
44 changes: 34 additions & 10 deletions src/metadata/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use melior::{
},
Context,
};
use std::{cell::UnsafeCell, mem};
use starknet_types_core::felt::Felt;
use std::{cell::UnsafeCell, collections::HashMap, mem};

pub struct ProfilerMeta {
_private: (),
Expand Down Expand Up @@ -193,34 +194,57 @@ impl ProfilerMeta {
}

thread_local! {
static PROFILER_IMPL: UnsafeCell<ProfilerImpl> = const { UnsafeCell::new(ProfilerImpl::new()) };
static PROFILER_IMPL: UnsafeCell<ProfilerImpl> = UnsafeCell::new(ProfilerImpl::new()) ;
}

pub struct ProfilerImpl {
trace: Vec<(StatementIdx, u64)>,
traces: HashMap<Felt, Vec<(StatementIdx, u64)>>,
contracts: Vec<Felt>,
}

impl ProfilerImpl {
const fn new() -> Self {
Self { trace: Vec::new() }
fn new() -> Self {
Self {
traces: HashMap::new(),
contracts: Vec::new(),
}
}

pub fn take() -> Vec<(StatementIdx, u64)> {
pub fn push_contract(hash: Felt) {
PROFILER_IMPL.with(|x| {
let x = unsafe { &mut *x.get() };

let mut trace = Vec::new();
mem::swap(&mut x.trace, &mut trace);
x.contracts.push(hash.clone());
x.traces.entry(hash).or_insert(Vec::new());
})
}

pub fn pop_contract(&mut self) {
PROFILER_IMPL.with(|x| {
let x = unsafe { &mut *x.get() };

x.contracts.pop();
})
}

pub fn take() -> HashMap<Felt, Vec<(StatementIdx, u64)>> {
PROFILER_IMPL.with(|x| {
let x = unsafe { &mut *x.get() };

let mut traces = HashMap::new();
mem::swap(&mut x.traces, &mut traces);

trace
traces
})
}

pub extern "C" fn callback(statement_idx: u64, tick_delta: u64) {
PROFILER_IMPL.with(|x| {
let x = unsafe { &mut *x.get() };

x.trace
x.traces
.get_mut(x.contracts.last().unwrap())
.unwrap()
.push((StatementIdx(statement_idx as usize), tick_delta));
});
}
Expand Down
Loading