Skip to content

Commit

Permalink
Combine builtin and BPF compute cost in cost model (#29)
Browse files Browse the repository at this point in the history
* Combine builtin and BPF execution cost into programs_execution_cost since VM has started to consume CUs uniformly

* update tests

* apply suggestions from code review

(cherry picked from commit 8f3f06c)

# Conflicts:
#	core/src/banking_stage/consumer.rs
#	cost-model/src/cost_model.rs
  • Loading branch information
tao-stones authored and mergify[bot] committed Mar 16, 2024
1 parent 3809dc0 commit 4871097
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 107 deletions.
3 changes: 1 addition & 2 deletions core/src/banking_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ pub struct BatchedTransactionCostDetails {
pub batched_signature_cost: u64,
pub batched_write_lock_cost: u64,
pub batched_data_bytes_cost: u64,
pub batched_builtins_execute_cost: u64,
pub batched_bpf_execute_cost: u64,
pub batched_programs_execute_cost: u64,
}

#[derive(Debug, Default)]
Expand Down
12 changes: 11 additions & 1 deletion core/src/banking_stage/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1263,16 +1263,26 @@ mod tests {
assert_eq!(retryable_transaction_indexes, vec![1]);

let expected_block_cost = if !apply_cost_tracker_during_replay_enabled {
<<<<<<< HEAD
let actual_bpf_execution_cost = match commit_transactions_result.get(0).unwrap() {
CommitTransactionDetails::Committed { compute_units } => *compute_units,
CommitTransactionDetails::NotCommitted => {
unreachable!()
}
};
=======
let actual_programs_execution_cost =
match commit_transactions_result.first().unwrap() {
CommitTransactionDetails::Committed { compute_units } => *compute_units,
CommitTransactionDetails::NotCommitted => {
unreachable!()
}
};
>>>>>>> 8f3f06cc7f (Combine builtin and BPF compute cost in cost model (#29))

let mut cost = CostModel::calculate_cost(&transactions[0], &bank.feature_set);
if let TransactionCost::Transaction(ref mut usage_cost) = cost {
usage_cost.bpf_execution_cost = actual_bpf_execution_cost;
usage_cost.programs_execution_cost = actual_programs_execution_cost;
}

block_cost + cost.sum()
Expand Down
61 changes: 19 additions & 42 deletions core/src/banking_stage/qos_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,10 @@ impl QosService {
batched_transaction_details.costs.batched_data_bytes_cost,
Ordering::Relaxed,
);
self.metrics.stats.estimated_builtins_execute_cu.fetch_add(
self.metrics.stats.estimated_programs_execute_cu.fetch_add(
batched_transaction_details
.costs
.batched_builtins_execute_cost,
Ordering::Relaxed,
);
self.metrics.stats.estimated_bpf_execute_cu.fetch_add(
batched_transaction_details.costs.batched_bpf_execute_cost,
.batched_programs_execute_cost,
Ordering::Relaxed,
);

Expand Down Expand Up @@ -297,7 +293,7 @@ impl QosService {
pub fn accumulate_actual_execute_cu(&self, units: u64) {
self.metrics
.stats
.actual_bpf_execute_cu
.actual_programs_execute_cu
.fetch_add(units, Ordering::Relaxed);
}

Expand Down Expand Up @@ -331,12 +327,8 @@ impl QosService {
saturating_add_assign!(
batched_transaction_details
.costs
.batched_builtins_execute_cost,
cost.builtins_execution_cost()
);
saturating_add_assign!(
batched_transaction_details.costs.batched_bpf_execute_cost,
cost.bpf_execution_cost()
.batched_programs_execute_cost,
cost.programs_execution_cost()
);
}
Err(transaction_error) => match transaction_error {
Expand Down Expand Up @@ -427,14 +419,11 @@ struct QosServiceMetricsStats {
/// accumulated estimated instructino data Compute Units to be packed into block
estimated_data_bytes_cu: AtomicU64,

/// accumulated estimated builtin programs Compute Units to be packed into block
estimated_builtins_execute_cu: AtomicU64,

/// accumulated estimated SBF program Compute Units to be packed into block
estimated_bpf_execute_cu: AtomicU64,
/// accumulated estimated program Compute Units to be packed into block
estimated_programs_execute_cu: AtomicU64,

/// accumulated actual program Compute Units that have been packed into block
actual_bpf_execute_cu: AtomicU64,
actual_programs_execute_cu: AtomicU64,

/// accumulated actual program execute micro-sec that have been packed into block
actual_execute_time_us: AtomicU64,
Expand Down Expand Up @@ -515,24 +504,19 @@ impl QosServiceMetrics {
i64
),
(
"estimated_builtins_execute_cu",
"estimated_programs_execute_cu",
self.stats
.estimated_builtins_execute_cu
.estimated_programs_execute_cu
.swap(0, Ordering::Relaxed),
i64
),
(
"estimated_bpf_execute_cu",
"actual_programs_execute_cu",
self.stats
.estimated_bpf_execute_cu
.actual_programs_execute_cu
.swap(0, Ordering::Relaxed),
i64
),
(
"actual_bpf_execute_cu",
self.stats.actual_bpf_execute_cu.swap(0, Ordering::Relaxed),
i64
),
(
"actual_execute_time_us",
self.stats.actual_execute_time_us.swap(0, Ordering::Relaxed),
Expand Down Expand Up @@ -735,7 +719,7 @@ mod tests {
let commited_status: Vec<CommitTransactionDetails> = qos_cost_results
.iter()
.map(|tx_cost| CommitTransactionDetails::Committed {
compute_units: tx_cost.as_ref().unwrap().bpf_execution_cost()
compute_units: tx_cost.as_ref().unwrap().programs_execution_cost()
+ execute_units_adjustment,
})
.collect();
Expand Down Expand Up @@ -862,7 +846,7 @@ mod tests {
CommitTransactionDetails::NotCommitted
} else {
CommitTransactionDetails::Committed {
compute_units: tx_cost.as_ref().unwrap().bpf_execution_cost()
compute_units: tx_cost.as_ref().unwrap().programs_execution_cost()
+ execute_units_adjustment,
}
}
Expand Down Expand Up @@ -898,8 +882,7 @@ mod tests {
let signature_cost = 1;
let write_lock_cost = 2;
let data_bytes_cost = 3;
let builtins_execution_cost = 4;
let bpf_execution_cost = 10;
let programs_execution_cost = 10;
let num_txs = 4;

let tx_cost_results: Vec<_> = (0..num_txs)
Expand All @@ -909,8 +892,7 @@ mod tests {
signature_cost,
write_lock_cost,
data_bytes_cost,
builtins_execution_cost,
bpf_execution_cost,
programs_execution_cost,
..UsageCostDetails::default()
}))
} else {
Expand All @@ -922,8 +904,7 @@ mod tests {
let expected_signatures = signature_cost * (num_txs / 2);
let expected_write_locks = write_lock_cost * (num_txs / 2);
let expected_data_bytes = data_bytes_cost * (num_txs / 2);
let expected_builtins_execution_costs = builtins_execution_cost * (num_txs / 2);
let expected_bpf_execution_costs = bpf_execution_cost * (num_txs / 2);
let expected_programs_execution_costs = programs_execution_cost * (num_txs / 2);
let batched_transaction_details =
QosService::accumulate_batched_transaction_costs(tx_cost_results.iter());
assert_eq!(
Expand All @@ -939,14 +920,10 @@ mod tests {
batched_transaction_details.costs.batched_data_bytes_cost
);
assert_eq!(
expected_builtins_execution_costs,
expected_programs_execution_costs,
batched_transaction_details
.costs
.batched_builtins_execute_cost
);
assert_eq!(
expected_bpf_execution_costs,
batched_transaction_details.costs.batched_bpf_execute_cost
.batched_programs_execute_cost
);
}
}
Loading

0 comments on commit 4871097

Please sign in to comment.