BREAKING CHANGE(smartmetrics): add system-wide metrics collection, Prometheus gauges, and normalized CPU reporting

This commit is contained in:
2026-02-19 10:10:58 +00:00
parent 32d75804c0
commit b99d2cc04b
7 changed files with 210 additions and 14 deletions

View File

@@ -1,6 +1,12 @@
import * as fs from 'fs';
import * as os from 'os';
import { execSync } from 'child_process';
// CPU core count (cached at module load)
const cpuCoreCount = typeof os.availableParallelism === 'function'
? os.availableParallelism()
: os.cpus().length;
// Cached system constants
let clkTck: number | null = null;
let pageSize: number | null = null;
@@ -61,8 +67,10 @@ function hrtimeSeconds(): number {
}
export interface IPidUsageResult {
cpu: number;
memory: number;
cpu: number; // raw per-core CPU% (can exceed 100%)
cpuCoreCount: number; // number of CPU cores on the machine
cpuNormalizedPercent: number; // cpu / coreCount — 0-100% of total machine
memory: number; // RSS in bytes
}
/**
@@ -95,12 +103,16 @@ export async function getPidUsage(
result[pid] = {
cpu: cpuPercent,
cpuCoreCount,
cpuNormalizedPercent: cpuPercent / cpuCoreCount,
memory: memoryBytes,
};
} else {
// First call for this PID — no delta available, report 0% cpu
result[pid] = {
cpu: 0,
cpuCoreCount,
cpuNormalizedPercent: 0,
memory: memoryBytes,
};
}