How to get specific information about an Android device from the file "/ proc / cpuinfo"?

How can I parse the /proc/cpuinfovirtual file of my Android tablet to get information about the processor core and clock speed? I do not need all the information provided by the above file; just these two bits. Can anybody help?

+2
source share
2 answers
  • It is unclear whether you want this information in your application or just for your own use.

    you can get this information using adb:

    adb shell cat /proc/cpuinfo
    

    adb shell cpuinfo

  • If you want to use this information in your application, create a simple function to return 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;
    }
    

    , , . . CpuInfoMaps.

    ,

    Log.d("getCpuInfoMap test", getCpuInfoMap().toString());
    
+4

, , :

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;
 }

}

, : -D

int core1CurrentFreq = getCurrentFrequency(1, this); 

, , -1

.
MHz - core1CurrentFreq/1e3
- core1CurrentFreq/1e6

getCurFrequencyFilePath(), .

: scaling_cur_freq

:

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

(XX) cpu, :

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

INSERTION_POINT - , (XX), , , cpu

cpufreq, , , ..

3

0

Source: https://habr.com/ru/post/1618019/


All Articles