diff --git a/Cargo.lock b/Cargo.lock index 498b98ce33..40c34be089 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -110,6 +110,15 @@ dependencies = [ "url", ] +[[package]] +name = "aligned-vec" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc890384c8602f339876ded803c97ad529f3842aba97f6392b3dba0dd171769b" +dependencies = [ + "equator", +] + [[package]] name = "allocator-api2" version = "0.2.21" @@ -2147,7 +2156,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "881c5d0a13b2f1498e2306e82cbada78390e152d4b1378fb28a84f4dcd0dc4f3" dependencies = [ "dispatch", - "nix", + "nix 0.30.1", "windows-sys 0.61.2", ] @@ -2895,6 +2904,26 @@ dependencies = [ "serde", ] +[[package]] +name = "equator" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4711b213838dfee0117e3be6ac926007d7f433d7bbe33595975d4190cb07e6fc" +dependencies = [ + "equator-macro", +] + +[[package]] +name = "equator-macro" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44f23cf4b44bfce11a86ace86f8a73ffdec849c9fd00a386a53d278bd9e81fb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.108", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -3355,6 +3384,7 @@ dependencies = [ "ethrex-trie", "ethrex-vm", "hex", + "pprof", "rustc-hash 2.1.1", "secp256k1", "serde_json", @@ -3995,6 +4025,18 @@ dependencies = [ "glob", ] +[[package]] +name = "findshlibs" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40b9e59cd0f7e0806cca4be089683ecb6434e602038df21fe6bf6711b2f07f64" +dependencies = [ + "cc", + "lazy_static", + "libc", + "winapi", +] + [[package]] name = "fixed-hash" version = "0.8.0" @@ -6315,6 +6357,17 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" +[[package]] +name = "nix" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" +dependencies = [ + "bitflags 1.3.2", + "cfg-if 1.0.4", + "libc", +] + [[package]] name = "nix" version = "0.30.1" @@ -7428,6 +7481,29 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "pprof" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38a01da47675efa7673b032bf8efd8214f1917d89685e07e395ab125ea42b187" +dependencies = [ + "aligned-vec", + "backtrace", + "cfg-if 1.0.4", + "findshlibs", + "libc", + "log", + "nix 0.26.4", + "once_cell", + "protobuf 3.7.2", + "protobuf-codegen", + "smallvec", + "spin 0.10.0", + "symbolic-demangle", + "tempfile", + "thiserror 2.0.17", +] + [[package]] name = "pprof_util" version = "0.8.0" @@ -7711,6 +7787,37 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "protobuf-codegen" +version = "3.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d3976825c0014bbd2f3b34f0001876604fe87e0c86cd8fa54251530f1544ace" +dependencies = [ + "anyhow", + "once_cell", + "protobuf 3.7.2", + "protobuf-parse", + "regex", + "tempfile", + "thiserror 1.0.69", +] + +[[package]] +name = "protobuf-parse" +version = "3.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4aeaa1f2460f1d348eeaeed86aea999ce98c1bded6f089ff8514c9d9dbdc973" +dependencies = [ + "anyhow", + "indexmap 2.12.0", + "log", + "protobuf 3.7.2", + "protobuf-support", + "tempfile", + "thiserror 1.0.69", + "which", +] + [[package]] name = "protobuf-support" version = "3.7.2" @@ -10212,6 +10319,15 @@ dependencies = [ "lock_api", ] +[[package]] +name = "spin" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5fe4ccb98d9c292d56fec89a5e07da7fc4cf0dc11e156b41793132775d3e591" +dependencies = [ + "lock_api", +] + [[package]] name = "spinoff" version = "0.8.0" @@ -10383,6 +10499,29 @@ dependencies = [ "zip 0.6.6", ] +[[package]] +name = "symbolic-common" +version = "12.16.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d03f433c9befeea460a01d750e698aa86caf86dcfbd77d552885cd6c89d52f50" +dependencies = [ + "debugid", + "memmap2", + "stable_deref_trait", + "uuid 1.18.1", +] + +[[package]] +name = "symbolic-demangle" +version = "12.16.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13d359ef6192db1760a34321ec4f089245ede4342c27e59be99642f12a859de8" +dependencies = [ + "cpp_demangle", + "rustc-demangle", + "symbolic-common", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/Cargo.toml b/Cargo.toml index 1257ab29e0..f895db7d2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -120,6 +120,8 @@ indexmap = { version = "2.11.4" } rocksdb = { version="0.24.0", default-features = false, features = ["bindgen-runtime"] } +pprof = { version = "0.15", features = ["cpp", "protobuf-codec"] } + [patch.crates-io] secp256k1 = { git = "https://github.com/sp1-patches/rust-secp256k1", tag = "patch-0.30.0-sp1-5.0.0" } diff --git a/crates/blockchain/Cargo.toml b/crates/blockchain/Cargo.toml index 9ade036cd4..0a543bf6ae 100644 --- a/crates/blockchain/Cargo.toml +++ b/crates/blockchain/Cargo.toml @@ -25,6 +25,7 @@ tokio = { workspace = true, features = ["time", "rt"] } tokio-util.workspace = true ethrex-metrics = { path = "./metrics", default-features = false } +pprof.workspace = true [dev-dependencies] serde_json.workspace = true diff --git a/crates/blockchain/blockchain.rs b/crates/blockchain/blockchain.rs index 9a8e182abb..2b2aca60ba 100644 --- a/crates/blockchain/blockchain.rs +++ b/crates/blockchain/blockchain.rs @@ -37,6 +37,7 @@ use ethrex_vm::backends::levm::db::DatabaseLogger; use ethrex_vm::{BlockExecutionResult, DynVmDatabase, Evm, EvmError}; use mempool::Mempool; use payload::PayloadOrTask; +use pprof::protos::Message; use rustc_hash::FxHashMap; use std::collections::hash_map::Entry; use std::collections::{BTreeMap, HashMap}; @@ -833,6 +834,12 @@ impl Blockchain { } pub fn add_block_pipeline(&self, block: Block) -> Result<(), ChainError> { + use std::io::Write; + + let guard = pprof::ProfilerGuardBuilder::default() + .frequency(99997) + .build() + .unwrap(); let (res, account_updates_list, merkle_queue_length, instants) = self.execute_block_pipeline(&block)?; @@ -863,9 +870,16 @@ impl Blockchain { instants, ); } + if let Ok(report) = guard.report().build() { + let mut file = std::fs::File::create("profile.pb").unwrap(); + let profile = report.pprof().unwrap(); + + let mut content = Vec::new(); + profile.write_to_vec(&mut content).unwrap(); + file.write_all(&content).unwrap(); + } result } - #[allow(clippy::too_many_arguments)] fn print_add_block_logs( gas_used: u64,