The problem with htop on Linux is that once there are 200+ processes running on the system htop takes significant portion of CPU time and utilization. This is because htop has to go through each process entry in procfs (open, read, close) every second and parse the text context instead of just calling appropriate syscall like on the OpenBSD and friends to retrieve such information.
It would help if kernel provided process information in binary form instead of serializing it into text. Or even better to provide specific syscalls for it like on macOS, Windows, OpenBSD, Solaris and others.
Significant in what way? I created 400 processes + 328 threads on a 10-year-old CPU and htop is not using more than 1.3% CPU on a machine with 800% available CPU power (quad-core, 8-thread)[0]. That means 0.16% total CPU used. While I agree that it is _less_ efficient than some other ways, in what way is that _significant_?
I remember way back in the early years of the Third Age, I had to write a process accounting system that supported all kinds of Unices (HP-UX, AIX, Solaris, FreeBSD, Linux ...). And you're right, there's a plethora of other options other than procfs, although IIRC, Linux wasn't the only one I had to support with that variant. Wasn't Solaris one of them?
I will say, htop is a lot more efficient than GNU top, despite its functionality. I do not use it in the (default?) mode where it lists all the threads, because that is nuts.
Traditional Unix implementation of ps and similar tools work by directly reading the appropriate data structures from kernel (through /dev/kmem or something to that effect). Modern BSD implementations have libkvm.a, that abstracts that somehow, but still directly reads stuff from kernel space.
I don't know, but I don't see doing random reads from kernel memory as particularly sane API to get list of processes and procfs is several orders of magnitude cleaner solution.
Its not meant to be used 24/7, its just used as a guide/diagnsostic to how the machine and its processes are performing! I use it for maybe about 10 seconds.
Well, you are right (and top has the same problem if not worse), but I'm always surprised what these tools are doing that they take significat CPU time. Parsing 200 virtual text files every second for sure should not.
> Or even better to provide specific syscalls for it like on macOS, Windows, OpenBSD, Solaris and others.
One of my favorite things about htop are some of the projects that have been created that are modeled after htop but focus on information other than system resources.
Cointop [0] is one of these projects that comes to mind.
intel_gpu_top helped me solve a mysterious performance issue on a MacBook after countless hours of fruitless investigation. Overheating and throttling was an issue but even after I fixed it the system would lag hard - instantly when I used the external 4k display, and after a while on the internal 1440p screen. Turns out cool-retro-term was maxing out the integrated Intel GPU which caused the entire system to stutter and lag.
Unfortunately both the MBP and my current XPS 15 are unable to drive cool-retro-term on a 4k display with the CPU integrated graphics, and they both overheat and throttle if I use the nvidia graphics card :/
htop is an excellent tool. I appreciate his valiant effort to explain what load average is; it's confused Unix users forever. His explanation is more or less right but I think misses a bit of context about the old days of Unix performance.
It used to be in the early 90s that Unix systems were quite frequently I/O bound; disks were slow and typically bottlenecked through one bus and lots of processes wanted disk access all the time. Swapping was also way more common. Load average is more or less a quick count of all processes that are either waiting on CPU or waiting on I/O. Either was likely in an old Unix system, so adding them all up was sensible.
In modern systems your workload is probably very specifically disk bound, or CPU bound, or network bound. It's much better to actually look at all three kinds of load separately. htop is a great tool for doing that! but the summary load average number is not so useful anymore.
These are two different commands. "strace | grep foo" looks for any line containing foo. It will find "foobar" and "food" system calls. It will find the word "foo" in the (abridged) string data that it prints out (read(..., "foo...", ...)).
Meanwhile, strace's -e filter will find exact matches of syscalls that are named on the command line.
Obviously the author wants the second one, but it is hardly useless to grep. And, once you know how to use grep, you know how to do this sort of search for every program ever written. It is nice that strace has its own budget string matching built in... but knowing how strace works only tells you how strace works. Knowing how grep works lets you do this sort of thing to any program that outputs text.
(A lot of work has been done to try and make something as good as "strace -e" but generically; for example, Powershell's Select-Object cmdlet. I have never managed to get that to do anything useful, but they did manage to break grep, erm ... Select-String, in the process. Great work!)
I disagree in this case, I think it's more Unix-style to use grep when appropriate. You should learn a lot of generally applicable tools, not the intricate details of a few specialized tools. The "useless cat" case can be represented as, instead of grep foo filename, grep foo < filename.
This is such a great article - I’ve worked with linux for more than a decade and never really understood what “setuid root” actually meant or that “kill” is a builtin to Bash
> I decided to look everything up and document it here.
You can be a hero, too! I find this inspiring. It's nice to see such an accessible and pragmatic way of making a contribution to the community. My very first thought on seeing that was "I could do that!"
I regret neither Top neither Htop show you estimates of "IO Activity time" like in Windows 10 task manager - I need to use separate iostat to observe that.
I found the "IO Activity time", percentage of time when IO is used, to be a really good indicator of IO load on machine level - neither io-op per second, neither bandwith tell much if you're already using up all available IO. Load does not help here, as number of processes doing IO influences "load" more.
To be fair it’s a far worse problem on Windows (although I guess a lot of my Linux machines are running from ram now so maybe I wouldn’t notice if it weren’t.)
The one thing I don't get about htop is the progress bars... they never seem to behave the way I'd expect them to based on the percentages, and they've got some colour coding I'm not clear on either... surely there is something I'm missing.
>There will be a lot of output. We can grep for the open system call. But that will not really work since strace outputs everything to the standard error (stderr) stream. We can redirect the stderr to the standard output (stdout) stream with 2>&1.
Besides being a great explanation of htop, I like the way this article captures the way I - far from a shell guru - tend to think when putting together a few steps in the terminal. And even then it shows that it pays to read the man page too!
I am convinced that load average on a machine is one of the most misleading statistics you can view. It never means what you think it means and half the time it doesn't even indicate a real problem when it's high.
> One process runs for a bit of time, then it is suspended while the other processes waiting to run take turns running for a while. The time slice is usually a few milliseconds so you don't really notice it that much when your system is not under high load. (It'd be really interesting to find out how long time slices usually are in Linux.)
Isn't this the famous kernel HZ? It was originally 100 (interrupts/second), but nowadays often 250 or 1000:
It’s much more complex than that these days. With the CFS scheduler a process will run for somewhere between the target latency (basically the size of slice that N processes waiting to be scheduled are competing for, I think defaulted to 20ms) as the upper bound and the minimum granularity (the smallest slice that may be granted to a process being scheduled) as the lower bound, I think defaulted to 4ms.
This is made more complex by the ability to configure the scheduler with high granularity, including the ability to schedule different processors and process groups with different schedulers (and the rules that govern how the schedulers can then preempt each other).
Awesome article, this HN thread should indicate it was originally published on HN in 2016. As mentioned on the front page "#1 on Hacker News on December 2, 2016"
This is a great explanation of everything in the output of htop and related, but I suggest the author clean up the prose a little bit to make it a bit less conversational and easier to read.
[+] [-] for_xyz|6 years ago|reply
It would help if kernel provided process information in binary form instead of serializing it into text. Or even better to provide specific syscalls for it like on macOS, Windows, OpenBSD, Solaris and others.
[+] [-] herpderperator|6 years ago|reply
[0] https://i.imgur.com/onNSHQw.png
[+] [-] mhd|6 years ago|reply
[+] [-] microcolonel|6 years ago|reply
[+] [-] dfox|6 years ago|reply
I don't know, but I don't see doing random reads from kernel memory as particularly sane API to get list of processes and procfs is several orders of magnitude cleaner solution.
[+] [-] dana321|6 years ago|reply
[+] [-] seppel|6 years ago|reply
> Or even better to provide specific syscalls for it like on macOS, Windows, OpenBSD, Solaris and others.
top manages to take 6% CPU on my Macbook.
[+] [-] zantana|6 years ago|reply
[+] [-] coleifer|6 years ago|reply
[+] [-] shanecoin|6 years ago|reply
Cointop [0] is one of these projects that comes to mind.
[0] https://github.com/miguelmota/cointop
[+] [-] the_pwner224|6 years ago|reply
Unfortunately both the MBP and my current XPS 15 are unable to drive cool-retro-term on a 4k display with the CPU integrated graphics, and they both overheat and throttle if I use the nvidia graphics card :/
It's a really cool terminal though: https://github.com/Swordfish90/cool-retro-term
[+] [-] MasterScrat|6 years ago|reply
"NVIDIA GPUs htop like monitoring tool"
[+] [-] Pawamoy|6 years ago|reply
https://github.com/pawamoy/aria2p
[+] [-] NelsonMinar|6 years ago|reply
It used to be in the early 90s that Unix systems were quite frequently I/O bound; disks were slow and typically bottlenecked through one bus and lots of processes wanted disk access all the time. Swapping was also way more common. Load average is more or less a quick count of all processes that are either waiting on CPU or waiting on I/O. Either was likely in an old Unix system, so adding them all up was sensible.
In modern systems your workload is probably very specifically disk bound, or CPU bound, or network bound. It's much better to actually look at all three kinds of load separately. htop is a great tool for doing that! but the summary load average number is not so useful anymore.
[+] [-] herpderperator|6 years ago|reply
> $ strace uptime 2>&1 | grep open
Instead, you should do `strace -e open uptime` to select that system call. Like Useless Use of Cat[0], this could be considered a Useless Use of Grep.
[0] https://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_...
Edit: Heh, when I went back to continue reading the article this was mentioned on the following line. Oops :)
[+] [-] jrockway|6 years ago|reply
Meanwhile, strace's -e filter will find exact matches of syscalls that are named on the command line.
Obviously the author wants the second one, but it is hardly useless to grep. And, once you know how to use grep, you know how to do this sort of search for every program ever written. It is nice that strace has its own budget string matching built in... but knowing how strace works only tells you how strace works. Knowing how grep works lets you do this sort of thing to any program that outputs text.
(A lot of work has been done to try and make something as good as "strace -e" but generically; for example, Powershell's Select-Object cmdlet. I have never managed to get that to do anything useful, but they did manage to break grep, erm ... Select-String, in the process. Great work!)
[+] [-] ddevault|6 years ago|reply
[+] [-] CUViper|6 years ago|reply
[+] [-] jimmaswell|6 years ago|reply
[+] [-] AdrienLemaire|6 years ago|reply
After digging in the documentation, I finally found something working:
EDIT: got it. my system calls are openat, not open. Hence the following works[+] [-] anderspitman|6 years ago|reply
[+] [-] ehsankia|6 years ago|reply
[+] [-] brown9-2|6 years ago|reply
[+] [-] redsparrow|6 years ago|reply
You can be a hero, too! I find this inspiring. It's nice to see such an accessible and pragmatic way of making a contribution to the community. My very first thought on seeing that was "I could do that!"
Thank you.
[+] [-] aljarry|6 years ago|reply
I found the "IO Activity time", percentage of time when IO is used, to be a really good indicator of IO load on machine level - neither io-op per second, neither bandwith tell much if you're already using up all available IO. Load does not help here, as number of processes doing IO influences "load" more.
[+] [-] swiley|6 years ago|reply
[+] [-] Avamander|6 years ago|reply
[+] [-] trasz|6 years ago|reply
[+] [-] microcolonel|6 years ago|reply
[+] [-] ajryan|6 years ago|reply
[+] [-] zymhan|6 years ago|reply
[+] [-] lma21|6 years ago|reply
[+] [-] jszymborski|6 years ago|reply
[+] [-] boomskats|6 years ago|reply
[+] [-] catalogia|6 years ago|reply
[+] [-] samfriedman|6 years ago|reply
Besides being a great explanation of htop, I like the way this article captures the way I - far from a shell guru - tend to think when putting together a few steps in the terminal. And even then it shows that it pays to read the man page too!
[+] [-] zaphar|6 years ago|reply
[+] [-] vesinisa|6 years ago|reply
Isn't this the famous kernel HZ? It was originally 100 (interrupts/second), but nowadays often 250 or 1000:
http://man7.org/linux/man-pages/man7/time.7.html
[+] [-] bri3d|6 years ago|reply
This is made more complex by the ability to configure the scheduler with high granularity, including the ability to schedule different processors and process groups with different schedulers (and the rules that govern how the schedulers can then preempt each other).
[+] [-] jmercha|6 years ago|reply
[+] [-] jasoneckert|6 years ago|reply
[+] [-] carlchenet|6 years ago|reply
[+] [-] helb|6 years ago|reply
[+] [-] zepearl|6 years ago|reply
Btw. additional recommendation (especially when a lot of CPUs and/or disks are involved + you want to keep an eye on multiple things at once): nmon
[+] [-] pmoriarty|6 years ago|reply
Seeing the last page of htop's output after it exits is usually useful to me.
[+] [-] Operyl|6 years ago|reply
[+] [-] rajandatta|6 years ago|reply
[+] [-] kccqzy|6 years ago|reply
[+] [-] Teknoman117|6 years ago|reply
[+] [-] ngcc_hk|6 years ago|reply