Android如何獲取當前CPU頻率和占用率
最近在優(yōu)化 App 的性能,需要獲取當前 CPU視頻頻率和占用率,通過查詢資料,大致思路如下:
目前沒有標準的 API 來獲取 CPU 的使用頻率,只能通過讀取指定 CPU 文件獲取當前 CPU 頻率,在某些機器或者特定版本中,可能需要ROOT 權限或者特殊權限,因此會存在一定幾率的失敗,因此需要做好 Try…catch 動作。又因為現(xiàn)在手機 CPU 的多核數目,因此我們可能需要獲取多個 CPU 頻率數,并取平均值。
獲取系統(tǒng) 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頻率和占用率內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android應用中使用ViewPager實現(xiàn)類似QQ的界面切換效果
這篇文章主要介紹了Android應用中使用ViewPager實現(xiàn)類似QQ的界面切換效果的示例,文中的例子重寫了PagerAdapter,并且講解了如何解決Android下ViewPager和PagerAdapter中調用notifyDataSetChanged失效的問題,需要的朋友可以參考下2016-03-03
Android App在線程中創(chuàng)建handler的方法講解
這篇文章主要介紹了Android App在線程中創(chuàng)建handler的方法講解,文中同時講解了handler和線程的關系以及使用Handler時一些需要注意的地方,需要的朋友可以參考下2016-03-03
Android TextView前增加紅色必填項星號*的示例代碼
TextView是一個完整的文本編輯器,但是基類為不允許編輯,其子類EditText允許文本編輯,這篇文章主要介紹了Android TextView前增加紅色必填項星號*的示例代碼,需要的朋友可以參考下2024-03-03
Android UI設計與開發(fā)之ViewPager介紹和簡單實現(xiàn)引導界面
這篇文章主要為大家詳細介紹了Android UI設計與開發(fā)之ViewPager介紹和簡單實現(xiàn)引導界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

