@@ -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
120121pub struct MutTxId {
121122 lock : ArcMutexGuard < RawMutex , Inner > ,
123+ lock_wait_time : Duration ,
124+ timer : Instant ,
122125}
123126
124127struct 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
0 commit comments