Revert exponential moving average cost model changes (backport #23541) (#23543)

* Revert "fix tests after merge"

This reverts commit ba2d83f580.

(cherry picked from commit 0a17edcc1f)

* Revert "1. Persist to blockstore less frequently;"

This reverts commit 7aa1fb4e24.

(cherry picked from commit c878c9e2cb)

# Conflicts:
#	core/src/cost_update_service.rs
#	core/src/tvu.rs
#	runtime/src/cost_model.rs

* Revert "use EMA in place of Welford"

This reverts commit 6587dbfa47.

(cherry picked from commit 9acbfa5eb1)

* Revert "- estimate a program cost as 2 standard deviation above mean"

This reverts commit a25ac1c988.

(cherry picked from commit 5a0cd05866)

# Conflicts:
#	core/src/cost_update_service.rs
#	runtime/src/cost_model.rs

* fix merge conflicts

Co-authored-by: Carl Lin <carl@solana.com>
Co-authored-by: Tao Zhu <tao@solana.com>
This commit is contained in:
mergify[bot]
2022-03-09 03:55:36 +00:00
committed by GitHub
parent 714cf0eff2
commit 4e5d9885da
5 changed files with 293 additions and 281 deletions

View File

@@ -8,6 +8,7 @@ use {
crate::{block_cost_limits::*, execute_cost_table::ExecuteCostTable},
log::*,
solana_sdk::{pubkey::Pubkey, transaction::SanitizedTransaction},
std::collections::HashMap,
};
const MAX_WRITABLE_ACCOUNTS: usize = 256;
@@ -73,9 +74,28 @@ impl CostModel {
.map(|(key, cost)| (key, cost))
.chain(BUILT_IN_INSTRUCTION_COSTS.iter())
.for_each(|(program_id, cost)| {
self.instruction_execution_cost_table
.upsert(program_id, *cost);
match self
.instruction_execution_cost_table
.upsert(program_id, *cost)
{
Some(c) => {
debug!(
"initiating cost table, instruction {:?} has cost {}",
program_id, c
);
}
None => {
debug!(
"initiating cost table, failed for instruction {:?}",
program_id
);
}
}
});
debug!(
"restored cost model instruction cost table from blockstore, current values: {:?}",
self.get_instruction_cost_table()
);
}
pub fn calculate_cost(&self, transaction: &SanitizedTransaction) -> TransactionCost {
@@ -90,20 +110,30 @@ impl CostModel {
tx_cost
}
// update-or-insert op is always successful. However the result of upsert, eg the aggregated
// value, requires additional calculation, which should only be envoked when needed.
pub fn upsert_instruction_cost(&mut self, program_key: &Pubkey, cost: u64) {
pub fn upsert_instruction_cost(
&mut self,
program_key: &Pubkey,
cost: u64,
) -> Result<u64, &'static str> {
self.instruction_execution_cost_table
.upsert(program_key, cost);
match self.instruction_execution_cost_table.get_cost(program_key) {
Some(cost) => Ok(*cost),
None => Err("failed to upsert to ExecuteCostTable"),
}
}
pub fn get_instruction_cost_table(&self) -> &HashMap<Pubkey, u64> {
self.instruction_execution_cost_table.get_cost_table()
}
pub fn find_instruction_cost(&self, program_key: &Pubkey) -> u64 {
match self.instruction_execution_cost_table.get_cost(program_key) {
Some(cost) => cost,
Some(cost) => *cost,
None => {
let default_value = self.instruction_execution_cost_table.get_default();
let default_value = self.instruction_execution_cost_table.get_mode();
debug!(
"instruction {:?} does not have aggregated cost, using default {}",
"Program key {:?} does not have assigned cost, using mode {}",
program_key, default_value
);
default_value
@@ -181,7 +211,6 @@ mod tests {
transaction::Transaction,
},
std::{
collections::HashMap,
str::FromStr,
sync::{Arc, RwLock},
thread::{self, JoinHandle},
@@ -205,16 +234,18 @@ mod tests {
let mut testee = CostModel::default();
let known_key = Pubkey::from_str("known11111111111111111111111111111111111111").unwrap();
testee.upsert_instruction_cost(&known_key, 100);
testee.upsert_instruction_cost(&known_key, 100).unwrap();
// find cost for known programs
assert_eq!(100, testee.find_instruction_cost(&known_key));
testee.upsert_instruction_cost(&bpf_loader::id(), 1999);
testee
.upsert_instruction_cost(&bpf_loader::id(), 1999)
.unwrap();
assert_eq!(1999, testee.find_instruction_cost(&bpf_loader::id()));
// unknown program is assigned with default cost
assert_eq!(
testee.instruction_execution_cost_table.get_default(),
testee.instruction_execution_cost_table.get_mode(),
testee.find_instruction_cost(
&Pubkey::from_str("unknown111111111111111111111111111111111111").unwrap()
)
@@ -232,7 +263,7 @@ mod tests {
});
test_key_and_cost.iter().for_each(|(key, cost)| {
testee.upsert_instruction_cost(key, *cost);
let _ = testee.upsert_instruction_cost(key, *cost).unwrap();
info!("key {:?} cost {}", key, cost);
});
@@ -267,7 +298,9 @@ mod tests {
let expected_cost = 8;
let mut testee = CostModel::default();
testee.upsert_instruction_cost(&system_program::id(), expected_cost);
testee
.upsert_instruction_cost(&system_program::id(), expected_cost)
.unwrap();
assert_eq!(
expected_cost,
testee.get_transaction_cost(&simple_transaction)
@@ -295,7 +328,9 @@ mod tests {
let expected_cost = program_cost * 2;
let mut testee = CostModel::default();
testee.upsert_instruction_cost(&system_program::id(), program_cost);
testee
.upsert_instruction_cost(&system_program::id(), program_cost)
.unwrap();
assert_eq!(expected_cost, testee.get_transaction_cost(&tx));
}
@@ -327,7 +362,7 @@ mod tests {
let result = testee.get_transaction_cost(&tx);
// expected cost for two random/unknown program is
let expected_cost = testee.instruction_execution_cost_table.get_default() * 2;
let expected_cost = testee.instruction_execution_cost_table.get_mode() * 2;
assert_eq!(expected_cost, result);
}
@@ -371,12 +406,12 @@ mod tests {
let mut cost_model = CostModel::default();
// Using default cost for unknown instruction
assert_eq!(
cost_model.instruction_execution_cost_table.get_default(),
cost_model.instruction_execution_cost_table.get_mode(),
cost_model.find_instruction_cost(&key1)
);
// insert instruction cost to table
cost_model.upsert_instruction_cost(&key1, cost1);
assert!(cost_model.upsert_instruction_cost(&key1, cost1).is_ok());
// now it is known insturction with known cost
assert_eq!(cost1, cost_model.find_instruction_cost(&key1));
@@ -396,7 +431,9 @@ mod tests {
let expected_execution_cost = 8;
let mut cost_model = CostModel::default();
cost_model.upsert_instruction_cost(&system_program::id(), expected_execution_cost);
cost_model
.upsert_instruction_cost(&system_program::id(), expected_execution_cost)
.unwrap();
let tx_cost = cost_model.calculate_cost(&tx);
assert_eq!(expected_account_cost, tx_cost.write_lock_cost);
assert_eq!(expected_execution_cost, tx_cost.execution_cost);
@@ -408,17 +445,16 @@ mod tests {
let key1 = Pubkey::new_unique();
let cost1 = 100;
let cost2 = 200;
// updated_cost = (mean + 2*std) of [100, 200] => 120.899
let updated_cost = 121;
let updated_cost = (cost1 + cost2) / 2;
let mut cost_model = CostModel::default();
// insert instruction cost to table
cost_model.upsert_instruction_cost(&key1, cost1);
assert!(cost_model.upsert_instruction_cost(&key1, cost1).is_ok());
assert_eq!(cost1, cost_model.find_instruction_cost(&key1));
// update instruction cost
cost_model.upsert_instruction_cost(&key1, cost2);
assert!(cost_model.upsert_instruction_cost(&key1, cost2).is_ok());
assert_eq!(updated_cost, cost_model.find_instruction_cost(&key1));
}
@@ -460,8 +496,8 @@ mod tests {
if i == 5 {
thread::spawn(move || {
let mut cost_model = cost_model.write().unwrap();
cost_model.upsert_instruction_cost(&prog1, cost1);
cost_model.upsert_instruction_cost(&prog2, cost2);
assert!(cost_model.upsert_instruction_cost(&prog1, cost1).is_ok());
assert!(cost_model.upsert_instruction_cost(&prog2, cost2).is_ok());
})
} else {
thread::spawn(move || {

View File

@@ -4,10 +4,7 @@
/// When its capacity limit is reached, it prunes old and less-used programs
/// to make room for new ones.
use log::*;
use {
solana_sdk::pubkey::Pubkey,
std::collections::{hash_map::Entry, HashMap},
};
use {solana_sdk::pubkey::Pubkey, std::collections::HashMap};
// prune is rather expensive op, free up bulk space in each operation
// would be more efficient. PRUNE_RATIO defines the after prune table
@@ -18,22 +15,10 @@ const OCCURRENCES_WEIGHT: i64 = 100;
const DEFAULT_CAPACITY: usize = 1024;
// The coefficient represents the degree of weighting decrease in EMA,
// a constant smoothing factor between 0 and 1. A higher alpha
// discounts older observations faster.
// Setting it using `2/(N+1)` where N is 200 samples
const COEFFICIENT: f64 = 0.01;
#[derive(Debug, Default)]
struct AggregatedVarianceStats {
ema: f64,
ema_var: f64,
}
#[derive(Debug)]
#[derive(AbiExample, Debug)]
pub struct ExecuteCostTable {
capacity: usize,
table: HashMap<Pubkey, AggregatedVarianceStats>,
table: HashMap<Pubkey, u64>,
occurrences: HashMap<Pubkey, (usize, u128)>,
}
@@ -52,59 +37,55 @@ impl ExecuteCostTable {
}
}
// number of programs in table
pub fn get_cost_table(&self) -> &HashMap<Pubkey, u64> {
&self.table
}
pub fn get_count(&self) -> usize {
self.table.len()
}
// default program cost to max
pub fn get_default(&self) -> u64 {
// default max compute units per program
200_000u64
}
// returns None if program doesn't exist in table. In this case,
// it is advised to call `get_default()` for default program cost.
// Program cost is estimated as 2 standard deviations above mean, eg
// cost = (mean + 2 * std)
pub fn get_cost(&self, key: &Pubkey) -> Option<u64> {
let aggregated = self.table.get(key)?;
let cost_f64 = (aggregated.ema + 2.0 * aggregated.ema_var.sqrt()).ceil();
// check if cost:f64 can be losslessly convert to u64, otherwise return None
let cost_u64 = cost_f64 as u64;
if cost_f64 == cost_u64 as f64 {
Some(cost_u64)
// instead of assigning unknown program with a configured/hard-coded cost
// use average or mode function to make a educated guess.
pub fn get_average(&self) -> u64 {
if self.table.is_empty() {
0
} else {
None
self.table.iter().map(|(_, value)| value).sum::<u64>() / self.get_count() as u64
}
}
pub fn upsert(&mut self, key: &Pubkey, value: u64) {
let need_to_add = !self.table.contains_key(key);
pub fn get_mode(&self) -> u64 {
if self.occurrences.is_empty() {
0
} else {
let key = self
.occurrences
.iter()
.max_by_key(|&(_, count)| count)
.map(|(key, _)| key)
.expect("cannot find mode from cost table");
*self.table.get(key).unwrap()
}
}
// returns None if program doesn't exist in table. In this case,
// client is advised to call `get_average()` or `get_mode()` to
// assign a 'default' value for new program.
pub fn get_cost(&self, key: &Pubkey) -> Option<&u64> {
self.table.get(key)
}
pub fn upsert(&mut self, key: &Pubkey, value: u64) -> Option<u64> {
let need_to_add = self.table.get(key).is_none();
let current_size = self.get_count();
if current_size == self.capacity && need_to_add {
self.prune_to(&((current_size as f64 * PRUNE_RATIO) as usize));
}
// exponential moving average algorithm
// https://en.wikipedia.org/wiki/Moving_average#Exponentially_weighted_moving_variance_and_standard_deviation
match self.table.entry(*key) {
Entry::Occupied(mut entry) => {
let aggregated = entry.get_mut();
let theta = value as f64 - aggregated.ema;
aggregated.ema += theta * COEFFICIENT;
aggregated.ema_var =
(1.0 - COEFFICIENT) * (aggregated.ema_var + COEFFICIENT * theta * theta);
}
Entry::Vacant(entry) => {
// the starting values
entry.insert(AggregatedVarianceStats {
ema: value as f64,
ema_var: 0.0,
});
}
}
let program_cost = self.table.entry(*key).or_insert(value);
*program_cost = (*program_cost + value) / 2;
let (count, timestamp) = self
.occurrences
@@ -112,6 +93,8 @@ impl ExecuteCostTable {
.or_insert((0, Self::micros_since_epoch()));
*count += 1;
*timestamp = Self::micros_since_epoch();
Some(*program_cost)
}
pub fn get_program_keys(&self) -> Vec<&Pubkey> {
@@ -205,9 +188,9 @@ mod tests {
let key2 = Pubkey::new_unique();
let key3 = Pubkey::new_unique();
// simulate a lot of occurrences to key1, so even there're longer than
// simulate a lot of occurences to key1, so even there're longer than
// usual delay between upsert(key1..) and upsert(key2, ..), test
// would still satisfy as key1 has enough occurrences to compensate
// would still satisfy as key1 has enough occurences to compensate
// its age.
for i in 0..1000 {
testee.upsert(&key1, i);
@@ -240,21 +223,25 @@ mod tests {
// insert one record
testee.upsert(&key1, cost1);
assert_eq!(1, testee.get_count());
assert_eq!(cost1, testee.get_cost(&key1).unwrap());
assert_eq!(cost1, testee.get_average());
assert_eq!(cost1, testee.get_mode());
assert_eq!(&cost1, testee.get_cost(&key1).unwrap());
// insert 2nd record
testee.upsert(&key2, cost2);
assert_eq!(2, testee.get_count());
assert_eq!(cost1, testee.get_cost(&key1).unwrap());
assert_eq!(cost2, testee.get_cost(&key2).unwrap());
assert_eq!((cost1 + cost2) / 2_u64, testee.get_average());
assert_eq!(cost2, testee.get_mode());
assert_eq!(&cost1, testee.get_cost(&key1).unwrap());
assert_eq!(&cost2, testee.get_cost(&key2).unwrap());
// update 1st record
testee.upsert(&key1, cost2);
assert_eq!(2, testee.get_count());
// expected key1 cost is EMA of [100, 110] with alpha=0.01 => 103
let expected_cost = 103;
assert_eq!(expected_cost, testee.get_cost(&key1).unwrap());
assert_eq!(cost2, testee.get_cost(&key2).unwrap());
assert_eq!(((cost1 + cost2) / 2 + cost2) / 2, testee.get_average());
assert_eq!((cost1 + cost2) / 2, testee.get_mode());
assert_eq!(&((cost1 + cost2) / 2), testee.get_cost(&key1).unwrap());
assert_eq!(&cost2, testee.get_cost(&key2).unwrap());
}
#[test]
@@ -275,50 +262,33 @@ mod tests {
// insert one record
testee.upsert(&key1, cost1);
assert_eq!(1, testee.get_count());
assert_eq!(cost1, testee.get_cost(&key1).unwrap());
assert_eq!(&cost1, testee.get_cost(&key1).unwrap());
// insert 2nd record
testee.upsert(&key2, cost2);
assert_eq!(2, testee.get_count());
assert_eq!(cost1, testee.get_cost(&key1).unwrap());
assert_eq!(cost2, testee.get_cost(&key2).unwrap());
assert_eq!(&cost1, testee.get_cost(&key1).unwrap());
assert_eq!(&cost2, testee.get_cost(&key2).unwrap());
// insert 3rd record, pushes out the oldest (eg 1st) record
testee.upsert(&key3, cost3);
assert_eq!(2, testee.get_count());
assert_eq!((cost2 + cost3) / 2_u64, testee.get_average());
assert_eq!(cost3, testee.get_mode());
assert!(testee.get_cost(&key1).is_none());
assert_eq!(cost2, testee.get_cost(&key2).unwrap());
assert_eq!(cost3, testee.get_cost(&key3).unwrap());
assert_eq!(&cost2, testee.get_cost(&key2).unwrap());
assert_eq!(&cost3, testee.get_cost(&key3).unwrap());
// update 2nd record, so the 3rd becomes the oldest
// add 4th record, pushes out 3rd key
testee.upsert(&key2, cost1);
testee.upsert(&key4, cost4);
assert_eq!(((cost1 + cost2) / 2 + cost4) / 2_u64, testee.get_average());
assert_eq!((cost1 + cost2) / 2, testee.get_mode());
assert_eq!(2, testee.get_count());
assert!(testee.get_cost(&key1).is_none());
// expected key2 cost = (mean + 2*std) of [110, 100] => 112
let expected_cost_2 = 112;
assert_eq!(expected_cost_2, testee.get_cost(&key2).unwrap());
assert_eq!(&((cost1 + cost2) / 2), testee.get_cost(&key2).unwrap());
assert!(testee.get_cost(&key3).is_none());
assert_eq!(cost4, testee.get_cost(&key4).unwrap());
}
#[test]
fn test_get_cost_overflow_u64() {
solana_logger::setup();
let mut testee = ExecuteCostTable::default();
let key1 = Pubkey::new_unique();
let cost1: u64 = f64::MAX as u64;
let cost2: u64 = u64::MAX / 2; // create large variance so the final result will overflow
// insert one record
testee.upsert(&key1, cost1);
assert_eq!(1, testee.get_count());
assert_eq!(cost1, testee.get_cost(&key1).unwrap());
// update cost
testee.upsert(&key1, cost2);
assert!(testee.get_cost(&key1).is_none());
assert_eq!(&cost4, testee.get_cost(&key4).unwrap());
}
}