Skip to content

Commit 03aab7d

Browse files
blockifier: add class cache metrics struct and inject from batcher (#10287)
1 parent 15543f7 commit 03aab7d

File tree

14 files changed

+176
-52
lines changed

14 files changed

+176
-52
lines changed

Cargo.lock

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

crates/apollo_batcher/src/block_builder.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use crate::cende_client_types::{StarknetClientStateDiff, StarknetClientTransacti
5454
use crate::metrics::{
5555
record_block_close_reason,
5656
BlockCloseReason,
57+
BATCHER_CLASS_CACHE_METRICS,
5758
PROPOSER_DEFERRED_TXS,
5859
VALIDATOR_WASTED_TXS,
5960
};
@@ -667,8 +668,8 @@ pub struct BlockMetadata {
667668

668669
// Type definitions for the abort channel required to abort the block builder.
669670
pub type AbortSignalSender = tokio::sync::oneshot::Sender<()>;
670-
pub type BatcherWorkerPool =
671-
Arc<WorkerPool<CachedState<StateReaderAndContractManager<ApolloReader>>>>;
671+
pub type ApolloStateReaderAndContractManager = StateReaderAndContractManager<ApolloReader>;
672+
pub type BatcherWorkerPool = Arc<WorkerPool<CachedState<ApolloStateReaderAndContractManager>>>;
672673

673674
/// The BlockBuilderFactoryTrait is responsible for creating a new block builder.
674675
#[cfg_attr(test, automock)]
@@ -703,9 +704,8 @@ impl BlockBuilderFactory {
703704
&self,
704705
block_metadata: BlockMetadata,
705706
runtime: tokio::runtime::Handle,
706-
) -> BlockBuilderResult<
707-
ConcurrentTransactionExecutor<StateReaderAndContractManager<ApolloReader>>,
708-
> {
707+
) -> BlockBuilderResult<ConcurrentTransactionExecutor<ApolloStateReaderAndContractManager>>
708+
{
709709
info!(
710710
"preprocess and create transaction executor for block {}",
711711
block_metadata.block_info.block_number
@@ -728,6 +728,7 @@ impl BlockBuilderFactory {
728728
let state_reader = StateReaderAndContractManager {
729729
state_reader: apollo_reader,
730730
contract_class_manager: self.contract_class_manager.clone(),
731+
class_cache_metrics: BATCHER_CLASS_CACHE_METRICS,
731732
};
732733

733734
let executor = ConcurrentTransactionExecutor::start_block(

crates/apollo_batcher/src/metrics.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use apollo_infra::metrics::{
88
};
99
use apollo_metrics::{define_infra_metrics, define_metrics, generate_permutation_labels};
1010
use blockifier::metrics::{
11+
CacheMetrics,
1112
CALLS_RUNNING_NATIVE,
12-
CLASS_CACHE_HITS,
13-
CLASS_CACHE_MISSES,
1413
NATIVE_CLASS_RETURNED,
1514
NATIVE_COMPILATION_ERROR,
1615
TOTAL_CALLS,
@@ -23,6 +22,9 @@ define_infra_metrics!(batcher);
2322

2423
define_metrics!(
2524
Batcher => {
25+
// Global class cache
26+
MetricCounter { CLASS_CACHE_MISSES, "batcher_class_cache_misses", "Counter of the batcher's global class cache misses", init=0 },
27+
MetricCounter { CLASS_CACHE_HITS, "batcher_class_cache_hits", "Counter of the batcher's global class cache hits", init=0 },
2628
// Heights
2729
MetricGauge { STORAGE_HEIGHT, "batcher_storage_height", "The height of the batcher's storage" },
2830
MetricGauge { LAST_BATCHED_BLOCK_HEIGHT, "batcher_last_batched_block_height", "The height of the last block received by batching" },
@@ -75,6 +77,9 @@ pub(crate) fn record_block_close_reason(reason: BlockCloseReason) {
7577
BLOCK_CLOSE_REASON.increment(1, &[(LABEL_NAME_BLOCK_CLOSE_REASON, reason.into())]);
7678
}
7779

80+
pub const BATCHER_CLASS_CACHE_METRICS: CacheMetrics =
81+
CacheMetrics { misses: CLASS_CACHE_MISSES, hits: CLASS_CACHE_HITS };
82+
7883
pub fn register_metrics(storage_height: BlockNumber) {
7984
STORAGE_HEIGHT.register();
8085
STORAGE_HEIGHT.set_lossy(storage_height.0);
@@ -104,9 +109,8 @@ pub fn register_metrics(storage_height: BlockNumber) {
104109
PROVING_GAS_IN_LAST_BLOCK.register();
105110

106111
// Blockifier's metrics
112+
BATCHER_CLASS_CACHE_METRICS.register();
107113
CALLS_RUNNING_NATIVE.register();
108-
CLASS_CACHE_HITS.register();
109-
CLASS_CACHE_MISSES.register();
110114
NATIVE_CLASS_RETURNED.register();
111115
NATIVE_COMPILATION_ERROR.register();
112116
TOTAL_CALLS.register();

crates/apollo_dashboard/resources/dev_grafana.json

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -950,22 +950,33 @@
950950
"Blockifier": {
951951
"panels": [
952952
{
953-
"title": "class_cache_miss_ratio",
954-
"description": "The ratio of cache misses when requesting compiled classes from the Blockifier State Reader",
953+
"title": "Class Cache Miss in Batcher",
954+
"description": "The ratio of cache misses when requesting compiled classes from the Blockifier State Reader in Batcher",
955955
"type": "timeseries",
956956
"exprs": [
957-
"(increase(class_cache_misses{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) / (increase(class_cache_misses{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) + increase(class_cache_hits{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m])))"
957+
"(increase(batcher_class_cache_misses{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) / (increase(batcher_class_cache_misses{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) + increase(batcher_class_cache_hits{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m])))"
958958
],
959959
"extra_params": {
960960
"unit": "percentunit"
961961
}
962962
},
963963
{
964-
"title": "native_class_returned_ratio",
965-
"description": "The ratio of Native classes returned by the Blockifier",
964+
"title": "Native Class Returned Ratio in Batcher",
965+
"description": "The ratio of Native classes returned by the Blockifier in Batcher",
966966
"type": "timeseries",
967967
"exprs": [
968-
"(increase(native_class_returned{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) / (increase(class_cache_hits{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) + increase(class_cache_misses{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m])))"
968+
"(increase(native_class_returned{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) / (increase(batcher_class_cache_misses{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) + increase(batcher_class_cache_hits{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m])))"
969+
],
970+
"extra_params": {
971+
"unit": "percentunit"
972+
}
973+
},
974+
{
975+
"title": "Class Cache Miss in Gateway",
976+
"description": "The ratio of cache misses when requesting compiled classes from the Blockifier State Reader in Gateway",
977+
"type": "timeseries",
978+
"exprs": [
979+
"(increase(gateway_class_cache_misses{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) / (increase(gateway_class_cache_misses{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m]) + increase(gateway_class_cache_hits{cluster=~\"$cluster\", namespace=~\"$namespace\"}[5m])))"
969980
],
970981
"extra_params": {
971982
"unit": "percentunit"
@@ -981,7 +992,7 @@
981992
"extra_params": {}
982993
},
983994
{
984-
"title": "native_execution_ratio",
995+
"title": "Native Execution Ratio",
985996
"description": "The ratio of calls running Cairo Native in the Blockifier",
986997
"type": "timeseries",
987998
"exprs": [

crates/apollo_dashboard/src/dashboard.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ impl Panel {
279279
}
280280

281281
pub(crate) fn ratio_time_series(
282-
name: &'static str,
283-
description: &'static str,
282+
name: &str,
283+
description: &str,
284284
numerator: &dyn MetricQueryName,
285285
denominator_parts: &[&dyn MetricQueryName],
286286
duration: &str,

crates/apollo_dashboard/src/panels/blockifier.rs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use apollo_batcher::metrics::{
2+
BATCHER_CLASS_CACHE_METRICS,
23
NUM_TRANSACTION_IN_BLOCK,
34
PROVING_GAS_IN_LAST_BLOCK,
45
SIERRA_GAS_IN_LAST_BLOCK,
56
};
6-
use apollo_metrics::metrics::MetricQueryName;
7+
use apollo_gateway::metrics::GATEWAY_CLASS_CACHE_METRICS;
8+
use apollo_metrics::metrics::{MetricQueryName, MetricScope};
79
use blockifier::metrics::{
10+
CacheMetrics,
811
BLOCKIFIER_METRIC_RATE_DURATION,
912
CALLS_RUNNING_NATIVE,
10-
CLASS_CACHE_HITS,
11-
CLASS_CACHE_MISSES,
1213
NATIVE_CLASS_RETURNED,
1314
NATIVE_COMPILATION_ERROR,
1415
TOTAL_CALLS,
@@ -19,24 +20,41 @@ use crate::dashboard::{Panel, PanelType, Row};
1920
const DENOMINATOR_DIVISOR_FOR_READABILITY: f64 = 1_000_000_000.0;
2021

2122
// TODO(MatanL/Shahak): use clamp_min(X, 1) on denom to avoid division by zero.
22-
fn get_panel_blockifier_state_reader_class_cache_miss_ratio() -> Panel {
23-
Panel::ratio_time_series(
24-
"class_cache_miss_ratio",
23+
fn get_panel_blockifier_state_reader_class_cache_miss_ratio(
24+
class_cache_metrics: &CacheMetrics,
25+
) -> Panel {
26+
let scope = class_cache_metrics.get_scope();
27+
// TODO(Arni): Share this code.
28+
let scope = match scope {
29+
MetricScope::Batcher => "Batcher",
30+
MetricScope::Gateway => "Gateway",
31+
_ => unreachable!("Scope of class cache metrics must be Batcher or Gateway. Got {scope:?}"),
32+
};
33+
let name = format!("Class Cache Miss in {scope}");
34+
let description = format!(
2535
"The ratio of cache misses when requesting compiled classes from the Blockifier State \
26-
Reader",
27-
&CLASS_CACHE_MISSES,
28-
&[&CLASS_CACHE_MISSES, &CLASS_CACHE_HITS],
36+
Reader in {scope}"
37+
);
38+
Panel::ratio_time_series(
39+
name.as_str(),
40+
description.as_str(),
41+
&class_cache_metrics.misses,
42+
&[&class_cache_metrics.misses, &class_cache_metrics.hits],
2943
BLOCKIFIER_METRIC_RATE_DURATION,
3044
)
3145
}
3246

3347
// TODO(MatanL/Shahak): use clamp_min(X, 1) on denom to avoid division by zero.
3448
fn get_panel_blockifier_state_reader_native_class_returned_ratio() -> Panel {
49+
let class_cache_metrics = BATCHER_CLASS_CACHE_METRICS;
50+
51+
let name = "Native Class Returned Ratio in Batcher";
52+
let description = "The ratio of Native classes returned by the Blockifier in Batcher";
3553
Panel::ratio_time_series(
36-
"native_class_returned_ratio",
37-
"The ratio of Native classes returned by the Blockifier",
54+
name,
55+
description,
3856
&NATIVE_CLASS_RETURNED,
39-
&[&CLASS_CACHE_HITS, &CLASS_CACHE_MISSES],
57+
&[&class_cache_metrics.misses, &class_cache_metrics.hits],
4058
BLOCKIFIER_METRIC_RATE_DURATION,
4159
)
4260
}
@@ -52,7 +70,7 @@ fn get_panel_native_compilation_error() -> Panel {
5270

5371
fn get_panel_native_execution_ratio() -> Panel {
5472
Panel::ratio_time_series(
55-
"native_execution_ratio",
73+
"Native Execution Ratio",
5674
"The ratio of calls running Cairo Native in the Blockifier",
5775
&CALLS_RUNNING_NATIVE,
5876
&[&TOTAL_CALLS],
@@ -98,8 +116,10 @@ pub(crate) fn get_blockifier_row() -> Row {
98116
Row::new(
99117
"Blockifier",
100118
vec![
101-
get_panel_blockifier_state_reader_class_cache_miss_ratio(),
119+
get_panel_blockifier_state_reader_class_cache_miss_ratio(&BATCHER_CLASS_CACHE_METRICS),
120+
// TODO(Arni): Add native class returned ratio for gateway
102121
get_panel_blockifier_state_reader_native_class_returned_ratio(),
122+
get_panel_blockifier_state_reader_class_cache_miss_ratio(&GATEWAY_CLASS_CACHE_METRICS),
103123
get_panel_native_compilation_error(),
104124
get_panel_native_execution_ratio(),
105125
get_panel_transactions_per_block(),

crates/apollo_gateway/src/metrics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use apollo_infra::metrics::{
1414
use apollo_metrics::metrics::LabeledMetricCounter;
1515
use apollo_metrics::{define_infra_metrics, define_metrics, generate_permutation_labels};
1616
use apollo_network_types::network_types::BroadcastedMessageMetadata;
17+
use blockifier::metrics::CacheMetrics;
1718
use starknet_api::rpc_transaction::{RpcTransaction, RpcTransactionLabelValue};
1819
use strum::{EnumVariantNames, VariantNames};
1920
use strum_macros::IntoStaticStr;
@@ -39,6 +40,8 @@ define_infra_metrics!(gateway);
3940

4041
define_metrics!(
4142
Gateway => {
43+
MetricCounter { CLASS_CACHE_MISSES, "gateway_class_cache_misses", "Counter of the gateway's global class cache misses", init=0 },
44+
MetricCounter { CLASS_CACHE_HITS, "gateway_class_cache_hits", "Counter of the gateway's global class cache hits", init=0 },
4245
LabeledMetricCounter { GATEWAY_TRANSACTIONS_RECEIVED, "gateway_transactions_received", "Counter of transactions received", init = 0 , labels = TRANSACTION_TYPE_AND_SOURCE_LABELS},
4346
LabeledMetricCounter { GATEWAY_TRANSACTIONS_FAILED, "gateway_transactions_failed", "Counter of failed transactions", init = 0 , labels = TRANSACTION_TYPE_AND_SOURCE_LABELS},
4447
LabeledMetricCounter { GATEWAY_TRANSACTIONS_SENT_TO_MEMPOOL, "gateway_transactions_sent_to_mempool", "Counter of transactions sent to the mempool", init = 0 , labels = TRANSACTION_TYPE_AND_SOURCE_LABELS},
@@ -243,7 +246,11 @@ impl Drop for GatewayMetricHandle {
243246
}
244247
}
245248

249+
pub const GATEWAY_CLASS_CACHE_METRICS: CacheMetrics =
250+
CacheMetrics { misses: CLASS_CACHE_MISSES, hits: CLASS_CACHE_HITS };
251+
246252
pub(crate) fn register_metrics() {
253+
GATEWAY_CLASS_CACHE_METRICS.register();
247254
GATEWAY_TRANSACTIONS_RECEIVED.register();
248255
GATEWAY_TRANSACTIONS_FAILED.register();
249256
GATEWAY_TRANSACTIONS_SENT_TO_MEMPOOL.register();

crates/apollo_metrics/src/metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use crate::metrics::histograms::{HistogramValue, LabeledMetricHistogram, Met
2020
pub static COLLECT_SEQUENCER_PROFILING_METRICS: OnceLock<bool> = OnceLock::new();
2121

2222
/// Relevant components for which metrics can be defined.
23-
#[derive(Clone, Copy, Debug)]
23+
#[derive(Clone, Copy, Debug, PartialEq)]
2424
pub enum MetricScope {
2525
Batcher,
2626
Blockifier,

crates/blockifier/src/metrics.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use apollo_metrics::define_metrics;
2+
use apollo_metrics::metrics::{MetricCounter, MetricDetails, MetricScope};
23

34
define_metrics!(
45
Blockifier => {
5-
MetricCounter { CLASS_CACHE_MISSES, "class_cache_misses", "Counter of global class cache misses", init=0 },
6-
MetricCounter { CLASS_CACHE_HITS, "class_cache_hits", "Counter of global class cache hits", init=0 },
76
MetricCounter {
87
NATIVE_CLASS_RETURNED,
98
"native_class_returned",
@@ -29,3 +28,33 @@ define_metrics!(
2928
);
3029

3130
pub const BLOCKIFIER_METRIC_RATE_DURATION: &str = "5m";
31+
32+
// TODO(Arni): Fix visibility of members of this struct (make them not public).
33+
pub struct CacheMetrics {
34+
pub misses: MetricCounter,
35+
pub hits: MetricCounter,
36+
}
37+
38+
impl CacheMetrics {
39+
pub fn register(&self) {
40+
self.misses.register();
41+
self.hits.register();
42+
}
43+
44+
pub fn increment_miss(&self) {
45+
self.misses.increment(1);
46+
}
47+
48+
pub fn increment_hit(&self) {
49+
self.hits.increment(1);
50+
}
51+
52+
pub fn get_scope(&self) -> MetricScope {
53+
assert_eq!(
54+
self.misses.get_scope(),
55+
self.hits.get_scope(),
56+
"Scope of misses and hits must be the same"
57+
);
58+
self.misses.get_scope()
59+
}
60+
}

crates/blockifier/src/state/state_reader_and_contract_manager.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use starknet_api::core::{ClassHash, CompiledClassHash};
22
use starknet_types_core::felt::Felt;
33

44
use crate::execution::contract_class::RunnableCompiledClass;
5-
use crate::metrics::{CLASS_CACHE_HITS, CLASS_CACHE_MISSES};
5+
use crate::metrics::CacheMetrics;
66
use crate::state::contract_class_manager::ContractClassManager;
77
use crate::state::errors::StateError;
88
use crate::state::global_cache::CompiledClasses;
@@ -21,6 +21,7 @@ pub trait FetchCompiledClasses: StateReader {
2121
pub struct StateReaderAndContractManager<S: FetchCompiledClasses> {
2222
pub state_reader: S,
2323
pub contract_class_manager: ContractClassManager,
24+
pub class_cache_metrics: CacheMetrics,
2425
}
2526

2627
impl<S: FetchCompiledClasses> StateReaderAndContractManager<S> {
@@ -40,11 +41,11 @@ impl<S: FetchCompiledClasses> StateReaderAndContractManager<S> {
4041
}
4142
}
4243
}
43-
CLASS_CACHE_HITS.increment(1);
44+
self.class_cache_metrics.increment_hit();
4445
self.update_native_metrics(&runnable_class);
4546
return Ok(runnable_class);
4647
}
47-
CLASS_CACHE_MISSES.increment(1);
48+
self.class_cache_metrics.increment_miss();
4849

4950
let compiled_class = self.state_reader.get_compiled_classes(class_hash)?;
5051
self.contract_class_manager.set_and_compile(class_hash, compiled_class.clone());

0 commit comments

Comments
 (0)