当前位置: 首页 > news >正文

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 = 0Lfor (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 = 0private var cpuMaxFrequency = 0Lprivate var cpuMinFrequency = 0Lfun 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 = 0LcpuMinFrequency = 0Lfor (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 = 0Lfor (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()
http://www.lryc.cn/news/460165.html

相关文章:

  • pymobiledevice3使用介绍(安装、常用命令、访问iOS沙盒目录)
  • python 爬虫模拟登录
  • AOP基础、快速入门、进阶
  • 哪款宠物空净运行吸毛好、噪音小?希喂、霍尼韦尔、安德迈测评!
  • 新兴的安全职业挑战
  • 代码随想录算法训练营Day32 | 122.买卖股票的最佳时机Ⅱ、55.跳跃游戏、45.跳跃游戏Ⅱ、1005.K次取反后最大化的数组和
  • 3D Slicer 教程一
  • github pages + hugo 搭建静态博客网站
  • Python爬虫如何爬取并解析JSON数据
  • 【C++】精妙的哈希算法
  • 智慧链动青春:国家区块链中心接待北京市十一学校青少年访学探索
  • 利用C++封装鼠标轨迹算法为DLL:游戏行为检测的利器
  • Qt- QSS风格选择器常用属性选择器样式表盒子
  • 粤智助自助一体机大厂浮出水面 OBOO鸥柏已成服务终端中坚力量
  • SpringBoot-application.properties配置
  • STM32-ADC模数转换
  • lspci | grep VGA
  • 智慧厂区车辆导航解决方案;智慧工厂电子地图应用解决方案;大型工厂内部导航解决方案;智慧工厂可视化地图应用方案
  • 决策树C4.5算法详解及实现
  • prompt learning
  • 适用于 Windows 11 的 5 大数据恢复软件 [免费和付费]
  • vue实现获取当前时间并实时显示
  • 【论文阅读】SRCNN
  • 数据结构与算法——Java实现 32.堆
  • 深度学习 .dot()
  • idea2024 git merge 时丢失 Merge remote-tracking branch问题
  • pdf怎么删除多余不想要的页面?删除pdf多余页面的多个方法
  • 树莓派应用--AI项目实战篇来啦-3.OpenCV 读取写入和显示图像
  • 一句话就把HTTPS工作原理讲明白了
  • CPU 和处理核心(Core)中间有3个缓存