首页 > 系统相关 >如何获取 Android 设备的CPU核数、时钟频率以及内存大小

如何获取 Android 设备的CPU核数、时钟频率以及内存大小

时间:2023-04-06 22:07:18浏览次数:54  
标签:return int UNKNOWN 内存大小 CLASS buffer static 核数 CPU

Device Year Class 的主要功能是根据 CPU核数、时钟频率 以及 内存大小 对设备进行分级。


下表是 Facebook 公司提供的分级标准,其中 Year 栏表示分级结果。



如何获取 Android 设备的CPU核数、时钟频率以及内存大小_sed




以下类就是从Facebook 的开源项目

Device Year Class中拿出来的

package com.yirui.youbao.util;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;

import java.io.*;
/**
 * Helper class for accessing hardware specifications, including the number of CPU cores, CPU clock speed
 * and total available RAM.
 */
public class DeviceInfo {

  /**
   * The default return value of any method in this class when an
   * error occurs or when processing fails (Currently set to -1). Use this to check if
   * the information about the device in question was successfully obtained.
   */
  public static final int DEVICEINFO_UNKNOWN = -1;

  /**
   * Reads the number of CPU cores from {@code /sys/devices/system/cpu/}.
   *
   * @return Number of CPU cores in the phone, or DEVICEINFO_UKNOWN = -1 in the event of an error.
   */
  public static int getNumberOfCPUCores() {
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
      // Gingerbread doesn't support giving a single application access to both cores, but a
      // handful of devices (Atrix 4G and Droid X2 for example) were released with a dual-core
      // chipset and Gingerbread; that can let an app in the background run without impacting
      // the foreground application. But for our purposes, it makes them single core.
      return 1;
    }
    int cores;
    try {
      cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;
    } catch (SecurityException e) {
      cores = DEVICEINFO_UNKNOWN;
    } catch (NullPointerException e) {
      cores = DEVICEINFO_UNKNOWN;
    }
    return cores;
  }

  private static final FileFilter CPU_FILTER = new FileFilter() {
    @Override
    public boolean accept(File pathname) {
      String path = pathname.getName();
      //regex is slow, so checking char by char.
      if (path.startsWith("cpu")) {
        for (int i = 3; i < path.length(); i++) {
          if (path.charAt(i) < '0' || path.charAt(i) > '9') {
            return false;
          }
        }
        return true;
      }
      return false;
    }
  };

  /**
   * Method for reading the clock speed of a CPU core on the device. Will read from either
   * {@code /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq} or {@code /proc/cpuinfo}.
   *
   * @return Clock speed of a core on the device, or -1 in the event of an error.
   */
  public static int getCPUMaxFreqKHz() {
    int maxFreq = DEVICEINFO_UNKNOWN;
    try {
      for (int i = 0; i < getNumberOfCPUCores(); i++) {
        String filename =
            "/sys/devices/system/cpu/cpu" + i + "/cpufreq/cpuinfo_max_freq";
        File cpuInfoMaxFreqFile = new File(filename);
        if (cpuInfoMaxFreqFile.exists()) {
          byte[] buffer = new byte[128];
          FileInputStream stream = new FileInputStream(cpuInfoMaxFreqFile);
          try {
            stream.read(buffer);
            int endIndex = 0;
            //Trim the first number out of the byte buffer.
            while (buffer[endIndex] >= '0' && buffer[endIndex] <= '9'
                && endIndex < buffer.length) endIndex++;
            String str = new String(buffer, 0, endIndex);
            Integer freqBound = Integer.parseInt(str);
            if (freqBound > maxFreq) maxFreq = freqBound;
          } catch (NumberFormatException e) {
            //Fall through and use /proc/cpuinfo.
          } finally {
            stream.close();
          }
        }
      }
      if (maxFreq == DEVICEINFO_UNKNOWN) {
        FileInputStream stream = new FileInputStream("/proc/cpuinfo");
        try {
          int freqBound = parseFileForValue("cpu MHz", stream);
          freqBound *= 1000; //MHz -> kHz
          if (freqBound > maxFreq) maxFreq = freqBound;
        } finally {
          stream.close();
        }
      }
    } catch (IOException e) {
      maxFreq = DEVICEINFO_UNKNOWN; //Fall through and return unknown.
    }
    return maxFreq;
  }

  /**
   * Calculates the total RAM of the device through Android API or /proc/meminfo.
   *
   * @param c - Context object for current running activity.
   * @return Total RAM that the device has, or DEVICEINFO_UNKNOWN = -1 in the event of an error.
   */
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  public static long getTotalMemory(Context c) {
    // memInfo.totalMem not supported in pre-Jelly Bean APIs.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
      ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
      am.getMemoryInfo(memInfo);
      if (memInfo != null) {
        return memInfo.totalMem;
      } else {
        return DEVICEINFO_UNKNOWN;
      }
    } else {
      long totalMem = DEVICEINFO_UNKNOWN;
      try {
        FileInputStream stream = new FileInputStream("/proc/meminfo");
        try {
          totalMem = parseFileForValue("MemTotal", stream);
          totalMem *= 1024;
        } finally {
          stream.close();
        }
      } catch (IOException e) {
      }
      return totalMem;
    }
  }

  /**
   * Helper method for reading values from system files, using a minimised buffer.
   *
   * @param textToMatch - Text in the system files to read for.
   * @param stream      - FileInputStream of the system file being read from.
   * @return A numerical value following textToMatch in specified the system file.
   * -1 in the event of a failure.
   */
  private static int parseFileForValue(String textToMatch, FileInputStream stream) {
    byte[] buffer = new byte[1024];
    try {
      int length = stream.read(buffer);
      for (int i = 0; i < length; i++) {
        if (buffer[i] == '\n' || i == 0) {
          if (buffer[i] == '\n') i++;
          for (int j = i; j < length; j++) {
            int textIndex = j - i;
            //Text doesn't match query at some point.
            if (buffer[j] != textToMatch.charAt(textIndex)) {
              break;
            }
            //Text matches query here.
            if (textIndex == textToMatch.length() - 1) {
              return extractValue(buffer, j);
            }
          }
        }
      }
    } catch (IOException e) {
      //Ignore any exceptions and fall through to return unknown value.
    } catch (NumberFormatException e) {
    }
    return DEVICEINFO_UNKNOWN;
  }

  /**
   * Helper method used by {@link #parseFileForValue(String, FileInputStream) parseFileForValue}. Parses
   * the next available number after the match in the file being read and returns it as an integer.
   * @param index - The index in the buffer array to begin looking.
   * @return The next number on that line in the buffer, returned as an int. Returns
   * DEVICEINFO_UNKNOWN = -1 in the event that no more numbers exist on the same line.
   */
  private static int extractValue(byte[] buffer, int index) {
    while (index < buffer.length && buffer[index] != '\n') {
      if (buffer[index] >= '0' && buffer[index] <= '9') {
        int start = index;
        index++;
        while (index < buffer.length && buffer[index] >= '0' && buffer[index] <= '9') {
          index++;
        }
        String str = new String(buffer, 0, start, index - start);
        return Integer.parseInt(str);
      }
      index++;
    }
    return DEVICEINFO_UNKNOWN;
  }
}




package com.yirui.youbao.util;

import java.util.ArrayList;
import java.util.Collections;

import android.content.Context;

public class YearClass {
  // Year definitions
  public static final int CLASS_UNKNOWN = -1;
  public static final int CLASS_2008 = 2008;
  public static final int CLASS_2009 = 2009;
  public static final int CLASS_2010 = 2010;
  public static final int CLASS_2011 = 2011;
  public static final int CLASS_2012 = 2012;
  public static final int CLASS_2013 = 2013;
  public static final int CLASS_2014 = 2014;

  private static final long MB = 1024 * 1024;
  private static final int MHZ_IN_KHZ = 1000;

  private volatile static Integer mYearCategory;

  /**
   * Entry Point of YearClass. Extracts YearClass variable with memoizing.
   * Example usage:
   * <p>
   * <pre>
   *   int yearClass = YearClass.get(context);
   * </pre>
   */
  public static int get(Context c) {
    if (mYearCategory == null) {
      synchronized(YearClass.class) {
        if (mYearCategory == null) {
          mYearCategory = categorizeByYear(c);
        }
      }
    }
    return mYearCategory;
  }

  private static void conditionallyAdd(ArrayList<Integer> list, int value) {
    if (value != CLASS_UNKNOWN) {
      list.add(value);
    }
  }

