@kaynooo/cli-proc 0.7.1 → 0.7.5

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/procs.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ExitRow } from '@kaynooo/cli-kit'
1
2
  import type { Group } from './select'
2
3
  import { existsSync } from 'node:fs'
3
4
  import { homedir } from 'node:os'
@@ -12,9 +13,8 @@ const usage = `procs
12
13
 
13
14
  Lists the leftover or heavy processes of the calling user that are worth cleaning, grouped by origin: the project folder their working directory is in, a browser profile for a chromium and its helpers, else their command. Each group shows its process count, total resident memory and oldest age.
14
15
  A process is listed when it is a dev tool (bun, node, vite, nuxt, next, chromium and chrome, playwright and webview browsers and their helpers, language servers, test runners) and either an orphan, reparented to init or to the user's systemd, or heavy (500M resident by default). The processes below a listed one, and those sharing a browser profile with it, are listed too, so a chromium whose main process was killed groups with its leftover helpers.
15
- \`--kill\` is a dry run that names what it would stop unless --yes is given; it sends SIGTERM, then SIGKILL after --grace to what is still there. It never signals pid 1, shells, terminals, this command and its ancestors, or processes of other users.
16
-
17
- Exits 0 when it listed or stopped everything, 1 when a process survived SIGKILL, 2 on a usage error.`
16
+ \`--kill\` is a dry run that names what it would stop unless --yes is given; it sends SIGTERM, then SIGKILL after --grace to what is still there. It never signals pid 1, shells, terminals, this command and its ancestors, or processes of other users.`
17
+ const EXITS: ExitRow[] = [[0, 'it listed or stopped everything'], [1, 'a process survived SIGKILL'], [2, 'a usage error']]
18
18
 
19
19
  const options = {
20
20
  'json': jsonOption('{ groups: [{ origin, kind, count, rss, oldestSeconds, tools, orphans, pids, processes }], skipped }'),
@@ -62,6 +62,7 @@ function render(groups: Group[]): string {
62
62
 
63
63
  export async function procs(args: string[]): Promise<number> {
64
64
  const { values } = parse(args, usage, options, {
65
+ exits: EXITS,
65
66
  replaces: 'pkill -f and pgrep -f one-liners (which match their own shell), ps aux | grep for leftover chromium, bun or vite processes',
66
67
  examples: [
67
68
  ['procs', 'leftover and heavy dev processes, by origin'],
package/src/read.ts CHANGED
@@ -143,13 +143,29 @@ export async function socketsOf(pid: number, root = '/proc'): Promise<Set<number
143
143
  return inodes
144
144
  }
145
145
 
146
- // Which pid holds each socket inode, for the processes whose fd directory can be read.
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.
147
147
  export async function socketOwners(root = '/proc'): Promise<Map<number, number>> {
148
- const owners = new Map<number, number>()
148
+ const holders = new Map<number, number[]>()
149
149
 
150
150
  for (const pid of await listPids(root)) {
151
151
  for (const inode of await socketsOf(pid, root))
152
- owners.set(inode, pid)
152
+ holders.set(inode, [...holders.get(inode) ?? [], pid])
153
+ }
154
+
155
+ const owners = new Map<number, number>()
156
+ const parents = new Map<number, number | undefined>()
157
+
158
+ for (const [inode, pids] of holders) {
159
+ const [first = 0] = pids
160
+
161
+ if (pids.length > 1) {
162
+ for (const pid of pids) {
163
+ if (!parents.has(pid))
164
+ parents.set(pid, parseStat(await text(`${root}/${pid}/stat`) ?? '')?.ppid)
165
+ }
166
+ }
167
+
168
+ owners.set(inode, pids.find(pid => !pids.includes(parents.get(pid) ?? -1)) ?? first)
153
169
  }
154
170
 
155
171
  return owners