openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
119 lines (118 loc) • 4.07 kB
JavaScript
import { t as boundedJsonUtf8Bytes } from "./json-utf8-bytes-DNwtq2cu.mjs";
import { a as createInspectorProfileCapture, i as captureDiagnosticProfile, l as sanitizeDiagnosticProfileFrame, n as ProfileFailure, o as createNativeCpuProfileCapture, r as assertProfile, t as DIAGNOSTIC_PROFILE_MAX_BYTES } from "./diagnostic-profile-2SXDazNA.mjs";
import { r as observeRuntimeProfileCapability } from "./runtime-capabilities-DcqfMou5.mjs";
//#region src/logging/diagnostic-cpu-profile.ts
const DURATION_MS = 5e3;
const INTERVAL_MICROS = 1e4;
const MAX_NODES = 16384;
const MAX_SAMPLES = 65536;
const SUMMARY_STACK_DEPTH = 8;
const MAX_SUMMARY_ROWS = 128;
function sanitizeDiagnosticCpuProfile(e, t, { startBlockedMs: n }) {
assertProfile(e.samples !== void 0 && e.timeDeltas !== void 0);
if (e.nodes.length > MAX_NODES || e.samples.length > MAX_SAMPLES) throw new ProfileFailure("profile-too-large");
let r = 0;
const i = e.nodes.map((e) => {
const { callFrame: n, redacted: i } = sanitizeDiagnosticProfileFrame(e.callFrame, t);
r += Number(i || e.deoptReason !== void 0);
return {
id: e.id,
callFrame: n,
...e.children !== void 0 ? { children: e.children } : {},
...e.hitCount !== void 0 ? { hitCount: e.hitCount } : {},
...e.positionTicks !== void 0 ? { positionTicks: e.positionTicks } : {},
...e.deoptReason !== void 0 ? { deoptReason: "[redacted]" } : {}
};
});
const a = {
requestedDurationMs: DURATION_MS,
actualDurationMs: (e.endTime - e.startTime) / 1e3,
startBlockedMs: n,
samplingIntervalMicros: INTERVAL_MICROS,
sampleLossCount: null,
redactedNodeCount: r,
profile: {
nodes: i,
startTime: e.startTime,
endTime: e.endTime,
samples: e.samples,
timeDeltas: e.timeDeltas
}
};
if (boundedJsonUtf8Bytes(a, 1048576).complete) return a;
const o = new Map(i.map((e) => [e.id, e]));
const s = /* @__PURE__ */ new Map();
for (const e of i) for (const t of e.children ?? []) s.set(t, e.id);
const c = /* @__PURE__ */ new Map();
for (const t of e.samples) c.set(t, (c.get(t) ?? 0) + 1);
const l = /* @__PURE__ */ new Map();
for (const [e, t] of c) {
const n = [];
let r = o.get(e);
while (r && n.length < SUMMARY_STACK_DEPTH) {
n.push(r.callFrame);
r = o.get(s.get(r.id));
}
let i = 0;
if (!n.some((e) => e.url.startsWith("openclaw:"))) {
let e = 0;
while (r && !r.callFrame.url.startsWith("openclaw:")) {
e++;
r = o.get(s.get(r.id));
}
if (r) {
n.push(r.callFrame);
i = e;
}
}
const a = JSON.stringify({
stack: n,
elidedFrameCount: i
});
const c = l.get(a);
if (c) c.sampleCount += t;
else l.set(a, {
stack: n,
elidedFrameCount: i,
sampleCount: t
});
}
const u = [];
const d = {
...a,
profile: void 0,
truncated: true,
originalNodeCount: i.length,
sampleCount: e.samples.length,
omittedSampleCount: e.samples.length,
summary: u
};
let f = Buffer.byteLength(JSON.stringify(d));
for (const e of [...l.values()].toSorted((e, t) => t.sampleCount - e.sampleCount)) {
const t = Buffer.byteLength(JSON.stringify(e)) + Number(d.summary.length > 0);
if (d.summary.length === MAX_SUMMARY_ROWS || f + t > 1048576) break;
d.summary.push(e);
d.omittedSampleCount -= e.sampleCount;
f += t;
}
return d;
}
/** Captures CPU samples through Node's shared native profiler or Bun's inspector. */
function captureDiagnosticCpuProfile(e) {
return observeRuntimeProfileCapability("cpuProfiling", captureDiagnosticProfile({
...e,
durationMs: DURATION_MS,
create: (e) => process.versions.bun ? createInspectorProfileCapture(e, {
setup: async (e) => {
await e.post("Profiler.enable");
await e.post("Profiler.setSamplingInterval", { interval: INTERVAL_MICROS });
},
start: (e) => e.post("Profiler.start"),
stop: (e) => e.post("Profiler.stop"),
disable: (e) => e.post("Profiler.disable")
}) : createNativeCpuProfileCapture(INTERVAL_MICROS),
sanitize: sanitizeDiagnosticCpuProfile
}));
}
//#endregion
export { sanitizeDiagnosticCpuProfile as n, captureDiagnosticCpuProfile as t };