refactor(processes): extract Linux start-time capture

This commit is contained in:
Merge Sim
2026-09-05 01:32:02 -07:00
parent e043107104
commit a78f3758d5
2 changed files with 39 additions and 37 deletions
+38
View File
@@ -0,0 +1,38 @@
import { readFile } from 'node:fs/promises'
import type { ProcessTableRow } from './process-table-snapshot'
/** Read Linux's stable PID start-time ticks without spawning another process. */
export async function readLinuxProcessStartTimes(
rows: readonly ProcessTableRow[]
): Promise<ReadonlyMap<number, string> | undefined> {
if (process.platform !== 'linux') {
return undefined
}
const candidates = rows.filter((row) => row.tty !== undefined && row.tty !== '?')
const starts = await Promise.all(
candidates.map(async (row) => {
try {
const stat = await readFile(`/proc/${row.pid}/stat`, 'utf8')
const closingParen = stat.lastIndexOf(')')
if (closingParen === -1) {
return null
}
const tail = stat
.slice(closingParen + 1)
.trim()
.split(/\s+/)
const startTime = tail[19]
return startTime ? ([row.pid, startTime] as const) : null
} catch {
return null
}
})
)
const result = new Map<number, string>()
for (const entry of starts) {
if (entry) {
result.set(entry[0], entry[1])
}
}
return result
}
+1 -37
View File
@@ -1,6 +1,6 @@
import { execFile as execFileCb } from 'node:child_process'
import { readFile } from 'node:fs/promises'
import { promisify } from 'node:util'
import { readLinuxProcessStartTimes } from './linux-process-start-times'
import {
PS_ARGS,
PS_MAX_BUFFER_BYTES,
@@ -217,42 +217,6 @@ function assertWholeCapture(stdout: string): string {
return stdout
}
/** Read Linux's stable PID start-time ticks without spawning another process. */
async function readLinuxProcessStartTimes(
rows: readonly ProcessTableRow[]
): Promise<ReadonlyMap<number, string> | undefined> {
if (process.platform !== 'linux') {
return undefined
}
const candidates = rows.filter((row) => row.tty !== undefined && row.tty !== '?')
const starts = await Promise.all(
candidates.map(async (row) => {
try {
const stat = await readFile(`/proc/${row.pid}/stat`, 'utf8')
const closingParen = stat.lastIndexOf(')')
if (closingParen === -1) {
return null
}
const tail = stat
.slice(closingParen + 1)
.trim()
.split(/\s+/)
const startTime = tail[19]
return startTime ? ([row.pid, startTime] as const) : null
} catch {
return null
}
})
)
const result = new Map<number, string>()
for (const entry of starts) {
if (entry) {
result.set(entry[0], entry[1])
}
}
return result
}
const processTableReader = createProcessTableSnapshotReader<ProcessTableCapture>({
runPs: () => captureProcessTable(PS_ARGS),
now: () => Date.now()