Windows AMD/Intel cards: real memory size from the DirectX adapter list, load and memory in use from the Windows GPU counters, virtual monitors no longer listed as cards (#3727, #3836)
This commit is contained in:
1 parent
90a7aeea51
commit
4ccfd7ca30
6 files changed
+206
-36
No files matched your search
@@ -1,6 +1,7 @@
|
||||
/** Physical topology and GPU sensors; unavailable values stay null. */
|
||||
import { readFile, readdir, realpath } from 'node:fs/promises';
|
||||
import { probe, round } from './util.mjs';
|
||||
import { windowsGpuReading } from './windows-gpu.mjs';
|
||||
|
||||
/** Unified GPUs share one system memory pool; dedicated GPUs each have their own. */
|
||||
export function gpuMemoryTotals(devices, readings) {
|
||||
@@ -83,6 +84,10 @@ export async function readGpuTelemetry(gpus) {
|
||||
return Promise.all(gpus.map(async gpu=>{
|
||||
let reading=nvidia.get(gpu.id)??{id:gpu.id??`${gpu.vendor}:${gpu.index}`,temperatureC:null,utilizationPct:null,memoryUsedGb:null,powerW:null,powerLimitW:null,pcieGen:null,pcieGenMax:null,pcieWidth:null,pcieRxMbs:null};
|
||||
if(gpu.vendor==='nvidia')reading={...reading,pcieRxMbs:transfer.get(gpu.sensorIndex??gpu.index)??null};
|
||||
if(gpu.luid&&!nvidia.has(gpu.id)) {
|
||||
const counters=windowsGpuReading(gpu.luid);
|
||||
if(counters)reading={...reading,utilizationPct:counters.utilizationPct,memoryUsedGb:round(counters.memoryUsedGb,2)};
|
||||
}
|
||||
if(gpu.sensorPath&&!nvidia.has(gpu.id)) {
|
||||
let temperature=null,power=null,powerLimit=null;
|
||||
for(const hwmon of await list(`${gpu.sensorPath}/hwmon`)) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import { CONTEXT_TOKENS, VARIANTS, footprint } from './catalog.mjs';
|
||||
import { MODELS_DIR, diskFreeGb, diskTotalGb } from './store.mjs';
|
||||
import { probe, round } from './util.mjs';
|
||||
import { utf8Script } from './powershell-utf8.mjs';
|
||||
import { DX_ADAPTERS_SCRIPT, parseDxAdapters, windowsOtherGpus } from './windows-gpu.mjs';
|
||||
|
||||
let cached = null;
|
||||
let cachedAt = 0;
|
||||
@@ -187,11 +188,12 @@ async function detectGpus(platform, totalGb) {
|
||||
}
|
||||
if(platform==='win32') {
|
||||
const raw=await probe('powershell.exe',['-NoProfile','-NonInteractive','-Command',utf8Script('Get-CimInstance Win32_VideoController | Select-Object Name,PNPDeviceID | ConvertTo-Json -Compress')]);
|
||||
try {const parsed=JSON.parse(raw);for(const row of Array.isArray(parsed)?parsed:[parsed]) {
|
||||
if(!row?.Name||(/nvidia/i.test(row.Name)&&gpus.some(g=>g.vendor==='nvidia')))continue;
|
||||
const vendor=/amd|radeon/i.test(row.Name)?'amd':/intel/i.test(row.Name)?'intel':'unknown';
|
||||
gpus.push({id:String(row.PNPDeviceID),index:gpus.length,vendor,name:row.Name,vramGb:0,unified:false,backend:'vulkan'});
|
||||
}}catch{}
|
||||
try {
|
||||
const parsed=JSON.parse(raw);
|
||||
const rows=(Array.isArray(parsed)?parsed:[parsed]).filter(row=>row?.Name&&!(/nvidia/i.test(row.Name)&&gpus.some(g=>g.vendor==='nvidia')));
|
||||
const dx=rows.length?parseDxAdapters(await probe('powershell.exe',['-NoProfile','-NonInteractive','-Command',DX_ADAPTERS_SCRIPT],8000)):[];
|
||||
for(const card of windowsOtherGpus(rows,dx))gpus.push({...card,index:gpus.length,unified:false,backend:'vulkan'});
|
||||
}catch{}
|
||||
}
|
||||
gpus.forEach((g,i)=>{g.index=i;});
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/** 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';
|
||||
@@ -47,45 +48,25 @@ export function parseWindowsDiskLine(line) {
|
||||
return m?{read:Number(m[1]),write:Number(m[2])}:null;
|
||||
}
|
||||
|
||||
/**
|
||||
* One long-lived PowerShell reads the disk counters in a loop. A new PowerShell per sample
|
||||
* paid the cold WMI start every time, missed its timeout and burned WmiPrvSE (#3939).
|
||||
* Gives up after repeated early exits, so a broken counter stays quiet.
|
||||
*/
|
||||
/** 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 child=null,latest=null,starts=0,buffer='';
|
||||
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}}`;
|
||||
function start() {
|
||||
if(child||starts>=maxStarts)return;
|
||||
starts++;
|
||||
let proc;
|
||||
try { proc=spawnFn('powershell.exe',['-NoProfile','-NonInteractive','-Command',script],{windowsHide:true,stdio:['ignore','pipe','ignore']}); }
|
||||
catch { return; }
|
||||
child=proc;
|
||||
proc.on?.('error',()=>{ if(child===proc)child=null; });
|
||||
proc.on?.('exit',()=>{ if(child===proc){child=null;latest=null;} });
|
||||
proc.stdout?.on('data',chunk=>{
|
||||
buffer+=chunk;
|
||||
let i;
|
||||
while((i=buffer.indexOf('\n'))>=0){
|
||||
const parsed=parseWindowsDiskLine(buffer.slice(0,i));
|
||||
buffer=buffer.slice(i+1);
|
||||
if(parsed){latest={...parsed,at:now()};starts=0;}
|
||||
}
|
||||
});
|
||||
proc.unref?.();
|
||||
proc.stdout?.unref?.();
|
||||
}
|
||||
const loop=createPowerShellLoop({script,spawnFn,maxStarts,onLine:line=>{
|
||||
const parsed=parseWindowsDiskLine(line);
|
||||
if(parsed)latest={...parsed,at:now()};
|
||||
return !!parsed;
|
||||
}});
|
||||
return {
|
||||
read() { start(); return latest; },
|
||||
stop() { try{child?.kill();}catch{} child=null; },
|
||||
get starts() { return starts; },
|
||||
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() {
|
||||
if(!windowsReader){windowsReader=createWindowsDiskReader();process.once('exit',()=>windowsReader.stop());}
|
||||
windowsReader??=endWithHost(createWindowsDiskReader());
|
||||
return windowsReader.read();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Windows graphics cards that nvidia-smi does not cover (AMD, Intel): their size and their load.
|
||||
* - size: the DirectX adapter list in the registry (HKLM\SOFTWARE\Microsoft\DirectX) has a 64-bit DedicatedVideoMemory and the adapter
|
||||
* LUID. Win32_VideoController.AdapterRAM is 32 bits and reads 0 or 4 GB for a 16 GB card (#3727, #3836).
|
||||
* - load and memory in use: the Windows GPU performance counters (the ones Task Manager shows), read by one long-lived PowerShell and
|
||||
* matched to the card by LUID. Temperature, power and the PCIe link need the vendor's own library, so they stay "not available".
|
||||
*/
|
||||
import { createPowerShellLoop, endWithHost } from './windows-poll.mjs';
|
||||
import { utf8Script } from './powershell-utf8.mjs';
|
||||
|
||||
export const DX_ADAPTERS_SCRIPT = utf8Script(
|
||||
"Get-ChildItem HKLM:\\SOFTWARE\\Microsoft\\DirectX | ForEach-Object { $p = Get-ItemProperty $_.PSPath; " +
|
||||
"if ($p.Description -and $null -ne $p.AdapterLuid) { [pscustomobject]@{ Description = [string]$p.Description; VendorId = [string]$p.VendorId; " +
|
||||
"Luid = [string]$p.AdapterLuid; Dedicated = [string]$p.DedicatedVideoMemory; Software = [string]$p.SoftwareAdapter } } } | ConvertTo-Json -Compress");
|
||||
|
||||
/** luid_0x<high>_0x<low> in the spelling of the performance counters, from the registry's 64-bit number. */
|
||||
export function luidName(value) {
|
||||
try {
|
||||
const n = BigInt.asUintN(64, BigInt(String(value)));
|
||||
const hex = v => '0x' + v.toString(16).padStart(8, '0');
|
||||
return `luid_${hex(n >> 32n)}_${hex(n & 0xffffffffn)}`;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
/** The adapters the DirectX registry lists: [{ description, vendorId, luid, bytes, software }]. Never throws. */
|
||||
export function parseDxAdapters(raw) {
|
||||
try {
|
||||
const parsed = JSON.parse(raw), rows = Array.isArray(parsed) ? parsed : [parsed];
|
||||
return rows.filter(r => r?.Description).map(r => ({
|
||||
description: String(r.Description), vendorId: Number(r.VendorId) || 0, luid: luidName(r.Luid),
|
||||
bytes: Number(r.Dedicated) || 0, software: String(r.Software) === '1' || String(r.Software).toLowerCase() === 'true',
|
||||
}));
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
const norm = text => String(text ?? '').toLowerCase().replace(/[^a-z0-9]/g, '');
|
||||
const VIRTUAL = /virtual|remote|indirect|mirror|basic render|meta |parsec|displaylink|idd/i;
|
||||
|
||||
/**
|
||||
* Win32_VideoController rows + the DirectX adapter list -> the cards this app can show. A card keeps the size from the registry;
|
||||
* a display driver for a virtual monitor (no size, no known maker) is not a graphics card and is dropped.
|
||||
*/
|
||||
export function windowsOtherGpus(videoRows, dx) {
|
||||
const used = new Set();
|
||||
const out = [];
|
||||
for (const row of videoRows) {
|
||||
if (!row?.Name) continue;
|
||||
const match = dx.find((a, i) => !used.has(i) && !a.software && norm(a.description) === norm(row.Name));
|
||||
if (match) used.add(dx.indexOf(match));
|
||||
const vendor = /amd|radeon/i.test(row.Name) ? 'amd' : /intel/i.test(row.Name) ? 'intel' : 'unknown';
|
||||
const vramGb = match?.bytes ? Math.round(match.bytes / 1024 ** 3 * 10) / 10 : 0;
|
||||
if (vendor === 'unknown' && (!vramGb || VIRTUAL.test(row.Name))) continue;
|
||||
out.push({ id: String(row.PNPDeviceID), vendor, name: row.Name, vramGb, luid: match?.luid ?? null });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** One counter line: "G <luid> <load percent> <dedicated bytes in use>". */
|
||||
export function parseGpuCounterLine(line) {
|
||||
const m = /^G\s+(luid_0x[0-9a-f]+_0x[0-9a-f]+)\s+(\d+(?:\.\d+)?)\s+(\d+)\s*$/i.exec(String(line));
|
||||
return m ? { luid: m[1].toLowerCase(), utilizationPct: Math.max(0, Math.min(100, Number(m[2]))), memoryUsedGb: Number(m[3]) / 1024 ** 3 } : null;
|
||||
}
|
||||
|
||||
export function createWindowsGpuReader({ spawnFn, intervalSec = 4, maxStarts = 3, now = Date.now } = {}) {
|
||||
const readings = new Map();
|
||||
const script = utf8Script(
|
||||
`while($true){` +
|
||||
`$e=@(Get-CimInstance Win32_PerfFormattedData_GPUPerformanceCounters_GPUEngine);$m=@(Get-CimInstance Win32_PerfFormattedData_GPUPerformanceCounters_GPUAdapterMemory);` +
|
||||
`$u=@{};foreach($r in $e){if($r.Name -match '(luid_0x[0-9A-Fa-f]+_0x[0-9A-Fa-f]+)_phys_\\d+_eng_\\d+_engtype_(\\w+)'){$k=$matches[1].ToLower()+'|'+$matches[2];$u[$k]=[double]$u[$k]+[double]$r.UtilizationPercentage}};` +
|
||||
`$b=@{};foreach($k in $u.Keys){$l=$k.Split('|')[0];if(-not $b.ContainsKey($l) -or $u[$k] -gt $b[$l]){$b[$l]=$u[$k]}};` +
|
||||
`foreach($r in $m){if($r.Name -match '(luid_0x[0-9A-Fa-f]+_0x[0-9A-Fa-f]+)'){$l=$matches[1].ToLower();[Console]::Out.WriteLine('G '+$l+' '+[math]::Round([double]$b[$l],0)+' '+$r.DedicatedUsage)}};` +
|
||||
`[Console]::Out.Flush();Start-Sleep -Seconds ${intervalSec}}`);
|
||||
const loop = createPowerShellLoop({ script, spawnFn, maxStarts, onLine: line => {
|
||||
const parsed = parseGpuCounterLine(line);
|
||||
if (parsed) readings.set(parsed.luid, { ...parsed, at: now() });
|
||||
return !!parsed;
|
||||
} });
|
||||
return {
|
||||
read(luid) { loop.start(); const r = readings.get(String(luid ?? '').toLowerCase()); return r && now() - r.at < 6 * intervalSec * 1000 ? r : null; },
|
||||
stop() { loop.stop(); },
|
||||
};
|
||||
}
|
||||
|
||||
let shared = null;
|
||||
/** The card's load and memory in use from the Windows counters; null when there is no reading. */
|
||||
export function windowsGpuReading(luid) {
|
||||
if (process.platform !== 'win32' || !luid) return null;
|
||||
shared ??= endWithHost(createWindowsGpuReader());
|
||||
return shared.read(luid);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* One long-lived PowerShell that prints a line per reading, in a loop. The monitor used to start a cold PowerShell for every sample;
|
||||
* its WMI start cost more than the probe timeout, so every query failed and WmiPrvSE burned a core (#3939).
|
||||
* The loop gives up after `maxStarts` early exits in a row, so a counter that cannot be read stays quiet.
|
||||
*/
|
||||
import { spawn } from 'node:child_process';
|
||||
|
||||
export function createPowerShellLoop({ script, onLine, spawnFn = spawn, maxStarts = 3 }) {
|
||||
let child = null, starts = 0, buffer = '';
|
||||
function start() {
|
||||
if (child || starts >= maxStarts) return;
|
||||
starts++;
|
||||
let proc;
|
||||
try { proc = spawnFn('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], { windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] }); }
|
||||
catch { return; }
|
||||
child = proc;
|
||||
buffer = '';
|
||||
proc.on?.('error', () => { if (child === proc) child = null; });
|
||||
proc.on?.('exit', () => { if (child === proc) child = null; });
|
||||
proc.stdout?.on('data', chunk => {
|
||||
buffer += chunk;
|
||||
let i;
|
||||
while ((i = buffer.indexOf('\n')) >= 0) {
|
||||
const line = buffer.slice(0, i);
|
||||
buffer = buffer.slice(i + 1);
|
||||
if (onLine(line)) starts = 0;
|
||||
}
|
||||
});
|
||||
proc.unref?.();
|
||||
proc.stdout?.unref?.();
|
||||
}
|
||||
return {
|
||||
start,
|
||||
stop() { try { child?.kill(); } catch { /* gone */ } child = null; },
|
||||
get starts() { return starts; },
|
||||
};
|
||||
}
|
||||
|
||||
let stoppers = null;
|
||||
/** Every loop is ended with the host. */
|
||||
export function endWithHost(loop) {
|
||||
if (!stoppers) { stoppers = new Set(); process.once('exit', () => stoppers.forEach(l => l.stop())); }
|
||||
stoppers.add(loop);
|
||||
return loop;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { luidName, parseDxAdapters, windowsOtherGpus, parseGpuCounterLine, createWindowsGpuReader } from '../server/windows-gpu.mjs';
|
||||
|
||||
const dxRaw = JSON.stringify([
|
||||
{ Description: 'AMD Radeon RX 9070 XT', VendorId: '4098', Luid: '90370', Dedicated: '17094934528', Software: '0' },
|
||||
{ Description: 'Microsoft Basic Render Driver', VendorId: '5140', Luid: '1', Dedicated: '0', Software: '1' },
|
||||
]);
|
||||
|
||||
test('the LUID is spelled as the performance counters spell it', () => {
|
||||
assert.equal(luidName('90370'), 'luid_0x00000000_0x00016102');
|
||||
assert.equal(luidName(String(0x12n * 2n ** 32n + 5n)), 'luid_0x00000012_0x00000005');
|
||||
assert.equal(luidName('nope'), null);
|
||||
});
|
||||
|
||||
test('an AMD card on Windows gets its real memory size and a LUID, virtual monitors are not cards (#3727, #3836)', () => {
|
||||
const dx = parseDxAdapters(dxRaw);
|
||||
const cards = windowsOtherGpus([
|
||||
{ Name: 'AMD Radeon RX 9070 XT', PNPDeviceID: 'PCI-A' },
|
||||
{ Name: 'Meta Virtual Monitor', PNPDeviceID: 'ROOT-1' },
|
||||
{ Name: 'Virtual Desktop Monitor', PNPDeviceID: 'ROOT-2' },
|
||||
], dx);
|
||||
assert.equal(cards.length, 1);
|
||||
assert.equal(cards[0].vendor, 'amd');
|
||||
assert.equal(cards[0].vramGb, 15.9);
|
||||
assert.equal(cards[0].luid, 'luid_0x00000000_0x00016102');
|
||||
});
|
||||
|
||||
test('without the registry list an AMD card is still listed, with no size', () => {
|
||||
const cards = windowsOtherGpus([{ Name: 'AMD Radeon RX 7900 XT', PNPDeviceID: 'PCI-B' }], parseDxAdapters('garbage'));
|
||||
assert.deepEqual(cards.map(c => [c.vendor, c.vramGb, c.luid]), [['amd', 0, null]]);
|
||||
});
|
||||
|
||||
test('counter lines are read, and the load and VRAM in use reach the card by its LUID', () => {
|
||||
assert.equal(parseGpuCounterLine('nonsense'), null);
|
||||
const line = parseGpuCounterLine('G luid_0x00000000_0x00016102 37 4294967296');
|
||||
assert.equal(line.utilizationPct, 37);
|
||||
assert.equal(line.memoryUsedGb, 4);
|
||||
const procs = [];
|
||||
const reader = createWindowsGpuReader({ spawnFn: () => { const p = new EventEmitter(); p.stdout = new EventEmitter(); p.kill = () => {}; procs.push(p); return p; }, now: () => 5 });
|
||||
assert.equal(reader.read('luid_0x00000000_0x00016102'), null, 'nothing before the first line');
|
||||
procs[0].stdout.emit('data', 'G luid_0x00000000_0x00016102 37 4294967296\nG luid_0x00000000_0x00000001 0 0\n');
|
||||
assert.equal(reader.read('LUID_0x00000000_0x00016102').utilizationPct, 37);
|
||||
assert.equal(reader.read('luid_0x00000000_0x00099999'), null);
|
||||
assert.equal(procs.length, 1, 'one process for every read');
|
||||
});
|
||||
Reference in new issue
Block a user