Skip to content

Conversation

@ilitteri
Copy link
Contributor

@ilitteri ilitteri commented Nov 6, 2025

Motivation

For SP1 we already have profiling tools like Samply to generate flamegraphs of the execution and get an idea of the spent cycles during the execution, but it is not always desired to run those.

As an alternative, granulating the current cycle's report is very useful.

Description

  • Decouple L1 stateless validation
  • Add report_cycles! macro that wraps a block of code with optional cycle-tracking prints
  • Wrap code blocks inside the report_cycles! macro to track cycles of them.

Example Output

2025-11-06T20:27:17.842628Z  INFO ethrex_replay::fetcher: Getting block 23721540 data from cache
2025-11-06T20:27:22.669322Z  INFO execute: sp1_core_executor::syscalls::write: ┌╴read_input
2025-11-06T20:27:23.074825Z  INFO execute: sp1_core_executor::syscalls::write: └╴12,868,356 cycles
2025-11-06T20:27:23.074862Z  INFO execute: sp1_core_executor::syscalls::write: ┌╴execution
2025-11-06T20:27:23.074915Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴GuestProgramState::from(execution_witness)
stderr: WARNING: Using insecure random number generator.
2025-11-06T20:27:28.337580Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴191,414,247 cycles
2025-11-06T20:27:28.337737Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴initialize_block_header_hashes
2025-11-06T20:27:28.338575Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴11,402 cycles
2025-11-06T20:27:28.338592Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴get_first_invalid_block_hash
2025-11-06T20:27:28.338617Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴585 cycles
2025-11-06T20:27:28.338707Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴state_trie_root
2025-11-06T20:27:29.836352Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴38,685,313 cycles
2025-11-06T20:27:29.836390Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴validate_block
2025-11-06T20:27:29.837260Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴16,766 cycles
2025-11-06T20:27:29.837278Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴setup_evm
2025-11-06T20:27:29.837297Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴458 cycles
2025-11-06T20:27:29.837310Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴execute_block
2025-11-06T20:27:57.108787Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴531,651,252 cycles
2025-11-06T20:27:57.108819Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴get_state_transitions
2025-11-06T20:27:57.175020Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴2,383,891 cycles
2025-11-06T20:27:57.175052Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴apply_account_updates
2025-11-06T20:27:59.351855Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴60,042,986 cycles
2025-11-06T20:27:59.381137Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴validate_gas_and_receipts
2025-11-06T20:27:59.381168Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴379 cycles
2025-11-06T20:27:59.381180Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴validate_receipts_root
2025-11-06T20:27:59.621535Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴7,450,196 cycles
2025-11-06T20:27:59.621572Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴validate_requests_hash
2025-11-06T20:27:59.621785Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴2,734 cycles
2025-11-06T20:27:59.621851Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴get_final_state_root
2025-11-06T20:28:00.570636Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴24,414,357 cycles
2025-11-06T20:28:00.570669Z  INFO execute: sp1_core_executor::syscalls::write: │ ┌╴validate_state_root
2025-11-06T20:28:00.570689Z  INFO execute: sp1_core_executor::syscalls::write: │ └╴624 cycles
2025-11-06T20:28:00.652210Z  INFO execute: sp1_core_executor::syscalls::write: └╴858,494,608 cycles
2025-11-06T20:28:00.652242Z  INFO execute: sp1_core_executor::syscalls::write: ┌╴commit_public_inputs
2025-11-06T20:28:00.652577Z  INFO execute: sp1_core_executor::syscalls::write: └╴1,632 cycles
2025-11-06T20:28:00.731639Z  INFO execute: sp1_prover: gas: 1145215937
2025-11-06T20:28:00.733918Z  INFO ethrex_prover_lib::backend::sp1: Successfully executed SP1 program in 38.09s
2025-11-06T20:28:00.734204Z  INFO ethrex_replay::report: [mainnet] Block: 23721540, Gas: 20284615, #Txs: 223, Execution Time: 42s 794ms | https://etherscan.io/block/23721540

@ilitteri ilitteri self-assigned this Nov 6, 2025
Copilot AI review requested due to automatic review settings November 6, 2025 21:20
@ilitteri ilitteri requested a review from a team as a code owner November 6, 2025 21:20
@ilitteri ilitteri added the L2 Rollup client label Nov 6, 2025
@github-actions
Copy link

github-actions bot commented Nov 6, 2025

Lines of code report

Total lines added: 104
Total lines removed: 0
Total lines changed: 104

Detailed view
+------------------------------------------------------------+-------+------+
| File                                                       | Lines | Diff |
+------------------------------------------------------------+-------+------+
| ethrex/crates/l2/prover/src/guest_program/src/execution.rs | 505   | +94  |
+------------------------------------------------------------+-------+------+
| ethrex/crates/l2/prover/src/guest_program/src/lib.rs       | 22    | +10  |
+------------------------------------------------------------+-------+------+

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds cycle tracking capabilities for the SP1 zkVM and refactors the stateless_validation_l1 function to inline the execution logic. The main changes include introducing a report_cycles! macro that wraps code blocks with cycle tracking instrumentation, and expanding the previously abstracted execute_stateless call into explicit step-by-step validation and execution logic with cycle tracking at each step.

Key Changes

  • Introduced report_cycles! macro for cycle tracking in SP1 zkVM with a new sp1-cycles feature flag
  • Refactored stateless_validation_l1 to inline block execution logic with granular cycle tracking
  • Changed function signature from borrowing blocks (&[Block]) to taking ownership (Vec<Block>)

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
crates/l2/prover/src/guest_program/Cargo.toml Added sp1-cycles feature flag for cycle tracking
crates/l2/prover/src/guest_program/src/sp1/Cargo.toml Enabled report-cycles feature for guest_program dependency (has naming inconsistency)
crates/l2/prover/src/guest_program/src/lib.rs Introduced report_cycles! macro with cycle tracking instrumentation
crates/l2/prover/src/guest_program/src/execution.rs Refactored stateless_validation_l1 to inline execution logic with cycle tracking at each major step

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

ilitteri and others added 3 commits November 6, 2025 18:30
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
/// When the feature "sp1-cycles" is enabled, it will print start and end cycle
/// tracking messages that are compatible with SP1's cycle tracking system.
#[macro_export]
macro_rules! report_cycles {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this macro not always report cycles, maybe another name like try_report_cycles or something else

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L2 Rollup client

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants