Aggregate cost_model into cost_tracker (#18374)

* * aggregate cost_model into cost_tracker, decouple it from banking_stage to prevent accidental deadlock. * Simplified code, removed unused functions

* review fixes
This commit is contained in:
Tao Zhu
2021-07-06 10:41:25 -05:00
committed by GitHub
parent 660788d227
commit 0e039b4094
7 changed files with 168 additions and 310 deletions

View File

@@ -5,11 +5,11 @@
//! Instructions take time to execute, both historical and runtime data are
//! used to determine each instruction's execution time, the sum of that
//! is transaction's "execution cost"
//! The main function is `calculate_cost` which returns a TransactionCost struct.
//! The main function is `calculate_cost` which returns &TransactionCost.
//!
use crate::execute_cost_table::ExecuteCostTable;
use log::*;
use solana_sdk::{message::Message, pubkey::Pubkey, transaction::Transaction};
use solana_sdk::{pubkey::Pubkey, transaction::Transaction};
use std::collections::HashMap;
// Guestimated from mainnet-beta data, sigver averages 1us, average read 7us and average write 25us
@@ -26,6 +26,7 @@ const SIGNED_WRITABLE_ACCOUNT_ACCESS_COST: u64 =
pub const ACCOUNT_MAX_COST: u64 = 100_000_000;
pub const BLOCK_MAX_COST: u64 = 2_500_000_000;
const MAX_WRITABLE_ACCOUNTS: usize = 256;
const DEMOTE_SYSVAR_WRITE_LOCKS: bool = true;
// cost of transaction is made of account_access_cost and instruction execution_cost
@@ -61,6 +62,9 @@ pub struct CostModel {
account_cost_limit: u64,
block_cost_limit: u64,
instruction_execution_cost_table: ExecuteCostTable,
// reusable variables
transaction_cost: TransactionCost,
}
impl Default for CostModel {
@@ -75,6 +79,7 @@ impl CostModel {
account_cost_limit: chain_max,
block_cost_limit: block_max,
instruction_execution_cost_table: ExecuteCostTable::default(),
transaction_cost: TransactionCost::new_with_capacity(MAX_WRITABLE_ACCOUNTS),
}
}
@@ -86,36 +91,8 @@ impl CostModel {
self.block_cost_limit
}
pub fn calculate_cost(&self, transaction: &Transaction) -> TransactionCost {
let (
signed_writable_accounts,
signed_readonly_accounts,
non_signed_writable_accounts,
non_signed_readonly_accounts,
) = CostModel::sort_accounts_by_type(transaction.message());
let mut cost = TransactionCost {
writable_accounts: vec![],
account_access_cost: CostModel::find_account_access_cost(
&signed_writable_accounts,
&signed_readonly_accounts,
&non_signed_writable_accounts,
&non_signed_readonly_accounts,
),
execution_cost: self.find_transaction_cost(transaction),
};
cost.writable_accounts.extend(&signed_writable_accounts);
cost.writable_accounts.extend(&non_signed_writable_accounts);
debug!("transaction {:?} has cost {:?}", transaction, cost);
cost
}
// calculate `transaction` cost, the result is passed back to caller via mutable
// parameter `cost`. Existing content in `cost` will be erased before adding new content
// This is to allow this function to reuse pre-allocated memory, as this function
// is often on hot-path.
pub fn calculate_cost_no_alloc(&self, transaction: &Transaction, cost: &mut TransactionCost) {
cost.reset();
pub fn calculate_cost(&mut self, transaction: &Transaction) -> &TransactionCost {
self.transaction_cost.reset();
let message = transaction.message();
message.account_keys.iter().enumerate().for_each(|(i, k)| {
@@ -123,19 +100,25 @@ impl CostModel {
let is_writable = message.is_writable(i, DEMOTE_SYSVAR_WRITE_LOCKS);
if is_signer && is_writable {
cost.writable_accounts.push(*k);
cost.account_access_cost += SIGNED_WRITABLE_ACCOUNT_ACCESS_COST;
self.transaction_cost.writable_accounts.push(*k);
self.transaction_cost.account_access_cost += SIGNED_WRITABLE_ACCOUNT_ACCESS_COST;
} else if is_signer && !is_writable {
cost.account_access_cost += SIGNED_READONLY_ACCOUNT_ACCESS_COST;
self.transaction_cost.account_access_cost += SIGNED_READONLY_ACCOUNT_ACCESS_COST;
} else if !is_signer && is_writable {
cost.writable_accounts.push(*k);
cost.account_access_cost += NON_SIGNED_WRITABLE_ACCOUNT_ACCESS_COST;
self.transaction_cost.writable_accounts.push(*k);
self.transaction_cost.account_access_cost +=
NON_SIGNED_WRITABLE_ACCOUNT_ACCESS_COST;
} else {
cost.account_access_cost += NON_SIGNED_READONLY_ACCOUNT_ACCESS_COST;
self.transaction_cost.account_access_cost +=
NON_SIGNED_READONLY_ACCOUNT_ACCESS_COST;
}
});
cost.execution_cost = self.find_transaction_cost(transaction);
debug!("transaction {:?} has cost {:?}", transaction, cost);
self.transaction_cost.execution_cost = self.find_transaction_cost(transaction);
debug!(
"transaction {:?} has cost {:?}",
transaction, self.transaction_cost
);
&self.transaction_cost
}
// To update or insert instruction cost to table.
@@ -186,50 +169,6 @@ impl CostModel {
}
cost
}
fn find_account_access_cost(
signed_writable_accounts: &[Pubkey],
signed_readonly_accounts: &[Pubkey],
non_signed_writable_accounts: &[Pubkey],
non_signed_readonly_accounts: &[Pubkey],
) -> u64 {
let mut cost = 0;
cost += signed_writable_accounts.len() as u64 * SIGNED_WRITABLE_ACCOUNT_ACCESS_COST;
cost += signed_readonly_accounts.len() as u64 * SIGNED_READONLY_ACCOUNT_ACCESS_COST;
cost += non_signed_writable_accounts.len() as u64 * NON_SIGNED_WRITABLE_ACCOUNT_ACCESS_COST;
cost += non_signed_readonly_accounts.len() as u64 * NON_SIGNED_READONLY_ACCOUNT_ACCESS_COST;
cost
}
fn sort_accounts_by_type(
message: &Message,
) -> (Vec<Pubkey>, Vec<Pubkey>, Vec<Pubkey>, Vec<Pubkey>) {
let demote_sysvar_write_locks = true;
let mut signer_writable: Vec<Pubkey> = vec![];
let mut signer_readonly: Vec<Pubkey> = vec![];
let mut non_signer_writable: Vec<Pubkey> = vec![];
let mut non_signer_readonly: Vec<Pubkey> = vec![];
message.account_keys.iter().enumerate().for_each(|(i, k)| {
let is_signer = message.is_signer(i);
let is_writable = message.is_writable(i, demote_sysvar_write_locks);
if is_signer && is_writable {
signer_writable.push(*k);
} else if is_signer && !is_writable {
signer_readonly.push(*k);
} else if !is_signer && is_writable {
non_signer_writable.push(*k);
} else {
non_signer_readonly.push(*k);
}
});
(
signer_writable,
signer_readonly,
non_signer_writable,
non_signer_readonly,
)
}
}
#[cfg(test)]
@@ -387,25 +326,14 @@ mod tests {
vec![prog1, prog2],
instructions,
);
debug!("many random transaction {:?}", tx);
let (
signed_writable_accounts,
signed_readonly_accounts,
non_signed_writable_accounts,
non_signed_readonly_accounts,
) = CostModel::sort_accounts_by_type(tx.message());
assert_eq!(2, signed_writable_accounts.len());
assert_eq!(signer1.pubkey(), signed_writable_accounts[0]);
assert_eq!(signer2.pubkey(), signed_writable_accounts[1]);
assert_eq!(0, signed_readonly_accounts.len());
assert_eq!(2, non_signed_writable_accounts.len());
assert_eq!(key1, non_signed_writable_accounts[0]);
assert_eq!(key2, non_signed_writable_accounts[1]);
assert_eq!(2, non_signed_readonly_accounts.len());
assert_eq!(prog1, non_signed_readonly_accounts[0]);
assert_eq!(prog2, non_signed_readonly_accounts[1]);
let mut cost_model = CostModel::default();
let tx_cost = cost_model.calculate_cost(&tx);
assert_eq!(2 + 2, tx_cost.writable_accounts.len());
assert_eq!(signer1.pubkey(), tx_cost.writable_accounts[0]);
assert_eq!(signer2.pubkey(), tx_cost.writable_accounts[1]);
assert_eq!(key1, tx_cost.writable_accounts[2]);
assert_eq!(key2, tx_cost.writable_accounts[3]);
}
#[test]
@@ -448,33 +376,6 @@ mod tests {
assert_eq!(2, tx_cost.writable_accounts.len());
}
#[test]
fn test_cost_model_calculate_cost_no_alloc() {
let (mint_keypair, start_hash) = test_setup();
let tx =
system_transaction::transfer(&mint_keypair, &Keypair::new().pubkey(), 2, start_hash);
let expected_account_cost = SIGNED_WRITABLE_ACCOUNT_ACCESS_COST
+ NON_SIGNED_WRITABLE_ACCOUNT_ACCESS_COST
+ NON_SIGNED_READONLY_ACCOUNT_ACCESS_COST;
let expected_execution_cost = 8;
let mut cost_model = CostModel::default();
cost_model
.upsert_instruction_cost(&system_program::id(), &expected_execution_cost)
.unwrap();
// allocate cost, set some random number
let mut tx_cost = TransactionCost::new_with_capacity(8);
tx_cost.execution_cost = 101;
tx_cost.writable_accounts.push(Pubkey::new_unique());
cost_model.calculate_cost_no_alloc(&tx, &mut tx_cost);
assert_eq!(expected_account_cost, tx_cost.account_access_cost);
assert_eq!(expected_execution_cost, tx_cost.execution_cost);
assert_eq!(2, tx_cost.writable_accounts.len());
}
#[test]
fn test_cost_model_update_instruction_cost() {
let key1 = Pubkey::new_unique();
@@ -493,43 +394,6 @@ mod tests {
assert_eq!(updated_cost, cost_model.find_instruction_cost(&key1));
}
#[test]
fn test_cost_model_can_be_shared_concurrently_as_immutable() {
let (mint_keypair, start_hash) = test_setup();
let number_threads = 10;
let expected_account_cost = SIGNED_WRITABLE_ACCOUNT_ACCESS_COST
+ NON_SIGNED_WRITABLE_ACCOUNT_ACCESS_COST
+ NON_SIGNED_READONLY_ACCOUNT_ACCESS_COST;
let cost_model = Arc::new(CostModel::default());
let thread_handlers: Vec<JoinHandle<()>> = (0..number_threads)
.map(|_| {
// each thread creates its own simple transaction
let simple_transaction = system_transaction::transfer(
&mint_keypair,
&Keypair::new().pubkey(),
2,
start_hash,
);
let cost_model = cost_model.clone();
thread::spawn(move || {
let tx_cost = cost_model.calculate_cost(&simple_transaction);
assert_eq!(2, tx_cost.writable_accounts.len());
assert_eq!(expected_account_cost, tx_cost.account_access_cost);
assert_eq!(
cost_model.instruction_execution_cost_table.get_mode(),
tx_cost.execution_cost
);
})
})
.collect();
for th in thread_handlers {
th.join().unwrap();
}
}
#[test]
fn test_cost_model_can_be_shared_concurrently_with_rwlock() {
let (mint_keypair, start_hash) = test_setup();
@@ -573,7 +437,8 @@ mod tests {
})
} else {
thread::spawn(move || {
let tx_cost = cost_model.read().unwrap().calculate_cost(&tx);
let mut cost_model = cost_model.write().unwrap();
let tx_cost = cost_model.calculate_cost(&tx);
assert_eq!(3, tx_cost.writable_accounts.len());
assert_eq!(expected_account_cost, tx_cost.account_access_cost);
})