  /**
   * Calculates the "best-in-class year" of the device. This represents the top-end or flagship
   * devices of that year, not the actual release year of the phone. For example, the Galaxy Duos
   * S was released in 2012, but its specs are very similar to the Galaxy S that was released in
   * 2010 as a then top-of-the-line phone, so it is a 2010 device.
   *
   * @return The year when this device would have been considered top-of-the-line.
   */
  private static int categorizeByYear(Context c) {
    ArrayList<Integer> componentYears = new ArrayList<Integer>();
    conditionallyAdd(componentYears, getNumCoresYear());
    conditionallyAdd(componentYears, getClockSpeedYear());
    conditionallyAdd(componentYears, getRamYear(c));
    if (componentYears.isEmpty())
      return CLASS_UNKNOWN;
    Collections.sort(componentYears);
    if ((componentYears.size() & 0x01) == 1) {  // Odd number; pluck the median.
      return componentYears.get(componentYears.size() / 2);
    } else { // Even number. Average the two "center" values.
      int baseIndex = componentYears.size() / 2 - 1;
      // There's an implicit rounding down in here; 2011.5 becomes 2011.
      return componentYears.get(baseIndex) +
          (componentYears.get(baseIndex + 1) - componentYears.get(baseIndex)) / 2;
    }
  }

  /**
   * Calculates the year class by the number of processor cores the phone has.
   * Evaluations are based off the table below:
   * <table border="1">
   * <thead>
   * <tr><th width="50%">Amount</th><th>Year</th></tr>
   * <thead>
   * <tbody>
   * <tr><td>>4 or More</td><td>2012</td></tr>
   * <tr><td>2 or 3</td><td>2011</td></tr>
   * <tr><td>1</td><td>2008</td></tr>
   * </tbody>
   * </table>
   *
   * @return the year in which top-of-the-line phones had the same number of processors as this phone.
   */
  private static int getNumCoresYear() {
    int cores = DeviceInfo.getNumberOfCPUCores();
    if (cores < 1) return CLASS_UNKNOWN;
    if (cores == 1) return CLASS_2008;
    if (cores <= 3) return CLASS_2011;
    return CLASS_2012;
  }

  /**
   * Calculates the year class by the clock speed of the cores in the phone.
   * Evaluations are based off the table below:
   * <table border="1">
   * <thead>
   * <tr><th width="50%">Amount</th><th>Year</th></tr>
   * <thead>
   * <tbody>
   * <tr><td>>2GHz</td><td>2014</td></tr>
   * <tr><td><=2GHz</td><td>2013</td></tr>
   * <tr><td><=1.5GHz</td><td>2012</td></tr>
   * <tr><td><=1.2GHz</td><td>2011</td></tr>
   * <tr><td><=1GHz</td><td>2010</td></tr>
   * <tr><td><=600MHz</td><td>2009</td></tr>
   * <tr><td><=528MHz</td><td>2008</td></tr>
   * </tbody>
   * </table>
   *
   * @return the year in which top-of-the-line phones had the same clock speed.
   */
  private static int getClockSpeedYear() {
    long clockSpeedKHz = DeviceInfo.getCPUMaxFreqKHz();
    if (clockSpeedKHz == DeviceInfo.DEVICEINFO_UNKNOWN) return CLASS_UNKNOWN;
    // These cut-offs include 20MHz of "slop" because my "1.5GHz" Galaxy S3 reports
    // its clock speed as 1512000. So we add a little slop to keep things nominally correct.
    if (clockSpeedKHz <= 528 * MHZ_IN_KHZ) return CLASS_2008;
    if (clockSpeedKHz <= 620 * MHZ_IN_KHZ) return CLASS_2009;
    if (clockSpeedKHz <= 1020 * MHZ_IN_KHZ) return CLASS_2010;
    if (clockSpeedKHz <= 1220 * MHZ_IN_KHZ) return CLASS_2011;
    if (clockSpeedKHz <= 1520 * MHZ_IN_KHZ) return CLASS_2012;
    if (clockSpeedKHz <= 2020 * MHZ_IN_KHZ) return CLASS_2013;
    return CLASS_2014;
  }

