openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.69 kB
JavaScript
import{c as parseNativeCpuProfile,l as sanitizeDiagnosticProfileFrame}from"./diagnostic-profile-C4EYsS8Z.mjs";import{performance}from"node:perf_hooks";
//!src/logging/diagnostic-stall-profile.ts
const SAMPLE_MS=25;const MAX_SAMPLES=1024;const MAX_PROFILE_BYTES=1048576;const MAX_REPORT_BYTES=2048;function formatStallFrames(e,t,n,r){const i=[...t].toSorted((e,t)=>t[1]-e[1]).slice(0,8);const a=[...n.values()].toSorted((e,t)=>(t.timeUs??0)-(e.timeUs??0)||t.samples-e.samples).slice(0,10).map(({frame:e,samples:t,timeUs:n,app:r})=>[e,t,n===null?null:Math.round(n/100)/10,r]);const o=[...r.values()].toSorted((e,t)=>(t.timeUs??0)-(e.timeUs??0)||t.samples-e.samples).slice(0,3).map(({samples:e,frames:t})=>[e,t]);let s=false;const render=()=>`${e} inclusive=${JSON.stringify(i)} reportTruncated=${s} self=${JSON.stringify(a)} deepest=${JSON.stringify(o)}`;let c=render();while(Buffer.byteLength(c)>MAX_REPORT_BYTES){s=true;if(i.length)i.pop();else if(o.length>1)o.pop();else if(a.length>1)a.pop();else if((o[0]?.[1].length??0)>3)o[0][1].splice(1,2,`[...]`);else if(a.length)a.pop();else o.pop();c=render()}return c}function formatStallProfile(e,t,n,r=``,i=[]){const a=/* @__PURE__ */ new Map;const o=/* @__PURE__ */ new Map;const s=/* @__PURE__ */ new Map;let c=0;let l=e.length===0;const recordStack=(e,r)=>{const i=/* @__PURE__ */ new Set;const c=[];let l;let u=null;let d=true;for(const t of e){const{callFrame:e,redacted:o}=sanitizeDiagnosticProfileFrame(t,n);if(e.url||e.functionName===`[native]`||e.functionName===`(garbage collector)`){const t=`${e.functionName||`(anonymous)`}@${e.url}:${e.lineNumber+1}`.slice(0,240);if(!i.has(t)){i.add(t);a.set(t,(a.get(t)??0)+r.samples)}c.push(t);if(d)l=t;if(u===null&&e.url.startsWith(`openclaw:`))u=t}else if(d&&o){l=`[redacted]`;c.push(l)}d=false}if(l){const e=u===null&&t.task&&t.task!==`unattributed`?`[async task:${t.task.slice(0,160)}]`:null;if(e){c.push(e);u=e}const n=JSON.stringify([l,u]);const i=o.get(n)??{frame:l,app:u,samples:0,timeUs:0};i.samples+=r.samples;i.timeUs=r.timeUs===null?null:(i.timeUs??0)+r.timeUs;o.set(n,i);const a=c.slice(0,Math.min(16,u?c.indexOf(u)+1:16));if(u&&a.at(-1)!==u)a.push(`[...]`,u);a.reverse();const d=JSON.stringify(a);const f=s.get(d)??{frames:a,samples:0,timeUs:0};f.samples+=r.samples;f.timeUs=r.timeUs===null?null:(f.timeUs??0)+r.timeUs;s.set(d,f)}};for(const n of e){if(n.json===void 0){l=true;continue}const e=parseNativeCpuProfile(n.json);const r=new Map(e.nodes.map(e=>[e.id,e]));const i=/* @__PURE__ */ new Map;for(const t of e.nodes)for(const e of t.children??[])i.set(e,t.id);let a=e.startTime;const o=e.endTime+(t.startedAt-n.endedAt)*1e3;const s=e.endTime+(t.endedAt-n.endedAt)*1e3;l||=(e.samples?.length??0)>=MAX_SAMPLES;const u=/* @__PURE__ */ new Map;for(const[t,n]of(e.samples??[]).entries()){const r=e.timeDeltas?.[t]??0;a+=r;if(a<o||a>s)continue;c++;const i=u.get(n)??{samples:0,timeUs:0};i.samples++;i.timeUs+=Math.min(r,a-o);u.set(n,i)}for(const[e,t]of u){function*callFrames(){let t=e;while(t!==void 0){const e=r.get(t);if(!e)break;yield e.callFrame;t=i.get(t)}}recordStack(callFrames(),t)}}for(const e of i)if(e.at>=t.startedAt&&e.at<=t.endedAt){c++;recordStack(e.frames,{samples:1,timeUs:null})}const u=i.length?`samples=${c} sampleMode=interrupt`:e.length?`samples=${c} intervalMs=${25}`:`cpuProfile=unavailable`;return formatStallFrames(`windowMs=${Math.round(t.endedAt-t.startedAt)} windowStartMs=${Math.round(performance.timeOrigin+t.startedAt)} windowEndMs=${Math.round(performance.timeOrigin+t.endedAt)} reportedAtMs=${Math.round(performance.timeOrigin+performance.now())} ${u} truncated=${l}${r}`,a,o,s)}export{MAX_SAMPLES as n,formatStallProfile as r,MAX_PROFILE_BYTES as t};