From b33457456a03d5c975f6c79feae45aa9a81a563a Mon Sep 17 00:00:00 2001 From: Siva Velusamy Date: Thu, 8 Nov 2012 15:12:52 -0800 Subject: ddms: Fix procrank/meminfo parser for the Sysinfo panel. Change-Id: I86d00756242c4a3f5ac70a926065789fde428825 --- ddms/libs/ddmuilib/.classpath | 1 + ddms/libs/ddmuilib/Android.mk | 5 +- ddms/libs/ddmuilib/etc/manifest.txt | 2 +- .../src/com/android/ddmuilib/SysinfoPanel.java | 98 ++++++++++++++-------- .../com/android/ddmuilib/BugReportParserTest.java | 71 ++++++++++++++++ 5 files changed, 138 insertions(+), 39 deletions(-) diff --git a/ddms/libs/ddmuilib/.classpath b/ddms/libs/ddmuilib/.classpath index 01d110b..e2e92e6 100644 --- a/ddms/libs/ddmuilib/.classpath +++ b/ddms/libs/ddmuilib/.classpath @@ -10,5 +10,6 @@ + diff --git a/ddms/libs/ddmuilib/Android.mk b/ddms/libs/ddmuilib/Android.mk index 4b34500..22eb2e6 100644 --- a/ddms/libs/ddmuilib/Android.mk +++ b/ddms/libs/ddmuilib/Android.mk @@ -20,8 +20,9 @@ LOCAL_JAVA_LIBRARIES := \ org.eclipse.core.commands_3.6.0.I20100512-1500 \ jcommon-1.0.12 \ jfreechart-1.0.9 \ - jfreechart-1.0.9-swt - + jfreechart-1.0.9-swt \ + guava-tools + LOCAL_MODULE := ddmuilib include $(BUILD_HOST_JAVA_LIBRARY) diff --git a/ddms/libs/ddmuilib/etc/manifest.txt b/ddms/libs/ddmuilib/etc/manifest.txt index b74fd14..f222c61 100644 --- a/ddms/libs/ddmuilib/etc/manifest.txt +++ b/ddms/libs/ddmuilib/etc/manifest.txt @@ -1 +1 @@ -Class-Path: ddmlib.jar org.eclipse.jface_3.6.2.M20110210-1200.jar org.eclipse.equinox.common_3.6.0.v20100503.jar org.eclipse.core.commands_3.6.0.I20100512-1500.jar jcommon-1.0.12.jar jfreechart-1.0.9.jar jfreechart-1.0.9-swt.jar +Class-Path: ddmlib.jar org.eclipse.jface_3.6.2.M20110210-1200.jar org.eclipse.equinox.common_3.6.0.v20100503.jar org.eclipse.core.commands_3.6.0.I20100512-1500.jar jcommon-1.0.12.jar jfreechart-1.0.9.jar jfreechart-1.0.9-swt.jar guava-13.0.1.jar diff --git a/ddms/libs/ddmuilib/src/com/android/ddmuilib/SysinfoPanel.java b/ddms/libs/ddmuilib/src/com/android/ddmuilib/SysinfoPanel.java index adeb424..dad57dd 100644 --- a/ddms/libs/ddmuilib/src/com/android/ddmuilib/SysinfoPanel.java +++ b/ddms/libs/ddmuilib/src/com/android/ddmuilib/SysinfoPanel.java @@ -22,6 +22,8 @@ import com.android.ddmlib.IShellOutputReceiver; import com.android.ddmlib.Log; import com.android.ddmlib.ShellCommandUnresponsiveException; import com.android.ddmlib.TimeoutException; +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; @@ -78,14 +80,6 @@ public class SysinfoPanel extends TablePanel implements IShellOutputReceiver { private static final int MODE_SYNC = 4; // argument to dumpsys; section in the bugreport holding the data - private static final String BUGREPORT_SECTION[] = { - "cpuinfo", - "alarm", - "batteryinfo", - "MEMORY INFO", - "content" - }; - private static final String DUMP_COMMAND[] = { "dumpsys cpuinfo", "dumpsys alarm", @@ -502,7 +496,6 @@ public class SysinfoPanel extends TablePanel implements IShellOutputReceiver { } if (!found) { - System.out.println("no match: " + line); continue; } @@ -560,12 +553,12 @@ public class SysinfoPanel extends TablePanel implements IShellOutputReceiver { long other = 0; // Scan meminfo - while (true) { - String line = br.readLine(); - if (line == null) { - // End of file - break; + String line = null; + while ((line = br.readLine()) != null) { + if (line.contains("----")) { + continue; } + Matcher m = valuePattern.matcher(line); if (m.find()) { long kb = Long.parseLong(m.group(1)); @@ -594,33 +587,66 @@ public class SysinfoPanel extends TablePanel implements IShellOutputReceiver { break; } } - // Scan procrank - while (true) { - String line = br.readLine(); - if (line == null) { - break; - } - if (line.indexOf("PROCRANK") >= 0 || line.indexOf("PID") >= 0) { - // procrank header - continue; + + List procRankResults = readProcRankDataset(br, line); + for (DataValue procRank : procRankResults) { + if (procRank.value > 2000) { // only show processes using > 2000K in memory + results.add(procRank); + } else { + other += procRank.value; } - if (line.indexOf("----") >= 0) { - //end of procrank section + + total -= procRank.value; + } + + if (other > 0) { + results.add(new DataValue("Other", other)); + } + + // The Pss calculation is not necessarily accurate as accounting memory to + // a process is not accurate. So only if there really is unaccounted for memory do we + // add it to the pie. + if (total > 0) { + results.add(new DataValue("Unknown", total)); + } + + return results; + } + + static List readProcRankDataset(BufferedReader br, String header) + throws IOException { + List results = new ArrayList(); + + if (header == null || !header.contains("PID")) { + return results; + } + + Splitter PROCRANK_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults(); + List fields = Lists.newArrayList(PROCRANK_SPLITTER.split(header)); + int pssIndex = fields.indexOf("Pss"); + int cmdIndex = fields.indexOf("cmdline"); + + if (pssIndex == -1 || cmdIndex == -1) { + return results; + } + + String line; + while ((line = br.readLine()) != null) { + // Extract pss field from procrank output + fields = Lists.newArrayList(PROCRANK_SPLITTER.split(line)); + + if (fields.size() < cmdIndex) { break; } - // Extract pss field from procrank output - long pss = Long.parseLong(line.substring(23, 31).trim()); - String cmdline = line.substring(43).trim().replace("/system/bin/", ""); - // Arbitrary minimum size to display - if (pss > 2000) { - results.add(new DataValue(cmdline, pss)); - } else { - other += pss; + + String cmdline = fields.get(cmdIndex).replace("/system/bin/", ""); + String pssInK = fields.get(pssIndex); + if (pssInK.endsWith("K")) { + pssInK = pssInK.substring(0, pssInK.length() - 1); } - total -= pss; + long pss = safeParseLong(pssInK); + results.add(new DataValue(cmdline, pss)); } - results.add(new DataValue("Other", other)); - results.add(new DataValue("Unknown", total)); return results; } diff --git a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/BugReportParserTest.java b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/BugReportParserTest.java index 73a2fce..5a69ca5 100644 --- a/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/BugReportParserTest.java +++ b/ddms/libs/ddmuilib/tests/src/com/android/ddmuilib/BugReportParserTest.java @@ -63,4 +63,75 @@ public class BugReportParserTest extends TestCase { assertEquals("675/system_server (user)", data.get(0).name); assertEquals("Idle", data.get(3).name); } + + public void testParseProcRankEclair() throws IOException { + String memInfo = + " 51 39408K 37908K 18731K 14936K system_server\n" + + " 96 27432K 27432K 9501K 6816K android.process.acore\n" + + " 27 248K 248K 83K 76K /system/bin/debuggerd\n"; + BufferedReader br = new BufferedReader(new StringReader(memInfo)); + List data = BugReportParser.readProcRankDataset(br, + " PID Vss Rss Pss Uss cmdline\n"); + + assertEquals(3, data.size()); + assertEquals("debuggerd", data.get(2).name); + if (data.get(0).value - 18731 > 0.0002) { + fail("Unexpected PSS Value " + data.get(0).value); + } + } + + public void testParseProcRankJb() throws IOException { + String memInfo = + " 675 101120K 100928K 63452K 52624K system_server\n" + + "10170 82100K 82012K 58246K 53580K com.android.chrome:sandboxed_process0\n" + + " 8742 27296K 27224K 6849K 5620K com.google.android.apps.walletnfcrel\n" + + " ------ ------ ------\n" + + " 480598K 394172K TOTAL\n" + + "\n" + + "RAM: 1916984K total, 886404K free, 72036K buffers, 482544K cached, 456K shmem, 34864K slab\n"; + BufferedReader br = new BufferedReader(new StringReader(memInfo)); + List data = BugReportParser.readProcRankDataset(br, + " PID Vss Rss Pss Uss cmdline\n"); + + assertEquals(3, data.size()); + } + + public void testParseMeminfoEclair() throws IOException { + String memInfo = + "------ MEMORY INFO ------\n" + + "MemTotal: 516528 kB\n" + + "MemFree: 401036 kB\n" + + "Buffers: 0 kB\n" + + " PID Vss Rss Pss Uss cmdline\n" + + " 51 39408K 37908K 18731K 14936K system_server\n" + + " 96 27432K 27432K 9501K 6816K android.process.acore\n" + + " 297 23348K 23348K 5245K 2276K com.android.gallery\n"; + BufferedReader br = new BufferedReader(new StringReader(memInfo)); + List data = BugReportParser.readMeminfoDataset(br); + assertEquals(5, data.size()); + + assertEquals("Free", data.get(0).name); + } + + public void testParseMeminfoJb() throws IOException { + + String memInfo = // note: This dataset does not have all entries, so the totals will be off + "------ MEMORY INFO ------\n" + + "MemTotal: 1916984 kB\n" + + "MemFree: 888048 kB\n" + + "Buffers: 72036 kB\n" + + " PID Vss Rss Pss Uss cmdline\n" + + " 675 101120K 100928K 63452K 52624K system_server\n" + + "10170 82100K 82012K 58246K 53580K com.android.chrome:sandboxed_process0\n" + + " 8742 27296K 27224K 6849K 5620K com.google.android.apps.walletnfcrel\n" + + " ------ ------ ------\n" + + " 480598K 394172K TOTAL\n" + + "\n" + + "RAM: 1916984K total, 886404K free, 72036K buffers, 482544K cached, 456K shmem, 34864K slab\n"; + + BufferedReader br = new BufferedReader(new StringReader(memInfo)); + List data = BugReportParser.readMeminfoDataset(br); + + assertEquals(6, data.size()); + } } -- cgit v1.1