Skip to content

Commit d03dac8

Browse files
committed
Track detailed gas
1 parent bfb643e commit d03dac8

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

crates/sui-types/src/gas_model/tables.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ pub struct GasStatus {
7070
instructions_current_tier_mult: u64,
7171

7272
pub num_native_calls: u64,
73+
pub transl_gas: u64,
74+
}
75+
76+
#[derive(Debug)]
77+
pub struct GasDetails {
78+
pub gas_left: u64,
79+
pub instructions_executed: u64,
80+
pub stack_height: u64,
81+
pub memory_allocated: u64,
82+
pub transl_gas_used: u64,
7383
}
7484

7585
impl GasStatus {
@@ -106,6 +116,7 @@ impl GasStatus {
106116
stack_size_next_tier_start,
107117
instructions_next_tier_start,
108118
num_native_calls: 0,
119+
transl_gas: 0,
109120
}
110121
}
111122

@@ -133,6 +144,21 @@ impl GasStatus {
133144
stack_size_next_tier_start: None,
134145
instructions_next_tier_start: None,
135146
num_native_calls: 0,
147+
transl_gas: 0,
148+
}
149+
}
150+
151+
pub fn set_transl_gas(&mut self, transl_gas: u64) {
152+
self.transl_gas = transl_gas
153+
}
154+
155+
pub fn gas_details(&self) -> GasDetails {
156+
GasDetails {
157+
gas_left: self.gas_left.into(),
158+
instructions_executed: self.instructions_executed,
159+
stack_height: self.stack_height_high_water_mark,
160+
memory_allocated: self.stack_size_high_water_mark,
161+
transl_gas_used: self.transl_gas,
136162
}
137163
}
138164

