1 /* 2 * Copyright (C) 2011 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.internal.util; 18 19 import android.os.Debug; 20 import android.os.StrictMode; 21 22 public final class MemInfoReader { 23 final long[] mInfos = new long[Debug.MEMINFO_COUNT]; 24 readMemInfo()25 public void readMemInfo() { 26 // Permit disk reads here, as /proc/meminfo isn't really "on 27 // disk" and should be fast. TODO: make BlockGuard ignore 28 // /proc/ and /sys/ files perhaps? 29 StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); 30 try { 31 Debug.getMemInfo(mInfos); 32 } finally { 33 StrictMode.setThreadPolicy(savedPolicy); 34 } 35 } 36 getTotalSize()37 public long getTotalSize() { 38 return mInfos[Debug.MEMINFO_TOTAL] * 1024; 39 } 40 getFreeSize()41 public long getFreeSize() { 42 return mInfos[Debug.MEMINFO_FREE] * 1024; 43 } 44 getCachedSize()45 public long getCachedSize() { 46 return mInfos[Debug.MEMINFO_CACHED] * 1024; 47 } 48 getTotalSizeKb()49 public long getTotalSizeKb() { 50 return mInfos[Debug.MEMINFO_TOTAL]; 51 } 52 getFreeSizeKb()53 public long getFreeSizeKb() { 54 return mInfos[Debug.MEMINFO_FREE]; 55 } 56 getCachedSizeKb()57 public long getCachedSizeKb() { 58 return mInfos[Debug.MEMINFO_CACHED]; 59 } 60 getBuffersSizeKb()61 public long getBuffersSizeKb() { 62 return mInfos[Debug.MEMINFO_BUFFERS]; 63 } 64 getShmemSizeKb()65 public long getShmemSizeKb() { 66 return mInfos[Debug.MEMINFO_SHMEM]; 67 } 68 getSlabSizeKb()69 public long getSlabSizeKb() { 70 return mInfos[Debug.MEMINFO_SLAB]; 71 } 72 getSwapTotalSizeKb()73 public long getSwapTotalSizeKb() { 74 return mInfos[Debug.MEMINFO_SWAP_TOTAL]; 75 } 76 getSwapFreeSizeKb()77 public long getSwapFreeSizeKb() { 78 return mInfos[Debug.MEMINFO_SWAP_FREE]; 79 } 80 getZramTotalSizeKb()81 public long getZramTotalSizeKb() { 82 return mInfos[Debug.MEMINFO_ZRAM_TOTAL]; 83 } 84 } 85