android获取cpu核数

android获取cpu核数,第1张

因为Android本质上是linux系统,所以关系硬件相关的信息实际上是用文件表示的

CPU相关信息放在 /sys/devices/system/cpu/ 这个路径下,

要知道cpu的核数只需要查看这个路径中的文件即可

如果CPU是双核的,则 /sys/devices/system/cpu/ 路径下有两个子文件夹 cpu0 和 cpu1

如果CPU是四核的,则 /sys/devices/system/cpu/ 路径下有四个子文件夹 cpu0 和 cpu1 和 cpu2 和 cpu3

以此类推

这个是抄来的

Device Year Class 的主要功能是根据 CPU核数、时钟频率 以及 内存大小 对设备进行分级。代码很简单,只包含两个类:

DeviceInfo-> 获取设备参数,

YearClass-> 根据参数进行分级。

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

Year Cores Clock RAM

2008 1 528MHz 192MB

2009 n/a 600MHz 290MB

2010 n/a 10GHz 512MB

2011 2 12GHz 1GB

2012 4 15GHz 15GB

2013 n/a 20GHz 2GB

2014 n/a >2GHz >2GB

关于输出年份的计算方法可以参考源码,本文只把一些比较常用的功能抽取出来做一个简要介绍。

获取 CPU 核数

我们都知道,Linux 中的设备都是以文件的形式存在,CPU 也不例外,因此 CPU 的文件个数就等价与核数。

Android 的 CPU 设备文件位于/sys/devices/system/cpu/目录,文件名的的格式为cpu\d+。

1

2

3

4

5

6

7

8

9

10

root@generic_x86_64:/sys/devices/system/cpu # ls <b>cpu0</b> cpufreq

cpuidle

kernel_max

modalias

offline

online

possible

power

present

uevent

统计一下文件个数便可以获得 CPU 核数。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

public static int getNumberOfCPUCores() {

if (BuildVERSIONSDK_INT <= BuildVERSION_CODESGINGERBREAD_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 = pathnamegetName();

//regex is slow, so checking char by char

if (pathstartsWith("cpu")) {

for (int i = 3; i < pathlength(); i++) {

if (pathcharAt(i) < '0' || pathcharAt(i) > '9') {

return false;

}

}

return true;

}

return false;

}

};

回到顶部

获取时钟频率

获取时钟频率需要读取系统文件 -/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq或者/proc/cpuinfo。

我的 Android 模拟器中并没有cpuinfo_max_freq文件,因此只能读取/proc/cpuinfo。

/proc/cpuinfo包含了很多 cpu 数据。

processor : 0

vendor_id : GenuineIntel

cpu family : 6

model : 70

model name : Intel(R) Core(TM) i7-4770HQ CPU @ 220GHz

stepping : 1

cpu MHz : 0000

cache size : 1024 KB

fdiv_bug : no

hlt_bug : no

f00f_bug : no

coma_bug : no

fpu : yes

fpu_exception : yes

cpuid level : 4

wp : yes

代码如下:

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 (cpuInfoMaxFreqFileexists()) {

byte[] buffer = new byte[128];

FileInputStream stream = new FileInputStream(cpuInfoMaxFreqFile);

try {

streamread(buffer);

int endIndex = 0;

//Trim the first number out of the byte buffer

while (buffer[endIndex] >= '0' && buffer[endIndex] <= '9'

&& endIndex < bufferlength) endIndex++;

String str = new String(buffer, 0, endIndex);

Integer freqBound = IntegerparseInt(str);

if (freqBound > maxFreq) maxFreq = freqBound;

} catch (NumberFormatException e) {

//Fall through and use /proc/cpuinfo

} finally {

streamclose();

}

}

}

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 {

streamclose();

}

}

} catch (IOException e) {

maxFreq = DEVICEINFO_UNKNOWN; //Fall through and return unknown

}

return maxFreq;

}

回到顶部

获取内存大小

如果 SDK 版本大于等于JELLY_BEAN,可以通过ActivityManager来获取内从大小。

 

ActivityManagerMemoryInfo memInfo = new ActivityManagerMemoryInfo();

ActivityManager am = (ActivityManager) cgetSystemService(ContextACTIVITY_SERVICE);

