openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
209 lines (208 loc) • 5.6 kB
JavaScript
import { t as boundedJsonUtf8Bytes } from "./json-utf8-bytes-DNwtq2cu.mjs";
import { a as createInspectorProfileCapture, i as captureDiagnosticProfile, l as sanitizeDiagnosticProfileFrame, t as DIAGNOSTIC_PROFILE_MAX_BYTES } from "./diagnostic-profile-2SXDazNA.mjs";
import { r as observeRuntimeProfileCapability } from "./runtime-capabilities-DcqfMou5.mjs";
import { setImmediate } from "node:timers/promises";
//#region src/logging/diagnostic-heap-profile.ts
async function sortAllocationSummaries(e) {
const compare = (e, t) => t.totalBytes - e.totalBytes || t.selfBytes - e.selfBytes;
let t = [];
let n = [];
for (const r of e) {
n.push(r);
if (n.length === 2048) {
t.push(n.toSorted(compare));
n = [];
await setImmediate();
}
}
if (n.length) t.push(n.toSorted(compare));
while (t.length > 1) {
const e = [];
for (let n = 0; n < t.length; n += 2) {
const r = t[n];
const i = t[n + 1];
if (!i) {
e.push(r);
continue;
}
const a = [];
let o = 0;
let s = 0;
while (o < r.length || s < i.length) {
a.push(s === i.length || o < r.length && compare(r[o], i[s]) <= 0 ? r[o++] : i[s++]);
if (a.length % 2048 === 0) await setImmediate();
}
e.push(a);
}
t = e;
}
return t[0] ?? [];
}
async function sanitizeDiagnosticHeapProfile(e, t, n) {
const r = e.samples;
const i = /* @__PURE__ */ new Map();
const a = [{ source: e.head }];
let o = 0;
let s = 0;
for (const { source: e, parent: n } of a) {
if (++s % 1024 === 0) await setImmediate();
const { callFrame: r, redacted: c } = sanitizeDiagnosticProfileFrame(e.callFrame, t);
o += Number(c);
const l = {
id: e.id,
callFrame: r,
selfSize: e.selfSize,
children: []
};
const u = {
node: l,
parent: n,
totalBytes: l.selfSize,
count: 0,
frameId: 0
};
i.set(l.id, u);
n?.node.children.push(l);
for (const t of e.children) {
a.push({
source: t,
parent: u
});
if (++s % 1024 === 0) await setImmediate();
}
}
a.length = 0;
const c = [];
let l = 0;
let u = 0;
for (const e of r) {
if (++s % 1024 === 0) await setImmediate();
const t = i.get(e.nodeId);
if (!t) {
l++;
u += e.size;
continue;
}
t.count++;
c.push({
size: e.size,
nodeId: e.nodeId,
ordinal: e.ordinal
});
}
const d = [...i.values()];
i.clear();
for (let e = d.length - 1; e >= 0; e--) {
if (++s % 1024 === 0) await setImmediate();
const t = d[e];
if (t.parent) t.parent.totalBytes += t.totalBytes;
}
const f = {
...n,
redactedNodeCount: o,
unattributedSampleCount: l,
unattributedSampleBytes: u
};
const p = {
...f,
truncated: l > 0,
profile: {
head: d[0].node,
samples: c
}
};
if (boundedJsonUtf8Bytes(p, 1048576).complete) return p;
const m = /* @__PURE__ */ new Map();
const h = /* @__PURE__ */ new Map();
const g = [];
for (const e of d) {
if (++s % 1024 === 0) await setImmediate();
const t = JSON.stringify(e.node.callFrame);
let n = m.get(t);
if (n === void 0) {
n = m.size;
m.set(t, n);
}
e.frameId = n;
const { totalBytes: r, count: i } = e;
if (r === 0) continue;
const a = e.node.selfSize;
const o = [e.node.callFrame];
g.length = 0;
g.push(n);
for (let t = e.parent; t && o.length < 8; t = t.parent) {
o.push(t.node.callFrame);
g.push(t.frameId);
}
const c = g.join(",");
const l = h.get(c);
if (l) {
l.selfBytes += a;
l.totalBytes += r;
l.count += i;
} else h.set(c, {
stack: o,
selfBytes: a,
totalBytes: r,
count: i
});
}
m.clear();
d.length = 0;
const _ = {
...f,
truncated: true,
summary: []
};
let v = Buffer.byteLength(JSON.stringify(_));
for (const e of await sortAllocationSummaries(h.values())) {
if (++s % 1024 === 0) await setImmediate();
const t = Buffer.byteLength(JSON.stringify(e)) + Number(_.summary.length > 0);
if (v + t > 1048576) break;
_.summary.push(e);
v += t;
}
return _;
}
/** Samples allocations in the main isolate without a snapshot or disk output. */
function captureDiagnosticHeapProfile(e) {
const t = Math.max(4096, e.samplingIntervalBytes ?? 32768);
const n = {
includeObjectsCollectedByMajorGC: e.includeObjectsCollectedByMajorGC ?? e.includeCollected ?? false,
includeObjectsCollectedByMinorGC: e.includeObjectsCollectedByMinorGC ?? e.includeCollected ?? false
};
const r = n.includeObjectsCollectedByMajorGC || n.includeObjectsCollectedByMinorGC ? 3e4 : 9e5;
const i = e.durationMs ?? 5e3;
const a = Math.min(r, Math.max(1, i));
return observeRuntimeProfileCapability("heapSampling", captureDiagnosticProfile({
signal: e.signal,
hasAuthority: e.hasAuthority,
durationMs: a,
create: (e) => createInspectorProfileCapture(e, {
setup: (e) => e.post("HeapProfiler.enable"),
start: (e) => e.post("HeapProfiler.startSampling", {
samplingInterval: t,
...n
}),
stop: (e) => e.post("HeapProfiler.stopSampling"),
disable: (e) => e.post("HeapProfiler.disable")
}),
sanitize: (e, o, s) => sanitizeDiagnosticHeapProfile(e, o, {
durationMs: s.durationMs,
requestedDurationMs: i,
effectiveDurationMs: a,
actualDurationMs: s.durationMs,
durationLimitReason: i <= r ? null : r === 3e4 ? "collected-samples" : "maximum-duration",
samplingIntervalBytes: t,
...n,
heapUsedBefore: s.before.heapUsed,
heapUsedAfter: s.after.heapUsed,
heapSpacesBefore: s.before.heapSpaces,
heapSpacesAfter: s.after.heapSpaces,
rssBefore: s.before.rss,
rssAfter: s.after.rss
})
}));
}
//#endregion
export { sanitizeDiagnosticHeapProfile as n, captureDiagnosticHeapProfile as t };