How to get specific information of an Android device from “/proc/cpuinfo” file?

前端 未结 2 1387
予麋鹿
予麋鹿 2020-12-10 21:20

How can I parse /proc/cpuinfo virtual file of my Android tablet to get information of the processor\'s core and clockspeed? I don’t need all information provide

2条回答
  •  眼角桃花
    2020-12-10 21:47

    I hope its not too late for an answer but, this is how i get the current frequency for a specific cpu core:

    public class MainActivity extends Activity{
    
    private static final int INSERTION_POINT = 27;
    
    private static String getCurFrequencyFilePath(int whichCpuCore){
        StringBuilder filePath = new StringBuilder("/sys/devices/system/cpu/cpu/cpufreq/scaling_cur_freq");
        filePath.insert(INSERTION_POINT, whichCpuCore);
        return filePath.toString();
     }
    
    public static int getCurrentFrequency(int whichCpuCore){
    
         int curFrequency = -1;
         String cpuCoreCurFreqFilePath = getCurFrequencyFilePath(whichCpuCore);
    
         if(new File(cpuCoreCurFreqFilePath).exists()){
    
             try {
                    BufferedReader br = new BufferedReader(new FileReader(new File(cpuCoreCurFreqFilePath)));
                    String aLine;
                    while ((aLine = br.readLine()) != null) {
    
                        try{
                            curFrequency = Integer.parseInt(aLine);
                        }
                        catch(NumberFormatException e){
    
                            Log.e(getPackageName(), e.toString());
                        }
    
                    }
                    if (br != null) {
                        br.close();
                    }
            } 
            catch (IOException e) {
                Log.e(getPackageName(), e.toString());
            }
    
         }
    
         return curFrequency;
     }
    
    }
    

    From here its a piece of cake, you simply call the method :-D

    int core1CurrentFreq = getCurrentFrequency(1, this); 
    

    Sometimes the cores go offline, in which case the file path will not exist and -1 will be returned

    NOTE. the returned value is in KHz
    MHz value is core1CurrentFreq / 1e3
    GHz value is core1CurrentFreq / 1e6

    Some explainations on the getCurFrequencyFilePath() method since it is not all that clear.

    Current frequency is usually stored in the file: scaling_cur_freq

    The file path is:

    "/sys/devices/system/cpu/cpu(XX)/cpufreq/scaling_cur_freq"
    

    where (XX) is substituted for the cpu core number eg:

    "/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq"
    

    The INSERTION_POINT variable is nothing more than the index of (XX), the point at which we want to place the number corresponding to the cpu core

    I suggest you take a look at some of the other files in the cpufreq folder, you can use them to get other information like maximum and minimum frequency, list of availables frequencies etc.

    Click this Link and scroll down to heading 3

提交回复
热议问题