Skip to content

Commit 298e7ed

Browse files
committed
Track detailed gas
1 parent bfb643e commit 298e7ed

File tree

4 files changed

+76
-11
lines changed

4 files changed

+76
-11
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: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ mod checked {
99
use crate::execution_mode::{self, ExecutionMode};
1010
use crate::execution_value::SuiResolver;
1111
use csv::Writer;
12-
use lz4_flex::frame::FrameEncoder;
1312
use move_binary_format::CompiledModule;
1413
use move_trace_format::format::MoveTraceBuilder;
1514
use move_vm_runtime::move_vm::MoveVM;
@@ -19,7 +18,6 @@ mod checked {
1918
use std::fs::File;
2019
use std::io::BufWriter;
2120
use std::sync::Mutex;
22-
use similar::TextDiff;
2321
use std::{cell::RefCell, collections::HashSet, rc::Rc, sync::Arc};
2422
use sui_types::accumulator_root::{ACCUMULATOR_ROOT_CREATE_FUNC, ACCUMULATOR_ROOT_MODULE};
2523
use sui_types::balance::{
@@ -28,6 +26,7 @@ mod checked {
2826
};
2927
use sui_types::execution_params::ExecutionOrEarlyError;
3028
use sui_types::gas_coin::GAS;
29+
use sui_types::gas_model::tables::GasDetails;
3130
use sui_types::messages_checkpoint::CheckpointTimestamp;
3231
use sui_types::metrics::LimitsMetrics;
3332
use sui_types::object::OBJECT_START_VERSION;
@@ -69,7 +68,6 @@ mod checked {
6968
use sui_types::error::{ExecutionError, ExecutionErrorKind};
7069
use sui_types::execution::{ExecutionTiming, ResultWithTimings};
7170
use sui_types::execution_status::{ExecutionFailureStatus, ExecutionStatus};
72-
use sui_types::gas::GasCostSummary;
7371
use sui_types::gas::SuiGasStatus;
7472
use sui_types::gas::{GasCostSummary, GasUsageReport};
7573
use sui_types::id::UID;
@@ -93,14 +91,13 @@ mod checked {
9391
sui_system_state::{ADVANCE_EPOCH_FUNCTION_NAME, SUI_SYSTEM_MODULE_NAME},
9492
};
9593

96-
static CSV_WRITER: Lazy<Mutex<csv::Writer<FrameEncoder<std::io::BufWriter<std::fs::File>>>>> =
94+
static CSV_WRITER: Lazy<Mutex<csv::Writer<std::io::BufWriter<std::fs::File>>>> =
9795
once_cell::sync::Lazy::new(|| {
98-
let file = File::create("/opt/sui/gas.csv.lz4").expect("failed to create file");
99-
let enc = BufWriter::new(file);
100-
let enc = FrameEncoder::new(enc);
101-
let mut writer = Writer::from_writer(enc);
96+
let file = File::create("/opt/sui/gas.csv").expect("failed to create file");
97+
let buf = BufWriter::new(file);
98+
let mut writer = Writer::from_writer(buf);
10299
writer
103-
.write_record(&[
100+
.write_record([
104101
"tx_digest",
105102
"gas_budget",
106103
"new_computation_cost",
@@ -111,6 +108,13 @@ mod checked {
111108
"old_storage_rebate",
112109
"new_gas_used",
113110
"old_gas_used",
111+
"new_instructions",
112+
"old_instructions",
113+
"new_stack_height",
114+
"old_stack_hight",
115+
"new_memory_allocated",
116+
"old_memory_allocated",
117+
"translation",
114118
])
115119
.expect("failed to write gas header");
116120
Mutex::new(writer)
@@ -120,13 +124,15 @@ mod checked {
120124
transaction_digest: String,
121125
new_gas: &GasUsageReport,
122126
old_gas: &GasUsageReport,
127+
new_details: &GasDetails,
128+
old_details: &GasDetails,
123129
) {
124130
if new_gas.cost_summary == old_gas.cost_summary {
125131
return;
126132
}
127133
let mut writer = CSV_WRITER.lock().unwrap();
128134
writer
129-
.write_record(&[
135+
.write_record([
130136
&transaction_digest,
131137
&new_gas.gas_budget.to_string(),
132138
&new_gas.cost_summary.computation_cost.to_string(),
@@ -137,6 +143,13 @@ mod checked {
137143
&old_gas.cost_summary.storage_rebate.to_string(),
138144
&new_gas.gas_used.to_string(),
139145
&old_gas.gas_used.to_string(),
146+
&new_details.instructions_executed.to_string(),
147+
&old_details.instructions_executed.to_string(),
148+
&new_details.stack_height.to_string(),
149+
&old_details.stack_height.to_string(),
150+
&new_details.memory_allocated.to_string(),
151+
&old_details.memory_allocated.to_string(),
152+
&new_details.transl_gas_used.to_string(),
140153
])
141154
.expect("failed to write gas row");
142155
writer.flush().expect("failed to flush gas writer");
@@ -208,7 +221,8 @@ mod checked {
208221

209222
compare_effects::<Mode>(&normal_effects, &new_effects);
210223

211-
normal_effects
224+
let (temp_store, gas_status, effects, _gas_details, timing, res) = normal_effects;
225+
(temp_store, gas_status, effects, timing, res)
212226
}
213227

214228
#[instrument(name = "new_tx_execute_to_effects", level = "debug", skip_all)]
@@ -232,6 +246,7 @@ mod checked {
232246
InnerTemporaryStore,
233247
SuiGasStatus,
234248
TransactionEffects,
249+
GasDetails,
235250
Vec<ExecutionTiming>,
236251
Result<Mode::ExecutionResults, ExecutionError>,
237252
) {
@@ -306,6 +321,8 @@ mod checked {
306321
trace_builder_opt,
307322
);
308323

324+
let gas_details = gas_charger.move_gas_status().gas_details();
325+
309326
let status = if let Err(error) = &execution_result {
310327
// Elaborate errors in logs if they are unexpected or their status is terse.
311328
use ExecutionErrorKind as K;
@@ -398,6 +415,7 @@ mod checked {
398415
inner,
399416
gas_charger.into_gas_status(),
400417
effects,
418+
gas_details,
401419
timings,
402420
execution_result,
403421
)
@@ -408,13 +426,15 @@ mod checked {
408426
InnerTemporaryStore,
409427
SuiGasStatus,
410428
TransactionEffects,
429+
GasDetails,
411430
Vec<ExecutionTiming>,
412431
Result<Mode::ExecutionResults, ExecutionError>,
413432
),
414433
new_effects: &(
415434
InnerTemporaryStore,
416435
SuiGasStatus,
417436
TransactionEffects,
437+
GasDetails,
418438
Vec<ExecutionTiming>,
419439
Result<Mode::ExecutionResults, ExecutionError>,
420440
),
@@ -453,6 +473,8 @@ mod checked {
453473
normal_effects.2.transaction_digest().to_string(),
454474
&new_effects.1.gas_usage_report(),
455475
&normal_effects.1.gas_usage_report(),
476+
&new_effects.3,
477+
&normal_effects.3,
456478
);
457479

458480
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)