Android整機效能監控:多核CPU相關資料的獲取(使用率、主頻)
根據最近的調研,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
D.使用率,通過 cat /proc/stat 獲取,檔案內容如下:第一行代表整體CPU的使用情況,cpu0為第一個cpu的使用情況,不同的列數代表cpu的耗時情況,如第4列代表的就是空閒時間idle的值。
[email protected]:/ $ 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 index
public int CPU_index = 0;
// sampling time in calculate usage
public 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 {
@Override
public 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 block
e.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相關資料的獲取(使用率、主頻)
根據最近的調研,Android整機的效能主要有如下方面: 1、CPU 2、記憶體 3、耗電量 4、網路 本文著重介紹CPU相關資料的獲取,在多核情況下,對每個CPU執行情況進行監控,獲取相關的屬性。 A. 當前主頻,通過 cat /sys/devices/system/cp
四、JAVA多執行緒:執行緒安全與資料同步 (synchronized、This Monitor、Class Monitor)
本章首先從一個簡單的例子入手,講解了資料同步的概念,以及會引發資料不一致性問題的情況,然後非常詳細地介紹了synchronized關鍵字以及與其對應的JVM指令。本章的最後還分析了幾種可能引起程式進入死鎖的原因,以及如何使用工具進行診斷,執行緒安全與資料同步
css多行超出部分用...代替(span、div)
css樣式 /*超出指定行*/ overflow:hidden; text-overflow:ellipsis; display: -webkit-box; -webkit-line-clamp:2; -webkit-box-orient
強大的螢幕適配佈局rem響應式 實現一套web程式碼多端自適應適配 (實用、贊)
原文出處:https://blog.csdn.net/qq_14997169/article/details/53914201 實現強大的螢幕適配佈局 流式的佈局、固定的寬度,還有響應式來做,但是這些方案都不是最佳的解決方法。->->rem rem是什麼?
自適應濾波:最小均方誤差濾波器(LMS、NLMS)
作者:桂。 時間:2017-04-02 08:08:31 宣告:歡迎被轉載,不過記得註明出處哦~ 【讀書筆記08】 前言 西蒙.赫金的《自適應濾波器原理》第四版第五、六章:最小均方自適應濾波器(LMS,Least Mean Square)以及歸一化最小均方自適應濾波器(NLMS,
Zabbix通過SNMP監控多核CPU使用率時, 計算CPU平均使用率
mes ces zabb ext 觸發器 trend times http sso 環境:沒有Agent,只能通過SNMP監控時,需要獲取多核CPU的平均使用率。 ZABBIX的使用SNMP監控CPU使用率時,由於設備都是多核CPU,監控的都是單獨某一核心的使用率,但單獨某
python學習筆記- day10-【問題: python為什麽python的多線程不能利用多核CPU?】
例如 currency 視頻 stat 解碼 核心數 __name__ args 制作 為什麽python的多線程不能利用多核CPU,但是咱們在寫代碼的時候,多線程的確是在並發,而且還比單線程快。 一、python的多線程不能利用多核CPU? 原因: 因為GIL,
Zabbix監控多核CPU
cto RoCE http size mage f11 cover 空格 建圖 服務器的cpu核心數不同,需要對服務器性能的監控需要對cpu各個核心進行監控。1、創建搜索規則自動發現服務器上所有CPU核心數鍵值名為:system.cpu.discovery2、創建監控項原型
MySQL並行寫入、查詢效能調優(多核CPU)
[html] view plain copy [client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock based
淘寶面試題:如何充分利用多核CPU,計算很大的List中所有整數的和
引用 前幾天在網上看到一個淘寶的面試題:有一個很大的整數list,需要求這個list中所有整數的和,寫一個可以充分利用多核CPU的程式碼,來計算結果。 一:分析題目從題中可以看到“很大的List”以及“充分利用多核CPU”,這就已經充分告訴我們要採用多執行緒(任務)進行
Verilog學習筆記12:使用多核CPU協同編譯
在使用Quartus Prime進行編譯時,會提示如下警告資訊。產生這條警告資訊的原因在於我們現在使用的CPU是多核,但是我們沒有對如何使用多核CPU編譯進行設定,所以提示如下警告資訊。本文將講述如何在Quartus Prime中進行設定,使得多核CPU協同編譯。 第1種
【好文】淘寶面試題:如何充分利用多核CPU,計算很大的List中所有整數的和
引用 前幾天在網上看到一個淘寶的面試題:有一個很大的整數list,需要求這個list中所有整數的和,寫一個可以充分利用多核CPU的程式碼,來計算結果。 一:分析題目 從題中可以看到“很大的List”以及“充分利用多核CPU”,這就已經充分告訴我們要採用多執行緒(任務)進行
Android 效能優化:多執行緒
前言 Android Performance Patterns Season 5 主要介紹了 Android 多執行緒環境下的效能問題。通過介紹 Android 提供的多種多執行緒工具類 (AsyncTask, HandlerThread, Inte
linux top命令查看內存及多核CPU的使用講述【轉】
http 最小 文件 改變 總計 href 獲取 gif www. 轉載一下top使用後詳細的參數,之前做的筆記找不見了,轉載一下,作為以後的使用參考: 原文地址:http://blog.csdn.net/linghao00/article/details/8059244
python的多線程為什麽不能利用多核CPU?
虛擬 pytho 能夠 並發 我們 就是 比較 inter 情況 python 為什麽不能利用多核CPU GIL: (1)其實是因為在python中有一個GIL(Global Interpreter Lock),中文為:全局解釋器鎖。 1、是最開始python為了數據安全設
為什麽python的多線程不能利用多核CPU,但是咱們在寫代碼的時候,多線程的確是在並發,而且還比單線程快。
全局 睡眠 read 處理 sleep roc 需要 寫代碼 強制 python裏的多線程是單cpu意義上的多線程,它和多cpu上的多線程有著本質的區別。單cpu多線程:並發多cpu多線程:並行內部包含並發 首先強調背景: 1、GIL是什麽?GIL的全稱是Gl
多核CPU上python多線程並行的一個假象
llb 使用 發展 重要 multi 定律 依賴 並發 操作 引用鏈接:http://www.cnblogs.com/pengyingh/articles/6586760.html GIL 與 Python 線程的糾葛 GIL 是什麽東西?它對我們的 python 程序會產
查看系統cpu性能top和多核cpu使用性能
sta 情況 top 查看系統 系統 停止 排序。 性能 內存 1、使用top查看系統當前負載情況。 在顯示過程 按P會按照cpu使用率排序,按M會按照內存占用率排序。 2、多核CPU上,每個核的使用率,使用 mpstat mpstat -P ALL 1
查看多核CPU各核的狀態
nbsp 圖片 sta alt 16px 技術分享 定向 rep color 1 top 命令,然後按數字“1” 2 命令:mpstat -P ALL 3 命令:sar -P ALL 輸出較多,可grep或者重定向至文件查看 個人推薦使用第二種方式,操作方便且
【Android】效能優化:電量消耗統計
電量的消耗和使用對於移動裝置非常重要,一項調查問卷顯示,電池的容量和壽命是手機最重要的營銷點:所謂“the one thing that you can't do without”。 硬體 從硬體的角度看,Android電量的消耗主要來自螢幕,CPU,網路裝置和各樣的感測器:指紋,亮度