Android Open Source - cpustats My Util






From Project

Back to project page cpustats.

License

The source code is released under:

Apache License

If you think the Android project cpustats listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jp.takke.cpustats;
//from   w w w  . java2 s .  c  o  m
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.regex.Pattern;

public class MyUtil {

    // core count cache
    private static int sLastCpuCoreCount = -1;
    
    /**
     * Gets the number of cores available in this device, across all processors.
     * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
     * 
     * @return The number of cores, or "availableProcessors" if failed to get result
     */
    // from http://stackoverflow.com/questions/7962155/how-can-you-detect-a-dual-core-cpu-on-an-android-device-from-code
    public static int calcCpuCoreCount() {
        
        if (sLastCpuCoreCount >= 1) {
            // ????????????
            return sLastCpuCoreCount;
        }
        
        try {
            // Get directory containing CPU info
            final File dir = new File("/sys/devices/system/cpu/");
            // Filter to only list the devices we care about
            final File[] files = dir.listFiles(new FileFilter() {
                
                public boolean accept(File pathname) {
                    //Check if filename is "cpu", followed by a single digit number
                    if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                        return true;
                    }
                    return false;
                }
            });
            
            // Return the number of cores (virtual CPU devices)
            sLastCpuCoreCount = files.length;
            
        } catch(Exception e) {
            sLastCpuCoreCount = Runtime.getRuntime().availableProcessors();
        }
        
        return sLastCpuCoreCount;
    }

    
    /**
     * ???????CPU?????????????
     * 
     * @return 384000 ????????????(???????????0)
     */
    public static int takeCurrentCpuFreq() {
        return readIntegerFile("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq");
    }
    
    
    /**
     * ???CPU?????????????
     * 
     * @return 384000 ????????????(???????????0)
     */
    public static int takeMinCpuFreq() {
        return readIntegerFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq");
    }
    
    
    /**
     * ??CPU?????????????
     * 
     * @return 384000 ????????????(???????????0)
     */
    public static int takeMaxCpuFreq() {
        return readIntegerFile("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
    }
    
    
    private static int readIntegerFile(String filePath) {
        
        try {
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(filePath)), 1000);
            final String line = reader.readLine();
            reader.close();
            
            return Integer.parseInt(line);
        } catch (Exception e) {
            MyLog.e(e);
            return 0;
        }
    }


    /**
     * /proc/stat ???????????? CPU ??????????
     * 
     * @return ???????? CPU ???????(???????????0)
     */
    public static ArrayList<OneCpuInfo> takeCpuUsageSnapshot() {
        
        // [0] ???????[1]????????CPU
        final ArrayList<OneCpuInfo> result = new ArrayList<OneCpuInfo>();
        
        try {
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream("/proc/stat")), 1000);
//          final RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
            
//          MyLog.i("load:-----");
            
            String line = null;
            while ((line=reader.readLine()) != null) {
                if (!line.startsWith("cpu")) {
                    break;
                }
//              MyLog.i(" load:" + load);
                    
                //     user     nice    system  idle    iowait  irq     softirq     steal
                //cpu  48200 4601 35693 979258 5095 1 855 0 0 0
                //cpu0 26847 1924 25608 212324 2212 1 782 0 0 0
                //cpu1 8371 1003 4180 254096 1026 0 50 0 0 0
                //cpu2 8450 983 3916 252872 1304 0 9 0 0 0
                //cpu3 4532 691 1989 259966 553 0 14 0 0 0
            
                final String[] tokens = line.split(" +");
                final OneCpuInfo oci = new OneCpuInfo();
                oci.idle = Long.parseLong(tokens[4]);
                oci.total = Long.parseLong(tokens[1]) 
                        + Long.parseLong(tokens[2])
                        + Long.parseLong(tokens[3])
                        + oci.idle
                        + Long.parseLong(tokens[5])
                        + Long.parseLong(tokens[6])
                        + Long.parseLong(tokens[7]);
                result.add(oci);
            }
            
            reader.close();
        } catch (Exception ex) {
            MyLog.e(ex);
        }
        
        return result;
    }
    
    
    /**
     * CPU????????
     * 
     * @param currentInfo
     * @param lastInfo
     * @return CPU?????????(????1????[0]????CPU??[1]???????????)??????????? null
     */
    public static int[] calcCpuUsages(ArrayList<OneCpuInfo> currentInfo, ArrayList<OneCpuInfo> lastInfo) {
        
        if (currentInfo == null || lastInfo == null) {
            // NPE??(???????????????????????????????????????????????????????????????)
            return null;
        }
        
        final int nLast = lastInfo.size();
        final int nCurr = currentInfo.size();
        if (nLast == 0 || nCurr == 0) {
            MyLog.d(" no info: [" + nLast + "][" + nCurr + "]");
            return null;
        }
        
        // ??????????????????????????????????
        // ?Galaxy S II ???????????????????????????????????????????????????????????CPU??????????
        final int n = (nLast < nCurr ? nLast : nCurr);  // min(nLast, nCurr)
        final int[] cpuUsages = new int[n];
        for (int i=0; i<n; i++) {
            final OneCpuInfo last = lastInfo.get(i);
            final OneCpuInfo curr = currentInfo.get(i);
            
            final int totalDiff = (int) (curr.total - last.total);  // ?????????????int?????????????????
            if (totalDiff > 0) {
                final int idleDiff = (int) (curr.idle - last.idle);
//              final double cpuUsage = 1.0 - (double)idleDiff / totalDiff;
//              cpuUsages[i] = (int)(cpuUsage * 100.0);
                // ??????????????????????(???????????????????????????????????????)
                cpuUsages[i] = 100 - idleDiff*100 / totalDiff;
                
//              MyLog.i(" idle[" + idleDiff + "], total[" + totalDiff + "], " +
//                      "/[" + (100-idleDiff*100/totalDiff) + "], " +
//                      "rate[" + cpuUsages[i] + "], " +
//                      "rate[" + ((1.0-(double)idleDiff/totalDiff) * 100.0) + "]"
//                      );
            } else {
                cpuUsages[i] = 0;
            }
            
//          MyLog.d(" [" + (i == 0 ? "all" : i) + "] : [" + (int)(cpuUsage * 100.0) + "%]" +
//                  " idle[" + idleDiff + "], total[" + totalDiff + "]");
        }
        
        return cpuUsages;
    }


    /**
     * ???????????????
     * 
     * @param clockHz ???????[KHz]
     * @return "XX MHz" ????????? "X.X GHz"
     */
    public static String formatFreq(int clockHz) {
        
        if (clockHz < 1000*1000) {
            return (clockHz / 1000) + " MHz";
        }
        
        // a.b GHz
        final int a = (clockHz / 1000 / 1000);      // a.b GHz ??? a ?
        final int b = (clockHz / 1000 / 100) % 10;  // a.b GHz ??? b ?
        return a + "." + b + " GHz";
    }
}




Java Source Code List

jp.takke.cpustats.BootReceiver.java
jp.takke.cpustats.C.java
jp.takke.cpustats.ConfigActivity.java
jp.takke.cpustats.MyAsyncTask.java
jp.takke.cpustats.MyLog.java
jp.takke.cpustats.MyUtil.java
jp.takke.cpustats.PreviewActivity.java
jp.takke.cpustats.QuadResourceUtil.java
jp.takke.cpustats.QuadResourceUtil.java
jp.takke.cpustats.ResourceUtil.java
jp.takke.cpustats.UsageUpdateService.java