0

I'm trying to write an Android app that gathers usage statistics of the device (just to learn about Android development, and how things work, I'm pretty new). What I'm trying to get right now is the frequency of the cores of my device (it has 4 cores).

I'm trying to do

cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_cur_freq
                            OR
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq

but after getting the value from cpu0, it doesn't seem to work. I'm not crashing the app, but I have tried a bunch of things, including messing around in the terminal, where it is telling me I am giving it an invalid argument. I'll include the code below, if anyone could help, you'd be awesome!

try {    
        //Process process = Runtime.getRuntime().exec("su");
        RandomAccessFile reader = new RandomAccessFile
                ("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
        String load = reader.readLine();
        freqs[0] = Integer.parseInt(load);
        reader = new RandomAccessFile
                ("/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq", "r");
        load = reader.readLine();
        freqs[1] = Integer.parseInt(load);
        reader = new RandomAccessFile
                ("/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq", "r");
        load = reader.readLine();
        freqs[2] = Integer.parseInt(load);
        reader = new RandomAccessFile
                ("/sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq", "r");
        load = reader.readLine();
        freqs[3] = Integer.parseInt(load);

       //A for loop I had tried and failed to implement

        /*RandomAccessFile reader = null;
        for (i = 0; i < cores; i++){
            reader = new RandomAccessFile("/sys/devices/system/cpu/cpu" + i 
                    + "/cpufreq/cpuinfo_cur_freq", "r");
            String load = reader.readLine();
            freqs[i] = Integer.parseInt(load);
        }*/         

    } catch (IOException ex) {
        ex.printStackTrace();
    }

The core0 freq looks correct, the rest read 0, thanks again!

Kmanc
  • 349
  • 1
  • 5
  • 20
  • It is giving you an 'invalid argument' *where?* – user207421 Jan 31 '14 at 00:27
  • For one thing, you need root access to see .../cpufreq/cpuinfo_cur_freq and you might not even be able to get write access (pretty sure you can't). No idea why you can't read the cpu1, cpu2 and cpu3 directories. – Larry Jan 31 '14 at 01:02
  • Sorry for being unclear. The terminal that I was using to look at the file is telling me that the cat argument is invalid, and this is the case with and without root access. I don't actually want to write to the file, just read values from it. And it almost doesn't matter which file I read from; cpuinfo is more accurate, but even scaling would do – Kmanc Jan 31 '14 at 17:20
  • Any updates? It seems like I can't even read the files from the terminal. I get invalid argument. I shouldn't need root for reading scaling frequencies, but I still can't get it to work – Kmanc Feb 04 '14 at 18:26

0 Answers0