Skip to content

wcprof: report views, clients reporting their own work, and per-exec and per-session CPU - #14866

Merged
sipsma merged 18 commits into
mainfrom
sipsma/wcprof-extension
Oct 29, 2026
Merged

sipsma merged 18 commits into
mainfrom
sipsma/wcprof-extension

Conversation

@sipsma

@sipsma sipsma commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

wcprof, the engine's wall-clock profiler, could only see the engine's own work. Measuring where a cold Go build's time goes needed one-off scripts, patched engines and instrumented builds. This PR adds what those were standing in for, in four parts.

What changes for users

Almost nothing, apart from one small addition to every exec in normal use. Whenever telemetry is on, each exec now reads its cgroup's CPU time and peak memory once its work is done: two small cgroup files, which the engine's resource sampler already reads for every exec, and two more when the exec ran a mount helper. Its exec.run span gets up to six wcprof.usage.* attributes. I haven't measured the time this adds; it runs on a path that has just started and stopped a container. Each exec also keeps its cleanups' names, a few small allocations.

Everything else only runs when the engine is profiling, or when a CPU profile has been armed through the debug endpoint.

Report views (engine-lab wcprof-report)

wcprof-report gains views for questions that so far needed scripts:

  • -label REGEXP: labels each exec by a submatch of its command, and carries the label up to the ancestors whose labelled descendants all share it. For example, a Go build's request, lazy evaluation and exec ops then carry their package's name.
  • -view ancestors: ops grouped by their class and the classes of up to N ancestors, to see where a class is called from.
  • -view occupancy: per class or per label, how many ops were in progress over time (mean per bucket and peak), rather than how many started.
  • -view window: for one op, over its interval or part of it, three quantities per class: time on the critical chain, occupied wall time, and self time summed across concurrent ops.
  • -view compare -by label: two dumps compared label by label, e.g. package by package.

On a real build with 1,412 packages, the per-package numbers match the script they replace within 0.03 ms on average.

Clients reporting their own work

A program inside an exec, or a module, can now report its own units of work with plain OTel spans and links, extending the wcprof.* attributes engine spans already use:

  • tasks and task groups, labelled (wcprof.label) and tagged with a producer ID (wcprof.producer);
  • dependencies, as wait links with reason dependency;
  • limited resources (a resource span with a capacity and an admission policy), and each acquisition of one unit (an acquire span from request to release, with its priority, the inputs the priority came from, when it was granted and its outcome);
  • a final span per producer declaring how many spans it reported, so a reader can tell a complete report from a partial one.

engine/wcprof/contract is the one decoder of this contract. When a session is profiled, its span exporter records reported spans in wcprof's native dump. Each span is recorded once; live snapshots wait for the final export. References between spans resolve when the dump is written, and anything still missing is dumped as unresolved_span.

Work a client reports from inside a dagql call's exec or module is attached to that call. A query made under a reported task links its engine work to the task.

One limit: a dump that flushes the recorder while a client is mid-task loses that client's links to the queries it made. Dumps taken after the clients finish are complete.

Each exec's CPU and memory, and the engine's CPU per session

  • Per exec: the exec's resource cgroup, the workload plus its mount helpers, is read after mount teardown (exec_cgroup). When a mount helper ran, the helpers' own cgroup inside it is read too (mount_helpers), so the workload's usage is the first less the second; without one, the first is the workload's alone. Nothing is read between the workload's exit and the container's removal, so this works whether or not the runtime removes the container itself. The readings go into wcprof as usage events on the exec.run op, and onto its span.
  • Per session: a profiled session is an op, session, from the end of its initialization to the end of its removal. It carries the CPU the engine used meanwhile: the engine process, its reaped child processes, and its cgroup. These are activity during the session, not CPU the session owned. Its removal is an op too, session.teardown, with an op for each main step: stopping its services, draining its queries, releasing its cache, and shutting its telemetry down. On a cold build, releasing the cache can take a second after the last query while the client waits; these ops show it. Like the session's op, they are only in the engine's native profile, since the session's telemetry is itself shut down during the removal.
  • Armed profiles: /debug/wcprof/profile?phase=session arms a CPU profile of the engine over the next session, and returns it in pprof's format when that session ends.

Checked on real builds

  • One engine recorded these per-exec readings next to the old measurement-only reading, on all 1,670 execs of a first build of dagger's CLI. Both totals were 233.88 s of CPU, and no exec was more than 0.02% apart.
  • A 40.1 s first build's session recorded 94.3 s of engine-process CPU and 242.4 s of reaped-children CPU, and the armed profile of it came back. In that nested dev-engine setup, the exec cgroups sit outside the engine's own cgroup, so the engine cgroup reading (102.1 s) is mostly the engine process.

Steps inside engine operations

