Skip to content

refactor: extract a format-neutral profile normalizer from the pprof serializer - #430

Open
szegedi wants to merge 3 commits into
szegedi/pprof-fixesfrom
szegedi/profile-normalizer
Open

szegedi wants to merge 3 commits into
szegedi/pprof-fixesfrom
szegedi/profile-normalizer

Conversation

@szegedi

@szegedi szegedi commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?:

Splits pprof serialization into two layers: a format-neutral normalizer that turns V8 profiles into frames, stacks and samples, and the pprof encoder on top of it. pprof output is unchanged.

The normalizer (ts/src/profile-normalizer.ts) owns everything in the old serializer that was profile semantics rather than pprof encoding. It delivers the result to a ProfileVisitor.

The pprof serializer becomes one visitor.

The visitor design maps directly onto formats whose dictionaries reference frames and stacks by index, such as OpenTelemetry profiles (stack_table, timestamps_unix_nano). An OTel visitor is planned as a follow-up.

Motivation:

This is step 1 of emitting the OpenTelemetry profiles signal from the Node.js profiler: separate the profile semantics from the pprof encoding, so a second encoder can reuse them without duplicating, and drifting from, the pprof behaviour.

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Overall package size

Self size: 2.74 MB
Deduped: 3.45 MB
No deduping: 3.45 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | pprof-format | 2.3.2 | 504.33 kB | 504.33 kB | | source-map | 0.8.0 | 185.66 kB | 185.66 kB | | node-gyp-build | 4.8.4 | 13.86 kB | 13.86 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@szegedi
szegedi marked this pull request as ready for review November 4, 2026 10:54
Move everything in profile-serializer.ts that is about profile semantics
rather than pprof encoding into a new profile-normalizer.ts. That covers
the tree walk, source mapping, synthetic runtime frames, per-context
sample expansion, CPU time attribution, label generation and sample
values. The normalizer streams resolved frames, stacks and samples to a
ProfileVisitor, and reports the visit result to its end() method. The
pprof serializer is now one such visitor and its output is
byte-identical.

Samples refer to a deduplicated stack by ID, which mirrors the OTel
profiles stack_table and libdatadog's stacktrace interning. Stacks are
interned by prefix, through a small map of child stacks per stack, so
equal stacks get equal IDs without comparing arrays. A stack is only
delivered when its first sample is emitted, so visitors never receive
stacks that no sample uses.

Frames are looked up by their raw position first, with a short key. The
source mapper, and the longer key that includes the resolved file, are
needed only for a position not seen before, so a repeated position no
longer goes through the source mapper again, which the serializer on
main did for every node. On profiles from a TypeScript-compiler workload
(about 43,000 time and 12,000 to 20,000 heap nodes), pprof serialization
is within about 3-7% of main.

Expose the new layer publicly: normalizeTimeProfile/normalizeHeapProfile,
visitTimeProfile/visitHeapProfile, time.stopRaw(), time.stopNormalized()
and heap.normalizedProfile(). This lets other encoders, such as an OTel
profiles exporter, reuse the same profile semantics.
Samples emitted for a labelled context now carry the context's timestamp
as NormalizedSample.timestamp, in nanoseconds since epoch. Aggregated
samples and synthetic contexts (timestamp 0) have none. Until now the
only way to see a timestamp was through generateLabels, which dd-trace
uses to add an end_timestamp_ns label. OTel profiles carry timestamps in
Sample.timestamps_unix_nano instead, and labels there should have
bounded cardinality.

By default, the hits and CPU time of contexts without labels are folded
into one sample per node, without a timestamp, so a profile can mix
timestamped and aggregated samples. OTel profiles recommend that all
samples in a profile have the same shape. With the new samplePerContext
option, every context becomes its own timestamped sample. Hits without
any context are still aggregated, so the totals match the default mode.

time.stopNormalized() now takes an options object (generateLabels,
samplePerContext). The option types are exported.
The collection time in nanoseconds since epoch is around 1.7e18, beyond
Number.MAX_SAFE_INTEGER, so Date.now() * 1000 * 1000 rounded it to a
double. ProfileMetadata.timeNanos is now a bigint computed exactly, and
so is the heap normalizer's startTimeNanos option that feeds it. The
pprof serializer still gives Profile.timeNanos a number equal to
Date.now() * 1000 * 1000, so pprof output and the Profile objects
returned to callers are unchanged. serializeHeapProfile() accepts either
a number or a bigint start time.

Context timestamps are V8 timer values converted to microseconds since
the UNIX epoch with an offset the native profiler computes at stop. The
profile's startTime and endTime remained raw V8 timer values, so the
collection period couldn't be placed on the epoch timeline that sample
timestamps use, and timeNanos, the time of normalization, lies after
every sample. A format that bounds sample timestamps by the profile's
time range (OTel: [time_unix_nano, time_unix_nano + duration_nano))
therefore got timestamps before the range.

The native profiler now computes the offset once per stop, uses it for
the sample timestamps, and returns it as the profile's epochOffset. The
normalizer reports the start of the collection period as the new
ProfileMetadata.startTimeNanos: exactly with the offset, or estimated
as the collection time minus the duration without one. timeNanos keeps
its meaning, so pprof output is unchanged.
@szegedi
szegedi force-pushed the szegedi/profile-normalizer branch from 57960ad to 212f122 Compare November 4, 2026 12:39

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant