diff --git a/src/host-sensors.ts b/src/host-sensors.ts index 07f9263c..23af12df 100644 --- a/src/host-sensors.ts +++ b/src/host-sensors.ts @@ -180,23 +180,22 @@ const readNumber = async (path: string) => { }; /** Linux running under WSL: Windows programs can be started, and the GPU belongs to Windows. */ +let wsl: boolean | undefined; function inWsl(): boolean { - return ( + wsl ??= process.platform === 'linux' && (Boolean(process.env.WSL_INTEROP) || existsSync('/proc/sys/fs/binfmt_misc/WSLInterop') || - existsSync('/proc/sys/fs/binfmt_misc/WSLInterop-late')) - ); + existsSync('/proc/sys/fs/binfmt_misc/WSLInterop-late')); + return wsl; } -const typeperfPath = () => (existsSync(TYPEPERF) ? TYPEPERF : 'typeperf.exe'); - let windowsRamMB: Promise | undefined; /** Windows' installed RAM, asked once (PowerShell takes about a second) and remembered when it answers. */ function readWindowsRamMB(): Promise { windowsRamMB ??= run( - existsSync(POWERSHELL) ? POWERSHELL : 'powershell.exe', + POWERSHELL, ['-NoProfile', '-NonInteractive', '-Command', '(Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory'], 8000 ).then((out) => { @@ -240,14 +239,12 @@ export function createCounterStream(options: CounterStreamOptions) { let startedAt = 0; let lastRead = 0; let blockedUntil = 0; - let header: string | undefined; let latest: { at: number; value: WindowsCounters } | undefined; let waiting: Array<() => void> = []; const stop = () => { const old = child; child = undefined; - header = undefined; if (old) { try { old.kill(); @@ -264,6 +261,9 @@ export function createCounterStream(options: CounterStreamOptions) { if (nextKey !== key) latest = undefined; key = nextKey; startedAt = now(); + // The header row and the partial line belong to this process alone: a late row from a replaced sampler + // must never become the next one's header. + let header: string | undefined; let buffer = ''; let sampled = false; let started: SamplerChild; @@ -275,6 +275,7 @@ export function createCounterStream(options: CounterStreamOptions) { } child = started; started.stdout?.on('data', (chunk: Buffer | string) => { + if (child !== started) return; buffer += chunk.toString(); let at: number; while ((at = buffer.indexOf('\n')) >= 0) { @@ -332,24 +333,47 @@ export function createCounterStream(options: CounterStreamOptions) { }; } -const windowsCounters = createCounterStream({ - start: (counters) => - spawn(typeperfPath(), ['-si', '2', ...counters], { stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true }), -}); -setInterval(() => windowsCounters.sweep(), 10000).unref(); +const GPU_COUNTERS = [ + '\\GPU Engine(*)\\Utilization Percentage', + '\\GPU Adapter Memory(*)\\Shared Usage', + '\\GPU Adapter Memory(*)\\Dedicated Usage', +]; +const CPU_COUNTERS = ['\\Processor Information(_Total)\\Processor Frequency']; -async function readWindowsCounters(request: SensorRequest): Promise { - const counters: string[] = []; - if (request.gpu) { - counters.push( - '\\GPU Engine(*)\\Utilization Percentage', - '\\GPU Adapter Memory(*)\\Shared Usage', - '\\GPU Adapter Memory(*)\\Dedicated Usage' - ); - } - if (request.cpuMhz) counters.push('\\Processor Information(_Total)\\Processor Frequency'); - if (!counters.length) return {}; - return (await windowsCounters.read(counters)) ?? {}; +/** + * The counters one sampler reads. They do not depend on which reading a browser asked for (those are per + * device, so two browsers can differ): the CPU clock is always sampled, and the GPU counters are added, once + * and for good, on a machine with no native GPU reader. A request then only picks its fields from the sample. + */ +export function windowsCounterList(needGpu: boolean): string[] { + return needGpu ? [...GPU_COUNTERS, ...CPU_COUNTERS] : [...CPU_COUNTERS]; +} + +export function createWindowsSource(stream: Pick, 'read' | 'stop'>) { + let needGpu = false; + return { + /** A GPU reading is wanted and no native reader answered: sample the GPU counters too. */ + needGpuCounters() { + needGpu = true; + }, + read: () => stream.read(windowsCounterList(needGpu)), + stop: () => stream.stop(), + }; +} + +const windowsCounters = createCounterStream({ + // Spawn the Windows program directly: if it is missing, the error lands in the stream's back-off. + start: (counters) => + spawn(TYPEPERF, ['-si', '2', ...counters], { stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true }), +}); +const windowsSource = createWindowsSource(windowsCounters); +let sweepTimer: ReturnType | undefined; + +/** Stop the background sampler and its idle sweep (the server calls this on shutdown). */ +export function stopHostSensors(): void { + if (sweepTimer) clearInterval(sweepTimer); + sweepTimer = undefined; + windowsSource.stop(); } async function readAmdGpu(): Promise { @@ -374,14 +398,18 @@ async function readAmdGpu(): Promise { return undefined; } -/** NVIDIA, then AMD; the Windows counters (WSL only) are the slower last resort and only for load and memory. */ -async function readGpu(windows: WindowsCounters | undefined): Promise { +/** NVIDIA, then AMD: the readers that need nothing from Windows. */ +async function readNativeGpu(): Promise { const out = await run('nvidia-smi', [ '--query-gpu=utilization.gpu,memory.used,memory.total,clocks.gr', '--format=csv,noheader,nounits', ]); - const native = (out && parseNvidiaSmi(out)) || (await readAmdGpu()); - if (native || !windows?.gpu) return native; + return (out && parseNvidiaSmi(out)) || readAmdGpu(); +} + +/** A GPU reading from the Windows counters (WSL only): load and memory in use, but no clock. */ +async function gpuFromWindows(windows: WindowsCounters | undefined): Promise { + if (!windows?.gpu) return undefined; const { percent, memUsedMB, dedicated } = windows.gpu; const reading: GpuReading = { percent, memUsedMB }; // An integrated GPU draws its memory from system RAM, and Windows lets it use up to half of it. @@ -404,29 +432,35 @@ async function readCpuMhz(windows: WindowsCounters | undefined): Promise } | undefined; +const cache = new Map(); +const inFlight = new Map>(); /** Read only what was asked for. Missing sensors are simply absent from the result. */ export async function readHostSensors(request: SensorRequest): Promise { const key = `${request.gpu ? 'g' : ''}${request.cpuMhz ? 'c' : ''}`; if (!key) return {}; - if (cache && cache.key === key && Date.now() - cache.at < CACHE_MS) return cache.value; - if (inFlight?.key === key) return inFlight.promise; + sweepTimer ??= setInterval(() => windowsCounters.sweep(), 10000).unref(); + const hit = cache.get(key); + if (hit && Date.now() - hit.at < CACHE_MS) return hit.value; + const running = inFlight.get(key); + if (running) return running; const promise = (async () => { - const windows = inWsl() ? await readWindowsCounters(request) : undefined; + // Native GPU readers first: a machine with a working nvidia-smi never starts the GPU counters. + const nativeGpu = request.gpu ? await readNativeGpu() : undefined; + const wsl = inWsl(); + const windowsGpu = wsl && request.gpu && !nativeGpu; + if (windowsGpu) windowsSource.needGpuCounters(); + const windows = wsl && (request.cpuMhz || windowsGpu) ? await windowsSource.read() : undefined; const [gpu, cpuMhz] = await Promise.all([ - request.gpu ? readGpu(windows) : undefined, - request.cpuMhz ? readCpuMhz(windows) : undefined, + request.gpu ? (nativeGpu ?? gpuFromWindows(windows)) : undefined, + request.cpuMhz ? readCpuMhz(wsl ? (windows ?? {}) : undefined) : undefined, ]); const value: HostSensors = {}; if (gpu) value.gpu = gpu; if (cpuMhz) value.cpuMhz = cpuMhz; - cache = { at: Date.now(), key, value }; + cache.set(key, { at: Date.now(), value }); return value; - })().finally(() => { - inFlight = undefined; - }); - inFlight = { key, promise }; + })().finally(() => inFlight.delete(key)); + inFlight.set(key, promise); return promise; } diff --git a/src/web/public/app.js b/src/web/public/app.js index 09ed42af..ed6ebbc2 100644 --- a/src/web/public/app.js +++ b/src/web/public/app.js @@ -2402,8 +2402,6 @@ class CodemanApp { if (data.parentSessionId) this.markConnectionLineEntering?.('lineage:' + data.id); this.renderSessionTabs(); this.updateCost(); - // Start stats polling when first session appears - if (this.sessions.size === 1) this.startSystemStatsPolling(); if (this._urlSessionId === data.id) this._selectUrlSession(); } diff --git a/src/web/public/index.html b/src/web/public/index.html index c4b16c17..a6bc3859 100644 --- a/src/web/public/index.html +++ b/src/web/public/index.html @@ -2122,7 +2122,7 @@ - + diff --git a/src/web/public/panels-ui.js b/src/web/public/panels-ui.js index 7c301089..6a852b6a 100644 --- a/src/web/public/panels-ui.js +++ b/src/web/public/panels-ui.js @@ -6757,8 +6757,6 @@ Object.assign(CodemanApp.prototype, { this._setStatRing('statCpuRing', stats.cpu); this._setStatRing('statMemRing', stats.memory?.percent); - this._updateHostSensors(stats); - if (memEl && memBar) { const memGB = (stats.memory.usedMB / 1024).toFixed(1); memEl.textContent = `${memGB}G`; @@ -6774,6 +6772,9 @@ Object.assign(CodemanApp.prototype, { memBar.classList.add('medium'); } } + + // After the MEM block, so nothing in the optional readings can stop MEM updating. + this._updateHostSensors(stats); }, /** @@ -6810,16 +6811,19 @@ Object.assign(CodemanApp.prototype, { fill('statVram', 'statVramRing', 'statVramBar', gpu.memPercent, used); if (ring) ring.style.display = ''; } else { - this.$('statVram').textContent = used; + const vram = this.$('statVram'); + if (vram) vram.textContent = used; if (ring) ring.style.display = 'none'; } } // The GPU clock is a third GPU Stats item (the host may not report one: Windows has none for Intel). show('statGpuMhzItem', Boolean(gpu && gpu.mhz !== undefined)); - if (gpu && gpu.mhz !== undefined) this.$('statGpuMhz').textContent = `${gpu.mhz} MHz`; + const gpuMhzEl = gpu && gpu.mhz !== undefined ? this.$('statGpuMhz') : null; + if (gpuMhzEl) gpuMhzEl.textContent = `${gpu.mhz} MHz`; const cpuMhz = settings.showCpuMhz === true && stats.cpuMhz !== undefined ? stats.cpuMhz : undefined; show('statCpuMhzItem', cpuMhz !== undefined); - if (cpuMhz !== undefined) this.$('statCpuMhz').textContent = `${cpuMhz} MHz`; + const cpuMhzEl = cpuMhz !== undefined ? this.$('statCpuMhz') : null; + if (cpuMhzEl) cpuMhzEl.textContent = `${cpuMhz} MHz`; }, /** diff --git a/src/web/public/settings-ui.js b/src/web/public/settings-ui.js index 0d540451..84874d37 100644 --- a/src/web/public/settings-ui.js +++ b/src/web/public/settings-ui.js @@ -3239,6 +3239,9 @@ Object.assign(CodemanApp.prototype, { showCronButton: _crb, // Per-device bottom-bar indicator, absent from SettingsUpdateSchema (.strict()): it must not reach the PUT. showGitStatus: _sgs, + // Opt-in header readings (GPU Stats, CPU MHz): per-device and absent from the .strict() schema. + showGpuStats: _sgp, + showCpuMhz: _scm, gitStatusTree: _gst, gitStatusMaxRepos: _gsm, gitStatusTimeoutSeconds: _gst2, diff --git a/src/web/schemas.ts b/src/web/schemas.ts index fac445ba..86aa3f25 100644 --- a/src/web/schemas.ts +++ b/src/web/schemas.ts @@ -1619,9 +1619,6 @@ export const SettingsUpdateSchema = z // UI visibility showFontControls: z.boolean().optional(), showSystemStats: z.boolean().optional(), - /** Opt-in host readings after CPU and MEM in the header (default off): GPU load/memory/clock (showGpuStats) and the CPU clock (showCpuMhz). */ - showGpuStats: z.boolean().optional(), - showCpuMhz: z.boolean().optional(), /** * How the header draws its WS / CPU / MEM / plan-usage cluster. Display key * (per-device), desktop only (the cluster is hidden below 768px). diff --git a/src/web/server.ts b/src/web/server.ts index b3f537e9..fa172289 100644 --- a/src/web/server.ts +++ b/src/web/server.ts @@ -45,6 +45,7 @@ import { looksLikeHostReboot, newestPersistedActivity, planRebootRestore } from import { rebootRestoreRegistry } from './reboot-restore-registry.js'; import { dataPath, getDataDir, CODEMAN_INSTANCE } from '../config/instance.js'; import { WebhookNotifier, readWebhookConfig } from '../webhook-notify.js'; +import { stopHostSensors } from '../host-sensors.js'; import type { WebhookUrgency } from '../types/push.js'; import { webviewFetch } from './webview-egress.js'; import { readRemoteHosts, rehydrateRemoteHostFields } from '../remote-hosts.js'; @@ -4173,6 +4174,9 @@ export class WebServer extends EventEmitter { // got wrong once. void stopDeepSeekWeb(); + // The header's GPU / CPU clock sampler is a Windows child process under WSL, and its idle sweep a timer. + stopHostSensors(); + // Same teardown rule: the per-endpoint llama-swap log tails are otherwise closed // only by the periodic idle sweep, whose interval is disposed just below. closeAllLlamaSwapLogTails(); diff --git a/test/header-host-sensors-ui.test.ts b/test/header-host-sensors-ui.test.ts index 50f9df22..2e13b479 100644 --- a/test/header-host-sensors-ui.test.ts +++ b/test/header-host-sensors-ui.test.ts @@ -131,7 +131,7 @@ describe('header host sensors', () => { expect(appJs).not.toMatch(/\} else \{\s*this\.stopSystemStatsPolling\(\);/); }); - it('is opt-in in the settings page: unchecked chips, off in every default, accepted by the schema', () => { + it('is opt-in in the settings page: unchecked chips, off in every default, stripped from the settings PUT', () => { const html = read('index.html'); for (const id of ['appSettingsShowGpuStats', 'appSettingsShowCpuMhz']) { expect(html).toMatch(new RegExp(``)); @@ -139,6 +139,9 @@ describe('header host sensors', () => { const settingsUi = read('settings-ui.js'); expect(settingsUi).toContain('showGpuStats ?? defaults.showGpuStats ?? false'); expect(settingsUi).toContain('showCpuMhz ?? defaults.showCpuMhz ?? false'); - expect(readFileSync(join(process.cwd(), 'src/web/schemas.ts'), 'utf-8')).toMatch(/showCpuMhz: z\.boolean\(\)/); + // per-device, so stripped from the PUT body like the other header toggles (the schema is .strict()) + expect(settingsUi).toContain('showGpuStats: _sgp,'); + expect(settingsUi).toContain('showCpuMhz: _scm,'); + expect(readFileSync(join(process.cwd(), 'src/web/schemas.ts'), 'utf-8')).not.toContain('showCpuMhz'); }); }); diff --git a/test/host-sensors.test.ts b/test/host-sensors.test.ts index 93b4820a..0053058c 100644 --- a/test/host-sensors.test.ts +++ b/test/host-sensors.test.ts @@ -7,6 +7,8 @@ import { PassThrough } from 'node:stream'; import { describe, expect, it } from 'vitest'; import { createCounterStream, + createWindowsSource, + windowsCounterList, meanKhzAsMhz, parseCpuInfoMhz, parseDpmClock, @@ -174,3 +176,90 @@ describe('createCounterStream', () => { expect(await stream.read(counters)).toBeUndefined(); }); }); + +describe('the Windows sampler shared by every browser', () => { + const HEADER = '"(PDH-CSV 4.0)","\\\\H\\Processor Information(_Total)\\Processor Frequency"\r\n'; + + function rig() { + const children: Array<{ stdout: PassThrough; killed: boolean; counters: string[] }> = []; + const stream = createCounterStream({ + start: (counters) => { + const stdout = new PassThrough(); + const child = { stdout, killed: false, counters }; + children.push(child); + return { + stdout, + kill: () => { + child.killed = true; + return true; + }, + once: () => undefined, + } as never; + }, + firstSampleMs: 20, + }); + return { children, source: createWindowsSource(stream) }; + } + + it('samples the same counters whatever a browser asked for, so two browsers share one process', async () => { + const { children, source } = rig(); + // browser A (GPU Stats) and browser B (GPU Stats and CPU MHz) read the same sampler in turn + for (let i = 0; i < 8; i++) { + const read = source.read(); + if (i === 0) children[0].stdout.write(HEADER + '"t","1700"\r\n'); + await read; + } + expect(children).toHaveLength(1); + expect(children[0].killed).toBe(false); + }); + + it('adds the GPU counters once, for good, on a machine with no native GPU reader', async () => { + const { children, source } = rig(); + void source.read(); + expect(children[0].counters).toEqual(windowsCounterList(false)); + expect(children[0].counters.some((c) => c.includes('GPU Engine'))).toBe(false); + source.needGpuCounters(); + void source.read(); + expect(children).toHaveLength(2); + expect(children[0].killed).toBe(true); + expect(children[1].counters).toEqual(windowsCounterList(true)); + source.needGpuCounters(); + void source.read(); + void source.read(); + expect(children).toHaveLength(2); + }); +}); + +describe('a late row from a replaced sampler', () => { + it("is ignored, so it can never become the new sampler's header", async () => { + const children: PassThrough[] = []; + let clock = 1000; + const stream = createCounterStream({ + start: () => { + const stdout = new PassThrough(); + children.push(stdout); + return { stdout, kill: () => true, once: () => undefined } as never; + }, + now: () => clock, + firstSampleMs: 20, + recycleMs: 30000, + }); + const counters = ['\\Processor Information(_Total)\\Processor Frequency']; + const header = '"(PDH-CSV 4.0)","\\\\H\\Processor Information(_Total)\\Processor Frequency"\r\n'; + const first = stream.read(counters); + children[0].write(header + '"t","1700"\r\n'); + await first; + clock += 31000; + const second = stream.read(counters); // replaces the sampler + expect(children).toHaveLength(2); + // the killed process still had a data row in its pipe: it arrives after the replacement began + children[0].write('"t","999"\r\n'); + await new Promise((r) => setImmediate(r)); + // the stale row is not a sample: with nothing from the new process yet there is still nothing to hand out + expect(await stream.read(counters)).toBeUndefined(); + children[1].write(header + '"t","1800"\r\n'); + await second; + await new Promise((r) => setImmediate(r)); + expect(await stream.read(counters)).toEqual({ cpuMhz: 1800 }); + }); +});