Skip to content

Commit 8198784

Browse files
committed
reduce the tracer to just increment one number
1 parent fd708cf commit 8198784

File tree

1 file changed

+105
-67
lines changed
  • core/lib/multivm/src/versions/vm_fast

1 file changed

+105
-67
lines changed

core/lib/multivm/src/versions/vm_fast/vm.rs

Lines changed: 105 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,22 @@ pub(crate) const LOG_DECOMMIT_DECOMMITTER_SORTER_CYCLES: u32 = 1;
108108

109109
#[derive(Debug, Default, Clone, PartialEq)]
110110
pub struct CircuitsTracer {
111-
main_vm_cycles: u32,
112-
ram_permutation_cycles: u32,
113-
storage_application_cycles: u32,
114-
storage_sorter_cycles: u32,
115-
code_decommitter_cycles: u32,
116-
code_decommitter_sorter_cycles: u32,
117-
log_demuxer_cycles: u32,
118-
events_sorter_cycles: u32,
119-
keccak256_cycles: u32,
120-
ecrecover_cycles: u32,
121-
sha256_cycles: u32,
122-
secp256k1_verify_cycles: u32,
123-
transient_storage_checker_cycles: u32,
111+
rich_addressing_opcodes: u32,
112+
average_opcodes: u32,
113+
storage_reads: u32,
114+
storage_writes: u32,
115+
transient_storage_reads: u32,
116+
transient_storage_writes: u32,
117+
events: u32,
118+
precompile_calls: u32,
119+
decommits: u32,
120+
far_calls: u32,
121+
heap_writes: u32,
122+
heap_reads: u32,
124123
}
125124

