Android如何获取当前CPU频率和占用率
最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:
目前没有标准的 API 来获取 CPU 的使用频率,只能通过读取指定 CPU 文件获取当前 CPU 频率,在某些机器或者特定版本中,可能需要ROOT 权限或者特殊权限,因此会存在一定几率的失败,因此需要做好 Try…catch 动作。又因为现在手机 CPU 的多核数目,因此我们可能需要获取多个 CPU 频率数,并取平均值。
获取系统 CPU 核心数:
val cpuCoreNum = Runtime.getRuntime().availableProcessors()
获取指定 CPU 当前频率:
/sys/devices/system/cpu/cpu${index}/cpufreq/scaling_cur_freq
那么核心代码为:
private fun getAllCpuCoreFrequency() : Long {
var frequency = 0L
for (index in 0 until cpuCoreNum){
frequency += readFile("/sys/devices/system/cpu/cpu$index/cpufreq/scaling_cur_freq")
}
BLog.d("frequency : $frequency")
return frequency / cpuCoreNum
}
private fun readFile(filePath: String): Long{
try {
val file = RandomAccessFile(filePath, "r")
val content = file.readLine()
file.close()
if (TextUtils.isEmpty(content)){
return 0L
}
BLog.d("readFile content : $content")
return content.trim().toLong()
}catch (e : Exception){
e.printStackTrace()
return 0L
}
}如果需要获取 CPU 的占用率,那么就需要知道每个核心的最大频率和最小频率,同样是通过文件获取:
//max frequency file
/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_max_freq
//min frequency file
/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_min_freq
那么核心代码为:
object CPUUtils {
private var cpuCoreNum = 0
private var cpuMaxFrequency = 0L
private var cpuMinFrequency = 0L
fun initCpuCoreNum(){
if (cpuCoreNum <= 0 || cpuMaxFrequency <= 0L || cpuMinFrequency <= 0L){
cpuCoreNum = Runtime.getRuntime().availableProcessors()
initMaxAndMinFrequency()
if (cpuCoreNum > 0 && cpuMaxFrequency > 0L && cpuMinFrequency > 0L){
SpManager.getInstance().setCanUseCPUFrequency(true)
}
}
BLog.d("cpuCoreNum : $cpuCoreNum")
}
private fun initMaxAndMinFrequency() {
if (cpuCoreNum <= 0){
return
}
cpuMaxFrequency = 0L
cpuMinFrequency = 0L
for (index in 0 until cpuCoreNum){
cpuMaxFrequency += readFile("/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_max_freq")
cpuMinFrequency += readFile("/sys/devices/system/cpu/cpu${index}/cpufreq/cpuinfo_min_freq")
}
BLog.d("cpuMaxFrequency : $cpuMaxFrequency, cpuMinFrequency : $cpuMinFrequency")
}
private fun readFile(filePath: String): Long{
try {
val file = RandomAccessFile(filePath, "r")
val content = file.readLine()
file.close()
if (TextUtils.isEmpty(content)){
return 0L
}
BLog.d("readFile content : $content")
return content.trim().toLong()
}catch (e : Exception){
ExceptionHandler.recordException(e)
return 0L
}
}
private fun getAllCpuCoreFrequency() : Long {
initCpuCoreNum()
if (cpuCoreNum <=0){
return 0L
}
var frequency = 0L
for (index in 0 until cpuCoreNum){
frequency += readFile("/sys/devices/system/cpu/cpu$index/cpufreq/scaling_cur_freq")
}
BLog.d("frequency : $frequency")
return frequency
}
fun findCurrentFrequencyPercent() : Long {
val currentFrequency = getAllCpuCoreFrequency()
BLog.d("currentFrequency : $currentFrequency, cpuMinFrequency : $cpuMinFrequency, cpuMaxFrequency : $cpuMaxFrequency")
if (cpuMaxFrequency - cpuMinFrequency <= 0L || currentFrequency - cpuMinFrequency < 0L || cpuMaxFrequency - currentFrequency < 0L){
return 0L
}
return (currentFrequency - cpuMinFrequency) * 100 / (cpuMaxFrequency - cpuMinFrequency)
}
fun getCpuCoreFrequency() : Long {
initCpuCoreNum()
if (cpuCoreNum <=0){
return 0L
}
return getAllCpuCoreFrequency() / cpuCoreNum
}
}
获取 CPU 频率:
CPUUtils.getCpuCoreFrequency()
获取 CPU 占用率:
CPUtils.findCurrentFrequencyPercent()
到此这篇关于Android如何获取当前CPU频率和占用率的文章就介绍到这了,更多相关Android获取CPU频率和占用率内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Android百度定位导航之基于百度地图移动获取位置和自动定位
项目需求是这样的,首先定位我当前的起始位置,并跟随移动不断自动定位我的当前位置,下面通过本文给大家介绍android百度定位导航之基于百度地图移动获取位置和自动定位,需要的朋友参考下2016-01-01
Kotlin语言使用BroadcastReceiver示例介绍
Android开发的四大组件分别是:活动(activity),用于表现功能;服务(service),后台运行服务,不提供界面呈现;广播接受者(Broadcast Receive),勇于接收广播;内容提供者(Content Provider),支持多个应用中存储和读取数据,相当于数据库,本篇着重介绍广播组件2022-09-09
仿iPhone风格对话框(附件包含例子/jar包/jar包源码)
这个对框完全继承、仿照AlertDialog,只是实现了自定义效果;另外,没有实现setIcon,因为iphone中的对话框多数都没有图标;附件包含例子、jar包、jar包源码2013-01-01


最新评论