LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-01-2022, 11:40 AM   #1
whk102
LQ Newbie
 
Registered: Jun 2022
Posts: 21

Rep: Reputation: 0
I need help understanding /proc/stat


I am building a cross platform app for both android and desktop linux systems and currently I need to get the % usage of each cpu core. For greater compatibility without having to depend on the installation of other applications, I have decided to use /proc/stat since it is compatible with any system with a linux kernel.

The problem with using commands is that you would have to attach the binaries for each cpu architecture and its dependencies, to avoid that I prefer to use commands that are available on both systems (gnu/linux desktop and android).

I found a lot of info on how to calculate the % of cpu usage using /proc/stat, but in every place I found info, a different way of doing the calculation came out, most of them take the idle time as a reference to get the % of I use adding the rest of the values ​​but it does not give me a real %, I did a benchamrk and the htop correctly shows me the cpu usage at 90 and 100% of some cores but my application only reaches 2 or 4%, I saw in others places that take the past value as a reference to the current value but it is not clear to me why, so I was reading the use of each value of the cpu stat and it says that it is the time of use in a numerical unit, then there I get the following question... each item is the time of use or dead time, but what is the total reference time? how to get the % of usage time, does /proc/stat have a reference time to make that calculation? because for some reason if I add all the values ​​to have a 100% and then take the % of the rest that is not idle I get a very low % and it hardly ever changes.

Code:
whk@machine:~$ cat /proc/stat
cpu  257200 9018 69467 16581469 49277 0 43684 0 0 0
cpu0 13512 586 3878 1042531 732 0 25487 0 0 0
cpu1 16070 547 4130 1040727 367 0 6867 0 0 0
cpu2 17530 668 4499 1037825 858 0 2390 0 0 0
cpu3 17659 530 4136 1038608 318 0 938 0 0 0
cpu4 19063 600 4613 1036692 463 0 467 0 0 0
cpu5 13180 514 3833 1043240 429 0 153 0 0 0
cpu6 16221 636 4361 1039216 852 0 186 0 0 0
cpu7 14439 383 4751 1025566 15819 0 120 0 0 0
cpu8 16862 498 4340 1020855 18711 0 100 0 0 0
cpu9 14430 511 4955 1021473 6781 0 5407 0 0 0
cpu10 17046 607 4822 1038008 1195 0 48 0 0 0
cpu11 16133 510 4505 1039033 591 0 473 0 0 0
cpu12 15821 541 4394 1039839 560 0 44 0 0 0
cpu13 18446 603 4382 1035827 609 0 954 0 0 0
cpu14 16098 642 4129 1040134 506 0 25 0 0 0
cpu15 14684 638 3732 1041889 478 0 20 0 0 0
intr 65070753 38 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15183 15183 0 2201804 1268977 0 0 0 0 0 0 0 1104611 0 0 0 0 0 0 0 0 0 48173 48928 44907 47665 47561 56692 44649 39956 42233 31667 42982 42998 43430 55383 44710 45184 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2104 1086 1509 8794 12455 2205 2174 2450 963 3532 1471 14264 11574 888 3028 1181 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2066532 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 829 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 97035969
btime 1662039791
processes 55088
procs_running 1
procs_blocked 0
softirq 28962641 3382724 2036250 2390 1315101 32461 0 407476 12489991 70 9296178
My code in Dart language is:

Code:
  double _getCoreUsage(int coreId) {
    // https://www.baeldung.com/linux/get-cpu-usage#2-getting-cpu-usage-using-procstat

    final File file = File('/proc/stat');
    if(!file.existsSync()) { throw Exception(); }
    String statString = file.readAsStringSync().trim();

    final RegExpMatch? regExpMatch = RegExp(
      '^cpu$coreId\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\$',
      multiLine: true
    ).firstMatch(statString);
    if(regExpMatch == null) { throw Exception(); }

    final int user = int.parse(regExpMatch.group(1)!);
    final int nice = int.parse(regExpMatch.group(2)!);
    final int system = int.parse(regExpMatch.group(3)!);
    final int idle = int.parse(regExpMatch.group(4)!);
    final int ioWait = int.parse(regExpMatch.group(5)!);
    final int hardIrq = int.parse(regExpMatch.group(6)!);
    final int softIrq = int.parse(regExpMatch.group(7)!);
    final int steal = int.parse(regExpMatch.group(8)!);
    final int guest = int.parse(regExpMatch.group(9)!);
    final int guestNice = int.parse(regExpMatch.group(10)!);

    final int idleTime = idle + ioWait;
    final int workingTime = (user + nice + system + hardIrq + softIrq + steal + guest + guestNice);
    final int total = idleTime + workingTime;
    final double usage = (workingTime * 100) / total;
    return usage;

    /*return (
      (idle * 100) /
      (user + nice + system + idle + iowait + irq + softirq + steal + guest + guestNice)
    );*/
  }
