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

memory stats #61

Draft
wants to merge 3 commits into
base: main
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ target/
.envrc

compiled_programs/
traces/
memory.json
*.csv
*.png
*.svg
21 changes: 17 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ cairo-native = { git = "https://github.com/lambdaclass/cairo_native" }
tracing = "0.1"

[patch.'https://github.com/lambdaclass/cairo_native']
cairo-native = { git = 'https://github.com/lambdaclass//cairo_native.git', rev = "6fb1fecc71c6c604943717d6a72f4810090017a6" }
cairo-native = { git = 'https://github.com/lambdaclass//cairo_native.git', branch = "main"}
38 changes: 38 additions & 0 deletions memory.gnuplot
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
set encoding utf8
set style line 1 lc rgb '#E41A1C' pt 1 ps 1 lt 1 lw 2 # red
set style line 2 lc rgb '#377EB8' pt 6 ps 1 lt 1 lw 2 # blue
set style line 3 lc rgb '#4DAF4A' pt 2 ps 1 lt 1 lw 2 # green
set style line 4 lc rgb '#984EA3' pt 3 ps 1 lt 1 lw 2 # purple
set style line 5 lc rgb '#FF7F00' pt 4 ps 1 lt 1 lw 2 # orange
set style line 6 lc rgb '#FFFF33' pt 5 ps 1 lt 1 lw 2 # yellow
set style line 7 lc rgb '#A65628' pt 7 ps 1 lt 1 lw 2 # brown
set style line 8 lc rgb '#F781BF' pt 8 ps 1 lt 1 lw 2 # pink
# Palette
set palette maxcolors 8
set palette defined ( 0 '#E41A1C', 1 '#377EB8', 2 '#4DAF4A', 3 '#984EA3',\
4 '#FF7F00', 5 '#FFFF33', 6 '#A65628', 7 '#F781BF' )

# Standard border
set style line 11 lc rgb '#808080' lt 1 lw 3
set border 0 back ls 11
set tics out nomirror

# Standard grid
set style line 12 lc rgb '#808080' lt 0 lw 1
set grid back ls 12
unset grid

set title 'Memory per tx'
set key autotitle columnhead
set ylabel 'Memory (MB)'
set xlabel 'Tx Count'
set grid
set grid xtics
set grid ytics
set grid mxtics
set grid mytics
set yrange [0:]
set term svg size 1024, 768 dynamic rounded
set output 'out.svg'
set datafile separator ','
plot 'memory.csv' using 0:1 with lines, '' using 0:4 with lines
2 changes: 2 additions & 0 deletions plot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cat memory.json | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' > memory.csv
gnuplot -p memory.gnuplot
3 changes: 2 additions & 1 deletion rpc-state-reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ cairo-lang-sierra = "2.8.2"
cairo-lang-starknet-classes = "2.8.2"
cairo-lang-utils = "2.8.2"
cairo-native = { workspace = true }
starknet = "0.7.0"
starknet = "0.7.0"
thiserror = { workspace = true }
flate2 = "1.0.25"
dotenv = "0.15.0"
cairo-vm = "1.0.0-rc5"
blockifier = { workspace = true }
tracing = { workspace = true }
memory-stats = "1.2.0"

[dev-dependencies]
pretty_assertions_sorted = "1.2.3"
Expand Down
38 changes: 37 additions & 1 deletion rpc-state-reader/src/blockifier_state_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use blockifier::{
};

use cairo_vm::types::program::Program;
use serde::{Deserialize, Serialize};
use starknet::core::types::ContractClass as SNContractClass;
use starknet_api::{
block::BlockNumber,
Expand All @@ -31,7 +32,7 @@ use starknet_api::{
state::StorageKey,
transaction::{Transaction as SNTransaction, TransactionHash},
};
use std::sync::Arc;
use std::sync::{atomic::AtomicU64, Arc};

use crate::{
rpc_state::{RpcBlockInfo, RpcChain, RpcState, RpcTransactionReceipt, TransactionTrace},
Expand Down Expand Up @@ -270,6 +271,16 @@ fn calculate_class_info_for_testing(contract_class: ContractClass) -> ClassInfo
ClassInfo::new(&contract_class, sierra_program_length, 100).unwrap()
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
struct MemoryData {
physical: usize,
r#virtual: usize,
tx_hash: TransactionHash,
tx_count: u64,
}

static TX_COUNT: AtomicU64 = AtomicU64::new(1);

pub fn execute_tx_configurable_with_state(
tx_hash: &TransactionHash,
tx: SNTransaction,
Expand All @@ -278,6 +289,31 @@ pub fn execute_tx_configurable_with_state(
_skip_nonce_check: bool,
state: &mut CachedState<RpcStateReader>,
) -> TransactionExecutionResult<TransactionExecutionInfo> {
if let Some(usage) = memory_stats::memory_stats() {
tracing::info!(
"Current physical memory usage: {}",
usage.physical_mem / 1024 / 1024
);
tracing::info!(
"Current virtual memory usage: {}",
usage.virtual_mem / 1024 / 1024
);
let data = if let Ok(data) = std::fs::read_to_string("memory.json") {
data
} else {
"[]".to_string()
};

let mut data: Vec<MemoryData> = serde_json::from_str(&data).unwrap();
data.push(MemoryData {
physical: usage.physical_mem / 1024 / 1024,
r#virtual: usage.virtual_mem / 1024 / 1024,
tx_hash: *tx_hash,
tx_count: TX_COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
});
std::fs::write("memory.json", serde_json::to_string_pretty(&data).unwrap()).unwrap();
}

let fee_token_address = FeeTokenAddresses {
strk_fee_token_address: ContractAddress(
felt!("0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d")
Expand Down
Loading