Files
leleka/engine/strata/apps/host/server/system-telemetry.mjs
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

105 lines
4.8 KiB
JavaScript

/** Host counters. CPU sockets are measured only when their logical CPU IDs are known. */
import os from 'node:os';
import { spawn } from 'node:child_process';
import { createPowerShellLoop, endWithHost } from './windows-poll.mjs';
import { readFile, readdir } from 'node:fs/promises';
import { availableMemoryGb } from './hardware.mjs';
import { probe } from './util.mjs';
export function cpuUtilization(previous, current, ids) {
if (!previous || !ids.length) return null;
let total=0,idle=0;
for (const id of ids) {
const before=previous[id]?.times,after=current[id]?.times;
if(!before||!after)return null;
const elapsed=Object.values(after).reduce((n,v)=>n+v,0)-Object.values(before).reduce((n,v)=>n+v,0);
if(elapsed<0||after.idle<before.idle)return null;
total+=elapsed; idle+=after.idle-before.idle;
}
return total>0?Math.round(Math.max(0,Math.min(100,100*(1-idle/total)))):null;
}
export function diskRate(previous, current, time) {
const elapsed=previous?((current?.at??time)-(previous.counters?.at??previous.time))/1000:0;
if(elapsed<=0||!current||!previous?.counters)return {readMbs:null,writeMbs:null};
const read=current.read-previous.counters.read,write=current.write-previous.counters.write;
return {readMbs:read>=0?read/1024**2/elapsed:null,writeMbs:write>=0?write/1024**2/elapsed:null};
}
export function parseLinuxDiskCounters(raw, devices) {
let read=0,write=0,found=false;
for(const line of raw.trim().split('\n')) {
const fields=line.trim().split(/\s+/);
if(!devices.includes(fields[2])||fields.length<14)continue;
const reads=Number(fields[5]),writes=Number(fields[9]);
if(!Number.isFinite(reads)||!Number.isFinite(writes))continue;
read+=reads*512; write+=writes*512; found=true;
}
return found?{read,write}:null;
}
export function parseMacDiskCounters(raw) {
const reads=[...raw.matchAll(/"Bytes \(Read\)"\s*=\s*(\d+)/g)],writes=[...raw.matchAll(/"Bytes \(Write\)"\s*=\s*(\d+)/g)];
return reads.length&&writes.length?{read:reads.reduce((n,m)=>n+Number(m[1]),0),write:writes.reduce((n,m)=>n+Number(m[1]),0)}:null;
}
export function parseWindowsDiskLine(line) {
const m=/^\s*(\d+)\s+(\d+)\s*$/.exec(String(line));
return m?{read:Number(m[1]),write:Number(m[2])}:null;
}
/** The disk counters, read by one long-lived PowerShell (windows-poll.mjs; #3939). */
export function createWindowsDiskReader({ spawnFn=spawn, intervalSec=4, maxStarts=3, now=Date.now }={}) {
let latest=null;
const script=`while($true){$d=Get-CimInstance Win32_PerfRawData_PerfDisk_PhysicalDisk -Filter "Name='_Total'";if($d){[Console]::Out.WriteLine(([string]$d.DiskReadBytesPersec)+' '+([string]$d.DiskWriteBytesPersec));[Console]::Out.Flush()};Start-Sleep -Seconds ${intervalSec}}`;
const loop=createPowerShellLoop({script,spawnFn,maxStarts,onLine:line=>{
const parsed=parseWindowsDiskLine(line);
if(parsed)latest={...parsed,at:now()};
return !!parsed;
}});
return {
read() { loop.start(); return latest&&now()-latest.at<5*intervalSec*1000?latest:null; },
stop() { loop.stop(); },
get starts() { return loop.starts; },
};
}
let windowsReader=null;
function windowsDisks() {
windowsReader??=endWithHost(createWindowsDiskReader());
return windowsReader.read();
}
async function disks() {
try {
if(process.platform==='linux') {
const [raw,devices]=await Promise.all([readFile('/proc/diskstats','utf8'),readdir('/sys/block')]);
return parseLinuxDiskCounters(raw,devices.filter(d=>!/^(loop|ram|dm-|md|zram)/.test(d)));
}
if(process.platform==='darwin')return parseMacDiskCounters(await probe('ioreg',['-r','-c','IOBlockStorageDriver','-l'],1500));
if(process.platform==='win32') {
return windowsDisks();
}
} catch { /* An unsupported counter stays unavailable. */ }
return null;
}
export function createSystemSampler() {
let previousCpu=null,previousDisk=null,lastRate={readMbs:null,writeMbs:null};
return async processors => {
const current=os.cpus(),time=Date.now();
const cpuLoad=cpuUtilization(previousCpu,current,current.map((_,i)=>i));
const cpus=processors.map(p=>({id:p.id,utilizationPct:cpuUtilization(previousCpu,current,p.logicalIds??(processors.length===1?current.map((_,i)=>i):[]))}));
previousCpu=current;
const [available,counters]=await Promise.all([availableMemoryGb(process.platform),disks()]);
let rate=lastRate;
if(!(counters&&counters.at!==undefined&&previousDisk?.counters?.at===counters.at)) {
rate=diskRate(previousDisk,counters,time);
previousDisk={time,counters};
lastRate=rate;
}
const total=os.totalmem()/1024**3,free=available??os.freemem()/1024**3;
return {cpuLoad,cpus,ramUsedGb:Math.max(0,Math.min(total,total-free)),ramTotalGb:total,diskReadGbs:rate.readMbs===null?null:rate.readMbs/1024,diskWriteMbs:rate.writeMbs};
};
}