amgetMemoryInfo(memInfo);

如果版本低于JELLY_BEAN,则只能读取系统文件了。

FileInputStream stream = new FileInputStream("/proc/meminfo");

totalMem = parseFileForValue("MemTotal", stream);

完整代码如下:

 

@TargetApi(BuildVERSION_CODESJELLY_BEAN)

public static long getTotalMemory(Context c) {

// memInfototalMem not supported in pre-Jelly Bean APIs

if (BuildVERSIONSDK_INT >= BuildVERSION_CODESJELLY_BEAN) {

ActivityManagerMemoryInfo memInfo = new ActivityManagerMemoryInfo();

ActivityManager am = (ActivityManager) cgetSystemService(ContextACTIVITY_SERVICE);

amgetMemoryInfo(memInfo);

if (memInfo != null) {

return memInfototalMem;

} else {

return DEVICEINFO_UNKNOWN;

}

} else {

long totalMem = DEVICEINFO_UNKNOWN;

try {

FileInputStream stream = new FileInputStream("/proc/meminfo");

try {

totalMem = parseFileForValue("MemTotal", stream);

totalMem = 1024;

} finally {

streamclose();

}

} catch (IOException e) {

}

return totalMem;

}

}

android的Cpu信息是存在/sys/devices/system/cpu中的,在目录中,我们可以看到存在多个文件,一个文件就是一核Cpu的信息。上面写有cpu0,cpu1,cup3诸如此类的文件夹。

要获得它是几核的Cpu,只要读取一下那个cpu目录下有几个cpu+数字的文件夹就可以了。

/

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 1 if failed to get result

/

private int getNumCores() {

//Private Class to display only CPU devices in the directory listing

class CpuFilter implements FileFilter {

@Override

public boolean accept(File pathname) {

//Check if filename is "cpu", followed by a single digit number

if(Patternmatches("cpu[0-9]", pathnamegetName())) {

return true;

}

return false;

}

}

try {

//Get directory containing CPU info

File dir = new File("/sys/devices/system/cpu/");

//Filter to only list the devices we care about

File[] files = dirlistFiles(new CpuFilter());

//Return the number of cores (virtual CPU devices)

return fileslength;

} catch(Exception e) {

//Default to return 1 core

return 1;

}

}

MODEL 应该就是吧

Logd("build","BOARD:" + BuildBOARD);

Logd("build","BOOTLOADER:" + BuildBOOTLOADER);

Logd("build","BRAND:" + BuildBRAND);

Logd("build","CPU_ABI:" + BuildCPU_ABI);

Logd("build","CPU_ABI2:" + BuildCPU_ABI2);

Logd("build","DEVICE:" + BuildDEVICE);

Logd("build","DISPLAY:" + BuildDISPLAY);

Logd("build","FINGERPRINT:" + BuildFINGERPRINT);

Logd("build","HARDWARE:" + BuildHARDWARE);

Logd("build","HOST:" + BuildHOST);

Logd("build","ID:" + BuildID);

Logd("build","MANUFACTURER:" + BuildMANUFACTURER);

Logd("build","MODEL:" + BuildMODEL);

Logd("build","PRODUCT:" + BuildPRODUCT);

Logd("build","RADIO:" + BuildRADIO);

Logd("build","TAGS:" + BuildTAGS);

Logd("build","TIME:" + BuildTIME);

Logd("build","TYPE:" + BuildTYPE);

Logd("build","UNKNOWN:" + BuildUNKNOWN);

Logd("build","USER:" + BuildUSER);

Logd("build","VERSIONCODENAME:" + BuildVERSIONCODENAME);

Logd("build","VERSIONINCREMENTAL:" + BuildVERSIONINCREMENTAL);

Logd("build","VERSIONRELEASE:" + BuildVERSIONRELEASE);

Logd("build","VERSIONSDK:" + BuildVERSIONSDK);

Logd("build","VERSIONSDK_INT:" + BuildVERSIONSDK_INT);

以上就是关于android获取cpu核数全部的内容,包括:android获取cpu核数、如何获取 Android 设备的CPU核数,时钟频率以及内存大小、android获取手机cpu并判断是单核还是多核等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9698266.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存