dagql.BeginStep records a step inside an engine operation, named <operation>.<step>, when the work under its context is profiled, as an internal op in the native profile only. It's never an OTel span: the UI hides everything under an internal span, and a step can run user-facing work, such as evaluating a lazy input and its exec. Work that isn't profiled records nothing; the helper allocates nothing then. The first steps:

  • gathering files into a directory (copyFiles, behind withFile and withFiles): evaluating the inputs, creating the snapshot, mounting it and copying (each source's mount and copy), the usage, the commit and the release;
  • each cleanup an exec runs after its process, exec.cleanup.<name>, with a mount target left out of the name. Cleanups now keep the names they were added with (a few small allocations per exec), and RunEach runs them through a wrapper that gets it.

Tests

  • The new tests are in engine/wcprof, engine/wcprof/contract, engine/server, engine/engineutil, engine/engineutil/resources and dagql. They run in the "Package tests outside core/integration" check, with the race detector.
  • The report views' tests run in engine-lab's wcprof-report-test check.

sipsma added 17 commits October 28, 2026 02:41
…windows

wcprof-report now reads each exec op's recorded command, and adds:

- -label: a regexp over each exec's command whose first submatch labels
  the op and every ancestor whose labelled descendants all share that
  label, so a package's request, lazy evaluation and exec carry the
  package its compile script names;
- the ancestors view: ops grouped by their class and the classes of up
  to -levels ancestors, to see where a class is called from;
- the occupancy view: per class, or per label with -by label, how many
  ops were in progress over time (mean per bucket and peak), rather than
  how many started;
- the window view: for one op over its interval or a -from/-to part of
  it, three named quantities per class: time on the critical chain,
  occupied wall time (the union of the class's ops), and self time
  summed across concurrent ops;
- compare -by label, to compare two dumps package by package.

Signed-off-by: Erik Sipsma <[email protected]>
…ngine-lab flags

The window view's occupied and self totals now walk the op's whole
subtree, since a descendant can outlive its parent (publication work
started by a call that already returned), and say they cover the subtree
while the critical chain also follows waits into shared work. Ops still
open at dump time keep their recorded command, so they can be labelled.
engine-lab's wcprofReport passes label, by, levels, from and to through
to the helper and documents the new views; flag help and the compare
heading follow.

Signed-off-by: Erik Sipsma <[email protected]>
Clients and programs inside execs can report their own work to wcprof
with OTel spans: tasks (a unit of their work, e.g. one package of a
build) and task groups (a parent whose tasks run concurrently), each
task's dependency waits as wait links (reason "dependency", written even
when the dependency was already done), their limited resources (a
"resource" span with a capacity and an admission policy) and each
acquisition of one (an "acquire" span from request to release, with its
priority, what the priority came from, when it was granted and its
outcome), and a producer's count of the spans it reported.

wcprof gains the op kinds task, task_group, resource and acquire and the
wait reason dependency; telemetryattrs the wcprof.label,
wcprof.producer, wcprof.resource.* and wcprof.producer.* attributes. The
new engine/wcprof/contract package decodes a span under this contract,
for the recorder and the offline analyzers alike.

Signed-off-by: Erik Sipsma <[email protected]>
Spans that clients and programs inside execs report under the wcprof
reporting contract reach the engine with the session's telemetry; the
session span exporter now decodes them with engine/wcprof/contract and
records them when the session is profiled. The recorder gives each
reported span an op ID the first time it sees it (a span exported live
and again at its end is recorded once, at its end) and keeps parents,
wait targets and acquired resources as span references, resolved when
the dump is written; one still missing then is dumped as an
unresolved_span link. Resources and acquisitions are dumped as their own
events, reported ops carry their label and producer, and the dump header
lists each producer's received and declared span counts.

serveQuery links its session op to the caller's span, so in the dump the
engine work a reported task requested resolves to that task (a
reported_cause link).

Signed-off-by: Erik Sipsma <[email protected]>
A client a dagql call runs, such as a build tool inside the call's exec or
the module the call invokes, gets the call's span as its traceparent, so
what it reports to wcprof is parented to that span. The native dump had no
op for that span, so those parents were dumped as unresolved_span links and
the reported work floated free of the call that ran it.

Each call op is now mapped to the span it begins under, and a reported
op's parent resolves to it. When calls share a span (under a
telemetry-suppressed caller), the outermost call keeps it.

Signed-off-by: Erik Sipsma <[email protected]>
…p resource names

- A live snapshot's unset end can arrive as a time before its start, as
  the session's span store already handles. It was recorded as ended, and
  the span's final export, with its real end and the links it gained, was
  then dropped as a repeat. Such a snapshot is now left for the final
  export.
- A producer may report work with an existing op kind, e.g. its compile
  as exec work: a non-engine span carrying a producer ID is now recorded
  whatever its kind, instead of dropped.
- A resource's display name is kept in its dump event.
- Note that a flushing dump drops caller links it couldn't resolve.

Signed-off-by: Erik Sipsma <[email protected]>
When an exec is profiled (by wcprof or by OTel profile emission), the
engine reads two cgroups once the exec's work is done and records what
it used:

- container: the workload's own cgroup, read after its processes exit
  and before the container is deleted (cleanups run in reverse, so the
  read is registered right after the delete);
- exec_cgroup: the exec's resource cgroup, the workload and the mount
  helpers beside it, read after the existing final resource sample.

