FORUMS
Remove All Ads from XDA

Read CPU usage for multi core cpu

269 posts
Thanks Meter: 25
 
By t4ure4n, Senior Member on 25th July 2013, 02:54 PM
Post Reply Email Thread
Question: How to Read cpu usage for an android device with multi-core (quad-core etc)

I been trying to read cpu usage in my application using the szcoder answer to

How to get Memory usage and CPU usage in android at Stackoverflow

His solution used to work on my Samsung Galaxy S2 (dual-core cpu), but its not working on my HTC One M7 which is quad core.

Most of the readings for ercentage cpu usage (Value from szcoder's method x 100) are either negative, most of them < 10% and few are more than 100%.

I also tried to use Fabian Knapp's solution which uses
Code:
top -n 1
command to determine the CPU usage but in my testing parsing
Code:
/proc/stat
is much faster than
Code:
top -n 1
(0.3xx seconds comapred to 3.xx seconds) so in case some one suggests it, I won't be using the top command because of time consumption.

I also asked optc76 the author of [APP][Android 2.1+]CPU Usage, Frequency & Memory Monitor (Quad Core Support) to provide some guidence but he never replied.

So can some one please help me to read CPU usage for android device multicore cpu.

Regards
 
 
26th July 2013, 11:39 AM |#2  
Senior Member
Flag Qazvin
Thanks Meter: 61
 
More
I don't get what you want.
I think you want to detect the number of cores. Here it is :
private int getNumCores
() {
//Private Class to
display only CPU
devices in the
directory listing
class CpuFilter
implements FileFilter {
@override
public boolean
accept(File pathname) {
//Check if
filename is "cpu",
followed by a single
digit number
if
(Pattern.matches("cpu
[0-9]", pathname.getName
())) {
return
true;
}
return
false;
}
}
try {
//Get directory
containing CPU info
File dir = new
File("/sys/devices/
system/cpu/");
//Filter to
only list the devices
we care about
File[] files =
dir.listFiles(new
CpuFilter());
//Return the
number of cores
(virtual CPU devices)
return
files.length;
} catch(Exception
e) {
//Default to
return 1 core
return 1;
}
}
If you want sth else such as frequency tell me.
26th July 2013, 07:07 PM |#3  
TheUnkn0wn's Avatar
Senior Member
Thanks Meter: 881
 
More
0.3 seconds isn't so bad since I guess you will only be reading/updating the usage every 0.5 - 1 seconds. Try and figure out faster ways to parse the file?
27th July 2013, 12:06 PM |#4  
OP Senior Member
Flag London
Thanks Meter: 25
 
More
Quote:
Originally Posted by torpedo mohammadi

I don't get what you want.
I think you want to detect the number of cores. Here it is :
If you want sth else such as frequency tell me.

Quote:
Originally Posted by TheUnkn0wn

0.3 seconds isn't so bad since I guess you will only be reading/updating the usage every 0.5 - 1 seconds. Try and figure out faster ways to parse the file?

My problem is in-corrent reading on quad-core mobile device like HTC One. Most of the readings for ercentage cpu usage (Value from szcoder's method x 100) are either negative, most of them < 10% and few are more than 100%.

I have tested the same code on Samsung Galaxy S3 which is quad-core as wellI don't have less than 0 or more than 100 for cpu usage,
so I am assuming its something wrong with the kernel on HTC One M7.

The time used to read from /proc/stat is not the problem, its the accuracy of readings.
27th July 2013, 02:07 PM |#5  
Recognized Developer
Thanks Meter: 646
 
More
Quote:
Originally Posted by t4ure4n

My problem is in-corrent reading on quad-core mobile device like HTC One. Most of the readings for ercentage cpu usage (Value from szcoder's method x 100) are either negative, most of them < 10% and few are more than 100%.

I have tested the same code on Samsung Galaxy S3 which is quad-core as wellI don't have less than 0 or more than 100 for cpu usage,
so I am assuming its something wrong with the kernel on HTC One M7.

The time used to read from /proc/stat is not the problem, its the accuracy of readings.

eheh yes, just add the red magic code
Code:
.
.
.
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
String load = reader.readLine();
load = load.replace("  ", " ");
String[] toks = load.split(" ");
.
.
.
reader.seek(0);
load = reader.readLine();
reader.close();
load = load.replace("  ", " ");
toks = load.split(" ");
.
.
.
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes