Skip to content

Commit e977713

Browse files
committed
[Metrics] Use helper traits and functions when possible
Simplify the code and prepare for further changes.
1 parent e9db58e commit e977713

File tree

34 files changed

+166
-236
lines changed

34 files changed

+166
-236
lines changed

aptos-move/aptos-vm/src/aptos_vm.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use aptos_gas_schedule::{
4444
AptosGasParameters, VMGasParameters,
4545
};
4646
use aptos_logger::{enabled, prelude::*, Level};
47+
use aptos_metrics_core::IntCounterVecHelper;
4748
#[cfg(any(test, feature = "testing"))]
4849
use aptos_types::state_store::StateViewId;
4950
use aptos_types::{
@@ -2855,7 +2856,7 @@ impl AptosVM {
28552856
TransactionStatus::Retry => None,
28562857
};
28572858
if let Some(label) = counter_label {
2858-
USER_TRANSACTIONS_EXECUTED.with_label_values(&[label]).inc();
2859+
USER_TRANSACTIONS_EXECUTED.inc_with(&[label]);
28592860
}
28602861
(vm_status, output)
28612862
},
@@ -3126,9 +3127,7 @@ impl VMValidator for AptosVM {
31263127
),
31273128
};
31283129

3129-
TRANSACTIONS_VALIDATED
3130-
.with_label_values(&[counter_label])
3131-
.inc();
3130+
TRANSACTIONS_VALIDATED.inc_with(&[counter_label]);
31323131

31333132
result
31343133
}

