【发布时间】:2019-06-26 19:14:58
【问题描述】:
我想了解(所有内核的)CPU 使用率
目前我无法获取“/proc/stat”的信息(自 API 27 起)
所以我想以频率或类似的频率获取信息:
使用所有以前的答案,我得到了我想要的,但还不够
主函数
private fun ReadCpu3():String{
val sb = StringBuffer();
sb.append("abi: ").append(Build.CPU_ABI).append("\n");
if (File("/proc/cpuinfo").exists()) {
try {
//val br = BufferedReader(FileReader(File("/proc/cpuinfo")));
val file = File("/proc/cpuinfo")
file.bufferedReader().forEachLine {
sb.append(it+"\n");
}
} catch (e: IOException) {
e.printStackTrace();
}
}
return sb.toString();
}
我的结果
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 45
model name : Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz
stepping : 6
microcode : 1561
cpu MHz : 600.000
cache size : 20480 KB
physical id : 0
siblings : 16
core id : 0
cpu cores : 8
apicid : 0
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx lahf_lm ida arat epb pln pts dtherm tpr_shadow vnmi flexpriority ept vpid xsaveopt
bogomips : 4399.93
clflush size : 64
cache_alignment : 64
address sizes : 46 bits physical, 48 bits virtual
power management:
所以我得到了 CPU 的所有信息,但没有得到实际负载或使用情况。我不知道我是否可以通过当前信息来了解 cpu 使用情况以及准确程度。
【问题讨论】:
-
当您想询问 kotlin 时,为什么要使用 java 标记。而且我认为 kotlin 遵循类似的命名约定:变量名应该以 camelCase 开头。大写用于类名。另请注意:垂直间距(空行)的目的是对事物进行分组。你不只是在这里或那里扔掉空行,因为你可以。想象一下,如果你的代码被编写出来会是什么感觉……就像你关心它一样?!
-
@GhostCat 谢谢你的建议,我更正了:)
-
@Divergence 但是我只想要 CPU 负载,而我在这个 awnser 中它返回一个完整的信息文本视图,你能告诉我我该怎么做才能获得 CPU 负载使用这段代码作为基础?
-
@WhySoBizarreCode 那么请附上它返回的示例。