View previous topic :: View next topic |
Author |
Message |
andrewwalker27 l33t
Joined: 27 Jun 2005 Posts: 660
|
Posted: Sun Apr 01, 2018 6:03 pm Post subject: Trouble reading CPU load |
|
|
I'm running a minecraft server on a Raspberry Pi with a small oled screen which reads out memory, disk space, ip address and CPU load but the CPU load seems wrong.
The line of code that displays it is as follows
Code: | cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
|
If I run top via a terminal with ssh the actual cpu load is nearer 10 - 15% but the oled says 1.04%. I suspect top is correct but, as the code isn't mine and I don't know anything about awk, I can't work out what is going wrong.
I'm running a Pi 3 which I think has 4 cores, can anyone give me any pointers?
EDIT:
Just realised it is reading load average, what I want it to display is CPU load, any ideas? |
|
Back to top |
|
|
mike155 Advocate
Joined: 17 Sep 2010 Posts: 4438 Location: Frankfurt, Germany
|
Posted: Sun Apr 01, 2018 6:50 pm Post subject: |
|
|
Quote: | the actual cpu load is nearer 10 - 15% but the oled says 1.04%.
|
Please look at the output of 'top':
- The 'load average' is shown in the first line and it's given as an absolute number, not in percent. The 'load average' has a particular meaning and it's probably not what you think it is. See https://en.wikipedia.org/wiki/Load_(computing).
- The %CPU value (which is shown in percent) is a task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.
Please don't confuse the two values.
This code (you said it isn't yours, right?)
Code: | top -bn1 | grep load | awk '{printf "CPU Load: %.2f", $(NF-2)}'
|
is probably the most stupid code I've seen for a long time - and it really makes me angry! Everything is wrong:
- There's no need to call 'top' if you can read the value from /proc/loadavg.
- 'grep load' will return more that one line if a process runs whose name contains 'load'.
- If you start somewhat heavy programs like top or awk, it surely will affect load and %CPU values.
- The code is very fragile. As soon as the output format of 'top' changes, the code won't work any longer.
It would be better to use:
Code: | echo "CPU Load: $( cut -d ' ' -f 1 /proc/loadavg )"
|
Last edited by mike155 on Sun Apr 01, 2018 7:09 pm; edited 1 time in total |
|
Back to top |
|
|
Ant P. Watchman
Joined: 18 Apr 2009 Posts: 6920
|
Posted: Sun Apr 01, 2018 7:09 pm Post subject: |
|
|
I'd do something like this instead:
Code: | atop -P CPU 5 | awk -n '/^CPU/ { print $9 / $6; fflush() }' | while read pct_cpu; do
printf "CPU Load: %.1f" $pct_cpu > /tmp/cpuavg
done |
And then cat /tmp/cpuavg in the actual command. Better yet would be to let this loop drive the updates, but you probably can't do that. |
|
Back to top |
|
|
mike155 Advocate
Joined: 17 Sep 2010 Posts: 4438 Location: Frankfurt, Germany
|
Posted: Sun Apr 01, 2018 7:32 pm Post subject: |
|
|
Ant P.:
I like your code. It's a nice solution!
Minor nits:- On my machine, it works only if I use '$10 / $6', instead of '$9 / $6'.
- Please don't call it 'CPU Load' - it's not a CPU load. 'CPU Usage' would be better.
|
|
Back to top |
|
|
Ant P. Watchman
Joined: 18 Apr 2009 Posts: 6920
|
Posted: Sun Apr 01, 2018 7:49 pm Post subject: |
|
|
I probably should've read the manpage more closely. Make that ($9 + $10) / $6... |
|
Back to top |
|
|
andrewwalker27 l33t
Joined: 27 Jun 2005 Posts: 660
|
Posted: Sun Apr 01, 2018 7:59 pm Post subject: |
|
|
Thanks for the info, the code I used was just some demo code from adafruit for their oled library but it seemed to work so I'm not going to criticise it!
Just in case anyone is interested, I was building this
https://www.instructables.com/id/The-OreServer-a-Raspberry-Pi-Dedicated-Minecraft-S/
and was having a few lag issues. My idea was to put a small oled 128 x 32 pixel display on it to monitor performance, the load average was slightly over 1 so I wanted to see if an individual core was being maxed out.
Not strictly Gentoo I know but I was ssh'ing in from my Gentoo box!
I'll give your code a go but otherwise I may just try out the Pi 3+ in case it has that slight performance increase to run it smoothly.
Maybe I could try running Gentoo instead of Raspbian on the PI, now there's a thought......
Thanks again. |
|
Back to top |
|
|
khayyam Watchman
Joined: 07 Jun 2012 Posts: 6227 Location: Room 101
|
Posted: Sun Apr 01, 2018 11:45 pm Post subject: |
|
|
mike155 wrote: | Code: | echo "CPU Load: $( cut -d ' ' -f 1 /proc/loadavg )" |
|
mike155, et al ... it's unlikely the OP has zsh but perhaps of interest to others:
Code: | % echo "CPU Usage:" ${$(</proc/loadavg)[1]}
CPU Usage: 0.04 |
best ... khay |
|
Back to top |
|
|
|