import assert from'node:assert/strict';import{inspectPhysicalCheckpoint}from'./physical-checkpoint-proof.mjs';import{inspectProgressSnapshots,progressPhysicalPlan}from'./cpu-watchdog-progress-proof.mjs';import{publicationPhysicalPlan}from'./cpu-watchdog-publication-proof.mjs';import{frameServicePhysicalPlan}from'./cpu-watchdog-frame-proof.mjs';import{asidPhysicalPlan}from'./asid-barrier-proof.mjs';import{tlbPhysicalPlan}from'./tlb-shootdown-proof.mjs';import{affinityPhysicalPlan}from'./cpu-affinity-proof.mjs';import{migrationPhysicalPlan}from'./thread-migration-proof.mjs';import{remoteWakePhysicalPlan}from'./remote-wake-proof.mjs';import{progressGlobals}from'./smp-progress-capture.mjs';import{metricsGlobals,metricsSymbols}from'./lock-metrics-capture.mjs';import{inspectMetricsInputs}from'./cpu-watchdog-metrics-inputs-proof.mjs';import{inspectMetricsModel}from'./lock-metrics-model-proof.mjs'; export{inspectMetricsInputs}from'./cpu-watchdog-metrics-inputs-proof.mjs';const u=(b,p)=>b.readBigUInt64LE(p),W=b=>Array.from({length:b.length/8},(_,i)=>u(b,i*8)),zero=b=>assert.ok(b.every(v=>v===0)); export function metricsPhysicalPlan(data,symbols){const e=W(data.OS_K2090_EVIDENCE);return[...metricsGlobals.map(([name,symbol])=>({name,symbol,val:symbols[symbol].address,size:symbols[symbol].size})),{name:'metricsMeter',val:Number(e[10]),size:Number(e[11])},...progressPhysicalPlan(data,symbols).slice(progressGlobals.length+1)];} export function inspectMetricsSnapshots(base,migration,affinity,tlb,asid,frames,publication,progress,snapshots,memoryMiB,bootId,symbols,compiled,bootDtb,cpus){ assert.equal(snapshots.length,6);const inherited=inspectProgressSnapshots(base,migration,affinity,tlb,asid,frames,publication,progress,memoryMiB,bootId,symbols,compiled,bootDtb,cpus),input=inspectMetricsInputs(compiled,symbols),original=base[3],prior=progress[5],b=W(original.OS_K2045_EVIDENCE);let distributions; const validSp=(sp,cpu)=>assert.ok(sp%16n===0n&&sp>=b[32+cpu]+16n&&sp=4?1:0],[20,index===5?12:0],[21,index===5?6:0],[22,index===5?6:0],[23,index===5?1:0],[24,index===5?3:0]])expected[i]=BigInt(value);assert.deepEqual(e,expected);assert.equal(state.frequency,u(snapshots[0].OS_K2090_EVIDENCE,40)); for(const name of['authority','lifecycle','shared','dtb','kernelRoot','kernelL2','deviceL2','gicL3',...Object.keys(prior).filter(n=>n.startsWith('OS_')&&!['OS_K2045_COMMANDS','OS_K2045_PRIMARY_STACK'].includes(n)),...Object.keys(prior).filter(n=>n.startsWith('thread')||n.startsWith('reservation')||n.startsWith('kernelL3-')||n.startsWith('irq')||n.startsWith('mailbox')||n.startsWith('framePage'))])assert.deepEqual(data[name],prior[name],name+' retained during timing'); assert.deepEqual(W(data.OS_K2090_ACKS),Array(3).fill(BigInt(state.command)));assert.deepEqual(W(data.OS_K2090_READY),Array(3).fill(BigInt(index<2?0:index===2?3:4))); for(let cpu=1;cpu<=3;cpu++){ const n=cpu-1,graph=W(data.OS_K2090_GRAPH.subarray(n*128,cpu*128));assert.deepEqual(graph.slice(0,4),[BigInt(state.command),BigInt(cpu),b[6],0x3c0n]);validSp(graph[4],cpu);assert.ok(graph[5]>0n);if(index)assert.ok(graph[5]>=u(snapshots[index-1].OS_K2090_GRAPH,n*128+40));zero(data.OS_K2090_GRAPH.subarray(n*128+48,cpu*128));if(index===2||index===4)assert.deepEqual(data.OS_K2090_GRAPH,snapshots[index-1].OS_K2090_GRAPH); assert.equal(u(data.OS_K2045_COMMANDS,n*128),BigInt(index===2?102:state.command));zero(data.OS_K2045_COMMANDS.subarray(n*128+8,cpu*128));assert.deepEqual(data['local'+n],prior['local'+n]);const at=Number(u(prior.OS_K2060_GRAPH,n*512)-b[33+n]);assert.deepEqual(data['local'+n],data['executive'+n].subarray(at,at+6144));assert.equal(u(data['executive'+n],0),0x2045455845434c4fn^BigInt(n));assert.equal(u(data['executive'+n],131064),0x2045455845434849n^BigInt(n)); } for(let row=0;row<12;row++){ const sequence=(row/3|0)+1,cpu=row%3+1,raw=data.OS_K2090_RAW.subarray(row*128,(row+1)*128),grant=data.OS_K2090_GRANTS.subarray(row*64,(row+1)*64);if(sequence>state.sequence){zero(raw);zero(grant);continue}const r=W(raw),s=W(data.OS_K2090_SAMPLES.subarray(row*80,(row+1)*80)),g=W(grant),recorded=sequence<=2?1:3; assert.deepEqual(r.slice(0,4),[BigInt(cpu),BigInt(sequence),b[6],0x3c0n]);validSp(r[4],cpu);assert.deepEqual(r.slice(5),[s[7],s[8],s[9],s[6],BigInt(cpu),BigInt(cpu),state.frequency,2090n,1n,0n,0n]);assert.deepEqual(raw,snapshots[recorded].OS_K2090_RAW.subarray(row*128,(row+1)*128));assert.ok(u(data.OS_K2090_GRAPH,(cpu-1)*128+40)>=r[7]); assert.deepEqual(g.slice(0,3),[BigInt(cpu),BigInt(sequence),s[5]]);assert.ok(g[3]>0n&&g[4]>=g[3]&&g[3]<=r[6]);assert.deepEqual(g.slice(5),[0n,0n,b[6]]);if(sequence<=2)assert.ok(g[4]<=r[5]);else{const blocked=W(data.OS_K2090_BLOCKED.subarray(((sequence-3)*3+cpu-1)*64,((sequence-3)*3+cpu)*64));assert.ok(g[3]>=blocked[3]);}assert.deepEqual(grant,snapshots[recorded].OS_K2090_GRANTS.subarray(row*64,(row+1)*64)); } for(let row=0;row<6;row++){ const sequence=(row/3|0)+3,cpu=row%3+1,raw=data.OS_K2090_BLOCKED.subarray(row*64,(row+1)*64);if(index<2||index===2&&sequence===4){zero(raw);continue}const r=W(raw),recorded=sequence===3?2:3;assert.deepEqual(r.slice(0,2),[BigInt(cpu),BigInt(sequence)]);assert.ok(r[2]>0n&&r[3]>=r[2]);assert.deepEqual(r.slice(4,6),[0n,b[6]]);validSp(r[6],cpu);assert.equal(r[7],0x3c0n);assert.deepEqual(raw,snapshots[recorded].OS_K2090_BLOCKED.subarray(row*64,(row+1)*64));const completed=snapshots[3].OS_K2090_SAMPLES;assert.equal(r[2],u(completed,((sequence-1)*3+cpu-1)*80+56));assert.ok(r[3]<=u(completed,((sequence-1)*3+cpu-1)*80+64)); } let previousRelease=0n;for(let sequence=1;sequence<=state.sequence;sequence++)for(const cpu of sequence<=2?[1,2,3]:[3,2,1]){const row=(sequence-1)*3+cpu-1,s=W(data.OS_K2090_SAMPLES.subarray(row*80,(row+1)*80)),g=W(data.OS_K2090_GRANTS.subarray(row*64,(row+1)*64));assert.ok(g[3]>=previousRelease);previousRelease=s[9];} assert.deepEqual(data.OS_K2090_MODEL_LAYOUT,compiled.OS_K2090_MODEL_LAYOUT);distributions=state.groups; } return{...inherited,physicalCheckpoints:72,metricsCheckpoints:6,metricsPrivateMeterViews:6,metricsSamples:12,metricsUncontendedSamples:6,metricsContendedSamples:6,metricsGroups:6,metricsSamplesPerGroup:2,metricsCombinedRefusals:1,metricsWaitingCpus:3,metricsSeparateWaitAndHold:true,metricsRawCounterBoundariesRetained:true,metricsRejectedInputPreservesState:true,metricsOwnershipIntervalsDisjoint:true,metricsAuthorityUnchanged:true,metricsCompiledModelBytes:input.metricsCompiledModelBytes,metricsCompiledMeasurementBytes:input.metricsCompiledMeasurementBytes,metricsCompiledCodeBytes:input.metricsCompiledCodeBytes,lockMetricsAccepted:true,latencyBoundAccepted:false,metricsFrequencyTicksPerSecond:String(u(snapshots[5].OS_K2090_EVIDENCE,40)),metricsDistributions:distributions,productAccepted:false}; } export function inspectMetricsEvidence(p){const{captures,migration,affinity,tlb,asid,frameService,publication,progress,metrics,symbols,qmp,timeline,wholeRsp,readArtifact}=p;assert.equal(captures.length,9);assert.equal(migration.length,5);assert.equal(affinity.length,6);assert.equal(tlb.length,9);assert.equal(asid.length,11);assert.equal(frameService.length,12);assert.equal(publication.length,8);assert.equal(progress.length,6);assert.equal(metrics.length,6);const base=[],mig=[],aff=[],shoot=[],tags=[],frames=[],pub=[],workers=[],measurements=[];let lastQmp=-1,lastRsp=-1;for(const c of[...captures.slice(0,4),...migration,...affinity,...tlb,...asid,...frameService,...publication,...progress,...metrics,...captures.slice(4)]){const data=Object.fromEntries(Object.entries(c.dumps).map(([n,a])=>[n,readArtifact(a)]));let target,expected,gate;if(c.stage>=100){target=measurements;expected=metricsPhysicalPlan(data,symbols);gate='OS_K2090_CAPTURE_GATE'}else if(c.stage>=90){target=workers;expected=progressPhysicalPlan(data,symbols);gate='OS_K2085_CAPTURE_GATE'}else if(c.stage>=80){target=pub;expected=publicationPhysicalPlan(data,symbols);gate='OS_K2080_CAPTURE_GATE'}else if(c.stage>=60){target=frames;expected=frameServicePhysicalPlan(data,symbols);gate='OS_K2075_CAPTURE_GATE'}else if(c.stage>=40){target=tags;expected=asidPhysicalPlan(data,symbols);gate='OS_K2070_CAPTURE_GATE'}else if(c.stage>=30){target=shoot;expected=tlbPhysicalPlan(data,symbols);gate='OS_K2065_CAPTURE_GATE'}else if(c.stage>=20){target=aff;expected=affinityPhysicalPlan(data,symbols);gate='OS_K2060_CAPTURE_GATE'}else if(c.stage>=10){target=mig;expected=migrationPhysicalPlan(data,symbols);gate='OS_K2055_CAPTURE_GATE'}else{target=base;expected=remoteWakePhysicalPlan(data,symbols);gate='OS_K2045_CAPTURE_GATE'}const stage=20900+c.stage,snapshot=inspectPhysicalCheckpoint({capture:c,expected,stage,qmp,timeline,wholeRsp,readArtifact,gateAddress:symbols[gate].address}),steps=timeline.filter(r=>r.stage===stage),start=qmp.findIndex(r=>r.direction==='tx'&&r.message.id===steps[0].id),end=qmp.findIndex(r=>r.direction==='rx'&&r.message.id===steps.at(-1).id);assert.ok(start>lastQmp);lastQmp=end;const rsp=c.rsp.filter(r=>r.id!==null),rs=wholeRsp.findIndex(r=>r.id===rsp[0].id&&r.direction==='tx'),re=wholeRsp.findIndex(r=>r.id===rsp.at(-1).id&&r.direction==='rx');assert.ok(rs>lastRsp);lastRsp=re;target.push(snapshot);}return inspectMetricsSnapshots(base,mig,aff,shoot,tags,frames,pub,workers,measurements,p.memoryMiB,p.bootId,symbols,p.compiled,p.bootDtb,p.cpus);}