Skip to content

Commit 301045c

Browse files
add LIBFUNC_PROFILES_MAP for collection profiles
1 parent 09614fa commit 301045c

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ cairo-lang-sierra = "2.12.0-dev.1"
100100
cairo-lang-sierra-to-casm = "2.12.0-dev.1"
101101
cairo-lang-starknet-classes = "2.12.0-dev.1"
102102
cairo-lang-utils = "2.12.0-dev.1"
103-
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "ce831bde6cef3bd9dd1cbac58917c828f904146c" }
104-
sierra-emu = { git = "https://github.com/lambdaclass/cairo_native", rev = "ce831bde6cef3bd9dd1cbac58917c828f904146c" }
103+
cairo-native = { git = "https://github.com/lambdaclass/cairo_native", rev = "6e0318913b8360b152f2837001044dd21426bedd" }
104+
sierra-emu = { git = "https://github.com/lambdaclass/cairo_native", rev = "6e0318913b8360b152f2837001044dd21426bedd" }
105105
cairo-vm = "2.0.1"
106106
camelpaste = "0.1.0"
107107
chrono = "0.4.26"

crates/blockifier/src/execution/native/executor.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use std::collections::HashMap;
12
use std::fs::{self, File};
23
use std::io::Write;
34
use std::path::PathBuf;
45
use std::sync::atomic::AtomicU64;
5-
use std::sync::Arc;
6+
use std::sync::{Arc, LazyLock, Mutex};
67

78
use cairo_lang_sierra::program::Program;
89
use cairo_lang_starknet_classes::compiler_version::VersionId;
@@ -12,11 +13,26 @@ use cairo_native::executor::AotContractExecutor;
1213
use cairo_native::starknet::StarknetSyscallHandler;
1314
use cairo_native::utils::BuiltinCosts;
1415
use itertools::Itertools;
16+
use serde::Serialize;
1517
use sierra_emu::VirtualMachine;
1618
use starknet_types_core::felt::Felt;
1719

1820
use super::syscall_handler::NativeSyscallHandler;
1921

22+
#[cfg(feature = "with-libfunc-profiling")]
23+
#[derive(Debug, Serialize)]
24+
pub struct ProfilerResults {
25+
pub libfunc_idx: u64,
26+
pub samples: u64,
27+
pub total_time: u64,
28+
pub average_time: f64,
29+
pub std_deviation: f64,
30+
pub quartiles: [u64; 5],
31+
}
32+
33+
pub static LIBFUNC_PROFILES_MAP: LazyLock<Mutex<HashMap<Felt, Vec<ProfilerResults>>>> =
34+
LazyLock::new(|| Mutex::new(HashMap::new()));
35+
2036
#[derive(Debug)]
2137
pub enum ContractExecutor {
2238
Aot(AotContractExecutor),
@@ -176,22 +192,28 @@ impl ContractExecutor {
176192
// Retreive trace dump for current execution
177193
let profile = LIBFUNC_PROFILE.lock().unwrap().remove(&counter).unwrap();
178194

179-
// Save trace dump to file
180-
let profile_path = PathBuf::from(format!("libfunc_profiles/{counter}.md"));
181-
let profile_parent_path = profile_path.parent().unwrap();
182-
fs::create_dir_all(profile_parent_path).unwrap();
183-
let mut profile_file = File::create(&profile_path).unwrap();
184-
185195
for (libfunc_id, (n_samples, sum, quartiles, average, std_dev)) in
186196
profile.process()
187197
{
188-
writeln!(profile_file, "{libfunc_id}")?;
189-
writeln!(profile_file, " Total Samples: {n_samples}")?;
190-
writeln!(profile_file, " Total Execution Time: {sum}")?;
191-
writeln!(profile_file, " Average Execution Time: {average}")?;
192-
writeln!(profile_file, " Standard Deviation: {std_dev}")?;
193-
writeln!(profile_file, " Quartiles: {quartiles:?}")?;
194-
writeln!(profile_file)?;
198+
let profile_result = ProfilerResults {
199+
libfunc_idx: libfunc_id.id,
200+
samples: n_samples,
201+
total_time: sum,
202+
average_time: average,
203+
std_deviation: std_dev,
204+
quartiles,
205+
};
206+
207+
let mut profiles_map = LIBFUNC_PROFILES_MAP.lock().unwrap();
208+
209+
match profiles_map.get_mut(&selector) {
210+
Some(profiles) => {
211+
profiles.push(profile_result);
212+
}
213+
None => {
214+
profiles_map.insert(selector, vec![profile_result]);
215+
}
216+
}
195217
}
196218

197219
*libfunc_profiling_trace_id = libfunc_profiling_old_trace_id;

0 commit comments

Comments
 (0)