博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓性能监控(APM)之CPU使用率监控
阅读量:1895 次
发布时间:2019-04-26

本文共 4298 字,大约阅读时间需要 14 分钟。

目标

监控app的CPU使用率.

方案

  1. 对于8.0以上版本,执行top命令,从结果里直接提取出当前app占用CPU比例.
  2. 对于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())

完整源代码

转载地址:http://ggadf.baihongyu.com/

你可能感兴趣的文章
二叉排序树、二叉平衡树
查看>>
21、栈的压入、弹出序列
查看>>
如何利用平台的不同去推广?
查看>>
如何在 MacOS 中删除 Time Machine 本地快照?
查看>>
如何使用 SSD 升级 MacBook Pro
查看>>
Windows 11无法支持 Mac 安装?用这招轻松绕过 TPM 限制
查看>>
MacBook 键盘出现故障,如何修复?
查看>>
M1 Mac专用utm虚拟机安装Windows 11教程
查看>>
无需升级 macOS Monterey 使用新版本 safari 浏览器的方法
查看>>
macOS 12 Monterey beta 2
查看>>
苹果Mac如何限制进程 CPU 资源占用?
查看>>
数字货币是什么?和贵金属及法定货币有什么区别?
查看>>
FileCombo社区是什么?
查看>>
FileCombo怎么玩?Filecoin怎么赚?
查看>>
Filecoin的应用场景
查看>>
Filecoin要被五倍回收了吗??
查看>>
filecoin什么时候上线?
查看>>
“30岁”的网络该如何自救(上)
查看>>
又一浏览器集成IPFS,分布式影响力再扩大
查看>>
IPFS为何被视为“明天的网络”?
查看>>