Skip to content

Commit

Permalink
[gas] fix dependency gas for module publishing + minor gas profiler i…
Browse files Browse the repository at this point in the history
…mprovements
  • Loading branch information
vgao1996 committed Mar 1, 2024
1 parent 163aec7 commit e973972
Show file tree
Hide file tree
Showing 27 changed files with 357 additions and 163 deletions.
8 changes: 3 additions & 5 deletions aptos-move/aptos-gas-meter/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,15 @@ impl GasAlgebra for StandardGasAlgebra {
}

fn count_dependency(&mut self, size: NumBytes) -> PartialVMResult<()> {
if self.feature_version >= 14 {
if self.feature_version >= 15 {
self.num_dependencies += 1.into();
self.total_dependency_size += size;

if self.num_dependencies > self.vm_gas_params.txn.max_num_dependencies {
return Err(PartialVMError::new(StatusCode::TOO_MANY_DEPENDENCIES));
return Err(PartialVMError::new(StatusCode::DEPENDENCY_LIMIT_REACHED));
}
if self.total_dependency_size > self.vm_gas_params.txn.max_total_dependency_size {
return Err(PartialVMError::new(
StatusCode::TOTAL_DEPENDENCY_SIZE_TOO_BIG,
));
return Err(PartialVMError::new(StatusCode::DEPENDENCY_LIMIT_REACHED));
}
}
Ok(())
Expand Down
10 changes: 8 additions & 2 deletions aptos-move/aptos-gas-meter/src/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,18 @@ where
#[inline]
fn charge_dependency(
&mut self,
_is_new: bool,
addr: &AccountAddress,
_name: &IdentStr,
size: NumBytes,
) -> PartialVMResult<()> {
// TODO: How about 0xA550C18?
if self.feature_version() >= 14 && !addr.is_special() {
// Modules under special addresses are considered system modules that should always
// be loaded, and are therefore excluded from gas charging.
//
// TODO: 0xA550C18 is a legacy system address we used, but it is currently not covered by
// `.is_special()`. We should double check if this address still needs special
// treatment.
if self.feature_version() >= 15 && !addr.is_special() {
self.algebra
.charge_execution(DEPENDENCY_PER_MODULE + DEPENDENCY_PER_BYTE * size)?;
self.algebra.count_dependency(size)?;
Expand Down
9 changes: 8 additions & 1 deletion aptos-move/aptos-gas-profiling/src/erased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,14 @@ impl WriteTransient {

impl Dependency {
fn to_erased(&self) -> Node<InternalGas> {
Node::new(format!("{}", Render(&self.id)), self.cost)
Node::new(
format!(
"{}{}",
Render(&self.id),
if self.is_new { " (new)" } else { "" }
),
self.cost,
)
}
}

Expand Down
9 changes: 8 additions & 1 deletion aptos-move/aptos-gas-profiling/src/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ impl ExecutionAndIOCosts {
}

for dep in &self.dependencies {
lines.push(format!("dependencies;{}", Render(&dep.id)), dep.cost)
lines.push(
format!(
"dependencies;{}{}",
Render(&dep.id),
if dep.is_new { "(new)" } else { "" }
),
dep.cost,
)
}

impl<'a> Rec<'a> {
Expand Down
21 changes: 11 additions & 10 deletions aptos-move/aptos-gas-profiling/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use smallvec::{smallvec, SmallVec};

/// An event occurred during the execution of a function, along with the
/// gas cost associated with it, if any.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum ExecutionGasEvent {
/// A special event indicating that the program counter has moved to
/// a specific offset. This is emitted by the branch instructions
Expand Down Expand Up @@ -42,7 +42,7 @@ pub enum ExecutionGasEvent {

/// An enum representing the name of a call frame.
/// Could be either a script or a function.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum FrameName {
Script,
Function {
Expand All @@ -54,7 +54,7 @@ pub enum FrameName {

/// A struct containing information about a function call, including the name of the
/// function and all gas events that happened during the call.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CallFrame {
pub name: FrameName,
pub events: Vec<ExecutionGasEvent>,
Expand All @@ -63,31 +63,31 @@ pub struct CallFrame {
/// The type of an operation performed on a storage item.
///
/// Possible values: Creation, Modification & Deletion.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum WriteOpType {
Creation,
Modification,
Deletion,
}

/// Struct representing the transient (IO) cost of a write operation.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct WriteTransient {
pub key: StateKey,
pub op_type: WriteOpType,
pub cost: InternalGas,
}

/// Struct representing the storage cost of a write operation.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct WriteStorage {
pub key: StateKey,
pub op_type: WriteOpType,
pub cost: Fee,
pub refund: Fee,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
/// Struct representing the storage cost of an event.
pub struct EventStorage {
pub ty: TypeTag,
Expand All @@ -97,13 +97,14 @@ pub struct EventStorage {
#[derive(Debug, Clone)]
/// Struct representing the cost of a dependency.
pub struct Dependency {
pub is_new: bool,
pub id: ModuleId,
pub size: NumBytes,
pub cost: InternalGas,
}

/// Struct containing all execution and io costs.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ExecutionAndIOCosts {
pub gas_scaling_factor: GasScalingFactor,
pub total: InternalGas,
Expand All @@ -114,7 +115,7 @@ pub struct ExecutionAndIOCosts {
pub write_set_transient: Vec<WriteTransient>,
}

#[derive(Debug)]
#[derive(Debug, Clone)]
/// Struct containing all types of storage fees.
pub struct StorageFees {
pub total: Fee,
Expand All @@ -128,7 +129,7 @@ pub struct StorageFees {

/// A complete log that contains all gas-related information about a transaction, including
/// the intrinsic cost, a detailed execution log and the write set costs.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct TransactionGasLog {
pub exec_io: ExecutionAndIOCosts,
pub storage: StorageFees,
Expand Down
5 changes: 4 additions & 1 deletion aptos-move/aptos-gas-profiling/src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,17 @@ where

fn charge_dependency(
&mut self,
is_new: bool,
addr: &AccountAddress,
name: &IdentStr,
size: NumBytes,
) -> PartialVMResult<()> {
let (cost, res) = self.delegate_charge(|base| base.charge_dependency(addr, name, size));
let (cost, res) =
self.delegate_charge(|base| base.charge_dependency(is_new, addr, name, size));

if !cost.is_zero() {
self.dependencies.push(Dependency {
is_new,
id: ModuleId::new(*addr, name.to_owned()),
size,
cost,
Expand Down
16 changes: 11 additions & 5 deletions aptos-move/aptos-gas-profiling/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ impl TransactionGasLog {
Value::Array(
deps.iter()
.map(|dep| {
let name = format!("{}", Render(&dep.id));
let name = format!(
"{}{}",
Render(&dep.id),
if dep.is_new { " (new)" } else { "" }
);
let cost_scaled =
format!("{:.8}", (u64::from(dep.cost) as f64 / scaling_factor));
let cost_scaled =
Expand Down Expand Up @@ -204,11 +208,12 @@ impl TransactionGasLog {
}

// Storage fees & refunds for state changes
let mut storage_writes = self.storage.write_set_storage.clone();
storage_writes.sort_by(|lhs, rhs| rhs.cost.cmp(&lhs.cost));
data.insert(
"storage-writes".to_string(),
Value::Array(
self.storage
.write_set_storage
storage_writes
.iter()
.map(|write| {
let (refund_scaled, refund_percentage) = if write.refund.is_zero() {
Expand Down Expand Up @@ -239,11 +244,12 @@ impl TransactionGasLog {
);

// Storage fees for events
let mut storage_events = self.storage.events.clone();
storage_events.sort_by(|lhs, rhs| rhs.cost.cmp(&lhs.cost));
data.insert(
"storage-events".to_string(),
Value::Array(
self.storage
.events
storage_events
.iter()
.map(|event| {
json!({
Expand Down
5 changes: 3 additions & 2 deletions aptos-move/aptos-gas-schedule/src/ver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
/// - Changing how gas is calculated in any way
///
/// Change log:
/// - V14
/// - V15
/// - Gas & limits for dependencies
/// - V14
/// - Gas for type creation
/// - V13
/// - Storage Fee: Make state bytes refundable and remove the per slot free quota, gated by flag REFUNDABLE_BYTES
Expand Down Expand Up @@ -52,4 +53,4 @@
/// global operations.
/// - V1
/// - TBA
pub const LATEST_GAS_FEATURE_VERSION: u64 = 14;
pub const LATEST_GAS_FEATURE_VERSION: u64 = 15;
2 changes: 1 addition & 1 deletion aptos-move/aptos-memory-usage-tracker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ where

fn charge_create_ty(&mut self, num_nodes: NumTypeNodes) -> PartialVMResult<()>;

fn charge_dependency(&mut self, addr: &AccountAddress, name: &IdentStr, size: NumBytes) -> PartialVMResult<()>;
fn charge_dependency(&mut self, is_new: bool, addr: &AccountAddress, name: &IdentStr, size: NumBytes) -> PartialVMResult<()>;
}

#[inline]
Expand Down
Loading

0 comments on commit e973972

Please sign in to comment.