sui-execution/latest/sui-adapter/src/execution_engine.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ mod checked {
1919
use std::fs::File;
2020
use std::io::BufWriter;
2121
use std::sync::Mutex;
22-
use similar::TextDiff;
2322
use std::{cell::RefCell, collections::HashSet, rc::Rc, sync::Arc};
2423
use sui_types::accumulator_root::{ACCUMULATOR_ROOT_CREATE_FUNC, ACCUMULATOR_ROOT_MODULE};
2524
use sui_types::balance::{
@@ -28,6 +27,7 @@ mod checked {
2827
};
2928
use sui_types::execution_params::ExecutionOrEarlyError;
3029
use sui_types::gas_coin::GAS;
30+
use sui_types::gas_model::tables::GasDetails;
3131
use sui_types::messages_checkpoint::CheckpointTimestamp;
3232
use sui_types::metrics::LimitsMetrics;
3333
use sui_types::object::OBJECT_START_VERSION;
@@ -69,7 +69,6 @@ mod checked {
6969
use sui_types::error::{ExecutionError, ExecutionErrorKind};
7070
use sui_types::execution::{ExecutionTiming, ResultWithTimings};
7171
use sui_types::execution_status::{ExecutionFailureStatus, ExecutionStatus};
72-
use sui_types::gas::GasCostSummary;
7372
use sui_types::gas::SuiGasStatus;
7473
use sui_types::gas::{GasCostSummary, GasUsageReport};
7574
use sui_types::id::UID;
@@ -100,7 +99,7 @@ mod checked {
10099
let enc = FrameEncoder::new(enc);
101100
let mut writer = Writer::from_writer(enc);
102101
writer
103-
.write_record(&[
102+
.write_record([
104103
"tx_digest",
105104
"gas_budget",
106105
"new_computation_cost",
@@ -111,6 +110,13 @@ mod checked {
111110
"old_storage_rebate",
112111
"new_gas_used",
113112
"old_gas_used",
113+
"new_instructions",
114+
"old_instructions",
115+
"new_stack_height",
116+
"old_stack_hight",
117+
"new_memory_allocated",
118+
"old_memory_allocated",
119+
"translation",
114120
])
115121
.expect("failed to write gas header");
116122
Mutex::new(writer)
@@ -120,13 +126,15 @@ mod checked {
120126
transaction_digest: String,
121127
new_gas: &GasUsageReport,
122128
old_gas: &GasUsageReport,
129+
new_details: &GasDetails,
130+
old_details: &GasDetails,
123131
) {
124132
if new_gas.cost_summary == old_gas.cost_summary {
125133
return;
126134
}
127135
let mut writer = CSV_WRITER.lock().unwrap();
128136
writer
129-
.write_record(&[
137+
.write_record([
130138
&transaction_digest,
131139
&new_gas.gas_budget.to_string(),
132140
&new_gas.cost_summary.computation_cost.to_string(),
@@ -137,6 +145,13 @@ mod checked {
137145
&old_gas.cost_summary.storage_rebate.to_string(),
138146
&new_gas.gas_used.to_string(),
139147
&old_gas.gas_used.to_string(),
148+
&new_details.instructions_executed.to_string(),
149+
&old_details.instructions_executed.to_string(),
150+
&new_details.stack_height.to_string(),
151+
&old_details.stack_height.to_string(),
152+
&new_details.memory_allocated.to_string(),
153+
&old_details.memory_allocated.to_string(),
154+
&new_details.transl_gas_used.to_string(),
140155
])
141156
.expect("failed to write gas row");
142157
writer.flush().expect("failed to flush gas writer");
@@ -208,7 +223,8 @@ mod checked {
208223

209224
compare_effects::<Mode>(&normal_effects, &new_effects);
210225

211-
normal_effects
226+
let (temp_store, gas_status, effects, _gas_details, timing, res) = normal_effects;
227+
(temp_store, gas_status, effects, timing, res)
212228
}
213229

214230
#[instrument(name = "new_tx_execute_to_effects", level = "debug", skip_all)]
@@ -232,6 +248,7 @@ mod checked {
232248
InnerTemporaryStore,
233249
SuiGasStatus,
234250
TransactionEffects,
251+
GasDetails,
235252
Vec<ExecutionTiming>,
236253
Result<Mode::ExecutionResults, ExecutionError>,
237254
) {
@@ -306,6 +323,8 @@ mod checked {
306323
trace_builder_opt,
307324
);
308325

326+
let gas_details = gas_charger.move_gas_status().gas_details();
327+
309328
let status = if let Err(error) = &execution_result {
310329
// Elaborate errors in logs if they are unexpected or their status is terse.
311330
use ExecutionErrorKind as K;
@@ -398,6 +417,7 @@ mod checked {
398417
inner,
399418
gas_charger.into_gas_status(),
400419
effects,
420+
gas_details,
401421
timings,
402422
execution_result,
403423
)
@@ -408,13 +428,15 @@ mod checked {
408428
InnerTemporaryStore,
409429
SuiGasStatus,
410430
TransactionEffects,
431+
GasDetails,
411432
Vec<ExecutionTiming>,
412433
Result<Mode::ExecutionResults, ExecutionError>,
413434
),
414435
new_effects: &(
415436
InnerTemporaryStore,
416437
SuiGasStatus,
417438
TransactionEffects,
439+
GasDetails,
418440
Vec<ExecutionTiming>,
419441
Result<Mode::ExecutionResults, ExecutionError>,
420442
),
@@ -453,6 +475,8 @@ mod checked {
453475
normal_effects.2.transaction_digest().to_string(),
454476
&new_effects.1.gas_usage_report(),
455477
&normal_effects.1.gas_usage_report(),
478+
&new_effects.3,
479+
&normal_effects.3,
456480
);
457481

458482
if !ok {

sui-execution/latest/sui-adapter/src/static_programmable_transactions/metering/translation_meter.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,32 @@ use sui_types::error::{ExecutionError, ExecutionErrorKind};
1414
pub struct TranslationMeter<'pc, 'gas> {
1515
protocol_config: &'pc ProtocolConfig,
1616
charger: &'gas mut GasCharger,
17+
translation_gas: u64,
18+
}
19+
20+
impl<'pc, 'gas> Drop for TranslationMeter<'pc, 'gas> {
21+
fn drop(&mut self) {
22+
let final_gas = self.charger.move_gas_status().gas_used_pre_gas_price();
23+
if final_gas > self.translation_gas {
24+
self.charger.move_gas_status_mut().set_transl_gas(
25+
final_gas
26+
.checked_sub(self.translation_gas)
27+
.expect("translation gas should not fail"),
28+
);
29+
}
30+
}
1731
}
1832

1933
impl<'pc, 'gas> TranslationMeter<'pc, 'gas> {
2034
pub fn new(
2135
protocol_config: &'pc ProtocolConfig,
2236
gas_charger: &'gas mut GasCharger,
2337
) -> TranslationMeter<'pc, 'gas> {
38+
let translation_gas = gas_charger.move_gas_status().gas_used_pre_gas_price();
2439
TranslationMeter {
2540
protocol_config,
2641
charger: gas_charger,
42+
translation_gas,
2743
}
2844
}
2945

sui-execution/latest/sui-adapter/src/static_programmable_transactions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub fn execute<Mode: ExecutionMode>(
6464
};
6565
let txn = typing::translate_and_verify::<Mode>(&mut translation_meter, &env, txn)
6666
.map_err(|e| (e, vec![]))?;
67+
drop(translation_meter);
6768
execution::interpreter::execute::<Mode>(
6869
&mut env,
6970
metrics,

0 commit comments

Comments
 (0)