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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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
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.