Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions .github/scripts/aggregate_recursion_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/env python3
"""Format the recursion-guest per-function profile as a Markdown PR comment.

`test_recursion_pc_histogram` prints a per-function summary table (cycles folded
over each function's PCs, computed across the *full* histogram) followed by a
per-address detail table. We extract the per-function table — the view that
shows where the cycles actually go — and render it as Markdown.

Top 25 functions by cycle count (aggregated over their PCs):
rank cycles % cum % PCs function (file:line)
1 5335072 24.95% 24.95% 72 <...>::visit_seq::<...>

Reads the test's captured output from argv[1]; writes the Markdown body to
argv[2] (or stdout).
"""

import re
import sys

# A per-function summary row: rank, cycles, pct%, cum%, pcs, function.
# Distinguished from the per-PC detail rows by the absence of a 0x<pc> column.
FN_ROW = re.compile(
r"^\s*\d+\s+(\d+)\s+([\d.]+)%\s+([\d.]+)%\s+(\d+)\s+(.*\S)\s*$"
)
FN_TABLE_START = re.compile(r"Top \d+ functions by cycle count")
PC_TABLE_START = re.compile(r"Top \d+ PCs by cycle count")
TOTAL_CYCLES = re.compile(r"Total cycles\s*:\s*(\d+)")
UNIQUE_PCS = re.compile(r"Unique PCs\s*:\s*(\d+)")
EXEC_TIME = re.compile(r"Exec time\s*:\s*(\S+)")


def parse(text):
total_cycles = unique_pcs = exec_time = None
rows = []
in_fn_table = False
for line in text.splitlines():
if total_cycles is None and (m := TOTAL_CYCLES.search(line)):
total_cycles = int(m.group(1))
if unique_pcs is None and (m := UNIQUE_PCS.search(line)):
unique_pcs = int(m.group(1))
if exec_time is None and (m := EXEC_TIME.search(line)):
exec_time = m.group(1)
if FN_TABLE_START.search(line):
in_fn_table = True
continue
if PC_TABLE_START.search(line):
in_fn_table = False
continue
if in_fn_table and (m := FN_ROW.match(line)):
rows.append(
{
"cycles": int(m.group(1)),
"pct": m.group(2),
"cum": m.group(3),
"pcs": int(m.group(4)),
"fn": m.group(5),
}
)
return total_cycles, unique_pcs, exec_time, rows


def short(name, width=90):
return name if len(name) <= width else name[: width - 1] + "…"


def render(total_cycles, unique_pcs, exec_time, rows, title="Recursion guest profile"):
if not rows:
return (
f"### {title}\n\n"
"> ⚠️ No per-function rows found in the test output — the run may "
"have failed before printing the table. Check the workflow logs.\n"
)

body = f"### {title}\n\n"
if total_cycles is not None:
body += f"**Total cycles:** {total_cycles:,}"
if unique_pcs is not None:
body += f" · **Unique PCs:** {unique_pcs:,}"
if exec_time:
body += f" · **Exec time:** {exec_time}"
body += "\n\n"

body += f"#### Top {len(rows)} functions by cycles (folded over their PCs)\n\n"
body += "| Rank | Cycles | % | Cum % | PCs | Function |\n"
body += "|-----:|-------:|--:|------:|----:|----------|\n"
for i, r in enumerate(rows, 1):
body += (
f"| {i} | {r['cycles']:,} | {r['pct']}% | {r['cum']}% | "
f"{r['pcs']} | `{short(r['fn'])}` |\n"
)

last_cum = rows[-1]["cum"]
body += (
f"\n<sub>Each function's cycles are summed over all its program counters "
f"across the full histogram; the top {len(rows)} cover {last_cum}% of total "
f"cycles. Percentages are of total cycles.</sub>\n"
)
return body


def main():
import argparse

ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("log", help="captured test output to parse")
ap.add_argument("-o", "--out", help="write Markdown here instead of stdout")
ap.add_argument(
"-t",
"--title",
default="Recursion guest profile",
help="section heading (e.g. the test/config name)",
)
args = ap.parse_args()

with open(args.log, "r", errors="replace") as f:
text = f.read()
body = render(*parse(text), title=args.title)
if args.out:
with open(args.out, "w") as f:
f.write(body)
else:
sys.stdout.write(body)


if __name__ == "__main__":
main()
175 changes: 175 additions & 0 deletions .github/workflows/profile-recursion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
name: Profile Recursion (PR)

# Runs the recursion-guest PC histogram diagnostics (single-query and
# multi-query, in parallel via a matrix) and posts a combined per-function
# profile as a PR comment. Triggered by a `/profile_recursion` comment from a
# repo member, or manually via workflow_dispatch.

on:
workflow_dispatch:
issue_comment:
types: [created]

permissions:
contents: read
pull-requests: write

concurrency:
group: profile-recursion-${{ github.event.issue.number || github.run_id }}
cancel-in-progress: true

jobs:
# One job per configuration; they run in parallel and each uploads a Markdown
# fragment artifact. The `comment` job stitches them into one PR comment.
profile:
# Skip unless: workflow_dispatch, or "/profile_recursion" comment on a PR by a member.
if: >-
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
startsWith(github.event.comment.body, '/profile_recursion') &&
contains(fromJSON('["MEMBER","OWNER","COLLABORATOR"]'), github.event.comment.author_association))
runs-on: [self-hosted, bench]
timeout-minutes: 90
strategy:
fail-fast: false
matrix:
include:
- name: single-query
test: single
title: "Single query (blowup=2, 1 query)"
- name: multi-query
test: single

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: the multi-query matrix entry uses test: single, so this job runs make test-profile-recursion-single (the blowup=2, 1-query test) but presents the result under the "Multi query (blowup=8, 128-bit)" title. The multiquery test (test_recursion_pc_histogram_multiquery / make test-profile-recursion-multi) is never executed — both jobs profile the same single-query run.

Suggested change
test: single
test: multi

title: "Multi query (blowup=8, 128-bit)"
steps:
- name: React to comment
if: github.event_name == 'issue_comment' && matrix.name == 'single-query'
uses: actions/github-script@v7
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'eyes'
});

- name: Get PR head ref
id: pr-ref
if: github.event_name == 'issue_comment'
env:
GH_TOKEN: ${{ github.token }}
PR_NUM: ${{ github.event.issue.number }}
run: |
SHA=$(gh pr view "$PR_NUM" --repo "$GITHUB_REPOSITORY" --json headRefOid -q .headRefOid)
echo "sha=$SHA" >> "$GITHUB_OUTPUT"

- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ steps.pr-ref.outputs.sha || github.sha }}

- name: Setup Rust Environment
uses: ./.github/actions/setup-rust

- name: Add cargo to PATH
run: echo "$HOME/.cargo/bin" >> "$GITHUB_PATH"

- name: Run recursion PC histogram (${{ matrix.name }})
env:
TEST: ${{ matrix.test }}
run: |
# Self-provision the RISC-V sysroot in a user-writable dir (the default
# /opt path on the bench runner is root-owned); the guest ELF build the
# test triggers picks this up via the Makefile's `SYSROOT_DIR ?=`.
export SYSROOT_DIR="$HOME/.lambda-vm-sysroot"
set -o pipefail
make test-profile-recursion-$TEST

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: nothing writes /tmp/hist.log, but the next step (aggregate_recursion_histogram.py /tmp/hist.log) reads it. The make target only prints the histogram to stderr via eprintln! and is not redirected. set -o pipefail here suggests a tee was intended. The aggregate step will fail to open the file (or read a stale one). Capture the output, e.g.:

Suggested change
make test-profile-recursion-$TEST
make test-profile-recursion-$TEST 2>&1 | tee /tmp/hist.log


- name: Aggregate into a per-function fragment
if: always()
env:
TITLE: ${{ matrix.title }}
run: |
python3 .github/scripts/aggregate_recursion_histogram.py \
/tmp/hist.log --title "$TITLE" --out "/tmp/fragment-${{ matrix.name }}.md"
cat "/tmp/fragment-${{ matrix.name }}.md" >> "$GITHUB_STEP_SUMMARY"

- name: Upload fragment
if: always()
uses: actions/upload-artifact@v4
with:
name: profile-fragment-${{ matrix.name }}
path: /tmp/fragment-${{ matrix.name }}.md
retention-days: 7

# Stitch the matrix fragments into a single PR comment.
comment:
needs: profile
if: always() && github.event_name == 'issue_comment'
runs-on: [self-hosted, bench]
steps:
- name: Get PR head ref
id: pr-ref
env:
GH_TOKEN: ${{ github.token }}
PR_NUM: ${{ github.event.issue.number }}
run: |
SHA=$(gh pr view "$PR_NUM" --repo "$GITHUB_REPOSITORY" --json headRefOid -q .headRefOid)
echo "sha=$SHA" >> "$GITHUB_OUTPUT"

