安卓性能监控(APM)之CPU使用率监控
发布日期:2021-06-29 18:29:36
浏览次数:2
分类:技术文章
本文共 4298 字,大约阅读时间需要 14 分钟。
目标
监控app的CPU使用率.
方案
- 对于8.0以上版本,执行top命令,从结果里直接提取出当前app占用CPU比例.
- 对于8.0以下版本,通过读取“/proc/stat”和“/proc/App进程ID/stat”两个文件,计算出app进程占用的CPU比例.
- 完整代码:
package com.mb.roamdriver.myapm.cpuimport android.os.Buildimport android.text.TextUtilsimport java.io.BufferedReaderimport java.io.IOExceptionimport java.io.InputStreamReaderimport java.io.RandomAccessFileobject CpuUtil { private var mProcStatFile: RandomAccessFile? = null private var mAppStatFile: RandomAccessFile? = null private var mLastCpuTime: Long? = null private var mLastAppCpuTime: Long? = null /** * 获取cpu使用率 */ fun getCpuUsage(): Float { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { getCpuUsageForHigherVersion() } else { getCpuUsageForLowerVersion() } } /** * 安卓8.0以上版本获取cpu使用率 */ private fun getCpuUsageForHigherVersion(): Float { var process: Process? = null try { process = Runtime.getRuntime().exec("top -n 1") val reader = BufferedReader(InputStreamReader(process.inputStream)) var line: String var cpuIndex = -1 while (reader.readLine().also { line = it } != null) { line = line.trim { it <= ' ' } if (TextUtils.isEmpty(line)) { continue } val tempIndex = getCPUIndex(line) if (tempIndex != -1) { cpuIndex = tempIndex continue } if (line.startsWith(android.os.Process.myPid().toString())) { if (cpuIndex == -1) { continue } val param = line.split("\\s+".toRegex()).toTypedArray() if (param.size <= cpuIndex) { continue } var cpu = param[cpuIndex] if (cpu.endsWith("%")) { cpu = cpu.substring(0, cpu.lastIndexOf("%")) } return cpu.toFloat() / Runtime.getRuntime().availableProcessors() } } } catch (e: IOException) { e.printStackTrace() } finally { process?.destroy() } return 0F } /** * 安卓8.0以下版本获取cpu使用率 */ private fun getCpuUsageForLowerVersion(): Float { val cpuTime: Long val appTime: Long var value = 0.0f try { if (mProcStatFile == null || mAppStatFile == null) { mProcStatFile = RandomAccessFile("/proc/stat", "r") mAppStatFile = RandomAccessFile("/proc/" + android.os.Process.myPid() + "/stat", "r") } else { mProcStatFile!!.seek(0L) mAppStatFile!!.seek(0L) } val procStatString = mProcStatFile!!.readLine() val appStatString = mAppStatFile!!.readLine() val procStats = procStatString.split(" ".toRegex()).toTypedArray() val appStats = appStatString.split(" ".toRegex()).toTypedArray() cpuTime = procStats[2].toLong() + procStats[3].toLong() + procStats[4].toLong() + procStats[5].toLong() + procStats[6].toLong() + procStats[7].toLong() + procStats[8].toLong() appTime = appStats[13].toLong() + appStats[14].toLong() if (mLastCpuTime == null && mLastAppCpuTime == null) { mLastCpuTime = cpuTime mLastAppCpuTime = appTime return value } value = (appTime - mLastAppCpuTime!!).toFloat() / (cpuTime - mLastCpuTime!!).toFloat() * 100f mLastCpuTime = cpuTime mLastAppCpuTime = appTime } catch (e: Exception) { e.printStackTrace() } return value } private fun getCPUIndex(line: String): Int { if (line.contains("CPU")) { val titles = line.split("\\s+".toRegex()).toTypedArray() for (i in titles.indices) { if (titles[i].contains("CPU")) { return i } } } return -1 }}
- 调用样例:
Log.d("MyApm-CPU使用率", CpuUtil.getCpuUsage().toString())
完整源代码
转载地址:https://cxyxy.blog.csdn.net/article/details/106587239 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月12日 04时57分41秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
案例——原生js返回顶部
2019-04-30
案例——原生js模块化开发日历控件
2019-04-30
基于原生JS封装Tool库(源码)
2019-04-30
案例——模块化开发选项卡
2019-04-30
如何利用平台的不同去推广?
2019-04-30
如何在 MacOS 中删除 Time Machine 本地快照?
2019-04-30
如何使用 SSD 升级 MacBook Pro
2019-04-30
Windows 11无法支持 Mac 安装?用这招轻松绕过 TPM 限制
2019-04-30
MacBook 键盘出现故障,如何修复?
2019-04-30
M1 Mac专用utm虚拟机安装Windows 11教程
2019-04-30
无需升级 macOS Monterey 使用新版本 safari 浏览器的方法
2019-04-30
macOS 12 Monterey beta 2
2019-04-30
苹果Mac如何限制进程 CPU 资源占用?
2019-04-30
如何安装 iOS 15 和 iPadOS 15 公共测试版?
2019-04-30
Apple 产品购买指南(2021/07)
2019-04-30
如何清除 MacBook 或 iMac 上的灰尘
2019-04-30
parallels desktop安装win11,M1和intel Mac安装的区别
2019-04-30
如何解决欠款等问题,你可能需要一个去中心化的系统——区块链
2019-04-30
今天,我们又见证了一个传说(币圈爆仓)
2019-04-30
区块链中的“工人”是怎么工作的
2019-04-30