how can I correctly interpret /proc/stat and get the % usage of each core?, why do I need to make a comparison between an old value and a new value? if the value of time of use does not have reference to previous values but to a static value of total time which I do not understand well.
 
Old 09-01-2022, 03:29 PM   #2
whk102
LQ Newbie
 
Registered: Jun 2022
Posts: 21

Original Poster
Rep: Reputation: 0
In "man proc" says:

Quote:
/proc/stat
kernel/system statistics. Varies with architecture. Common entries include:

cpu 10132153 290696 3084719 46828483 16683 0 25195 0 175628 0
cpu0 1393280 32966 572056 13343292 6130 0 17875 0 23933 0
The amount of time, measured in units of USER_HZ (1/100ths of a second on most architectures, use sysconf(_SC_CLK_TCK) to obtain the right value), that the system ("cpu" line)
or the specific CPU ("cpuN" line) spent in various states:
Ok, if cpu stat says "cpu0 13512 586 3878 1042531 732 0 25487 0 0 0", the user usage is 13512 * 100ths times in how much time? in one second?, the 60 is the 100%?, and what would be the total capacity of times per second?
 
Old 09-01-2022, 08:06 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,163

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
OK, you need to step back for a moment (don't ask how long a moment is ... )

The values in /proc/stat are accumulated values since /proc was created at boot. These are the reference values for the entire system. This is why you need to take (at least) two values a known time apart (1 second is good) so you can use the differences to calculate your percentages for that duration.
This is why the first value reported by vmstat or top and the like is so out of line with later values reported - the first iteration is "since boot", whilst the following are for the requested duration.

BTW iowait is a component of idle time. On a single CPU system it is of limited value, on a multi-core it is totally pointless. Ignore it completely for CPU% calculations.
 
Old 09-02-2022, 01:36 PM   #4
whk102
LQ Newbie
 
Registered: Jun 2022
Posts: 21

Original Poster
Rep: Reputation: 0
That means that there is no way for the kernel to natively tell us what the state of cpu usage is in real time? In my case, the application is modular and separated into layers, each time it requires the state of the cpu would have to have a timeout to perform the comparison or carry global variables between controllers, that's bad.
 
Old 09-02-2022, 04:08 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,708
Blog Entries: 4

Rep: Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949
Quote:
Originally Posted by whk102 View Post
That means that there is no way for the kernel to natively tell us what the state of cpu usage is in real time? In my case, the application is modular and separated into layers, each time it requires the state of the cpu would have to have a timeout to perform the comparison or carry global variables between controllers, that's bad.
Unfortunately for you, "that is the case." And you will have to plan accordingly. Linux is not a real-time OS. The only thing that you can do is to periodically sample the available statistics. How you decide to preserve the sample-data between samplings is entirely up to you.
 
Old 09-02-2022, 07:27 PM   #6
whk102
LQ Newbie
 
Registered: Jun 2022
Posts: 21

Original Poster
Rep: Reputation: 0
Thank you very much for the help, I understand well.
 
Old 09-03-2022, 05:03 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,163

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Quote:
Originally Posted by whk102 View Post
That means that there is no way for the kernel to natively tell us what the state of cpu usage is in real time?
As you yourself noted there are only two states for a CPU/Core - busy or not. The kernel can tell you lots of things about CPUs - all of which require it to use a CPU for some time ... bit of a circular conundrum that.

The only way to get a percentage use is to sample over time - even for the kernel should the devs decide to bother. You do the sampling, you can decide things like frequency.
 
  


Reply



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 On
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
[SOLVED] hidden symbol `stat' in /usr/lib/libc_nonshared.a(stat.oS) is referenced by DSO harig Programming 1 03-28-2013 06:34 AM
[SOLVED] gcc linker error - hidden symbol `stat' in /usr/lib/libc_nonshared.a(stat.oS) chaosless Linux - Software 1 09-17-2009 12:33 PM
Boot hang after 'proc on /proc type proc (rw)' Hagoromo Slackware 13 10-05-2007 05:03 PM
Linux stat to Windows Stat sridurai Programming 3 09-24-2004 04:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:43 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