a Windows PowerShell Script used to monitor an Android process' CPU/Mem info
for used on Android
不过Android各个版本之间格式输出差异较大,需要依据具体格式调整
脚本可监控Android平台上某个应用的系统资源使用率,包括CPU,物理内存PSS,总堆内存Heap,Dalvik虚拟机堆内存。Heap和Dalvik之差即为jni的堆内存消耗。
使用之前先保证PC上adb环境可用,Android手机的驱动也安装了
然后用adb shell ps找到需要监控的应用进程,及对应的进程号processID
# define the process id that want to be monitored
$processId = "278"
# define the time interval for data collection, in seconds.
$timeInterval = 2
# define the continuous running hours.
# generally should be 24, that's for 1 day; or 72, for 3 days
# if need to take a test, set it as 5/3600, which means 5 seconds
$rHours = 2/3600
# define output data folder path, note there must be a "\" after disk & path
$disk = "C:\"
$path = "logs\"
$folder = $disk+$path
# DO NOT CHANGE BELOW IF NO NEED
# create output data folder if not exist
if (test-path $folder){echo "$folder already exit"}
else {new-item -path $disk -name $path -type directory}
# got output data file name
$ymd = get-date -format "yyyyMMdd"
$fname="monitor_"+$ymd
$fname=$fname+"_"+$processid+".log"
$outfile = "$folder"+$fname
# got Sample number according to running hours
$SampleNumber = $rHours*3600/$timeInterval
echo "********************************************"
echo " Begin collecting data..."
echo "pid is $processid"
echo "time interval is every $timeInterval seconds"
echo "plan running for $rHours hours"
echo " Total_Heap(KB) java_Heap(KB) pss(KB) CPU(%)" > $outfile
for ($i=0;$i -le $SampleNumber; $i++)
{
$d_time = get-date -format "HH:mm:ss"
$m_Dal = adb shell dumpsys meminfo $processId | select-string "Dalvik" | Out-String
$m_Dal = $m_Dal.trim() -replace "\s{2,}" ," "
$m_Dal = $m_Dal.split(" ")[-3]
$m_total = adb shell dumpsys meminfo $processId | select-string "TOTAL" | Out-String
$m_total = $m_total.trim() -replace "\s{2,}" ," "
$m_pss = $m_total.split(" ")[1]
$m_heap = $m_total.split(" ")[-3]
$cpu = adb shell dumpsys cpuinfo | select-string $processId | Out-String
$cpu = $cpu.trim()
$cpu = $cpu.split(" ")[0]
# Note: for Powershell version below 5.1, tee-object -Append not supported!
# echo $d_time" "$m_heap" "$m_Dal" "$m_pss" "$cpu | tee-object -filepath $outfile -Append
echo $d_time" "$m_heap" "$m_Dal" "$m_pss" "$cpu >> $outfile
echo "Mem:$m_pss(KB) CPU:$cpu"
sleep ($timeInterval-1)
}
echo "data output to $outfile"
echo " End collecting data... "
echo " Press any key to Exit"
echo "********************************************"
$s = Read-Host
不过Android各个版本之间格式输出差异较大,需要依据具体格式调整
脚本可监控Android平台上某个应用的系统资源使用率,包括CPU,物理内存PSS,总堆内存Heap,Dalvik虚拟机堆内存。Heap和Dalvik之差即为jni的堆内存消耗。
使用之前先保证PC上adb环境可用,Android手机的驱动也安装了
然后用adb shell ps找到需要监控的应用进程,及对应的进程号processID
# define the process id that want to be monitored
$processId = "278"
# define the time interval for data collection, in seconds.
$timeInterval = 2
# define the continuous running hours.
# generally should be 24, that's for 1 day; or 72, for 3 days
# if need to take a test, set it as 5/3600, which means 5 seconds
$rHours = 2/3600
# define output data folder path, note there must be a "\" after disk & path
$disk = "C:\"
$path = "logs\"
$folder = $disk+$path
# DO NOT CHANGE BELOW IF NO NEED
# create output data folder if not exist
if (test-path $folder){echo "$folder already exit"}
else {new-item -path $disk -name $path -type directory}
# got output data file name
$ymd = get-date -format "yyyyMMdd"
$fname="monitor_"+$ymd
$fname=$fname+"_"+$processid+".log"
$outfile = "$folder"+$fname
# got Sample number according to running hours
$SampleNumber = $rHours*3600/$timeInterval
echo "********************************************"
echo " Begin collecting data..."
echo "pid is $processid"
echo "time interval is every $timeInterval seconds"
echo "plan running for $rHours hours"
echo " Total_Heap(KB) java_Heap(KB) pss(KB) CPU(%)" > $outfile
for ($i=0;$i -le $SampleNumber; $i++)
{
$d_time = get-date -format "HH:mm:ss"
$m_Dal = adb shell dumpsys meminfo $processId | select-string "Dalvik" | Out-String
$m_Dal = $m_Dal.trim() -replace "\s{2,}" ," "
$m_Dal = $m_Dal.split(" ")[-3]
$m_total = adb shell dumpsys meminfo $processId | select-string "TOTAL" | Out-String
$m_total = $m_total.trim() -replace "\s{2,}" ," "
$m_pss = $m_total.split(" ")[1]
$m_heap = $m_total.split(" ")[-3]
$cpu = adb shell dumpsys cpuinfo | select-string $processId | Out-String
$cpu = $cpu.trim()
$cpu = $cpu.split(" ")[0]
# Note: for Powershell version below 5.1, tee-object -Append not supported!
# echo $d_time" "$m_heap" "$m_Dal" "$m_pss" "$cpu | tee-object -filepath $outfile -Append
echo $d_time" "$m_heap" "$m_Dal" "$m_pss" "$cpu >> $outfile
echo "Mem:$m_pss(KB) CPU:$cpu"
sleep ($timeInterval-1)
}
echo "data output to $outfile"
echo " End collecting data... "
echo " Press any key to Exit"
echo "********************************************"
$s = Read-Host
评论
发表评论