| 255 | } |
| 256 | |
| 257 | class RecordingTracer implements Tracer { |
| 258 | readonly enabled = true; |
| 259 | private events: TraceEvent[] = []; |
| 260 | // Open synchronous sections, innermost last. |
| 261 | private readonly openKind: SpanKind[] = []; |
| 262 | private readonly openStart: number[] = []; |
| 263 | private readonly openFile: Array<number | undefined> = []; |
| 264 | private readonly openChildUs: number[] = []; |
| 265 | private openNonAwaiting = 0; |
| 266 | private readonly spanCount = new Float64Array(SPAN_KINDS); |
| 267 | private readonly spanInclusiveUs = new Float64Array(SPAN_KINDS); |
| 268 | private readonly spanSelfUs = new Float64Array(SPAN_KINDS); |
| 269 | private readonly asyncCount = new Float64Array(SPAN_KINDS); |
| 270 | private readonly asyncWallUs = new Float64Array(SPAN_KINDS); |
| 271 | /** Start of each open async slice, keyed `fileId * SPAN_KINDS + kind`. */ |
| 272 | private readonly asyncOpen = new Map<number, number>(); |
| 273 | private readonly counters = new Float64Array(COUNTERS); |
| 274 | private busyUs = 0; |
| 275 | private startUs = nowMicros(); |
| 276 | private nextFileId = 1; |
| 277 | private rust: RustSummary | undefined = undefined; |
| 278 | private gcObserver: PerformanceObserver | undefined; |
| 279 | private gcCount = 0; |
| 280 | private gcUs = 0; |
| 281 | |
| 282 | constructor(private readonly pid: number) { |
| 283 | // Only created when tracing. GC pauses go on their own track, since their timestamps come |
| 284 | // from a different clock and could cross the main thread's slice boundaries by a few µs. |
| 285 | this.gcObserver = new PerformanceObserver((list) => this.addGcEntries(list.getEntries())); |
| 286 | this.gcObserver.observe({entryTypes: ['gc']}); |
| 287 | } |
| 288 | |
| 289 | private addGcEntries(entries: PerformanceEntry[]): void { |
| 290 | const origin = processStartMicros(); |
| 291 | for (const entry of entries) { |
| 292 | const kind = (entry as PerformanceEntry & {detail?: {kind?: number}}).detail?.kind; |
| 293 | const dur = entry.duration * 1e3; |
| 294 | this.gcCount++; |
| 295 | this.gcUs += dur; |
| 296 | this.events.push({ |
| 297 | name: 'ngp.Gc', |
| 298 | cat: 'ngp.gc', |
| 299 | ph: 'X', |
| 300 | pid: this.pid, |
| 301 | tid: GC_TID, |
| 302 | ts: origin + entry.startTime * 1e3, |
| 303 | dur, |
| 304 | args: {kind: GC_KINDS[kind ?? 0] ?? 'other'}, |
| 305 | }); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** Opens a section and returns a token for `end()`. */ |
| 310 | private begin(kind: SpanKind, fileId: number | undefined): number { |
| 311 | this.openKind.push(kind); |
| 312 | this.openStart.push(nowMicros()); |
| 313 | this.openFile.push(fileId); |
| 314 | this.openChildUs.push(0); |
nothing calls this directly
no test coverage detected