Skip to content

Commit

Permalink
feat(vm_executor): Add new histogram metric for gas per tx in vm_exec…
Browse files Browse the repository at this point in the history
…utor (#3215)

## What ❔

- Adds a new histogram metric <uint type> representing total gas used
per tx
- Adds a new histogram metric <uint type> representing the gas limit per
failed tx

## Why ❔

- The existing metric is a histogram of rates. That's a little difficult
to extract useful throughput numbers out of with the notable exception
of efficiency.
- Exporting this metric to let the consumer do their own: rate/anomoly
calculation on gas/tx, ratio of gas burn for failed tx, etc.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [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 `zkstack dev fmt` and `zkstack dev
lint`.
  • Loading branch information
doyleish authored Nov 8, 2024
1 parent e69d15b commit 3606fc1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 6 additions & 3 deletions core/lib/vm_executor/src/batch/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,21 @@ where
let elapsed = latency.observe();

if !res.tx_result.result.is_failed() {
let gas_per_nanosecond =
res.tx_result.statistics.computational_gas_used as f64 / elapsed.as_nanos() as f64;
let gas_used = res.tx_result.statistics.computational_gas_used;
EXECUTOR_METRICS
.computational_gas_per_nanosecond
.observe(gas_per_nanosecond);
.observe(gas_used as f64 / elapsed.as_nanos() as f64);
EXECUTOR_METRICS
.computational_gas_used
.observe(gas_used.into());
} else {
// The amount of computational gas paid for failed transactions is hard to get
// but comparing to the gas limit makes sense, since we can burn all gas
// if some kind of failure is a DDoS vector otherwise.
EXECUTOR_METRICS
.failed_tx_gas_limit_per_nanosecond
.observe(tx_gas_limit as f64 / elapsed.as_nanos() as f64);
EXECUTOR_METRICS.failed_tx_gas_limit.observe(tx_gas_limit);
}
Ok(res)
}
Expand Down
10 changes: 10 additions & 0 deletions core/lib/vm_executor/src/batch/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const GAS_PER_NANOSECOND_BUCKETS: Buckets = Buckets::values(&[
0.01, 0.03, 0.1, 0.3, 0.5, 0.75, 1., 1.5, 3., 5., 10., 20., 50.,
]);

const GAS_USED_BUCKETS: Buckets = Buckets::values(&[
10000., 25000., 45000., 70000., 100000., 150000., 225000., 350000., 500000.,
]);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EncodeLabelValue, EncodeLabelSet)]
#[metrics(label = "stage", rename_all = "snake_case")]
pub(super) enum TxExecutionStage {
Expand All @@ -37,8 +41,14 @@ pub(super) struct ExecutorMetrics {
pub batch_executor_command_response_time: Family<ExecutorCommand, Histogram<Duration>>,
#[metrics(buckets = GAS_PER_NANOSECOND_BUCKETS)]
pub computational_gas_per_nanosecond: Histogram<f64>,
/// Computational gas used, per transaction.
#[metrics(buckets = GAS_USED_BUCKETS)]
pub computational_gas_used: Histogram<u64>,
#[metrics(buckets = GAS_PER_NANOSECOND_BUCKETS)]
pub failed_tx_gas_limit_per_nanosecond: Histogram<f64>,
/// Gas limit, per failed transaction.
#[metrics(buckets = GAS_USED_BUCKETS)]
pub failed_tx_gas_limit: Histogram<u64>,
/// Cumulative latency of interacting with the storage when executing a transaction
/// in the batch executor.
#[metrics(buckets = Buckets::LATENCIES)]
Expand Down

0 comments on commit 3606fc1

Please sign in to comment.