nexus-xyz / nexus-zkvm

The Nexus zkVM: The zero-knowledge virtual machine
https://docs.nexus.xyz
Apache License 2.0
661 stars 220 forks source link

Add RISC-V profiling cycles #243

Closed duc-nx closed 1 month ago

duc-nx commented 2 months ago

This PR adds proc macro #[nexus_rt::profile]:

Example output:

$ assets/scripts/smoke.sh examples/src/bin/fib3_profiling.rs
00002f4 00052503 lw x10, x10, 0                          3      2f8     3326
00002f8 0005a583 lw x11, x11, 0                          3      2fc     3329
00002fc 04b50863 beq x10, x11, 50                        0      34c     3331
000034c 07c12083 lw x1, x2, 7c                          9c      350     3334
0000350 08010113 addi x2, x2, 80                    3fffec      354     3335
0000354 00008067 jalr x0, x1, 0                        358       9c     3336
000009c 00c12083 lw x1, x2, c                           30       a0     3339
00000a0 01010113 addi x2, x2, 10                    3ffffc       a4     3340
00000a4 00008067 jalr x0, x1, 0                         a8       30     3341
0000030 c0001073 unimp                                   0       30     3341
....

Executed 1609 instructions in 348.346875ms
Function 'nexus-project-ci/src/main.rs:fib' took 1050 cycles, which is 31% of the total execution time
Function 'nexus-project-ci/src/main.rs:fib2' took 1979 cycles, which is 59% of the total execution time
Total program cycles: 3341
vercel[bot] commented 2 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nexus-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 6, 2024 4:28pm
duc-nx commented 1 month ago

Improved the overhead from cycle counting.

00002f4 00052503 lw x10, x10, 0                          3      2f8     3326
00002f8 0005a583 lw x11, x11, 0                          3      2fc     3329
00002fc 04b50863 beq x10, x11, 50                        0      34c     3331
000034c 07c12083 lw x1, x2, 7c                          9c      350     3334
0000350 08010113 addi x2, x2, 80                    3fffec      354     3335
0000354 00008067 jalr x0, x1, 0                        358       9c     3336
000009c 00c12083 lw x1, x2, c                           30       a0     3339
00000a0 01010113 addi x2, x2, 10                    3ffffc       a4     3340
00000a4 00008067 jalr x0, x1, 0                         a8       30     3341
0000030 c0001073 unimp                                   0       30     3341

Final Machine State: pc: 30
  x00:        0  x01:       30  x02:   3ffffc  x03:     3c90  x04:        0  x05:        0  x06:        0  x07:        0
  x08:   3ffffc  x09:        0  x10:        3  x11:        3  x12:       23  x13:        0  x14:        0  x15:        0
  x16:        0  x17:        0  x18:        0  x19:        0  x20:        0  x21:        0  x22:        0  x23:        0
  x24:        0  x25:        0  x26:        0  x27:        0  x28:        0  x29:        0  x30:        0  x31:        0

Executed 1609 instructions in 348.346875ms
Function 'nexus-project-ci/src/main.rs:fib' took 1050 cycles, which is 31% of the total execution time
Function 'nexus-project-ci/src/main.rs:fib2' took 1979 cycles, which is 59% of the total execution time
Total program cycles: 3341
yoichi-nexus commented 1 month ago

Is there a reason to avoid using release build with debugging information so that eval.rs can look up the function name from the program counter?

duc-nx commented 1 month ago

Nicer output:

Execution Summary:
└── Total program cycles: 2016
    ├──  'src/main.rs:fib': 1050 cycles (52% of total)
    └──  'src/main.rs:fib2': 664 cycles (32% of total)