Skip to content

bench: add read and index benchmarks on a pinned kernel tree - #149

Draft
12458 wants to merge 10 commits into
facebookexperimental:mainfrom
12458:benchmark-suite
Draft

12458 wants to merge 10 commits into
facebookexperimental:mainfrom
12458:benchmark-suite

Conversation

@12458

@12458 12458 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Adds benchmarks of semcode on a pinned Linux kernel tree (v7.3.6 from linux-stable, fetched from kernel.googlesource.com), so a change in the numbers is a change in semcode, not in the input. An index is built once and read many times, so reads come first.

  • bench/bench.py (a uv script, stdlib only) builds the checkout it's in and times it. To compare two versions, run it in each and compare the results; each run records the commit it measured.
    • Read scenarios (the default):
      • mcp: bench/workload.jsonl replayed through semcode-mcp: time to ready, a first and a warm pass, and a pass with 4 requests in flight
      • mcp-dirty: the same on a working copy with 20 edited files
      • mcp-behind: the server started on a v7.3.5 index with the tree at v7.3.6
      • mcp-aged: the same workload on an index holding every commit of v7.3.5..v7.3.6
      • cli: one-shot semcode -q queries
    • Index scenarios (-s index): subset (fs/), full and update (v7.3.5 → v7.3.6).
  • bench/workload.rs generates the committed workload: 94 calls to find_function, find_callers, find_calls, find_type and grep_functions, weighted towards the kernel's most used names, since real queries go there and those calls cost the most.
  • benches/analyzer.rs (Criterion): parse_only, analyze_file (7 large files) and corpus (~200 files sampled across the tree).
  • semcode-index --perf-json FILE, and phase timings in the tree and range pipelines; without --vectors, --perf used to report nothing. These are the only changes to semcode itself.

Every answer is checked against something only a real result prints (a caller, a callee, a path from a result row, or a minimum caller count), because a lookup that finds nothing answers fast and semcode echoes the query even then. Each index scenario starts with an untimed run that warms the page cache, records the phase breakdown, and fails if the index has no functions.

Benchmarks

From earlier runs on a laptop, with the workload at 118–120 calls before it was trimmed to the five tools above:

Scenario Result
mcp warm pass 68 s; p50 243 ms, p95 1.5 s, max 8.4 s (find_callers per_cpu_ptr)
mcp, 4 requests in flight 68 s, the same as one at a time; p50 1.7 s
mcp-dirty warm pass 78 s: the overlay costs ~15% on every call
mcp-behind ready in 10 s, then 254 s a pass, 3.7× mcp
mcp-aged warm pass 73 s
cli ~600 ms floor per query; callers kfree 1.6 s
full index 127 s, 3.9 GiB peak RSS, 1.05 GiB index
update 9.4 s

Points to follow up:

  • An index the server brings up to date reads 3.7× slower. mcp-aged, with far more history but built by semcode-index, reads at normal speed, so the likely cause is the server's background catch-up skipping the optimization the CLI indexer runs. Not yet confirmed.
  • Requests in flight are answered one at a time. mcp: improve lookup throughput #142 dispatches tool calls concurrently; the mcp parallel pass measures it.
  • Inserts look like the indexing bottleneck: about 110 s of the 114 s file pipeline on the two default --db-threads. Parsing is about a quarter of analysis time.
  • Grep attributes kernel/bpf/verifier.c:11781 to a function named BTF_ID, a macro.

Validation

On these commits:

  • Each of the 6 commits passes cargo clippy --all-targets -- -D warnings (-j 2) or, for bench.py, ruff; cargo fmt --check passes.
  • cargo bench --bench analyzer -- --test passes.
  • The workload's checks pass against semcode-mcp, and answers that found nothing are rejected.
  • bench/bench.py run -s all -r 1 of this code ran every scenario with all checks passing: mcp warm pass 61 s (p50 236 ms), mcp-behind 257 s, mcp-aged 74 s, full index 126 s, update 9.4 s.
  • cargo test passed (632 tests) on an earlier version of this branch; it hasn't been rerun since, and these commits change semcode itself only in --perf-json's argument checks.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 23, 2026
