summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Rosenfeld <mrosenfeld@google.com>2015-11-25 09:44:13 -0800
committerMichael Rosenfeld <mrosenfeld@google.com>2015-11-30 17:12:00 -0800
commite71adcabacf12f921153ed184dee3d30aca83638 (patch)
tree3a7482138dc1e4919fa2527c0d91dcab12e51965
parent21a5f6afae53e05fdfe9c9ee2cc82d9b8f9da37e (diff)
downloadloganalysis-e71adcabacf12f921153ed184dee3d30aca83638.tar.gz
Parse compact meminfo for lost RAM
Change-Id: Ic878cf94ecc5fd16ba12a81409dbb2ea09097563
-rw-r--r--src/com/android/loganalysis/item/CompactMemInfoItem.java61
-rw-r--r--src/com/android/loganalysis/parser/CompactMemInfoParser.java37
-rw-r--r--tests/src/com/android/loganalysis/UnitTests.java2
-rw-r--r--tests/src/com/android/loganalysis/parser/CompactMemInfoParserTest.java (renamed from tests/src/com/android/loganalysis/parser/CompactMemInfoTest.java)25
4 files changed, 96 insertions, 29 deletions
diff --git a/src/com/android/loganalysis/item/CompactMemInfoItem.java b/src/com/android/loganalysis/item/CompactMemInfoItem.java
index 58c3496..fff79b9 100644
--- a/src/com/android/loganalysis/item/CompactMemInfoItem.java
+++ b/src/com/android/loganalysis/item/CompactMemInfoItem.java
@@ -28,7 +28,22 @@ import java.util.Set;
* compact mem info file. Refer to CompactMemInfoParser for more details.
*/
public class CompactMemInfoItem implements IItem {
+ /** Constants for JSON output */
+ public static final String PID_JSON_KEY = "pid";
+ public static final String NAME_JSON_KEY = "name";
+ public static final String PSS_JSON_KEY = "pss";
+ public static final String TYPE_JSON_KEY = "type";
+ public static final String ACTIVITIES_JSON_KEY = "activities";
+ public static final String PROCESSES_JSON_KEY = "processes";
+ public static final String LOST_RAM_JSON_KEY = "lostRam";
+ /** Constants for attributes HashMap */
+ private static final String NAME_ATTR_KEY = "name";
+ private static final String PSS_ATTR_KEY = "pss";
+ private static final String TYPE_ATTR_KEY = "type";
+ private static final String ACTIVITIES_ATTR_KEY = "activities";
+
private Map<Integer, Map<String, Object>> mPids = new HashMap<Integer, Map<String, Object>>();
+ private long mLostRam;
@Override
public IItem merge(IItem other) throws ConflictingItemException {
@@ -44,24 +59,28 @@ public class CompactMemInfoItem implements IItem {
public JSONObject toJson() {
JSONObject object = new JSONObject();
JSONArray processes = new JSONArray();
+ // Add processes and their attributes
for(int pid : getPids()) {
JSONObject proc = new JSONObject();
try {
- proc.put("pid", pid);
- proc.put("name", getName(pid));
- proc.put("pss", getPss(pid));
- proc.put("type", getType(pid));
- proc.put("activities", hasActivities(pid));
+ proc.put(PID_JSON_KEY, pid);
+ proc.put(NAME_JSON_KEY, getName(pid));
+ proc.put(PSS_JSON_KEY, getPss(pid));
+ proc.put(TYPE_JSON_KEY, getType(pid));
+ proc.put(ACTIVITIES_JSON_KEY, hasActivities(pid));
processes.put(proc);
} catch (JSONException e) {
// ignore
}
}
try {
- object.put("processes", processes);
+ object.put(PROCESSES_JSON_KEY, processes);
+ // Add the lost RAM field
+ object.put(LOST_RAM_JSON_KEY, getLostRam());
} catch (JSONException e) {
// ignore
}
+
return object;
}
@@ -82,10 +101,10 @@ public class CompactMemInfoItem implements IItem {
*/
public void addPid(int pid, String name, String type, long pss, boolean activities) {
Map<String, Object> attributes = new HashMap<String, Object>();
- attributes.put("name", name);
- attributes.put("type", type);
- attributes.put("pss", pss);
- attributes.put("activities", activities);
+ attributes.put(NAME_ATTR_KEY, name);
+ attributes.put(TYPE_ATTR_KEY, type);
+ attributes.put(PSS_ATTR_KEY, pss);
+ attributes.put(ACTIVITIES_ATTR_KEY, activities);
mPids.put(pid, attributes);
}
@@ -93,14 +112,14 @@ public class CompactMemInfoItem implements IItem {
* Returns the name of the process with a given pid.
*/
public String getName(int pid) {
- return (String)get(pid).get("name");
+ return (String)get(pid).get(NAME_ATTR_KEY);
}
/**
* Return pss of the process with a given name.
*/
public long getPss(int pid) {
- return (Long)get(pid).get("pss");
+ return (Long)get(pid).get(PSS_ATTR_KEY);
}
/**
@@ -108,13 +127,27 @@ public class CompactMemInfoItem implements IItem {
* foreground and etc.
*/
public String getType(int pid) {
- return (String)get(pid).get("type");
+ return (String)get(pid).get(TYPE_ATTR_KEY);
}
/**
* Returns true if a process has any activities assosiated with it. False otherwise.
*/
public boolean hasActivities(int pid) {
- return (Boolean)get(pid).get("activities");
+ return (Boolean)get(pid).get(ACTIVITIES_ATTR_KEY);
+ }
+
+ /**
+ * Sets the lost RAM field
+ */
+ public void setLostRam(long lostRam) {
+ mLostRam = lostRam;
+ }
+
+ /**
+ * Returns the lost RAM field
+ */
+ public long getLostRam() {
+ return mLostRam;
}
}
diff --git a/src/com/android/loganalysis/parser/CompactMemInfoParser.java b/src/com/android/loganalysis/parser/CompactMemInfoParser.java
index 8282731..c3f29d0 100644
--- a/src/com/android/loganalysis/parser/CompactMemInfoParser.java
+++ b/src/com/android/loganalysis/parser/CompactMemInfoParser.java
@@ -17,6 +17,7 @@ package com.android.loganalysis.parser;
import com.android.loganalysis.item.CompactMemInfoItem;
+import java.lang.NumberFormatException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -36,9 +37,10 @@ import java.util.regex.Pattern;
*
*/
public class CompactMemInfoParser implements IParser {
-
private static final Pattern PROC_PREFIX = Pattern.compile(
"proc,(.+),(.+),(\\d+),(\\d+),(.?)");
+ private static final Pattern LOST_RAM_PREFIX = Pattern.compile(
+ "lostram,(.+)");
/**
* Parse compact meminfo log. Output a CompactMemInfoItem which contains
@@ -49,16 +51,31 @@ public class CompactMemInfoParser implements IParser {
CompactMemInfoItem item = new CompactMemInfoItem();
for (String line : lines) {
Matcher m = PROC_PREFIX.matcher(line);
- if (!m.matches()) continue;
-
- if (m.groupCount() != 5) continue;
+ if (m.matches()) {
+ String type = m.group(1);
+ String name = m.group(2);
+ try {
+ int pid = Integer.parseInt(m.group(3));
+ long pss = Long.parseLong(m.group(4));
+ boolean activities = "a".equals(m.group(5));
+ item.addPid(pid, name, type, pss, activities);
+ continue;
+ } catch (NumberFormatException nfe) {
+ // ignore exception
+ }
+ }
- String type = m.group(1);
- String name = m.group(2);
- int pid = Integer.parseInt(m.group(3));
- long pss = Long.parseLong(m.group(4));
- boolean activities = "a".equals(m.group(5));
- item.addPid(pid, name, type, pss, activities);
+ m = LOST_RAM_PREFIX.matcher(line);
+ if (m.matches()) {
+ String name = "Lost RAM";
+ try {
+ long lostRam = Long.parseLong(m.group(1));
+ item.setLostRam(lostRam);
+ continue;
+ } catch (NumberFormatException nfe) {
+ // ignore exception
+ }
+ }
}
return item;
}
diff --git a/tests/src/com/android/loganalysis/UnitTests.java b/tests/src/com/android/loganalysis/UnitTests.java
index c896d90..638c2f7 100644
--- a/tests/src/com/android/loganalysis/UnitTests.java
+++ b/tests/src/com/android/loganalysis/UnitTests.java
@@ -29,6 +29,7 @@ import com.android.loganalysis.item.WakelockItemTest;
import com.android.loganalysis.parser.AbstractSectionParserTest;
import com.android.loganalysis.parser.AnrParserTest;
import com.android.loganalysis.parser.BugreportParserTest;
+import com.android.loganalysis.parser.CompactMemInfoParserTest;
import com.android.loganalysis.parser.InterruptParserTest;
import com.android.loganalysis.parser.JavaCrashParserTest;
import com.android.loganalysis.parser.KernelLogParserTest;
@@ -79,6 +80,7 @@ public class UnitTests extends TestSuite {
addTestSuite(AbstractSectionParserTest.class);
addTestSuite(AnrParserTest.class);
addTestSuite(BugreportParserTest.class);
+ addTestSuite(CompactMemInfoParserTest.class);
addTestSuite(InterruptParserTest.class);
addTestSuite(JavaCrashParserTest.class);
addTestSuite(KernelLogParserTest.class);
diff --git a/tests/src/com/android/loganalysis/parser/CompactMemInfoTest.java b/tests/src/com/android/loganalysis/parser/CompactMemInfoParserTest.java
index 32c7068..6400aca 100644
--- a/tests/src/com/android/loganalysis/parser/CompactMemInfoTest.java
+++ b/tests/src/com/android/loganalysis/parser/CompactMemInfoParserTest.java
@@ -26,9 +26,9 @@ import org.json.JSONObject;
import java.util.Arrays;
import java.util.List;
-public class CompactMemInfoTest extends TestCase {
+public class CompactMemInfoParserTest extends TestCase {
- public void testSingleLine() {
+ public void testSingleProcLine() {
List<String> input = Arrays.asList("proc,cached,com.google.android.youtube,2964,19345,e");
CompactMemInfoItem item = new CompactMemInfoParser().parse(input);
@@ -40,8 +40,17 @@ public class CompactMemInfoTest extends TestCase {
assertEquals(false, item.hasActivities(2964));
}
- public void testMalformedLine() {
- List<String> input = Arrays.asList("proc,cached,com.google.android.youtube,a,b,e");
+ public void testSingleLostRamLine() {
+ List<String> input = Arrays.asList("lostram,1005");
+ CompactMemInfoItem item = new CompactMemInfoParser().parse(input);
+ assertEquals(1005, item.getLostRam());
+ }
+
+ public void testSomeMalformedLines() {
+ List<String> input = Arrays.asList(
+ "proc,cached,com.google.android.youtube,a,b,e",
+ "lostram,a,1000",
+ "lostram,1000,a");
CompactMemInfoItem item = new CompactMemInfoParser().parse(input);
@@ -54,7 +63,8 @@ public class CompactMemInfoTest extends TestCase {
"proc,cached,com.google.android.apps.plus,2877,9604,e",
"proc,cached,com.google.android.apps.magazines,2009,20111,e",
"proc,cached,com.google.android.apps.walletnfcrel,10790,11164,e",
- "proc,cached,com.google.android.incallui,3410,9491,e");
+ "proc,cached,com.google.android.incallui,3410,9491,e",
+ "lostram,1005");
CompactMemInfoItem item = new CompactMemInfoParser().parse(input);
@@ -63,6 +73,8 @@ public class CompactMemInfoTest extends TestCase {
assertEquals(19345, item.getPss(2964));
assertEquals("cached", item.getType(2964));
assertEquals(false, item.hasActivities(2964));
+
+ assertEquals(1005, item.getLostRam());
}
public void testSkipNonProcLines() {
@@ -94,6 +106,7 @@ public class CompactMemInfoTest extends TestCase {
"proc,cached,com.google.android.apps.magazines,2009,20111,e",
"proc,cached,com.google.android.apps.walletnfcrel,10790,11164,e",
"proc,cached,com.google.android.incallui,3410,9491,e",
+ "lostram,1005",
"cat,Native,63169");
CompactMemInfoItem item = new CompactMemInfoParser().parse(input);
@@ -102,5 +115,7 @@ public class CompactMemInfoTest extends TestCase {
JSONArray processes = json.getJSONArray("processes");
assertEquals(5, processes.length());
+
+ assertEquals(1005, (long)json.get("lostRam"));
}
}