Skip to content
Snippets Groups Projects
Commit a81b9db0 authored by Michael Rosenfeld's avatar Michael Rosenfeld
Browse files

Add swap memory to the compact memory info parser

Change-Id: I5f6bbf8ee41311ef7dfb3d60fd76c7d902c187f8
parent f19dfae7
Branches
No related tags found
No related merge requests found
...@@ -32,6 +32,7 @@ public class CompactMemInfoItem implements IItem { ...@@ -32,6 +32,7 @@ public class CompactMemInfoItem implements IItem {
public static final String PID_JSON_KEY = "pid"; public static final String PID_JSON_KEY = "pid";
public static final String NAME_JSON_KEY = "name"; public static final String NAME_JSON_KEY = "name";
public static final String PSS_JSON_KEY = "pss"; public static final String PSS_JSON_KEY = "pss";
public static final String SWAP_JSON_KEY = "swap";
public static final String TYPE_JSON_KEY = "type"; public static final String TYPE_JSON_KEY = "type";
public static final String ACTIVITIES_JSON_KEY = "activities"; public static final String ACTIVITIES_JSON_KEY = "activities";
public static final String PROCESSES_JSON_KEY = "processes"; public static final String PROCESSES_JSON_KEY = "processes";
...@@ -39,6 +40,7 @@ public class CompactMemInfoItem implements IItem { ...@@ -39,6 +40,7 @@ public class CompactMemInfoItem implements IItem {
/** Constants for attributes HashMap */ /** Constants for attributes HashMap */
private static final String NAME_ATTR_KEY = "name"; private static final String NAME_ATTR_KEY = "name";
private static final String PSS_ATTR_KEY = "pss"; private static final String PSS_ATTR_KEY = "pss";
private static final String SWAP_ATTR_KEY = "swap";
private static final String TYPE_ATTR_KEY = "type"; private static final String TYPE_ATTR_KEY = "type";
private static final String ACTIVITIES_ATTR_KEY = "activities"; private static final String ACTIVITIES_ATTR_KEY = "activities";
...@@ -66,6 +68,7 @@ public class CompactMemInfoItem implements IItem { ...@@ -66,6 +68,7 @@ public class CompactMemInfoItem implements IItem {
proc.put(PID_JSON_KEY, pid); proc.put(PID_JSON_KEY, pid);
proc.put(NAME_JSON_KEY, getName(pid)); proc.put(NAME_JSON_KEY, getName(pid));
proc.put(PSS_JSON_KEY, getPss(pid)); proc.put(PSS_JSON_KEY, getPss(pid));
proc.put(SWAP_JSON_KEY, getSwap(pid));
proc.put(TYPE_JSON_KEY, getType(pid)); proc.put(TYPE_JSON_KEY, getType(pid));
proc.put(ACTIVITIES_JSON_KEY, hasActivities(pid)); proc.put(ACTIVITIES_JSON_KEY, hasActivities(pid));
processes.put(proc); processes.put(proc);
...@@ -99,11 +102,12 @@ public class CompactMemInfoItem implements IItem { ...@@ -99,11 +102,12 @@ public class CompactMemInfoItem implements IItem {
/** /**
* Adds a process to the list stored in this item. * Adds a process to the list stored in this item.
*/ */
public void addPid(int pid, String name, String type, long pss, boolean activities) { public void addPid(int pid, String name, String type, long pss, long swap, boolean activities) {
Map<String, Object> attributes = new HashMap<String, Object>(); Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put(NAME_ATTR_KEY, name); attributes.put(NAME_ATTR_KEY, name);
attributes.put(TYPE_ATTR_KEY, type); attributes.put(TYPE_ATTR_KEY, type);
attributes.put(PSS_ATTR_KEY, pss); attributes.put(PSS_ATTR_KEY, pss);
attributes.put(SWAP_ATTR_KEY, swap);
attributes.put(ACTIVITIES_ATTR_KEY, activities); attributes.put(ACTIVITIES_ATTR_KEY, activities);
mPids.put(pid, attributes); mPids.put(pid, attributes);
} }
...@@ -122,6 +126,13 @@ public class CompactMemInfoItem implements IItem { ...@@ -122,6 +126,13 @@ public class CompactMemInfoItem implements IItem {
return (Long)get(pid).get(PSS_ATTR_KEY); return (Long)get(pid).get(PSS_ATTR_KEY);
} }
/**
* Return swap memory of the process with a given name.
*/
public long getSwap(int pid) {
return (Long)get(pid).get(SWAP_ATTR_KEY);
}
/** /**
* Returns the type of the process with a given pid. Some possible types are native, cached, * Returns the type of the process with a given pid. Some possible types are native, cached,
* foreground and etc. * foreground and etc.
......
...@@ -38,7 +38,7 @@ import java.util.regex.Pattern; ...@@ -38,7 +38,7 @@ import java.util.regex.Pattern;
*/ */
public class CompactMemInfoParser implements IParser { public class CompactMemInfoParser implements IParser {
private static final Pattern PROC_PREFIX = Pattern.compile( private static final Pattern PROC_PREFIX = Pattern.compile(
"proc,(.+),(.+),(\\d+),(\\d+),(.?)"); "proc,(\\w+),([a-zA-Z_0-9\\.]+),(\\d+),(\\d+),((\\S+),)?(.*)");
private static final Pattern LOST_RAM_PREFIX = Pattern.compile( private static final Pattern LOST_RAM_PREFIX = Pattern.compile(
"lostram,(.+)"); "lostram,(.+)");
...@@ -57,8 +57,12 @@ public class CompactMemInfoParser implements IParser { ...@@ -57,8 +57,12 @@ public class CompactMemInfoParser implements IParser {
try { try {
int pid = Integer.parseInt(m.group(3)); int pid = Integer.parseInt(m.group(3));
long pss = Long.parseLong(m.group(4)); long pss = Long.parseLong(m.group(4));
boolean activities = "a".equals(m.group(5)); long swap = 0;
item.addPid(pid, name, type, pss, activities); if (m.group(6) != null && !"N/A".equals(m.group(6))) {
swap = Long.parseLong(m.group(6));
}
boolean activities = "a".equals(m.group(6));
item.addPid(pid, name, type, pss, swap, activities);
continue; continue;
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
// ignore exception // ignore exception
......
...@@ -28,7 +28,20 @@ import java.util.List; ...@@ -28,7 +28,20 @@ import java.util.List;
public class CompactMemInfoParserTest extends TestCase { public class CompactMemInfoParserTest extends TestCase {
public void testSingleProcLine() { public void testSingleProcLineWithSwap() {
List<String> input = Arrays.asList("proc,cached,com.google.android.youtube1,2964,19345,1005,e");
CompactMemInfoItem item = new CompactMemInfoParser().parse(input);
assertEquals(1, item.getPids().size());
assertEquals("com.google.android.youtube1", item.getName(2964));
assertEquals(19345, item.getPss(2964));
assertEquals(1005, item.getSwap(2964));
assertEquals("cached", item.getType(2964));
assertEquals(false, item.hasActivities(2964));
}
public void testSingleProcLineWithoutSwap() {
List<String> input = Arrays.asList("proc,cached,com.google.android.youtube,2964,19345,e"); List<String> input = Arrays.asList("proc,cached,com.google.android.youtube,2964,19345,e");
CompactMemInfoItem item = new CompactMemInfoParser().parse(input); CompactMemInfoItem item = new CompactMemInfoParser().parse(input);
...@@ -36,6 +49,7 @@ public class CompactMemInfoParserTest extends TestCase { ...@@ -36,6 +49,7 @@ public class CompactMemInfoParserTest extends TestCase {
assertEquals(1, item.getPids().size()); assertEquals(1, item.getPids().size());
assertEquals("com.google.android.youtube", item.getName(2964)); assertEquals("com.google.android.youtube", item.getName(2964));
assertEquals(19345, item.getPss(2964)); assertEquals(19345, item.getPss(2964));
assertEquals(0, item.getSwap(2964));
assertEquals("cached", item.getType(2964)); assertEquals("cached", item.getType(2964));
assertEquals(false, item.hasActivities(2964)); assertEquals(false, item.hasActivities(2964));
} }
...@@ -49,6 +63,9 @@ public class CompactMemInfoParserTest extends TestCase { ...@@ -49,6 +63,9 @@ public class CompactMemInfoParserTest extends TestCase {
public void testSomeMalformedLines() { public void testSomeMalformedLines() {
List<String> input = Arrays.asList( List<String> input = Arrays.asList(
"proc,cached,com.google.android.youtube,a,b,e", "proc,cached,com.google.android.youtube,a,b,e",
"proc,cached,com.google.android.youtube,2964,c,e",
"proc,cached,com.google.android.youtube,2964,e",
"proc,cached,com.google.android.youtube,2964,19345,a,e",
"lostram,a,1000", "lostram,a,1000",
"lostram,1000,a"); "lostram,1000,a");
...@@ -59,11 +76,11 @@ public class CompactMemInfoParserTest extends TestCase { ...@@ -59,11 +76,11 @@ public class CompactMemInfoParserTest extends TestCase {
public void testMultipleLines() { public void testMultipleLines() {
List<String> input = Arrays.asList( List<String> input = Arrays.asList(
"proc,cached,com.google.android.youtube,2964,19345,e", "proc,cached,com.google.android.youtube,2964,19345,123,e",
"proc,cached,com.google.android.apps.plus,2877,9604,e", "proc,cached,com.google.android.apps.plus,2877,9604,N/A,e",
"proc,cached,com.google.android.apps.magazines,2009,20111,e", "proc,cached,com.google.android.apps.magazines,2009,20111,N/A,e",
"proc,cached,com.google.android.apps.walletnfcrel,10790,11164,e", "proc,cached,com.google.android.apps.walletnfcrel,10790,11164,100,e",
"proc,cached,com.google.android.incallui,3410,9491,e", "proc,cached,com.google.android.incallui,3410,9491,N/A,e",
"lostram,1005"); "lostram,1005");
CompactMemInfoItem item = new CompactMemInfoParser().parse(input); CompactMemInfoItem item = new CompactMemInfoParser().parse(input);
...@@ -71,6 +88,7 @@ public class CompactMemInfoParserTest extends TestCase { ...@@ -71,6 +88,7 @@ public class CompactMemInfoParserTest extends TestCase {
assertEquals(5, item.getPids().size()); assertEquals(5, item.getPids().size());
assertEquals("com.google.android.youtube", item.getName(2964)); assertEquals("com.google.android.youtube", item.getName(2964));
assertEquals(19345, item.getPss(2964)); assertEquals(19345, item.getPss(2964));
assertEquals(123, item.getSwap(2964));
assertEquals("cached", item.getType(2964)); assertEquals("cached", item.getType(2964));
assertEquals(false, item.hasActivities(2964)); assertEquals(false, item.hasActivities(2964));
...@@ -82,11 +100,11 @@ public class CompactMemInfoParserTest extends TestCase { ...@@ -82,11 +100,11 @@ public class CompactMemInfoParserTest extends TestCase {
List<String> input = Arrays.asList( List<String> input = Arrays.asList(
"oom,cached,141357", "oom,cached,141357",
"proc,cached,com.google.android.youtube,2964,19345,e", "proc,cached,com.google.android.youtube,2964,19345,54321,e",
"proc,cached,com.google.android.apps.plus,2877,9604,e", "proc,cached,com.google.android.apps.plus,2877,9604,4321,e",
"proc,cached,com.google.android.apps.magazines,2009,20111,e", "proc,cached,com.google.android.apps.magazines,2009,20111,321,e",
"proc,cached,com.google.android.apps.walletnfcrel,10790,11164,e", "proc,cached,com.google.android.apps.walletnfcrel,10790,11164,21,e",
"proc,cached,com.google.android.incallui,3410,9491,e", "proc,cached,com.google.android.incallui,3410,9491,1,e",
"cat,Native,63169"); "cat,Native,63169");
CompactMemInfoItem item = new CompactMemInfoParser().parse(input); CompactMemInfoItem item = new CompactMemInfoParser().parse(input);
...@@ -94,6 +112,7 @@ public class CompactMemInfoParserTest extends TestCase { ...@@ -94,6 +112,7 @@ public class CompactMemInfoParserTest extends TestCase {
assertEquals(5, item.getPids().size()); assertEquals(5, item.getPids().size());
assertEquals("com.google.android.youtube", item.getName(2964)); assertEquals("com.google.android.youtube", item.getName(2964));
assertEquals(19345, item.getPss(2964)); assertEquals(19345, item.getPss(2964));
assertEquals(54321, item.getSwap(2964));
assertEquals("cached", item.getType(2964)); assertEquals("cached", item.getType(2964));
assertEquals(false, item.hasActivities(2964)); assertEquals(false, item.hasActivities(2964));
} }
...@@ -101,11 +120,11 @@ public class CompactMemInfoParserTest extends TestCase { ...@@ -101,11 +120,11 @@ public class CompactMemInfoParserTest extends TestCase {
public void testJson() throws JSONException { public void testJson() throws JSONException {
List<String> input = Arrays.asList( List<String> input = Arrays.asList(
"oom,cached,141357", "oom,cached,141357",
"proc,cached,com.google.android.youtube,2964,19345,e", "proc,cached,com.google.android.youtube,2964,19345,N/A,e",
"proc,cached,com.google.android.apps.plus,2877,9604,e", "proc,cached,com.google.android.apps.plus,2877,9604,50,e",
"proc,cached,com.google.android.apps.magazines,2009,20111,e", "proc,cached,com.google.android.apps.magazines,2009,20111,100,e",
"proc,cached,com.google.android.apps.walletnfcrel,10790,11164,e", "proc,cached,com.google.android.apps.walletnfcrel,10790,11164,0,e",
"proc,cached,com.google.android.incallui,3410,9491,e", "proc,cached,com.google.android.incallui,3410,9491,500,e",
"lostram,1005", "lostram,1005",
"cat,Native,63169"); "cat,Native,63169");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment