Skip to content

Commit e12c26b

Browse files
perf(470): Add metrics for transaction cpu and elapsed time (#483)
Closes #470.
1 parent 5f98d70 commit e12c26b

File tree

2 files changed

+40
-1
lines changed
  • crates/core/src/db

2 files changed

+40
-1
lines changed

crates/core/src/db/datastore/locking_tx_datastore/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::{
1313
collections::{BTreeMap, BTreeSet, HashMap},
1414
ops::{Deref, RangeBounds},
1515
sync::Arc,
16+
time::{Duration, Instant},
1617
vec,
1718
};
1819

@@ -119,6 +120,8 @@ impl<'a> DataRef<'a> {
119120

120121
pub struct MutTxId {
121122
lock: ArcMutexGuard<RawMutex, Inner>,
123+
lock_wait_time: Duration,
124+
timer: Instant,
122125
}
123126

124127
struct CommittedState {
@@ -1972,29 +1975,55 @@ impl traits::MutTx for Locking {
19721975
type MutTxId = MutTxId;
19731976

19741977
fn begin_mut_tx(&self) -> Self::MutTxId {
1978+
let timer = Instant::now();
19751979
let mut inner = self.inner.lock_arc();
1980+
let lock_wait_time = timer.elapsed();
19761981
if inner.tx_state.is_some() {
19771982
panic!("The previous transaction was not properly rolled back or committed.");
19781983
}
19791984
inner.tx_state = Some(TxState::new());
1980-
MutTxId { lock: inner }
1985+
MutTxId {
1986+
lock: inner,
1987+
lock_wait_time,
1988+
timer,
1989+
}
19811990
}
19821991

19831992
fn rollback_mut_tx(&self, ctx: &ExecutionContext, mut tx: Self::MutTxId) {
1993+
let elapsed_time = tx.timer.elapsed();
1994+
let cpu_time = elapsed_time - tx.lock_wait_time;
19841995
DB_METRICS
19851996
.rdb_num_txns_rolledback
19861997
.with_label_values(&ctx.txn_type(), &ctx.database(), ctx.reducer_name().unwrap_or(""))
19871998
.inc();
1999+
DB_METRICS
2000+
.rdb_txn_cpu_time_ns
2001+
.with_label_values(&ctx.txn_type(), &ctx.database(), ctx.reducer_name().unwrap_or(""))
2002+
.observe(cpu_time.as_nanos() as f64);
2003+
DB_METRICS
2004+
.rdb_txn_elapsed_time_ns
2005+
.with_label_values(&ctx.txn_type(), &ctx.database(), ctx.reducer_name().unwrap_or(""))
2006+
.observe(elapsed_time.as_nanos() as f64);
19882007
tx.lock.rollback();
19892008
}
19902009

19912010
fn commit_mut_tx(&self, ctx: &ExecutionContext, mut tx: Self::MutTxId) -> super::Result<Option<TxData>> {
2011+
let elapsed_time = tx.timer.elapsed();
2012+
let cpu_time = elapsed_time - tx.lock_wait_time;
19922013
// Note, we record empty transactions in our metrics.
19932014
// That is, transactions that don't write any rows to the commit log.
19942015
DB_METRICS
19952016
.rdb_num_txns_committed
19962017
.with_label_values(&ctx.txn_type(), &ctx.database(), ctx.reducer_name().unwrap_or(""))
19972018
.inc();
2019+
DB_METRICS
2020+
.rdb_txn_cpu_time_ns
2021+
.with_label_values(&ctx.txn_type(), &ctx.database(), ctx.reducer_name().unwrap_or(""))
2022+
.observe(cpu_time.as_nanos() as f64);
2023+
DB_METRICS
2024+
.rdb_txn_elapsed_time_ns
2025+
.with_label_values(&ctx.txn_type(), &ctx.database(), ctx.reducer_name().unwrap_or(""))
2026+
.observe(elapsed_time.as_nanos() as f64);
19982027
tx.lock.commit()
19992028
}
20002029

crates/core/src/db/db_metrics/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ metrics_group!(
7575
#[help = "The cumulative number of rolled back transactions"]
7676
#[labels(txn_type: TransactionType, db: Address, reducer: str)]
7777
pub rdb_num_txns_rolledback: IntCounterVec,
78+
79+
#[name = spacetime_txn_elapsed_time_ns]
80+
#[help = "The total elapsed (wall) time of a transaction (nanoseconds)"]
81+
#[labels(txn_type: TransactionType, db: Address, reducer: str)]
82+
pub rdb_txn_elapsed_time_ns: HistogramVec,
83+
84+
#[name = spacetime_txn_cpu_time_ns]
85+
#[help = "The time spent executing a transaction (nanoseconds), excluding time spent waiting to acquire database locks"]
86+
#[labels(txn_type: TransactionType, db: Address, reducer: str)]
87+
pub rdb_txn_cpu_time_ns: HistogramVec,
7888
}
7989
);
8090

0 commit comments

Comments
 (0)