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

feat(blockifier): add cairo native cache rate metric #4393

Merged
merged 1 commit into from
Mar 2, 2025
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Monitoring/sequencer/dev_grafana.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
"type": "stat",
"expr": "batcher_batched_transactions",
"extra_params": {}
},
{
"title": "cairo_native_cache_miss_ratio",
"description": "The ratio of cache misses in the Cairo native cache",
"type": "graph",
"expr": "100 * (class_cache_misses / clamp_min((class_cache_misses + class_cache_hits), 1))",
"extra_params": {}
}
],
"Http Server": [
Expand Down
9 changes: 9 additions & 0 deletions crates/blockifier/src/state/contract_class_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ pub mod trivial_class_manager {
pub fn get_cache_size(&self) -> usize {
self.cache.lock().cache_size()
}

// TODO(Aviv): consider support cache miss metrics.
pub fn take_cache_miss_counter(&self) -> u64 {
0
}

pub fn take_cache_hit_counter(&self) -> u64 {
0
}
}
}

Expand Down
55 changes: 53 additions & 2 deletions crates/blockifier/src/state/native_class_manager.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc::{sync_channel, Receiver, SyncSender, TrySendError};
use std::sync::Arc;

Expand Down Expand Up @@ -49,6 +50,28 @@ pub struct NativeClassManager {
sender: Option<SyncSender<CompilationRequest>>,
/// The sierra-to-native compiler.
compiler: Option<Arc<dyn SierraToNativeCompiler>>,
/// cache_miss_rate
cache_metrics: Arc<CacheMetrics>,
}

#[derive(Default)]
pub struct CacheMetrics {
pub cache_misses: AtomicU64,
pub cache_hits: AtomicU64,
}

impl CacheMetrics {
pub fn new() -> Self {
Self { cache_misses: AtomicU64::new(0), cache_hits: AtomicU64::new(0) }
}

pub fn record_miss(&self) {
self.cache_misses.fetch_add(1, Ordering::Relaxed);
}

pub fn record_hit(&self) {
self.cache_hits.fetch_add(1, Ordering::Relaxed);
}
}

impl NativeClassManager {
Expand All @@ -70,6 +93,7 @@ impl NativeClassManager {
cache,
sender: None,
compiler: None,
cache_metrics: Arc::new(CacheMetrics::new()),
};
}

Expand All @@ -82,6 +106,7 @@ impl NativeClassManager {
cache,
sender: None,
compiler: Some(compiler),
cache_metrics: Arc::new(CacheMetrics::new()),
};
}

Expand All @@ -92,12 +117,28 @@ impl NativeClassManager {
move || run_compilation_worker(cache, receiver, compiler)
});

NativeClassManager { cairo_native_run_config, cache, sender: Some(sender), compiler: None }
// TODO(AVIV): Add private constructor with default values.
NativeClassManager {
cairo_native_run_config,
cache,
sender: Some(sender),
compiler: None,
cache_metrics: Arc::new(CacheMetrics::new()),
}
}

