@kaynooo/cli-proc 0.8.3 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/read.ts CHANGED
@@ -55,6 +55,34 @@ export async function bootMs(root = '/proc'): Promise<number> {
55
55
  return match === null ? Date.now() : Number(match[1]) * 1000
56
56
  }
57
57
 
58
+ // Runs an async mapper with bounded concurrency so /proc walks stay fast without exhausting file descriptors.
59
+ async function mapBounded<T, R>(items: readonly T[], limit: number, fn: (item: T) => Promise<R>): Promise<R[]> {
60
+ const results = Array.from<R>({ length: items.length })
61
+ let cursor = 0
62
+
63
+ async function worker(): Promise<void> {
64
+ while (cursor < items.length) {
65
+ const idx = cursor++
66
+
67
+ results[idx] = await fn(items[idx]!)
68
+ }
69
+ }
70
+
71
+ await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker))
72
+
73
+ return results
74
+ }
75
+
76
+ const PROC_CONCURRENCY = 32
77
+
78
+ async function readDetail(pid: number, root: string): Promise<Pick<ProcInfo, 'cwd' | 'exe' | 'unit' | 'container'>> {
79
+ const dir = `${root}/${pid}`
80
+ const [cwd, exe, cgroup] = await Promise.all([link(`${dir}/cwd`), link(`${dir}/exe`), text(`${dir}/cgroup`)])
81
+ const groups = parseCgroup(cgroup ?? '')
82
+
83
+ return { cwd, exe, unit: cgroupUnit(groups), container: cgroupContainer(groups) }
84
+ }
85
+
58
86
  // One process, undefined once it is gone or its stat cannot be read. `detail` adds what only the owner can read: the working directory, the executable and the cgroup.
59
87
  export async function readProc(pid: number, boot: number, { root = '/proc', detail = true } = {}): Promise<ProcInfo | undefined> {
60
88
  const dir = `${root}/${pid}`
@@ -77,30 +105,22 @@ export async function readProc(pid: number, boot: number, { root = '/proc', deta
77
105
  startMs: boot + (stat.startTicks / CLOCK_TICKS) * 1000,
78
106
  }
79
107
 
80
- if (!detail)
81
- return info
82
-
83
- const [cwd, exe, cgroup] = await Promise.all([link(`${dir}/cwd`), link(`${dir}/exe`), text(`${dir}/cgroup`)])
84
- const groups = parseCgroup(cgroup ?? '')
85
-
86
- return { ...info, cwd, exe, unit: cgroupUnit(groups), container: cgroupContainer(groups) }
108
+ return detail ? { ...info, ...await readDetail(pid, root) } : info
87
109
  }
88
110
 
89
- // Every process, with the detail only for those `detailed` accepts: the ones of the calling user, whose directories are readable.
111
+ // Every process, read in parallel without re-reading stat/status/cmdline; `detailed` controls which UIDs read cwd/exe/cgroup.
90
112
  export async function snapshot(root = '/proc', detailed: (uid: number) => boolean = () => true): Promise<ProcInfo[]> {
91
- const boot = await bootMs(root)
92
- const found: ProcInfo[] = []
93
-
94
- for (const pid of await listPids(root)) {
113
+ const [boot, pids] = await Promise.all([bootMs(root), listPids(root)])
114
+ const entries = await mapBounded(pids, PROC_CONCURRENCY, async (pid) => {
95
115
  const light = await readProc(pid, boot, { root, detail: false })
96
116
 
97
117
  if (light === undefined)
98
- continue
118
+ return undefined
99
119
 
100
- found.push(detailed(light.uid) ? await readProc(pid, boot, { root }) ?? light : light)
101
- }
120
+ return detailed(light.uid) ? { ...light, ...await readDetail(pid, root) } : light
121
+ })
102
122
 
103
- return found
123
+ return entries.filter((entry): entry is ProcInfo => entry !== undefined)
104
124
  }
105
125
 
106
126
  // The pids from `pid` up to init, nearest first, `pid` included.
@@ -143,12 +163,14 @@ export async function socketsOf(pid: number, root = '/proc'): Promise<Set<number
143
163
  return inodes
144
164
  }
145
165
 
146
- // Which pid holds each socket inode, for the processes whose fd directory can be read. Forked workers share the listen socket of their master: the holder whose parent holds it too is passed over.
166
+ // Which pid holds each socket inode, read in parallel across readable processes. Forked workers share the listen socket of their master: the holder whose parent holds it too is passed over.
147
167
  export async function socketOwners(root = '/proc'): Promise<Map<number, number>> {
168
+ const pids = await listPids(root)
169
+ const perPid = await mapBounded(pids, PROC_CONCURRENCY, async pid => ({ pid, inodes: await socketsOf(pid, root) }))
148
170
  const holders = new Map<number, number[]>()
149
171
 
150
- for (const pid of await listPids(root)) {
151
- for (const inode of await socketsOf(pid, root))
172
+ for (const { pid, inodes } of perPid) {
173
+ for (const inode of inodes)
152
174
  holders.set(inode, [...holders.get(inode) ?? [], pid])
153
175
  }
154
176