Skip to content
Closed
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()
177 changes: 177 additions & 0 deletions .github/workflows/profile-recursion.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
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: test_recursion_pc_histogram
title: "Single query (blowup=2, 1 query)"
- name: multi-query
test: test_recursion_pc_histogram_multiquery
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
# The test is #[ignore]d and prints the histogram to stderr.
cargo test --package lambda-vm-prover --lib "$TEST" \
-- --ignored --nocapture 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,
});
}
Loading
Loading