Skip to content

Commit

Permalink
Use vise metrics in zksync_core
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Sep 29, 2023
1 parent adfddfc commit 3e25ee9
Show file tree
Hide file tree
Showing 71 changed files with 2,237 additions and 1,562 deletions.
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.

1 change: 1 addition & 0 deletions core/lib/merkle_tree/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) struct GeneralMetrics {
pub leaf_count: Gauge<u64>,
}

#[vise::register]
pub(crate) static GENERAL_METRICS: Global<GeneralMetrics> = Global::new();

const BYTE_SIZE_BUCKETS: Buckets = Buckets::exponential(65_536.0..=16.0 * 1_024.0 * 1_024.0, 2.0);
Expand Down
2 changes: 1 addition & 1 deletion core/lib/types/src/aggregated_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl L1BatchExecuteOperation {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AggregatedActionType {
Commit,
PublishProofOnchain,
Expand Down
3 changes: 2 additions & 1 deletion core/lib/zksync_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keywords = ["blockchain", "zksync"]
categories = ["cryptography"]

[dependencies]
vise = { git = "https://github.com/matter-labs/vise.git", version = "0.1.0", rev = "9d097ab747b037b6e62504df1db5b975425b6bdd" }
zksync_state = { path = "../state" }
zksync_types = { path = "../types" }
zksync_dal = { path = "../dal" }
Expand Down Expand Up @@ -40,8 +41,8 @@ multivm = { path = "../multivm" }

serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
metrics = "0.21"
itertools = "0.10.3"
metrics = "0.21"
ctrlc = { version = "3.1", features = ["termination"] }
rand = "0.8"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::Instant;

use actix_web::{
web::{self, Json},
HttpResponse, Result as ActixResult,
Expand All @@ -8,7 +6,7 @@ use serde::Serialize;

use zksync_types::{contract_verification_api::VerificationIncomingRequest, Address};

use super::api_decl::RestApi;
use super::{api_decl::RestApi, metrics::METRICS};

fn ok_json(data: impl Serialize) -> ActixResult<HttpResponse> {
Ok(HttpResponse::Ok().json(data))
Expand All @@ -32,7 +30,7 @@ impl RestApi {
self_: web::Data<Self>,
Json(request): Json<VerificationIncomingRequest>,
) -> ActixResult<HttpResponse> {
let start = Instant::now();
let method_latency = METRICS.call[&"contract_verification"].start();
if let Err(res) = Self::validate_contract_verification_query(&request) {
return Ok(res);
}
Expand Down Expand Up @@ -65,7 +63,7 @@ impl RestApi {
.await
.unwrap();

metrics::histogram!("api.contract_verification.call", start.elapsed(), "method" => "contract_verification");
method_latency.observe();
ok_json(request_id)
}

Expand All @@ -74,8 +72,7 @@ impl RestApi {
self_: web::Data<Self>,
id: web::Path<usize>,
) -> ActixResult<HttpResponse> {
let start = Instant::now();

let method_latency = METRICS.call[&"contract_verification_request_status"].start();
let status = self_
.replica_connection_pool
.access_storage_tagged("api")
Expand All @@ -86,7 +83,7 @@ impl RestApi {
.await
.unwrap();

metrics::histogram!("api.contract_verification.call", start.elapsed(), "method" => "contract_verification_request_status");
method_latency.observe();
match status {
Some(status) => ok_json(status),
None => Ok(HttpResponse::NotFound().finish()),
Expand All @@ -95,8 +92,7 @@ impl RestApi {

#[tracing::instrument(skip(self_))]
pub async fn zksolc_versions(self_: web::Data<Self>) -> ActixResult<HttpResponse> {
let start = Instant::now();

let method_latency = METRICS.call[&"contract_verification_zksolc_versions"].start();
let versions = self_
.replica_connection_pool
.access_storage_tagged("api")
Expand All @@ -107,14 +103,13 @@ impl RestApi {
.await
.unwrap();

metrics::histogram!("api.contract_verification.call", start.elapsed(), "method" => "contract_verification_zksolc_versions");
method_latency.observe();
ok_json(versions)
}

#[tracing::instrument(skip(self_))]
pub async fn solc_versions(self_: web::Data<Self>) -> ActixResult<HttpResponse> {
let start = Instant::now();

let method_latency = METRICS.call[&"contract_verification_solc_versions"].start();
let versions = self_
.replica_connection_pool
.access_storage_tagged("api")
Expand All @@ -125,14 +120,13 @@ impl RestApi {
.await
.unwrap();

metrics::histogram!("api.contract_verification.call", start.elapsed(), "method" => "contract_verification_solc_versions");
method_latency.observe();
ok_json(versions)
}

#[tracing::instrument(skip(self_))]
pub async fn zkvyper_versions(self_: web::Data<Self>) -> ActixResult<HttpResponse> {
let start = Instant::now();

let method_latency = METRICS.call[&"contract_verification_zkvyper_versions"].start();
let versions = self_
.replica_connection_pool
.access_storage_tagged("api")
Expand All @@ -143,14 +137,13 @@ impl RestApi {
.await
.unwrap();

metrics::histogram!("api.contract_verification.call", start.elapsed(), "method" => "contract_verification_zkvyper_versions");
method_latency.observe();
ok_json(versions)
}

#[tracing::instrument(skip(self_))]
pub async fn vyper_versions(self_: web::Data<Self>) -> ActixResult<HttpResponse> {
let start = Instant::now();

let method_latency = METRICS.call[&"contract_verification_vyper_versions"].start();
let versions = self_
.replica_connection_pool
.access_storage_tagged("api")
Expand All @@ -161,7 +154,7 @@ impl RestApi {
.await
.unwrap();

metrics::histogram!("api.contract_verification.call", start.elapsed(), "method" => "contract_verification_vyper_versions");
method_latency.observe();
ok_json(versions)
}

Expand All @@ -170,7 +163,7 @@ impl RestApi {
self_: web::Data<Self>,
address: web::Path<Address>,
) -> ActixResult<HttpResponse> {
let start = Instant::now();
let method_latency = METRICS.call[&"contract_verification_info"].start();

let info = self_
.replica_connection_pool
Expand All @@ -182,7 +175,7 @@ impl RestApi {
.await
.unwrap();

metrics::histogram!("api.contract_verification.call", start.elapsed(), "method" => "contract_verification_info");
method_latency.observe();
match info {
Some(info) => ok_json(info),
None => Ok(HttpResponse::NotFound().finish()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Metrics for contract verification.
use vise::{Buckets, Histogram, LabeledFamily, Metrics};

use std::time::Duration;

#[derive(Debug, Metrics)]
#[metrics(prefix = "api_contract_verification")]
pub(super) struct ContractVerificationMetrics {
#[metrics(buckets = Buckets::LATENCIES, labels = ["method"])]
pub call: LabeledFamily<&'static str, Histogram<Duration>>,
}

#[vise::register]
pub(super) static METRICS: vise::Global<ContractVerificationMetrics> = vise::Global::new();
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ use zksync_config::configs::api::ContractVerificationApiConfig;
use zksync_dal::connection::ConnectionPool;
use zksync_utils::panic_notify::{spawn_panic_handler, ThreadPanicNotify};

use api_decl::RestApi;
mod api_decl;
mod api_impl;
mod metrics;

pub mod api_decl;
pub mod api_impl;
use self::api_decl::RestApi;

fn start_server(api: RestApi, bind_to: SocketAddr, threads: usize) -> Server {
HttpServer::new(move || {
Expand Down
16 changes: 9 additions & 7 deletions core/lib/zksync_core/src/api_server/execution_sandbox/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//!
//! This module is intended to be blocking.
use multivm::{VmInstance, VmInstanceData};
use std::time::{Duration, Instant};
use vm::{constants::BLOCK_GAS_LIMIT, HistoryDisabled, L1BatchEnv, L2BlockEnv, SystemEnv};

use multivm::{VmInstance, VmInstanceData};
use vm::{constants::BLOCK_GAS_LIMIT, HistoryDisabled, L1BatchEnv, L2BlockEnv, SystemEnv};
use zksync_config::constants::{
SYSTEM_CONTEXT_ADDRESS, SYSTEM_CONTEXT_CURRENT_L2_BLOCK_INFO_POSITION,
SYSTEM_CONTEXT_CURRENT_TX_ROLLING_HASH_POSITION, ZKPORTER_IS_AVAILABLE,
Expand All @@ -26,7 +26,10 @@ use zksync_types::{
};
use zksync_utils::{h256_to_u256, time::seconds_since_epoch, u256_to_h256};

use super::{vm_metrics, BlockArgs, TxExecutionArgs, TxSharedArgs, VmPermit};
use super::{
vm_metrics::{self, SandboxStage, SANDBOX_METRICS},
BlockArgs, TxExecutionArgs, TxSharedArgs, VmPermit,
};

#[allow(clippy::too_many_arguments)]
pub(super) fn apply_vm_in_sandbox<T>(
Expand Down Expand Up @@ -209,18 +212,17 @@ pub(super) fn apply_vm_in_sandbox<T>(
&mut initial_version,
));

metrics::histogram!("api.web3.sandbox", stage_started_at.elapsed(), "stage" => "initialization");
SANDBOX_METRICS.sandbox[&SandboxStage::Initialization].observe(stage_started_at.elapsed());
span.exit();

let tx_id = format!(
"{:?}-{}",
tx.initiator_account(),
tx.nonce().unwrap_or(Nonce(0))
);
let stage_started_at = Instant::now();
let execution_latency = SANDBOX_METRICS.sandbox[&SandboxStage::Execution].start();
let result = apply(&mut vm, tx);
let vm_execution_took = stage_started_at.elapsed();
metrics::histogram!("api.web3.sandbox", vm_execution_took, "stage" => "execution");
let vm_execution_took = execution_latency.observe();

let memory_metrics = vm.record_vm_memory_metrics();
if let Some(memory_metrics) = memory_metrics {
Expand Down
20 changes: 10 additions & 10 deletions core/lib/zksync_core/src/api_server/execution_sandbox/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::time::Duration;
use tokio::runtime::Handle;

use vm::utils::fee::derive_base_fee_and_gas_per_pubdata;
Expand All @@ -9,8 +9,6 @@ use zksync_state::{PostgresStorage, PostgresStorageCaches, ReadStorage, StorageV
use zksync_types::{api, AccountTreeId, L2ChainId, MiniblockNumber, U256};
use zksync_utils::bytecode::{compress_bytecode, hash_bytecode};

use super::tx_sender::MultiVMBaseSystemContracts;

// Note: keep the modules private, and instead re-export functions that make public interface.
mod apply;
mod error;
Expand All @@ -19,11 +17,14 @@ mod tracers;
mod validate;
mod vm_metrics;

use self::vm_metrics::SandboxStage;
pub(super) use self::{
error::SandboxExecutionError,
execute::{execute_tx_eth_call, execute_tx_with_pending_state, TxExecutionArgs},
tracers::ApiTracer,
vm_metrics::{SubmitTxStage, SANDBOX_METRICS},
};
use super::tx_sender::MultiVMBaseSystemContracts;

/// Permit to invoke VM code.
///
Expand Down Expand Up @@ -120,21 +121,20 @@ impl VmConcurrencyLimiter {
/// Returns a permit that should be dropped when the VM execution is finished.
pub async fn acquire(&self) -> Option<VmPermit> {
let available_permits = self.limiter.available_permits();
metrics::histogram!(
"api.web3.sandbox.semaphore.permits",
available_permits as f64
);
SANDBOX_METRICS
.sandbox_execution_permits
.observe(available_permits);

let start = Instant::now();
let latency = SANDBOX_METRICS.sandbox[&SandboxStage::VmConcurrencyLimiterAcquire].start();
let permit = Arc::clone(&self.limiter).acquire_owned().await.ok()?;
let elapsed = start.elapsed();
let elapsed = latency.observe();
// We don't want to emit too many logs.
if elapsed > Duration::from_millis(10) {
tracing::debug!(
"Permit is obtained. Available permits: {available_permits}. Took {elapsed:?}"
);
}
metrics::histogram!("api.web3.sandbox", elapsed, "stage" => "vm_concurrency_limiter_acquire");

Some(VmPermit {
rt_handle: self.rt_handle.clone(),
_permit: Arc::new(permit),
Expand Down
Loading

0 comments on commit 3e25ee9

Please sign in to comment.