Skip to content

Commit 7d1b975

Browse files
authored
starknet_committer,apollo_committer: add timers for fetch patricia paths (#14189)
1 parent 55ad270 commit 7d1b975

3 files changed

Lines changed: 89 additions & 16 deletions

File tree

crates/apollo_committer/src/committer.rs

Lines changed: 49 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.get_nodes_count(),
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.get_nodes_count(),
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.get_nodes_count(),
623+
deleted_nodes_count = deleted_nodes.len(),
614624
);
615625
block_measurements.start_measurement(Action::Write);
616626
let n_write_entries = self
@@ -669,9 +679,17 @@ impl ComponentStarter for ApolloCommitter {
669679
}
670680

671681
#[allow(clippy::as_conversions)]
682+
// TODO(Ariel): 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+
#[cfg(feature = "os_input")]
691+
fetched_witnesses_count,
692+
}: &BlockMeasurement,
675693
) {
676694
BLOCKS_COMMITTED.increment(1);
677695
TOTAL_BLOCK_DURATION.increment((durations.block * 1000.0) as u64);
@@ -736,6 +754,8 @@ fn update_metrics(
736754
write_rate,
737755
modifications_counts,
738756
emptied_leaves_percentage,
757+
#[cfg(feature = "os_input")]
758+
*fetched_witnesses_count,
739759
);
740760
}
741761

@@ -749,12 +769,24 @@ fn log_block_measurements(
749769
write_rate: Option<f64>,
750770
modifications_counts: &BlockModificationsCounts,
751771
emptied_leaves_percentage: Option<f64>,
772+
#[cfg(feature = "os_input")] fetched_witnesses_count: usize,
752773
) {
774+
#[cfg(feature = "os_input")]
775+
let witness_log = format!(
776+
"witness fetch ms (pre-commit/post-commit): {:.0}/{:.0}, witness entries: {}",
777+
durations.fetch_witnesses_first_pass * 1000.0,
778+
durations.fetch_witnesses_second_pass * 1000.0,
779+
fetched_witnesses_count,
780+
);
781+
#[cfg(not(feature = "os_input"))]
782+
let witness_log = String::new();
783+
753784
debug!(
754785
"Block {height} stats: durations in ms (total/read/compute/write): \
755786
{:.0}/{:.0}/{:.0}/{:.0}, total block duration per modification in µs: {}, rates in \
756787
entries/sec (read/compute/write): {}/{}/{}, modifications count \
757-
(storage_tries/contracts_trie/classes_trie/emptied_storage_leaves): {}/{}/{}/{}{}",
788+
(storage_tries/contracts_trie/classes_trie/emptied_storage_leaves): {}/{}/{}/{}{}, \
789+
{witness_log}",
758790
durations.block * 1000.0,
759791
durations.read * 1000.0,
760792
durations.compute * 1000.0,
@@ -767,6 +799,7 @@ fn log_block_measurements(
767799
modifications_counts.contracts_trie,
768800
modifications_counts.classes_trie,
769801
modifications_counts.emptied_storage_leaves,
770-
emptied_leaves_percentage.map_or(String::new(), |p| format!(" ({p:.2}%)"))
802+
emptied_leaves_percentage.map_or(String::new(), |p| format!(" ({p:.2}%)")),
803+
witness_log = witness_log,
771804
);
772805
}

crates/starknet_committer/src/block_committer/measurements_util.rs

Lines changed: 30 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,9 @@ pub struct BlockMeasurement {
113131
pub n_reads: usize,
114132
pub durations: BlockDurations,
115133
pub modifications_counts: BlockModificationsCounts,
134+
#[cfg(feature = "os_input")]
135+
// Number of witnesses fetched in the first pass (pre-commit).
136+
pub fetched_witnesses_count: usize,
116137
}
117138

118139
impl BlockMeasurement {
@@ -137,6 +158,15 @@ impl BlockMeasurement {
137158
Action::EndToEnd => {
138159
self.durations.block = duration_in_seconds;
139160
}
161+
#[cfg(feature = "os_input")]
162+
Action::FetchWitnessesFirstPass => {
163+
self.durations.fetch_witnesses_first_pass = duration_in_seconds;
164+
self.fetched_witnesses_count += entries_count;
165+
}
166+
#[cfg(feature = "os_input")]
167+
Action::FetchWitnessesSecondPass => {
168+
self.durations.fetch_witnesses_second_pass = duration_in_seconds;
169+
}
140170
}
141171
}
142172
}

crates/starknet_committer/src/patricia_merkle_tree/types.rs

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

7080
pub struct RootHashes {

0 commit comments

Comments
 (0)