FILE fp = popen(Shell命令,"r") 这样用popen来输入shell命令,fp取到的就是输入这个命令后的信息了,关于查看CPU状态的命令,查linux命令大全。
获取 CPU 核数
Linux 中的设备都是以文件的形式存在,CPU 也不例外,因此 CPU 的文件个数就等价与核数。
Android 的 CPU 设备文件位于/sys/devices/system/cpu/目录,文件名的的格式为cpu\d+。
root@generic_x86_64:/sys/devices/system/cpu # ls cpu0 cpufreq
cpuidle
kernel_max
modalias
offline
online
possible
power
present
uevent
统计一下文件个数便可以获得 CPU 核数。
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;
}
}
1、打开电脑,在电脑桌面上找到左下方的开始菜单按钮,点击右键。
2、在d出的菜单中,找到任务管理器选项,点击打开。
3、在任务管理器界面中,找到性能选项框,点击切换。
4、在该选项框中,即可看到CPU的核心数以及相关的信息。
以上就是关于在linux中如何使用qt来编写C++代码来找出CPU的使用情况全部的内容,包括:在linux中如何使用qt来编写C++代码来找出CPU的使用情况、如何获取 Android 设备的CPU核数,时钟频率以及内存大小、如何看CPU几核几核的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)