1. Are you ready for the Galaxy S20? Here is everything we know so far!

How can I get the CPU load in a multi-core android device?

Discussion in 'Android Development' started by Matt Pil, Jun 6, 2019.

  1. Matt Pil

    Matt Pil Lurker
    Thread Starter

    As a student, I have a project where I must analyze the CPU load of every core in multi-core and Big.little architectures. I execute some applications on recent Android devices (Oreo 8.0). From what I've seen on the internet, it seems that the CPU load (average and detailled for each core) is not available anymore to developers. Therefore, it is impossible for me to analyze it, unless I try to root all my devices to unlock features.

    I currently have 3 devices: a Samsung A5 (2017), a Samsung A7 (2018) and a LG Nexus 5X. I can't access the CPU load for any of those devices.

    I've tried the following code to access the data related to the CPU utilization.

    RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");

    This should give me information about the CPU, and it only gives me an exception.

    java.io.FileNotFoundException: /proc/stat: open failed: EACCES (Permission denied)

    Is there any other way to access this information (average CPU load and the detailled load for each core)? Or do I have to root my devices and hope it solves my problem?

    lillycarrie82 likes this.

    1. Download the Forums for Android™ app!


  2. GameTheory

    GameTheory Android Expert

    @Matt Pil
    Root is required. Prior to android 6, you can read root files with RandomAccessFile, but as of android 6 this was changed for security reasons.

    You can get info for each individual core in the following directory:
    Code (Text):
    1. /sys/devices/system/cpu
    On a rooted device, the following example can get you started. It's a method that you can use to read root files...
    Code (Java):
    1. static String exec(String cmd) {
    2.     try {
    3.         Process process = Runtime.getRuntime().exec("su -c " + cmd);
    4.         BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    5.         int read;
    6.         char[] buffer = new char[4096];
    7.         StringBuilder output = new StringBuilder();
    8.         while ((read = reader.read(buffer)) > 0) {
    9.             output.append(buffer, 0, read);
    10.         }
    11.         reader.close();
    12.         process.waitFor();
    13.         return output.toString();
    14.     } catch (IOException | InterruptedException e) {
    15.         throw new RuntimeException(e);
    16.     }
    17. }
    Usage of the above method...
    Code (Java):
    1. String maxFreq = exec("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq");
    2. setText(maxFreq);
    If you still get permission denied, then you'll have to change SELinux context for the file temporarily like so...

    Assuming you're rooted with magisk. Upon reboot the file will go back to its normal se-context...
    Code (Java):
    1. String[] cmds = { "chcon u:object_r:magisk_file:s0 /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" };
    2. exec(cmds);
    4. static void exec(String[] cmds) {
    5.     try {
    6.         Process process = Runtime.getRuntime().exec("su");
    7.         DataOutputStream os = new DataOutputStream(process.getOutputStream());
    8.         for (String tmpCmd : cmds) {
    9.             os.writeBytes(tmpCmd + "\n");
    10.         }
    11.         os.writeBytes("exit\n");
    12.         os.flush();
    13.         os.close();
    14.     } catch (IOException e) {
    15.         e.printStackTrace();
    16.     }
    17. }
    With the above method you can pass multiple commands, reducing the amount of su calls. For example...
    Code (Java):
    1. String[] cmds = {
    2.     "cat /dir/file_one",
    3.     "cat /dir/file_two",
    4.     "cat /dir/file_three"
    5. };
    6. exec(cmds);
    I provided 2 different methods, one for reading root files to get stdout and the other for executing root commands.

    As these are brief samples, I gave you basic code to get you started with root development.
    Word of caution: The su call is a blocking call which blocks the UI thread. You need to wrap these methods in an AsyncTask or any other background threading otherwise you'll get a lot of ANRs(android not responding). The above samples should all work as is, at least for testing and getting started.

    There's also root libraries that make this all really simple so you may want to find one and read the documentation on how to use it.

    Good luck :)

Share This Page