Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Trouble reading CPU load
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
andrewwalker27
l33t
l33t


Joined: 27 Jun 2005
Posts: 610

PostPosted: Sun Apr 01, 2018 6:03 pm    Post subject: Trouble reading CPU load Reply with quote

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
View user's profile Send private message
mike155
Guru
Guru


Joined: 17 Sep 2010
Posts: 463
Location: Frankfurt, Germany

PostPosted: Sun Apr 01, 2018 6:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ant P.
Advocate
Advocate


Joined: 18 Apr 2009
Posts: 4984

PostPosted: Sun Apr 01, 2018 7:09 pm    Post subject: Reply with quote

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.
_________________
*.ebuild // /etc/service/*
Back to top
View user's profile Send private message
mike155
Guru
Guru


Joined: 17 Sep 2010
Posts: 463
Location: Frankfurt, Germany

PostPosted: Sun Apr 01, 2018 7:32 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ant P.
Advocate
Advocate


Joined: 18 Apr 2009
Posts: 4984

PostPosted: Sun Apr 01, 2018 7:49 pm    Post subject: Reply with quote

I probably should've read the manpage more closely. Make that ($9 + $10) / $6...
_________________
*.ebuild // /etc/service/*
Back to top
View user's profile Send private message
andrewwalker27
l33t
l33t


Joined: 27 Jun 2005
Posts: 610

PostPosted: Sun Apr 01, 2018 7:59 pm    Post subject: Reply with quote

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
View user's profile Send private message
khayyam
Watchman
Watchman


Joined: 07 Jun 2012
Posts: 5632
Location: Room 101

PostPosted: Sun Apr 01, 2018 11:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum