根据最近的调研,Android整机的性能主要有如下方面:

1、CPU

2、内存

3、耗电量

4、网络

本文着重介绍CPU相关数据的获取,在多核情况下,对每个CPU运行情况进行监控,获取相关的属性。

A. 当前主频,通过 cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq 获取,cpu0代表第一个CPU

B. 最大主频,通过 cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq 获取,cpu0代表第一个CPU

C. 最小主频,通过 cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq 获取,cpu0代表第一个CPU

D.使用率,通过 cat /proc/stat 获取,文件内容如下:第一行代表整体CPU的使用情况,cpu0为第一个cpu的使用情况,不同的列数代表cpu的耗时情况,如第4列代表的就是空闲时间idle的值。

shell@V4:/ $ cat proc/stat
cat proc/stat
cpu  3866978 127487 3035722 26246226 432119 27 52118 0 0 
cpu0 3004154 33263 2209598 18130481 92464 16 51202 0 0 0


也就是说使用率我们无法直接获取,需要通过计算,具体的计算方式如下:

usage=(total-idel)/total

说明:使用率指得是cpu在某段时间内被使用的比率,因此事关两个时刻(t1、t2)的值。

分别需要在t1时刻和t2时刻获取total和idel

t1时刻获取: total_t1=所有值相加(同一行) idle_t1=第4个数值

t2时刻获取: total_t2=所有值相加(同一行) idle_t2=第4个数值

上述公式中的

total = total_t2 - total_t1

idle = idle_t2 - idle_t1

这是在国外的资料中说的,可是事实上运行的时候,这样的计算方式可能会出现负数,这边还有点疑问,如果哪位大神知道,麻烦指教

此外,还有一些CPU的名称,核数信息等,此处不做详解。

下面给出一个类CPUManager,为了避免编码问题,直接用英文写的注释,如有语法错误。。。看懂就行

