mirror of
https://github.com/nushell/nushell.git
synced 2026-12-03 10:38:41 +08:00
## Description This PR refactors the `ps` command and consolidates some code with `nu_system::ProcessInfo` while also making it more portable having the same columns on all operating systems with `ps` and `ps -l`. It also fixes some bugs where all the processes weren't being seen on MacOS. Also there was some debate as to whether 0 was valid for cpu. `cpu` is the percent of one core used during a ~100ms sample, like `top`. Most processes are idle during any 100ms, and `/bin/ps` agrees. Per-process values won't always match `/bin/ps`: macOS's `%CPU` is the kernel's decaying recent average (a process stopped after 3 seconds at 100% shows 0.0 five seconds later), and Linux's `ps` shows lifetime average CPU. ## User-facing changes (Release notes) - `ps` now lists every process on macOS (zombies included) and Windows (all but the System Idle Process), including other users' and protected processes. Values the operating system doesn't make available for a process (on macOS, the CPU, memory and threads of other users' processes unless you run as root) are null instead of the process being left out. - `ps` and `ps -l` now return the same columns on every platform. A column is null when the platform or the process doesn't provide that value. - `ps`: `pid ppid name user status cpu cpu_time mem` - `ps -l` adds: `virtual private command exe start_time user_id process_group_id session_id priority nice threads read written cwd environment` - New columns: - `cpu_time`: total CPU time the process has used. - `private`: the memory only the process uses, the number Activity Monitor and Task Manager show as "Memory". - `nice`: the nice value; on Windows, the priority class on the nice scale. - `read` and `written`: bytes of storage I/O since the process started (all I/O on Windows). - `exe`: the path of the executable. - Also new on the platforms that were missing them: `user`, `user_id`, `process_group_id`, `session_id`, `threads`, `cwd` and `environment`, and `start_time` on the BSDs. - Changed columns: - `user` is now in `ps`, and `virtual` is now only in `ps -l`. - `process_threads` is renamed to `threads`. - `working` (always the same as `mem`) and `paged` are removed. Use `private` for the memory a process uses by itself. - Linux: - `cpu` is now measured to the nanosecond instead of in 10% steps. - `name` is no longer cut to 15 characters when argv[0] or the executable shows the full name. - `cwd` and `environment` are null for processes you can't read (other users' processes unless you are root); `cwd` used to be an empty string. - Windows: - `ps` now has a `status` column (`Running`, `Sleeping` or `Suspended`). - `user_sid` is replaced by `user_id`, which holds the SID string (for example `S-1-5-18`). - `virtual` is now the virtual address space size, as on other platforms. - `priority` is the base priority (8 for normal priority). - `nice` is the priority class on the nice scale, as libuv and Node.js map it (normal 0, high -14, idle 19). - `process_group_id` is the console process group, the one `CREATE_NEW_PROCESS_GROUP` starts and `GenerateConsoleCtrlEvent` sends Ctrl+Break to. - macOS: - `cwd` is now the current working directory rather than the directory the process started in. - `status` now uses `Sleeping` instead of `Sleep` and `Waiting`, matching the other platforms, and zombies are listed as `Zombie`. - `kernel_task` (pid 0) is now listed, as Activity Monitor does. - FreeBSD, NetBSD and OpenBSD: - Zombies and other processes whose arguments can't be read are now listed, under the kernel's process name. - On FreeBSD, `cpu` now includes threads that start or exit during the sample. - On OpenBSD, `virtual` is no longer always 0. - `command` shows newlines and tabs in arguments as spaces on Windows and the BSDs too, as it already did on Linux and macOS. - Null sorts after every number, so the examples now drop nulls first: `ps | compact cpu | sort-by cpu | last 3`. Matching a text column that can be null with `=~` is an error, so also drop those rows first: `ps | compact status | where status =~ Run`. ## Additional notes closes #18358 Let this serve as documentation about what the columns mean per platform. The column set follows what top, htop, btop, btm, procs, Task Manager and Activity Monitor show: | column | Linux | macOS | Windows | FreeBSD | NetBSD / OpenBSD | | --- | --- | --- | --- | --- | --- | | `user` | name of the effective uid | same | account of the token's SID | same as Linux | same as Linux | | `status` | `stat` state | thread states; zombie/stopped for all | thread states | `ki_stat` | `p_stat` | | `cpu` | process CPU-time clock (ns), or `stat` ticks | task times | process times | `ki_runtime` | `p_rtime` | | `cpu_time` | `utime + stime` | task times | user + kernel time | `ki_runtime` | `p_rtime` (null for zombies) | | `mem` | RSS | resident size | working set | RSS | RSS | | `virtual` | VSZ | virtual size | virtual size | VSZ | VSZ | | `private` | `RssAnon` | physical footprint (own processes) | private working set | resident pages of anonymous mappings from `KERN_PROC_VMMAP` (own processes) | null: no private-memory figure | | `exe` | `/proc/<pid>/exe` (own processes) | `proc_pidpath` | `QueryFullProcessImageNameW` | `KERN_PROC_PATHNAME` | `KERN_PROC_PATHNAME`; null on OpenBSD, which doesn't expose it | | `start_time` | `stat` | `kinfo_proc` | creation time | `ki_start` | `p_ustart` (null for zombies) | | `user_id` | effective uid | effective uid | SID string | `ki_uid` | `p_uid` | | `process_group_id` | `stat` | short BSD info | console process group from the process parameters | `ki_pgid` | `p__pgid` | | `session_id` | `stat` | `getsid` (null for kernel_task) | Terminal Services session | `ki_sid` | `p_sid` | | `priority` | kernel priority | task priority (own processes) | base priority | `ki_pri.pri_level` | `p_priority` | | `nice` | `stat` | `kinfo_proc` | priority class on the nice scale | `ki_nice` | `p_nice` | | `threads` | `stat` | task info (own processes) | snapshot | `ki_numthreads` | `p_nlwps`; null on OpenBSD, whose `kinfo_proc` has no thread count | | `read` / `written` | storage bytes from `/proc/<pid>/io` (own processes) | storage bytes from `proc_pid_rusage` (own processes) | all I/O bytes, including pipes and network | null: the kernel counts block operations, not bytes | null: the kernel counts block operations, not bytes | | `cwd` | `/proc/<pid>/cwd` (own processes) | `PROC_PIDVNODEPATHINFO` (own processes) | process parameters | `KERN_PROC_CWD` (own processes) | `KERN_PROC_CWD` (own processes) | | `environment` | `/proc/<pid>/environ` (own processes) | `KERN_PROCARGS2` (own processes) | process parameters | `KERN_PROC_ENV` (own processes) | `KERN_PROC_ENV` (own processes) | "Own processes" means the current user's processes, or every process when running as root. Every backend reads each column from the kernel. These have no kernel interface to read from and stay null, with the reason in a comment where the backend sets them: `read`/`written` on the BSDs, `private` on NetBSD and OpenBSD, and `exe` and `threads` on OpenBSD.