htop
htop copied to clipboard
Improving Command display/sort
Summary of discussion in https://github.com/hishamhm/htop/issues/801:
I) On Linux a process is able to modify its own command name (/proc/pid/comm) say using prctl, and its own command line (/proc/pid/cmdline) say by writing into its arguments vector. But /proc/pid/exe is set by the kernel and provides the definitive location of the executable image. While htop currently displays cmdline, for the above reason it is desirable to display the executable image path and command name, along with cmdline, merging them where possible.
After this change, Command displayed is to be interpreted as follows:
- If no token is colorized, it implies htop didn't have permission to read /proc/pid/exe of the process, and htop has fallen back to displaying only cmdline. Note that only when htop is run with root privilleges (done with care), is it able to read the /proc/pid/exe of ALL processes.
- If a token (which may have embedded spaces) is colorized, upto 15 bytes of it is understood to be comm. This implies htop was able to read the process' /proc/pid/exe and /proc/pid/comm. During display, htop first tries to find/merge comm in exe's basename; If that fails and also if the new option 'Try to find command name in cmdline' is set, htop tries to find/merge comm in cmdline (this may mis-identify a string in cmdline in very rare cases, say if comm or cmdline had been unsuitably modified). a) If comm was not merged into either exe or cmdline, three fields are displayed (with "│" as the field separator), the first being exe, the second comm, and the last cmdline. b) If comm was merged into exe or cmdline: If exe could not be merged with cmdline, two fields are displayed, the first being exe and the last cmdline; Otherwise, exe and cmdline are merged into a single field.
II) While htop currently sorts/filters the Command based always on cmdline, irrespective of whether it is full path or basename that is displayed, it is desirable to sort/filter based on what is displayed. After this change, Command is sorted/filtered based on what is displayed, whether it is exe or cmdline, full path or basename.
Implementation notes:
- Command Name (/proc/pid/comm) is displayed in a separate color (PROCESS_COMM), based on CPU_SOFTIRQ
- A new option "Try to find command name in cmdline" introduced to try to look for comm in cmdline
- LinuxProcess_getCommandStr to return the displayed string (rather than p->comm always)
- When reading /proc/pid/cmdline, cmdline is concatenated with '\n' (instead of space) as delimiter; This helps when looking for comm in basenames of cmdline tokens with embedded spaces. Later during display in RichString_writeFrom, '\n' is translated into space. - LinuxProcess_writeCommand displays exe, comm, and cmdline as described above, retaining the existing options like basename display/highlighting
I have been using these patches and they work well! it really cleans up the process display output. thanks!
I really like the pull request. Can please community merge it?
I'd love to see this rebased against https://github.com/htop-dev/htop. I've been running this patch locally for a while.
@zikaeroh: https://github.com/htop-dev/htop/pull/42