@12458 12458 changed the title Add a benchmark suite on a pinned kernel tree bench: add read and index benchmarks on a pinned kernel tree Nov 23, 2026
@12458
12458 force-pushed the benchmark-suite branch 6 times, most recently from 2a4f5bc to 8d2daff Compare November 28, 2026 09:48
@12458
12458 marked this pull request as ready for review November 28, 2026 10:50
@12458
12458 marked this pull request as draft November 28, 2026 15:49
12458 added 10 commits November 28, 2026 09:49
--perf prints its timing table for a person to read.  A benchmark
harness comparing two builds wants the same numbers in a form it can
parse.

--perf-json FILE writes the statistics as JSON, keyed by metric name,
with durations in milliseconds.  It implies --perf, so the table is
still printed.  The lore, commits-only and branch modes return before
the statistics are written, so the flag is refused there, and its
directory is checked before indexing rather than after the run.
--perf only timed vector generation, so an index run without vectors
reported nothing.  Record the phases of the tree and range pipelines:
loading the processed-file set, commit metadata, the file pipeline,
database optimization and the call-edge index.  Inside the pipeline,
record each file's analyzer setup and analysis, and each batch insert.

The per-file and per-batch metrics run on worker threads, so their
totals are CPU time summed across threads, not wall time.  Together
they show where a run spends its time: on the full v7.3.6 kernel tree,
inserts take about 110 s of the 114 s file pipeline on the two default
--db-threads, while analysis needs about 54 s across the workers.

Without --perf a measurement costs two clock reads and an atomic load,
next to milliseconds of analysis per file.
Analyzer changes need a quicker, quieter measurement than indexing the
kernel.  cargo bench --bench analyzer times the Tree-sitter analyzer on
real sources from a pinned Linux tree (v7.3.6 from linux-stable):

- parse_only: Tree-sitter alone, the floor under analysis, so a
  regression can be placed in the grammar or in semcode's extraction.
  Parsing is about a quarter of analysis time, an eighth for headers.
- analyze_file: seven large, call-heavy files across subsystems, a big
  header and a Rust driver file.
- corpus: a fixed sample of ~200 files spread over the whole tree, so
  typical files and per-file overhead count too.

Each file's analysis is checked to find something before it is timed.
Sources are read from the tree's committed blobs, so edited, untracked
or generated files cannot change what is measured, and are not vendored:
the kernel is GPL-2.0.  Without a tree the benchmarks skip; a tree that
is not at the tag, or has local changes, is refused.  bench/bench.py
setup, added later in this series, fetches it.
An index is built once and read many times, so the read path is what a
benchmark should exercise most, with the calls users actually make.
Those go to the kernel's core API far more often than to its leaf
functions, and they are the expensive ones: find_callers on per_cpu_ptr
takes 8 s through the MCP server, on a uniformly sampled leaf function
a few milliseconds.  A uniform sample would make reads look ~100x
cheaper than they are.

bench/workload.rs reads a full index of the pinned tree and writes
bench/workload.jsonl: 94 calls to find_function, find_callers,
find_calls, find_type and grep_functions, drawn from fan-in and fan-out
tiers (>=1000, 100-999, 20-99 callers), the most used types, names with
many definitions and the longest bodies, with a small light share so
per-call overhead stays visible.  Sampling is weighted reservoir
sampling on a salted hash of each name, so the same index gives the
same workload; the file is committed with the tag and regenerated only
when the tag moves.  An index holding more than one commit is refused.

Each call lists what its answer must contain.  A lookup that finds
nothing answers in 80 ms against 1.6 s for a heavy one, so without the
check a build that broke lookups would look faster.  semcode echoes the
query even into an answer that found nothing, so the check is on
something only a result prints: a caller, a callee or a file path from
a result row, or for a heavy find_callers a printed caller count of at
least a quarter of the fan-in.

