- move cost tracker into bank, so each bank has its own cost tracker; (#20527)

- move related modules to runtime
This commit is contained in:
Tao Zhu 2021-10-12 08:51:33 -05:00 committed by GitHub
parent a723de0e25
commit 005d6863fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 60 additions and 39 deletions

1
Cargo.lock generated
View File

@ -5480,6 +5480,7 @@ dependencies = [
"ed25519-dalek", "ed25519-dalek",
"flate2", "flate2",
"fnv", "fnv",
"histogram",
"itertools 0.10.1", "itertools 0.10.1",
"lazy_static", "lazy_static",
"libsecp256k1 0.6.0", "libsecp256k1 0.6.0",

View File

@ -4,7 +4,7 @@ use crossbeam_channel::unbounded;
use log::*; use log::*;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use rayon::prelude::*; use rayon::prelude::*;
use solana_core::{banking_stage::BankingStage, cost_model::CostModel, cost_tracker::CostTracker}; use solana_core::banking_stage::BankingStage;
use solana_gossip::{cluster_info::ClusterInfo, cluster_info::Node}; use solana_gossip::{cluster_info::ClusterInfo, cluster_info::Node};
use solana_ledger::{ use solana_ledger::{
blockstore::Blockstore, blockstore::Blockstore,
@ -16,6 +16,7 @@ use solana_perf::packet::to_packets_chunked;
use solana_poh::poh_recorder::{create_test_recorder, PohRecorder, WorkingBankEntry}; use solana_poh::poh_recorder::{create_test_recorder, PohRecorder, WorkingBankEntry};
use solana_runtime::{ use solana_runtime::{
accounts_background_service::AbsRequestSender, bank::Bank, bank_forks::BankForks, accounts_background_service::AbsRequestSender, bank::Bank, bank_forks::BankForks,
cost_model::CostModel, cost_tracker::CostTracker,
}; };
use solana_sdk::{ use solana_sdk::{
hash::Hash, hash::Hash,

View File

@ -8,9 +8,6 @@ use log::*;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use rayon::prelude::*; use rayon::prelude::*;
use solana_core::banking_stage::{BankingStage, BankingStageStats}; use solana_core::banking_stage::{BankingStage, BankingStageStats};
use solana_core::cost_model::CostModel;
use solana_core::cost_tracker::CostTracker;
use solana_core::cost_tracker_stats::CostTrackerStats;
use solana_entry::entry::{next_hash, Entry}; use solana_entry::entry::{next_hash, Entry};
use solana_gossip::cluster_info::ClusterInfo; use solana_gossip::cluster_info::ClusterInfo;
use solana_gossip::cluster_info::Node; use solana_gossip::cluster_info::Node;
@ -21,6 +18,9 @@ use solana_perf::packet::to_packets_chunked;
use solana_perf::test_tx::test_tx; use solana_perf::test_tx::test_tx;
use solana_poh::poh_recorder::{create_test_recorder, WorkingBankEntry}; use solana_poh::poh_recorder::{create_test_recorder, WorkingBankEntry};
use solana_runtime::bank::Bank; use solana_runtime::bank::Bank;
use solana_runtime::cost_model::CostModel;
use solana_runtime::cost_tracker::CostTracker;
use solana_runtime::cost_tracker_stats::CostTrackerStats;
use solana_sdk::genesis_config::GenesisConfig; use solana_sdk::genesis_config::GenesisConfig;
use solana_sdk::hash::Hash; use solana_sdk::hash::Hash;
use solana_sdk::message::Message; use solana_sdk::message::Message;

View File

@ -1,9 +1,7 @@
//! The `banking_stage` processes Transaction messages. It is intended to be used //! The `banking_stage` processes Transaction messages. It is intended to be used
//! to contruct a software pipeline. The stage uses all available CPU cores and //! to contruct a software pipeline. The stage uses all available CPU cores and
//! can do its processing in parallel with signature verification on the GPU. //! can do its processing in parallel with signature verification on the GPU.
use crate::{ use crate::packet_hasher::PacketHasher;
cost_tracker::CostTracker, cost_tracker_stats::CostTrackerStats, packet_hasher::PacketHasher,
};
use crossbeam_channel::{Receiver as CrossbeamReceiver, RecvTimeoutError}; use crossbeam_channel::{Receiver as CrossbeamReceiver, RecvTimeoutError};
use itertools::Itertools; use itertools::Itertools;
use lru::LruCache; use lru::LruCache;
@ -27,6 +25,8 @@ use solana_runtime::{
TransactionExecutionResult, TransactionExecutionResult,
}, },
bank_utils, bank_utils,
cost_tracker::CostTracker,
cost_tracker_stats::CostTrackerStats,
transaction_batch::TransactionBatch, transaction_batch::TransactionBatch,
vote_sender_types::ReplayVoteSender, vote_sender_types::ReplayVoteSender,
}; };
@ -1697,7 +1697,6 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::cost_model::CostModel;
use crossbeam_channel::unbounded; use crossbeam_channel::unbounded;
use itertools::Itertools; use itertools::Itertools;
use solana_entry::entry::{next_entry, Entry, EntrySlice}; use solana_entry::entry::{next_entry, Entry, EntrySlice};
@ -1714,6 +1713,7 @@ mod tests {
poh_service::PohService, poh_service::PohService,
}; };
use solana_rpc::transaction_status_service::TransactionStatusService; use solana_rpc::transaction_status_service::TransactionStatusService;
use solana_runtime::cost_model::CostModel;
use solana_sdk::{ use solana_sdk::{
hash::Hash, hash::Hash,
instruction::InstructionError, instruction::InstructionError,

View File

@ -3,10 +3,9 @@
//! packing transactions into block; it also triggers persisting cost //! packing transactions into block; it also triggers persisting cost
//! table to blockstore. //! table to blockstore.
use crate::cost_model::CostModel;
use solana_ledger::blockstore::Blockstore; use solana_ledger::blockstore::Blockstore;
use solana_measure::measure::Measure; use solana_measure::measure::Measure;
use solana_runtime::bank::ExecuteTimings; use solana_runtime::{bank::ExecuteTimings, cost_model::CostModel};
use solana_sdk::timing::timestamp; use solana_sdk::timing::timestamp;
use std::{ use std::{
sync::{ sync::{

View File

@ -20,12 +20,8 @@ pub mod cluster_slots_service;
pub mod commitment_service; pub mod commitment_service;
pub mod completed_data_sets_service; pub mod completed_data_sets_service;
pub mod consensus; pub mod consensus;
pub mod cost_model;
pub mod cost_tracker;
pub mod cost_tracker_stats;
pub mod cost_update_service; pub mod cost_update_service;
pub mod duplicate_repair_status; pub mod duplicate_repair_status;
pub mod execute_cost_table;
pub mod fetch_stage; pub mod fetch_stage;
pub mod fork_choice; pub mod fork_choice;
pub mod gen_keys; pub mod gen_keys;

View File

@ -8,8 +8,6 @@ use crate::{
ClusterInfoVoteListener, GossipDuplicateConfirmedSlotsSender, GossipVerifiedVoteHashSender, ClusterInfoVoteListener, GossipDuplicateConfirmedSlotsSender, GossipVerifiedVoteHashSender,
VerifiedVoteSender, VoteTracker, VerifiedVoteSender, VoteTracker,
}, },
cost_model::CostModel,
cost_tracker::CostTracker,
fetch_stage::FetchStage, fetch_stage::FetchStage,
sigverify::TransactionSigVerifier, sigverify::TransactionSigVerifier,
sigverify_stage::SigVerifyStage, sigverify_stage::SigVerifyStage,
@ -24,6 +22,8 @@ use solana_rpc::{
}; };
use solana_runtime::{ use solana_runtime::{
bank_forks::BankForks, bank_forks::BankForks,
cost_model::CostModel,
cost_tracker::CostTracker,
vote_sender_types::{ReplayVoteReceiver, ReplayVoteSender}, vote_sender_types::{ReplayVoteReceiver, ReplayVoteSender},
}; };
use std::{ use std::{

View File

@ -12,7 +12,6 @@ use crate::{
cluster_slots::ClusterSlots, cluster_slots::ClusterSlots,
completed_data_sets_service::CompletedDataSetsSender, completed_data_sets_service::CompletedDataSetsSender,
consensus::Tower, consensus::Tower,
cost_model::CostModel,
cost_update_service::CostUpdateService, cost_update_service::CostUpdateService,
ledger_cleanup_service::LedgerCleanupService, ledger_cleanup_service::LedgerCleanupService,
replay_stage::{ReplayStage, ReplayStageConfig}, replay_stage::{ReplayStage, ReplayStageConfig},
@ -43,6 +42,7 @@ use solana_runtime::{
bank::ExecuteTimings, bank::ExecuteTimings,
bank_forks::BankForks, bank_forks::BankForks,
commitment::BlockCommitmentCache, commitment::BlockCommitmentCache,
cost_model::CostModel,
snapshot_config::SnapshotConfig, snapshot_config::SnapshotConfig,
snapshot_package::{AccountsPackageReceiver, AccountsPackageSender, PendingSnapshotPackage}, snapshot_package::{AccountsPackageReceiver, AccountsPackageSender, PendingSnapshotPackage},
vote_sender_types::ReplayVoteSender, vote_sender_types::ReplayVoteSender,

View File

@ -8,7 +8,6 @@ use {
cluster_info_vote_listener::VoteTracker, cluster_info_vote_listener::VoteTracker,
completed_data_sets_service::CompletedDataSetsService, completed_data_sets_service::CompletedDataSetsService,
consensus::{reconcile_blockstore_roots_with_tower, Tower}, consensus::{reconcile_blockstore_roots_with_tower, Tower},
cost_model::CostModel,
rewards_recorder_service::{RewardsRecorderSender, RewardsRecorderService}, rewards_recorder_service::{RewardsRecorderSender, RewardsRecorderService},
sample_performance_service::SamplePerformanceService, sample_performance_service::SamplePerformanceService,
serve_repair::ServeRepair, serve_repair::ServeRepair,
@ -69,6 +68,7 @@ use {
bank::Bank, bank::Bank,
bank_forks::BankForks, bank_forks::BankForks,
commitment::BlockCommitmentCache, commitment::BlockCommitmentCache,
cost_model::CostModel,
hardened_unpack::{open_genesis_config, MAX_GENESIS_ARCHIVE_UNPACKED_SIZE}, hardened_unpack::{open_genesis_config, MAX_GENESIS_ARCHIVE_UNPACKED_SIZE},
snapshot_archive_info::SnapshotArchiveInfoGetter, snapshot_archive_info::SnapshotArchiveInfoGetter,
snapshot_config::SnapshotConfig, snapshot_config::SnapshotConfig,

View File

@ -15,9 +15,6 @@ use solana_clap_utils::{
is_bin, is_parsable, is_pubkey, is_pubkey_or_keypair, is_slot, is_valid_percentage, is_bin, is_parsable, is_pubkey, is_pubkey_or_keypair, is_slot, is_valid_percentage,
}, },
}; };
use solana_core::cost_model::CostModel;
use solana_core::cost_tracker::CostTracker;
use solana_core::cost_tracker_stats::CostTrackerStats;
use solana_entry::entry::Entry; use solana_entry::entry::Entry;
use solana_ledger::{ use solana_ledger::{
ancestor_iterator::AncestorIterator, ancestor_iterator::AncestorIterator,
@ -32,6 +29,9 @@ use solana_runtime::{
accounts_index::AccountsIndexConfig, accounts_index::AccountsIndexConfig,
bank::{Bank, RewardCalculationEvent}, bank::{Bank, RewardCalculationEvent},
bank_forks::BankForks, bank_forks::BankForks,
cost_model::CostModel,
cost_tracker::CostTracker,
cost_tracker_stats::CostTrackerStats,
hardened_unpack::{open_genesis_config, MAX_GENESIS_ARCHIVE_UNPACKED_SIZE}, hardened_unpack::{open_genesis_config, MAX_GENESIS_ARCHIVE_UNPACKED_SIZE},
snapshot_archive_info::SnapshotArchiveInfoGetter, snapshot_archive_info::SnapshotArchiveInfoGetter,
snapshot_config::SnapshotConfig, snapshot_config::SnapshotConfig,

View File

@ -1,7 +1,6 @@
use crate::{ use crate::{
block_cost_limits::*, block_error::BlockError, blockstore::Blockstore, block_error::BlockError, blockstore::Blockstore, blockstore_db::BlockstoreError,
blockstore_db::BlockstoreError, blockstore_meta::SlotMeta, blockstore_meta::SlotMeta, leader_schedule_cache::LeaderScheduleCache,
leader_schedule_cache::LeaderScheduleCache,
}; };
use chrono_humanize::{Accuracy, HumanTime, Tense}; use chrono_humanize::{Accuracy, HumanTime, Tense};
use crossbeam_channel::Sender; use crossbeam_channel::Sender;
@ -25,6 +24,7 @@ use solana_runtime::{
}, },
bank_forks::BankForks, bank_forks::BankForks,
bank_utils, bank_utils,
block_cost_limits::*,
commitment::VOTE_THRESHOLD_SIZE, commitment::VOTE_THRESHOLD_SIZE,
snapshot_config::SnapshotConfig, snapshot_config::SnapshotConfig,
snapshot_package::{AccountsPackageSender, SnapshotType}, snapshot_package::{AccountsPackageSender, SnapshotType},

View File

@ -11,7 +11,6 @@ pub mod block_error;
#[macro_use] #[macro_use]
pub mod blockstore; pub mod blockstore;
pub mod ancestor_iterator; pub mod ancestor_iterator;
pub mod block_cost_limits;
pub mod blockstore_db; pub mod blockstore_db;
pub mod blockstore_meta; pub mod blockstore_meta;
pub mod blockstore_processor; pub mod blockstore_processor;

View File

@ -1112,6 +1112,12 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "histogram"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12cb882ccb290b8646e554b157ab0b71e64e8d5bef775cd66b6531e52d302669"
[[package]] [[package]]
name = "hmac" name = "hmac"
version = "0.8.1" version = "0.8.1"
@ -3242,6 +3248,7 @@ dependencies = [
"dir-diff", "dir-diff",
"flate2", "flate2",
"fnv", "fnv",
"histogram",
"itertools 0.10.1", "itertools 0.10.1",
"lazy_static", "lazy_static",
"log", "log",

View File

@ -21,6 +21,7 @@ crossbeam-channel = "0.5"
dir-diff = "0.3.2" dir-diff = "0.3.2"
flate2 = "1.0.22" flate2 = "1.0.22"
fnv = "1.0.7" fnv = "1.0.7"
histogram = "0.6.9"
itertools = "0.10.1" itertools = "0.10.1"
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4.14" log = "0.4.14"

View File

@ -4,9 +4,8 @@
//! //!
//! The main function is `calculate_cost` which returns &TransactionCost. //! The main function is `calculate_cost` which returns &TransactionCost.
//! //!
use crate::execute_cost_table::ExecuteCostTable; use crate::{block_cost_limits::*, execute_cost_table::ExecuteCostTable};
use log::*; use log::*;
use solana_ledger::block_cost_limits::*;
use solana_sdk::{pubkey::Pubkey, transaction::SanitizedTransaction}; use solana_sdk::{pubkey::Pubkey, transaction::SanitizedTransaction};
use std::collections::HashMap; use std::collections::HashMap;
@ -25,7 +24,7 @@ pub enum CostModelError {
WouldExceedAccountMaxLimit, WouldExceedAccountMaxLimit,
} }
#[derive(Default, Debug)] #[derive(AbiExample, Default, Debug)]
pub struct TransactionCost { pub struct TransactionCost {
pub writable_accounts: Vec<Pubkey>, pub writable_accounts: Vec<Pubkey>,
pub signature_cost: u64, pub signature_cost: u64,
@ -55,7 +54,7 @@ impl TransactionCost {
} }
} }
#[derive(Debug)] #[derive(AbiExample, Debug)]
pub struct CostModel { pub struct CostModel {
account_cost_limit: u64, account_cost_limit: u64,
block_cost_limit: u64, block_cost_limit: u64,
@ -219,7 +218,7 @@ impl CostModel {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use solana_runtime::{ use crate::{
bank::Bank, bank::Bank,
genesis_utils::{create_genesis_config, GenesisConfigInfo}, genesis_utils::{create_genesis_config, GenesisConfigInfo},
}; };

View File

@ -14,7 +14,7 @@ use std::{
const WRITABLE_ACCOUNTS_PER_BLOCK: usize = 512; const WRITABLE_ACCOUNTS_PER_BLOCK: usize = 512;
#[derive(Debug)] #[derive(AbiExample, Debug)]
pub struct CostTracker { pub struct CostTracker {
cost_model: Arc<RwLock<CostModel>>, cost_model: Arc<RwLock<CostModel>>,
account_cost_limit: u64, account_cost_limit: u64,
@ -24,6 +24,12 @@ pub struct CostTracker {
block_cost: u64, block_cost: u64,
} }
impl Default for CostTracker {
fn default() -> Self {
CostTracker::new(Arc::new(RwLock::new(CostModel::default())))
}
}
impl CostTracker { impl CostTracker {
pub fn new(cost_model: Arc<RwLock<CostModel>>) -> Self { pub fn new(cost_model: Arc<RwLock<CostModel>>) -> Self {
let (account_cost_limit, block_cost_limit) = { let (account_cost_limit, block_cost_limit) = {
@ -191,7 +197,7 @@ impl CostTracker {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use solana_runtime::{ use crate::{
bank::Bank, bank::Bank,
genesis_utils::{create_genesis_config, GenesisConfigInfo}, genesis_utils::{create_genesis_config, GenesisConfigInfo},
}; };

View File

@ -5,7 +5,7 @@
/// to make room for new ones. /// to make room for new ones.
use log::*; use log::*;
use solana_sdk::pubkey::Pubkey; use solana_sdk::pubkey::Pubkey;
use std::{collections::HashMap, time::SystemTime}; use std::collections::HashMap;
// prune is rather expensive op, free up bulk space in each operation // prune is rather expensive op, free up bulk space in each operation
// would be more efficient. PRUNE_RATIO defines the after prune table // would be more efficient. PRUNE_RATIO defines the after prune table
@ -16,11 +16,11 @@ const OCCURRENCES_WEIGHT: i64 = 100;
const DEFAULT_CAPACITY: usize = 1024; const DEFAULT_CAPACITY: usize = 1024;
#[derive(Debug)] #[derive(AbiExample, Debug)]
pub struct ExecuteCostTable { pub struct ExecuteCostTable {
capacity: usize, capacity: usize,
table: HashMap<Pubkey, u64>, table: HashMap<Pubkey, u64>,
occurrences: HashMap<Pubkey, (usize, SystemTime)>, occurrences: HashMap<Pubkey, (usize, u128)>,
} }
impl Default for ExecuteCostTable { impl Default for ExecuteCostTable {
@ -91,9 +91,9 @@ impl ExecuteCostTable {
let (count, timestamp) = self let (count, timestamp) = self
.occurrences .occurrences
.entry(*key) .entry(*key)
.or_insert((0, SystemTime::now())); .or_insert((0, Self::micros_since_epoch()));
*count += 1; *count += 1;
*timestamp = SystemTime::now(); *timestamp = Self::micros_since_epoch();
Some(*program_cost) Some(*program_cost)
} }
@ -120,12 +120,12 @@ impl ExecuteCostTable {
return; return;
} }
let now = SystemTime::now(); let now = Self::micros_since_epoch();
let mut sorted_by_weighted_age: Vec<_> = self let mut sorted_by_weighted_age: Vec<_> = self
.occurrences .occurrences
.iter() .iter()
.map(|(key, (count, timestamp))| { .map(|(key, (count, timestamp))| {
let age = now.duration_since(*timestamp).unwrap().as_micros(); let age = now - timestamp;
let weighted_age = *count as i64 * OCCURRENCES_WEIGHT + -(age as i64); let weighted_age = *count as i64 * OCCURRENCES_WEIGHT + -(age as i64);
(weighted_age, *key) (weighted_age, *key)
}) })
@ -140,6 +140,13 @@ impl ExecuteCostTable {
} }
} }
} }
fn micros_since_epoch() -> u128 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_micros()
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -14,6 +14,7 @@ pub mod bank;
pub mod bank_client; pub mod bank_client;
pub mod bank_forks; pub mod bank_forks;
pub mod bank_utils; pub mod bank_utils;
pub mod block_cost_limits;
pub mod blockhash_queue; pub mod blockhash_queue;
pub mod bloom; pub mod bloom;
pub mod bucket_map_holder; pub mod bucket_map_holder;
@ -23,7 +24,11 @@ pub mod cache_hash_data;
pub mod cache_hash_data_stats; pub mod cache_hash_data_stats;
pub mod commitment; pub mod commitment;
pub mod contains; pub mod contains;
pub mod cost_model;
pub mod cost_tracker;
pub mod cost_tracker_stats;
pub mod epoch_stakes; pub mod epoch_stakes;
pub mod execute_cost_table;
pub mod genesis_utils; pub mod genesis_utils;
pub mod hardened_unpack; pub mod hardened_unpack;
pub mod in_mem_accounts_index; pub mod in_mem_accounts_index;