Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent access to directories on rooted phone

Tags:

android

root

I am writing an app that needs to work on a rooted Sony xperia z (Android 4.4.2, build number 10.5.A.0.230). The app reads the core speeds of the four cores by passing one of the following strings...

"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"
"/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq"
"/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq"
"/sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq"

...to the function below...

   long sysfilenum(String str)
    {
        String text = null;
        try
        {
             File file = new File(str);

             BufferedReader br = new BufferedReader(new FileReader(file));    
             text = br.readLine();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        return Long.valueOf(text);
    }

This function gets executed once per second:

This all works perfectly. I display the numbers in my app, and I see that they behave exactly as I would expect. The core speeds are often different to one another, and when the core heats up too much (I read the CPU temperature too) the speeds reduce. So I am confident these numbers are being read correctly.

Now this is where the mystery starts. If I open up an adb shell to my device, then type "su" as my first command. I can navigate to the "/sys/devices/system/cpu/cpu0/cpufreq/" directory without problem. But If I attempt to navigate to the directories for core 1,2 or 3. I get "No such file or directory". I am baffled.

EDIT: I just tested using a terminal emulator which runs on the device itself, and found I could access the directories for all the cores. So its adb shell that is behaving strangely.

EDIT: Just on a hunch I tried su -c "ls cpu1/cpufreq/" and it worked, I saw the cpu1 directory. So maybe typing su in the adb shell didn't give me superuser access?

EDIT: OMG! I just executed su -c "ls cpu1/cpufreq/" over and over again to find that sometimes it worked (i.e. I saw the directory listing) and sometimes it didn't (i.e. I got no such file or directory)!

EDIT:: According to this document, it appears that the directories are virtual. At one point it says says "cd /sys/devices/system/cpu # a virtual directory made visible by device drivers". Hmmm, maybe I have screwed up device drivers?

like image 605
Mick Avatar asked Sep 30 '22 09:09

Mick


1 Answers

The reason you are seeing the directories disappear is cpu hotplugging. This is the linux kernel feature which allows the OS to disable specific cores on the cpu to save power. The default Android kernel is designed to disable cores 1-3 on nearly all phones when the system load is such that they are not necessary for computations. When these cores are disabled, the corresponding directories are also removed.

Therefore, you can infer that if the directory exists, the core is operating at the specified frequency. If it does not exist, the core is disabled and thus has no frequency.

like image 194
Jeffrey Mixon Avatar answered Oct 03 '22 01:10

Jeffrey Mixon