  /**
   * Calculates the year class by the amount of RAM the phone has.
   * Evaluations are based off the table below:
   * <table border="1">
   * <thead>
   * <tr><th width="50%">Amount</th><th>Year</th></tr>
   * <thead>
   * <tbody>
   * <tr><td>>2GB</td><td>2014</td></tr>
   * <tr><td><=2GB</td><td>2013</td></tr>
   * <tr><td><=1.5GB</td><td>2012</td></tr>
   * <tr><td><=1GB</td><td>2011</td></tr>
   * <tr><td><=512MB</td><td>2010</td></tr>
   * <tr><td><=256MB</td><td>2009</td></tr>
   * <tr><td><=128MB</td><td>2008</td></tr>
   * </tbody>
   * </table>
   *
   * @return the year in which top-of-the-line phones had the same amount of RAM as this phone.
   */
  private static int getRamYear(Context c) {
    long totalRam = DeviceInfo.getTotalMemory(c);
    if (totalRam <= 0) return CLASS_UNKNOWN;
    if (totalRam <= 192 * MB) return CLASS_2008;
    if (totalRam <= 290 * MB) return CLASS_2009;
    if (totalRam <= 512 * MB) return CLASS_2010;
    if (totalRam <= 1024 * MB) return CLASS_2011;
    if (totalRam <= 1536 * MB) return CLASS_2012;
    if (totalRam <= 2048 * MB) return CLASS_2013;
    return CLASS_2014;
  }
}




使用方法:


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import com.facebook.device.yearclass.YearClass;

public class MainActivity extends Activity {
  private static final String PREF_FILE = "YearClass";
  private static final String PREF_NAME = "yearclass";
  private TextView mYearClass;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GetOrComputeYearClass findYearClass = new GetOrComputeYearClass();
    findYearClass.execute();
    mYearClass = (TextView) findViewById(R.id.year_class);
  }

  private class GetOrComputeYearClass extends AsyncTask<Void, Void, Integer> {

    @Override
    protected Integer doInBackground(Void... voids) {
      int yearClass = YearClass.CLASS_UNKNOWN;
      SharedPreferences prefs = getSharedPreferences(PREF_FILE, 0);
      if (prefs.contains(PREF_NAME)) {
        yearClass = prefs.getInt(PREF_NAME, YearClass.CLASS_UNKNOWN);
      }
      //Try again if device was previously unknown.
      if (yearClass == YearClass.CLASS_UNKNOWN) {
        yearClass = YearClass.get(getApplicationContext());
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(PREF_NAME, yearClass);
        editor.apply();
      }
      return yearClass;
    }

    @Override
    protected void onPostExecute(Integer yearClass) {
      //update UI
      mYearClass.setText(Integer.toString(yearClass));
    }
  }
}




运行在Android设备上,启动本程序之后,在任何窗口可显示当前CPU工作频率等状态,显示当前电压电流电池容量等信息


https://github.com/will86/android-runninginfo

  • 如何获取 Android 设备的CPU核数、时钟频率以及内存大小_i++_02

  • 大小: 15.8 KB
  • 查看图片附件

标签:return,int,UNKNOWN,内存大小,CLASS,buffer,static,核数,CPU
From: https://blog.51cto.com/u_5454003/6174214

