Replace GPU detection with runtime memory info

Remove external GPU detection (no more exec/promisify or detectGpus) and stop returning GPU list from /api/system. Instead expose the Node.js process runtime info (pid, rss, heapTotal, heapUsed, external, arrayBuffers) from the server. Update types to replace gpus with runtime and update the Overview UI: remove GPU list, show PID and formatted runtime memory stats, and adjust imports/icons accordingly. This simplifies system probing (avoids spawning platform commands) and surfaces useful process memory metrics in the WebUI.
This commit is contained in:
手瓜一十雪
2026-05-07 09:39:12 +08:00
parent 3ac4bc79f0
commit f7cfec1a93
3 changed files with 43 additions and 97 deletions
+10 -81
View File
@@ -11,13 +11,9 @@ import path from 'path';
import { existsSync, readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import os from 'os';
import { exec } from 'child_process';
import { promisify } from 'util';
import { WebuiAuth, evaluatePasswordRules, isStrongPassword } from './auth';
import { findAvailablePort } from './port';
const execAsync = promisify(exec);
const log = createLogger('WebUI');
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -269,87 +265,13 @@ export async function initWebUI(
return usage;
}
let cachedGpu: { name: string; vendor: string }[] | null = null;
let gpuFetchedAt = 0;
async function detectGpus(): Promise<{ name: string; vendor: string }[]> {
const now = Date.now();
if (cachedGpu && now - gpuFetchedAt < 60_000) return cachedGpu;
const platform = os.platform();
const result: { name: string; vendor: string }[] = [];
const unique = new Set<string>();
const pushGpu = (name: string, vendor = '') => {
const normalizedName = name.trim();
const normalizedVendor = vendor.trim();
if (!normalizedName) return;
if (normalizedName.toLowerCase() === 'name') return;
const key = `${normalizedName.toLowerCase()}|${normalizedVendor.toLowerCase()}`;
if (unique.has(key)) return;
unique.add(key);
result.push({ name: normalizedName, vendor: normalizedVendor });
};
try {
if (platform === 'win32') {
// Prefer CIM on modern Windows; WMIC is deprecated and may not exist.
const commands = [
'powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command "[Console]::OutputEncoding=[System.Text.Encoding]::UTF8; Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name"',
'wmic path win32_VideoController get Name /value',
];
for (const command of commands) {
try {
const { stdout } = await execAsync(command, { timeout: 5000, windowsHide: true });
for (const line of stdout.split(/\r?\n/)) {
const value = line.includes('=') ? line.split('=').slice(1).join('=') : line;
pushGpu(value);
}
if (result.length > 0) break;
} catch {
// Try next fallback command.
}
}
} else if (platform === 'linux') {
try {
const { stdout } = await execAsync('lspci -mm | grep -Ei "vga|3d|display"', { timeout: 4000 });
for (const line of stdout.split(/\r?\n/)) {
const parts = line.match(/"([^"]+)"\s+"([^"]+)"\s+"([^"]+)"/);
if (parts) pushGpu(parts[3], parts[2]);
}
} catch {
try {
const { stdout } = await execAsync('nvidia-smi --query-gpu=name --format=csv,noheader', { timeout: 4000 });
for (const line of stdout.split(/\r?\n/)) {
pushGpu(line, 'NVIDIA');
}
} catch {
/* ignore */
}
}
} else if (platform === 'darwin') {
const { stdout } = await execAsync('system_profiler SPDisplaysDataType -json', { timeout: 5000 });
try {
const json = JSON.parse(stdout);
const list = json.SPDisplaysDataType ?? [];
for (const item of list) {
if (item._name) pushGpu(item._name, item.spdisplays_vendor ?? '');
}
} catch {
/* ignore */
}
}
} catch {
/* best-effort */
}
cachedGpu = result;
gpuFetchedAt = now;
return result;
}
app.get('/api/system', async (c) => {
app.get('/api/system', (c) => {
const cpus = os.cpus();
const usage = sampleCpuLoad();
const totalMem = os.totalmem();
const freeMem = os.freemem();
const usedMem = totalMem - freeMem;
const gpus = await detectGpus().catch(() => []);
const runtimeMemory = process.memoryUsage();
return c.json({
hostname: os.hostname(),
platform: os.platform(),
@@ -372,7 +294,14 @@ export async function initWebUI(
used: usedMem,
usagePercent: totalMem ? (usedMem / totalMem) * 100 : 0,
},
gpus,
runtime: {
pid: process.pid,
rss: runtimeMemory.rss,
heapTotal: runtimeMemory.heapTotal,
heapUsed: runtimeMemory.heapUsed,
external: runtimeMemory.external,
arrayBuffers: runtimeMemory.arrayBuffers,
},
});
});
@@ -5,7 +5,6 @@ import {
AlertCircle,
CheckCircle2,
Cpu,
HardDrive,
Loader2,
MemoryStick,
MonitorCog,
@@ -224,28 +223,39 @@ export function OverviewPage({
</div>
</div>
{/* GPU */}
{/* Runtime */}
<div className="rounded-lg border bg-card/40 p-4">
<div className="mb-2 flex items-center gap-2">
<HardDrive className="size-4 text-primary" />
<span className="text-sm font-semibold">显示适配器</span>
<div className="mb-2 flex items-center justify-between">
<div className="flex items-center gap-2">
<Server className="size-4 text-primary" />
<span className="text-sm font-semibold">运行进程</span>
</div>
<span className="text-sm font-semibold tabular-nums text-primary">
{systemInfo ? `PID ${systemInfo.runtime.pid}` : '—'}
</span>
</div>
{!systemInfo ? (
<div className="space-y-2">
<Skeleton className="h-4 w-3/4" />
<Skeleton className="h-4 w-1/2" />
</div>
) : systemInfo.gpus.length === 0 ? (
<p className="text-xs text-muted-foreground">未检测到 GPU 信息</p>
) : (
<ul className="space-y-1.5">
{systemInfo.gpus.map((g, i) => (
<li key={i} className="rounded-md bg-background/60 px-2 py-1.5 text-xs">
<div className="truncate font-medium" title={g.name}>{g.name}</div>
{g.vendor && <div className="text-[10px] text-muted-foreground">{g.vendor}</div>}
</li>
))}
</ul>
<div className="space-y-1.5 text-xs">
<div className="flex items-center justify-between rounded-md bg-background/60 px-2 py-1.5">
<span className="text-muted-foreground">RSS</span>
<span className="font-medium tabular-nums">{formatBytes(systemInfo.runtime.rss)}</span>
</div>
<div className="flex items-center justify-between rounded-md bg-background/60 px-2 py-1.5">
<span className="text-muted-foreground">堆内存</span>
<span className="font-medium tabular-nums">
{formatBytes(systemInfo.runtime.heapUsed)} / {formatBytes(systemInfo.runtime.heapTotal)}
</span>
</div>
<div className="flex items-center justify-between rounded-md bg-background/60 px-2 py-1.5">
<span className="text-muted-foreground">外部内存</span>
<span className="font-medium tabular-nums">{formatBytes(systemInfo.runtime.external)}</span>
</div>
</div>
)}
</div>
</CardContent>
+8 -1
View File
@@ -81,7 +81,14 @@ export interface SystemInfo {
used: number;
usagePercent: number;
};
gpus: { name: string; vendor: string }[];
runtime: {
pid: number;
rss: number;
heapTotal: number;
heapUsed: number;
external: number;
arrayBuffers: number;
};
}
export interface LogEntry {