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

前端 未结 2 1385
予麋鹿
予麋鹿 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

    0 讨论(0)
  • 2020-12-10 22:00
    • It is not clear if you want this information inside your app, or just for your own use.

      you can get this information on with adb:

      adb shell cat /proc/cpuinfo
      

      adb shell cpuinfo

    • If you want to use this information in your app, create a simple function to return a Map<String,String>, for example,

      public static Map<String, String> getCpuInfoMap() {
          Map<String, String> map = new HashMap<String, String>();
          try {
              Scanner s = new Scanner(new File("/proc/cpuinfo"));
              while (s.hasNextLine()) {
                  String[] vals = s.nextLine().split(": ");
                  if (vals.length > 1) map.put(vals[0].trim(), vals[1].trim());
              }
          } catch (Exception e) {Log.e("getCpuInfoMap",Log.getStackTraceString(e));}
          return map;
      }
      

      Note, this will not get multiple cpus information, overwrites. Most of the values are similar anyways. or Modify to create List of CpuInfoMaps.

      try,

      Log.d("getCpuInfoMap test", getCpuInfoMap().toString());
      
    0 讨论(0)
提交回复
热议问题