怎么获得CPU的信息

怎么获得CPU的信息,第1张

Android获取cpu和内存信息、网址的代码如下:

/ 获取用户硬件信息 /

public static String getMobileInfo() {

//StringBuffer sb = new StringBuffer();

JSONObject mbInfo = new JSONObject();

//通过反射获取用户硬件信息

try {

Field[] fields = BuildclassgetDeclaredFields();

for (Field field : fields) {

// 暴力反射,获取私有信息

fieldsetAccessible(true);

String name = fieldgetName();

String value = fieldget(null)toString();

//sbappend(name + "=" + value);

//sbappend("\n");

mbInfoput(name, value);

}

} catch (Exception e) {

eprintStackTrace();

}

//return sbtoString();

return mbInfotoString();

}

static public String getCpuString(){

if(BuildCPU_ABIequalsIgnoreCase("x86")){

return "Intel";

}

String strInfo = "";

try

{

byte[] bs = new byte[1024];

RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r");

readerread(bs);

String ret = new String(bs);

int index = retindexOf(0);

if(index != -1) {

strInfo = retsubstring(0, index);

} else {

strInfo = ret;

}

}

catch (IOException ex){

exprintStackTrace();

}

return strInfo;

}

static public String getCpuType(){

String strInfo = getCpuString();

String strType = null;

if (strInfocontains("ARMv5")) {

strType = "armv5";

} else if (strInfocontains("ARMv6")) {

strType = "armv6";

} else if (strInfocontains("ARMv7")) {

strType = "armv7";

} else if (strInfocontains("Intel")){

strType = "x86";

}else{

strType = "unknown";

return strType;

}

if (strInfocontains("neon")) {

strType += "_neon";

}else if (strInfocontains("vfpv3")) {

strType += "_vfpv3";

}else if (strInfocontains(" vfp")) {

strType += "_vfp";

}else{

strType += "_none";

}

return strType;

}

/

@hide

@return

/

public static CPUInfo getCPUInfo() {

String strInfo = null;

try

{

byte[] bs = new byte[1024];

RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r");

readerread(bs);

String ret = new String(bs);

int index = retindexOf(0);

if(index != -1) {

strInfo = retsubstring(0, index);

} else {

strInfo = ret;

}

}

catch (IOException ex)

{

strInfo = "";

exprintStackTrace();

}

CPUInfo info = parseCPUInfo(strInfo);

infomCPUMaxFreq = getMaxCpuFreq();

return info;

}

private final static String kCpuInfoMaxFreqFilePath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";

