in All availableThis forumThis topic
Author Message
andrewwalker27
l33t

Joined: 27 Jun 2005
Posts: 610

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?
mike155
Guru

Joined: 17 Sep 2010
Posts: 463
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
Ant P.

Joined: 18 Apr 2009
Posts: 4984

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.
_________________
*.ebuild // /etc/service/*
mike155
Guru

Joined: 17 Sep 2010
Posts: 463
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.
Ant P.

Joined: 18 Apr 2009
Posts: 4984

 Posted: Sun Apr 01, 2018 7:49 pm    Post subject: I probably should've read the manpage more closely. Make that (\$9 + \$10) / \$6..._________________*.ebuild // /etc/service/*
andrewwalker27
l33t

Joined: 27 Jun 2005
Posts: 610

 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.
khayyam
Watchman

Joined: 07 Jun 2012
Posts: 5632
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:" \${\$(

best ... khay
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT Page 1 of 1

 Jump to: Select a forum Assistance----------------News & AnnouncementsFrequently Asked QuestionsInstalling GentooMultimediaDesktop EnvironmentsNetworking & SecurityKernel & HardwarePortage & ProgrammingGamers & PlayersOther Things GentooUnsupported Software Discussion & Documentation----------------Documentation, Tips & TricksGentoo ChatGentoo Forums FeedbackOff the WallDuplicate Threads International Gentoo Users----------------中文 (Chinese)DutchFinnishFrenchDeutsches Forum (German)  Diskussionsforum  Deutsche DokumentationGreekForum italiano (Italian)  Forum di discussione italiano  Risorse italiane (documentazione e tools)Polskie forum (Polish)  Instalacja i sprzęt  Polish OTWPortuguese  Documentação, Ferramentas e DicasRussianScandinavianSpanishOther Languages Architectures & Platforms----------------Gentoo on AMD64Gentoo on ARMGentoo on PPCGentoo on SparcGentoo on Alternative ArchitecturesGentoo for Mac OS X (Portage for Mac OS X)
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