aptos-move/aptos-vm/src/sharded_block_executor/sharded_executor_service.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use aptos_block_executor::{
2020
code_cache_global_manager::AptosModuleCacheManager, txn_provider::default::DefaultTxnProvider,
2121
};
2222
use aptos_logger::{info, trace};
23+
use aptos_metrics_core::TimerHelper;
2324
use aptos_types::{
2425
block_executor::{
2526
config::{BlockExecutorConfig, BlockExecutorLocalConfig},
@@ -190,11 +191,11 @@ impl<S: StateView + Sync + Send + 'static> ShardedExecutorService<S> {
190191
let mut result = vec![];
191192
for (round, sub_block) in transactions.into_sub_blocks().into_iter().enumerate() {
192193
let _timer = SHARDED_BLOCK_EXECUTION_BY_ROUNDS_SECONDS
193-
.with_label_values(&[&self.shard_id.to_string(), &round.to_string()])
194-
.start_timer();
195-
SHARDED_BLOCK_EXECUTOR_TXN_COUNT
196-
.with_label_values(&[&self.shard_id.to_string(), &round.to_string()])
197-
.observe(sub_block.transactions.len() as f64);
194+
.timer_with(&[&self.shard_id.to_string(), &round.to_string()]);
195+
SHARDED_BLOCK_EXECUTOR_TXN_COUNT.observe_with(
196+
&[&self.shard_id.to_string(), &round.to_string()],
197+
sub_block.transactions.len() as f64,
198+
);
198199
info!(
199200
"executing sub block for shard {} and round {}, number of txns {}",
200201
self.shard_id,
@@ -234,8 +235,7 @@ impl<S: StateView + Sync + Send + 'static> ShardedExecutorService<S> {
234235
num_txns
235236
);
236237
let exe_timer = SHARDED_EXECUTOR_SERVICE_SECONDS
237-
.with_label_values(&[&self.shard_id.to_string(), "execute_block"])
238-
.start_timer();
238+
.timer_with(&[&self.shard_id.to_string(), "execute_block"]);
239239
let ret = self.execute_block(
240240
transactions,
241241
state_view.as_ref(),
@@ -250,8 +250,7 @@ impl<S: StateView + Sync + Send + 'static> ShardedExecutorService<S> {
250250
drop(exe_timer);
251251

252252
let _result_tx_timer = SHARDED_EXECUTOR_SERVICE_SECONDS
253-
.with_label_values(&[&self.shard_id.to_string(), "result_tx"])
254-
.start_timer();
253+
.timer_with(&[&self.shard_id.to_string(), "result_tx"]);
255254
self.coordinator_client.send_execution_result(ret);
256255
},
257256
ExecutorShardCommand::Stop => {

aptos-move/block-executor/src/counters.rs

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use aptos_metrics_core::{
55
exponential_buckets, register_avg_counter_vec, register_histogram, register_histogram_vec,
66
register_int_counter, register_int_counter_vec, register_int_gauge, Histogram, HistogramVec,
7-
IntCounter, IntCounterVec, IntGauge,
7+
IntCounter, IntCounterVec, IntGauge, TimerHelper,
88
};
99
use aptos_mvhashmap::BlockStateStats;
1010
use aptos_types::fee_statement::FeeStatement;
@@ -242,29 +242,35 @@ pub static BLOCK_VIEW_BASE_VALUES_MEMORY_USAGE: Lazy<HistogramVec> = Lazy::new(|
242242
});
243243

244244
fn observe_gas(counter: &Lazy<HistogramVec>, mode_str: &str, fee_statement: &FeeStatement) {
245-
counter
246-
.with_label_values(&[mode_str, GasType::TOTAL_GAS])
247-
.observe(fee_statement.gas_used() as f64);
248-
249-
counter
250-
.with_label_values(&[mode_str, GasType::EXECUTION_GAS])
251-
.observe(fee_statement.execution_gas_used() as f64);
252-
253-
counter
254-
.with_label_values(&[mode_str, GasType::IO_GAS])
255-
.observe(fee_statement.io_gas_used() as f64);
256-
257-
counter
258-
.with_label_values(&[mode_str, GasType::NON_STORAGE_GAS])
259-
.observe((fee_statement.execution_gas_used() + fee_statement.io_gas_used()) as f64);
260-
261-
counter
262-
.with_label_values(&[mode_str, GasType::STORAGE_FEE])
263-
.observe(fee_statement.storage_fee_used() as f64);
264-
265-
counter
266-
.with_label_values(&[mode_str, GasType::STORAGE_FEE_REFUND])
267-
.observe(fee_statement.storage_fee_refund() as f64);
245+
counter.observe_with(
246+
&[mode_str, GasType::TOTAL_GAS],
247+
fee_statement.gas_used() as f64,
248+
);
249+
250+
counter.observe_with(
251+
&[mode_str, GasType::EXECUTION_GAS],
252+
fee_statement.execution_gas_used() as f64,
253+
);
254+
255+
counter.observe_with(
256+
&[mode_str, GasType::IO_GAS],
257+
fee_statement.io_gas_used() as f64,
258+
);
259+
260+
counter.observe_with(
261+
&[mode_str, GasType::NON_STORAGE_GAS],
262+
(fee_statement.execution_gas_used() + fee_statement.io_gas_used()) as f64,
263+
);
264+
265+
counter.observe_with(
266+
&[mode_str, GasType::STORAGE_FEE],
267+
fee_statement.storage_fee_used() as f64,
268+
);
269+
270+
counter.observe_with(
271+
&[mode_str, GasType::STORAGE_FEE_REFUND],
272+
fee_statement.storage_fee_refund() as f64,
273+
);
268274
}
269275

270276
pub(crate) fn update_block_gas_counters(
@@ -281,17 +287,11 @@ pub(crate) fn update_block_gas_counters(
281287
};
282288

283289
observe_gas(&BLOCK_GAS, mode_str, accumulated_fee_statement);
284-
BLOCK_COMMITTED_TXNS
285-
.with_label_values(&[mode_str])
286-
.observe(num_committed as f64);
290+
BLOCK_COMMITTED_TXNS.observe_with(&[mode_str], num_committed as f64);
287291

288-
EFFECTIVE_BLOCK_GAS
289-
.with_label_values(&[mode_str])
290-
.observe(accumulated_effective_gas as f64);
292+
EFFECTIVE_BLOCK_GAS.observe_with(&[mode_str], accumulated_effective_gas as f64);
291293

292-
APPROX_BLOCK_OUTPUT_SIZE
293-
.with_label_values(&[mode_str])
294-
.observe(accumulated_approx_output_size as f64);
294+
APPROX_BLOCK_OUTPUT_SIZE.observe_with(&[mode_str], accumulated_approx_output_size as f64);
295295
}
296296

297297
pub(crate) fn update_txn_gas_counters(txn_fee_statements: &Vec<FeeStatement>, is_parallel: bool) {
@@ -313,25 +313,29 @@ pub(crate) fn update_state_counters(block_state_stats: BlockStateStats, is_paral
313313
Mode::SEQUENTIAL
314314
};
315315

316+
BLOCK_VIEW_DISTINCT_KEYS.observe_with(
317+
&[mode_str, "resource"],
318+
block_state_stats.num_resources as f64,
319+
);
320+
BLOCK_VIEW_DISTINCT_KEYS.observe_with(
321+
&[mode_str, "resource_group"],
322+
block_state_stats.num_resource_groups as f64,
323+
);
324+
BLOCK_VIEW_DISTINCT_KEYS.observe_with(
325+
&[mode_str, "delayed_field"],
326+
block_state_stats.num_delayed_fields as f64,
327+
);
316328
BLOCK_VIEW_DISTINCT_KEYS
317-
.with_label_values(&[mode_str, "resource"])
318-
.observe(block_state_stats.num_resources as f64);
319-
BLOCK_VIEW_DISTINCT_KEYS
320-
.with_label_values(&[mode_str, "resource_group"])
321-
.observe(block_state_stats.num_resource_groups as f64);
322-
BLOCK_VIEW_DISTINCT_KEYS
323-
.with_label_values(&[mode_str, "delayed_field"])
324-
.observe(block_state_stats.num_delayed_fields as f64);
325-
BLOCK_VIEW_DISTINCT_KEYS
326-
.with_label_values(&[mode_str, "module"])
327-
.observe(block_state_stats.num_modules as f64);
328-
329-
BLOCK_VIEW_BASE_VALUES_MEMORY_USAGE
330-
.with_label_values(&[mode_str, "resource"])
331-
.observe(block_state_stats.base_resources_size as f64);
332-
BLOCK_VIEW_BASE_VALUES_MEMORY_USAGE
333-
.with_label_values(&[mode_str, "delayed_field"])
334-
.observe(block_state_stats.base_delayed_fields_size as f64);
329+
.observe_with(&[mode_str, "module"], block_state_stats.num_modules as f64);
330+
331+
BLOCK_VIEW_BASE_VALUES_MEMORY_USAGE.observe_with(
332+
&[mode_str, "resource"],
333+
block_state_stats.base_resources_size as f64,
334+
);
335+
BLOCK_VIEW_BASE_VALUES_MEMORY_USAGE.observe_with(
336+
&[mode_str, "delayed_field"],
337+
block_state_stats.base_delayed_fields_size as f64,
338+
);
335339
}
336340

337341
pub static GLOBAL_MODULE_CACHE_SIZE_IN_BYTES: Lazy<IntGauge> = Lazy::new(|| {

aptos-move/block-executor/src/limit_processor.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
counters, hot_state_op_accumulator::BlockHotStateOpAccumulator, types::ReadWriteSummary,
66
};
77
use aptos_logger::{info, warn};
8+
use aptos_metrics_core::IntCounterVecHelper;
89
use aptos_types::{
910
fee_statement::FeeStatement,
1011
on_chain_config::BlockGasLimitType,
@@ -131,9 +132,7 @@ impl<'s, T: Transaction, S: TStateView<Key = T::Key>> BlockGasLimitProcessor<'s,
131132
// PER_BLOCK_GAS_LIMIT, early halt BlockSTM.
132133
let accumulated_block_gas = self.get_effective_accumulated_block_gas();
133134
if accumulated_block_gas >= per_block_gas_limit {
134-
counters::EXCEED_PER_BLOCK_GAS_LIMIT_COUNT
135-
.with_label_values(&[mode])
136-
.inc();
135+
counters::EXCEED_PER_BLOCK_GAS_LIMIT_COUNT.inc_with(&[mode]);
137136
info!(
138137
"[BlockSTM]: execution ({}) early halted due to \
139138
accumulated_block_gas {} >= PER_BLOCK_GAS_LIMIT {}",
@@ -146,9 +145,7 @@ impl<'s, T: Transaction, S: TStateView<Key = T::Key>> BlockGasLimitProcessor<'s,
146145
if let Some(per_block_output_limit) = self.block_gas_limit_type.block_output_limit() {
147146
let accumulated_output = self.get_accumulated_approx_output_size();
148147
if accumulated_output >= per_block_output_limit {
149-
counters::EXCEED_PER_BLOCK_OUTPUT_LIMIT_COUNT
150-
.with_label_values(&[mode])
151-
.inc();
148+
counters::EXCEED_PER_BLOCK_OUTPUT_LIMIT_COUNT.inc_with(&[mode]);
152149
info!(
153150
"[BlockSTM]: execution ({}) early halted due to \
154151
accumulated_output {} >= PER_BLOCK_OUTPUT_LIMIT {}",

execution/block-partitioner/src/v2/build_edge.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
use crate::v2::{counters::MISC_TIMERS_SECONDS, state::PartitionState, PartitionerV2};
5+
use aptos_metrics_core::TimerHelper;
56
use aptos_types::{
67
block_executor::partitioner::{
78
PartitionedTransactions, SubBlock, SubBlocksForShard, TransactionWithDependencies,
@@ -16,9 +17,7 @@ use std::sync::Mutex;
1617

1718
impl PartitionerV2 {
1819
pub(crate) fn add_edges(state: &mut PartitionState) -> PartitionedTransactions {
19-
let _timer = MISC_TIMERS_SECONDS
20-
.with_label_values(&["add_edges"])
21-
.start_timer();
20+
let _timer = MISC_TIMERS_SECONDS.timer_with(&["add_edges"]);
2221

2322
state.sub_block_matrix = state.thread_pool.install(|| {
2423
(0..state.num_rounds())

execution/block-partitioner/src/v2/init.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ use crate::{
88
state::PartitionState, types::OriginalTxnIdx, PartitionerV2,
99
},
1010
};
11+
use aptos_metrics_core::TimerHelper;
1112
use rayon::{iter::ParallelIterator, prelude::IntoParallelIterator};
1213
use std::sync::RwLock;
1314

1415
impl PartitionerV2 {
1516
pub(crate) fn init(state: &mut PartitionState) {
16-
let _timer = MISC_TIMERS_SECONDS
17-
.with_label_values(&["init"])
18-
.start_timer();
17+
let _timer = MISC_TIMERS_SECONDS.timer_with(&["init"]);
1918

2019
state.thread_pool.install(|| {
2120
(0..state.num_txns())

execution/block-partitioner/src/v2/partition_to_matrix.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::v2::{
99
PartitionerV2,
1010
};
1111
use aptos_logger::trace;
12+
use aptos_metrics_core::TimerHelper;
1213
use aptos_types::block_executor::partitioner::{RoundId, TxnIndex};
1314
use dashmap::DashMap;
1415
use rayon::{
@@ -27,9 +28,7 @@ impl PartitionerV2 {
2728
/// Populate `state.finalized_txn_matrix` with txns flattened into a matrix (num_rounds by num_shards),
2829
/// in a way that avoid in-round cross-shard conflicts.
2930
pub(crate) fn remove_cross_shard_dependencies(state: &mut PartitionState) {
30-
let _timer = MISC_TIMERS_SECONDS
31-
.with_label_values(&["remove_cross_shard_dependencies"])
32-
.start_timer();
31+
let _timer = MISC_TIMERS_SECONDS.timer_with(&["remove_cross_shard_dependencies"]);
3332

3433
let mut remaining_txns = mem::take(&mut state.pre_partitioned);
3534
assert_eq!(state.num_executor_shards, remaining_txns.len());
@@ -48,9 +47,7 @@ impl PartitionerV2 {
4847
}
4948
}
5049

51-
let _timer = MISC_TIMERS_SECONDS
52-
.with_label_values(&["last_round"])
53-
.start_timer();
50+
let _timer = MISC_TIMERS_SECONDS.timer_with(&["last_round"]);
5451

5552
if !state.partition_last_round {
5653
trace!("Merging txns after discarding stopped.");
@@ -83,9 +80,7 @@ impl PartitionerV2 {
8380
Vec<Vec<PrePartitionedTxnIdx>>,
8481
Vec<Vec<PrePartitionedTxnIdx>>,
8582
) {
86-
let _timer = MISC_TIMERS_SECONDS
87-
.with_label_values(&[format!("round_{round_id}").as_str()])
88-
.start_timer();
83+
let _timer = MISC_TIMERS_SECONDS.timer_with(&[format!("round_{round_id}").as_str()]);
8984

9085
let num_shards = remaining_txns.len();
9186

@@ -182,9 +177,7 @@ impl PartitionerV2 {
182177
}
183178

184179
pub(crate) fn build_index_from_txn_matrix(state: &mut PartitionState) {
185-
let _timer = MISC_TIMERS_SECONDS
186-
.with_label_values(&["build_index_from_txn_matrix"])
187-
.start_timer();
180+
let _timer = MISC_TIMERS_SECONDS.timer_with(&["build_index_from_txn_matrix"]);
188181

189182
let num_rounds = state.finalized_txn_matrix.len();
190183
state.start_index_matrix = vec![vec![0; state.num_executor_shards]; num_rounds];

execution/block-partitioner/src/v2/state.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
},
1515
Sender,
1616
};
17+
use aptos_metrics_core::TimerHelper;
1718
use aptos_types::{
1819
block_executor::partitioner::{
1920
CrossShardDependencies, RoundId, ShardId, ShardedTxnIndex, SubBlock,
@@ -116,9 +117,7 @@ impl PartitionState {
116117
cross_shard_dep_avoid_threshold: f32,
117118
partition_last_round: bool,
118119
) -> Self {
119-
let _timer = MISC_TIMERS_SECONDS
120-
.with_label_values(&["new"])
121-
.start_timer();
120+
let _timer = MISC_TIMERS_SECONDS.timer_with(&["new"]);
122121
let num_txns = txns.len();
123122
let sender_counter = AtomicUsize::new(0);
124123
let key_counter = AtomicUsize::new(0);

execution/executor-benchmark/src/block_preparation.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use aptos_block_partitioner::{BlockPartitioner, PartitionerConfig};
99
use aptos_crypto::HashValue;
1010
use aptos_experimental_runtimes::thread_manager::optimal_min_len;
1111
use aptos_logger::info;
12+
use aptos_metrics_core::{IntCounterVecHelper, TimerHelper};
1213
use aptos_types::{
1314
block_executor::partitioner::{ExecutableBlock, ExecutableTransactions},
1415
transaction::{signature_verified_transaction::SignatureVerifiedTransaction, Transaction},
@@ -70,12 +71,10 @@ impl BlockPreparationStage {
7071
let block_id = HashValue::random();
7172
let sig_verified_txns: Vec<SignatureVerifiedTransaction> =
7273
self.sig_verify_pool.install(|| {
73-
let _timer = TIMER.with_label_values(&["sig_verify"]).start_timer();
74+
let _timer = TIMER.timer_with(&["sig_verify"]);
7475

7576
let num_txns = txns.len();
76-
NUM_TXNS
77-
.with_label_values(&["sig_verify"])
78-
.inc_by(num_txns as u64);
77+
NUM_TXNS.inc_with_by(&["sig_verify"], num_txns as u64);
7978

8079
txns.into_par_iter()
8180
.with_min_len(optimal_min_len(num_txns, SIG_VERIFY_RAYON_MIN_THRESHOLD))
@@ -85,12 +84,10 @@ impl BlockPreparationStage {
8584
let block: ExecutableBlock = match &self.maybe_partitioner {
8685
None => (block_id, sig_verified_txns).into(),
8786
Some(partitioner) => {
88-
NUM_TXNS
89-
.with_label_values(&["partition"])
90-
.inc_by(sig_verified_txns.len() as u64);
87+
NUM_TXNS.inc_with_by(&["partition"], sig_verified_txns.len() as u64);
9188
let analyzed_transactions =
9289
sig_verified_txns.into_iter().map(|t| t.into()).collect();
93-
let timer = TIMER.with_label_values(&["partition"]).start_timer();
90+
let timer = TIMER.timer_with(&["partition"]);
9491
let partitioned_txns =
9592
partitioner.partition(analyzed_transactions, self.num_executor_shards);
9693
timer.stop_and_record();

0 commit comments

Comments
 (0)