@@ -202,17 +202,23 @@ function readWindowsProcessTreeSnapshot(): ProcessTreeSnapshot | null {
202202 return parseWindowsProcessTreeSnapshot ( result . stdout ) ;
203203}
204204
205- export function readProcessTreeCpuMs ( rootPid : number | null | undefined ) : number | null {
205+ function readProcessTreeMetric ( params : {
206+ rootPid : number | null | undefined ;
207+ posixColumn : "time=" | "rss=" ;
208+ parsePosixMetric : ( raw : string ) => number | null ;
209+ windowsMetric : "cpuByPid" | "rssByPid" ;
210+ } ) : number | null {
211+ const { rootPid } = params ;
206212 if ( typeof rootPid !== "number" || ! Number . isInteger ( rootPid ) || rootPid <= 0 ) {
207213 return null ;
208214 }
209215 if ( process . platform === "win32" ) {
210216 const snapshot = readWindowsProcessTreeSnapshot ( ) ;
211217 return snapshot
212- ? collectProcessTreeMetric ( rootPid , snapshot . childrenByParent , snapshot . cpuByPid )
218+ ? collectProcessTreeMetric ( rootPid , snapshot . childrenByParent , snapshot [ params . windowsMetric ] )
213219 : null ;
214220 }
215- const result = spawnSync ( "ps" , [ "-eo" , " pid=,ppid=,time=" ] , {
221+ const result = spawnSync ( "ps" , [ "-eo" , ` pid=,ppid=,${ params . posixColumn } ` ] , {
216222 encoding : "utf8" ,
217223 killSignal : "SIGKILL" ,
218224 stdio : [ "ignore" , "pipe" , "ignore" ] ,
@@ -223,73 +229,42 @@ export function readProcessTreeCpuMs(rootPid: number | null | undefined): number
223229 }
224230
225231 const childrenByParent = new Map < number , number [ ] > ( ) ;
226- const cpuByPid = new Map < number , number > ( ) ;
232+ const metricByPid = new Map < number , number > ( ) ;
227233 for ( const line of result . stdout . split ( "\n" ) ) {
228234 const match = line . trim ( ) . match ( / ^ ( \d + ) \s + ( \d + ) \s + ( \S + ) $ / u) ;
229235 if ( ! match ) {
230236 continue ;
231237 }
232- const [ , pidRaw , ppidRaw , cpuRaw ] = match ;
238+ const [ , pidRaw , ppidRaw , metricRaw ] = match ;
233239 const pid = Number ( pidRaw ) ;
234240 const ppid = Number ( ppidRaw ) ;
235- const cpuMs = parsePsCpuTimeMs ( cpuRaw ?? "" ) ;
236- if ( ! Number . isInteger ( pid ) || ! Number . isInteger ( ppid ) || cpuMs === null ) {
241+ const metric = params . parsePosixMetric ( metricRaw ?? "" ) ;
242+ if ( ! Number . isInteger ( pid ) || ! Number . isInteger ( ppid ) || metric === null ) {
237243 continue ;
238244 }
239- cpuByPid . set ( pid , cpuMs ) ;
245+ metricByPid . set ( pid , metric ) ;
240246 const children = childrenByParent . get ( ppid ) ?? [ ] ;
241247 children . push ( pid ) ;
242248 childrenByParent . set ( ppid , children ) ;
243249 }
244- if ( ! cpuByPid . has ( rootPid ) ) {
245- return null ;
246- }
247250
248- return collectProcessTreeMetric ( rootPid , childrenByParent , cpuByPid ) ;
251+ return collectProcessTreeMetric ( rootPid , childrenByParent , metricByPid ) ;
249252}
250253
251- export function readProcessTreeRssBytes ( rootPid : number | null | undefined ) : number | null {
252- if ( typeof rootPid !== "number" || ! Number . isInteger ( rootPid ) || rootPid <= 0 ) {
253- return null ;
254- }
255- if ( process . platform === "win32" ) {
256- const snapshot = readWindowsProcessTreeSnapshot ( ) ;
257- return snapshot
258- ? collectProcessTreeMetric ( rootPid , snapshot . childrenByParent , snapshot . rssByPid )
259- : null ;
260- }
261- const result = spawnSync ( "ps" , [ "-eo" , "pid=,ppid=,rss=" ] , {
262- encoding : "utf8" ,
263- killSignal : "SIGKILL" ,
264- stdio : [ "ignore" , "pipe" , "ignore" ] ,
265- timeout : PROCESS_TREE_SNAPSHOT_TIMEOUT_MS ,
254+ export function readProcessTreeCpuMs ( rootPid : number | null | undefined ) : number | null {
255+ return readProcessTreeMetric ( {
256+ rootPid,
257+ posixColumn : "time=" ,
258+ parsePosixMetric : parsePsCpuTimeMs ,
259+ windowsMetric : "cpuByPid" ,
266260 } ) ;
267- if ( result . status !== 0 ) {
268- return null ;
269- }
270-
271- const childrenByParent = new Map < number , number [ ] > ( ) ;
272- const rssByPid = new Map < number , number > ( ) ;
273- for ( const line of result . stdout . split ( "\n" ) ) {
274- const match = line . trim ( ) . match ( / ^ ( \d + ) \s + ( \d + ) \s + ( \S + ) $ / u) ;
275- if ( ! match ) {
276- continue ;
277- }
278- const [ , pidRaw , ppidRaw , rssRaw ] = match ;
279- const pid = Number ( pidRaw ) ;
280- const ppid = Number ( ppidRaw ) ;
281- const rssBytes = parsePsRssBytes ( rssRaw ?? "" ) ;
282- if ( ! Number . isInteger ( pid ) || ! Number . isInteger ( ppid ) || rssBytes === null ) {
283- continue ;
284- }
285- rssByPid . set ( pid , rssBytes ) ;
286- const children = childrenByParent . get ( ppid ) ?? [ ] ;
287- children . push ( pid ) ;
288- childrenByParent . set ( ppid , children ) ;
289- }
290- if ( ! rssByPid . has ( rootPid ) ) {
291- return null ;
292- }
261+ }
293262
294- return collectProcessTreeMetric ( rootPid , childrenByParent , rssByPid ) ;
263+ export function readProcessTreeRssBytes ( rootPid : number | null | undefined ) : number | null {
264+ return readProcessTreeMetric ( {
265+ rootPid,
266+ posixColumn : "rss=" ,
267+ parsePosixMetric : parsePsRssBytes ,
268+ windowsMetric : "rssByPid" ,
269+ } ) ;
295270}
0 commit comments