-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·443 lines (414 loc) · 13.8 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·443 lines (414 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/bin/bash
#
# Usage: ./run.sh [OPTIONS]
#
# Options:
# --mode <MODE> Set the proving mode (default: prove-app)
# Valid modes: prove-app, prove-stark, prove-evm, keygen, generate-vm-vkey
# --generate-vm-vkey Shortcut for --mode generate-vm-vkey
# --profile <PROFILE> Set the Cargo build profile (default: profiling)
# Valid profiles: dev, release, profiling
# --block <N> Set the block number to prove (default: 24001988)
# --app-log-blowup <N>
# --app-l-skip <N> Log of univariate skip domain size (default: 4)
# --leaf-log-blowup <N>
# --internal-log-blowup <N>
# --root-log-blowup <N>
# --num-children-leaf <N>
# --num-children-internal <N>
# --segment-max-memory <N>
# --cuda Force CUDA acceleration (auto-detected if nvidia-smi available)
# --tco Use TCO instead of AOT (default is AOT on x86_64)
# --perf Run with perf + samply host profiling and upload to Firefox Profiler
# --nsys Run with nsys profiling and output summary stats
# --nsys-gpu-metrics Include nsys GPU hardware metrics (`--gpu-metrics-devices=all`).
# Higher overhead; use for hardware-counter analysis, not baseline timing.
# --<tool> Run with compute-sanitizer --tool <tool> where tool is one of memcheck, synccheck, or racecheck
# --proof-cache <DIR> Directory to cache the intermediate stark proof for prove-root mode.
# If set, the stark proof is stored at <DIR>/stark.bitcode and reused
# on subsequent runs. If unset, no proof caching is performed.
#
# Examples:
# ./run.sh # Run with defaults
# ./run.sh --mode prove-stark # Run in prove-stark mode
# ./run.sh --profile release # Build with release profile
# ./run.sh --cuda --mode prove-app # Force CUDA with prove-app mode
# ./run.sh --perf --mode execute # Run with host profiling (Firefox Profiler link)
# ./run.sh --nsys --mode prove-app # Run with nsys profiling
# ./run.sh --nsys --nsys-gpu-metrics --mode prove-app
# ./run.sh --block 24001988 # Prove a specific block
# ./run.sh --mode generate-vm-vkey # Generate reth.vm.vk locally
# ./run.sh --generate-vm-vkey # Same as above (shortcut)
#
set -e
REPO_ROOT=$(git rev-parse --show-toplevel)
WORKDIR=$REPO_ROOT
RUST_TOOLCHAIN=$(sed -n 's/^channel = "\(.*\)"/\1/p' "$REPO_ROOT/rust-toolchain.toml")
DEST="$REPO_ROOT/bin/reth-benchmark/elf/openvm-stateless-guest"
build_openvm_guest_elf() {
cd "$REPO_ROOT/bin/stateless-guest"
OPENVM_RUST_TOOLCHAIN=$RUST_TOOLCHAIN cargo openvm build
mkdir -p ../reth-benchmark/elf
SRC="target/riscv32im-risc0-zkvm-elf/release/openvm-stateless-guest"
cp "$SRC" "$DEST"
cd "$WORKDIR"
}
cd $WORKDIR
# =============== GPU memory usage monitoring ============================
source "$REPO_ROOT/scripts/gpu_monitor.sh"
GPU_LOG_FILE="$WORKDIR/gpu_memory_usage.csv"
trap finalize_gpu_monitor EXIT
NVIDIA_SMI_READY=false
if command -v nvidia-smi >/dev/null 2>&1 && nvidia-smi >/dev/null 2>&1; then
NVIDIA_SMI_READY=true
fi
# Parse command-line arguments
MODE_OVERRIDE=""
PROFILE_OVERRIDE=""
BLOCK_NUMBER_OVERRIDE=""
USE_CUDA=false
CUDA_REASON=""
USE_TCO=false
USE_PERF=false
USE_NSYS=false
USE_NSYS_GPU_METRICS=false
USE_NCU=false
COMPUTE_SANITIZER_ARGS=""
while [[ $# -gt 0 ]]; do
case $1 in
--mode)
MODE_OVERRIDE="$2"
shift 2
;;
--profile)
PROFILE_OVERRIDE="$2"
shift 2
;;
--generate-vm-vkey)
MODE_OVERRIDE="generate-vm-vkey"
shift
;;
--block)
BLOCK_NUMBER_OVERRIDE="$2"
shift 2
;;
--segment-max-memory)
SEGMENT_MAX_MEMORY="$2"
shift 2
;;
--app-log-blowup)
APP_LOG_BLOWUP="$2"
shift 2
;;
--leaf-log-blowup)
LEAF_LOG_BLOWUP="$2"
shift 2
;;
--internal-log-blowup)
INTERNAL_LOG_BLOWUP="$2"
shift 2
;;
--root-log-blowup)
ROOT_LOG_BLOWUP="$2"
shift 2
;;
--num-children-leaf)
NUM_CHILDREN_LEAF="$2"
shift 2
;;
--num-children-internal)
NUM_CHILDREN_INTERNAL="$2"
shift 2
;;
--app-l-skip)
APP_L_SKIP="$2"
shift 2
;;
--cuda)
USE_CUDA=true
CUDA_REASON="requested via --cuda script argument"
shift
;;
--tco)
USE_TCO=true
shift
;;
--perf)
USE_PERF=true
shift
;;
--nsys)
USE_NSYS=true
USE_CUDA=true
CUDA_REASON="requested via --nsys script argument"
shift
;;
--nsys-gpu-metrics)
USE_NSYS_GPU_METRICS=true
shift
;;
--ncu)
USE_NCU=true
if [[ $# -lt 2 ]]; then
echo "Error: --ncu requires an argument" >&2
exit 1
fi
ncu_kernel="$2"
shift 2
;;
--launch-skip)
if [[ $# -lt 2 ]]; then
echo "Error: --launch-skip requires an argument" >&2
exit 1
fi
launch_skip="$2"
shift 2
;;
--launch-count)
if [[ $# -lt 2 ]]; then
echo "Error: --launch-count requires an argument" >&2
exit 1
fi
launch_count="$2"
shift 2
;;
--proof-cache)
PROOF_CACHE="$2"
shift 2
;;
--memcheck)
COMPUTE_SANITIZER_ARGS="compute-sanitizer --tool memcheck"
shift
;;
--synccheck)
COMPUTE_SANITIZER_ARGS="compute-sanitizer --tool synccheck"
shift
;;
--racecheck)
COMPUTE_SANITIZER_ARGS="compute-sanitizer --tool racecheck"
shift
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done
if [ "$USE_NSYS_GPU_METRICS" = "true" ] && [ "$USE_NSYS" = "false" ]; then
echo "Error: --nsys-gpu-metrics requires --nsys" >&2
exit 1
fi
if [ "$USE_CUDA" = "false" ] && [ "$NVIDIA_SMI_READY" = "true" ]; then
USE_CUDA=true
CUDA_REASON="nvidia-smi detected a CUDA-capable GPU"
fi
if [ "$USE_CUDA" = "true" ]; then
echo "Using CUDA acceleration ($CUDA_REASON)."
fi
if [ "$NVIDIA_SMI_READY" = "true" ] && [ "$USE_NSYS" = "false" ]; then
start_gpu_monitor "$GPU_LOG_FILE" "$GPU_MONITOR_INTERVAL"
elif [ "$USE_NSYS" = "true" ]; then
echo "GPU memory monitoring disabled for nsys profiling."
else
echo "nvidia-smi not detected; GPU memory monitoring disabled."
fi
mkdir -p rpc-cache
if [[ -f .env ]]; then
# Optional convenience file for local runs.
source .env
fi
if [[ -z "${RPC_1:-}" ]]; then
echo "Missing RPC endpoint: set RPC_1 env var or create reth-bench/.env with RPC_1=..." >&2
exit 1
fi
MODE="${MODE_OVERRIDE:-prove-app}" # can be prove-app, prove-stark, keygen, generate-vm-vkey
# Map profile aliases and set target directory
case "${PROFILE_OVERRIDE:-release}" in
dev|debug)
PROFILE="dev"
TARGET_DIR="debug"
;;
release)
PROFILE="release"
TARGET_DIR="release"
;;
*)
PROFILE="${PROFILE_OVERRIDE:-profiling}"
TARGET_DIR="$PROFILE"
;;
esac
FEATURES="parallel,metrics,jemalloc,unprotected"
BLOCK_NUMBER="${BLOCK_NUMBER_OVERRIDE:-24001988}"
TOOLCHAIN="+$RUST_TOOLCHAIN"
BIN_NAME="openvm-reth-benchmark"
export VPMM_PAGE_SIZE=$((4 << 20))
if [[ -z "${VPMM_PAGES:-}" ]] && [[ "$MODE" == "prove-stark" || "$MODE" == "prove-app" || "$MODE" == "prove-evm" ]]; then
export VPMM_PAGES=$((16 << 8)) # start with 16GB
fi
# Settings to turn off VPMM:
# VPMM_PAGE_SIZE=$((1<<35))
# VPMM_PAGES=0
if [ "$USE_CUDA" = "true" ]; then
FEATURES="$FEATURES,cuda,halo2-gpu"
fi
if [ "$USE_NSYS" = "true" ]; then
FEATURES="$FEATURES,nvtx"
fi
if [ "$MODE" = "prove-evm" ] || [ "$MODE" = "prove-root" ] || [ "$MODE" = "keygen-root" ]; then
FEATURES="$FEATURES,evm-verify"
fi
# `keygen-root` is a shell-level alias: enable evm-verify (handled above) and pass --mode keygen
# to the binary. The keygen branch then additionally writes <output_dir>/root.pk when evm-verify
# is compiled in.
if [ "$MODE" = "keygen-root" ]; then
MODE="keygen"
fi
arch=$(uname -m)
case $arch in
arm64|aarch64)
RUSTFLAGS="-Ctarget-cpu=native"
if [ "$USE_TCO" = "false" ]; then
USE_TCO=true
fi
;;
x86_64|amd64)
RUSTFLAGS="-Ctarget-cpu=native"
if [ "$USE_TCO" = "false" ]; then
# aot enables halo2curves-axiom/asm which is x86_64-only
FEATURES="$FEATURES,aot"
if [ "$MODE" = "prove-evm" ]; then
FEATURES="$FEATURES,halo2-asm"
fi
fi
;;
*)
echo "Unsupported architecture: $arch"
exit 1
;;
esac
if [ "$USE_TCO" = "true" ]; then
FEATURES="$FEATURES,tco"
fi
if [ "$USE_PERF" = "true" ] || [ "$USE_NSYS" = "true" ]; then
RUSTFLAGS="$RUSTFLAGS -C force-frame-pointers=yes"
# Default to profiling profile for host profiling if not overridden
if [ -z "$PROFILE_OVERRIDE" ]; then
PROFILE="profiling"
TARGET_DIR="profiling"
fi
fi
if [ "$USE_NSYS" = "false" ]; then
export JEMALLOC_SYS_WITH_MALLOC_CONF="retain:true,background_thread:true,metadata_thp:always,dirty_decay_ms:10000,muzzy_decay_ms:10000,abort_conf:true"
fi
if [[ "${OPENVM_BENCH_SKIP_BUILD:-0}" != "1" ]]; then
build_openvm_guest_elf
RUSTFLAGS=$RUSTFLAGS cargo $TOOLCHAIN build --bin $BIN_NAME --profile=$PROFILE --no-default-features --features=$FEATURES
fi
BIN=$REPO_ROOT/target/$TARGET_DIR/$BIN_NAME
CONFIG_ARGS=""
if [[ -n $APP_LOG_BLOWUP ]]
then
CONFIG_ARGS="$CONFIG_ARGS --app-log-blowup ${APP_LOG_BLOWUP}"
fi
if [[ -n $LEAF_LOG_BLOWUP ]]
then
CONFIG_ARGS="$CONFIG_ARGS --leaf-log-blowup ${LEAF_LOG_BLOWUP}"
fi
if [[ -n $INTERNAL_LOG_BLOWUP ]]
then
CONFIG_ARGS="$CONFIG_ARGS --internal-log-blowup ${INTERNAL_LOG_BLOWUP}"
fi
if [[ -n $APP_L_SKIP ]]
then
CONFIG_ARGS="$CONFIG_ARGS --app-l-skip ${APP_L_SKIP}"
fi
if [[ -n $ROOT_LOG_BLOWUP ]]
then
CONFIG_ARGS="$CONFIG_ARGS --root-log-blowup ${ROOT_LOG_BLOWUP}"
fi
if [[ -n $NUM_CHILDREN_LEAF ]]
then
CONFIG_ARGS="$CONFIG_ARGS --num-children-leaf ${NUM_CHILDREN_LEAF}"
fi
if [[ -n $NUM_CHILDREN_INTERNAL ]]
then
CONFIG_ARGS="$CONFIG_ARGS --num-children-internal ${NUM_CHILDREN_INTERNAL}"
fi
if [[ -n $PROOF_CACHE ]]
then
CONFIG_ARGS="$CONFIG_ARGS --proof-cache ${PROOF_CACHE}"
fi
if [[ -n $SEGMENT_MAX_MEMORY ]]
then
CONFIG_ARGS="$CONFIG_ARGS --segment-max-memory ${SEGMENT_MAX_MEMORY}"
fi
BIN_ARGS="--mode $MODE \
$CONFIG_ARGS"
if [ "$MODE" != "generate-vm-vkey" ]; then
BIN_ARGS="$BIN_ARGS \
--block-number $BLOCK_NUMBER \
--rpc-url $RPC_1 \
--cache-dir rpc-cache"
fi
export RUST_LOG="info,p3_=warn"
echo "Run command:"
echo "$BIN $BIN_ARGS"
if [ "$USE_PERF" = "true" ]; then
# Set sampling frequency based on mode
if [[ "$MODE" == "execute-host" || "$MODE" == "execute" || "$MODE" == "execute-metered" ]]; then
PERF_FREQ=4000
else
PERF_FREQ=100
fi
echo "Running with perf profiling (freq=${PERF_FREQ})..."
export OUTPUT_PATH="metrics.json"
perf record -F $PERF_FREQ --call-graph=dwarf -g -o perf.data -- $BIN $BIN_ARGS
echo "Converting perf.data with samply..."
mkdir -p samply_profile
samply import perf.data --unstable-presymbolicate --save-only --output samply_profile/profile.json.gz
echo "Saved profile: samply_profile/profile.json.gz"
FIREFOX_PROFILER_URL=$(python3 "$REPO_ROOT/scripts/upload_firefox_profile.py" samply_profile/profile.json.gz) || true
if [ -n "$FIREFOX_PROFILER_URL" ]; then
echo "Firefox Profiler URL: $FIREFOX_PROFILER_URL"
else
echo "Warning: failed to upload profile to Firefox Profiler"
fi
elif [ "$USE_NSYS" = "true" ]; then
NSYS_OUTPUT="reth.nsys-rep"
NSYS_ARGS="--trace=cuda,nvtx,osrt --sample=cpu --cpuctxsw=true --cuda-memory-usage=true --force-overwrite=true -o $NSYS_OUTPUT"
if [ "$USE_NSYS_GPU_METRICS" = "true" ]; then
NSYS_ARGS="$NSYS_ARGS --gpu-metrics-devices=all"
fi
echo "[sudo] Running with nsys profiling..."
sudo env PATH="$PATH" HOME="$HOME" RUST_LOG="$RUST_LOG" \
VPMM_PAGE_SIZE="${VPMM_PAGE_SIZE:-}" VPMM_PAGES="${VPMM_PAGES:-}" \
LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}" \
nsys profile $NSYS_ARGS \
$BIN $BIN_ARGS
echo "=== CUDA GPU Kernel Summary ==="
nsys stats --force-export=true --report cuda_gpu_kern_sum "$NSYS_OUTPUT"
echo "=== CUDA Memory Time Summary ==="
nsys stats --force-export=true --report cuda_gpu_mem_time_sum "$NSYS_OUTPUT"
echo "=== CUDA Memory Size Summary ==="
nsys stats --force-export=true --report cuda_gpu_mem_size_sum "$NSYS_OUTPUT"
echo "=== NCU Top Kernel Analysis ==="
TOP_KERNEL=$(nsys stats --report cuda_gpu_kern_sum "$NSYS_OUTPUT" 2>/dev/null | \
awk '/--------/{getline; print; exit}' | \
sed -E 's/.*::([a-zA-Z_][a-zA-Z0-9_]*)[<(].*/\1/; t; s/.*[[:space:]]([a-zA-Z_][a-zA-Z0-9_]*)[<(].*/\1/')
echo "Top kernel: $TOP_KERNEL"
elif [[ "$USE_NCU" == true ]]; then
echo "[sudo] Running with Ncu..."
NCU_OUTPUT="reth-${ncu_kernel}.ncu-rep"
sudo env PATH=$PATH ncu \
--target-processes all \
--kernel-name "$ncu_kernel" \
-f -o "${NCU_OUTPUT}" \
--launch-skip "${launch_skip:-0}" \
--launch-count "${launch_count:-4}" \
--set full \
$BIN $BIN_ARGS
ncu -i "$NCU_OUTPUT" > "reth-${ncu_kernel}.txt"
else
export OUTPUT_PATH="metrics.json"
$COMPUTE_SANITIZER_ARGS $BIN $BIN_ARGS
fi