nupp.profile
Two profiling channels: one for where the time went, one for whether it went there compiled.
local profile = nupp.profile
with session = profile.sample({intervalMs = 5}) do
render()
local report = session:stop("profile.out")
print(report.samples, report.stacks)
endprofile.sample is the statistical sampler. A timer interrupts the program and writes down where it was; what comes back is collapsed-stack text, the format speedscope.app, FlameGraph.pl and inferno all read.
profile.trace watches the JIT give up. It aggregates trace aborts, so blacklisted hot code, a bytecode the compiler will not record, and traces that grew past a limit all become rows rather than silence. That is the question a sampler cannot answer and the one that usually matters here: code the JIT refused is an order of magnitude slower than code it took, and nothing says so out loud.
Both channels attribute work through nupp.profile.zone, so a sample or an abort carries the zone path that was open when it happened. Both are process-wide rather than per-coroutine, and at most one session of each kind runs at a time.
Neither is free. A sample session pays a timer interrupt, a stack walk and a table write at every interval; a trace session pays a callback at every abort, inside the compiler. A session is a nupp.Closeable, so a with ends it where its block does: stop returns the report, and a session nothing stopped is closed without one.
See Profiling for reading a report, and LuaJIT trace checking for finding the same aborts without running the program.
Submodules
| Module | Description |
|---|---|
nupp.profile.trace | Stable identities shared by static trace checking and the opt-in runtime collector. |
nupp.profile.zone | Gated LuaJIT profiler zones: the stack work is skipped until a profiler asks for it. |
Module contents
Types
| Type | Kind | Description |
|---|---|---|
AbortSite | record | A place the JIT gave up, and how often it did. |
Sample | record | A distinct stack, and what landed on it. |
SampleOptions | type | What profile.sample collects, and how much of it. |
SampleReport | record | What a sample session saw, as SampleSession:stop returns it. |
SampleSession | record | A running sampler, as profile.sample returns it. |
Session | interface | A running profiling session whose declaration chooses the report stop returns. |
Severity | type | How much an abort is worth reading. |
SourceSample | type | Lua leaf samples attributed to a source line. |
TraceOptions | type | What profile.trace counts. |
TraceProfile | record | The exact recorder configuration used to normalize a trace report. |
TraceReport | record | What a trace session saw, as TraceSession:stop returns it. |
TraceSession | record | A running trace-abort collector, as profile.trace returns it. |
Functions
| Function | Kind | Description |
|---|---|---|
sample | function | Starts sampling. |
trace | function | Starts collecting trace aborts. |
Types#
AbortSiterecord#
record profile.AbortSite ...A place the JIT gave up, and how often it did.
A row of a TraceReport, built at stop.
Members
| Name | Kind | Description |
|---|---|---|
severity | field | How much it is worth reading. |
count | field | Times this exact severity, reason, location and zone fired. |
reason | field | The reason, from jit.vmdef.traceerr. |
reasonId | field | Stable compiler identity and classification for the raw VM reason above. |
reasonClass | field | What the identity is worth acting on: a blocker cannot record at all, a risk may or may not sit on a hot path, and a... |
rawReason | field | The VM's own text, before the registry mapped it. |
location | field | " |
rootLocation | field | Where the root trace began, including when recording entered a library. |
zonePath | field | The zone path that was open, "" when none was. |
reasonfield#
reason: stringThe reason, from jit.vmdef.traceerr. An unrecordable bytecode is rendered with the opcode's name.
reasonIdfield#
reasonId: stringStable compiler identity and classification for the raw VM reason above.
reasonClassfield#
reasonClass: 'blocker' | 'risk' | 'stop'What the identity is worth acting on: a blocker cannot record at all, a risk may or may not sit on a hot path, and a stop is trace formation working as designed.
rootLocationfield#
rootLocation: string?Where the root trace began, including when recording entered a library.
Samplerecord#
record profile.Sample ...A distinct stack, and what landed on it.
A row of a SampleReport, built at stop. The five state counts sum to count.
Members
| Name | Kind | Description |
|---|---|---|
zonePath | field | The zone path the samples were taken under, "" when none was open. |
stack | field | The stack as dumpstack rendered it, ";" between frames, outermost first. |
count | field | Samples on this stack, in every VM state. |
compiled | field | Samples running compiled machine code. |
interpreted | field | Samples in the interpreter. |
cCode | field | Samples inside a C function. |
collecting | field | Samples in the garbage collector. |
compiling | field | Samples inside the JIT compiler itself. |
SampleOptionstype#
type profile.SampleOptions = {
--- Milliseconds between samples; 10 by default, which is 100 a second. Must be
--- between 1 and 2147483647, the range LuaJIT's profiler accepts. Below about 10
--- the timer starts taking real time away from the thread it is measuring, so
--- lower it for a short window and read the result knowing that it was paid for.
intervalMs: integer?,
--- How many frames to walk per sample; 16 by default. Must be between 1 and
--- 2147483647, the range LuaJIT's stack dumper accepts. The walk is linear in
--- this and it happens on the interrupted thread, so raise it only when a
--- specific question needs the depth.
stackDepth: integer?,
--- Keep only the samples taken under a zone path starting with this, so
--- "frame/render" reads as that subtree alone.
---
--- Applied at `stop` rather than while sampling: narrowing it costs nothing at
--- runtime, and widening it afterwards is not possible, because the prefix is fixed
--- when the session starts.
zone: string?,
--- The module the program starts at, as a stack frame names it. Everything below
--- the outermost frame from it is dropped.
---
--- A profiler samples the whole stack it is embedded in, and what is under the
--- program, the loader that read it and the pcall that guards it, is not the
--- program. Naming its module cuts the report back to it.
---
--- Frames read "<module>:<name>", so the module is the part to give. A stack with
--- no frame from it is kept whole rather than emptied, and two stacks that differ
--- only below the root become one, their counts summed.
root: string?
}What profile.sample collects, and how much of it. Every field is optional.
SampleReportrecord#
record profile.SampleReport ...What a sample session saw, as SampleSession:stop returns it.
tostring on it is the collapsed-stack text, so it prints and pipes directly.
Members
| Name | Kind | Description |
|---|---|---|
intervalMs | field | The interval the session ran at, so the sample counts can be read as time. |
samples | field | Samples recorded, after the zone filter. |
sourceSamples | field | Lua leaf locations, ordered by file and line. |
stacks | field | Distinct stacks they fell on. |
text | field | One line per stack: semicolon-separated frames, a space, then the sample count. |
__tostring |
intervalMsfield#
intervalMs: integerThe interval the session ran at, so the sample counts can be read as time.
sourceSamplesfield#
sourceSamples: {profile.SourceSample}Lua leaf locations, ordered by file and line. Their sum can be less than samples because native VM states have no reliable Lua source location.
textfield#
text: stringOne line per stack: semicolon-separated frames, a space, then the sample count. Ordered by count descending. Empty when nothing was sampled, which a short session and a zone prefix that matched nothing both produce.
__tostring#
__tostring: function(self): stringArguments
| Name | Type | Description |
|---|---|---|
? | self |
Returns
| Type | Description |
|---|---|
string |
SampleSessionrecord#
A running sampler, as profile.sample returns it.
Live until stop or close, and there is at most one at a time. Held by a with, it is closed where the block ends, so the timer never outlives the question.
Members
| Name | Kind | Description |
|---|---|---|
Report | field | |
intervalMs | field | The interval it was started at. |
zoneFilter | field | The zone prefix stop will filter by, or nil for all of them. |
root | field | The module stop will cut the stacks back to, or nil to keep them whole. |
paused | field | Whether recording is suspended. |
stopped | field | Whether stop has run. |
aggregate | field | Samples so far, by zone path and then by stack. |
sourceSamples | field | Lua leaf locations counted separately in each zone, before filtering. |
pause | method | Stops recording without ending the session. |
resume | method | Resumes recording. |
stop | method | Ends the session and reports what it saw. |
close | method | Ends the session without a report, which is what leaving its with does. |
Reportfield#
Report: associatedDeclaggregatefield#
Samples so far, by zone path and then by stack. Two levels rather than one joined key, so a sample taken in a zone that has not changed since the last one costs a single table lookup.
sourceSamplesfield#
sourceSamples: {[string]: {[string]: integer}}Lua leaf locations counted separately in each zone, before filtering.
pausemethod#
pause: function(self)Stops recording without ending the session. The timer keeps firing, at the cost of one test per sample, and what was recorded on either side of the pause is kept, which is how a benchmark leaves its setup out.
Idempotent.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
Raises
| Type | Condition |
|---|---|
string | once the session has stopped |
resumemethod#
resume: function(self)Resumes recording. Idempotent.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
Raises
| Type | Condition |
|---|---|
string | once the session has stopped |
stopmethod#
stop: function(self, filename: string?): self.ReportEnds the session and reports what it saw.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
filename | string? |
Returns
| Type | Description |
|---|---|
self.Report |
Raises
| Type | Condition |
|---|---|
string | once the session has stopped, so it cannot be called twice |
closemethod#
close: function(takes self: profile.SampleSession): nilEnds the session without a report, which is what leaving its with does.
Does nothing once stop has run, so a block that stopped its session to read the report closes it for free.
Arguments
| Name | Type | Description |
|---|---|---|
takes self | profile.SampleSession |
Returns
| Type | Description |
|---|---|
nil |
Sessioninterface#
interface profile.Session ...A running profiling session whose declaration chooses the report stop returns.
Sampling and trace-abort sessions share this lifecycle. Generic helpers can use S.Report to preserve the concrete report chosen by a session declaration, and take the session borrows. Each session is also a nupp.Closeable: close, which leaving a with calls, ends one that nothing stopped without building a report, and does nothing after stop. The two are separate so a helper bounded by this one can still name S.Report.
Members
| Name | Kind | Description |
|---|---|---|
Report | field | What stop returns, chosen by the session declaration rather than here. |
pause | method | Stops recording without ending the session. |
resume | method | Resumes recording. |
stop | method | Ends the session and returns what it saw, optionally writing it to a file. |
Reportfield#
Report: associatedDeclWhat stop returns, chosen by the session declaration rather than here.
pausemethod#
pause: function(self)Stops recording without ending the session. Idempotent.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
resumemethod#
resume: function(self)Resumes recording. Idempotent.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
stopmethod#
stop: function(self, filename: string?): self.ReportEnds the session and returns what it saw, optionally writing it to a file.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
filename | string? |
Returns
| Type | Description |
|---|---|
self.Report |
Severitytype#
type profile.Severity = "blacklist" | "warn" | "info"How much an abort is worth reading.
blacklist is always actionable: the code is demoted to the interpreter for the rest of the process. warn is a refusal that may or may not sit on a hot path. info is trace formation working as designed.
SourceSampletype#
type profile.SourceSample = {
file: string,
line: integer,
samples: integer
}Lua leaf samples attributed to a source line. Native VM states have no reliable Lua location and are excluded.
TraceOptionstype#
type profile.TraceOptions = {
--- Include the aborts that are ordinary trace formation rather than a refusal:
--- leaving a loop, recursion, an inner loop. False by default; turn it on when the
--- question is why a particular trace never formed.
includeBenign: boolean?
}What profile.trace counts. Every field is optional.
TraceProfilerecord#
record profile.TraceProfile ...The exact recorder configuration used to normalize a trace report.
Two reports are comparable when their id matches. A report taken under an unsupported recorder still reads, but its reasons were mapped from a VM the registry was not written against.
Members
| Name | Kind | Description |
|---|---|---|
id | field | Every field below joined, which is what two reports are compared on. |
luajitRevision | field | The pinned LuaJIT commit, or "external: |
luajitVersion | field | The snapshot timestamp from the full version string. |
architecture | field | jit.arch. |
operatingSystem | field | jit.os. |
enabledRecorderFeatures | field | The recorder flags jit.status reported, sorted. |
bytecodeSchema | field | A digest of the VM's bytecode name table, so a renumbered opcode set is visible rather than silently remapped. |
supported | field | Whether this is the recorder the reason registry was written against. |
luajitRevisionfield#
luajitRevision: stringThe pinned LuaJIT commit, or "external:
enabledRecorderFeaturesfield#
enabledRecorderFeatures: {string}The recorder flags jit.status reported, sorted.
bytecodeSchemafield#
bytecodeSchema: stringA digest of the VM's bytecode name table, so a renumbered opcode set is visible rather than silently remapped.
supportedfield#
supported: booleanWhether this is the recorder the reason registry was written against.
TraceReportrecord#
record profile.TraceReport ...What a trace session saw, as TraceSession:stop returns it.
tostring on it renders sites as RFC 4180 CSV, so it prints, sorts and diffs directly.
Members
| Name | Kind | Description |
|---|---|---|
durationSec | field | Wallclock seconds the session was active, in whole seconds: it comes from os.time, so a session shorter than one... |
totalAborts | field | Abort events recorded. |
blacklisted | field | Blacklist events among them. |
sites | field | One row per distinct severity, reason, location and zone. |
traceProfile | field | The recorder and registry identities under which the events were interpreted. |
reasonCatalogId | field | The reason registry the identities came from. |
reasonCatalogVersion | field | Its version, so a stored report can be reread under a later one. |
__tostring |
durationSecfield#
durationSec: integerWallclock seconds the session was active, in whole seconds: it comes from os.time, so a session shorter than one reads as zero and dividing by it is the caller's problem.
totalAbortsfield#
totalAborts: integerAbort events recorded. Excludes the benign ones unless includeBenign was set.
sitesfield#
One row per distinct severity, reason, location and zone. Ordered by severity, then by count descending.
traceProfilefield#
traceProfile: profile.TraceProfileThe recorder and registry identities under which the events were interpreted.
reasonCatalogVersionfield#
reasonCatalogVersion: integerIts version, so a stored report can be reread under a later one.
__tostring#
__tostring: function(self): stringArguments
| Name | Type | Description |
|---|---|---|
? | self |
Returns
| Type | Description |
|---|---|
string |
TraceSessionrecord#
A running trace-abort collector, as profile.trace returns it.
Live until stop or close, and there is at most one at a time. Held by a with, it is closed where the block ends, so the event hook never outlives the question.
Members
| Name | Kind | Description |
|---|---|---|
Report | field | |
includeBenign | field | Whether the benign trace-formation events are being counted. |
startedAt | field | os.time when the session started. |
paused | field | Whether aggregation is suspended. |
stopped | field | Whether stop has run. |
sites | field | Aborts so far, by severity, reason, location and zone joined. |
totalAborts | field | Aborts counted so far. |
blacklisted | field | Blacklist events among them. |
traceProfile | field | |
callback | method | The handler to hand back to jit.attach to detach it. |
pause | method | Stops counting without ending the session. |
resume | method | Resumes counting. |
stop | method | Ends the session and reports what it saw. |
close | method | Ends the session without a report, which is what leaving its with does. |
Reportfield#
Report: associatedDeclincludeBenignfield#
includeBenign: booleanWhether the benign trace-formation events are being counted.
sitesfield#
Aborts so far, by severity, reason, location and zone joined.
traceProfilefield#
traceProfile: profile.TraceProfilecallbackmethod#
callback: function(...: any)The handler to hand back to jit.attach to detach it. Dropping the last reference to a handler does not remove it.
Arguments
| Name | Type | Description |
|---|---|---|
... | any |
pausemethod#
pause: function(self)Stops counting without ending the session. The hook stays attached, at the cost of one test per abort, and what was counted on either side of the pause is kept.
Idempotent.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
Raises
| Type | Condition |
|---|---|
string | once the session has stopped |
resumemethod#
resume: function(self)Resumes counting. Idempotent.
Arguments
| Name | Type | Description |
|---|---|---|
? | self |
Raises
| Type | Condition |
|---|---|
string | once the session has stopped |
stopmethod#
stop: function(self, filename: string?): self.ReportEnds the session and reports what it saw.
Arguments
| Name | Type | Description |
|---|---|---|
? | self | |
filename | string? |
Returns
| Type | Description |
|---|---|
self.Report |
Raises
| Type | Condition |
|---|---|
string | once the session has stopped, so it cannot be called twice |
closemethod#
close: function(takes self: profile.TraceSession): nilEnds the session without a report, which is what leaving its with does.
Does nothing once stop has run, so a block that stopped its session to read the report closes it for free.
Arguments
| Name | Type | Description |
|---|---|---|
takes self | profile.TraceSession |
Returns
| Type | Description |
|---|---|
nil |
Functions#
profile.samplefunction#
function profile.sample(options: profile.SampleOptions?): profile.SampleSessionStarts sampling.
with session = profile.sample({intervalMs = 5, zone = "frame/render"}) do
render()
local report = session:stop("render.collapsed")
print(report.samples, report.stacks)
endArguments
| Name | Type | Description |
|---|---|---|
options | profile.SampleOptions? | omitted samples every zone at 10 ms to a depth of 16 |
Returns
| Type | Description |
|---|---|
profile.SampleSession | the handle whose |
Raises
| Type | Condition |
|---|---|
string | when a sample session is already running or an option is out of range |
profile.tracefunction#
function profile.trace(options: profile.TraceOptions?): profile.TraceSessionStarts collecting trace aborts.
with session = profile.trace() do
render()
local report = session:stop("aborts.csv")
print(report.blacklisted, report.totalAborts)
endArguments
| Name | Type | Description |
|---|---|---|
options | profile.TraceOptions? | omitted leaves the benign trace-formation events out |
Returns
| Type | Description |
|---|---|
profile.TraceSession | the handle whose |
Raises
| Type | Condition |
|---|---|
string | when a trace session is already running |