Reduce uploader shutdown stalls and add bounded runtime profiling
CI / verify (push) Canceled after 0s
CI / verify (push) Canceled after 0s
This commit is contained in:
1 parent
80eb878f1b
commit
fe3797fd18
10 files changed
+121
-18
No files matched your search
@@ -359,6 +359,12 @@ class ConfigStore {
|
||||
return out.join('<') || '?';
|
||||
}
|
||||
|
||||
getGlobalSetting(key) {
|
||||
if (!this._cache) this.load();
|
||||
this._scheduleConfigRefresh();
|
||||
return this._clone(this._cache.globalSettings?.[key]);
|
||||
}
|
||||
|
||||
load() {
|
||||
if (!this._perfLog) return this._loadImpl();
|
||||
const hadCache = !!this._cache;
|
||||
|
||||
+3
-1
@@ -16,7 +16,7 @@ function page(items, args) {
|
||||
}
|
||||
|
||||
function createMcpApp({ loadConfig, invoke, invokeGuarded = (_context, channel, ...args) => invoke(channel, ...args), renderer, collectors, getGrants, trashFile, canWrite,
|
||||
getCapabilities, getProtectedPaths, getInstallationId, getDiagnosticSources, getRuntimeDiagnostics = () => null, getPerformance = () => null, getSystemMetrics = () => null, updates }) {
|
||||
getCapabilities, getProtectedPaths, getInstallationId, getDiagnosticSources, getRuntimeDiagnostics = () => null, getPerformance = () => null, getSystemMetrics = () => null, profile, updates }) {
|
||||
const transfers = createMcpTransfers({ getGrants, getCapabilities, getProtectedPaths, getInstallationId });
|
||||
const sanitize = createMcpSanitizer(loadConfig);
|
||||
const guard = context => {
|
||||
@@ -78,6 +78,8 @@ function createMcpApp({ loadConfig, invoke, invokeGuarded = (_context, channel,
|
||||
})), boundaries: ['Permissions and directory grants can only be changed locally.', 'Destructive actions require an exact local confirmation; MCP cannot approve it.', 'No arbitrary shell, SQL, mouse/keyboard or screen control.', 'Paused automation is resumed with mhu_automation_resume. A disabled monitor requires mhu_automation_start.'] };
|
||||
},
|
||||
mhu_performance_get: () => getPerformance(),
|
||||
mhu_profile_start: args => profile.start(args.seconds),
|
||||
mhu_profile_get: () => profile.snapshot(),
|
||||
mhu_io_limits_get: () => getUploadReadSnapshot(),
|
||||
mhu_io_limits_update: ({ readConcurrency }, context) => {
|
||||
canWrite(); guard(context);
|
||||
|
||||
@@ -19,6 +19,8 @@ const definitions = [
|
||||
['app_info', 'Read application version and runtime information.', 'read', {}, []],
|
||||
['capabilities', 'Read effective permissions, the supported action catalog and local security boundaries. This cannot grant permissions or approve actions.', 'read', {}, []],
|
||||
['performance_get', 'Read bounded in-memory main-thread, renderer, CPU, memory, I/O and transfer-slot measurements. Missing samples do not prove a freeze.', 'read', {}, []],
|
||||
['profile_start', 'Start one bounded main-thread sampling profile. No debugger port, source text, arguments or arbitrary commands are exposed.', 'diagnostics', { seconds: integer(10, 120) }, []],
|
||||
['profile_get', 'Read the latest bounded main-thread profile summary with function names, source basenames and sampling gaps.', 'diagnostics', {}, []],
|
||||
['io_limits_get', 'Read active, waiting and maximum concurrent upload file reads.', 'read', {}, []],
|
||||
['io_limits_update', 'Adjust bounded upload file-read concurrency in memory. Does not abort transfers or persist settings; default returns after restart.', 'control', { readConcurrency: integer(1, 32) }, ['readConcurrency']],
|
||||
['system_metrics', 'Read cached system CPU, RAM, disk activity, read/write throughput, cumulative byte counters and volume capacity. No shell commands. Samples every 10 seconds; null or stale values are not freeze evidence.', 'read', { limit: integer(1, 60) }, []],
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
const { Session } = require('node:inspector');
|
||||
const path = require('node:path');
|
||||
|
||||
function summarizeProfile(profile) {
|
||||
const nodes = new Map((profile.nodes || []).map(node => [node.id, node]));
|
||||
const parents = new Map();
|
||||
for (const node of nodes.values()) for (const child of node.children || []) parents.set(child, node.id);
|
||||
const frame = id => {
|
||||
const source = nodes.get(id)?.callFrame || {};
|
||||
return { name: String(source.functionName || '(anonymous)').slice(0, 160),
|
||||
file: path.basename(String(source.url || '').replace(/\\/g, '/').split(/[?#]/)[0]), line: (source.lineNumber ?? -1) + 1 };
|
||||
};
|
||||
const stack = id => {
|
||||
const result = [], seen = new Set();
|
||||
while (id !== undefined && !seen.has(id) && result.length < 12) {
|
||||
seen.add(id); result.push(frame(id)); id = parents.get(id);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
const totals = new Map(), gaps = [];
|
||||
let elapsedMs = 0;
|
||||
for (let index = 0; index < (profile.samples || []).length; index++) {
|
||||
const id = profile.samples[index], durationMs = Math.max(0, (profile.timeDeltas?.[index] || 0) / 1000);
|
||||
elapsedMs += durationMs;
|
||||
const total = totals.get(id) || { ...frame(id), samples: 0, sampledMs: 0 };
|
||||
total.samples++; total.sampledMs += durationMs; totals.set(id, total);
|
||||
if (durationMs >= 100) gaps.push({ elapsedMs, durationMs, stack: stack(id), previousStack: stack(profile.samples[index - 1]) });
|
||||
}
|
||||
return { sampleCount: profile.samples?.length || 0, elapsedMs,
|
||||
top: [...totals.values()].sort((a, b) => b.sampledMs - a.sampledMs).slice(0, 40),
|
||||
gaps: gaps.sort((a, b) => b.durationMs - a.durationMs).slice(0, 20),
|
||||
note: 'Sampling gaps can include OS scheduling and native waits; sampled stacks alone do not prove their cause.' };
|
||||
}
|
||||
|
||||
class RuntimeProfile {
|
||||
constructor({ createSession = () => new Session(), schedule = setTimeout, now = Date.now } = {}) {
|
||||
this.createSession = createSession; this.schedule = schedule; this.now = now;
|
||||
this.state = { status: 'idle' };
|
||||
}
|
||||
async start(seconds = 60) {
|
||||
if (!Number.isInteger(seconds) || seconds < 10 || seconds > 120) throw Error('PROFILE_DURATION');
|
||||
if (this.session) throw Error('PROFILE_BUSY');
|
||||
const session = this.createSession();
|
||||
this.session = session;
|
||||
this.state = { status: 'starting', startedAt: this.now(), seconds };
|
||||
this.post = (method, params = {}) => new Promise((resolve, reject) => session.post(method, params, (error, result) => error ? reject(error) : resolve(result)));
|
||||
try {
|
||||
session.connect();
|
||||
await this.post('Profiler.enable');
|
||||
await this.post('Profiler.setSamplingInterval', { interval: 10000 });
|
||||
await this.post('Profiler.start');
|
||||
this.state.status = 'recording';
|
||||
this.timer = this.schedule(() => this.stop(), seconds * 1000);
|
||||
this.timer.unref?.();
|
||||
return this.snapshot();
|
||||
} catch {
|
||||
session.disconnect(); this.session = null; this.state.status = 'failed';
|
||||
throw Error('PROFILE_UNAVAILABLE');
|
||||
}
|
||||
}
|
||||
async stop() {
|
||||
if (!this.session) return this.snapshot();
|
||||
const session = this.session;
|
||||
try {
|
||||
const { profile } = await this.post('Profiler.stop');
|
||||
this.state = { ...this.state, status: 'completed', completedAt: this.now(), result: summarizeProfile(profile) };
|
||||
} catch { this.state.status = 'failed'; }
|
||||
finally { session.disconnect(); this.session = null; }
|
||||
return this.snapshot();
|
||||
}
|
||||
snapshot() { return structuredClone(this.state); }
|
||||
}
|
||||
|
||||
module.exports = { RuntimeProfile, summarizeProfile };
|
||||
+16
-10
@@ -50,21 +50,27 @@ function createStartupDiagnostics({ fs, path, directory, metadata = {}, now = Da
|
||||
const startedAt = now();
|
||||
const session = `${startedAt}-${process.pid}`;
|
||||
let sequence = 0;
|
||||
let pending = Promise.resolve(true);
|
||||
const reportedPhases = new Set();
|
||||
const write = (event, details = {}) => {
|
||||
const entry = { session, sequence: sequence++, timestamp: new Date(now()).toISOString(), elapsedMs: Math.max(0, now() - startedAt), event, ...details };
|
||||
try {
|
||||
fs.mkdirSync(directory, { recursive: true });
|
||||
if (fs.existsSync(filePath) && fs.statSync(filePath).size >= 1024 * 1024) {
|
||||
if (!fs.promises) return false;
|
||||
const line = JSON.stringify(entry) + '\n';
|
||||
pending = pending.then(async () => {
|
||||
await fs.promises.mkdir(directory, { recursive: true });
|
||||
const stat = await fs.promises.stat(filePath).catch(error => {
|
||||
if (error.code !== 'ENOENT') throw error;
|
||||
return null;
|
||||
});
|
||||
if (stat && stat.size >= 1024 * 1024) {
|
||||
const backup = filePath + '.1';
|
||||
if (fs.existsSync(backup)) fs.unlinkSync(backup);
|
||||
fs.renameSync(filePath, backup);
|
||||
await fs.promises.unlink(backup).catch(error => { if (error.code !== 'ENOENT') throw error; });
|
||||
await fs.promises.rename(filePath, backup);
|
||||
}
|
||||
fs.appendFileSync(filePath, JSON.stringify(entry) + '\n', 'utf8');
|
||||
await fs.promises.appendFile(filePath, line, 'utf8');
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}).catch(() => false);
|
||||
return true;
|
||||
};
|
||||
const recordPhase = (phase, generation) => {
|
||||
if (typeof phase !== 'string' || !STARTUP_PHASES.has(phase)) return false;
|
||||
@@ -75,7 +81,7 @@ function createStartupDiagnostics({ fs, path, directory, metadata = {}, now = Da
|
||||
return write('renderer-phase', { phase, generation });
|
||||
};
|
||||
write('process-start', metadata);
|
||||
return { filePath, write, recordPhase };
|
||||
return { filePath, write, recordPhase, drain: () => pending };
|
||||
}
|
||||
|
||||
function summarizeStartupPixels(image) {
|
||||
|
||||
+10
-1
@@ -1,13 +1,22 @@
|
||||
const os = require('node:os');
|
||||
const { execFile } = require('node:child_process');
|
||||
const { promisify } = require('node:util');
|
||||
const { performance } = require('node:perf_hooks');
|
||||
const { runtimePerformance } = require('./performance-monitor');
|
||||
|
||||
const WINDOWS_PROBE = "$ErrorActionPreference='Stop'; $ProgressPreference='SilentlyContinue'; $d=@(Get-CimInstance Win32_PerfRawData_PerfDisk_PhysicalDisk | ForEach-Object { @{ Name=$_.Name; Timestamp_PerfTime=[string]$_.Timestamp_PerfTime; Frequency_PerfTime=[string]$_.Frequency_PerfTime; Timestamp_Sys100NS=[string]$_.Timestamp_Sys100NS; PercentIdleTime=[string]$_.PercentIdleTime; DiskReadBytesPersec=[string]$_.DiskReadBytesPersec; DiskWriteBytesPersec=[string]$_.DiskWriteBytesPersec; DiskReadsPersec=[string]$_.DiskReadsPersec; DiskWritesPersec=[string]$_.DiskWritesPersec; CurrentDiskQueueLength=[string]$_.CurrentDiskQueueLength } }); $v=@(Get-CimInstance Win32_LogicalDisk -Filter 'DriveType=3' | ForEach-Object { @{ DeviceID=$_.DeviceID; Size=[string]$_.Size; FreeSpace=[string]$_.FreeSpace } }); @{disks=$d;volumes=$v} | ConvertTo-Json -Depth 4 -Compress";
|
||||
|
||||
async function readWindowsCounters({ runner = promisify(execFile), signal } = {}) {
|
||||
const { stdout } = await runner('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
|
||||
const started = performance.now();
|
||||
let request;
|
||||
try {
|
||||
request = runner('C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
|
||||
['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', WINDOWS_PROBE],
|
||||
{ windowsHide: true, shell: false, timeout: 8000, maxBuffer: 1024 * 1024, encoding: 'utf8', signal });
|
||||
} finally {
|
||||
runtimePerformance.record('host.spawn.sync', performance.now() - started);
|
||||
}
|
||||
const { stdout } = await request;
|
||||
const value = JSON.parse(stdout.replace(/^\uFEFF/, ''));
|
||||
if (!Array.isArray(value.disks) || !Array.isArray(value.volumes) || value.disks.length > 256 || value.volumes.length > 256) throw Error('INVALID_METRICS');
|
||||
return value;
|
||||
|
||||
Reference in new issue
Block a user