It is an example, so it builds against the library, and lives next to
the workload it writes.  test = false keeps cargo test from linking it.
bench/bench.py builds the semcode checkout it is in and times it on the
pinned kernel tree:

  bench/bench.py setup        # shallow clone of v7.3.6, history to v7.3
  bench/bench.py run          # read scenarios
  bench/bench.py run -s index

To compare two versions, run it in each and compare the results; each
run records the commit it measured.

Read scenarios, the default, since an index is built once and read many
times:

  mcp         bench/workload.jsonl replayed through semcode-mcp with a
              built-in stdio client: time to ready, a first and a warm
              pass, and a pass with four requests in flight
  mcp-dirty   the same on a working copy with 20 edited files
  mcp-behind  the server started on a v7.3.5 index with the tree at
              v7.3.6, catching up before it answers
  mcp-aged    the same on an index holding every commit of
              v7.3.5..v7.3.6, history accumulated
  cli         one-shot semcode -q queries from bench/queries.txt

Index scenarios: subset (fs/ from empty), full (the whole tree from
empty) and update (a v7.3.5 index brought to v7.3.6 by a plain
semcode-index).

Every answer is checked, since a lookup that finds nothing returns
quickly.  Each index scenario starts with an untimed run that warms the
page cache, records the phase breakdown with --perf-json, and fails if
the index has no functions.  Indexes are built by each run, never kept.
The report gives mean and standard deviation per scenario, and per MCP
tool p50/p95/max latency and the slowest calls.

A first run on a laptop:

- A warm pass of the workload takes 68 s; p50 243 ms, p95 1.5 s, the
  slowest find_callers 8.4 s.
- Four requests in flight take as long as one at a time, with p50
  latency 1.7 s: the server answers requests one by one.
- The working-copy overlay costs ~15% on every call.
- After catching up from v7.3.5 (10 s), the server answers 3.7x slower
  (254 s a pass), while mcp-aged, with far more history but built by
  semcode-index, is not slower (73 s).
- Indexing the whole tree takes 127 s and 3.9 GiB; inserts take ~110 s
  of the 114 s file pipeline on the two default --db-threads.

The script runs under uv and uses only the standard library.
How to set up and run the harness and the Criterion benchmarks, what
each scenario measures, how answers are checked, how to regenerate the
workload, and what to change when the kernel tag moves.
- build() takes the binaries' paths from cargo's artifact messages, so a
  build target (target/<triple>/release) no longer measures stale ones.
- The update scenario fails if it leaves base.db's table counts
  unchanged, so an indexer that does nothing cannot pass as a speedup.
- MCP peak RSS is reported as n/a where VmHWM cannot be read, not 0.
- Phase timings are elapsed time summed across threads, not CPU time;
  say so in the report and in PerfStats::to_json().
- The Criterion corpus pins its extensions instead of following the
  indexer's defaults, which would change the sample under the same ID.
- bench_workload requires the new bench feature: test = false did not
  stop cargo test building and linking it.
- Note that workload.rs credits a caller with its first kernel
  definition, not the one that made the call.
The preflight checked only that the parent directory existed, so a
--perf-json naming an existing directory ran the whole indexing pass,
modifying the database, before failing with "Is a directory".

Validate the path with ReportSink::prepare, as --result-json does, and
write the statistics through the same sink so they are published
atomically. PerfStats::write_json has no callers left.
- Start semcode-mcp with --max-concurrent-requests set to the widest
  pass. The server defaults to 2 and refuses calls beyond its limit
  with -32001, so the four-wide parallel pass failed.
- Keep a reply's isError flag apart from its text, and match the
  server's miss messages only at the start of a line, so source text
  containing "error: " or "not found in database" is not a miss.
- Check that a reused kernel checkout holds BASE and BASE..TAG, in
  setup and before a run needing it, and print the fetch that fixes it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant