Skip to content
Open
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: the cycles
folded over each function's PCs, computed across the *full* histogram — the view
that shows where the cycles actually go. We parse that table and render it as
Markdown.
Top 25 functions by cycle count (aggregated over their PCs):
rank cycles % cum % PCs function
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.
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")
# The "====" rule the test prints right after the (now sole) function table.
TABLE_END = re.compile(r"^=+\s*$")
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 in_fn_table and TABLE_END.match(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()
178 changes: 178 additions & 0 deletions .github/workflows/profile-recursion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
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: 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 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
Comment thread
Oppen marked this conversation as resolved.
# always() so partial-matrix failures still post; skip when `profile` was
# skipped (non-/profile_recursion or non-member comment) so this job — and
# the self-hosted bench runner it spins up — doesn't fire on every comment.
if: always() && github.event_name == 'issue_comment' && needs.profile.result != 'skipped'
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: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: deps deps-linux deps-macos compile-programs-asm compile-programs-rust compile-bench \
compile-programs compile-recursion-elfs clean-asm clean-rust clean-bench clean-shared \
clean-recursion-elfs 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 @@ -51,7 +51,7 @@ BENCH_ARTIFACTS := $(addprefix $(BENCH_ARTIFACTS_DIR)/, $(addsuffix .elf, $(BENC
# rather than executor/programs/. The recursion guest is the in-VM STARK verifier.
RECURSION_GUESTS_DIR=./bench_vs/lambda
RECURSION_ARTIFACTS_DIR=./executor/program_artifacts/recursion
RECURSION_GUESTS := empty fibonacci recursion
RECURSION_GUESTS := empty fibonacci recursion deserialize-only
RECURSION_ARTIFACTS := $(addprefix $(RECURSION_ARTIFACTS_DIR)/, $(addsuffix .elf, $(RECURSION_GUESTS)))

# Override with: make ... SYSROOT_DIR=$HOME/.lambda-vm-sysroot
Expand Down Expand Up @@ -232,6 +232,14 @@ 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-recursion-elfs
cargo test --package lambda-vm-prover --lib test_recursion_profile_1query -- --ignored --nocapture

test-profile-recursion-multi: compile-recursion-elfs
cargo test --package lambda-vm-prover --lib test_recursion_profile_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: 7 additions & 0 deletions bench_vs/lambda/deserialize-only/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[target.riscv64im-lambda-vm-elf]
rustflags = [
"-C", "link-arg=-e",
"-C", "link-arg=main",
"--cfg", "getrandom_backend=\"custom\"",
"-C", "passes=lower-atomic"
]
Loading
Loading