I am developing an Android application that shows the CPU load per core and memory consumption. For CPU load I am reading /proc/stat and for memory -> /proc/meminfo. However I see that the number of CPU cores in /proc/stat is changing during subsequent reading of the file.
cpu 230599 10622 84595 1892023 8236 16 285 0 0 0
cpu0 138005 7992 58080 1738918 6407 16 278 0 0 0
intr 9136791 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9601 0 0 0 0 0 0 .......
ctxt 16904510
btime 1394641996
processes 16919
procs_running 2
procs_blocked 0
softirq 1688530 407 706934 422 1558 407 407 92978 324500 1267 559650
and after 5seconds it becomes:
cpu 230772 10623 84671 1890801 8236 16 286 0 0 0
cpu0 138104 7993 58126 1739267 6407 16 279 0 0 0
cpu1 92668 2630 26545 151534 1829 0 7 0 0 0
intr 9144729 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9601 0 0 0 0 0 0 ........
ctxt 16923744
btime 1394641996
processes 16946
procs_running 2
procs_blocked 0
softirq 1690205 407 707396 422 1558 407 407 93311 324790 1267 560240
Does it mean that cpu cores are sleeping in some cases?
I have been searching about how to get
CPU
usage from android for days and nights and I run into that problem some times.Well, if I am not wrong I think that when you don't get a
CPU
core means that the core that is not been displayed is offline (android do so to prevent huge battery drain).If you want to find out how many core the android has find another way, there are tooooons out there, and then do whatever you like.
The answers of @Torch2424 and @Andre are ok but for my uses they lack several things:
Here is the code that hopefully fix correctly this: available as a class or as a simple app.
I know this is old, but there does not seem to be an answer for this, and I've been searching for a solution as well.
What I figured, is that if I'm recording each core named "cpu" that follows a number. and the cpus go from 0-3. And if I'm reading the cores in order. Then if .readline() of /proc/stat returns a string that does NOT contain cpu, than that core must not be working, and is offline. So, in theory, it would be at zero percent usage. So, return 0.
* Completed answer with code, see below *
Heres some code, in case what I said didn't make sense, mine is based on this: Get Memory Usage in Android
and here is how I found a new calculation that gave me more accurate representation of core readings: How to get total cpu usage in Linux (c++)
First, here is some of my CPU function that displays a string after these loops and stuff to the user. I'm posting this so you have a better understanding of i, and what my code means
getNumCores can be found here, I will not be posting since I feel like the person who did should get the credit for it: How can you detect a dual-core cpu on an Android device from code?
And, lastly, here's my code, I hope it makes sense, and I offered a lot of comments.
And, as a last note, my readCore function will return a value of 0.0 - 1.0, you need to multiply by 100 to get a percentage.
EDIT As requested from the comments below, Android documentation: "While active, CPUs can be brought online or put offline, change clock speeds and associated voltages (possibly also affecting memory bus speeds and other system core power state), and can enter lower power idle states while in the kernel idle loop. Not only are these different CPU power states measured for the power profile, it may be necessary to avoid the power draw variance when measuring other parameters."
Link: https://source.android.com/devices/tech/power.html#
Even if the accepted answer was given a few months after the question, it was very useful to me. Hopefully my suggestion will be useful to anyone else reading this question in the future.
The answer of @Torch2424 was very good but is missing a check: as you stated, sometimes Android doesn't use all the CPUs; I tried your code on a 4-core tablet and indeed I see that most of the time 2 CPUs are not used at all or not changing that often, therefore the relative lines of /proc/stat file are exactly the same. This means that
(total2 - total1)
equals to 0 and then you try to divide by 0, whose result is NaN.This is actually my working code for the readCore() function:
Hope it helps!