package com.performancemonitor.tools;import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;import android.util.Log;public class CPUManager {// CPU indexpublic int CPU_index = 0;// sampling time in calculate usagepublic int sample_fre = DataManager.sample_fre;public CPUManager(int index) {this.CPU_index = index;}/*** Wether the CPU is online* @return  true if it is online*/public boolean isOnline() {BufferedReader reader;String result = "";ProcessBuilder cmd;InputStream in = null;try {String[] args = { "/system/bin/cat","/sys/devices/system/cpu/cpu" + CPU_index + "/online" };cmd = new ProcessBuilder(args);Process process = cmd.start();in = process.getInputStream();reader = new BufferedReader(new InputStreamReader(process.getInputStream()));String temp = "";while ((result = reader.readLine()) != null) {temp = temp + result;}if (temp.equals("1")) {return true;}in.close();} catch (IOException ex) {ex.printStackTrace();}return false;}/*** get the cpu's max frequency, return cpu0 max frequency by defalut* "/system/bin/cat"* "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" */public int getMaxCpuFreq() {String result = "";String[] args = {"/system/bin/cat","/sys/devices/system/cpu/cpu" + CPU_index+ "/cpufreq/cpuinfo_max_freq" };if (!isOnline()) {args[1] = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";}ProcessBuilder cmd;try {cmd = new ProcessBuilder(args);Process process = cmd.start();InputStream in = process.getInputStream();byte[] re = new byte[24];while (in.read(re) != -1) {result = result + new String(re);}in.close();} catch (IOException ex) {ex.printStackTrace();result = "0";}int max_fre = Integer.parseInt(result.trim()) / 1024;return max_fre;}/***  get the cpu's min frequency,return cpu0 min frequency by defalut*/public int getMinCpuFreq() {String result = "";ProcessBuilder cmd;String[] args = {"/system/bin/cat","/sys/devices/system/cpu/cpu" + CPU_index+ "/cpufreq/cpuinfo_min_freq" };if (!isOnline()) {args[1] = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq";}try {cmd = new ProcessBuilder(args);Process process = cmd.start();InputStream in = process.getInputStream();byte[] re = new byte[24];while (in.read(re) != -1) {result = result + new String(re);}in.close();} catch (IOException ex) {ex.printStackTrace();result = "-1";}int min_fre = Integer.parseInt(result.trim()) / 1024;return min_fre;}/*** get the cpu's current frequency*/public int getCurCpuFreq() {String result = "";int cur_fre = 0;if (!isOnline()) {result = "0";return 0;}try {FileReader fr = new FileReader("/sys/devices/system/cpu/cpu"+ CPU_index + "/cpufreq/scaling_cur_freq");BufferedReader br = new BufferedReader(fr);String text = br.readLine();result = text.trim();br.close();} catch (FileNotFoundException e) {e.printStackTrace();return 0;} catch (IOException e) {e.printStackTrace();return 0;}cur_fre = Integer.parseInt(result) / 1024;return cur_fre;}/*** get the cpu name*/public static String getCpuName() {try {FileReader fr = new FileReader("/proc/cpuinfo");BufferedReader br = new BufferedReader(fr);String text = br.readLine();String[] array = text.split(":\\s+", 2);for (int i = 0; i < array.length; i++) {}return array[1];} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return null;}/*** get the device's cpu num*/public static int getNumCores() {class CpuFilter implements FileFilter {@Overridepublic boolean accept(File pathname) {if (Pattern.matches("cpu[0-9]", pathname.getName())) {return true;}return false;}}try {File dir = new File("/sys/devices/system/cpu/");File[] files = dir.listFiles(new CpuFilter());return files.length;} catch (Exception e) {Log.e("cxq", "CPU Count: Failed.");e.printStackTrace();return 1;}}/*** get the cpu usage using specified samplling time*/public double getUsage() {double usage = 0;long total_start, total_end;long idel_start, idel_end;try {Map<String, Long> start_data = getCPUData();idel_start = start_data.get("idle");total_start = start_data.get("total_time");Thread.sleep(sample_fre);Map<String, Long> end_data = getCPUData();idel_end = end_data.get("idle");total_end = end_data.get("total_time");double idel = idel_end - idel_start;double total = total_end - total_start;if (total == 0) {return 0;}usage = (total - idel) / total;usage = Math.abs(usage * 100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}DecimalFormat df = new java.text.DecimalFormat("#.00");usage = Double.parseDouble(df.format(usage));return usage;}/*** take a data snapshot avoid reading the old data, especially using for usage calculation* @return Map it contains two values,("total_time",total_time) and ("idle",idle);*/public Map<String, Long> getCPUData() {Map<String, Long> data = new HashMap<String, Long>();ArrayList<Long> list = new ArrayList<Long>();ArrayList<Long> list_defalut = new ArrayList<Long>();for (int i = 0; i < 10; i++) {list_defalut.add(0L);}String cpuCompare = "cpu" + CPU_index;if (CPU_index == -1) {cpuCompare = "cpu";}String load = "";String[] temp = null;try {BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/stat")), 1000);while ((load = reader.readLine()) != null) {if (load.contains(cpuCompare)) {break;} else {load = "";}}reader.close();if (load == null || load.equals("")) {data.put("total_time", 0L);data.put("idle", 0L);return data;}temp = load.split(" ");for (int i = 1; i < temp.length; i++) {if (!temp[i].trim().equals("")) {list.add(Long.parseLong(temp[i]));}}} catch (IOException ex) {Log.e("CPU", "IOException" + ex.toString());data.put("total_time", 0L);data.put("idle", 0L);return data;}long total_time = 0;for (int i = 0; i < list.size(); i++) {total_time += list.get(i);}data.put("total_time", total_time);data.put("idle", list.get(3));return data;}}

如何使用?

如果要获取整个CPU的情况,则CPU_index=-1,该情况下无主频,如果要获取cpu0的情况,则在构造函数里面传0,以此类推

<p>CPUManager total_cpu=new CPUManager(i);
</p><span style="white-space:pre">   </span>double usage=total_cpu.getUsage()

其他性能方面的数据获取,还有待补充。。。。

Android整机性能监控:多核CPU相关数据的获取(使用率、主频)相关推荐

  1. Linux性能监控(CPU监控)

    Linux性能监控(CPU监控) 主要分为四类: cup监控 内存监控命令 IO性能 网络性能 cup监控 关于CPU,有3个重要的概念:上下文切换(context switchs),运行队列(Run ...

  2. android 应用性能监控软件,App性能监控工具,卡顿

    (609条消息) android 应用性能监控软件,App性能监控工具_weixin_39940154的博客-CSDN博客 APP性能监测的各种工具 - ClareBaby01 - 博客园 (cnbl ...

  3. linux top 命令可视化_linux性能监控:CPU监控命令之top命令

    ​ CPU监控命令之top命令 1概述: top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法. top是一 ...

  4. linux系统和性能监控之cpu篇,Linux系统和性能监控之CPU篇

    1.0 性能监控介绍 性能优化就是找到系统处理中的瓶颈以及去除这些的过程,多数管理员相信看一些相关的"cook book"就可以实现性能优化,通常通过对内核的一些配置是可以简单的解 ...

  5. linux进程cpu时间片,Linux性能监控之CPU篇

    这篇文章中,主要介绍CPU的一些基础知识. 首先介绍一下Linux kernel中的调度器(scheduler),调度器负责调度系统中的两种资源,一是线程,二是中断.调度器给不同资源不同的优先级.从高 ...

  6. android 应用性能监控软件,App性能监控工具

    Android的性能监控工具,之前已经介绍了Android monitor/DDMS,其他的Android App性能监控工具也能在某些测试需要的时候提供帮助. Android App性能监控工具介绍 ...

  7. 叮!快收好这份Android网络性能监控方案

    简介:移动互联网时代,移动端极大部分业务都需要通过App和Server之间的数据交互来实现,所以大部分App提供的业务功能都需要使用网络请求.如果因为网络请求慢或者请求失败,导致用户无法顺畅的使用业务 ...

  8. Android网络性能监控方案

    背景 移动互联网时代,移动端极大部分业务都需要通过App和Server之间的数据交互来实现,所以大部分App提供的业务功能都需要使用网络请求.如果因为网络请求慢或者请求失败,导致用户无法顺畅的使用业务 ...

  9. python多核cpu_Python中的多核CPU共享数据之协程详解

    一 : 科普一分钟 尽管进程间是独立存在的,不能相互访问彼此的数据,但是在python中却存在进程间的通信方法,来帮助我们可以利用多核CPU也能共享数据. 对于多线程其实也是存在一些缺点的,不是任何场 ...

最新文章

  1. 悬而未决的AI竞赛:全球企业人工智能发展现状
  2. linux驱动 pcie 框架_Linux PCI 设备驱动基本框架(二)
  3. seaborn系列 (7) | 核函数密度估计图kdeplot()
  4. python【力扣LeetCode算法题库】225-用队列实现栈
  5. Spring学习总结(2)——Spring的常用注解
  6. C#中使用ProtoBuf提高序列化速度对比二进制序列化
  7. SAP ABAP一组关键字 IS BOUND, IS NOT INITIAL和IS ASSIGNED的用法辨析
  8. jzoj 6302. 提高组
  9. zabbix计算型监控项函数last_zabbix 自定义key类型之计算(Calculated items)-阿里云开发者社区...
  10. python自定义配置文件读取_python读取和自定义配置文件的方法
  11. 【NOIP2016】【Luogu1909】买铅笔(模拟)
  12. nodejs 模板引擎ejs的使用
  13. 软考中级-数据库系统工程师复习大纲
  14. GlobalMapper20提取点云LAS文件当中的投影信息
  15. android手机内存单位 吉字节,Android的尺寸单位
  16. android跳转到应用市场并进入指定包名的应用详情
  17. dbus-glib编程2:d-feet的使用
  18. iphone文件服务器权限,苹果手机怎么开启文件共享权限
  19. HTML设置水平居中的几种方式
  20. 【php-fpm】重启、启动、关闭

热门文章

  1. python---打包exe文件运行自动化
  2. python自动聊天机器人_用python实现的一个自动聊天的机器人
  3. 一种基于信令数据的业务推销类骚扰电话识别方法
  4. 只是你没那么重要罢了
  5. Shadertoy 多个buffers 转成Threejs代码
  6. Shading 编程
  7. Web Service的使用
  8. Android zip文件解压缩工具类
  9. 手机wps云文档无法连接服务器,手机wps云文档怎么用
  10. UE4 物理系统实现