@margoengine/stats 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +413 -2
- package/dist/beacon.d.ts +80 -0
- package/dist/beacon.js +85 -0
- package/dist/compile.d.ts +112 -0
- package/dist/compile.js +141 -0
- package/dist/geometry.d.ts +64 -0
- package/dist/geometry.js +134 -0
- package/dist/gl.d.ts +78 -0
- package/dist/gl.js +174 -0
- package/dist/index.d.ts +78 -0
- package/dist/index.js +1 -0
- package/dist/leaks.d.ts +86 -0
- package/dist/leaks.js +119 -0
- package/dist/panel-gpu.d.ts +14 -0
- package/dist/panels.d.ts +83 -0
- package/dist/panels.js +177 -0
- package/dist/pass-prototypes.d.ts +26 -0
- package/dist/phases.d.ts +78 -0
- package/dist/phases.js +114 -0
- package/dist/profiler.d.ts +28 -0
- package/dist/report.d.ts +56 -0
- package/dist/report.js +125 -0
- package/dist/src-DtZiAj6x.js +368 -0
- package/package.json +80 -4
package/dist/gl.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin, FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
import type { Stats } from './index.js';
|
|
5
|
+
/** A completed named GPU interval in milliseconds. */
|
|
6
|
+
export interface GLPassTime {
|
|
7
|
+
name: string;
|
|
8
|
+
milliseconds: number;
|
|
9
|
+
}
|
|
10
|
+
/** A completed asynchronous WebGL2 timer sample and current frame draw count. */
|
|
11
|
+
export interface GLStatsReport {
|
|
12
|
+
supported: boolean;
|
|
13
|
+
drawCalls: number;
|
|
14
|
+
frameMilliseconds: number | null;
|
|
15
|
+
passes: GLPassTime[];
|
|
16
|
+
sampleId: number;
|
|
17
|
+
droppedFrames: number;
|
|
18
|
+
}
|
|
19
|
+
/** Bounds on query storage. */
|
|
20
|
+
export interface GLStatsOptions {
|
|
21
|
+
/** Named passes per sample, 1..128. Default 128. */
|
|
22
|
+
maxPasses?: number;
|
|
23
|
+
/** Outstanding frames, 1..8. Default 4. */
|
|
24
|
+
maxPendingFrames?: number;
|
|
25
|
+
}
|
|
26
|
+
/** WebGL2 asynchronous frame/pass GPU timers and native draw counters. */
|
|
27
|
+
export declare class GLStats implements EnginePlugin {
|
|
28
|
+
readonly name = "stats-gl";
|
|
29
|
+
drawCalls: number;
|
|
30
|
+
frameMilliseconds: number | null;
|
|
31
|
+
readonly passes: GLPassTime[];
|
|
32
|
+
sampleId: number;
|
|
33
|
+
droppedFrames: number;
|
|
34
|
+
private gl?;
|
|
35
|
+
private ext?;
|
|
36
|
+
private current?;
|
|
37
|
+
private active?;
|
|
38
|
+
private frameOpen;
|
|
39
|
+
private passName;
|
|
40
|
+
private passCount;
|
|
41
|
+
private readonly pending;
|
|
42
|
+
private readonly pool;
|
|
43
|
+
private readonly restore;
|
|
44
|
+
private engine?;
|
|
45
|
+
private disposed;
|
|
46
|
+
private row?;
|
|
47
|
+
private timer?;
|
|
48
|
+
private readonly maxPasses;
|
|
49
|
+
private readonly maxPending;
|
|
50
|
+
constructor({ maxPasses, maxPendingFrames }?: GLStatsOptions);
|
|
51
|
+
/** Attach before other optional plugins to name their registered passes automatically. */
|
|
52
|
+
static attach(engine: EngineKernel, options?: GLStatsOptions): GLStats;
|
|
53
|
+
get supported(): boolean;
|
|
54
|
+
private alive;
|
|
55
|
+
/** Wrap draws on a context. Reattaching after context restoration reacquires the extension. */
|
|
56
|
+
track(gl: WebGL2RenderingContext): void;
|
|
57
|
+
private start;
|
|
58
|
+
private end;
|
|
59
|
+
/** Begin a custom frame. Pending samples are polled without blocking. */
|
|
60
|
+
beginFrame(gl?: WebGL2RenderingContext): boolean;
|
|
61
|
+
/** Begin a non-nested named GPU pass; disjoint samples are discarded as a whole. */
|
|
62
|
+
beginPass(name: string): void;
|
|
63
|
+
endPass(): void;
|
|
64
|
+
/** Enclose a synchronous pass, restoring query state even when it throws. */
|
|
65
|
+
pass<T>(name: string, work: () => T): T;
|
|
66
|
+
endFrame(): void;
|
|
67
|
+
private recycle;
|
|
68
|
+
private clear;
|
|
69
|
+
/** Read only queries whose availability is true; call from a later task/frame. */
|
|
70
|
+
poll(): boolean;
|
|
71
|
+
setup(engine: Engine): void;
|
|
72
|
+
registerPasses(graph: FrameGraph): void;
|
|
73
|
+
/** JSON snapshot of the current counters and last complete GPU sample. */
|
|
74
|
+
toJSON(): GLStatsReport;
|
|
75
|
+
view(stats: Stats): this;
|
|
76
|
+
dispose(): void;
|
|
77
|
+
[Symbol.dispose]: () => void;
|
|
78
|
+
}
|
package/dist/gl.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
5
|
+
//#region src/gl.ts
|
|
6
|
+
var t = class t {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
7
|
+
constructor({ maxPasses: e = 128, maxPendingFrames: t = 4 } = {}) {
|
|
8
|
+
if (this.name = "stats-gl", this.drawCalls = 0, this.frameMilliseconds = null, this.passes = [], this.sampleId = 0, this.droppedFrames = 0, this.frameOpen = !1, this.passName = null, this.passCount = 0, this.pending = [], this.pool = [], this.restore = [], this.disposed = !1, !Number.isInteger(e) || e < 1 || e > 128) throw RangeError("Invalid maxPasses; use an integer from 1 to 128.");
|
|
9
|
+
if (!Number.isInteger(t) || t < 1 || t > 8) throw RangeError("Invalid maxPendingFrames; use an integer from 1 to 8.");
|
|
10
|
+
this.maxPasses = e, this.maxPending = t;
|
|
11
|
+
}
|
|
12
|
+
static attach(e, n = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
13
|
+
let r = new t(n);
|
|
14
|
+
return e.use(r), r;
|
|
15
|
+
}
|
|
16
|
+
get supported() {
|
|
17
|
+
return !!this.ext && !this.gl?.isContextLost();
|
|
18
|
+
}
|
|
19
|
+
alive() {
|
|
20
|
+
if (this.disposed) throw new e("disposed", "GLStats is disposed; create a new profiler.");
|
|
21
|
+
}
|
|
22
|
+
track(t) {
|
|
23
|
+
if (this.alive(), this.gl === t) return;
|
|
24
|
+
if (this.gl) throw new e("attached", "GLStats already tracks a context; create one per context.");
|
|
25
|
+
this.gl = t, this.ext = t.getExtension("EXT_disjoint_timer_query_webgl2") ?? void 0;
|
|
26
|
+
for (let e of [
|
|
27
|
+
"drawArrays",
|
|
28
|
+
"drawElements",
|
|
29
|
+
"drawRangeElements",
|
|
30
|
+
"drawArraysInstanced",
|
|
31
|
+
"drawElementsInstanced"
|
|
32
|
+
]) {
|
|
33
|
+
let n = t[e], r = (...e) => {
|
|
34
|
+
Reflect.apply(n, t, e), !this.disposed && this.frameOpen && this.drawCalls++;
|
|
35
|
+
};
|
|
36
|
+
Object.assign(t, { [e]: r }), this.restore.push(() => {
|
|
37
|
+
t[e] === r && Object.assign(t, { [e]: n });
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
let n = () => {
|
|
41
|
+
this.clear(), this.ext = void 0, this.frameOpen = !1;
|
|
42
|
+
}, r = () => {
|
|
43
|
+
this.disposed || (this.ext = t.getExtension("EXT_disjoint_timer_query_webgl2") ?? void 0);
|
|
44
|
+
};
|
|
45
|
+
t.canvas.addEventListener("webglcontextlost", n), t.canvas.addEventListener("webglcontextrestored", r), this.restore.push(() => {
|
|
46
|
+
t.canvas.removeEventListener("webglcontextlost", n), t.canvas.removeEventListener("webglcontextrestored", r);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
start(t) {
|
|
50
|
+
if (!this.current || this.current.invalid || !this.ext) return;
|
|
51
|
+
let n = this.gl, r = this.pool.pop() ?? n.createQuery();
|
|
52
|
+
if (!r) throw this.clear(), this.frameOpen = !1, this.passName = null, new e("failed", "Cannot allocate a GL timer query; reduce profiler capacity.");
|
|
53
|
+
let i = {
|
|
54
|
+
query: r,
|
|
55
|
+
name: t
|
|
56
|
+
};
|
|
57
|
+
this.current.segments.push(i), n.beginQuery(this.ext.TIME_ELAPSED_EXT, r), this.active = i;
|
|
58
|
+
}
|
|
59
|
+
end() {
|
|
60
|
+
this.active && this.ext && this.gl.endQuery(this.ext.TIME_ELAPSED_EXT), this.active = void 0;
|
|
61
|
+
}
|
|
62
|
+
beginFrame(t) {
|
|
63
|
+
if (this.alive(), t && this.track(t), !this.gl) throw new e("state", "No WebGL2 context; call track(gl) or beginFrame(gl) first.");
|
|
64
|
+
if (this.frameOpen) throw new e("state", "A GL frame is open; call endFrame() before beginning another.");
|
|
65
|
+
return this.poll(), this.drawCalls = 0, this.passCount = 0, this.passName = null, this.frameOpen = !0, this.supported ? this.pending.length >= this.maxPending || this.gl.getQuery(this.ext.TIME_ELAPSED_EXT, this.gl.CURRENT_QUERY) ? (this.droppedFrames++, !1) : (this.current = {
|
|
66
|
+
segments: [],
|
|
67
|
+
invalid: !1
|
|
68
|
+
}, this.start(null), !0) : !1;
|
|
69
|
+
}
|
|
70
|
+
beginPass(t) {
|
|
71
|
+
if (this.alive(), !this.frameOpen || this.passName !== null) throw new e("state", "Pass requires an open frame and no open pass; call endPass() first.");
|
|
72
|
+
if (typeof t != "string" || !t.trim()) throw TypeError("Invalid pass name; use a non-empty string.");
|
|
73
|
+
this.end(), this.passName = t, ++this.passCount > this.maxPasses && this.current && (this.current.invalid = !0), this.start(t);
|
|
74
|
+
}
|
|
75
|
+
endPass() {
|
|
76
|
+
if (this.alive(), this.passName === null) throw new e("state", "No pass is open; call beginPass(name) first.");
|
|
77
|
+
this.end(), this.passName = null, this.start(null);
|
|
78
|
+
}
|
|
79
|
+
pass(e, t) {
|
|
80
|
+
this.beginPass(e);
|
|
81
|
+
try {
|
|
82
|
+
return t();
|
|
83
|
+
} finally {
|
|
84
|
+
this.endPass();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
endFrame() {
|
|
88
|
+
if (this.alive(), !this.frameOpen) throw new e("state", "No frame is open; call beginFrame() first.");
|
|
89
|
+
this.end(), this.passName = null, this.frameOpen = !1, this.current &&= (this.pending.push(this.current), void 0);
|
|
90
|
+
}
|
|
91
|
+
recycle(e) {
|
|
92
|
+
for (let t of e.segments) this.pool.push(t.query);
|
|
93
|
+
}
|
|
94
|
+
clear() {
|
|
95
|
+
this.end(), this.current && this.recycle(this.current), this.current = void 0;
|
|
96
|
+
for (let e of this.pending.splice(0)) this.recycle(e);
|
|
97
|
+
for (let e of this.pool.splice(0)) this.gl.deleteQuery(e);
|
|
98
|
+
this.frameMilliseconds = null, this.passes.length = 0;
|
|
99
|
+
}
|
|
100
|
+
poll() {
|
|
101
|
+
if (this.alive(), !this.supported) return this.gl?.isContextLost() && this.clear(), !1;
|
|
102
|
+
let e = this.gl, t = this.ext;
|
|
103
|
+
if (e.getParameter(t.GPU_DISJOINT_EXT)) return this.clear(), !1;
|
|
104
|
+
let n = !1;
|
|
105
|
+
for (; this.pending.length;) {
|
|
106
|
+
let t = this.pending[0];
|
|
107
|
+
if (!t.segments.every(({ query: t }) => e.getQueryParameter(t, e.QUERY_RESULT_AVAILABLE))) break;
|
|
108
|
+
this.pending.shift();
|
|
109
|
+
let r = t.segments.map(({ query: t }) => Number(e.getQueryParameter(t, e.QUERY_RESULT)) / 1e6);
|
|
110
|
+
t.invalid || r.some((e) => !Number.isFinite(e) || e < 0) ? (this.frameMilliseconds = null, this.passes.length = 0) : (this.frameMilliseconds = r.reduce((e, t) => e + t, 0), this.passes.length = 0, t.segments.forEach(({ name: e }, t) => {
|
|
111
|
+
e !== null && this.passes.push({
|
|
112
|
+
name: e,
|
|
113
|
+
milliseconds: r[t]
|
|
114
|
+
});
|
|
115
|
+
}), this.sampleId++, n = !0), this.recycle(t);
|
|
116
|
+
}
|
|
117
|
+
return n;
|
|
118
|
+
}
|
|
119
|
+
setup(t) {
|
|
120
|
+
if (this.alive(), this.engine) throw new e("attached", "GLStats is attached; create one per engine.");
|
|
121
|
+
this.engine = t;
|
|
122
|
+
}
|
|
123
|
+
registerPasses(e) {
|
|
124
|
+
let t = e.execute, n = e.add, r = (n) => {
|
|
125
|
+
if (this.disposed || n.backend !== "webgl2") return t.call(e, n);
|
|
126
|
+
let r = n.draw;
|
|
127
|
+
this.frameOpen || (this.beginFrame(n.gl), queueMicrotask(() => {
|
|
128
|
+
!this.disposed && this.frameOpen && this.endFrame();
|
|
129
|
+
})), n.draw = (e) => this.passName === null ? this.pass("main", () => r.call(n, e)) : r.call(n, e);
|
|
130
|
+
try {
|
|
131
|
+
t.call(e, n);
|
|
132
|
+
} finally {
|
|
133
|
+
n.draw = r;
|
|
134
|
+
}
|
|
135
|
+
}, i = (t) => n.call(e, {
|
|
136
|
+
...t,
|
|
137
|
+
execute: (e) => this.disposed || e.backend !== "webgl2" || !this.frameOpen || this.passName !== null ? t.execute(e) : this.pass(t.name, () => t.execute(e))
|
|
138
|
+
});
|
|
139
|
+
e.execute = r, e.add = i, this.restore.push(() => {
|
|
140
|
+
e.execute === r && (e.execute = t), e.add === i && (e.add = n);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
toJSON() {
|
|
144
|
+
return {
|
|
145
|
+
supported: this.supported,
|
|
146
|
+
drawCalls: this.drawCalls,
|
|
147
|
+
frameMilliseconds: this.frameMilliseconds,
|
|
148
|
+
passes: this.passes.map((e) => ({ ...e })),
|
|
149
|
+
sampleId: this.sampleId,
|
|
150
|
+
droppedFrames: this.droppedFrames
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
view(e) {
|
|
154
|
+
if (this.alive(), this.row) return this;
|
|
155
|
+
let t = this.row = e.element.ownerDocument.createElement("div");
|
|
156
|
+
t.style.whiteSpace = "pre-line";
|
|
157
|
+
let n = () => {
|
|
158
|
+
t.textContent = `WebGL2 ${this.drawCalls} draws GPU ${this.frameMilliseconds === null ? this.supported ? "warming up" : "unavailable" : this.frameMilliseconds.toFixed(2) + " ms"}\n` + this.passes.map((e) => `${e.name}: ${e.milliseconds.toFixed(2)} ms`).join("\n");
|
|
159
|
+
};
|
|
160
|
+
return e.element.append(t), n(), this.timer = setInterval(n, 150), this;
|
|
161
|
+
}
|
|
162
|
+
dispose() {
|
|
163
|
+
if (!this.disposed) {
|
|
164
|
+
this.clear(), this.disposed = !0, this.engine?.sceneOf(this)?.removePlugin(this.name), this.engine = void 0;
|
|
165
|
+
for (let e of this.restore.splice(0).reverse()) e();
|
|
166
|
+
clearInterval(this.timer), this.row?.remove();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
170
|
+
this.dispose();
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
//#endregion
|
|
174
|
+
export { t as GLStats };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin, FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
import { GPUProfiler } from './profiler.js';
|
|
5
|
+
export { GPUProfiler };
|
|
6
|
+
/** Options for {@link Stats}. */
|
|
7
|
+
export interface StatsOptions {
|
|
8
|
+
/** Element the readout is added to. Default document.body. */
|
|
9
|
+
parent?: HTMLElement;
|
|
10
|
+
/** Number of rolling frame samples, 2..4096. Default 120. */
|
|
11
|
+
samples?: number;
|
|
12
|
+
/** Profile passes when timestamp-query is already enabled on the device. */
|
|
13
|
+
profiling?: boolean;
|
|
14
|
+
/** Opt into shared native-pass prototypes; changes reflective pass behavior. Default false. */
|
|
15
|
+
lowAllocationPasses?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Frame-time, draw-call and (optionally) GPU-time readout. Add it with `Stats.attach(engine)`; `using` or `dispose()` removes it. */
|
|
18
|
+
export declare class Stats implements EnginePlugin {
|
|
19
|
+
readonly name = "stats";
|
|
20
|
+
readonly samples: Float64Array;
|
|
21
|
+
readonly element: HTMLDivElement;
|
|
22
|
+
readonly graph: HTMLCanvasElement;
|
|
23
|
+
fps: number;
|
|
24
|
+
frameTime: number;
|
|
25
|
+
drawCalls: number;
|
|
26
|
+
dispatches: number;
|
|
27
|
+
gpuTime: number | null;
|
|
28
|
+
sampleCount: number;
|
|
29
|
+
sampleIndex: number;
|
|
30
|
+
private total;
|
|
31
|
+
private last;
|
|
32
|
+
private engine?;
|
|
33
|
+
private removeFrameListener?;
|
|
34
|
+
private profiler?;
|
|
35
|
+
private readonly context;
|
|
36
|
+
private readonly text;
|
|
37
|
+
private readonly timer;
|
|
38
|
+
private readonly tracked;
|
|
39
|
+
private readonly renderMethods;
|
|
40
|
+
private readonly computeMethods;
|
|
41
|
+
private disposed;
|
|
42
|
+
private readonly profiling;
|
|
43
|
+
private readonly lowAllocationPasses;
|
|
44
|
+
private passPrototypes?;
|
|
45
|
+
private profileFrame;
|
|
46
|
+
private completedGPU;
|
|
47
|
+
private encodedGPUAt;
|
|
48
|
+
private readGPUAt;
|
|
49
|
+
/** Monotonic ordinal of valid completed GPU samples, including equal-duration samples. */
|
|
50
|
+
get gpuSampleId(): number;
|
|
51
|
+
/** CPU performance.now() when the sampled encoder finished; not a GPU execution timestamp. */
|
|
52
|
+
get gpuSampleEncodedAt(): number | null;
|
|
53
|
+
/** CPU performance.now() when that sample's asynchronous readback completed. */
|
|
54
|
+
get gpuSampleReadAt(): number | null;
|
|
55
|
+
get gpuPassMilliseconds(): Float64Array | null;
|
|
56
|
+
get gpuPassCount(): number;
|
|
57
|
+
get gpuElapsedMilliseconds(): number | null;
|
|
58
|
+
constructor({ parent, samples, profiling, lowAllocationPasses, }?: StatsOptions);
|
|
59
|
+
private ticks;
|
|
60
|
+
private shown;
|
|
61
|
+
private key;
|
|
62
|
+
static attach(engine: EngineKernel, options?: StatsOptions): Stats;
|
|
63
|
+
/** Numeric sampling only: no DOM writes, arrays, strings or callbacks are created here. */
|
|
64
|
+
tick(milliseconds: number, drawCalls?: number, dispatches?: number): void;
|
|
65
|
+
enableGPUProfiling(device: GPUDevice): boolean;
|
|
66
|
+
/** Counts direct/indirect commands on this encoder; pass wrappers are separate from tick(). */
|
|
67
|
+
track(encoder: GPUCommandEncoder): void;
|
|
68
|
+
private static beginRender;
|
|
69
|
+
private static beginCompute;
|
|
70
|
+
private static finish;
|
|
71
|
+
private static readback;
|
|
72
|
+
setup(engine: Engine): void;
|
|
73
|
+
registerPasses(graph: FrameGraph): void;
|
|
74
|
+
refresh(): void;
|
|
75
|
+
onDeviceRestored(engine: Engine): void;
|
|
76
|
+
dispose(): void;
|
|
77
|
+
[Symbol.dispose]: () => void;
|
|
78
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { n as GPUProfiler, t as Stats } from "./src-DtZiAj6x.js";
|
package/dist/leaks.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Stats } from './index.js';
|
|
3
|
+
import type { GPUMemory } from './report.js';
|
|
4
|
+
/** A number that kept growing. */
|
|
5
|
+
export interface LeakAlert {
|
|
6
|
+
/** `'gpu bytes'`, `'buffers'`, `'textures'`, `'label <name> bytes'`, `'label <name> count'`, or a `watch` name. */
|
|
7
|
+
name: string;
|
|
8
|
+
/** The lowest value in the first quarter of the window. */
|
|
9
|
+
from: number;
|
|
10
|
+
/** The lowest value in the last quarter of the window. */
|
|
11
|
+
to: number;
|
|
12
|
+
/** Growth of that low point per minute. */
|
|
13
|
+
perMinute: number;
|
|
14
|
+
/** `performance.now()` of the sample that raised it. */
|
|
15
|
+
at: number;
|
|
16
|
+
}
|
|
17
|
+
/** What {@link LeakWatch.toJSON} returns; safe for `JSON.stringify`. */
|
|
18
|
+
export interface LeakWatchReport {
|
|
19
|
+
samples: number;
|
|
20
|
+
/** Names growing now. */
|
|
21
|
+
leaking: string[];
|
|
22
|
+
/** Every alert raised, newest last. */
|
|
23
|
+
alerts: LeakAlert[];
|
|
24
|
+
}
|
|
25
|
+
/** Options for {@link LeakWatch}. */
|
|
26
|
+
export interface LeakWatchOptions {
|
|
27
|
+
/** Time between samples, or 0 to call `sample()` yourself. Default 5000. */
|
|
28
|
+
intervalMilliseconds?: number;
|
|
29
|
+
/** Samples judged together, 8..4096, a multiple of 4. Default 48 (four minutes at the default interval). */
|
|
30
|
+
window?: number;
|
|
31
|
+
/** Bytes a low point must grow by to alert. Default 1048576. */
|
|
32
|
+
minBytes?: number;
|
|
33
|
+
/** Objects a low point must grow by to alert. Default 16. */
|
|
34
|
+
minCount?: number;
|
|
35
|
+
/** Numbers followed at most, the labels of a {@link GPUMemory} included. Default 64. */
|
|
36
|
+
maxSeries?: number;
|
|
37
|
+
/** Called for each alert. Default `console.warn`; null for none. */
|
|
38
|
+
warn?: ((alert: LeakAlert) => void) | null;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Warns when GPU memory or a resource count keeps growing. Each window of samples is cut into quarters and the lowest
|
|
42
|
+
* value of each is taken: a leak raises all of them, while a load, a level change or a pool that fills and empties does
|
|
43
|
+
* not, however high it peaks. Alerts fire once and can fire again after the growth stops.
|
|
44
|
+
*/
|
|
45
|
+
export declare class LeakWatch {
|
|
46
|
+
/** Samples taken. */
|
|
47
|
+
samples: number;
|
|
48
|
+
private readonly series;
|
|
49
|
+
private readonly alerts;
|
|
50
|
+
private readonly listeners;
|
|
51
|
+
private readonly clock;
|
|
52
|
+
private readonly size;
|
|
53
|
+
private readonly minBytes;
|
|
54
|
+
private readonly minCount;
|
|
55
|
+
private readonly maxSeries;
|
|
56
|
+
private readonly warn;
|
|
57
|
+
private readonly memory?;
|
|
58
|
+
private readonly interval?;
|
|
59
|
+
private timer?;
|
|
60
|
+
private row?;
|
|
61
|
+
private disposed;
|
|
62
|
+
/** Follows a {@link GPUMemory}: its totals and each of its labels. */
|
|
63
|
+
static attach(memory: GPUMemory, options?: LeakWatchOptions): LeakWatch;
|
|
64
|
+
constructor({ intervalMilliseconds, window, minBytes, minCount, maxSeries, warn, }?: LeakWatchOptions, memory?: GPUMemory);
|
|
65
|
+
/** Follows any number, such as a mesh count, until `dispose()`. `min` is how far its low point must grow. */
|
|
66
|
+
watch(name: string, read: () => number, { min }?: {
|
|
67
|
+
min?: number;
|
|
68
|
+
}): this;
|
|
69
|
+
private add;
|
|
70
|
+
private record;
|
|
71
|
+
private push;
|
|
72
|
+
/** Reads every number once and returns the alerts this raised. */
|
|
73
|
+
sample(now?: number): LeakAlert[];
|
|
74
|
+
private judge;
|
|
75
|
+
/** Names growing now. */
|
|
76
|
+
get leaking(): string[];
|
|
77
|
+
/** Calls `listener` for each alert; returns its remover. */
|
|
78
|
+
onLeak(listener: (alert: LeakAlert) => void): () => void;
|
|
79
|
+
/** A copy of the state. */
|
|
80
|
+
toJSON(): LeakWatchReport;
|
|
81
|
+
/** Shows what is growing in a row of the Stats panel, refreshed twice a second until `dispose()`. */
|
|
82
|
+
view(stats: Stats): this;
|
|
83
|
+
/** Stops sampling and removes the view. */
|
|
84
|
+
dispose(): void;
|
|
85
|
+
[Symbol.dispose]: () => void;
|
|
86
|
+
}
|
package/dist/leaks.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
5
|
+
//#region src/leaks.ts
|
|
6
|
+
var t = (e) => console.warn(`Possible leak: ${e.name} keeps growing, ${e.from} to ${e.to} (${Math.round(e.perMinute)} per minute)`), n = class n {
|
|
7
|
+
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
8
|
+
return new n(t, e);
|
|
9
|
+
}
|
|
10
|
+
constructor({ intervalMilliseconds: e = 5e3, window: n = 48, minBytes: r = 1048576, minCount: i = 16, maxSeries: a = 64, warn: o = t } = {}, s) {
|
|
11
|
+
if (this.samples = 0, this.series = /* @__PURE__ */ new Map(), this.alerts = [], this.listeners = /* @__PURE__ */ new Set(), this.disposed = !1, !Number.isFinite(e) || e < 0) throw RangeError("intervalMilliseconds must be 0 or above");
|
|
12
|
+
if (!Number.isInteger(n) || n < 8 || n > 4096 || n % 4) throw RangeError("window must be 8..4096 and a multiple of 4");
|
|
13
|
+
if (!(r > 0) || !(i > 0)) throw RangeError("minBytes and minCount must be above 0");
|
|
14
|
+
if (!Number.isInteger(a) || a < 1) throw RangeError("maxSeries must be 1 or above");
|
|
15
|
+
this.size = n, this.clock = new Float64Array(n), this.minBytes = r, this.minCount = i, this.maxSeries = a, this.warn = o, this.memory = s, e && (this.interval = setInterval(() => this.sample(), e));
|
|
16
|
+
}
|
|
17
|
+
watch(t, n, { min: r = this.minCount } = {}) {
|
|
18
|
+
if (this.disposed) throw new e("disposed", "LeakWatch is disposed; create a new one to watch a number.");
|
|
19
|
+
if (!t || typeof n != "function") throw TypeError("watch needs a name and a function; pass both.");
|
|
20
|
+
if (!(r > 0)) throw RangeError("min must be above 0");
|
|
21
|
+
if (this.series.has(t)) throw TypeError(`Already watching ${t}; pick another name.`);
|
|
22
|
+
let i = this.add(t, r);
|
|
23
|
+
if (!i) throw RangeError(`maxSeries is reached, so ${t} cannot be followed; raise it or watch fewer labels.`);
|
|
24
|
+
return i.read = n, this;
|
|
25
|
+
}
|
|
26
|
+
add(e, t) {
|
|
27
|
+
if (this.series.size >= this.maxSeries) return;
|
|
28
|
+
let n = {
|
|
29
|
+
values: new Float64Array(this.size),
|
|
30
|
+
count: 0,
|
|
31
|
+
min: t,
|
|
32
|
+
leaking: !1
|
|
33
|
+
};
|
|
34
|
+
return this.series.set(e, n), n;
|
|
35
|
+
}
|
|
36
|
+
record(e, t, n) {
|
|
37
|
+
let r = this.series.get(e) ?? this.add(e, n);
|
|
38
|
+
r && this.push(r, t);
|
|
39
|
+
}
|
|
40
|
+
push(e, t) {
|
|
41
|
+
e.values[this.samples % this.size] = t, e.count++;
|
|
42
|
+
}
|
|
43
|
+
sample(t = performance.now()) {
|
|
44
|
+
if (this.disposed) throw new e("disposed", "LeakWatch is disposed; create a new one to take samples.");
|
|
45
|
+
if (this.clock[this.samples % this.size] = t, this.memory) {
|
|
46
|
+
let { bytes: e, buffers: t, textures: n, labels: r } = this.memory.toJSON();
|
|
47
|
+
this.record("gpu bytes", e, this.minBytes), this.record("buffers", t.count, this.minCount), this.record("textures", n.count, this.minCount);
|
|
48
|
+
for (let e in r) this.record(`label ${e} bytes`, r[e].bytes, this.minBytes), this.record(`label ${e} count`, r[e].count, this.minCount);
|
|
49
|
+
}
|
|
50
|
+
for (let e of this.series.values()) if (e.read) {
|
|
51
|
+
let t = e.read();
|
|
52
|
+
if (!Number.isFinite(t)) throw RangeError("A watched number must be finite; make its read function return one.");
|
|
53
|
+
this.push(e, t);
|
|
54
|
+
}
|
|
55
|
+
this.samples++;
|
|
56
|
+
let n = [];
|
|
57
|
+
for (let [e, r] of this.series) {
|
|
58
|
+
let i = this.judge(e, r, t);
|
|
59
|
+
i && !r.leaking && n.push(i), r.leaking = !!i;
|
|
60
|
+
}
|
|
61
|
+
for (let e of n) this.alerts.push(e), this.alerts.length > 256 && this.alerts.shift(), this.warn?.(e), this.listeners.forEach((t) => t(e));
|
|
62
|
+
return n;
|
|
63
|
+
}
|
|
64
|
+
judge(e, t, n) {
|
|
65
|
+
let { size: r } = this;
|
|
66
|
+
if (t.count < r) return;
|
|
67
|
+
let i = this.samples % r, a = r / 4, o = [
|
|
68
|
+
Infinity,
|
|
69
|
+
Infinity,
|
|
70
|
+
Infinity,
|
|
71
|
+
Infinity
|
|
72
|
+
];
|
|
73
|
+
for (let e = 0; e < r; e++) {
|
|
74
|
+
let n = e / a | 0;
|
|
75
|
+
o[n] = Math.min(o[n], t.values[(i + e) % r]);
|
|
76
|
+
}
|
|
77
|
+
let [s, c, l, u] = o;
|
|
78
|
+
if (!(s < c && c < l && l < u) || u - s < t.min) return;
|
|
79
|
+
let d = (n - this.clock[i]) * 3 / 4;
|
|
80
|
+
return {
|
|
81
|
+
name: e,
|
|
82
|
+
from: s,
|
|
83
|
+
to: u,
|
|
84
|
+
perMinute: d > 0 ? (u - s) / d * 6e4 : 0,
|
|
85
|
+
at: n
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
get leaking() {
|
|
89
|
+
return [...this.series].filter(([, e]) => e.leaking).map(([e]) => e);
|
|
90
|
+
}
|
|
91
|
+
onLeak(e) {
|
|
92
|
+
return this.listeners.add(e), () => {
|
|
93
|
+
this.listeners.delete(e);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
toJSON() {
|
|
97
|
+
return {
|
|
98
|
+
samples: this.samples,
|
|
99
|
+
leaking: this.leaking,
|
|
100
|
+
alerts: this.alerts.map((e) => ({ ...e }))
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
view(e) {
|
|
104
|
+
if (this.row) return this;
|
|
105
|
+
let t = this.row = e.element.ownerDocument.createElement("div"), n = () => {
|
|
106
|
+
let e = this.leaking;
|
|
107
|
+
t.textContent = e.length ? `Leak? ${e.join(", ")}` : "No leaks seen";
|
|
108
|
+
};
|
|
109
|
+
return e.element.append(t), n(), this.timer = setInterval(n, 500), this;
|
|
110
|
+
}
|
|
111
|
+
dispose() {
|
|
112
|
+
this.disposed || (this.disposed = !0, clearInterval(this.interval), clearInterval(this.timer), this.row?.remove(), this.row = void 0, this.listeners.clear());
|
|
113
|
+
}
|
|
114
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
115
|
+
this.dispose();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
//#endregion
|
|
119
|
+
export { n as LeakWatch };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { GLPassTime } from './gl.js';
|
|
2
|
+
/** Internal timestamp owner keeping names and results in the same completed sample. */
|
|
3
|
+
export declare class PanelGPU {
|
|
4
|
+
private readonly complete;
|
|
5
|
+
private readonly error;
|
|
6
|
+
private readonly profiler;
|
|
7
|
+
private readonly tracked;
|
|
8
|
+
private frame;
|
|
9
|
+
private disposed;
|
|
10
|
+
constructor(device: GPUDevice, complete: (milliseconds: number | null, passes: GLPassTime[]) => void, error: (error: unknown) => void);
|
|
11
|
+
get supported(): boolean;
|
|
12
|
+
track(encoder: GPUCommandEncoder): void;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
}
|
package/dist/panels.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin, FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
import { Stats } from './index.js';
|
|
5
|
+
import { type GLPassTime } from './gl.js';
|
|
6
|
+
/** A custom-loop sample. GPU and memory are null when unavailable. */
|
|
7
|
+
export interface PanelSample {
|
|
8
|
+
fps: number;
|
|
9
|
+
cpuMilliseconds: number;
|
|
10
|
+
gpuMilliseconds: number | null;
|
|
11
|
+
memoryBytes: number | null;
|
|
12
|
+
passes?: readonly GLPassTime[];
|
|
13
|
+
}
|
|
14
|
+
/** Panel construction and sampling options. */
|
|
15
|
+
export interface StatsPanelsOptions {
|
|
16
|
+
/** Existing Stats overlay, if provided. Its ownership stays with the caller. */
|
|
17
|
+
stats?: Stats;
|
|
18
|
+
/** Parent for an owned Stats overlay. Default document.body. */
|
|
19
|
+
parent?: HTMLElement;
|
|
20
|
+
/** Fixed history capacity, 2..4096. Default 120. */
|
|
21
|
+
samples?: number;
|
|
22
|
+
/** DOM refresh interval, 16..60000 ms. Default 150. */
|
|
23
|
+
refreshMilliseconds?: number;
|
|
24
|
+
/** Custom memory sampler in bytes. Default tracked GPU resources, or browser JS heap when exposed. */
|
|
25
|
+
memory?: () => number | null;
|
|
26
|
+
}
|
|
27
|
+
/** Oldest-first graph history; missing values are null, never fabricated zeros. */
|
|
28
|
+
export interface StatsPanelsReport {
|
|
29
|
+
fps: (number | null)[];
|
|
30
|
+
cpuMilliseconds: (number | null)[];
|
|
31
|
+
gpuMilliseconds: (number | null)[];
|
|
32
|
+
memoryBytes: (number | null)[];
|
|
33
|
+
passes: GLPassTime[];
|
|
34
|
+
}
|
|
35
|
+
type Metric = 'fps' | 'cpuMilliseconds' | 'gpuMilliseconds' | 'memoryBytes';
|
|
36
|
+
/** FPS, CPU, GPU and memory history with a named per-pass GPU list on both backends. */
|
|
37
|
+
export declare class StatsPanels implements EnginePlugin {
|
|
38
|
+
private readonly options;
|
|
39
|
+
readonly name = "stats-panels";
|
|
40
|
+
readonly stats: Stats;
|
|
41
|
+
readonly element: HTMLDivElement;
|
|
42
|
+
readonly graphs: Readonly<Record<Metric, HTMLCanvasElement>>;
|
|
43
|
+
readonly series: Readonly<Record<Metric, Float64Array>>;
|
|
44
|
+
sampleCount: number;
|
|
45
|
+
sampleIndex: number;
|
|
46
|
+
private readonly labels;
|
|
47
|
+
private readonly contexts;
|
|
48
|
+
private readonly list;
|
|
49
|
+
private readonly ownStats;
|
|
50
|
+
private readonly graphDisplay;
|
|
51
|
+
private readonly timer;
|
|
52
|
+
private engine?;
|
|
53
|
+
private readonly phases;
|
|
54
|
+
private memory?;
|
|
55
|
+
private gpu?;
|
|
56
|
+
private gl?;
|
|
57
|
+
private gpuTime;
|
|
58
|
+
private gpuPasses;
|
|
59
|
+
private latestPasses;
|
|
60
|
+
private queued;
|
|
61
|
+
private disposed;
|
|
62
|
+
private memoryLabel;
|
|
63
|
+
private version;
|
|
64
|
+
private shown;
|
|
65
|
+
constructor(options?: StatsPanelsOptions);
|
|
66
|
+
/** Attach with GPU timestamp support requested when creating a WebGPU engine. */
|
|
67
|
+
static attach(engine: EngineKernel, options?: StatsPanelsOptions): StatsPanels;
|
|
68
|
+
/** Add one validated custom sample to all four fixed-capacity histories. */
|
|
69
|
+
sample(sample: PanelSample): void;
|
|
70
|
+
private readonly collect;
|
|
71
|
+
setup(engine: Engine): void;
|
|
72
|
+
private bindGPU;
|
|
73
|
+
registerPasses(graph: FrameGraph): void;
|
|
74
|
+
onPreUpdate(): void;
|
|
75
|
+
onDeviceRestored(engine: Engine): void;
|
|
76
|
+
/** Refresh DOM text and draw the histories; never called by sample(). */
|
|
77
|
+
refresh(): void;
|
|
78
|
+
/** Copy graph history in chronological order, with named pass results. */
|
|
79
|
+
toJSON(): StatsPanelsReport;
|
|
80
|
+
dispose(): void;
|
|
81
|
+
[Symbol.dispose]: () => void;
|
|
82
|
+
}
|
|
83
|
+
export {};
|