Call Stack Samples to Events
MediumStack ยท Anthropic
A sampling profiler records samples, a list of [timestamp, stack] pairs in increasing timestamp order. stack lists the active function names from outermost to innermost. Return the call events as strings "start <name> <t>" and "end <name> <t>".
The longest common prefix of two consecutive stacks keeps running. At the later timestamp, the remaining frames of the earlier stack end, innermost first, and then the remaining frames of the later stack start, outermost first. The first sample starts all of its frames. After the last sample, every open frame ends at its timestamp, innermost first.
Example 1
- Input:
- samples = [[0, ["main", "load"]], [5, ["main", "parse"]], [9, ["main"]]]
- Output:
- ["start main 0", "start load 0", "end load 5", "start parse 5", "end parse 9", "end main 9"]
- Explanation
mainstays open throughout.loadends andparsestarts at 5, andparseends at 9.
Example 2
- Input:
- samples = [[1, ["f", "f", "f"]], [4, ["f"]], [6, ["f", "g"]]]
- Output:
- ["start f 1", "start f 1", "start f 1", "end f 4", "end f 4", "start g 6", "end g 6", "end f 6"]
- Explanation
- Frames match by position, so the two deeper
fframes end at 4 even thoughfis still running.
Example 3
- Input:
- samples = [[2, []], [3, ["a"]], [7, []], [8, ["a"]]]
- Output:
- ["start a 3", "end a 7", "start a 8", "end a 8"]
Constraints
0 <= samples.length <= 10^40 <= timestamp <= 10^12, strictly increasing acrosssamples0 <= stack.length <= 50- Function names are non-empty strings of at most 20 characters and may repeat within a stack