Merge "Add more tests for MemoryStatUtil" into pi-dev

am: d4033e8eac

Change-Id: I61bb2caa61ed0801d85a6017a23a08686c47a51f
This commit is contained in:
Ng Zhi An
2018-03-07 00:02:46 +00:00
committed by android-build-merger
2 changed files with 125 additions and 51 deletions

View File

@@ -179,4 +179,4 @@ final class MemoryStatUtil {
/** Number of bytes of swap usage */ /** Number of bytes of swap usage */
long swapInBytes; long swapInBytes;
} }
} }

View File

@@ -17,6 +17,7 @@
package com.android.server.am; package com.android.server.am;
import static com.android.server.am.MemoryStatUtil.parseMemoryStatFromMemcg; import static com.android.server.am.MemoryStatUtil.parseMemoryStatFromMemcg;
import static com.android.server.am.MemoryStatUtil.parseMemoryStatFromProcfs;
import static com.android.server.am.MemoryStatUtil.MemoryStat; import static com.android.server.am.MemoryStatUtil.MemoryStat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -31,58 +32,131 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@SmallTest @SmallTest
public class MemoryStatUtilTest { public class MemoryStatUtilTest {
private String MEMORY_STAT_CONTENTS = String.join( private String MEMORY_STAT_CONTENTS = String.join(
"\n", "\n",
"cache 96", // keep different from total_cache to catch reading wrong value "cache 96", // keep different from total_cache to catch reading wrong value
"rss 97", // keep different from total_rss to catch reading wrong value "rss 97", // keep different from total_rss to catch reading wrong value
"rss_huge 0", "rss_huge 0",
"mapped_file 524288", "mapped_file 524288",
"writeback 0", "writeback 0",
"swap 95", // keep different from total_rss to catch reading wrong value "swap 95", // keep different from total_rss to catch reading wrong value
"pgpgin 16717", "pgpgin 16717",
"pgpgout 5037", "pgpgout 5037",
"pgfault 99", // keep different from total_pgfault to catch reading wrong value "pgfault 99", // keep different from total_pgfault to catch reading wrong value
"pgmajfault 98", // keep different from total_pgmajfault to catch reading wrong value "pgmajfault 98", // keep different from total_pgmajfault to catch reading wrong value
"inactive_anon 503808", "inactive_anon 503808",
"active_anon 46309376", "active_anon 46309376",
"inactive_file 876544", "inactive_file 876544",
"active_file 81920", "active_file 81920",
"unevictable 0", "unevictable 0",
"hierarchical_memory_limit 18446744073709551615", "hierarchical_memory_limit 18446744073709551615",
"hierarchical_memsw_limit 18446744073709551615", "hierarchical_memsw_limit 18446744073709551615",
"total_cache 4", "total_cache 4",
"total_rss 3", "total_rss 3",
"total_rss_huge 0", "total_rss_huge 0",
"total_mapped_file 524288", "total_mapped_file 524288",
"total_writeback 0", "total_writeback 0",
"total_swap 5", "total_swap 5",
"total_pgpgin 16717", "total_pgpgin 16717",
"total_pgpgout 5037", "total_pgpgout 5037",
"total_pgfault 1", "total_pgfault 1",
"total_pgmajfault 2", "total_pgmajfault 2",
"total_inactive_anon 503808", "total_inactive_anon 503808",
"total_active_anon 46309376", "total_active_anon 46309376",
"total_inactive_file 876544", "total_inactive_file 876544",
"total_active_file 81920", "total_active_file 81920",
"total_unevictable 0"); "total_unevictable 0");
private String PROC_STAT_CONTENTS = String.join(
" ",
"1040",
"(system_server)",
"S",
"544",
"544",
"0",
"0",
"-1",
"1077936448",
"1", // this is pgfault
"0",
"2", // this is pgmajfault
"0",
"44533",
"13471",
"0",
"0",
"18",
"-2",
"117",
"0",
"2206",
"1257177088",
"3", // this is rss in bytes
"4294967295",
"2936971264",
"2936991289",
"3198888320",
"3198879848",
"2903927664",
"0",
"4612",
"0",
"1073775864",
"4294967295",
"0",
"0",
"17",
"0",
"0",
"0",
"0",
"0",
"0",
"2936999088",
"2936999936",
"2958692352",
"3198888595",
"3198888671",
"3198888671",
"3198889956",
"0");
@Test @Test
public void testParseMemoryStat_parsesCorrectValues() throws Exception { public void testParseMemoryStatFromMemcg_parsesCorrectValues() throws Exception {
MemoryStat stat = parseMemoryStatFromMemcg(MEMORY_STAT_CONTENTS); MemoryStat stat = parseMemoryStatFromMemcg(MEMORY_STAT_CONTENTS);
assertEquals(stat.pgfault, 1); assertEquals(stat.pgfault, 1);
assertEquals(stat.pgmajfault, 2); assertEquals(stat.pgmajfault, 2);
assertEquals(stat.rssInBytes, 3); assertEquals(stat.rssInBytes, 3);
assertEquals(stat.cacheInBytes, 4); assertEquals(stat.cacheInBytes, 4);
assertEquals(stat.swapInBytes, 5); assertEquals(stat.swapInBytes, 5);
} }
@Test @Test
public void testParseMemoryStat_emptyMemoryStatContents() throws Exception { public void testParseMemoryStatFromMemcg_emptyMemoryStatContents() throws Exception {
MemoryStat stat = parseMemoryStatFromMemcg(""); MemoryStat stat = parseMemoryStatFromMemcg("");
assertNull(stat); assertNull(stat);
stat = parseMemoryStatFromMemcg(null); stat = parseMemoryStatFromMemcg(null);
assertNull(stat); assertNull(stat);
} }
@Test
public void testParseMemoryStatFromProcfs_parsesCorrectValues() throws Exception {
MemoryStat stat = parseMemoryStatFromProcfs(PROC_STAT_CONTENTS);
assertEquals(1, stat.pgfault);
assertEquals(2, stat.pgmajfault);
assertEquals(3, stat.rssInBytes);
assertEquals(0, stat.cacheInBytes);
assertEquals(0, stat.swapInBytes);
}
@Test
public void testParseMemoryStatFromProcfs_emptyContents() throws Exception {
MemoryStat stat = parseMemoryStatFromProcfs("");
assertNull(stat);
stat = parseMemoryStatFromProcfs(null);
assertNull(stat);
}
} }