Home | History | Annotate | Download | only in am
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.server.am;
     18 
     19 import static com.android.server.am.MemoryStatUtil.parseMemoryStatFromMemcg;
     20 import static com.android.server.am.MemoryStatUtil.parseMemoryStatFromProcfs;
     21 import static com.android.server.am.MemoryStatUtil.MemoryStat;
     22 
     23 import static org.junit.Assert.assertEquals;
     24 import static org.junit.Assert.assertNull;
     25 
     26 import android.support.test.filters.SmallTest;
     27 import android.support.test.runner.AndroidJUnit4;
     28 
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 
     32 @RunWith(AndroidJUnit4.class)
     33 @SmallTest
     34 public class MemoryStatUtilTest {
     35     private String MEMORY_STAT_CONTENTS = String.join(
     36             "\n",
     37             "cache 96", // keep different from total_cache to catch reading wrong value
     38             "rss 97", // keep different from total_rss to catch reading wrong value
     39             "rss_huge 0",
     40             "mapped_file 524288",
     41             "writeback 0",
     42             "swap 95", // keep different from total_rss to catch reading wrong value
     43             "pgpgin 16717",
     44             "pgpgout 5037",
     45             "pgfault 99", // keep different from total_pgfault to catch reading wrong value
     46             "pgmajfault 98", // keep different from total_pgmajfault to catch reading wrong value
     47             "inactive_anon 503808",
     48             "active_anon 46309376",
     49             "inactive_file 876544",
     50             "active_file 81920",
     51             "unevictable 0",
     52             "hierarchical_memory_limit 18446744073709551615",
     53             "hierarchical_memsw_limit 18446744073709551615",
     54             "total_cache 4",
     55             "total_rss 3",
     56             "total_rss_huge 0",
     57             "total_mapped_file 524288",
     58             "total_writeback 0",
     59             "total_swap 5",
     60             "total_pgpgin 16717",
     61             "total_pgpgout 5037",
     62             "total_pgfault 1",
     63             "total_pgmajfault 2",
     64             "total_inactive_anon 503808",
     65             "total_active_anon 46309376",
     66             "total_inactive_file 876544",
     67             "total_active_file 81920",
     68             "total_unevictable 0");
     69 
     70     private String PROC_STAT_CONTENTS = String.join(
     71             " ",
     72             "1040",
     73             "(system_server)",
     74             "S",
     75             "544",
     76             "544",
     77             "0",
     78             "0",
     79             "-1",
     80             "1077936448",
     81             "1", // this is pgfault
     82             "0",
     83             "2", // this is pgmajfault
     84             "0",
     85             "44533",
     86             "13471",
     87             "0",
     88             "0",
     89             "18",
     90             "-2",
     91             "117",
     92             "0",
     93             "2206",
     94             "1257177088",
     95             "3", // this is rss in bytes
     96             "4294967295",
     97             "2936971264",
     98             "2936991289",
     99             "3198888320",
    100             "3198879848",
    101             "2903927664",
    102             "0",
    103             "4612",
    104             "0",
    105             "1073775864",
    106             "4294967295",
    107             "0",
    108             "0",
    109             "17",
    110             "0",
    111             "0",
    112             "0",
    113             "0",
    114             "0",
    115             "0",
    116             "2936999088",
    117             "2936999936",
    118             "2958692352",
    119             "3198888595",
    120             "3198888671",
    121             "3198888671",
    122             "3198889956",
    123             "0");
    124 
    125     @Test
    126     public void testParseMemoryStatFromMemcg_parsesCorrectValues() throws Exception {
    127         MemoryStat stat = parseMemoryStatFromMemcg(MEMORY_STAT_CONTENTS);
    128         assertEquals(stat.pgfault, 1);
    129         assertEquals(stat.pgmajfault, 2);
    130         assertEquals(stat.rssInBytes, 3);
    131         assertEquals(stat.cacheInBytes, 4);
    132         assertEquals(stat.swapInBytes, 5);
    133     }
    134 
    135     @Test
    136     public void testParseMemoryStatFromMemcg_emptyMemoryStatContents() throws Exception {
    137         MemoryStat stat = parseMemoryStatFromMemcg("");
    138         assertNull(stat);
    139 
    140         stat = parseMemoryStatFromMemcg(null);
    141         assertNull(stat);
    142     }
    143 
    144     @Test
    145     public void testParseMemoryStatFromProcfs_parsesCorrectValues() throws Exception {
    146         MemoryStat stat = parseMemoryStatFromProcfs(PROC_STAT_CONTENTS);
    147         assertEquals(1, stat.pgfault);
    148         assertEquals(2, stat.pgmajfault);
    149         assertEquals(3, stat.rssInBytes);
    150         assertEquals(0, stat.cacheInBytes);
    151         assertEquals(0, stat.swapInBytes);
    152     }
    153 
    154     @Test
    155     public void testParseMemoryStatFromProcfs_emptyContents() throws Exception {
    156         MemoryStat stat = parseMemoryStatFromProcfs("");
    157         assertNull(stat);
    158 
    159         stat = parseMemoryStatFromProcfs(null);
    160         assertNull(stat);
    161     }
    162 }
    163