Profile Aggregator
Prerequisites: Profiling the Hot Path, Performance Counters Intro, Systematic Debugging Storytelling
A sampling profiler attached to the AAPL feed handler records one call stack per sample. Build the aggregator that turns them into the article's two views: a flat ranking of functions and the profile tree.
- Folded stack: one line per distinct stack, written by a script before anyone reads the result. It lists the frames from the outermost caller to the function that was executing, joined by
;, then a space and the number of samples with exactly that stack.
For example, handle_update;parse_message;memcpy 31 says that 31 samples caught memcpy running, called from parse_message, which was called from handle_update.
What to implement
process_commands(commands)returns the printed lines in command order. Each command is one line.- A line that starts with one of the uppercase keywords below is a query, and it sees every record before it.
- Every other line is a folded-stack record: it adds its samples to the current profile and prints nothing.
- On stdin, the first line is the number of lines that follow, records and queries together.
- There are three parts. One solution must pass all nine tests.
| Command | Meaning | Prints |
|---|---|---|
f1;f2;...;fn count | A record: count samples whose stack was f1 calling f2 and so on down to fn | nothing |
TOP k | Rank the functions by self samples | a header, then up to k lines |
TREE p | The call tree, pruned at p tenths of a percent (Part 2) | a header, then one line per kept node |
BASELINE | Save the current profile as the baseline and start an empty one (Part 3) | one line |
DIFF p | Compare self shares, baseline against current (Part 3) | a header, then one line per changed function |
Records
- Frame names are 1 to 40 characters from lowercase letters, digits,
_,., and:, and start with a lowercase letter. Sobook.applyandunordered_map::findare valid, and no name can be mistaken for a keyword. - A stack has 1 to 64 frames, and a record line contains exactly one space.
- The same stack may appear on several lines, and its counts add.
Limits
- Every count is between 1 and 1010, and there are at most 104 commands, so a profile can hold far more than 231 samples. Store counts and sums in 64-bit integers (
std::int64_t).
Part 1: Self and total samples
T is the number of samples in the current profile, the sum of its records' counts. Every function name in the profile gets two numbers:
- Self samples: those whose stack ends with it (the leaf frame), so the function itself was executing.
- Total samples: those whose stack contains it at least once, so it was executing or waiting on something it called.
- A function that appears more than once in one stack, through recursion, counts that stack's samples once toward its total.
- A name is one function wherever it appears:
memcpyunderparse_messageandmemcpyundersnapshotadd to the same two numbers. - A share is in tenths of a percent, rounded to the nearest tenth with halves up, using only integers:
tenths = (2000 * x + T) / (2 * T)forxsamples, with integer division. The product2000 * xneeds 64 bits. - A share prints as
tenths / 10, a dot,tenths % 10, and%. For example, 5 samples out of 16 is 31.25 percent and prints as31.3%; 2 out of 16 prints as12.5%, and all 16 print as100.0%. TOP korders functions by self samples descending, then total samples descending, then name ascending by character code (the order that<gives onstd::stringand on Python strings).- A function that is never a leaf has 0 self samples and is ranked too.
- When the profile has fewer than
kfunctions, all of them print. An empty profile prints only the header.
Output of TOP k: the header, then one line for each of the first k functions.
TOP k samples=T name self self_share total total_share
self_shareandtotal_shareare the shares ofselfandtotal.
Limits
kis between 1 and 104.
Tests 1–3 cover this part.
Part 2: The call tree
The flat view merges every call path into one pair of numbers per name. The tree keeps the paths apart.
- Each node of the call tree is a distinct stack prefix, named by its last frame. The record
handle_update;parse_message;memcpy 31passes through the nodes forhandle_update,handle_update;parse_message, andhandle_update;parse_message;memcpy. - A node's total counts the samples whose stack starts with its prefix. Its self counts those whose stack is exactly that prefix.
- Recursion therefore appears as nested nodes with the same name, each with its own counts.
- The outermost frames of all stacks form the top level, which may hold several nodes, such as
handle_updateandidle. TREE pprints the nodes in preorder: a node, then the subtree of each of its children.- The top-level nodes, and the children of every node, are ordered by total descending, then name ascending.
- A node prints only when its printed total share, in tenths, is at least
p. A node that fails this test is skipped together with its whole subtree.
Output of TREE p: the header, then one line per kept node.
TREE p samples=T {dots}name total total_share self
{dots}is2 * ddots for a node at depthd, where the top level is depth 0. The dots stand in for indentation.
Limits and guarantees
pis between 0 and 1000, soTREE 0prints every node.- A child's total never exceeds its parent's, so skipping a subtree never hides a node that would have passed.
Tests 4–6 cover this part.
Part 3: Before and after
The method from the article records a baseline, changes one thing, and measures again.
BASELINEprintsBASELINE samples=Tfor the current profile, makes that profile the baseline (replacing any earlier one), and starts a new, empty current profile.- Later records go into the new profile.
TOPandTREEreport only the current profile. DIFF pconsiders every function that appears in either profile. A function missing from one profile has 0 self samples there.- A function's change is its printed self share in the current profile minus its printed self share in the baseline, both in tenths. So the change is exactly the difference of the two printed numbers.
- Functions whose change has a magnitude of at least
pprint, ordered by magnitude descending, then name ascending.
Output of DIFF p: the header, then one line per printed function.
DIFF p before=B after=T name before_self before_share after_self after_share change
Bis the baseline's sample count, andTthe current profile's.changeis+or-followed by the magnitude in the share format, such as+10.3%.
Limits and guarantees
pis between 1 and 1000, so a printed change is never zero.- Every
DIFFcomes after aBASELINE. When it runs, both the baseline and the current profile hold at least one sample.
Tests 7–9 cover this part.
9 TOP 3 handle_update;parse_message;memcpy 31 handle_update;parse_message 7 handle_update;book.apply;unordered_map::find 29 handle_update;book.apply;vector::push_back 12 handle_update;book.apply 3 handle_update;strategy.on_update 15 handle_update 3 TOP 7
TOP 3 samples=0 TOP 7 samples=100 memcpy 31 31.0% 31 31.0% unordered_map::find 29 29.0% 29 29.0% strategy.on_update 15 15.0% 15 15.0% vector::push_back 12 12.0% 12 12.0% parse_message 7 7.0% 38 38.0% handle_update 3 3.0% 100 100.0% book.apply 3 3.0% 44 44.0%
The first TOP runs before any record, so it prints only its header. The seven records are the profile from the article, 100 samples in all.
memcpy is the leaf of 31 samples and appears in no other stack, so its self and total are both 31.
parse_message is the leaf of 7 samples and sits in the stacks of 31 + 7 = 38, which matches the 38% the article's tree shows for it.
handle_update and book.apply tie on 3 self samples, so the larger total decides: handle_update, at 100, ranks above book.apply, at 3 + 29 + 12 = 44.