Skip to content

Commit 701d19d

Browse files
committed
starknet_committer,apollo_committer: add timers for fetch patricia paths
1 parent 48a282a commit 701d19d

4 files changed

Lines changed: 88 additions & 16 deletions

File tree

crates/apollo_committer/src/committer.rs

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -549,11 +549,15 @@ where
549549
// 4. Merge the two sets of patricia paths and write the result to the storage.
550550
// 5. Update the commitment offset and return the global root and the patricia proofs.
551551
CommitBlockHeightPlan::CommitTip { state_diff_commitment } => {
552+
let mut block_measurements = SingleBlockMeasurements::default();
553+
block_measurements.start_measurement(Action::EndToEnd);
554+
552555
let pre_roots = self
553556
.forest_storage
554557
.read_roots(ForestDB::InitialReadContext::create_empty())
555558
.await
556559
.map_err(|e| self.map_internal_error(e))?;
560+
block_measurements.start_measurement(Action::FetchWitnessesFirstPass);
557561
let mut patricia_proofs = self
558562
.forest_storage
559563
.fetch_patricia_witnesses(
@@ -569,16 +573,21 @@ where
569573
height,
570574
message: format!("pre-commit witness paths: {e:?}"),
571575
})?;
576+
block_measurements
577+
.attempt_to_stop_measurement(
578+
Action::FetchWitnessesFirstPass,
579+
patricia_proofs.len(),
580+
)
581+
.ok();
572582

573-
let mut block_measurements = SingleBlockMeasurements::default();
574-
block_measurements.start_measurement(Action::EndToEnd);
575583
let CommitStateDiffOutput { filled_forest, global_root, deleted_nodes } =
576584
self.commit_state_diff(state_diff, &mut block_measurements).await?;
577585
let post_roots = filled_forest.state_roots();
578586

579587
let forest_updates = ForestDB::serialize_forest(&filled_forest)
580588
.map_err(|e| self.map_internal_error(e))?;
581589

590+
block_measurements.start_measurement(Action::FetchWitnessesSecondPass);
582591
let proof_after = self
583592
.forest_storage
584593
.fetch_patricia_witnesses(
@@ -594,23 +603,24 @@ where
594603
height,
595604
message: format!("post-commit witness paths: {e:?}"),
596605
})?;
606+
block_measurements
607+
.attempt_to_stop_measurement(
608+
Action::FetchWitnessesSecondPass,
609+
proof_after.len(),
610+
)
611+
.ok();
597612

598613
patricia_proofs.extend(proof_after);
599614

600615
let (metadata, next_offset) =
601616
commit_tip_metadata_bundle(height, global_root, state_diff_commitment);
602-
let witness_node_count = patricia_proofs.classes_trie_proof.len()
603-
+ patricia_proofs.contracts_trie_proof.nodes.len()
604-
+ patricia_proofs.contracts_trie_proof.leaves.len()
605-
+ patricia_proofs
606-
.contracts_trie_storage_proofs
607-
.values()
608-
.map(|proof| proof.len())
609-
.sum::<usize>();
617+
610618
info!(
611-
"For block number {height}, writing filled forest and {witness_node_count} \
612-
witness nodes to storage with metadata: {metadata:?}, delete {} nodes",
613-
deleted_nodes.len()
619+
"For block number {height}, writing filled forest and {witness_count} \
620+
witnesses to storage with metadata: {metadata:?}, delete \
621+
{deleted_nodes_count} nodes",
622+
witness_count = patricia_proofs.len(),
623+
deleted_nodes_count = deleted_nodes.len(),
614624
);
615625
block_measurements.start_measurement(Action::Write);
616626
let n_write_entries = self
@@ -669,9 +679,16 @@ impl ComponentStarter for ApolloCommitter {
669679
}
670680

671681
#[allow(clippy::as_conversions)]
682+
// TODO: Consider adding fetch witnesses measurements.
672683
fn update_metrics(
673684
height: BlockNumber,
674-
BlockMeasurement { n_reads, n_writes, durations, modifications_counts }: &BlockMeasurement,
685+
BlockMeasurement {
686+
n_reads,
687+
n_writes,
688+
durations,
689+
modifications_counts,
690+
fetched_witnesses_count,
691+
}: &BlockMeasurement,
675692
) {
676693
BLOCKS_COMMITTED.increment(1);
677694
TOTAL_BLOCK_DURATION.increment((durations.block * 1000.0) as u64);
@@ -736,6 +753,7 @@ fn update_metrics(
736753
write_rate,
737754
modifications_counts,
738755
emptied_leaves_percentage,
756+
*fetched_witnesses_count,
739757
);
740758
}
741759

