/** * Extra fields recorded with a trace event. Keep them flat and small: * every event is one JSON line. */ export type TraceFields = Record; /** * One trace event. `t` is an absolute wall clock in milliseconds * (`performance.timeOrigin + performance.now()`), the same clock on every * thread, so events from workers and the main thread can be laid on one * timeline. `tid` is the worker thread id, 0 for the main thread. */ export type TraceEvent = { t: number; tid: number; ev: string; } & TraceFields; export type TracerOptions = { /** Path of the JSON-lines file to append events to (`VLT_TRACE`). */ file?: string; /** Print a one-line phase summary to stderr at exit (`VLT_PHASES`). */ phases?: boolean; /** The thread id to stamp events with. Defaults to the real one. */ threadId?: number; /** Absolute clock in ms. Defaults to the shared performance clock. */ now?: () => number; /** Appends a chunk of lines to the trace file. */ append?: (file: string, data: string) => void; /** Writes the phase summary line. Defaults to stderr. */ write?: (line: string) => void; /** Flush after this many buffered lines. */ flushLines?: number; /** Flush this many ms after the first buffered line. */ flushMs?: number; /** Sample `rss` this often, in ms. 0 disables sampling. */ sampleMs?: number; /** Whether this is the main thread, which owns the event loop monitor. */ main?: boolean; }; /** * A low overhead tracer for the install pipeline. * * It is off unless `VLT_TRACE=` or `VLT_PHASES=1` is set, and when * off every method returns after one boolean check, so call sites can * stay in the hot path. * * With `VLT_TRACE` every event is appended to the file as one JSON line. * Lines are buffered and flushed every `flushLines` lines or `flushMs` * ms, and at exit. Worker threads * get their own instance from the same env and append to the same file, * so the file interleaves threads; sort by `t` to read it. * * With `VLT_PHASES` the main thread prints one line at exit: the ms * between consecutive {@link Tracer.mark} calls, the event loop delay * percentiles (the direct measure of how long the main thread was busy * with synchronous work while IO completions waited), and peak memory. */ export declare class Tracer { #private; readonly enabled: boolean; readonly file?: string; readonly phases: boolean; constructor(options?: TracerOptions); /** * Build a tracer from the environment: `VLT_TRACE` names the file, * `VLT_PHASES` (any value but `0` or empty) asks for the summary. */ static fromEnv(env?: Record, options?: Omit): Tracer; /** Record one event. */ trace(ev: string, fields?: TraceFields): void; /** * Record a `:start` event now and return a function that records * `:end` with the elapsed `ms` (plus any fields it is given). The * same `fields` are stamped on both ends so the two can be paired. */ span(ev: string, fields?: TraceFields): (end?: TraceFields) => void; /** * Mark the start of a phase. The summary reports the ms between each * mark and the next one, and the trace records a `phase` event. */ mark(name: string, fields?: TraceFields): void; /** * Record how busy a resource lane is: how many jobs it is running and * how many are waiting for a slot. The report derives idle time per * lane from these. */ lane(name: string, busy: number, queued: number): void; /** Record a memory sample. */ sample(): void; /** Write buffered lines to the file. */ flush(): void; /** * The one-line phase summary, or undefined when phases are off. Each * phase is reported as the ms from its mark to the next mark (or to * now, for the last one). */ summary(): string | undefined; /** * Stop sampling, flush, and print the summary. Runs at process exit * on the main thread; safe to call more than once. */ close(): void; } /** * The process-wide tracer, built from the environment. Worker threads * inherit the environment, so they trace to the same file. */ export declare const tracer: Tracer; /** Record one event on the process-wide tracer. */ export declare const trace: (ev: string, fields?: TraceFields) => void; /** Open a span on the process-wide tracer. See {@link Tracer.span}. */ export declare const span: (ev: string, fields?: TraceFields) => ((end?: TraceFields) => void); /** Mark a phase on the process-wide tracer. See {@link Tracer.mark}. */ export declare const mark: (name: string, fields?: TraceFields) => void; /** Record lane occupancy on the process-wide tracer. */ export declare const lane: (name: string, busy: number, queued: number) => void;