private static int getMaxCpuFreq() {

int result = 0;

FileReader fr = null;

BufferedReader br = null;

try {

fr = new FileReader(kCpuInfoMaxFreqFilePath);

br = new BufferedReader(fr);

String text = brreadLine();

if (text != null) {

result = IntegerparseInt(texttrim());

}

} catch (FileNotFoundException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

} finally {

if (fr != null)

try {

frclose();

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

if (br != null)

try {

brclose();

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

return result;

}

public static class CPUInfo{

public CPUInfo(){

}

public static final int CPU_TYPE_UNKNOWN = 0x00000000;

public static final int CPU_TYPE_ARMV5TE = 0x00000001;

public static final int CPU_TYPE_ARMV6 = 0x00000010;

public static final int CPU_TYPE_ARMV7 = 0x00000100;

public static final int CPU_FEATURE_UNKNOWS = 0x00000000;

public static final int CPU_FEATURE_VFP = 0x00000001;

public static final int CPU_FEATURE_VFPV3 = 0x00000010;

public static final int CPU_FEATURE_NEON = 0x00000100;

public int mCPUType;

public int mCPUCount;

public int mCPUFeature;

public double mBogoMips;

public long mCPUMaxFreq;

}

/

@param cpuInfo

@return

@hide

/

private static CPUInfo parseCPUInfo(String cpuInfo) {

if (cpuInfo == null || ""equals(cpuInfo)) {

return null;

}

CPUInfo ci = new CPUInfo();

cimCPUType = CPUInfoCPU_TYPE_UNKNOWN;

cimCPUFeature = CPUInfoCPU_FEATURE_UNKNOWS;

cimCPUCount = 1;

cimBogoMips = 0;

if (cpuInfocontains("ARMv5")) {

cimCPUType = CPUInfoCPU_TYPE_ARMV5TE;

} else if (cpuInfocontains("ARMv6")) {

cimCPUType = CPUInfoCPU_TYPE_ARMV6;

} else if (cpuInfocontains("ARMv7")) {

cimCPUType = CPUInfoCPU_TYPE_ARMV7;

}

if (cpuInfocontains("neon")) {

cimCPUFeature |= CPUInfoCPU_FEATURE_NEON;

}

if (cpuInfocontains("vfpv3")) {

cimCPUFeature |= CPUInfoCPU_FEATURE_VFPV3;

}

if (cpuInfocontains(" vfp")) {

cimCPUFeature |= CPUInfoCPU_FEATURE_VFP;

}

String[] items = cpuInfosplit("\n");

for (String item : items) {

if (itemcontains("CPU variant")) {

int index = itemindexOf(": ");

if (index >= 0) {

String value = itemsubstring(index + 2);

try {

cimCPUCount = Integerdecode(value);

cimCPUCount = cimCPUCount == 0 1 : cimCPUCount;

} catch (NumberFormatException e) {

cimCPUCount = 1;

}

}

} else if (itemcontains("BogoMIPS")) {

int index = itemindexOf(": ");

if (index >= 0) {

String value = itemsubstring(index + 2);

}

}

}

return ci;

}

/

获取设备内存大小值

@return 内存大小,单位MB

/

public static long getTotalMemory() {

String str1 = "/proc/meminfo";

String str2;

String[] arrayOfString;

long initial_memory = 0;

try {

FileReader localFileReader = new FileReader(str1);

BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);

str2 = localBufferedReaderreadLine();

if (str2 != null) {

arrayOfString = str2split("\\s+");

initial_memory = IntegervalueOf(arrayOfString[1])intValue()/1024;

}

localBufferedReaderclose();

return initial_memory;

}

catch (IOException e)

{

return -1;

}

}

/

@hide

@return

/

public CPUInfo getCPUInfo() {

String strInfo = null;

try

{

byte[] bs = new byte[1024];

RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r");

readerread(bs);

String ret = new String(bs);

int index = retindexOf(0);

if(index != -1) {

strInfo = retsubstring(0, index);

} else {

strInfo = ret;

}

}

catch (IOException ex)

{

strInfo = "";

exprintStackTrace();

}

CPUInfo info = parseCPUInfo(strInfo);

infomCPUMaxFreq = getMaxCpuFreq();

return info;

}

/

获取android CPU类型

@return String CPU类型

/

public static String getCpuModel(){

String cpu_model = "";

CPUInfo in = getCPUInfo();

if ((inmCPUType & CPUInfoCPU_TYPE_ARMV5TE) == CPUInfoCPU_TYPE_ARMV5TE)

cpu_model="armv5";

else if ((inmCPUType & CPUInfoCPU_TYPE_ARMV6) == CPUInfoCPU_TYPE_ARMV6)

cpu_model="armv6";

else if ((inmCPUType & CPUInfoCPU_TYPE_ARMV7) == CPUInfoCPU_TYPE_ARMV7)

cpu_model="armv7";

else

cpu_model="unknown";

return cpu_model;

}

/

获取android CPU特性

@return String CPU特性

/

public static String getCpuFeature(){

String cpu_feature = "";

CPUInfo in = getCPUInfo();

if ((inmCPUFeature & CPUInfoCPU_FEATURE_NEON ) == CPUInfoCPU_FEATURE_NEON)

cpu_feature="neon";

else if ((inmCPUFeature & CPUInfoCPU_FEATURE_VFP ) == CPUInfoCPU_FEATURE_VFP)

cpu_feature="vfp";

else if ((inmCPUFeature & CPUInfoCPU_FEATURE_VFPV3 ) == CPUInfoCPU_FEATURE_VFPV3)

cpu_feature="vfpv3";

else

cpu_feature="unknown";

return cpu_feature;

}

/

获取ip地址

@param mContext Context

@return ip地址字符串

/

public static String getIpAddress(Context mContext) {

String ipAddress = null;

try {

for (Enumeration en = NetworkInterface

getNetworkInterfaces(); enhasMoreElements();) {

NetworkInterface intf = ennextElement();

for (Enumeration enumIpAddr = intf

getInetAddresses(); enumIpAddrhasMoreElements();) {

InetAddress inetAddress = enumIpAddrnextElement();

if (!inetAddressisLoopbackAddress()) {

ipAddress = inetAddressgetHostAddress()toString();

}

}

}

} catch (SocketException ex) {

return null;

}

if (DEBUG) {

Logd(TAG, "ip address:" + ipAddress);

}

return ipAddress;

}

1、制作工艺

通常我们所说的CPU的“制作工艺”指的是在生产CPU过程中,要进行加工各种电路和电子元件,制造导线连接各个元器件。

通常其生产的精度以微米(长度单位,1微米等于千分之一毫米)来表示,未来有向纳米(1纳米等于千分之一微米)发展的趋势,精度越高,生产工艺越先进。在同样的材料中可以制造更多的电子元件,连接线也越细,提高CPU的集成度,CPU的功耗也越小。

2、接口类型

CPU需要通过某个接口与主板连接的才能进行工作。

CPU经过这么多年的发展,采用的接口方式有引脚式、卡式、触点式、针脚式等。而目前CPU的接口都是针脚式接口,对应到主板上就有相应的插槽类型。CPU接口类型不同,在插孔数、体积、形状都有变化,所以不能互相接插。

3、主频

主频即CPU的时钟频率,计算机的 *** 作在时钟信号的控制下分步执行,每个时钟信号周期完成一步 *** 作,时钟频率的高低在很大程度上反映了CPU速度的快慢。

主频和实际的运算速度存在一定的关系,但并不是一个简单的线性关系。主频表示在CPU内数字脉冲信号震荡的速度,CPU的运算速度还要看CPU的流水线、总线等各方面的性能指标。也就是说,主频仅仅是CPU性能表现的一个方面,而不代表CPU的整体性能。

4、CPU线程

多线程,是指从软件或者硬件上实现多个线程并发执行的技术。具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能。

具有这种能力的系统包括对称多处理机、多核心处理器以及芯片级多处理或同时多线程处理器。

5、CPU缓存

CPU缓存位于CPU与内存之间的临时存储器,它的容量比内存小但交换速度快。在缓存中的数据是内存中的一小部分,但这一小部分是短时间内CPU即将访问的,当CPU调用大量数据时,就可避开内存直接从缓存中调用,从而加快读取速度。

由此可见,在CPU中加入缓存是一种高效的解决方案,这样整个内存储器(缓存+内存)就变成了既有缓存的高速度,又有内存的大容量的存储系统了。缓存对CPU的性能影响很大,主要是因为CPU的数据交换顺序和CPU与缓存间的带宽引起的。

6、CPU核心

核心又称为内核,是CPU最重要的组成部分。

CPU中心那块隆起的芯片就是核心,是由单晶硅以一定的生产工艺制造出来的,CPU所有的计算、接受/存储命令、处理数据都由核心执行。各种CPU核心都具有固定的逻辑结构,一级缓存、二级缓存、执行单元、指令级单元和总线接口等逻辑单元都会有科学的布局。

用微米表示:

通常我们所说的CPU的“制作工艺”指得是在生产CPU过程中,要进行加工各种电路和电子元件,制造导线连接各个元器件。通常其生产的精度以微米(长度单位,1微米等于千分之一毫米)来表示,未来有向纳米(1纳米等于千分之一微米)发展的趋势,精度越高,生产工艺越先进。在同样的材料中可以制造更多的电子元件,连接线也越细,提高CPU的集成度,CPU的功耗也越小。

方法1: 使用CPU的处理能力基准计算实时CPU占用率

具体描述:

(1) 在RTOS系统启动前, 使用Tick中断测试CPU的处理能力基准 CPUPerformanceBase;

(2) 在系统进入运行后, 使用空闲任务执行与测试CPU处理能力基准完全相同的算法, 得到RTCPUPerformance

(3) 周期地计算CPU占用率, 并清除RTCPUPerformance的值, 一般每秒钟计算一次:

RealTime CPU Load = 1 - (RTCPUPerformance/CPUPerformanceBase) 100%

评价:

这个算法只适用于工控, 电信等对不需要使CPU进入掉电保护模式的领域。

方法2: 在Tick中断中对RTOS中的任务进行采样

具体描述:

(1) 系统进入运行后, 每次Tick中断发生时, 采样一下当前正在执行的任务, 如果CPU处于HALT态, 累加haltTimes

(2) 周期性地计算CPU占用率, 一般每秒钟计算一次, 并清除haltTimes:(tickIntFrequance表示Tick中断的发生频率)

RealTime CPU Load = haltTimes / tickIntFrequance

某个任务对CPU占用率的贡献 = 一个周期内该任务被采样到的次数 / tickIntFrequance 100%

评价:

这个算法适用于对CPU占用率精度要求不高的消息电子产品。

方法3: 精确计算每个任务对CPU占用率的贡献

具体描述:

(1) 除Tick中断外,另开一个比Tick中断频率快若干倍的周期中断(就叫AUXTimer中断吧), 这个中断只对一个计数器执行一次累加。

(2) 在OS每次执行任务切换时读取该计数器的值(AUXTimer), 并保存到TCB中, 比如, 从任务Task1切换到任务Task2, 算法如下:

Task1, 换出动作:

task1的结束运行时间 = AUXTimer的当前值

task1的总运行时间 = task1的总运行时间 + task1的结束运行时间 - task1的开始运行时间

Task2, 换入动作:

task2的开始运行时间 = AUXTimer的当前值

(以上算法中没有考虑数字回绕, 在工程实现时应当考虑, 发生回绕后任务的结束运行时间小于任务的开始运行时间。

(3) 周期性地计算CPU占用率, 一般每秒钟计算一次, 并清除每个任务的总运行时间, 下面的公式中, 一个周期内的总时间等于AUXTimer周期除以Tick周期得到的倍数:

某个任务对CPU占用率的贡献 = 一个周期内该任务的总运行时间 / 一个周期内的总时间

RealTime CPU Load = 所有任务的CPU占用率之和

以上就是关于怎么获得CPU的信息全部的内容,包括:怎么获得CPU的信息、详细解读CPU各种参数、cpu生产的精准度通常以什么来表示等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存