Use AtomicU64 now that it's stabilized (#6222)

This commit is contained in:
Ryo Onodera 2019-10-04 12:02:28 +09:00 committed by Michael Vines
parent a05d772aa9
commit ffae4662bc
2 changed files with 49 additions and 58 deletions

View File

@ -12,8 +12,7 @@ use crate::{
message_processor::{MessageProcessor, ProcessInstruction}, message_processor::{MessageProcessor, ProcessInstruction},
rent_collector::RentCollector, rent_collector::RentCollector,
serde_utils::{ serde_utils::{
deserialize_atomicbool, deserialize_atomicusize, serialize_atomicbool, deserialize_atomicbool, deserialize_atomicu64, serialize_atomicbool, serialize_atomicu64,
serialize_atomicusize,
}, },
stakes::Stakes, stakes::Stakes,
status_cache::{SlotDelta, StatusCache}, status_cache::{SlotDelta, StatusCache},
@ -52,7 +51,7 @@ use solana_sdk::{
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{BufReader, Cursor, Error as IOError, Read}; use std::io::{BufReader, Cursor, Error as IOError, Read};
use std::path::Path; use std::path::Path;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, RwLock, RwLockReadGuard}; use std::sync::{Arc, RwLock, RwLockReadGuard};
pub const SECONDS_PER_YEAR: f64 = (365.25 * 24.0 * 60.0 * 60.0); pub const SECONDS_PER_YEAR: f64 = (365.25 * 24.0 * 60.0 * 60.0);
@ -177,24 +176,24 @@ pub struct Bank {
parent_hash: Hash, parent_hash: Hash,
/// The number of transactions processed without error /// The number of transactions processed without error
#[serde(serialize_with = "serialize_atomicusize")] #[serde(serialize_with = "serialize_atomicu64")]
#[serde(deserialize_with = "deserialize_atomicusize")] #[serde(deserialize_with = "deserialize_atomicu64")]
transaction_count: AtomicUsize, // TODO: Use AtomicU64 if/when available transaction_count: AtomicU64,
/// Bank tick height /// Bank tick height
#[serde(serialize_with = "serialize_atomicusize")] #[serde(serialize_with = "serialize_atomicu64")]
#[serde(deserialize_with = "deserialize_atomicusize")] #[serde(deserialize_with = "deserialize_atomicu64")]
tick_height: AtomicUsize, // TODO: Use AtomicU64 if/when available tick_height: AtomicU64,
/// The number of signatures from valid transactions in this slot /// The number of signatures from valid transactions in this slot
#[serde(serialize_with = "serialize_atomicusize")] #[serde(serialize_with = "serialize_atomicu64")]
#[serde(deserialize_with = "deserialize_atomicusize")] #[serde(deserialize_with = "deserialize_atomicu64")]
signature_count: AtomicUsize, signature_count: AtomicU64,
/// Total capitalization, used to calculate inflation /// Total capitalization, used to calculate inflation
#[serde(serialize_with = "serialize_atomicusize")] #[serde(serialize_with = "serialize_atomicu64")]
#[serde(deserialize_with = "deserialize_atomicusize")] #[serde(deserialize_with = "deserialize_atomicu64")]
capitalization: AtomicUsize, // TODO: Use AtomicU64 if/when available capitalization: AtomicU64,
// Bank max_tick_height // Bank max_tick_height
max_tick_height: u64, max_tick_height: u64,
@ -221,9 +220,9 @@ pub struct Bank {
collector_id: Pubkey, collector_id: Pubkey,
/// Fees that have been collected /// Fees that have been collected
#[serde(serialize_with = "serialize_atomicusize")] #[serde(serialize_with = "serialize_atomicu64")]
#[serde(deserialize_with = "deserialize_atomicusize")] #[serde(deserialize_with = "deserialize_atomicu64")]
collector_fees: AtomicUsize, // TODO: Use AtomicU64 if/when available collector_fees: AtomicU64,
/// Latest transaction fees for transactions processed by this bank /// Latest transaction fees for transactions processed by this bank
fee_calculator: FeeCalculator, fee_calculator: FeeCalculator,
@ -324,22 +323,22 @@ impl Bank {
block_height: parent.block_height + 1, block_height: parent.block_height + 1,
fee_calculator: FeeCalculator::new_derived( fee_calculator: FeeCalculator::new_derived(
&parent.fee_calculator, &parent.fee_calculator,
parent.signature_count(), parent.signature_count() as usize,
), ),
capitalization: AtomicUsize::new(parent.capitalization() as usize), capitalization: AtomicU64::new(parent.capitalization()),
inflation: parent.inflation, inflation: parent.inflation,
transaction_count: AtomicUsize::new(parent.transaction_count() as usize), transaction_count: AtomicU64::new(parent.transaction_count()),
stakes: RwLock::new(parent.stakes.read().unwrap().clone_with_epoch(epoch)), stakes: RwLock::new(parent.stakes.read().unwrap().clone_with_epoch(epoch)),
epoch_stakes: parent.epoch_stakes.clone(), epoch_stakes: parent.epoch_stakes.clone(),
storage_accounts: RwLock::new(parent.storage_accounts.read().unwrap().clone()), storage_accounts: RwLock::new(parent.storage_accounts.read().unwrap().clone()),
parent_hash: parent.hash(), parent_hash: parent.hash(),
collector_id: *collector_id, collector_id: *collector_id,
collector_fees: AtomicUsize::new(0), collector_fees: AtomicU64::new(0),
ancestors: HashMap::new(), ancestors: HashMap::new(),
hash: RwLock::new(Hash::default()), hash: RwLock::new(Hash::default()),
is_delta: AtomicBool::new(false), is_delta: AtomicBool::new(false),
tick_height: AtomicUsize::new(parent.tick_height.load(Ordering::Relaxed)), tick_height: AtomicU64::new(parent.tick_height.load(Ordering::Relaxed)),
signature_count: AtomicUsize::new(0), signature_count: AtomicU64::new(0),
message_processor: MessageProcessor::default(), message_processor: MessageProcessor::default(),
}; };
@ -507,7 +506,7 @@ impl Bank {
); );
self.capitalization.fetch_add( self.capitalization.fetch_add(
(validator_rewards + storage_rewards) as usize, (validator_rewards + storage_rewards) as u64,
Ordering::Relaxed, Ordering::Relaxed,
); );
} }
@ -541,8 +540,7 @@ impl Bank {
let (unburned, burned) = self.fee_calculator.burn(collector_fees); let (unburned, burned) = self.fee_calculator.burn(collector_fees);
// burn a portion of fees // burn a portion of fees
self.deposit(&self.collector_id, unburned); self.deposit(&self.collector_id, unburned);
self.capitalization self.capitalization.fetch_sub(burned, Ordering::Relaxed);
.fetch_sub(burned as usize, Ordering::Relaxed);
} }
} }
@ -615,7 +613,7 @@ impl Bank {
for (pubkey, account) in genesis_block.accounts.iter() { for (pubkey, account) in genesis_block.accounts.iter() {
self.store_account(pubkey, account); self.store_account(pubkey, account);
self.capitalization self.capitalization
.fetch_add(account.lamports as usize, Ordering::Relaxed); .fetch_add(account.lamports, Ordering::Relaxed);
} }
for (pubkey, account) in genesis_block.rewards_pools.iter() { for (pubkey, account) in genesis_block.rewards_pools.iter() {
self.store_account(pubkey, account); self.store_account(pubkey, account);
@ -1023,8 +1021,8 @@ impl Bank {
Vec<Result<TransactionLoadResult>>, Vec<Result<TransactionLoadResult>>,
Vec<Result<()>>, Vec<Result<()>>,
Vec<usize>, Vec<usize>,
usize, u64,
usize, u64,
) { ) {
let txs = batch.transactions(); let txs = batch.transactions();
debug!("processing transactions: {}", txs.len()); debug!("processing transactions: {}", txs.len());
@ -1058,14 +1056,14 @@ impl Bank {
load_time.stop(); load_time.stop();
let mut execution_time = Measure::start("execution_time"); let mut execution_time = Measure::start("execution_time");
let mut signature_count = 0; let mut signature_count: u64 = 0;
let executed: Vec<Result<()>> = loaded_accounts let executed: Vec<Result<()>> = loaded_accounts
.iter_mut() .iter_mut()
.zip(OrderedIterator::new(txs, batch.iteration_order())) .zip(OrderedIterator::new(txs, batch.iteration_order()))
.map(|(accs, tx)| match accs { .map(|(accs, tx)| match accs {
Err(e) => Err(e.clone()), Err(e) => Err(e.clone()),
Ok((ref mut accounts, ref mut loaders, ref mut credits, ref mut _rents)) => { Ok((ref mut accounts, ref mut loaders, ref mut credits, ref mut _rents)) => {
signature_count += tx.message().header.num_required_signatures as usize; signature_count += u64::from(tx.message().header.num_required_signatures);
self.message_processor self.message_processor
.process_message(tx.message(), loaders, accounts, credits) .process_message(tx.message(), loaders, accounts, credits)
} }
@ -1080,7 +1078,7 @@ impl Bank {
execution_time.as_us(), execution_time.as_us(),
txs.len(), txs.len(),
); );
let mut tx_count = 0; let mut tx_count: u64 = 0;
let mut err_count = 0; let mut err_count = 0;
for (r, tx) in executed.iter().zip(txs.iter()) { for (r, tx) in executed.iter().zip(txs.iter()) {
if r.is_ok() { if r.is_ok() {
@ -1098,7 +1096,7 @@ impl Bank {
"bank-process_transactions-account_not_found", "bank-process_transactions-account_not_found",
error_counters.account_not_found error_counters.account_not_found
); );
inc_new_counter_error!("bank-process_transactions-error_count", err_count); inc_new_counter_error!("bank-process_transactions-error_count", err_count as usize);
} }
Self::update_error_counters(&error_counters); Self::update_error_counters(&error_counters);
@ -1146,8 +1144,7 @@ impl Bank {
}) })
.collect(); .collect();
self.collector_fees self.collector_fees.fetch_add(fees, Ordering::Relaxed);
.fetch_add(fees as usize, Ordering::Relaxed);
results results
} }
@ -1157,8 +1154,8 @@ impl Bank {
iteration_order: Option<&[usize]>, iteration_order: Option<&[usize]>,
loaded_accounts: &mut [Result<TransactionLoadResult>], loaded_accounts: &mut [Result<TransactionLoadResult>],
executed: &[Result<()>], executed: &[Result<()>],
tx_count: usize, tx_count: u64,
signature_count: usize, signature_count: u64,
) -> Vec<Result<()>> { ) -> Vec<Result<()>> {
if self.is_frozen() { if self.is_frozen() {
warn!("=========== FIXME: commit_transactions() working on a frozen bank! ================"); warn!("=========== FIXME: commit_transactions() working on a frozen bank! ================");
@ -1167,8 +1164,8 @@ impl Bank {
self.increment_transaction_count(tx_count); self.increment_transaction_count(tx_count);
self.increment_signature_count(signature_count); self.increment_signature_count(signature_count);
inc_new_counter_info!("bank-process_transactions-txs", tx_count); inc_new_counter_info!("bank-process_transactions-txs", tx_count as usize);
inc_new_counter_info!("bank-process_transactions-sigs", signature_count); inc_new_counter_info!("bank-process_transactions-sigs", signature_count as usize);
if executed.iter().any(|res| Self::can_commit(res)) { if executed.iter().any(|res| Self::can_commit(res)) {
self.is_delta.store(true, Ordering::Relaxed); self.is_delta.store(true, Ordering::Relaxed);
@ -1327,19 +1324,19 @@ impl Bank {
} }
pub fn transaction_count(&self) -> u64 { pub fn transaction_count(&self) -> u64 {
self.transaction_count.load(Ordering::Relaxed) as u64 self.transaction_count.load(Ordering::Relaxed)
} }
fn increment_transaction_count(&self, tx_count: usize) { fn increment_transaction_count(&self, tx_count: u64) {
self.transaction_count self.transaction_count
.fetch_add(tx_count, Ordering::Relaxed); .fetch_add(tx_count, Ordering::Relaxed);
} }
pub fn signature_count(&self) -> usize { pub fn signature_count(&self) -> u64 {
self.signature_count.load(Ordering::Relaxed) self.signature_count.load(Ordering::Relaxed)
} }
fn increment_signature_count(&self, signature_count: usize) { fn increment_signature_count(&self, signature_count: u64) {
self.signature_count self.signature_count
.fetch_add(signature_count, Ordering::Relaxed); .fetch_add(signature_count, Ordering::Relaxed);
} }
@ -1400,10 +1397,7 @@ impl Bank {
/// Return the number of ticks since genesis. /// Return the number of ticks since genesis.
pub fn tick_height(&self) -> u64 { pub fn tick_height(&self) -> u64 {
// tick_height is using an AtomicUSize because AtomicU64 is not yet a stable API. self.tick_height.load(Ordering::Relaxed)
// Until we can switch to AtomicU64, fail if usize is not the same as u64
assert_eq!(std::usize::MAX, 0xFFFF_FFFF_FFFF_FFFF);
self.tick_height.load(Ordering::Relaxed) as u64
} }
/// Return the inflation parameters of the Bank /// Return the inflation parameters of the Bank
@ -1413,10 +1407,7 @@ impl Bank {
/// Return the total capititalization of the Bank /// Return the total capititalization of the Bank
pub fn capitalization(&self) -> u64 { pub fn capitalization(&self) -> u64 {
// capitalization is using an AtomicUSize because AtomicU64 is not yet a stable API. self.capitalization.load(Ordering::Relaxed)
// Until we can switch to AtomicU64, fail if usize is not the same as u64
assert_eq!(std::usize::MAX, 0xFFFF_FFFF_FFFF_FFFF);
self.capitalization.load(Ordering::Relaxed) as u64
} }
/// Return this bank's max_tick_height /// Return this bank's max_tick_height
@ -2927,7 +2918,7 @@ mod tests {
let (genesis_block, _) = create_genesis_block(500); let (genesis_block, _) = create_genesis_block(500);
let bank = Arc::new(Bank::new(&genesis_block)); let bank = Arc::new(Bank::new(&genesis_block));
//set tick height to max //set tick height to max
let max_tick_height = ((bank.slot + 1) * bank.ticks_per_slot - 1) as usize; let max_tick_height = (bank.slot + 1) * bank.ticks_per_slot - 1;
bank.tick_height.store(max_tick_height, Ordering::Relaxed); bank.tick_height.store(max_tick_height, Ordering::Relaxed);
assert!(bank.is_votable()); assert!(bank.is_votable());
} }

View File

@ -1,5 +1,5 @@
use std::fmt; use std::fmt;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
struct U64Visitor; struct U64Visitor;
impl<'a> serde::de::Visitor<'a> for U64Visitor { impl<'a> serde::de::Visitor<'a> for U64Visitor {
@ -16,19 +16,19 @@ impl<'a> serde::de::Visitor<'a> for U64Visitor {
} }
} }
pub fn deserialize_atomicusize<'de, D>(d: D) -> Result<AtomicUsize, D::Error> pub fn deserialize_atomicu64<'de, D>(d: D) -> Result<AtomicU64, D::Error>
where where
D: serde::de::Deserializer<'de>, D: serde::de::Deserializer<'de>,
{ {
let value = d.deserialize_u64(U64Visitor)?; let value = d.deserialize_u64(U64Visitor)?;
Ok(AtomicUsize::new(value as usize)) Ok(AtomicU64::new(value))
} }
pub fn serialize_atomicusize<S>(x: &AtomicUsize, s: S) -> Result<S::Ok, S::Error> pub fn serialize_atomicu64<S>(x: &AtomicU64, s: S) -> Result<S::Ok, S::Error>
where where
S: serde::Serializer, S: serde::Serializer,
{ {
s.serialize_u64(x.load(Ordering::Relaxed) as u64) s.serialize_u64(x.load(Ordering::Relaxed))
} }
struct BoolVisitor; struct BoolVisitor;