2

htop has a great way to show CPU usage. It shows the activity (usage in %) of each cores, and each core gauge is divided into three parts :

  • blue: low priority processes
  • green: normal processes
  • red: kernel processes

I'm doing an htop-like in Bash (yep !) for my studies and I'd like to read these values from /proc (in preference), or otherwise from anywhere, commands for example.

top is displaying the same informations (as text) when you press 1. Eventually this can be a solution to read data from top if I can programmatically press 1 on background-ed top...

So: is there a simple way to get these informations?

If it is too complicated, read the global usage of the cores can be sufficient (I mean, for example cpu0: 12%).


EDIT: Just wrote this little command to get per-CPU usage, but without more details (so it doesn't answers perfectly my question, but if I can't find a solution I will use it):

ps axo psr,pcpu | awk 'END {
  for (E in a)
    print E, a[E]
    }
{ a[$1] += $2 }'

Btw, do you think it's reliable?

  • You could poke around in /proc/, or use ps to see the raw information that top chews on, and parse it as you like. – DopeGhoti Nov 25 '14 at 21:21
  • Yes, it seems to be a good way to do that. Please see my edit on the original post, I use ps to get global CPUs usage. Do you know what I can add in ps -o to get kernel-mode processes? (priority with -o ni) – Morgan Touverey Quilling Nov 25 '14 at 21:42
  • do man proc and search for stat – mikeserv Nov 25 '14 at 22:07
1

You can parse the load average (1 min, 5min, and 15min average number of non-idle processes) from the output of uptime. Note that this includes processes in disk-sleep, not just CPU-users.

  • I needed a "detailed" usage of each CPU, not an abstract number like the load average. Endly, i opted for the use of /proc/stat, where you can find global (not per-process) & detailed usage of each CPU core. Htop and top are using this file. I originally wanted a simpler solution but it doesn't exists (and it looks logical when you dig deeper). /proc/stat is a formidable file, you just need some documentation and a bit of maths, then you can easily make an htop-killer :D – Morgan Touverey Quilling Apr 19 '15 at 19:56
  • oh, I just saw "if too complicated, global usage"... missed the "of the cores". sorry >< – Peter Cordes Apr 19 '15 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.