@@ -16,18 +16,22 @@ export class HostResources extends Context.Service<
1616 { readonly read : Effect . Effect < HostResourcesSnapshot > }
1717> ( ) ( "t3/resourceTelemetry/HostResources" ) { }
1818
19- // os.cpus() drops iowait from idle and from the total, so a disk-blocked core reads as busy.
20- // libuv turns the 100 Hz tick counters behind os.cpus() into milliseconds, hence the scale here.
21- function readDiskWait ( platform : NodeJS . Platform ) : number | null {
22- if ( platform !== "linux" ) return 0 ;
23- try {
24- const stat = NodeFS . readFileSync ( "/proc/stat" , "utf8" ) ;
25- // cpu user nice system idle iowait irq softirq steal guest guest_nice
26- const ioWaitTicks = Number ( stat . slice ( 0 , stat . indexOf ( "\n" ) ) . split ( / \s + / ) [ 5 ] ) ;
27- return Number . isFinite ( ioWaitTicks ) ? ioWaitTicks * 10 : null ;
28- } catch {
29- return null ;
30- }
19+ /**
20+ * Core time from a /proc/stat "cpu" line, in the milliseconds os.cpus() reports: disk wait
21+ * counts as idle, and softirq and steal as work. os.cpus() leaves all three out of the total,
22+ * so a core blocked on a disk or spent on interrupts reads as the wrong one of the two.
23+ */
24+ export function parseLinuxCpu ( stat : string ) : { idle : number ; total : number } | null {
25+ const end = stat . indexOf ( "\n" ) ;
26+ // cpu user nice system idle iowait irq softirq steal guest guest_nice
27+ const ticks = ( end === - 1 ? stat : stat . slice ( 0 , end ) ) . split ( / \s + / ) . slice ( 1 , 9 ) . map ( Number ) ;
28+ if ( ticks . length !== 8 || ! ticks . every ( Number . isFinite ) ) return null ;
29+ const [ user = 0 , nice = 0 , system = 0 , idle = 0 , ioWait = 0 , irq = 0 , softirq = 0 , steal = 0 ] =
30+ ticks ;
31+ // libuv turns these 100 Hz counters into the milliseconds os.cpus() reports.
32+ const idleMs = ( idle + ioWait ) * 10 ;
33+ const busyMs = ( user + nice + system + irq + softirq + steal ) * 10 ;
34+ return { idle : idleMs , total : idleMs + busyMs } ;
3135}
3236
3337/**
@@ -36,29 +40,37 @@ function readDiskWait(platform: NodeJS.Platform): number | null {
3640 */
3741export const loadAverageCountsDiskWait = ( platform : NodeJS . Platform ) => platform === "linux" ;
3842
39- export function readCpu ( platform : NodeJS . Platform ) {
43+ export interface CpuTimes {
44+ readonly idle : number ;
45+ readonly total : number ;
46+ readonly count : number ;
47+ }
48+
49+ /** Cumulative core time, or nothing when this platform's counters cannot tell work from its absence. */
50+ export function readCpu ( platform : NodeJS . Platform ) : CpuTimes | null {
4051 const cpus = NodeOS . cpus ( ) ;
41- const cpu = cpus . reduce (
42- ( sum , { times } ) => ( {
43- idle : sum . idle + times . idle ,
44- total : sum . total + times . user + times . nice + times . sys + times . idle + times . irq ,
45- } ) ,
46- { idle : 0 , total : 0 } ,
47- ) ;
48- const diskWait = readDiskWait ( platform ) ;
52+ if ( platform === "linux" ) {
53+ try {
54+ const parsed = parseLinuxCpu ( NodeFS . readFileSync ( "/proc/stat" , "utf8" ) ) ;
55+ return parsed === null ? null : { ... parsed , count : cpus . length } ;
56+ } catch {
57+ return null ;
58+ }
59+ }
4960 return {
50- idle : cpu . idle + ( diskWait ?? 0 ) ,
51- total : cpu . total + ( diskWait ?? 0 ) ,
61+ ...cpus . reduce (
62+ ( sum , { times } ) => ( {
63+ idle : sum . idle + times . idle ,
64+ total : sum . total + times . user + times . nice + times . sys + times . idle + times . irq ,
65+ } ) ,
66+ { idle : 0 , total : 0 } ,
67+ ) ,
5268 count : cpus . length ,
53- diskWait,
5469 } ;
5570}
5671
57- type CpuTimes = ReturnType < typeof readCpu > ;
58-
5972/** Share of core time spent running work between two readings, null if they are not comparable. */
6073export function cpuBusyBetween ( previous : CpuTimes , next : CpuTimes ) : number | null {
61- if ( previous . diskWait === null || next . diskWait === null ) return null ;
6274 const totalDelta = next . total - previous . total ;
6375 const idleDelta = next . idle - previous . idle ;
6476 return previous . count === next . count && totalDelta > 0 && idleDelta >= 0
@@ -86,7 +98,8 @@ const make = Effect.fn("makeHostResources")(function* () {
8698 const previousCpu = readCpu ( platform ) ;
8799 yield * Effect . sleep ( "200 millis" ) ;
88100 const cpu = readCpu ( platform ) ;
89- const cpuUtilization = cpuBusyBetween ( previousCpu , cpu ) ;
101+ const cpuUtilization =
102+ previousCpu === null || cpu === null ? null : cpuBusyBetween ( previousCpu , cpu ) ;
90103 const totalMemoryBytes = NodeOS . totalmem ( ) ;
91104 // On Windows libuv returns GlobalMemoryStatusEx.ullAvailPhys, including standby memory.
92105 let availableMemoryBytes = NodeOS . freemem ( ) ;
@@ -108,7 +121,7 @@ const make = Effect.fn("makeHostResources")(function* () {
108121 return {
109122 sampledAt : DateTime . toEpochMillis ( yield * DateTime . now ) ,
110123 cpuUtilization,
111- cpuCount : cpu . count ,
124+ cpuCount : cpu ? .count ?? NodeOS . cpus ( ) . length ,
112125 availableMemoryBytes : Math . min ( totalMemoryBytes , Math . max ( 0 , availableMemoryBytes ) ) ,
113126 totalMemoryBytes,
114127 } ;
0 commit comments