LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 07-31-2019, 08:24 AM   #1
Kjeld Flarup
LQ Newbie
 
Registered: Oct 2010
Posts: 11

Rep: Reputation: 0
Inconsistent values in /proc/stat


I'm looking for an explanation to how /proc/stat reports CPU usage. It is the basis for ex. the top command.

I have a 2 core ARMv7 processor running Linux kernel 4.14.34 with RT preempt patches.

It s my understanding that /proc/stat is counting ticks. My kernel is running 100 HZ, thus there would be 100 jiffies/s
Now I have made an observation on CPU1 that /proc/stat it may count more than 100 jiffies during a second. Usually the value is less than 100, also on a PC with Ubuntu.

This script shows how I do the calculation
Code:
b=`awk /cpu1/'{print $2+$3+$4+$5+$6+$7+$8+$9+$10+$11}' /proc/stat`
sleep 1
a=`awk /cpu1/'{print $2+$3+$4+$5+$6+$7+$8+$9+$10+$11}' /proc/stat`
expr $a - $b
Every time I boot I get a new value. It typically varies from 40 to 140 jiffies/s
And with every new value, busybox top applet show a new percentage of load.

Hope that somebody knows a good article on how this works.
 
Old 08-02-2019, 08:10 AM   #2
greencedar
Senior Member
 
Registered: Sep 2018
Distribution: Linux Mint 19.1 Tessa & 19.3 Tricia
Posts: 1,314
Blog Entries: 1

Rep: Reputation: 128Reputation: 128
Does this article by github help your understanding?

https://github.com/Leo-G/DevopsWiki/...-is-calculated

If there article does not help, at the bottom of the article there are other references that may help your understanding.
 
Old 08-07-2019, 03:15 AM   #3
Kjeld Flarup
LQ Newbie
 
Registered: Oct 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks for the link and references. Unfortunately they did not solve my problem, but made me sure that there is something wrong with figures I see in /proc/stat.

The unit should be in USER_HZ which since kernel 2.6 has been fixed to 100. Thus no more than 100 tick/s

However I can see that user space counts 100 ticks, thus the CPU is fully booked.
And then idle also counts 33 ticks on that CPU. Should not be possible.
 
Old 08-08-2019, 02:56 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
I suspect it's ARM-specific. Quick Search (tm).

Also look at this shell script:
Code:
#!/bin/bash 

# calculates cpu usage from values found in /proc/stat

delay=1 # 1 second between reads

color=''

# optional eyecandy
#~ tput civis
#~ trap 'tput cnorm' EXIT

# sleep as a builtin
for file in /usr/lib*/bash/sleep; do
    [ -r "$file" ] && enable -f "$file" sleep && break
done
# Portable enough?

while :; do
  # Get the first line with aggregate of all CPUs 
  read -r -a cpu_now </proc/stat
  # Get all columns but skip the first (which is the "cpu" string) 
  cpu_sum="${cpu_now[@]:1}" 
  # Replace the column seperator (space) with + 
  cpu_sum="${cpu_sum// /+}"
  # Get the delta between two reads 
  cpu_delta=$((cpu_sum - cpu_last_sum)) 
  # Get the idle time Delta 
  cpu_idle=$((cpu_now[4]- cpu_last[4])) 
  # Calc time spent working 
  cpu_used=$((cpu_delta - cpu_idle)) 
  # Calc percentage 
  cpu_usage=$((100 * cpu_used / cpu_delta)) 

  # Keep this as last for our next read 
  cpu_last=("${cpu_now[@]}") 
  cpu_last_sum=$cpu_sum 

# optional eyecandy
#~   case $cpu_usage in
#~    [5-7]?) color='\e[33m' ;; # yellow
#~    [8-9]?|100) color='\e[31m' ;; # red
#~  esac

  printf "\r\E[0KCPU ${color}%02d%%\E[0m" $cpu_usage 
  
  # Wait a second before the next read 
  sleep "$delay"
done
 
  


Reply

Tags
load



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Are linux proc filesystem files like /proc/stat unlinked and recreated upon update? Longman Linux - Server 9 01-14-2019 01:47 PM
Boot hang after 'proc on /proc type proc (rw)' Hagoromo Slackware 13 10-05-2007 05:03 PM
/proc/stat values don't add up ! mike0w Linux - General 0 10-25-2005 05:01 AM
New cpu values in /proc/stat? eldonz Linux - General 1 10-19-2005 12:16 PM
Linux stat to Windows Stat sridurai Programming 3 09-24-2004 04:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 01:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration