This commit is contained in:
1 parent
fe3797fd18
commit
290b9db8ce
6 files changed
+98
-4
No files matched your search
@@ -0,0 +1,71 @@
|
||||
const path = require('node:path');
|
||||
const { Worker } = require('node:worker_threads');
|
||||
const { runtimePerformance } = require('./performance-monitor');
|
||||
|
||||
class BackgroundDiskReader {
|
||||
constructor({ createWorker = () => new Worker(path.join(__dirname, 'system-metrics-worker.js')), timeoutMs = 15000 } = {}) {
|
||||
this.createWorker = createWorker;
|
||||
this.timeoutMs = timeoutMs;
|
||||
this.sequence = 0;
|
||||
this.worker = null;
|
||||
this.pending = null;
|
||||
}
|
||||
connect() {
|
||||
if (this.worker) return this.worker;
|
||||
const worker = this.createWorker();
|
||||
this.worker = worker;
|
||||
worker.on('message', message => {
|
||||
if (worker !== this.worker || message.id !== this.pending?.id) return;
|
||||
const pending = this.pending;
|
||||
this.pending = null;
|
||||
if (Number.isFinite(message.spawnMs)) runtimePerformance.record('host.spawn.worker', message.spawnMs);
|
||||
pending.finish(message.error ? Error('STORAGE_QUERY_FAILED') : null, message.value);
|
||||
worker.unref();
|
||||
});
|
||||
const failed = () => {
|
||||
if (worker !== this.worker) return;
|
||||
this.worker = null;
|
||||
this.pending?.finish(Error('STORAGE_WORKER_FAILED'));
|
||||
this.pending = null;
|
||||
};
|
||||
worker.on('error', failed);
|
||||
worker.on('exit', failed);
|
||||
worker.unref();
|
||||
return worker;
|
||||
}
|
||||
read({ signal } = {}) {
|
||||
if (signal?.aborted) return Promise.reject(Error('STORAGE_QUERY_ABORTED'));
|
||||
if (this.pending) return Promise.reject(Error('STORAGE_QUERY_BUSY'));
|
||||
return new Promise((resolve, reject) => {
|
||||
const worker = this.connect();
|
||||
const id = ++this.sequence;
|
||||
let settled = false;
|
||||
const finish = (error, value) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
signal?.removeEventListener('abort', abort);
|
||||
if (error) reject(error); else resolve(value);
|
||||
};
|
||||
const cancel = code => {
|
||||
try { worker.postMessage({ type: 'cancel', id }); } catch {}
|
||||
worker.unref();
|
||||
finish(Error(code));
|
||||
};
|
||||
const abort = () => cancel('STORAGE_QUERY_ABORTED');
|
||||
const timer = setTimeout(() => cancel('STORAGE_QUERY_TIMEOUT'), this.timeoutMs);
|
||||
this.pending = { id, finish };
|
||||
signal?.addEventListener('abort', abort, { once: true });
|
||||
worker.ref();
|
||||
try { worker.postMessage({ type: 'read', id }); }
|
||||
catch {
|
||||
this.pending = null;
|
||||
worker.unref();
|
||||
finish(Error('STORAGE_WORKER_FAILED'));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const reader = new BackgroundDiskReader();
|
||||
module.exports = { BackgroundDiskReader, readWindowsCountersOffThread: options => reader.read(options) };
|
||||
@@ -0,0 +1,20 @@
|
||||
const { parentPort } = require('node:worker_threads');
|
||||
const { readWindowsCounters } = require('./system-metrics');
|
||||
const { runtimePerformance } = require('./performance-monitor');
|
||||
let active = null;
|
||||
|
||||
parentPort.on('message', async message => {
|
||||
if (message.type === 'cancel') {
|
||||
if (active?.id === message.id) active.controller.abort();
|
||||
return;
|
||||
}
|
||||
if (message.type !== 'read' || !Number.isSafeInteger(message.id) || active) return;
|
||||
const controller = new AbortController();
|
||||
active = { id: message.id, controller };
|
||||
let value, error = false;
|
||||
try { value = await readWindowsCounters({ signal: controller.signal }); }
|
||||
catch { error = true; }
|
||||
const spawnMs = runtimePerformance.snapshot().operations.find(operation => operation.name === 'host.spawn.sync')?.lastMs;
|
||||
active = null;
|
||||
parentPort.postMessage({ id: message.id, value, error, spawnMs });
|
||||
});
|
||||
@@ -3,6 +3,7 @@ const { execFile } = require('node:child_process');
|
||||
const { promisify } = require('node:util');
|
||||
const { performance } = require('node:perf_hooks');
|
||||
const { runtimePerformance } = require('./performance-monitor');
|
||||
const { readWindowsCountersOffThread } = require('./system-metrics-reader');
|
||||
|
||||
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";
|
||||
|
||||
@@ -61,7 +62,7 @@ class SystemMetrics {
|
||||
this.processCpu = options.processCpu || process.cpuUsage;
|
||||
this.processMemory = options.processMemory || process.memoryUsage;
|
||||
this.shouldCollect = options.shouldCollect || (() => true);
|
||||
this.readDisks = options.readDisks || (process.platform === 'win32' ? readWindowsCounters : null);
|
||||
this.readDisks = options.readDisks || (process.platform === 'win32' ? readWindowsCountersOffThread : null);
|
||||
this.limit = Math.max(1, Math.min(60, options.limit || 60));
|
||||
this.samples = [];
|
||||
this.previous = null;
|
||||
|
||||
Reference in new issue
Block a user