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 
37     /**
38      * Total amount of RAM available to the kernel.
39      */
getTotalSize()40     public long getTotalSize() {
41         return mInfos[Debug.MEMINFO_TOTAL] * 1024;
42     }
43 
44     /**
45      * Amount of RAM that is not being used for anything.
46      */
getFreeSize()47     public long getFreeSize() {
48         return mInfos[Debug.MEMINFO_FREE] * 1024;
49     }
50 
51     /**
52      * Amount of RAM that the kernel is being used for caches, not counting caches
53      * that are mapped in to processes.
54      */
getCachedSize()55     public long getCachedSize() {
56         return getCachedSizeKb() * 1024;
57     }
58 
59     /**
60      * Amount of RAM that is in use by the kernel for actual allocations.
61      */
getKernelUsedSize()62     public long getKernelUsedSize() {
63         return getKernelUsedSizeKb() * 1024;
64     }
65 
66     /**
67      * Total amount of RAM available to the kernel.
68      */
getTotalSizeKb()69     public long getTotalSizeKb() {
70         return mInfos[Debug.MEMINFO_TOTAL];
71     }
72 
73     /**
74      * Amount of RAM that is not being used for anything.
75      */
getFreeSizeKb()76     public long getFreeSizeKb() {
77         return mInfos[Debug.MEMINFO_FREE];
78     }
79 
80     /**
81      * Amount of RAM that the kernel is being used for caches, not counting caches
82      * that are mapped in to processes.
83      */
getCachedSizeKb()84     public long getCachedSizeKb() {
85         return mInfos[Debug.MEMINFO_BUFFERS] + mInfos[Debug.MEMINFO_SLAB_RECLAIMABLE]
86                 + mInfos[Debug.MEMINFO_CACHED] - mInfos[Debug.MEMINFO_MAPPED];
87     }
88 
89     /**
90      * Amount of RAM that is in use by the kernel for actual allocations.
91      */
getKernelUsedSizeKb()92     public long getKernelUsedSizeKb() {
93         return mInfos[Debug.MEMINFO_SHMEM] + mInfos[Debug.MEMINFO_SLAB_UNRECLAIMABLE]
94                 + mInfos[Debug.MEMINFO_VM_ALLOC_USED] + mInfos[Debug.MEMINFO_PAGE_TABLES]
95                 + mInfos[Debug.MEMINFO_KERNEL_STACK];
96     }
97 
getSwapTotalSizeKb()98     public long getSwapTotalSizeKb() {
99         return mInfos[Debug.MEMINFO_SWAP_TOTAL];
100     }
101 
getSwapFreeSizeKb()102     public long getSwapFreeSizeKb() {
103         return mInfos[Debug.MEMINFO_SWAP_FREE];
104     }
105 
getZramTotalSizeKb()106     public long getZramTotalSizeKb() {
107         return mInfos[Debug.MEMINFO_ZRAM_TOTAL];
108     }
109 
getRawInfo()110     public long[] getRawInfo() {
111         return mInfos;
112     }
113 }
114