- name: Download fragments
uses: actions/download-artifact@v4
with:
path: fragments
pattern: profile-fragment-*
merge-multiple: true

- name: Assemble comment body
env:
COMMIT_SHA: ${{ steps.pr-ref.outputs.sha }}
run: |
{
echo "## Recursion guest profile"
echo
# Single-query first, then multi-query, then any others.
for frag in fragments/fragment-single-query.md \
fragments/fragment-multi-query.md; do
[ -f "$frag" ] && { cat "$frag"; echo; }
done
echo "<sub>Commit: ${COMMIT_SHA:0:8} · Runner: self-hosted bench</sub>"
} > /tmp/profile_comment.md
cat /tmp/profile_comment.md

- name: Comment on PR
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const body = fs.readFileSync('/tmp/profile_comment.md', 'utf8');

const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
// Reuse our own marker comment so repeated /profile_recursion runs update in place.
const existing = comments.find(c =>
c.user.type === 'Bot' &&
c.body.includes('Recursion guest profile')
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: deps deps-linux deps-macos compile-programs-asm compile-programs-rust compile-bench \
compile-programs clean-asm clean-rust clean-bench clean-shared clean test test-asm \
test-rust test-executor test-flamegraph flamegraph-prover \
test-rust test-executor test-flamegraph flamegraph-prover test-profile-recursion test-profile-recursion-single test-profile-recursion-multi \
test-fast test-prover test-prover-all test-disk-spill test-math-cuda test-cuda-integration \
bench-math-cuda bench-prover bench-prover-cuda build check clippy fmt lint regen-ethrex-fixtures \
update-ethrex-fixture-checksums check-ethrex-fixture-checksums
Expand Down Expand Up @@ -197,6 +197,16 @@ test-rust: compile-programs-rust
test-flamegraph:
cargo test -p executor --test flamegraph



test-profile-recursion: test-profile-recursion-single test-profile-recursion-multi

test-profile-recursion-single: compile-programs-rust
cargo test --package lambda-vm-prover --lib test_recursion_pc_histogram_1query -- --ignored --nocapture

test-profile-recursion-multi: compile-programs-rust
cargo test --package lambda-vm-prover --lib test_recursion_pc_histogram_multiquery -- --ignored --nocapture

# Regenerate the committed ethrex block fixtures (see tooling/ethrex-fixtures).
# Run after bumping the ethrex rev; README checksums are refreshed automatically.
regen-ethrex-fixtures:
Expand Down
7 changes: 4 additions & 3 deletions bench_vs/build_recursion_elfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ build_one() {
echo "[recursion-elfs] building $name ..."
(
cd "$dir"
# The recursion guest pulls in lambda-vm-prover and its serde stack;
# pin serde to 1.0.219 (pre-`serde_core` split) so
# Recursion/deserialize-only guests pull in lambda-vm-prover and its
# serde stack; pin serde to 1.0.219 (pre-`serde_core` split) so
# `-Z build-std=core,alloc` works.
if [ "$name" = "recursion" ]; then
if [ "$name" = "recursion" ] || [ "$name" = "deserialize-only" ]; then
cargo "+$TOOLCHAIN" update -p serde --precise 1.0.219 2>/dev/null || true
fi
cargo "+$TOOLCHAIN" build --release \
Expand All @@ -35,5 +35,6 @@ build_one() {
build_one empty
build_one fibonacci
build_one recursion
build_one deserialize-only

echo "[recursion-elfs] done"
6 changes: 6 additions & 0 deletions bench_vs/lambda/deserialize-only/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[target.riscv64im-lambda-vm-elf]
rustflags = [
"-C", "link-arg=-e",
"-C", "link-arg=main",
"-C", "passes=lower-atomic"
]
13 changes: 13 additions & 0 deletions bench_vs/lambda/deserialize-only/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[workspace]

[package]
name = "deserialize-only-bench"
version = "0.1.0"
edition = "2024"

[dependencies]
lambda-vm-prover = { path = "../../../prover", default-features = false }
embedded-alloc = "0.6"
riscv = { version = "0.15", features = ["critical-section-single-hart"] }
serde = { version = "=1.0.219", default-features = false, features = ["derive", "alloc"] }
postcard = { version = "1.0", default-features = false, features = ["alloc"] }
Loading
Loading