@@ -749,12 +767,24 @@ fn log_block_measurements(
749767
write_rate: Option<f64>,
750768
modifications_counts: &BlockModificationsCounts,
751769
emptied_leaves_percentage: Option<f64>,
770+
fetched_witnesses_count: usize,
752771
) {
772+
#[cfg(feature = "os_input")]
773+
let witness_log = format!(
774+
"witness fetch ms (pre-commit/post-commit): {:.0}/{:.0}, witness entries: {}",
775+
durations.fetch_witnesses_first_pass * 1000.0,
776+
durations.fetch_witnesses_second_pass * 1000.0,
777+
fetched_witnesses_count,
778+
);
779+
#[cfg(not(feature = "os_input"))]
780+
let witness_log = String::new();
781+
753782
debug!(
754783
"Block {height} stats: durations in ms (total/read/compute/write): \
755784
{:.0}/{:.0}/{:.0}/{:.0}, total block duration per modification in µs: {}, rates in \
756785
entries/sec (read/compute/write): {}/{}/{}, modifications count \
757-
(storage_tries/contracts_trie/classes_trie/emptied_storage_leaves): {}/{}/{}/{}{}",
786+
(storage_tries/contracts_trie/classes_trie/emptied_storage_leaves): {}/{}/{}/{}{}, \
787+
{witness_log}",
758788
durations.block * 1000.0,
759789
durations.read * 1000.0,
760790
durations.compute * 1000.0,
@@ -767,6 +797,7 @@ fn log_block_measurements(
767797
modifications_counts.contracts_trie,
768798
modifications_counts.classes_trie,
769799
modifications_counts.emptied_storage_leaves,
770-
emptied_leaves_percentage.map_or(String::new(), |p| format!(" ({p:.2}%)"))
800+
emptied_leaves_percentage.map_or(String::new(), |p| format!(" ({p:.2}%)")),
801+
witness_log = witness_log,
771802
);
772803
}

crates/starknet_committer/src/block_committer/measurements_util.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ pub enum Action {
1111
Read,
1212
Compute,
1313
Write,
14+
#[cfg(feature = "os_input")]
15+
FetchWitnessesFirstPass,
16+
#[cfg(feature = "os_input")]
17+
FetchWitnessesSecondPass,
1418
}
1519

1620
#[derive(Default)]
@@ -19,6 +23,10 @@ pub struct BlockTimers {
1923
pub read_timer: Option<Instant>,
2024
pub compute_timer: Option<Instant>,
2125
pub writer_timer: Option<Instant>,
26+
#[cfg(feature = "os_input")]
27+
pub fetch_witnesses_first_pass_timer: Option<Instant>,
28+
#[cfg(feature = "os_input")]
29+
pub fetch_witnesses_second_pass_timer: Option<Instant>,
2230
}
2331

2432
impl BlockTimers {
@@ -28,6 +36,10 @@ impl BlockTimers {
2836
Action::Read => &mut self.read_timer,
2937
Action::Compute => &mut self.compute_timer,
3038
Action::Write => &mut self.writer_timer,
39+
#[cfg(feature = "os_input")]
40+
Action::FetchWitnessesFirstPass => &mut self.fetch_witnesses_first_pass_timer,
41+
#[cfg(feature = "os_input")]
42+
Action::FetchWitnessesSecondPass => &mut self.fetch_witnesses_second_pass_timer,
3143
}
3244
}
3345

@@ -91,6 +103,12 @@ pub struct BlockDurations {
91103
pub read: f64, // Duration of a read phase (seconds).
92104
pub compute: f64, // Duration of a computation phase (seconds).
93105
pub write: f64, // Duration of a write phase (seconds).
106+
#[cfg(feature = "os_input")]
107+
// Duration of fetching witnesses w.r.t the old root (seconds).
108+
pub fetch_witnesses_first_pass: f64,
109+
#[cfg(feature = "os_input")]
110+
// Duration of fetching witnesses w.r.t the new root (seconds).
111+
pub fetch_witnesses_second_pass: f64,
94112
}
95113

96114
#[derive(Default, Clone, Debug, PartialEq, Eq)]
@@ -113,6 +131,8 @@ pub struct BlockMeasurement {
113131
pub n_reads: usize,
114132
pub durations: BlockDurations,
115133
pub modifications_counts: BlockModificationsCounts,
134+
// Number of witnesses fetched in the first pass (pre-commit).
135+
pub fetched_witnesses_count: usize,
116136
}
117137

118138
impl BlockMeasurement {
@@ -137,6 +157,15 @@ impl BlockMeasurement {
137157
Action::EndToEnd => {
138158
self.durations.block = duration_in_seconds;
139159
}
160+
#[cfg(feature = "os_input")]
161+
Action::FetchWitnessesFirstPass => {
162+
self.durations.fetch_witnesses_first_pass = duration_in_seconds;
163+
self.fetched_witnesses_count += entries_count;
164+
}
165+
#[cfg(feature = "os_input")]
166+
Action::FetchWitnessesSecondPass => {
167+
self.durations.fetch_witnesses_second_pass = duration_in_seconds;
168+
}
140169
}
141170
}
142171
}

crates/starknet_committer/src/patricia_merkle_tree/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ impl StarknetForestProofs {
6565
self.contracts_trie_storage_proofs.entry(address).or_default().extend(proof);
6666
}
6767
}
68+
69+
pub fn len(&self) -> usize {
70+
self.classes_trie_proof.len()
71+
+ self.contracts_trie_proof.nodes.len()
72+
+ self
73+
.contracts_trie_storage_proofs
74+
.values()
75+
.fold(0, |count, proofs| count + proofs.len())
76+
}
6877
}
6978

7079
pub struct RootHashes {

crates/starknet_committer_cli/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ starknet_patricia_storage = { workspace = true, features = [
3131
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
3232
tracing.workspace = true
3333
tracing-subscriber.workspace = true
34+
35+
[features]
36+
os_input = ["starknet_committer/os_input"]

0 commit comments

Comments
 (0)