Improve poh.rs readability (#30298)

* chore: factor out variable hashes per tick to a constant

* chore: add units to `elapsed` in `compute_hashes_per_tick`
This commit is contained in:
Proph3t 2023-04-03 19:12:41 +00:00 committed by GitHub
parent f289a0726b
commit ab8d3066a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 12 deletions

View File

@ -5,6 +5,8 @@ use {
std::time::{Duration, Instant}, std::time::{Duration, Instant},
}; };
const LOW_POWER_MODE: u64 = std::u64::MAX;
pub struct Poh { pub struct Poh {
pub hash: Hash, pub hash: Hash,
num_hashes: u64, num_hashes: u64,
@ -26,7 +28,7 @@ impl Poh {
} }
pub fn new_with_slot_info(hash: Hash, hashes_per_tick: Option<u64>, tick_number: u64) -> Self { pub fn new_with_slot_info(hash: Hash, hashes_per_tick: Option<u64>, tick_number: u64) -> Self {
let hashes_per_tick = hashes_per_tick.unwrap_or(std::u64::MAX); let hashes_per_tick = hashes_per_tick.unwrap_or(LOW_POWER_MODE);
assert!(hashes_per_tick > 1); assert!(hashes_per_tick > 1);
let now = Instant::now(); let now = Instant::now();
Poh { Poh {
@ -90,9 +92,9 @@ impl Poh {
self.num_hashes += 1; self.num_hashes += 1;
self.remaining_hashes -= 1; self.remaining_hashes -= 1;
// If the hashes_per_tick is variable (std::u64::MAX) then always generate a tick. // If we are in low power mode then always generate a tick.
// Otherwise only tick if there are no remaining hashes // Otherwise only tick if there are no remaining hashes
if self.hashes_per_tick < std::u64::MAX && self.remaining_hashes != 0 { if self.hashes_per_tick != LOW_POWER_MODE && self.remaining_hashes != 0 {
return None; return None;
} }
@ -118,8 +120,8 @@ pub fn compute_hash_time_ns(hashes_sample_size: u64) -> u64 {
} }
pub fn compute_hashes_per_tick(duration: Duration, hashes_sample_size: u64) -> u64 { pub fn compute_hashes_per_tick(duration: Duration, hashes_sample_size: u64) -> u64 {
let elapsed = compute_hash_time_ns(hashes_sample_size) / (1000 * 1000); let elapsed_ms = compute_hash_time_ns(hashes_sample_size) / (1000 * 1000);
duration.as_millis() as u64 * hashes_sample_size / elapsed duration.as_millis() as u64 * hashes_sample_size / elapsed_ms
} }
#[cfg(test)] #[cfg(test)]

View File

@ -111,14 +111,14 @@ impl PohService {
solana_sys_tuner::request_realtime_poh(); solana_sys_tuner::request_realtime_poh();
if poh_config.hashes_per_tick.is_none() { if poh_config.hashes_per_tick.is_none() {
if poh_config.target_tick_count.is_none() { if poh_config.target_tick_count.is_none() {
Self::sleepy_tick_producer( Self::low_power_tick_producer(
poh_recorder, poh_recorder,
&poh_config, &poh_config,
&poh_exit_, &poh_exit_,
record_receiver, record_receiver,
); );
} else { } else {
Self::short_lived_sleepy_tick_producer( Self::short_lived_low_power_tick_producer(
poh_recorder, poh_recorder,
&poh_config, &poh_config,
&poh_exit_, &poh_exit_,
@ -162,7 +162,7 @@ impl PohService {
target_tick_duration_ns.saturating_sub(adjustment_per_tick) target_tick_duration_ns.saturating_sub(adjustment_per_tick)
} }
fn sleepy_tick_producer( fn low_power_tick_producer(
poh_recorder: Arc<RwLock<PohRecorder>>, poh_recorder: Arc<RwLock<PohRecorder>>,
poh_config: &PohConfig, poh_config: &PohConfig,
poh_exit: &AtomicBool, poh_exit: &AtomicBool,
@ -206,7 +206,7 @@ impl PohService {
} }
} }
fn short_lived_sleepy_tick_producer( fn short_lived_low_power_tick_producer(
poh_recorder: Arc<RwLock<PohRecorder>>, poh_recorder: Arc<RwLock<PohRecorder>>,
poh_config: &PohConfig, poh_config: &PohConfig,
poh_exit: &AtomicBool, poh_exit: &AtomicBool,

View File

@ -14,9 +14,8 @@ pub struct PohConfig {
pub target_tick_count: Option<u64>, pub target_tick_count: Option<u64>,
/// How many hashes to roll before emitting the next tick entry. /// How many hashes to roll before emitting the next tick entry.
/// None enables "Low power mode", which implies: /// None enables "Low power mode", which makes the validator sleep
/// * sleep for `target_tick_duration` instead of hashing /// for `target_tick_duration` instead of hashing
/// * the number of hashes per tick will be variable
pub hashes_per_tick: Option<u64>, pub hashes_per_tick: Option<u64>,
} }