126125
impl Tracer for CircuitsTracer {
127126
fn after_instruction<OP: OpcodeType, S: StateInterface>(&mut self, _state: &mut S) {
128-
self.main_vm_cycles += 1;
129-
130127
match OP::VALUE {
131128
Opcode::Nop
132129
| Opcode::Add
@@ -145,7 +142,7 @@ impl Tracer for CircuitsTracer {
145142
| Opcode::PointerSub
146143
| Opcode::PointerPack
147144
| Opcode::PointerShrink => {
148-
self.ram_permutation_cycles += RICH_ADDRESSING_OPCODE_RAM_CYCLES;
145+
self.rich_addressing_opcodes += 1;
149146
}
150147
Opcode::This
151148
| Opcode::Caller
@@ -159,91 +156,132 @@ impl Tracer for CircuitsTracer {
159156
| Opcode::IncrementTxNumber
160157
| Opcode::Ret(_)
161158
| Opcode::NearCall => {
162-
self.ram_permutation_cycles += AVERAGE_OPCODE_RAM_CYCLES;
159+
self.average_opcodes += 1;
163160
}
164161
Opcode::StorageRead => {
165-
self.ram_permutation_cycles += STORAGE_READ_RAM_CYCLES;
166-
self.log_demuxer_cycles += STORAGE_READ_LOG_DEMUXER_CYCLES;
167-
self.storage_sorter_cycles += STORAGE_READ_STORAGE_SORTER_CYCLES;
162+
self.storage_reads += 1;
168163
}
169164
Opcode::TransientStorageRead => {
170-
self.ram_permutation_cycles += TRANSIENT_STORAGE_READ_RAM_CYCLES;
171-
self.log_demuxer_cycles += TRANSIENT_STORAGE_READ_LOG_DEMUXER_CYCLES;
172-
self.transient_storage_checker_cycles +=
173-
TRANSIENT_STORAGE_READ_TRANSIENT_STORAGE_CHECKER_CYCLES;
165+
self.transient_storage_reads += 1;
174166
}
175167
Opcode::StorageWrite => {
176-
self.ram_permutation_cycles += STORAGE_WRITE_RAM_CYCLES;
177-
self.log_demuxer_cycles += STORAGE_WRITE_LOG_DEMUXER_CYCLES;
178-
self.storage_sorter_cycles += STORAGE_WRITE_STORAGE_SORTER_CYCLES;
168+
self.storage_writes += 1;
179169
}
180170
Opcode::TransientStorageWrite => {
181-
self.ram_permutation_cycles += TRANSIENT_STORAGE_WRITE_RAM_CYCLES;
182-
self.log_demuxer_cycles += TRANSIENT_STORAGE_WRITE_LOG_DEMUXER_CYCLES;
183-
self.transient_storage_checker_cycles +=
184-
TRANSIENT_STORAGE_WRITE_TRANSIENT_STORAGE_CHECKER_CYCLES;
171+
self.transient_storage_writes += 1;
185172
}
186173
Opcode::L2ToL1Message | Opcode::Event => {
187-
self.ram_permutation_cycles += EVENT_RAM_CYCLES;
188-
self.log_demuxer_cycles += EVENT_LOG_DEMUXER_CYCLES;
189-
self.events_sorter_cycles += EVENT_EVENTS_SORTER_CYCLES;
174+
self.events += 1;
190175
}
191176
Opcode::PrecompileCall => {
192-
self.ram_permutation_cycles += PRECOMPILE_RAM_CYCLES;
193-
self.log_demuxer_cycles += PRECOMPILE_LOG_DEMUXER_CYCLES;
177+
self.precompile_calls += 1;
194178
}
195179
Opcode::Decommit => {
196-
// Note, that for decommit the log demuxer circuit is not used.
197-
self.ram_permutation_cycles += LOG_DECOMMIT_RAM_CYCLES;
198-
self.code_decommitter_sorter_cycles += LOG_DECOMMIT_DECOMMITTER_SORTER_CYCLES;
180+
self.decommits += 1;
199181
}
200182
Opcode::FarCall(_) => {
201-
self.ram_permutation_cycles += FAR_CALL_RAM_CYCLES;
202-
self.code_decommitter_sorter_cycles += FAR_CALL_CODE_DECOMMITTER_SORTER_CYCLES;
203-
self.storage_sorter_cycles += FAR_CALL_STORAGE_SORTER_CYCLES;
204-
self.log_demuxer_cycles += FAR_CALL_LOG_DEMUXER_CYCLES;
183+
self.far_calls += 1;
205184
}
206185
Opcode::AuxHeapWrite | Opcode::HeapWrite /* StaticMemoryWrite */ => {
207-
self.ram_permutation_cycles += UMA_WRITE_RAM_CYCLES;
186+
self.heap_writes += 1;
208187
}
209188
Opcode::AuxHeapRead | Opcode::HeapRead | Opcode::PointerRead /* StaticMemoryRead */ => {
210-
self.ram_permutation_cycles += UMA_READ_RAM_CYCLES;
189+
self.heap_reads += 1;
211190
}
212191
}
213192
}
214193
}
215194

216195
impl CircuitsTracer {
217-
fn circuit_statistic(&self) -> CircuitStatistic {
196+
fn circuit_statistic(&self, extra: ExtraDataForCircuits) -> CircuitStatistic {
197+
let ExtraDataForCircuits {
198+
keccak256_cycles,
199+
ecrecover_cycles,
200+
sha256_cycles,
201+
secp256k1_verify_cycles,
202+
code_decommitter_cycles,
203+
storage_application_cycles,
204+
} = extra;
218205
CircuitStatistic {
219-
main_vm: self.main_vm_cycles as f32 / GEOMETRY_CONFIG.cycles_per_vm_snapshot as f32,
220-
ram_permutation: self.ram_permutation_cycles as f32
206+
main_vm: (self.rich_addressing_opcodes
207+
+ self.average_opcodes
208+
+ self.storage_reads
209+
+ self.storage_writes
210+
+ self.transient_storage_reads
211+
+ self.transient_storage_writes
212+
+ self.events
213+
+ self.precompile_calls
214+
+ self.decommits
215+
+ self.far_calls
216+
+ self.heap_writes
217+
+ self.heap_reads) as f32
218+
/ GEOMETRY_CONFIG.cycles_per_vm_snapshot as f32,
219+
ram_permutation: (self.rich_addressing_opcodes * RICH_ADDRESSING_OPCODE_RAM_CYCLES
220+
+ self.average_opcodes * AVERAGE_OPCODE_RAM_CYCLES
221+
+ self.storage_reads * STORAGE_READ_RAM_CYCLES
222+
+ self.storage_writes * STORAGE_WRITE_RAM_CYCLES
223+
+ self.transient_storage_reads * TRANSIENT_STORAGE_READ_RAM_CYCLES
224+
+ self.transient_storage_writes * TRANSIENT_STORAGE_WRITE_RAM_CYCLES
225+
+ self.events * EVENT_RAM_CYCLES
226+
+ self.precompile_calls * PRECOMPILE_RAM_CYCLES
227+
+ self.decommits * LOG_DECOMMIT_RAM_CYCLES
228+
+ self.far_calls * FAR_CALL_RAM_CYCLES
229+
+ self.heap_writes * UMA_WRITE_RAM_CYCLES
230+
+ self.heap_reads * UMA_READ_RAM_CYCLES) as f32
221231
/ GEOMETRY_CONFIG.cycles_per_ram_permutation as f32,
222-
storage_application: self.storage_application_cycles as f32
232+
storage_application: storage_application_cycles as f32
223233
/ GEOMETRY_CONFIG.cycles_per_storage_application as f32,
224-
storage_sorter: self.storage_sorter_cycles as f32
234+
storage_sorter: (self.storage_reads * STORAGE_READ_STORAGE_SORTER_CYCLES
235+
+ self.storage_writes * STORAGE_WRITE_STORAGE_SORTER_CYCLES
236+
+ self.transient_storage_reads
237+
* TRANSIENT_STORAGE_READ_TRANSIENT_STORAGE_CHECKER_CYCLES
238+
+ self.transient_storage_writes
239+
* TRANSIENT_STORAGE_WRITE_TRANSIENT_STORAGE_CHECKER_CYCLES
240+
+ self.far_calls * FAR_CALL_STORAGE_SORTER_CYCLES)
241+
as f32
225242
/ GEOMETRY_CONFIG.cycles_per_storage_sorter as f32,
226-
code_decommitter: self.code_decommitter_cycles as f32
243+
code_decommitter: code_decommitter_cycles as f32
227244
/ GEOMETRY_CONFIG.cycles_per_code_decommitter as f32,
228-
code_decommitter_sorter: self.code_decommitter_sorter_cycles as f32
245+
code_decommitter_sorter: (self.decommits * LOG_DECOMMIT_DECOMMITTER_SORTER_CYCLES
246+
+ self.far_calls * FAR_CALL_CODE_DECOMMITTER_SORTER_CYCLES)
247+
as f32
229248
/ GEOMETRY_CONFIG.cycles_code_decommitter_sorter as f32,
230-
log_demuxer: self.log_demuxer_cycles as f32
249+
log_demuxer: (self.storage_reads * STORAGE_READ_LOG_DEMUXER_CYCLES
250+
+ self.storage_writes * STORAGE_WRITE_LOG_DEMUXER_CYCLES
251+
+ self.transient_storage_reads * TRANSIENT_STORAGE_READ_LOG_DEMUXER_CYCLES
252+
+ self.transient_storage_writes * TRANSIENT_STORAGE_WRITE_LOG_DEMUXER_CYCLES
253+
+ self.events * EVENT_LOG_DEMUXER_CYCLES
254+
+ self.precompile_calls * PRECOMPILE_LOG_DEMUXER_CYCLES
255+
+ self.far_calls * FAR_CALL_LOG_DEMUXER_CYCLES) as f32
231256
/ GEOMETRY_CONFIG.cycles_per_log_demuxer as f32,
232-
events_sorter: self.events_sorter_cycles as f32
257+
events_sorter: (self.events * EVENT_EVENTS_SORTER_CYCLES) as f32
233258
/ GEOMETRY_CONFIG.cycles_per_events_or_l1_messages_sorter as f32,
234-
keccak256: self.keccak256_cycles as f32
259+
keccak256: keccak256_cycles as f32
235260
/ GEOMETRY_CONFIG.cycles_per_keccak256_circuit as f32,
236-
ecrecover: self.ecrecover_cycles as f32
261+
ecrecover: ecrecover_cycles as f32
237262
/ GEOMETRY_CONFIG.cycles_per_ecrecover_circuit as f32,
238-
sha256: self.sha256_cycles as f32 / GEOMETRY_CONFIG.cycles_per_sha256_circuit as f32,
239-
secp256k1_verify: self.secp256k1_verify_cycles as f32
263+
sha256: sha256_cycles as f32 / GEOMETRY_CONFIG.cycles_per_sha256_circuit as f32,
264+
secp256k1_verify: secp256k1_verify_cycles as f32
240265
/ GEOMETRY_CONFIG.cycles_per_secp256r1_verify_circuit as f32,
241-
transient_storage_checker: self.transient_storage_checker_cycles as f32
266+
transient_storage_checker: (self.transient_storage_reads
267+
* TRANSIENT_STORAGE_READ_TRANSIENT_STORAGE_CHECKER_CYCLES
268+
+ self.transient_storage_writes
269+
* TRANSIENT_STORAGE_WRITE_TRANSIENT_STORAGE_CHECKER_CYCLES)
270+
as f32
242271
/ GEOMETRY_CONFIG.cycles_per_transient_storage_sorter as f32,
243272
}
244273
}
245274
}
246275

276+
struct ExtraDataForCircuits {
277+
keccak256_cycles: u32,
278+
ecrecover_cycles: u32,
279+
sha256_cycles: u32,
280+
secp256k1_verify_cycles: u32,
281+
code_decommitter_cycles: u32,
282+
storage_application_cycles: u32,
283+
}
284+
247285
pub struct Vm<S> {
248286
pub(crate) world: World<S, CircuitsTracer>,
249287
pub(crate) inner: VirtualMachine<CircuitsTracer>,
@@ -682,14 +720,14 @@ impl<S: ReadStorage> VmInterface for Vm<S> {
682720

683721
let pubdata_after = self.inner.world_diff.pubdata();
684722

685-
self.tracer.keccak256_cycles = self.inner.state.keccak256_cycles as u32;
686-
self.tracer.ecrecover_cycles = self.inner.state.ecrecover_cycles as u32;
687-
self.tracer.sha256_cycles = self.inner.state.sha256_cycles as u32;
688-
self.tracer.ecrecover_cycles = self.inner.state.ecrecover_cycles as u32;
689-
self.tracer.code_decommitter_cycles = self.inner.state.code_decommitter_cycles as u32;
690-
self.tracer.storage_application_cycles = self.inner.state.storage_application_cycles as u32;
691-
692-
let circuit_statistic = self.tracer.circuit_statistic();
723+
let circuit_statistic = self.tracer.circuit_statistic(ExtraDataForCircuits {
724+
keccak256_cycles: self.inner.state.keccak256_cycles as u32,
725+
ecrecover_cycles: self.inner.state.ecrecover_cycles as u32,
726+
sha256_cycles: self.inner.state.sha256_cycles as u32,
727+
secp256k1_verify_cycles: self.inner.state.secp256v1_verify_cycles as u32,
728+
code_decommitter_cycles: self.inner.state.code_decommitter_cycles as u32,
729+
storage_application_cycles: self.inner.state.storage_application_cycles as u32,
730+
});
693731

694732
VmExecutionResultAndLogs {
695733
result,

0 commit comments

Comments
 (0)