Each reading is the cgroup's user and system CPU time and peak memory,
read with resources.ReadUsage. The recorder keeps readings apart from its
fixed-size events and dumps each as a usage event on the exec.run op ("e":
"usage", the scope as its class, cu/cs/mp for the values). The exec.run
OTel span carries the same readings as wcprof.usage.<scope>.* attributes.

Signed-off-by: Erik Sipsma <[email protected]>
… exec's cgroup; omit an unknown peak

When a mount helper couldn't join the exec's cgroup, the resource
cgroup's total leaves it out, so the exec_cgroup reading is skipped, as
the resource sampler already skips that cgroup's samples; the container's
own reading still counts. A peak memory that couldn't be read is left off
the exec.run span instead of reported as zero. The tests read a fixture
cgroup tree, so they see an unprofiled exec reading nothing.

Signed-off-by: Erik Sipsma <[email protected]>
…r a session

A profiled session is now an op in wcprof, "session" (kind session_phase,
the session ID as its ident), from its initialization to the end of its
removal. Its end carries the CPU the engine used meanwhile, as usage
readings: the engine process itself, its child processes the engine
reaped meanwhile (runc, and through it the containers it ran), and the
engine's whole cgroup, as the engine's cgroup metrics find it. These are
activity during the session, not CPU the session owned: sessions that
overlap see each other's work.

/debug/wcprof/profile?phase=session arms a CPU profile of the engine over
the next session's life and returns it, in pprof's format, when that
session ends. One profile can be armed at a time; a client that leaves
before the session starts disarms it.

Signed-off-by: Erik Sipsma <[email protected]>
…under one lock

- A session's phase now starts when the session is marked initialized,
  so a session that fails to initialize starts none, and no failure
  cleanup has to end one before the rest of the cleanup has run.
- Arming, starting and disarming a CPU profile happen under one lock: a
  client leaving can't disarm a profile a session is starting, and a
  started profile stays armed until its session ends, so no other profile
  can arm and then fail to start meanwhile. It is still delivered to its
  request at the end.
- Lint: the phase begins only as the session phase, and the profile write
  carries gosec's exemption for binary output.

Signed-off-by: Erik Sipsma <[email protected]>
A request whose profile failed to start, then disarming, cleared whatever
profile was armed, even one armed in its place since. It now clears only
its own.

Signed-off-by: Erik Sipsma <[email protected]>
…ainer cgroup

The container reading existed to tell the workload's usage from the mount
helpers' that run beside it in the exec's cgroup. It had to be read
between the workload's exit and the container's delete, which a runtime
that removes the container itself when its run returns doesn't leave.

Read the helpers' own cgroup (the exec cgroup's sshfs child) instead, with
the exec cgroup's reading after mount teardown, and only when a helper ran:
the workload's usage is the exec cgroup's less the helpers'. Without a
helper, the exec cgroup's reading is the workload's alone. When a helper
couldn't join the exec's cgroup, neither reading would hold it, so neither
is recorded. A separate workload memory peak is no longer read; the exec
cgroup's peak bounds it.

The usage scope "container" becomes "mount_helpers".

Signed-off-by: Erik Sipsma <[email protected]>
A session's removal can take a second after its last query: on a cold
build of dagger's CLI, the release of its dagql cache removes thousands of
owner leases one by one, and the CLI waits for it through its telemetry
streams. Profiles recorded nothing there.

A profiled session's removal is now an op, "session.teardown", under the
session's own op, with an op for each main step: stopping its services,
draining its queries, releasing its cache (the dagql cache release and the
wait for its client scopes), and shutting its telemetry down. They are
native only, like the session's op: the session's telemetry is itself shut
down during the removal.

Signed-off-by: Erik Sipsma <[email protected]>
Profiles stopped at the call and lazy-op level, so what happened inside
an operation (gathering files into a directory, an exec's cleanups after
its process) took a patched engine to see.

dagql.BeginStep records a step inside an operation, named
<operation>.<step>, when the work under its context is profiled: an
internal op in the native profile and, when the engine's telemetry records
there too, an internal OTel span nested the same way. Work that isn't
profiled records nothing and allocates nothing.

The first steps:
- gathering files into a directory (copyFiles, behind withFile and
  withFiles): evaluating the inputs, creating the snapshot, mounting it and
  copying (with each source's mount and copy), the usage, the commit and
  the release;
- each cleanup an exec runs after its process, by its name, with a mount
  target left out so the names stay a bounded set. Cleanups now keep the
  name they were added with, and RunEach runs them through a wrapper that
  gets it.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/wcprof-extension branch from 4dabb6a to bd89642 Compare October 28, 2026 02:42
@grouville
grouville self-requested a review October 28, 2026 16:06
A profiled step was also an internal OTel span, and the UI hides
everything under an internal span. withFiles.evalInputs evaluates lazy
inputs under its step, so on a profiling engine an input's evaluation,
an exec's output included, could disappear from the CLI. Steps are now
recorded only as ops in wcprof's native profile; work under a step
nests directly under the span the step began under.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma merged commit fb7fd04 into main Oct 29, 2026
92 of 93 checks passed
@sipsma
sipsma deleted the sipsma/wcprof-extension branch October 29, 2026 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants