Skip to content

Commit

Permalink
sdk: Deprecate timing::duration_as_*() functions (#2586)
Browse files Browse the repository at this point in the history
These functions are from a time when Duration did not natively support
these operations. Duration has now supported these methods for some
time, so we should do away with our functions and encourage any callers
to use the methods on Duration directly instead
  • Loading branch information
steviez authored Aug 14, 2024
1 parent 939d7f6 commit ae18213
Show file tree
Hide file tree
Showing 20 changed files with 69 additions and 82 deletions.
10 changes: 3 additions & 7 deletions accounts-db/src/append_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,6 @@ pub mod tests {
solana_sdk::{
account::{Account, AccountSharedData},
clock::Slot,
timing::duration_as_ms,
},
std::{mem::ManuallyDrop, time::Instant},
test_case::test_case,
Expand Down Expand Up @@ -1535,15 +1534,15 @@ pub mod tests {
indexes.push(pos);
assert_eq!(sizes, av.get_account_sizes(&indexes));
}
trace!("append time: {} ms", duration_as_ms(&now.elapsed()),);
trace!("append time: {} ms", now.elapsed().as_millis());

let now = Instant::now();
for _ in 0..size {
let sample = thread_rng().gen_range(0..indexes.len());
let account = create_test_account(sample + 1);
assert_eq!(av.get_account_test(indexes[sample]).unwrap(), account);
}
trace!("random read time: {} ms", duration_as_ms(&now.elapsed()),);
trace!("random read time: {} ms", now.elapsed().as_millis());

let now = Instant::now();
assert_eq!(indexes.len(), size);
Expand All @@ -1556,10 +1555,7 @@ pub mod tests {
assert_eq!(recovered, account.1);
sample += 1;
});
trace!(
"sequential read time: {} ms",
duration_as_ms(&now.elapsed()),
);
trace!("sequential read time: {} ms", now.elapsed().as_millis());
}

#[test_case(StorageAccess::Mmap)]
Expand Down
8 changes: 4 additions & 4 deletions banking-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use {
pubkey::{self, Pubkey},
signature::{Keypair, Signature, Signer},
system_instruction, system_transaction,
timing::{duration_as_us, timestamp},
timing::timestamp,
transaction::Transaction,
},
solana_streamer::socket::SocketAddrSpace,
Expand Down Expand Up @@ -534,7 +534,7 @@ fn main() {
bank.slot(),
bank.transaction_count(),
);
tx_total_us += duration_as_us(&now.elapsed());
tx_total_us += now.elapsed().as_micros() as u64;

let mut poh_time = Measure::start("poh_time");
poh_recorder
Expand Down Expand Up @@ -578,14 +578,14 @@ fn main() {
bank.slot(),
bank.transaction_count(),
);
tx_total_us += duration_as_us(&now.elapsed());
tx_total_us += now.elapsed().as_micros() as u64;
}

// This signature clear may not actually clear the signatures
// in this chunk, but since we rotate between CHUNKS then
// we should clear them by the time we come around again to re-use that chunk.
bank.clear_signatures();
total_us += duration_as_us(&now.elapsed());
total_us += now.elapsed().as_micros() as u64;
total_sent += sent;

if current_iteration_index % num_chunks == 0 {
Expand Down
14 changes: 7 additions & 7 deletions bench-tps/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use {
pubkey::Pubkey,
signature::{Keypair, Signer},
system_instruction,
timing::{duration_as_ms, duration_as_s, duration_as_us, timestamp},
timing::timestamp,
transaction::Transaction,
},
solana_tps_client::*,
Expand Down Expand Up @@ -233,12 +233,12 @@ where
"Done. {:.2} thousand signatures per second, {:.2} us per signature, {} ms total time, {:?}",
bsps * 1_000_000_f64,
nsps / 1_000_f64,
duration_as_ms(&duration),
duration.as_millis(),
blockhash,
);
datapoint_info!(
"bench-tps-generate_txs",
("duration", duration_as_us(&duration), i64)
("duration", duration.as_micros() as i64, i64)
);

transactions
Expand Down Expand Up @@ -1029,12 +1029,12 @@ fn do_tx_transfers<T: TpsClient + ?Sized>(
total_tx_sent_count.fetch_add(num_txs, Ordering::Relaxed);
info!(
"Tx send done. {} ms {} tps",
duration_as_ms(&transfer_start.elapsed()),
num_txs as f32 / duration_as_s(&transfer_start.elapsed()),
transfer_start.elapsed().as_millis(),
num_txs as f32 / transfer_start.elapsed().as_secs_f32(),
);
datapoint_info!(
"bench-tps-do_tx_transfers",
("duration", duration_as_us(&transfer_start.elapsed()), i64),
("duration", transfer_start.elapsed().as_micros() as i64, i64),
("count", num_txs, i64)
);
}
Expand Down Expand Up @@ -1107,7 +1107,7 @@ fn compute_and_report_stats(
);
info!(
"\tAverage TPS: {}",
max_tx_count as f32 / duration_as_s(tx_send_elapsed)
max_tx_count as f32 / tx_send_elapsed.as_secs_f32()
);
}

Expand Down
4 changes: 2 additions & 2 deletions bench-tps/src/perf_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
log::*,
solana_sdk::{commitment_config::CommitmentConfig, timing::duration_as_s},
solana_sdk::commitment_config::CommitmentConfig,
solana_tps_client::TpsClient,
std::{
sync::{
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn sample_txs<T>(
let sample_txs = txs - last_txs;
last_txs = txs;

let tps = sample_txs as f32 / duration_as_s(&elapsed);
let tps = sample_txs as f32 / elapsed.as_secs_f32();
if tps > max_tps {
max_tps = tps;
}
Expand Down
4 changes: 2 additions & 2 deletions core/benches/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use {
pubkey,
signature::{Keypair, Signature, Signer},
system_instruction, system_transaction,
timing::{duration_as_us, timestamp},
timing::timestamp,
transaction::{Transaction, VersionedTransaction},
},
solana_streamer::socket::SocketAddrSpace,
Expand Down Expand Up @@ -355,7 +355,7 @@ fn bench_banking(bencher: &mut Bencher, tx_type: TransactionType) {
bank.clear_signatures();
trace!(
"time: {} checked: {} sent: {}",
duration_as_us(&now.elapsed()),
now.elapsed().as_micros(),
txes / CHUNKS,
sent,
);
Expand Down
3 changes: 1 addition & 2 deletions core/benches/sigverify_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use {
packet::PacketFlags,
signature::{Keypair, Signer},
system_transaction,
timing::duration_as_ms,
},
std::time::{Duration, Instant},
test::Bencher,
Expand Down Expand Up @@ -167,7 +166,7 @@ fn bench_sigverify_stage(bencher: &mut Bencher, use_same_tx: bool) {
let batches = gen_batches(use_same_tx);
trace!(
"starting... generation took: {} ms batches: {}",
duration_as_ms(&now.elapsed()),
now.elapsed().as_millis(),
batches.len()
);

Expand Down
4 changes: 2 additions & 2 deletions core/src/repair/serve_repair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use {
pubkey::{Pubkey, PUBKEY_BYTES},
signature::{Signable, Signature, Signer, SIGNATURE_BYTES},
signer::keypair::Keypair,
timing::{duration_as_ms, timestamp},
timing::timestamp,
},
solana_streamer::{
sendmmsg::{batch_send, SendPktsError},
Expand Down Expand Up @@ -517,7 +517,7 @@ impl ServeRepair {
}

fn report_time_spent(label: &str, time: &Duration, extra: &str) {
let count = duration_as_ms(time);
let count = time.as_millis();
if count > 5 {
info!("{} took: {} ms {}", label, count, extra);
}
Expand Down
7 changes: 2 additions & 5 deletions core/src/sigverify_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,7 @@ impl SigVerifyStage {
mod tests {
use {
super::*,
crate::{
banking_trace::BankingTracer, sigverify::TransactionSigVerifier,
sigverify_stage::timing::duration_as_ms,
},
crate::{banking_trace::BankingTracer, sigverify::TransactionSigVerifier},
crossbeam_channel::unbounded,
solana_perf::{
packet::{to_packet_batches, Packet},
Expand Down Expand Up @@ -563,7 +560,7 @@ mod tests {
let batches = gen_batches(use_same_tx, packets_per_batch, total_packets);
trace!(
"starting... generation took: {} ms batches: {}",
duration_as_ms(&now.elapsed()),
now.elapsed().as_millis(),
batches.len()
);

Expand Down
11 changes: 5 additions & 6 deletions entry/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use {
solana_sdk::{
hash::Hash,
packet::Meta,
timing,
transaction::{
Result, SanitizedTransaction, Transaction, TransactionError,
TransactionVerificationMode, VersionedTransaction,
Expand Down Expand Up @@ -670,7 +669,7 @@ impl EntrySlice for [Entry] {
r
})
});
let poh_duration_us = timing::duration_as_us(&now.elapsed());
let poh_duration_us = now.elapsed().as_micros() as u64;
EntryVerificationState {
verification_status: if res {
EntryVerificationStatus::Success
Expand Down Expand Up @@ -756,7 +755,7 @@ impl EntrySlice for [Entry] {
})
})
});
let poh_duration_us = timing::duration_as_us(&now.elapsed());
let poh_duration_us = now.elapsed().as_micros() as u64;
EntryVerificationState {
verification_status: if res {
EntryVerificationStatus::Success
Expand Down Expand Up @@ -849,9 +848,9 @@ impl EntrySlice for [Entry] {
assert!(res == 0, "GPU PoH verify many failed");
inc_new_counter_info!(
"entry_verify-gpu_thread",
timing::duration_as_us(&gpu_wait.elapsed()) as usize
gpu_wait.elapsed().as_micros() as usize
);
timing::duration_as_us(&gpu_wait.elapsed())
gpu_wait.elapsed().as_micros() as u64
})
.unwrap();

Expand Down Expand Up @@ -879,7 +878,7 @@ impl EntrySlice for [Entry] {
});
EntryVerificationState {
verification_status: EntryVerificationStatus::Pending,
poh_duration_us: timing::duration_as_us(&start.elapsed()),
poh_duration_us: start.elapsed().as_micros() as u64,
device_verification_data,
}
}
Expand Down
7 changes: 3 additions & 4 deletions genesis/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use {
signature::{Keypair, Signer},
signer::keypair::read_keypair_file,
stake::state::StakeStateV2,
system_program, timing,
system_program,
},
solana_stake_program::stake_state,
solana_vote_program::vote_state::{self, VoteState},
Expand Down Expand Up @@ -144,8 +144,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.max(rent.minimum_balance(StakeStateV2::size_of()))
.to_string();

let default_target_tick_duration =
timing::duration_as_us(&PohConfig::default().target_tick_duration);
let default_target_tick_duration = PohConfig::default().target_tick_duration;
let default_ticks_per_slot = &clock::DEFAULT_TICKS_PER_SLOT.to_string();
let default_cluster_type = "mainnet-beta";
let default_genesis_archive_unpacked_size = MAX_GENESIS_ARCHIVE_UNPACKED_SIZE.to_string();
Expand Down Expand Up @@ -473,7 +472,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
target_tick_duration: if matches.is_present("target_tick_duration") {
Duration::from_micros(value_t_or_exit!(matches, "target_tick_duration", u64))
} else {
Duration::from_micros(default_target_tick_duration)
default_target_tick_duration
},
..PohConfig::default()
};
Expand Down
4 changes: 1 addition & 3 deletions ledger/src/blockstore_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use {
pubkey::Pubkey,
saturating_add_assign,
signature::{Keypair, Signature},
timing,
transaction::{
Result, SanitizedTransaction, TransactionError, TransactionVerificationMode,
VersionedTransaction,
Expand Down Expand Up @@ -1572,8 +1571,7 @@ fn confirm_slot_entries(
recyclers.clone(),
Arc::new(verify_transaction),
);
let transaction_cpu_duration_us =
timing::duration_as_us(&transaction_verification_start.elapsed());
let transaction_cpu_duration_us = transaction_verification_start.elapsed().as_micros() as u64;

let mut transaction_verification_result = match transaction_verification_result {
Ok(transaction_verification_result) => transaction_verification_result,
Expand Down
4 changes: 2 additions & 2 deletions local-cluster/src/cluster_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use {
pubkey::Pubkey,
signature::{Keypair, Signature, Signer},
system_transaction,
timing::{duration_as_ms, timestamp},
timing::timestamp,
transaction::Transaction,
transport::TransportError,
},
Expand Down Expand Up @@ -231,7 +231,7 @@ pub fn sleep_n_epochs(
ticks_per_slot: u64,
slots_per_epoch: u64,
) {
let num_ticks_per_second = (1000 / duration_as_ms(&config.target_tick_duration)) as f64;
let num_ticks_per_second = config.target_tick_duration.as_secs_f64().recip();
let num_ticks_to_sleep = num_epochs * ticks_per_slot as f64 * slots_per_epoch as f64;
let secs = ((num_ticks_to_sleep + num_ticks_per_second - 1.0) / num_ticks_per_second) as u64;
warn!("sleep_n_epochs: {} seconds", secs);
Expand Down
2 changes: 1 addition & 1 deletion measure/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ macro_rules! measure_us {
($val:expr) => {{
let start = std::time::Instant::now();
let result = $val;
(result, solana_sdk::timing::duration_as_us(&start.elapsed()))
(result, start.elapsed().as_micros() as u64)
}};
}

Expand Down
19 changes: 8 additions & 11 deletions measure/src/measure.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use {
solana_sdk::timing::{duration_as_ms, duration_as_ns, duration_as_s, duration_as_us},
std::{
fmt,
time::{Duration, Instant},
},
use std::{
fmt,
time::{Duration, Instant},
};

#[derive(Debug)]
Expand All @@ -23,7 +20,7 @@ impl Measure {
}

pub fn stop(&mut self) {
self.duration = duration_as_ns(&self.start.elapsed());
self.duration = self.start.elapsed().as_nanos() as u64;
}

pub fn as_ns(&self) -> u64 {
Expand All @@ -47,19 +44,19 @@ impl Measure {
}

pub fn end_as_ns(self) -> u64 {
duration_as_ns(&self.start.elapsed())
self.start.elapsed().as_nanos() as u64
}

pub fn end_as_us(self) -> u64 {
duration_as_us(&self.start.elapsed())
self.start.elapsed().as_micros() as u64
}

pub fn end_as_ms(self) -> u64 {
duration_as_ms(&self.start.elapsed())
self.start.elapsed().as_millis() as u64
}

pub fn end_as_s(self) -> f32 {
duration_as_s(&self.start.elapsed())
self.start.elapsed().as_secs_f32()
}

pub fn end_as_duration(self) -> Duration {
Expand Down
4 changes: 2 additions & 2 deletions poh/src/poh_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ mod tests {
solana_measure::measure::Measure,
solana_perf::test_tx::test_tx,
solana_runtime::bank::Bank,
solana_sdk::{clock, hash::hash, timing, transaction::VersionedTransaction},
solana_sdk::{clock, hash::hash, transaction::VersionedTransaction},
std::{thread::sleep, time::Duration},
};

Expand All @@ -402,7 +402,7 @@ mod tests {
.expect("Expected to be able to open database ledger");

let default_target_tick_duration =
timing::duration_as_us(&PohConfig::default().target_tick_duration);
PohConfig::default().target_tick_duration.as_micros() as u64;
let target_tick_duration = Duration::from_micros(default_target_tick_duration);
let poh_config = PohConfig {
hashes_per_tick: Some(clock::DEFAULT_HASHES_PER_TICK),
Expand Down
Loading

0 comments on commit ae18213

Please sign in to comment.