Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_47647075
emmagee
提交
5d0a85c8
E
emmagee
项目概览
weixin_47647075
/
emmagee
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
emmagee
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5d0a85c8
编写于
9月 12, 2014
作者:
A
andrewleo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added: calculate usage rate of each cpu, and record them in csv
上级
fad688ba
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
118 addition
and
36 deletion
+118
-36
project.properties
project.properties
+1
-1
src/com/netease/qa/emmagee/service/EmmageeService.java
src/com/netease/qa/emmagee/service/EmmageeService.java
+8
-9
src/com/netease/qa/emmagee/utils/CpuInfo.java
src/com/netease/qa/emmagee/utils/CpuInfo.java
+109
-26
未找到文件。
project.properties
浏览文件 @
5d0a85c8
...
...
@@ -8,4 +8,4 @@
# project structure.
# Project target.
target
=
android-
10
target
=
android-
8
src/com/netease/qa/emmagee/service/EmmageeService.java
浏览文件 @
5d0a85c8
...
...
@@ -271,11 +271,17 @@ public class EmmageeService extends Service {
bw
=
new
BufferedWriter
(
osw
);
long
totalMemorySize
=
memoryInfo
.
getTotalMemory
();
String
totalMemory
=
fomart
.
format
((
double
)
totalMemorySize
/
1024
);
String
multiCpuTitle
=
""
;
// titles of multiple cpu cores
ArrayList
<
String
>
cpuList
=
cpuInfo
.
getCpuList
();
for
(
int
i
=
0
;
i
<
cpuList
.
size
();
i
++){
multiCpuTitle
+=
","
+
cpuList
.
get
(
i
)+
"总使用率(%)"
;
}
bw
.
write
(
"指定应用的CPU内存监控情况\r\n"
+
"应用包名:,"
+
packageName
+
"\r\n"
+
"应用名称: ,"
+
processName
+
"\r\n"
+
"应用PID: ,"
+
pid
+
"\r\n"
+
"机器内存大小(MB):,"
+
totalMemory
+
"MB\r\n"
+
"机器CPU型号:,"
+
cpuInfo
.
getCpuName
()
+
"\r\n"
+
"机器android系统版本:,"
+
memoryInfo
.
getSDKVersion
()
+
"\r\n"
+
"手机型号:,"
+
memoryInfo
.
getPhoneType
()
+
"\r\n"
+
"UID:,"
+
uid
+
"\r\n"
+
START_TIME
);
bw
.
write
(
"时间"
+
","
+
"应用占用内存PSS(MB)"
+
","
+
"应用占用内存比(%)"
+
","
+
" 机器剩余内存(MB)"
+
","
+
"应用占用CPU率(%)"
+
","
+
"CPU总使用率(%)"
+
","
+
"流量(KB)"
+
","
+
"电量(%)"
+
","
+
"电流(mA)"
+
","
+
"温度(C)"
+
","
+
"电压(V)"
+
"\r\n"
);
bw
.
write
(
"时间"
+
","
+
"应用占用内存PSS(MB)"
+
","
+
"应用占用内存比(%)"
+
","
+
" 机器剩余内存(MB)"
+
","
+
"应用占用CPU率(%)"
+
","
+
"CPU总使用率(%)"
+
multiCpuTitle
+
","
+
"流量(KB)"
+
","
+
"电量(%)"
+
","
+
"电流(mA)"
+
","
+
"温度(C)"
+
","
+
"电压(V)"
+
"\r\n"
);
}
catch
(
IOException
e
)
{
Log
.
e
(
LOG_TAG
,
e
.
getMessage
());
}
...
...
@@ -310,12 +316,10 @@ public class EmmageeService extends Service {
startY
=
y
;
mTouchStartX
=
event
.
getX
();
mTouchStartY
=
event
.
getY
();
Log
.
d
(
"startP"
,
"startX"
+
mTouchStartX
+
"====startY"
+
mTouchStartY
);
break
;
case
MotionEvent
.
ACTION_MOVE
:
updateViewPosition
();
break
;
case
MotionEvent
.
ACTION_UP
:
updateViewPosition
();
// showImg();
...
...
@@ -400,7 +404,6 @@ public class EmmageeService extends Service {
strBuilder
.
append
(
line
);
strBuilder
.
append
(
"\r\n"
);
String
regex
=
".*Displayed.*"
+
startActivity
+
".*\\+(.*)ms.*"
;
Log
.
d
(
"my logs"
,
regex
);
if
(
line
.
matches
(
regex
))
{
Log
.
w
(
"my logs"
,
line
);
if
(
line
.
contains
(
"total"
))
{
...
...
@@ -413,8 +416,6 @@ public class EmmageeService extends Service {
}
}
getStartTimeCount
++;
// Log.w("my logs", "Start Time Log:" + strBuilder.toString());
Log
.
w
(
LOG_TAG
,
"getStartCount:"
+
getStartTimeCount
);
}
catch
(
IOException
e
)
{
Log
.
d
(
LOG_TAG
,
e
.
getMessage
());
}
...
...
@@ -434,7 +435,6 @@ public class EmmageeService extends Service {
String
processMemory
=
fomart
.
format
((
double
)
pidMemory
/
1024
);
String
currentBatt
=
String
.
valueOf
(
currentInfo
.
getCurrentValue
());
// 异常数据过滤
Log
.
d
(
"my logs-before"
,
currentBatt
);
try
{
if
(
Math
.
abs
(
Double
.
parseDouble
(
currentBatt
))
>=
500
)
{
currentBatt
=
"N/A"
;
...
...
@@ -442,7 +442,6 @@ public class EmmageeService extends Service {
}
catch
(
Exception
e
)
{
currentBatt
=
"N/A"
;
}
Log
.
d
(
"my logs-after"
,
currentBatt
);
ArrayList
<
String
>
processInfo
=
cpuInfo
.
getCpuRatioInfo
(
totalBatt
,
currentBatt
,
temperature
,
voltage
);
if
(
isFloating
)
{
String
processCpuRatio
=
"0"
;
...
...
src/com/netease/qa/emmagee/utils/CpuInfo.java
浏览文件 @
5d0a85c8
...
...
@@ -16,6 +16,8 @@
*/
package
com.netease.qa.emmagee.utils
;
import
java.io.File
;
import
java.io.FileFilter
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.io.RandomAccessFile
;
...
...
@@ -23,6 +25,7 @@ import java.text.DecimalFormat;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Calendar
;
import
java.util.regex.Pattern
;
import
com.netease.qa.emmagee.service.EmmageeService
;
...
...
@@ -41,8 +44,8 @@ public class CpuInfo {
private
Context
context
;
private
long
processCpu
;
private
long
idleCpu
;
private
long
totalCpu
;
private
ArrayList
<
Long
>
idleCpu
=
new
ArrayList
<
Long
>()
;
private
ArrayList
<
Long
>
totalCpu
=
new
ArrayList
<
Long
>()
;
private
boolean
isInitialStatics
=
true
;
private
SimpleDateFormat
formatterFile
;
private
MemoryInfo
mi
;
...
...
@@ -51,17 +54,19 @@ public class CpuInfo {
private
long
lastestTraffic
;
private
long
traffic
;
private
TrafficInfo
trafficInfo
;
private
ArrayList
<
String
>
cpuUsedRatio
;
private
long
totalCpu2
;
private
ArrayList
<
String
>
cpuUsedRatio
=
new
ArrayList
<
String
>()
;
private
ArrayList
<
Long
>
totalCpu2
=
new
ArrayList
<
Long
>()
;
private
long
processCpu2
;
private
long
idleCpu2
;
private
ArrayList
<
Long
>
idleCpu2
=
new
ArrayList
<
Long
>()
;
private
String
processCpuRatio
=
""
;
private
String
totalCpuRatio
=
""
;
private
ArrayList
<
String
>
totalCpuRatio
=
new
ArrayList
<
String
>()
;
private
int
pid
;
private
static
final
String
INTEL_CPU_NAME
=
"model name"
;
private
static
final
String
CPU_DIR_PATH
=
"/sys/devices/system/cpu/"
;
private
static
final
String
CPU_X86
=
"x86"
;
private
static
final
String
CPU_INFO_PATH
=
"/proc/cpuinfo"
;
private
static
final
String
CPU_STAT
=
"/proc/stat"
;
public
CpuInfo
(
Context
context
,
int
pid
,
String
uid
)
{
this
.
pid
=
pid
;
...
...
@@ -91,9 +96,6 @@ public class CpuInfo {
stringBuffer
.
append
(
line
+
"\n"
);
}
String
[]
tok
=
stringBuffer
.
toString
().
split
(
" "
);
for
(
int
i
=
0
;
i
<
tok
.
length
;
i
++){
Log
.
w
(
LOG_TAG
,
"tok["
+
i
+
"]=========="
+
tok
[
i
]);
}
processCpu
=
Long
.
parseLong
(
tok
[
13
])
+
Long
.
parseLong
(
tok
[
14
]);
processCpuInfo
.
close
();
}
catch
(
FileNotFoundException
e
)
{
...
...
@@ -102,17 +104,20 @@ public class CpuInfo {
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
read_total_cpu_stat
();
}
private
void
read_total_cpu_stat
()
{
try
{
// monitor total and idle cpu stat of certain process
RandomAccessFile
cpuInfo
=
new
RandomAccessFile
(
"/proc/stat"
,
"r"
);
String
[]
toks
=
cpuInfo
.
readLine
().
split
(
"\\s+"
);
for
(
int
i
=
0
;
i
<
toks
.
length
;
i
++){
Log
.
w
(
LOG_TAG
,
"toks["
+
i
+
"]=========="
+
toks
[
i
]);
RandomAccessFile
cpuInfo
=
new
RandomAccessFile
(
CPU_STAT
,
"r"
);
String
line
=
""
;
while
((
null
!=
(
line
=
cpuInfo
.
readLine
()))
&&
line
.
startsWith
(
"cpu"
))
{
String
[]
toks
=
line
.
split
(
"\\s+"
);
idleCpu
.
add
(
Long
.
parseLong
(
toks
[
4
]));
totalCpu
.
add
(
Long
.
parseLong
(
toks
[
1
])
+
Long
.
parseLong
(
toks
[
2
])
+
Long
.
parseLong
(
toks
[
3
])
+
Long
.
parseLong
(
toks
[
4
])
+
Long
.
parseLong
(
toks
[
6
])
+
Long
.
parseLong
(
toks
[
5
])
+
Long
.
parseLong
(
toks
[
7
]));
}
idleCpu
=
Long
.
parseLong
(
toks
[
4
]);
totalCpu
=
Long
.
parseLong
(
toks
[
1
])
+
Long
.
parseLong
(
toks
[
2
])
+
Long
.
parseLong
(
toks
[
3
])
+
Long
.
parseLong
(
toks
[
4
])
+
Long
.
parseLong
(
toks
[
6
])
+
Long
.
parseLong
(
toks
[
5
])
+
Long
.
parseLong
(
toks
[
7
]);
cpuInfo
.
close
();
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
...
...
@@ -149,6 +154,63 @@ public class CpuInfo {
return
""
;
}
/**
* display directories naming with "cpu*"
*
* @author andrewleo
*/
class
CpuFilter
implements
FileFilter
{
@Override
public
boolean
accept
(
File
pathname
)
{
// Check if filename matchs "cpu[0-9]"
if
(
Pattern
.
matches
(
"cpu[0-9]"
,
pathname
.
getName
()))
{
return
true
;
}
return
false
;
}
}
/**
* get CPU core numbers
*
* @return cpu core numbers
*/
public
int
getCpuNum
()
{
try
{
// Get directory containing CPU info
File
dir
=
new
File
(
CPU_DIR_PATH
);
// Filter to only list the devices we care about
File
[]
files
=
dir
.
listFiles
(
new
CpuFilter
());
return
files
.
length
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
1
;
}
}
/**
* get CPU core list
*
* @return cpu core list
*/
public
ArrayList
<
String
>
getCpuList
()
{
ArrayList
<
String
>
cpuList
=
new
ArrayList
<
String
>();
try
{
// Get directory containing CPU info
File
dir
=
new
File
(
CPU_DIR_PATH
);
// Filter to only list the devices we care about
File
[]
files
=
dir
.
listFiles
(
new
CpuFilter
());
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
cpuList
.
add
(
files
[
i
].
getName
());
}
return
cpuList
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
cpuList
.
add
(
"cpu0"
);
return
cpuList
;
}
}
/**
* reserve used ratio of process CPU and total CPU, meanwhile collect
* network traffic.
...
...
@@ -163,8 +225,11 @@ public class CpuInfo {
fomart
.
setMaximumFractionDigits
(
2
);
fomart
.
setMinimumFractionDigits
(
2
);
readCpuStat
();
cpuUsedRatio
.
clear
();
idleCpu
.
clear
();
totalCpu
.
clear
();
totalCpuRatio
.
clear
();
readCpuStat
();
try
{
String
mDateTime2
;
...
...
@@ -177,7 +242,6 @@ public class CpuInfo {
voltage
=
"N/A"
;
}
else
mDateTime2
=
formatterFile
.
format
(
cal
.
getTime
().
getTime
());
if
(
isInitialStatics
)
{
initialTraffic
=
trafficInfo
.
getTrafficInfo
();
isInitialStatics
=
false
;
...
...
@@ -187,8 +251,27 @@ public class CpuInfo {
traffic
=
-
1
;
else
traffic
=
(
lastestTraffic
-
initialTraffic
+
1023
)
/
1024
;
processCpuRatio
=
fomart
.
format
(
100
*
((
double
)
(
processCpu
-
processCpu2
)
/
((
double
)
(
totalCpu
-
totalCpu2
))));
totalCpuRatio
=
fomart
.
format
(
100
*
((
double
)
((
totalCpu
-
idleCpu
)
-
(
totalCpu2
-
idleCpu2
))
/
(
double
)
(
totalCpu
-
totalCpu2
)));
StringBuffer
totalCpuBuffer
=
new
StringBuffer
();
if
(
null
!=
totalCpu2
&&
totalCpu2
.
size
()
>
0
)
{
processCpuRatio
=
fomart
.
format
(
100
*
((
double
)
(
processCpu
-
processCpu2
)
/
((
double
)
(
totalCpu
.
get
(
0
)
-
totalCpu2
.
get
(
0
)))));
for
(
int
i
=
0
;
i
<
(
totalCpu
.
size
()
>
totalCpu2
.
size
()
?
totalCpu2
.
size
()
:
totalCpu
.
size
());
i
++)
{
String
cpuRatio
=
fomart
.
format
(
100
*
((
double
)
((
totalCpu
.
get
(
i
)
-
idleCpu
.
get
(
i
))
-
(
totalCpu2
.
get
(
i
)
-
idleCpu2
.
get
(
i
)))
/
(
double
)
(
totalCpu
.
get
(
i
)
-
totalCpu2
.
get
(
i
))));
totalCpuRatio
.
add
(
cpuRatio
);
totalCpuBuffer
.
append
(
cpuRatio
+
","
);
}
}
else
{
processCpuRatio
=
"0"
;
totalCpuRatio
.
add
(
"0"
);
totalCpuBuffer
.
append
(
"0,"
);
totalCpu2
=
(
ArrayList
<
Long
>)
totalCpu
.
clone
();
processCpu2
=
processCpu
;
idleCpu2
=
(
ArrayList
<
Long
>)
idleCpu
.
clone
();
}
// 多核cpu的值写入csv文件中
for
(
int
i
=
0
;
i
<
getCpuNum
()-
totalCpuRatio
.
size
()+
1
;
i
++){
totalCpuBuffer
.
append
(
"N/A,"
);
}
long
pidMemory
=
mi
.
getPidMemorySize
(
pid
,
context
);
String
pMemory
=
fomart
.
format
((
double
)
pidMemory
/
1024
);
long
freeMemory
=
mi
.
getFreeMemorySize
(
context
);
...
...
@@ -198,20 +281,20 @@ public class CpuInfo {
percent
=
fomart
.
format
(((
double
)
pidMemory
/
(
double
)
totalMemorySize
)
*
100
);
}
if
(
isPositive
(
processCpuRatio
)
&&
isPositive
(
totalCpuRatio
))
{
if
(
isPositive
(
processCpuRatio
)
&&
isPositive
(
totalCpuRatio
.
get
(
0
)
))
{
// whether certain device supports traffic statics or not
if
(
traffic
==
-
1
)
{
EmmageeService
.
bw
.
write
(
mDateTime2
+
","
+
pMemory
+
","
+
percent
+
","
+
fMemory
+
","
+
processCpuRatio
+
","
+
totalCpu
Ratio
+
","
+
"N/A"
+
","
+
totalBatt
+
","
+
currentBatt
+
","
+
temperature
+
","
+
voltage
+
"\r\n"
);
+
totalCpu
Buffer
.
toString
()
+
"N/A"
+
","
+
totalBatt
+
","
+
currentBatt
+
","
+
temperature
+
","
+
voltage
+
"\r\n"
);
}
else
{
EmmageeService
.
bw
.
write
(
mDateTime2
+
","
+
pMemory
+
","
+
percent
+
","
+
fMemory
+
","
+
processCpuRatio
+
","
+
totalCpu
Ratio
+
","
+
traffic
+
","
+
totalBatt
+
","
+
currentBatt
+
","
+
temperature
+
","
+
voltage
+
"\r\n"
);
+
totalCpu
Buffer
.
toString
()
+
traffic
+
","
+
totalBatt
+
","
+
currentBatt
+
","
+
temperature
+
","
+
voltage
+
"\r\n"
);
}
totalCpu2
=
totalCpu
;
totalCpu2
=
(
ArrayList
<
Long
>)
totalCpu
.
clone
()
;
processCpu2
=
processCpu
;
idleCpu2
=
idleCpu
;
idleCpu2
=
(
ArrayList
<
Long
>)
idleCpu
.
clone
()
;
cpuUsedRatio
.
add
(
processCpuRatio
);
cpuUsedRatio
.
add
(
totalCpuRatio
);
cpuUsedRatio
.
add
(
totalCpuRatio
.
get
(
0
)
);
cpuUsedRatio
.
add
(
String
.
valueOf
(
traffic
));
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录