File size: 9,257 Bytes
bad179a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | // Windows frame parser against SPEC §5 with hand-computed values. node t_parse_win.js <web-window root>
const path = require('path'), { check, done } = require('./lib');
const plat = require(path.join(process.argv[2], 'core/platform.js'));
const near = (a, b, eps = 1e-6) => Math.abs(a - b) <= eps * Math.max(1, Math.abs(b));
const GB = 2 ** 30;
const frame = (ts, o = {}) => ['@@SNAP_BEGIN', ...(o.info ? ['@@S info', 'hostname=H1', 'os=Win Server', 'kernel=10.0.1', 'arch=64-bit', 'cpumodel=Xeon = fast', 'mhz=2500', 'virt=VM', 'admin=1', '@@S users', '10|DOM\\alice', '20|'] : []),
'@@S os', `ts=${ts}`, `uptime=${o.uptime ?? 1000}`, `memtotal=${16 * GB}`, `swaptotal=${8 * GB}`, `swapfree=${6 * GB}`,
'@@S cpu', '1|30|20|10|2|100', `_Total|${o.cpu ?? 50}|35|15|5|4000`, '10|70|50|20|8|300', '0|50|35|15|5|200', '2|', // core "2" has empty fields
'@@S sys', `queue=${o.queue ?? 2}`, 'ctxt=9000', 'threads=777',
'@@S mem', `avail=${4 * GB}`, `free=${GB}`, `standby=${3 * GB}`, `syscache=${GB}`, `modified=${GB / 2}`, `pool=${GB / 4}`, 'commit=1', 'commitlimit=2', 'pages=3',
'@@S net', `Ethernet|Up|10000|Ethernet|${o.rx ?? 1000}|${o.tx ?? 500}|${o.rxp ?? 10}|20|1|2|Microsoft Hyper-V Network Adapter`, 'Loop|Up|1073|Loopback|5|5|5|5|0|0|Software Loopback',
'vEthernet (WSL)|Down|0|Ethernet|9|9|9|9|0|0|Hyper-V Virtual Ethernet Adapter', 'Tun0|Dormant|0|Tunnel|0|0|0|0|0|0|Teredo', 'Aaa|NotPresent||Wireless80211|0|0|0|0|||',
'@@S tcp', 'Established 40', 'TimeWait 3', 'CloseWait 1', 'Listen 27', 'SynSent 2',
'@@S disk', '0 C:|100|200|3|4|80', '1 D:|10|20|1|2|', '2 E:|1|1|1|1|130', '3 F:|1|1|1|1|-5',
'@@S df', `C:\\|System|NTFS|${100 * GB}|${25 * GB}`, `D:\\|Data|NTFS|${200 * GB}|${180 * GB}`, 'E:\\|Empty|NTFS|0|0',
'@@S procs', '0|0|999999|0|4|0|1|1|System Idle Process', `4|0|${o.p4 ?? 0}|8|100|0|${GB}|${GB / 8}|System`, `10|4|${o.p10 ?? 0}|13|5|${o.born ?? 0}|${2 * GB}|${GB}|sql server.exe`,
...(o.newProc ? ['30|4|50|8|1|0|1|1|new.exe'] : []), `20|4|${o.p20 ?? 0}|6|7|0|1|${GB / 2}|idle.exe`,
'@@S sessions', '3',
...(o.services ? ['@@S services', 'A|Running|Auto|0|Service A', 'B|Stopped|Auto|1067|Service B', 'C|Stopped|Auto|1077|Never started', 'D|Stopped|Manual|5|Manual stopped', 'E|Stopped|Auto||No code', 'F|Start Pending|Auto|0|Starting', 'G|Stopped|Disabled|1|Disabled'] : []),
'@@S thermal', 'TZ00|45.5', 'TZ00|50', 'TZ01|0', 'TZ02|151', 'TZ|03|60',
...(o.gpu ? ['@@S gpu', '0, NVIDIA RTX, 55, 1000, 8000, 60, 120.5, 30'] : []), '@@SNAP_END'].join('\r\n');
const st = {}, T = 1700000000000;
let m = plat.parse(frame(T, { info: true, services: true, gpu: true, p10: 1e7, p20: 0 }), st, 1);
check('first frame: valid=false, load = instant (cpu/100 × cores + queue)', m.valid === false && near(m.load1, 0.5 * 4 + 2) && m.load5 === m.load1 && m.load15 === m.load1, `load ${m.load1}`);
check('info: hostname, os, kernel, arch, cpu model (with "="), virt', m.hostname === 'H1' && m.os === 'Win Server' && m.kernel === '10.0.1' && m.arch === '64-bit' && m.cpuModel === 'Xeon = fast' && m.virt === 'VM');
m = plat.parse(frame(T + 2000, { cpu: 80, queue: 1, rx: 1000 + 4000, tx: 500 + 1000, rxp: 30, p4: 4e7, p10: 1e7 + 1.5e7, p20: 1e5, born: (T + 2000) / 1000 - 600, newProc: true }), st, 2);
const inst = 0.8 * 4 + 1, ema = T_ => 4 + (inst - 4) * (1 - Math.exp(-2 / T_));
check('second frame is valid; cached info kept', m.valid && m.hostname === 'H1' && m.uptime === 1000);
check('cpu: usage, user, irq, system = privileged − irq, interrupts/s, iowait = steal = 0', m.cpuUsage === 80 && m.cpuUser === 35 && m.cpuIrq === 5 && m.cpuSystem === 10 && m.intrPerSec === 4000 && m.cpuIowait === 0 && m.cpuSteal === 0);
check('cores sorted by number, mhz from info, cpuCount', m.cpuCount === 4 && m.cores.map(c => c.usage).join() === '50,30,0,70' && m.cores.every(c => c.mhz === 2500) && m.avgMhz === 2500, m.cores.map(c => c.usage).join());
check('load averages: exponential moving averages with T = 60, 300, 900 s', near(m.load1, ema(60)) && near(m.load5, ema(300)) && near(m.load15, ema(900)), `${m.load1} ${m.load5} ${m.load15}`);
check('procsRunning = round(cpu/100 × cores), procsBlocked = queue, ctxt', m.procsRunning === 3 && m.procsBlocked === 1 && m.ctxtPerSec === 9000);
check('memory: available, free, cached = standby + syscache, slab = pool, dirty = modified, used, percent', m.memTotal === 16 * GB && m.memAvailable === 4 * GB && m.memFree === GB && m.cached === 4 * GB && m.buffers === 0 && m.shared === 0 && m.slab === GB / 4 && m.dirty === GB / 2 && m.memUsed === 12 * GB && m.memPct === 75);
check('swap: used = total − free, percent', m.swapTotal === 8 * GB && m.swapUsed === 2 * GB && m.swapPct === 25);
const eth = m.nets.find(n => n.name === 'Ethernet');
check('net: rates from counter deltas / dt', eth.rxBps === 2000 && eth.txBps === 500 && eth.rxPps === 10 && eth.txPps === 0 && eth.rxBytes === 5000 && eth.errors === 1 && eth.drops === 2 && eth.speed === 10000 && eth.state === 'up');
check('net: virtual = Loopback / Tunnel type or name+description pattern; a Hyper-V VM NIC is physical', m.nets.map(n => `${n.name}:${+n.virt}`).join() === 'Aaa:0,Ethernet:0,Loop:1,Tun0:1,vEthernet (WSL):1', m.nets.map(n => `${n.name}:${+n.virt}:${n.state}`).join());
check('net: totals over physical interfaces only; states lower-cased', m.netRxBps === 2000 && m.netTxBps === 500 && m.nets.find(n => n.name === 'Tun0').state === 'dormant' && m.nets.find(n => n.name === 'vEthernet (WSL)').state === 'down');
check('tcp: names mapped, others upper-cased, socketsUsed = sum', m.tcp.ESTABLISHED === 40 && m.tcp.TIME_WAIT === 3 && m.tcp.CLOSE_WAIT === 1 && m.tcp.LISTEN === 27 && m.tcp.SYNSENT === 2 && m.socketsUsed === 73, JSON.stringify(m.tcp));
check('disk: util = clamp(100 − idle), 0 without idle; sums; busiest', m.disks.map(d => d.util).join() === '20,0,0,100' && m.diskReadBps === 112 && m.diskWriteBps === 222 && m.maxDiskUtil === 100 && m.busiestDisk === '3 F:' && m.disks[0].readIops === 3 && m.disks[0].totalRead === 0, m.disks.map(d => d.util).join());
check('df: device = label, used = total − free, percent, fullest; empty volume skipped', m.mounts.length === 2 && m.mounts[0].device === 'System' && m.mounts[0].used === 75 * GB && m.mounts[0].avail === 25 * GB && m.mounts[0].pct === 75 && m.maxDiskPct === 75 && m.fullestMount === 'C:\\' && m.mounts[0].type === 'NTFS');
const P = Object.fromEntries(m.procs.map(p => [p.pid, p]));
check('procs: pid 0 skipped; cpu % of one core = Δ100ns / 1e7 / dt × 100; clamped to 100 × cores', !(0 in P) && near(P[10].cpu, 75) && near(P[20].cpu, 0.5) && P[4].cpu === 200 || near(P[4].cpu, 200), `${P[4].cpu} ${P[10].cpu} ${P[20].cpu}`);
check('procs: state R above 0.5 %, nice = priority, age = now − created (0 unknown), user, memory %', P[10].state === 'R' && P[20].state === 'S' && P[10].nice === 13 && near(P[10].age, 600) && P[4].age === 0 && P[10].user === 'DOM\\alice' && P[20].user === '—' && P[4].user === '—' && P[10].memPct === 6.25 && P[10].name === 'sql server.exe' && P[10].ppid === 4 && P[10].rss === GB && P[10].vsz === 2 * GB);
check('procs: sorted by cpu then rss; threads total; forks/s = new pids / dt', m.procs.map(p => p.pid).join() === '4,10,20,30' && m.threadsTotal === 113 && m.forksPerSec === 0.5, m.procs.map(p => p.pid).join() + ' forks ' + m.forksPerSec);
const S = Object.fromEntries(m.services.map(s => [s.unit, s]));
check('services (cached list): failed only for Auto + Stopped + exit code not 0 / 1077', 'ABCDEFG'.split('').map(k => S[k].active).join() === 'active,failed,inactive,inactive,inactive,inactive,inactive' && S.A.sub === 'running' && S.F.sub === 'start pending' && S.B.load === 'Auto' && S.B.description === 'Service B' && m.servicesFailed === 1 && m.servicesRunning === 1, 'ABCDEFG'.split('').map(k => S[k].active).join());
check('thermal: 0 < t ≤ 150, unique names (name may contain "|")', m.temps.map(t => `${t.name}=${t.celsius}`).join() === 'TZ00=45.5,TZ|03=60' && m.maxTemp === 60, m.temps.map(t => `${t.name}=${t.celsius}`).join());
check('gpu list cached from the earlier frame; sessions', m.gpus.length === 1 && m.gpus[0].name === 'NVIDIA RTX' && m.gpus[0].util === 55 && m.gpus[0].power === 120.5 && m.loggedUsers === 3);
const before = [...st.load];
m = plat.parse(frame(T + 1000, { cpu: 80, queue: 1 }), st, 3); // the collector clock went back
check('clock set back: no rates, load unchanged, frame not valid', !m.valid && near(m.load1, before[0]) && m.procs.every(p => p.cpu === 0) && m.forksPerSec === 0, `valid ${m.valid} load ${m.load1}`);
m = plat.parse(frame(T + 3000, { uptime: 0 }), st, 4);
check('frame without uptime is ignored', m.uptime === 0 && !m.valid);
m = plat.parse('@@SNAP_BEGIN\n@@S os\nts=' + (T + 9000) + '\nuptime=5\n', st, 5);
check('frame with missing sections does not throw; every missing value is 0', m.valid && m.cpuCount === 1 && m.memPct === 0 && m.procs.length === 0 && m.services.length === 7);
check('protected processes', ['System', 'lsass.exe', 'svchost.exe', 'sshd.exe'].every(name => plat.isProtected({ pid: 500, rss: 1, name })) && plat.isProtected({ pid: 4, rss: 1, name: 'x' }) && plat.isProtected({ pid: 500, rss: 0, name: 'x' }) && !plat.isProtected({ pid: 500, rss: 1, name: 'w3wp.exe' }));
done();
|