mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-16 11:19:33 +01:00
fix(host-sensors): one shared Windows sampler, guard late rows, address review
This commit is contained in:
1 parent
e1d6471808
commit
ce7d198b91
9 files changed
+186
-54
No files matched your search
+75
-41
@@ -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<number | undefined> | undefined;
|
||||
|
||||
/** Windows' installed RAM, asked once (PowerShell takes about a second) and remembered when it answers. */
|
||||
function readWindowsRamMB(): Promise<number | undefined> {
|
||||
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<WindowsCounters> {
|
||||
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<ReturnType<typeof createCounterStream>, '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<typeof setInterval> | 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<GpuReading | undefined> {
|
||||
@@ -374,14 +398,18 @@ async function readAmdGpu(): Promise<GpuReading | undefined> {
|
||||
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<GpuReading | undefined> {
|
||||
/** NVIDIA, then AMD: the readers that need nothing from Windows. */
|
||||
async function readNativeGpu(): Promise<GpuReading | undefined> {
|
||||
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<GpuReading | undefined> {
|
||||
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<number
|
||||
return info ? parseCpuInfoMhz(info) : undefined;
|
||||
}
|
||||
|
||||
let cache: { at: number; key: string; value: HostSensors } | undefined;
|
||||
let inFlight: { key: string; promise: Promise<HostSensors> } | undefined;
|
||||
const cache = new Map<string, { at: number; value: HostSensors }>();
|
||||
const inFlight = new Map<string, Promise<HostSensors>>();
|
||||
|
||||
/** Read only what was asked for. Missing sensors are simply absent from the result. */
|
||||
export async function readHostSensors(request: SensorRequest): Promise<HostSensors> {
|
||||
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;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -2122,7 +2122,7 @@
|
||||
<label class="set-chip" data-preview="header" data-preview-order="1" data-preview-text="A+"><input type="checkbox" id="appSettingsShowFontControls"><svg class="set-chip-ico" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 20 10 5l6 15"/><path d="M6.5 15h7"/><path d="M18 12h4M20 10v4"/></svg><span>Font Size</span></label>
|
||||
<label class="set-chip" data-preview="header" data-preview-order="2" data-preview-text="CPU 12%"><input type="checkbox" id="appSettingsShowSystemStats" checked><svg class="set-chip-ico" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 12h4l2.5-7 4 14L16 12h5"/></svg><span>System Stats</span></label>
|
||||
<label class="set-chip" data-preview="header" data-preview-order="2" data-preview-text="GPU 8%"><input type="checkbox" id="appSettingsShowGpuStats"><svg class="set-chip-ico" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="7" width="18" height="10" rx="2"/><path d="M7 21v-4M12 21v-4M17 21v-4M7 7V3M17 7V3"/></svg><span>GPU Stats</span></label>
|
||||
<label class="set-chip" data-preview="header" data-preview-order="2" data-preview-text="3.4 GHz"><input type="checkbox" id="appSettingsShowCpuMhz"><svg class="set-chip-ico" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg><span>CPU MHz</span></label>
|
||||
<label class="set-chip" data-preview="header" data-preview-order="2" data-preview-text="3400 MHz"><input type="checkbox" id="appSettingsShowCpuMhz"><svg class="set-chip-ico" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg><span>CPU MHz</span></label>
|
||||
<label class="set-chip" data-preview="header" data-preview-order="3"><input type="checkbox" id="appSettingsShowRedrawButton"><svg class="set-chip-ico" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="1 4 1 10 7 10"/><polyline points="23 20 23 14 17 14"/><path d="M20.49 9A9 9 0 0 0 5.64 5.64L1 10m22 4l-4.64 4.36A9 9 0 0 1 3.51 15"/></svg><span>Redraw Terminal</span></label>
|
||||
<label class="set-chip" data-preview="header" data-preview-order="4"><input type="checkbox" id="appSettingsShowResponseViewer"><svg class="set-chip-ico" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg><span>Response Viewer</span></label>
|
||||
<label class="set-chip" data-preview="header" data-preview-order="5"><input type="checkbox" id="appSettingsShowAwayDigestButton"><svg class="set-chip-ico" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M8 6h13"/><path d="M8 12h13"/><path d="M8 18h13"/><path d="M3 6h.01"/><path d="M3 12h.01"/><path d="M3 18h.01"/></svg><span>Away Digest</span></label>
|
||||
|
||||
@@ -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`;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user