相关文章

  • 保证高效写入查询的情况下,如何实现 CPU 资源和磁盘 IO 的最低开销?
    从《写入性能:TDengine最高达到InfluxDB的10.3倍,TimeScaleDB的6.74倍》、《查询性能:TDengine最高达到了InfluxDB的37倍、TimescaleDB的28.6倍》两篇文章中,我们发现,TDengine不仅在写入和查询性能上超越了InfluxDB和TimescaleDB,在数据处理过程的资源消耗也比两......
  • Win10(CPU)+ Anaconda3 + python3.9安装pytorch
    Win10(CPU)+Anaconda3+python3.9安装pytorch1.安装Anaconda31.1下载Anaconda3可以在官网下载Anaconda3-2022.05-Windows-x86_64.exe,这个版本对应的是python3.9。1.2安装Anaconda3此步骤比较简单,双击.exe文件,一步一步执行即可,有不确定的可以自行百度,教程比较多,但基本一致......
  • 混沌工程之ChaosMesh使用之一模拟CPU使用率
    今天来玩一下混沌工程的开源工具之一的ChaosMesh。ChaosMesh的目标是要做一个通用的混沌测试工具。ChaosMesh是要和k8s结合使用的,其中用了云原生的能力。 ChaosMesh的基本工作流原理图:从原理图上可以看出大概的工作流程:用户用yaml文件或k8s客户端创建更新Chaos对象。Chaos-mesh......
  • 为什么是至强Xeon处理器当做服务器CPU?他和普通台式CPU的区别
    为什么是至强Xeon处理器当做服务器CPU?他和普通台式CPU的区别相同的普通台式机CPU架构,Xeon处理器是高度受欢迎由于一些高级特性,比如更高的核心价值,支持更多的RAM,大的缓存内存和机器检查体系结构能够提供企业级的可靠性,可用性和可服务性特征负责处理硬件异常。现在,我们将回答......
  • 性能分析之dubbo性能参数导致单cpu高
    今天记录一个小问题。问题不大,也没什么分析的逻辑可以讲的。但是想着比较典型,所以就写一写。某年某月的某一天,就像一张破碎的脸......不对,串台了。这一日,一个朋友发来个问题。听起来是个问题。一个线程忙,这种情况应该比较好处理吧。再看一下CPU的状态是什么样,记住这一步是看进程......
  • linux内核数据结构 --- list_head
    以structkobject为例,讲解如何使用链表structlist_headstructkobject{constchar*name;structlist_headentry;structkobject*parent;...};structlist_head类型变量作为structkobject的成员(从面向对象的角度,也可以看成str......
  • Python 多线程死循环挂服务器时CPU占用过高问题
    我的某个程序里有这样一段代码,把程序挂在服务器爬取信息,因此用到死循环,同时又需要进行三个任务,于是使用了多线程。刚开始在死循环部分并没有加time.sleep(60),于是它一直在for循环,同时会进行.is_alive()(不确定这个消耗大不大),但总之这使得CPU占用过高。而加上sleep之后,直接就降下......
  • 《程序是怎样跑起来的》读书笔记1——对程序员来说CPU是什么
    一丶什么是程序程序是指令和数组的组合体,如:print("你好世界"),其中print是指令,你好世界是数据。CPU能直接识别和执行的只有机器语言,使用C,java这种高级语言编写的程序需要编译转换后才可以运行。二丶CPU的内部结构CPU即中央处理器,相当于计算机的大脑,内部由许多晶体管构成,负责解......
  • 一个循环采集CPU的etl日志的脚本
    一个循环采集CPU的etl日志的脚本mdD:\\tempsetTargetDriveEtl=D:\\temp@echooffSET/A"index=1"SET/A"count=10":whileif%index%leq%count%(echoThevalueofindexis%index%wmicprocesswherename="wprui.exe"......
  • Windows 环境以 CPU 运行 stable diffusion
    前言 stable-diffusion-webui要求的Python版本是3.10.6,本机还是几年前装的3.10.0,为了避免处理更多幺蛾子,直接升级到3.10.6,还好之前就是3.10,可以直接升级。还有一个好处就是不用安装conda或者miniconda,Python虚拟环境直接就是3.10.6。其实3.10其他小版本的环......