-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(vm-runner): Improve VM runner / VM playground (#2840)
## What ❔ Various minor improvements to VM runner / VM playground: - Get batch storage asynchronously, so that it works efficiently with snapshot storage. - Add metrics / logs to ensure that snapshot storage works as expected. ## Why ❔ Improves usability. ## Checklist - [x] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [x] Tests for the changes have been added / updated. - [x] Documentation comments have been added / updated. - [x] Code has been formatted via `zk fmt` and `zk lint`.
- Loading branch information
Showing
22 changed files
with
552 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::time::Duration; | ||
|
||
use vise::{Buckets, EncodeLabelSet, EncodeLabelValue, Family, Histogram, Metrics, Unit}; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelValue, EncodeLabelSet)] | ||
#[metrics(label = "stage", rename_all = "snake_case")] | ||
pub(super) enum SnapshotStage { | ||
BatchHeader, | ||
ProtectiveReads, | ||
TouchedSlots, | ||
PreviousValues, | ||
InitialWrites, | ||
Bytecodes, | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelValue, EncodeLabelSet)] | ||
#[metrics(label = "kind", rename_all = "snake_case")] | ||
pub(super) enum AccessKind { | ||
ReadValue, | ||
IsWriteInitial, | ||
LoadFactoryDep, | ||
GetEnumerationIndex, | ||
} | ||
|
||
#[derive(Debug, Metrics)] | ||
#[metrics(prefix = "state_snapshot")] | ||
pub(super) struct SnapshotMetrics { | ||
/// Latency of loading a batch snapshot split by stage. | ||
#[metrics(buckets = Buckets::LATENCIES, unit = Unit::Seconds)] | ||
pub load_latency: Family<SnapshotStage, Histogram<Duration>>, | ||
/// Latency of accessing the fallback storage for a batch snapshot. | ||
#[metrics(buckets = Buckets::LATENCIES, unit = Unit::Seconds)] | ||
pub fallback_access_latency: Family<AccessKind, Histogram<Duration>>, | ||
} | ||
|
||
#[vise::register] | ||
pub(super) static SNAPSHOT_METRICS: vise::Global<SnapshotMetrics> = vise::Global::new(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::collections::HashMap; | ||
|
||
use zksync_types::{StorageKey, StorageValue, H256}; | ||
use zksync_vm_interface::storage::ReadStorage; | ||
|
||
use crate::RocksdbStorage; | ||
|
||
/// DB difference introduced by one batch. | ||
#[derive(Debug, Clone)] | ||
pub struct BatchDiff { | ||
/// Storage slots touched by this batch along with new values there. | ||
pub state_diff: HashMap<H256, H256>, | ||
/// Initial write indices introduced by this batch. | ||
pub enum_index_diff: HashMap<H256, u64>, | ||
/// Factory dependencies introduced by this batch. | ||
pub factory_dep_diff: HashMap<H256, Vec<u8>>, | ||
} | ||
|
||
/// A RocksDB cache instance with in-memory DB diffs that gives access to DB state at batches `N` to | ||
/// `N + K`, where `K` is the number of diffs. | ||
#[derive(Debug)] | ||
pub struct RocksdbWithMemory { | ||
/// RocksDB cache instance caught up to batch `N`. | ||
pub rocksdb: RocksdbStorage, | ||
/// Diffs for batches `N + 1` to `N + K`. | ||
pub batch_diffs: Vec<BatchDiff>, | ||
} | ||
|
||
impl ReadStorage for RocksdbWithMemory { | ||
fn read_value(&mut self, key: &StorageKey) -> StorageValue { | ||
let hashed_key = key.hashed_key(); | ||
match self | ||
.batch_diffs | ||
.iter() | ||
.rev() | ||
.find_map(|b| b.state_diff.get(&hashed_key)) | ||
{ | ||
None => self.rocksdb.read_value(key), | ||
Some(value) => *value, | ||
} | ||
} | ||
|
||
fn is_write_initial(&mut self, key: &StorageKey) -> bool { | ||
match self | ||
.batch_diffs | ||
.iter() | ||
.find_map(|b| b.enum_index_diff.get(&key.hashed_key())) | ||
{ | ||
None => self.rocksdb.is_write_initial(key), | ||
Some(_) => false, | ||
} | ||
} | ||
|
||
fn load_factory_dep(&mut self, hash: H256) -> Option<Vec<u8>> { | ||
match self | ||
.batch_diffs | ||
.iter() | ||
.find_map(|b| b.factory_dep_diff.get(&hash)) | ||
{ | ||
None => self.rocksdb.load_factory_dep(hash), | ||
Some(value) => Some(value.clone()), | ||
} | ||
} | ||
|
||
fn get_enumeration_index(&mut self, key: &StorageKey) -> Option<u64> { | ||
match self | ||
.batch_diffs | ||
.iter() | ||
.find_map(|b| b.enum_index_diff.get(&key.hashed_key())) | ||
{ | ||
None => self.rocksdb.get_enumeration_index(key), | ||
Some(value) => Some(*value), | ||
} | ||
} | ||
} |
Oops, something went wrong.