/// Returns the runnable compiled class for the given class hash, if it exists in cache.
pub fn get_runnable(&self, class_hash: &ClassHash) -> Option<RunnableCompiledClass> {
let cached_class = self.cache.get(class_hash)?;
let cached_class = match self.cache.get(class_hash) {
Some(class) => {
self.cache_metrics.record_hit();
class
}
None => {
self.cache_metrics.record_miss();
return None;
}
};
if let CachedClass::V1(_, _) = cached_class {
// TODO(Yoni): make sure `wait_on_native_compilation` cannot be set to true while
// `run_cairo_native` is false.
Expand Down Expand Up @@ -195,6 +236,16 @@ impl NativeClassManager {
pub fn get_cache_size(&self) -> usize {
self.cache.lock().cache_size()
}

/// Retrieves the current cache miss counter and resets it to zero.
pub fn take_cache_miss_counter(&self) -> u64 {
self.cache_metrics.cache_misses.swap(0, Ordering::Relaxed)
}

/// Retrieves the current cache hit counter and resets it to zero.
pub fn take_cache_hit_counter(&self) -> u64 {
self.cache_metrics.cache_hits.swap(0, Ordering::Relaxed)
}
}

/// Handles compilation requests from the channel, holding the receiver end of the channel.
Expand Down
2 changes: 2 additions & 0 deletions crates/blockifier/src/state/native_class_manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::state::global_cache::{
};
use crate::state::native_class_manager::{
process_compilation_request,
CacheMetrics,
CompilationRequest,
ContractClassManagerError,
NativeClassManager,
Expand Down Expand Up @@ -157,6 +158,7 @@ fn test_send_compilation_request_channel_disconnected() {
cache: RawClassCache::new(GLOBAL_CONTRACT_CACHE_SIZE_FOR_TEST),
sender: Some(sender),
compiler: None,
cache_metrics: Arc::new(CacheMetrics::new()),
};
// Disconnect the channel by dropping the receiver.
drop(receiver);
Expand Down
4 changes: 4 additions & 0 deletions crates/starknet_batcher/src/batcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ use crate::metrics::{
register_metrics,
ProposalMetricsHandle,
BATCHED_TRANSACTIONS,
CLASS_CACHE_HITS,
CLASS_CACHE_MISSES,
REJECTED_TRANSACTIONS,
REVERTED_BLOCKS,
STORAGE_HEIGHT,
Expand Down Expand Up @@ -496,6 +498,8 @@ impl Batcher {
let execution_infos: Vec<_> =
block_execution_artifacts.execution_infos.into_iter().map(|(_, info)| info).collect();

CLASS_CACHE_MISSES.increment(self.block_builder_factory.take_class_cache_miss_counter());
CLASS_CACHE_HITS.increment(self.block_builder_factory.take_class_cache_hit_counter());
BATCHED_TRANSACTIONS.increment(n_txs);
REJECTED_TRANSACTIONS.increment(n_rejected_txs);

Expand Down
6 changes: 5 additions & 1 deletion crates/starknet_batcher/src/batcher_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,16 @@ impl Default for MockDependencies {
let expected_gas_price =
propose_block_input(PROPOSAL_ID).block_info.gas_prices.strk_gas_prices.l2_gas_price;
mempool_client.expect_update_gas_price().with(eq(expected_gas_price)).returning(|_| Ok(()));
let mut block_builder_factory = MockBlockBuilderFactoryTrait::new();
block_builder_factory.expect_take_class_cache_miss_counter().return_const(0_u64);
block_builder_factory.expect_take_class_cache_hit_counter().return_const(0_u64);

Self {
storage_reader,
storage_writer: MockBatcherStorageWriterTrait::new(),
l1_provider_client: MockL1ProviderClient::new(),
mempool_client,
block_builder_factory: MockBlockBuilderFactoryTrait::new(),
block_builder_factory,
// TODO(noamsp): use MockClassManagerClient
class_manager_client: Arc::new(EmptyClassManagerClient),
}
Expand Down
12 changes: 12 additions & 0 deletions crates/starknet_batcher/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ pub trait BlockBuilderFactoryTrait: Send + Sync {
>,
runtime: tokio::runtime::Handle,
) -> BlockBuilderResult<(Box<dyn BlockBuilderTrait>, AbortSignalSender)>;

fn take_class_cache_miss_counter(&self) -> u64;

fn take_class_cache_hit_counter(&self) -> u64;
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
Expand Down Expand Up @@ -466,6 +470,14 @@ impl BlockBuilderFactoryTrait for BlockBuilderFactory {
));
Ok((block_builder, abort_signal_sender))
}

fn take_class_cache_hit_counter(&self) -> u64 {
self.contract_class_manager.take_cache_hit_counter()
}

fn take_class_cache_miss_counter(&self) -> u64 {
self.contract_class_manager.take_cache_miss_counter()
}
}

/// Supplementary information for use by downstream services.
Expand Down
4 changes: 4 additions & 0 deletions crates/starknet_batcher/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ define_metrics!(
MetricGauge { STORAGE_HEIGHT, "batcher_storage_height", "The height of the batcher's storage" },
// Counters
MetricCounter { PROPOSAL_STARTED, "batcher_proposal_started", "Counter of proposals started", init = 0 },
MetricCounter { CLASS_CACHE_MISSES, "class_cache_misses", "Counter of global class cache misses", init=0 },
MetricCounter { CLASS_CACHE_HITS, "class_cache_hits", "Counter of global class cache hits", init=0 },
MetricCounter { PROPOSAL_SUCCEEDED, "batcher_proposal_succeeded", "Counter of successful proposals", init = 0 },
MetricCounter { PROPOSAL_FAILED, "batcher_proposal_failed", "Counter of failed proposals", init = 0 },
MetricCounter { PROPOSAL_ABORTED, "batcher_proposal_aborted", "Counter of aborted proposals", init = 0 },
Expand All @@ -23,6 +25,8 @@ pub fn register_metrics(storage_height: BlockNumber) {
STORAGE_HEIGHT.register();
#[allow(clippy::as_conversions)]
STORAGE_HEIGHT.set(storage_height.0 as f64);
CLASS_CACHE_MISSES.register();
CLASS_CACHE_HITS.register();

PROPOSAL_STARTED.register();
PROPOSAL_SUCCEEDED.register();
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_sequencer_dashboard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license.workspace = true
workspace = true

[dependencies]
const_format.workspace = true
indexmap = { workspace = true, features = ["serde"] }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
Expand Down
17 changes: 16 additions & 1 deletion crates/starknet_sequencer_dashboard/src/dashboard_definitions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use const_format::formatcp;
use starknet_batcher::metrics::{
BATCHED_TRANSACTIONS,
CLASS_CACHE_HITS,
CLASS_CACHE_MISSES,
PROPOSAL_FAILED,
PROPOSAL_STARTED,
PROPOSAL_SUCCEEDED,
Expand All @@ -18,7 +21,6 @@ use starknet_sequencer_metrics::metric_definitions::{
};

use crate::dashboard::{Dashboard, Panel, PanelType, Row};

#[cfg(test)]
#[path = "dashboard_definitions_test.rs"]
mod dashboard_definitions_test;
Expand Down Expand Up @@ -57,6 +59,18 @@ const PANEL_BATCHED_TRANSACTIONS: Panel = Panel::new(
PanelType::Stat,
);

const PANEL_CAIRO_NATIVE_CACHE_MISS_RATIO: Panel = Panel::new(
"cairo_native_cache_miss_ratio",
"The ratio of cache misses in the Cairo native cache",
formatcp!(
"100 * ({} / clamp_min(({} + {}), 1))",
CLASS_CACHE_MISSES.get_name(),
CLASS_CACHE_MISSES.get_name(),
CLASS_CACHE_HITS.get_name()
),
PanelType::Graph,
);

const PANEL_MEMPOOL_P2P_NUM_CONNECTED_PEERS: Panel = Panel::new(
MEMPOOL_P2P_NUM_CONNECTED_PEERS.get_name(),
MEMPOOL_P2P_NUM_CONNECTED_PEERS.get_description(),
Expand Down Expand Up @@ -158,6 +172,7 @@ const BATCHER_ROW: Row<'_> = Row::new(
PANEL_PROPOSAL_SUCCEEDED,
PANEL_PROPOSAL_FAILED,
PANEL_BATCHED_TRANSACTIONS,
PANEL_CAIRO_NATIVE_CACHE_MISS_RATIO,
],
);
const HTTP_SERVER_ROW: Row<'_> = Row::new(
Expand Down
Loading