From 7cafbf7db054eb57f670e8f34b8f68d40aed722f Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 27 Dec 2023 13:14:43 +0100 Subject: [PATCH 1/9] Try example with dhat --- Cargo.lock | 104 +++++++++++++++++ packages/vm/Cargo.toml | 12 ++ packages/vm/examples/memory.rs | 198 +++++++++++++++++++++++++++++++++ 3 files changed, 314 insertions(+) create mode 100644 packages/vm/examples/memory.rs diff --git a/Cargo.lock b/Cargo.lock index 79cf3599a5..c9b1d05ff3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,6 +508,7 @@ dependencies = [ "crc32fast", "criterion", "derivative", + "dhat", "glob", "hex", "hex-literal", @@ -521,6 +522,7 @@ dependencies = [ "target-lexicon", "tempfile", "thiserror", + "time", "wasmer", "wasmer-middlewares", "wat", @@ -808,6 +810,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "deranged" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f32d04922c60427da6f9fef14d042d9edddef64cb9d4ce0d64d0685fbeb1fd3" +dependencies = [ + "powerfmt", +] + [[package]] name = "derivative" version = "2.2.0" @@ -819,6 +830,22 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "dhat" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2aaf837aaf456f6706cb46386ba8dffd4013a757e36f4ea05c20dd46b209a3" +dependencies = [ + "backtrace", + "lazy_static", + "mintex", + "parking_lot", + "rustc-hash", + "serde", + "serde_json", + "thousands", +] + [[package]] name = "difflib" version = "0.4.0" @@ -1415,6 +1442,16 @@ dependencies = [ "adler", ] +[[package]] +name = "mintex" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd7c5ba1c3b5a23418d7bbf98c71c3d4946a0125002129231da8d6b723d559cb" +dependencies = [ + "once_cell", + "sys-info", +] + [[package]] name = "more-asserts" version = "0.2.2" @@ -1483,6 +1520,16 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + [[package]] name = "parking_lot_core" version = "0.9.7" @@ -1546,6 +1593,12 @@ dependencies = [ "plotters-backend", ] +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "ppv-lite86" version = "0.2.17" @@ -1828,6 +1881,12 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustix" version = "0.37.19" @@ -2127,6 +2186,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sys-info" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b3a0d0aba8bf96a0e1ddfdc352fc53b3df7f39318c71854910c3c4b024ae52c" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "tap" version = "1.0.1" @@ -2178,6 +2247,41 @@ dependencies = [ "syn 2.0.28", ] +[[package]] +name = "thousands" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bf63baf9f5039dadc247375c29eb13706706cfde997d0330d05aa63a77d8820" + +[[package]] +name = "time" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a34ab300f2dee6e562c10a046fc05e358b29f9bf92277f30c3c8d82275f6f5" +dependencies = [ + "deranged", + "itoa", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" + +[[package]] +name = "time-macros" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ad70d68dba9e1f8aceda7aa6711965dfec1cac869f311a51bd08b3a2ccbce20" +dependencies = [ + "time-core", +] + [[package]] name = "tinytemplate" version = "1.2.1" diff --git a/packages/vm/Cargo.toml b/packages/vm/Cargo.toml index 9a5a9794f8..8eafec21d7 100644 --- a/packages/vm/Cargo.toml +++ b/packages/vm/Cargo.toml @@ -20,6 +20,8 @@ staking = ["cosmwasm-std/staking"] stargate = ["cosmwasm-std/stargate"] # Use cranelift backend instead of singlepass. This is required for development on Windows. cranelift = ["wasmer/cranelift"] +dhat-heap = [] # if you are doing heap profiling +dhat-ad-hoc = [] # if you are doing ad hoc profiling [lib] # See https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options @@ -65,7 +67,17 @@ clap = "4" rand = "0.8" leb128 = "0.2" target-lexicon = "0.12" +dhat = "0.3.2" +time = { version = "0.3.28", features = ["formatting"] } [[bench]] name = "main" harness = false + +[[example]] +name = "memory" +path = "examples/memory.rs" +required-features = [] + +[profile.release] +debug = 1 diff --git a/packages/vm/examples/memory.rs b/packages/vm/examples/memory.rs new file mode 100644 index 0000000000..63d0a5d72f --- /dev/null +++ b/packages/vm/examples/memory.rs @@ -0,0 +1,198 @@ +use std::time::SystemTime; +use tempfile::TempDir; +use time::{format_description::well_known::Rfc3339, OffsetDateTime}; + +use cosmwasm_std::{coins, Checksum, Empty}; +use cosmwasm_vm::testing::{mock_backend, mock_env, mock_info, MockApi, MockQuerier, MockStorage}; +use cosmwasm_vm::{ + call_execute, call_instantiate, capabilities_from_csv, Cache, CacheOptions, InstanceOptions, + Size, +}; + +#[cfg(feature = "dhat-heap")] +#[global_allocator] +static ALLOC: dhat::Alloc = dhat::Alloc; + +// Instance +const DEFAULT_MEMORY_LIMIT: Size = Size::mebi(128); +const DEFAULT_GAS_LIMIT: u64 = u64::MAX; +const DEFAULT_INSTANCE_OPTIONS: InstanceOptions = InstanceOptions { + gas_limit: DEFAULT_GAS_LIMIT, +}; +// Cache +const MEMORY_CACHE_SIZE: Size = Size::mebi(200); + +// static HACKATHON: &[u8] = ; +// static CYBERPUNK: &[u8] = include_bytes!("../testdata/cyberpunk.wasm"); + +struct Execute { + pub msg: &'static [u8], + pub expect_error: bool, +} + +struct Contract { + pub wasm: &'static [u8], + pub instantiate_msg: &'static [u8], + pub execute_msgs: [Option; 3], +} + +const CONTRACTS: [Contract; 2] = [ + Contract { + wasm: include_bytes!("../testdata/cyberpunk.wasm"), + instantiate_msg: b"{}", + execute_msgs: [ + Some(Execute { + msg: br#"{"unreachable":{}}"#, + expect_error: true, + }), + Some(Execute { + msg: br#"{"allocate_large_memory":{"pages":1000}}"#, + expect_error: false, + }), + Some(Execute { + // mem_cost in KiB + msg: br#"{"argon2":{"mem_cost":50000,"time_cost":1}}"#, + expect_error: false, + }), + ], + }, + Contract { + wasm: include_bytes!("../testdata/hackatom.wasm"), + instantiate_msg: br#"{"verifier": "verifies", "beneficiary": "benefits"}"#, + execute_msgs: [ + Some(Execute { + msg: br#"{"release":{}}"#, + expect_error: false, + }), + None, + None, + ], + }, +]; + +const END_AFTER: u64 = 10 * 60; // seconds +const ROUNDS: usize = 1024; +const ROUND_LEN: usize = 16; + +#[allow(clippy::collapsible_else_if)] +fn app() { + let start_time = SystemTime::now(); + + let options = CacheOptions::new( + TempDir::new().unwrap().into_path(), + capabilities_from_csv("iterator,staking"), + MEMORY_CACHE_SIZE, + DEFAULT_MEMORY_LIMIT, + ); + + let checksums = { + let cache: Cache = + unsafe { Cache::new(options.clone()).unwrap() }; + + let mut checksums = Vec::::new(); + for contract in CONTRACTS { + checksums.push(cache.save_wasm(contract.wasm).unwrap()); + } + checksums + }; + + let cache: Cache = + unsafe { Cache::new(options.clone()).unwrap() }; + for round in 0..ROUNDS { + for _ in 0..ROUND_LEN { + if SystemTime::now() + .duration_since(start_time) + .unwrap() + .as_secs() + > END_AFTER + { + eprintln!("End time reached. Ending the process"); + return; // ends app() + } + + for idx in 0..=1 { + let mut instance = cache + .get_instance(&checksums[idx], mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS) + .unwrap(); + + instance.set_debug_handler(|msg, info| { + let t = now_rfc3339(); + let gas = info.gas_remaining; + eprintln!("[{t}]: {msg} (gas remaining: {gas})"); + }); + + let info = mock_info("creator", &coins(1000, "earth")); + let msg = CONTRACTS[idx].instantiate_msg; + let contract_result = + call_instantiate::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg) + .unwrap(); + assert!(contract_result.into_result().is_ok()); + + for (execution_idx, e) in CONTRACTS[idx].execute_msgs.iter().enumerate() { + let Some(execute) = e else { + continue; + }; + let info = mock_info("verifies", &coins(15, "earth")); + let msg = execute.msg; + let res = + call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg); + + if execute.expect_error { + if res.is_ok() { + panic!( + "Round {round}, Execution {execution_idx}, Contract {idx}. Expected error but got {res:?}" + ); + } + } else { + if res.is_err() { + panic!("Round {round}, Execution {execution_idx}, Contract {idx}. Expected no error but got {res:?}"); + } + } + } + } + + /* + let mut instance = cache + .get_instance(&checksums[1], mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS) + .unwrap(); + // println!("Done instantiating contract {i}"); + + instance.set_debug_handler(|msg, info| { + let t = now_rfc3339(); + let gas = info.gas_remaining; + eprintln!("[{t}]: {msg} (gas remaining: {gas})"); + }); + + let info = mock_info("creator", &coins(1000, "earth")); + let msg = br#"{"verifier": "verifies", "beneficiary": "benefits"}"#; + let contract_result = + call_instantiate::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap(); + assert!(contract_result.into_result().is_ok()); + + let info = mock_info("verifies", &coins(15, "earth")); + let msg = br#"{"release":{}}"#; + let contract_result = + call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap(); + assert!(contract_result.into_result().is_ok()); + */ + } + + // let stats = cache.stats(); + // // eprintln!("Stats: {stats:?}"); + // assert_eq!(stats.misses, 0); + // assert_eq!(stats.hits_fs_cache, 2); + // assert_eq!(stats.hits_memory_cache as usize, 2 * (ROUND_LEN - 1)); + } +} + +fn now_rfc3339() -> String { + let dt = OffsetDateTime::from(SystemTime::now()); + dt.format(&Rfc3339).unwrap_or_default() +} + +pub fn main() { + #[cfg(feature = "dhat-heap")] + let _profiler = dhat::Profiler::new_heap(); + + app(); +} From abf0cd41009ca5e9e02b413df531ea48d7549415 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 18 Nov 2023 10:48:46 +0100 Subject: [PATCH 2/9] Improve testing --- packages/vm/examples/memory.rs | 137 +++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 59 deletions(-) diff --git a/packages/vm/examples/memory.rs b/packages/vm/examples/memory.rs index 63d0a5d72f..f5d1fd0d99 100644 --- a/packages/vm/examples/memory.rs +++ b/packages/vm/examples/memory.rs @@ -13,17 +13,18 @@ use cosmwasm_vm::{ #[global_allocator] static ALLOC: dhat::Alloc = dhat::Alloc; +const END_AFTER: u64 = 2 * 60; // seconds +const ROUNDS: usize = 1024; +const ROUND_LEN: usize = 16; + // Instance -const DEFAULT_MEMORY_LIMIT: Size = Size::mebi(128); +const DEFAULT_MEMORY_LIMIT: Size = Size::mebi(64); const DEFAULT_GAS_LIMIT: u64 = u64::MAX; const DEFAULT_INSTANCE_OPTIONS: InstanceOptions = InstanceOptions { gas_limit: DEFAULT_GAS_LIMIT, }; // Cache -const MEMORY_CACHE_SIZE: Size = Size::mebi(200); - -// static HACKATHON: &[u8] = ; -// static CYBERPUNK: &[u8] = include_bytes!("../testdata/cyberpunk.wasm"); +const MEMORY_CACHE_SIZE: Size = Size::mebi(5); struct Execute { pub msg: &'static [u8], @@ -32,47 +33,58 @@ struct Execute { struct Contract { pub wasm: &'static [u8], - pub instantiate_msg: &'static [u8], - pub execute_msgs: [Option; 3], + pub instantiate_msg: Option<&'static [u8]>, + pub execute_msgs: Vec, } -const CONTRACTS: [Contract; 2] = [ - Contract { - wasm: include_bytes!("../testdata/cyberpunk.wasm"), - instantiate_msg: b"{}", - execute_msgs: [ - Some(Execute { - msg: br#"{"unreachable":{}}"#, - expect_error: true, - }), - Some(Execute { - msg: br#"{"allocate_large_memory":{"pages":1000}}"#, - expect_error: false, - }), - Some(Execute { - // mem_cost in KiB - msg: br#"{"argon2":{"mem_cost":50000,"time_cost":1}}"#, +fn contracts() -> Vec { + vec![ + Contract { + wasm: include_bytes!("../testdata/cyberpunk.wasm"), + instantiate_msg: Some(b"{}"), + execute_msgs: vec![ + Execute { + msg: br#"{"unreachable":{}}"#, + expect_error: true, + }, + Execute { + msg: br#"{"allocate_large_memory":{"pages":1000}}"#, + expect_error: false, + }, + Execute { + // mem_cost in KiB + msg: br#"{"argon2":{"mem_cost":256,"time_cost":1}}"#, + expect_error: false, + }, + Execute { + msg: br#"{"memory_loop":{}}"#, + expect_error: true, + }, + ], + }, + Contract { + wasm: include_bytes!("../testdata/hackatom.wasm"), + instantiate_msg: Some(br#"{"verifier": "verifies", "beneficiary": "benefits"}"#), + execute_msgs: vec![Execute { + msg: br#"{"release":{}}"#, expect_error: false, - }), - ], - }, - Contract { - wasm: include_bytes!("../testdata/hackatom.wasm"), - instantiate_msg: br#"{"verifier": "verifies", "beneficiary": "benefits"}"#, - execute_msgs: [ - Some(Execute { + }], + }, + Contract { + wasm: include_bytes!("../testdata/hackatom_1.0.wasm"), + instantiate_msg: Some(br#"{"verifier": "verifies", "beneficiary": "benefits"}"#), + execute_msgs: vec![Execute { msg: br#"{"release":{}}"#, expect_error: false, - }), - None, - None, - ], - }, -]; - -const END_AFTER: u64 = 10 * 60; // seconds -const ROUNDS: usize = 1024; -const ROUND_LEN: usize = 16; + }], + }, + Contract { + wasm: include_bytes!("../testdata/ibc_reflect.wasm"), + instantiate_msg: None, + execute_msgs: vec![], + }, + ] +} #[allow(clippy::collapsible_else_if)] fn app() { @@ -80,22 +92,27 @@ fn app() { let options = CacheOptions::new( TempDir::new().unwrap().into_path(), - capabilities_from_csv("iterator,staking"), + capabilities_from_csv("iterator,staking,stargate"), MEMORY_CACHE_SIZE, DEFAULT_MEMORY_LIMIT, ); + let contracts = contracts(); + let checksums = { let cache: Cache = unsafe { Cache::new(options.clone()).unwrap() }; let mut checksums = Vec::::new(); - for contract in CONTRACTS { + for contract in &contracts { checksums.push(cache.save_wasm(contract.wasm).unwrap()); } checksums }; + let after = SystemTime::now().duration_since(start_time).unwrap(); + eprintln!("Done compiling after {after:?}"); + let cache: Cache = unsafe { Cache::new(options.clone()).unwrap() }; for round in 0..ROUNDS { @@ -106,32 +123,34 @@ fn app() { .as_secs() > END_AFTER { - eprintln!("End time reached. Ending the process"); + eprintln!("Round {round}. End time reached. Ending the process"); + + let metrics = cache.metrics(); + eprintln!("Cache metrics: {metrics:?}"); + return; // ends app() } - for idx in 0..=1 { + for idx in 0..contracts.len() { let mut instance = cache .get_instance(&checksums[idx], mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS) .unwrap(); - instance.set_debug_handler(|msg, info| { - let t = now_rfc3339(); - let gas = info.gas_remaining; - eprintln!("[{t}]: {msg} (gas remaining: {gas})"); + instance.set_debug_handler(|_msg, info| { + let _t = now_rfc3339(); + let _gas = info.gas_remaining; + //eprintln!("[{t}]: {msg} (gas remaining: {gas})"); }); - let info = mock_info("creator", &coins(1000, "earth")); - let msg = CONTRACTS[idx].instantiate_msg; - let contract_result = - call_instantiate::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg) - .unwrap(); - assert!(contract_result.into_result().is_ok()); + if let Some(msg) = contracts[idx].instantiate_msg { + let info = mock_info("creator", &coins(1000, "earth")); + let contract_result = + call_instantiate::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg) + .unwrap(); + assert!(contract_result.into_result().is_ok()); + } - for (execution_idx, e) in CONTRACTS[idx].execute_msgs.iter().enumerate() { - let Some(execute) = e else { - continue; - }; + for (execution_idx, execute) in contracts[idx].execute_msgs.iter().enumerate() { let info = mock_info("verifies", &coins(15, "earth")); let msg = execute.msg; let res = From 23535946f49783e6283fe72d886df78a4e4c193e Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 27 Dec 2023 13:19:12 +0100 Subject: [PATCH 3/9] Add some docs --- packages/vm/examples/memory.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/vm/examples/memory.rs b/packages/vm/examples/memory.rs index f5d1fd0d99..2e36e34561 100644 --- a/packages/vm/examples/memory.rs +++ b/packages/vm/examples/memory.rs @@ -1,3 +1,6 @@ +// Run with +// cargo run --features dhat-heap --example memory --release + use std::time::SystemTime; use tempfile::TempDir; use time::{format_description::well_known::Rfc3339, OffsetDateTime}; @@ -13,6 +16,7 @@ use cosmwasm_vm::{ #[global_allocator] static ALLOC: dhat::Alloc = dhat::Alloc; +/// Number of seconds after which the test stops const END_AFTER: u64 = 2 * 60; // seconds const ROUNDS: usize = 1024; const ROUND_LEN: usize = 16; From e621bb12821071176ae5992ee18eae8b4aaed519 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 9 Jan 2024 16:57:42 +0100 Subject: [PATCH 4/9] Ignore dhat-heap.json --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6f20a8ecdb..cbe15aa263 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ artifacts/ # Log files (e.g. memory profiling) *.log +dhat-heap.json From 80a2a43c8428014f62818728fd1ce83a872bf608 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 9 Jan 2024 17:03:53 +0100 Subject: [PATCH 5/9] Adpt example to API changes --- packages/vm/examples/memory.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/vm/examples/memory.rs b/packages/vm/examples/memory.rs index 2e36e34561..a0d066b361 100644 --- a/packages/vm/examples/memory.rs +++ b/packages/vm/examples/memory.rs @@ -37,15 +37,18 @@ struct Execute { struct Contract { pub wasm: &'static [u8], - pub instantiate_msg: Option<&'static [u8]>, + pub instantiate_msg: Option>, pub execute_msgs: Vec, } fn contracts() -> Vec { + let api = MockApi::default(); + let verifier = api.addr_make("verifies"); + let beneficiary = api.addr_make("benefits"); vec![ Contract { wasm: include_bytes!("../testdata/cyberpunk.wasm"), - instantiate_msg: Some(b"{}"), + instantiate_msg: Some(b"{}".to_vec()), execute_msgs: vec![ Execute { msg: br#"{"unreachable":{}}"#, @@ -68,7 +71,10 @@ fn contracts() -> Vec { }, Contract { wasm: include_bytes!("../testdata/hackatom.wasm"), - instantiate_msg: Some(br#"{"verifier": "verifies", "beneficiary": "benefits"}"#), + instantiate_msg: Some( + format!(r#"{{"verifier": "{verifier}", "beneficiary": "{beneficiary}"}}"#) + .into_bytes(), + ), execute_msgs: vec![Execute { msg: br#"{"release":{}}"#, expect_error: false, @@ -76,7 +82,10 @@ fn contracts() -> Vec { }, Contract { wasm: include_bytes!("../testdata/hackatom_1.0.wasm"), - instantiate_msg: Some(br#"{"verifier": "verifies", "beneficiary": "benefits"}"#), + instantiate_msg: Some( + format!(r#"{{"verifier": "{verifier}", "beneficiary": "{beneficiary}"}}"#) + .into_bytes(), + ), execute_msgs: vec![Execute { msg: br#"{"release":{}}"#, expect_error: false, @@ -146,7 +155,7 @@ fn app() { //eprintln!("[{t}]: {msg} (gas remaining: {gas})"); }); - if let Some(msg) = contracts[idx].instantiate_msg { + if let Some(msg) = &contracts[idx].instantiate_msg { let info = mock_info("creator", &coins(1000, "earth")); let contract_result = call_instantiate::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg) From f20d155cf169f700363cbb5578b61c740ca8ae55 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 9 Jan 2024 17:06:23 +0100 Subject: [PATCH 6/9] Remove unused dhat-ad-hoc --- packages/vm/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vm/Cargo.toml b/packages/vm/Cargo.toml index 8eafec21d7..b5cc005ee0 100644 --- a/packages/vm/Cargo.toml +++ b/packages/vm/Cargo.toml @@ -20,8 +20,8 @@ staking = ["cosmwasm-std/staking"] stargate = ["cosmwasm-std/stargate"] # Use cranelift backend instead of singlepass. This is required for development on Windows. cranelift = ["wasmer/cranelift"] -dhat-heap = [] # if you are doing heap profiling -dhat-ad-hoc = [] # if you are doing ad hoc profiling +# For heap profiling. Used in examples. +dhat-heap = [] [lib] # See https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options From fceaad5db8d6bcc93a28c0816aaa1610194052be Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Thu, 11 Jan 2024 13:37:33 +0100 Subject: [PATCH 7/9] Cleanup --- packages/vm/examples/memory.rs | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-) diff --git a/packages/vm/examples/memory.rs b/packages/vm/examples/memory.rs index a0d066b361..0eb1ce921b 100644 --- a/packages/vm/examples/memory.rs +++ b/packages/vm/examples/memory.rs @@ -126,8 +126,7 @@ fn app() { let after = SystemTime::now().duration_since(start_time).unwrap(); eprintln!("Done compiling after {after:?}"); - let cache: Cache = - unsafe { Cache::new(options.clone()).unwrap() }; + let cache: Cache = unsafe { Cache::new(options).unwrap() }; for round in 0..ROUNDS { for _ in 0..ROUND_LEN { if SystemTime::now() @@ -182,38 +181,7 @@ fn app() { } } } - - /* - let mut instance = cache - .get_instance(&checksums[1], mock_backend(&[]), DEFAULT_INSTANCE_OPTIONS) - .unwrap(); - // println!("Done instantiating contract {i}"); - - instance.set_debug_handler(|msg, info| { - let t = now_rfc3339(); - let gas = info.gas_remaining; - eprintln!("[{t}]: {msg} (gas remaining: {gas})"); - }); - - let info = mock_info("creator", &coins(1000, "earth")); - let msg = br#"{"verifier": "verifies", "beneficiary": "benefits"}"#; - let contract_result = - call_instantiate::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap(); - assert!(contract_result.into_result().is_ok()); - - let info = mock_info("verifies", &coins(15, "earth")); - let msg = br#"{"release":{}}"#; - let contract_result = - call_execute::<_, _, _, Empty>(&mut instance, &mock_env(), &info, msg).unwrap(); - assert!(contract_result.into_result().is_ok()); - */ } - - // let stats = cache.stats(); - // // eprintln!("Stats: {stats:?}"); - // assert_eq!(stats.misses, 0); - // assert_eq!(stats.hits_fs_cache, 2); - // assert_eq!(stats.hits_memory_cache as usize, 2 * (ROUND_LEN - 1)); } } From 1e4b8627430f7cd94543782a79a570beab385af5 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Thu, 11 Jan 2024 15:12:20 +0100 Subject: [PATCH 8/9] Only enable dhat dependency if feature is enabled --- packages/vm/Cargo.toml | 10 ++++++---- packages/vm/examples/memory.rs | 1 - 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/vm/Cargo.toml b/packages/vm/Cargo.toml index b5cc005ee0..ea65ad1a06 100644 --- a/packages/vm/Cargo.toml +++ b/packages/vm/Cargo.toml @@ -20,8 +20,8 @@ staking = ["cosmwasm-std/staking"] stargate = ["cosmwasm-std/stargate"] # Use cranelift backend instead of singlepass. This is required for development on Windows. cranelift = ["wasmer/cranelift"] -# For heap profiling. Used in examples. -dhat-heap = [] +# For heap profiling. Only used in "memory" example. +dhat-heap = ["dep:dhat"] [lib] # See https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options @@ -45,6 +45,9 @@ thiserror = "1.0.26" wasmer = { version = "=4.2.5", default-features = false, features = ["cranelift", "singlepass"] } wasmer-middlewares = "=4.2.5" strum = { version = "0.25.0", default-features = false, features = ["derive"] } +# For heap profiling. Only used in "memory" example. This has to be a non-dev dependency +# because cargo currently does not support optional dev-dependencies. +dhat = { version = "0.3.2", optional = true } # Dependencies that we do not use ourself. We add those entries # to bump the min version of them. @@ -67,7 +70,6 @@ clap = "4" rand = "0.8" leb128 = "0.2" target-lexicon = "0.12" -dhat = "0.3.2" time = { version = "0.3.28", features = ["formatting"] } [[bench]] @@ -77,7 +79,7 @@ harness = false [[example]] name = "memory" path = "examples/memory.rs" -required-features = [] +required-features = ["dhat-heap"] [profile.release] debug = 1 diff --git a/packages/vm/examples/memory.rs b/packages/vm/examples/memory.rs index 0eb1ce921b..9c917dc883 100644 --- a/packages/vm/examples/memory.rs +++ b/packages/vm/examples/memory.rs @@ -12,7 +12,6 @@ use cosmwasm_vm::{ Size, }; -#[cfg(feature = "dhat-heap")] #[global_allocator] static ALLOC: dhat::Alloc = dhat::Alloc; From a01f9c14177c0bd93372b6ddec366b4001f41f44 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Tue, 16 Jan 2024 11:31:03 +0100 Subject: [PATCH 9/9] Allow running memory example without dhat --- packages/vm/Cargo.toml | 1 - packages/vm/examples/memory.rs | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vm/Cargo.toml b/packages/vm/Cargo.toml index ea65ad1a06..3c7256f0e7 100644 --- a/packages/vm/Cargo.toml +++ b/packages/vm/Cargo.toml @@ -79,7 +79,6 @@ harness = false [[example]] name = "memory" path = "examples/memory.rs" -required-features = ["dhat-heap"] [profile.release] debug = 1 diff --git a/packages/vm/examples/memory.rs b/packages/vm/examples/memory.rs index 9c917dc883..0eb1ce921b 100644 --- a/packages/vm/examples/memory.rs +++ b/packages/vm/examples/memory.rs @@ -12,6 +12,7 @@ use cosmwasm_vm::{ Size, }; +#[cfg(feature = "dhat-heap")] #[global_allocator] static ALLOC: dhat::Alloc = dhat::Alloc;