From 5d0a85c8407950abddda120efb0ec10de5aa4611 Mon Sep 17 00:00:00 2001 From: andrewleo Date: Fri, 12 Sep 2014 11:10:07 +0800 Subject: [PATCH] Added: calculate usage rate of each cpu, and record them in csv --- project.properties | 2 +- .../qa/emmagee/service/EmmageeService.java | 17 ++- src/com/netease/qa/emmagee/utils/CpuInfo.java | 135 ++++++++++++++---- 3 files changed, 118 insertions(+), 36 deletions(-) diff --git a/project.properties b/project.properties index f049142..ea89160 100644 --- a/project.properties +++ b/project.properties @@ -8,4 +8,4 @@ # project structure. # Project target. -target=android-10 +target=android-8 diff --git a/src/com/netease/qa/emmagee/service/EmmageeService.java b/src/com/netease/qa/emmagee/service/EmmageeService.java index 3eaffbb..16dc947 100644 --- a/src/com/netease/qa/emmagee/service/EmmageeService.java +++ b/src/com/netease/qa/emmagee/service/EmmageeService.java @@ -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 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 processInfo = cpuInfo.getCpuRatioInfo(totalBatt, currentBatt, temperature, voltage); if (isFloating) { String processCpuRatio = "0"; diff --git a/src/com/netease/qa/emmagee/utils/CpuInfo.java b/src/com/netease/qa/emmagee/utils/CpuInfo.java index 607dae6..436150d 100644 --- a/src/com/netease/qa/emmagee/utils/CpuInfo.java +++ b/src/com/netease/qa/emmagee/utils/CpuInfo.java @@ -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 idleCpu = new ArrayList(); + private ArrayList totalCpu = new ArrayList(); 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 cpuUsedRatio; - private long totalCpu2; + private ArrayList cpuUsedRatio = new ArrayList(); + private ArrayList totalCpu2 = new ArrayList(); private long processCpu2; - private long idleCpu2; + private ArrayList idleCpu2 = new ArrayList(); private String processCpuRatio = ""; - private String totalCpuRatio = ""; + private ArrayList totalCpuRatio = new ArrayList(); 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 getCpuList() { + ArrayList cpuList = new ArrayList(); + 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) totalCpu.clone(); + processCpu2 = processCpu; + idleCpu2 = (ArrayList) idleCpu.clone(); + } + // 多核cpu的值写入csv文件中 + for(int i =0;i) totalCpu.clone(); processCpu2 = processCpu; - idleCpu2 = idleCpu; + idleCpu2 = (ArrayList) idleCpu.clone(); cpuUsedRatio.add(processCpuRatio); - cpuUsedRatio.add(totalCpuRatio); + cpuUsedRatio.add(totalCpuRatio.get(0)); cpuUsedRatio.add(String.valueOf(traffic)); } } -- GitLab