• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.UnsupportedAppUsage;
20 import android.os.Debug;
21 import android.os.StrictMode;
22 
23 public final class MemInfoReader {
24     final long[] mInfos = new long[Debug.MEMINFO_COUNT];
25 
26     @UnsupportedAppUsage
readMemInfo()27     public void readMemInfo() {
28         // Permit disk reads here, as /proc/meminfo isn't really "on
29         // disk" and should be fast.  TODO: make BlockGuard ignore
30         // /proc/ and /sys/ files perhaps?
31         StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
32         try {
33             Debug.getMemInfo(mInfos);
34         } finally {
35             StrictMode.setThreadPolicy(savedPolicy);
36         }
37     }
38 
39     /**
40      * Total amount of RAM available to the kernel.
41      */
42     @UnsupportedAppUsage
getTotalSize()43     public long getTotalSize() {
44         return mInfos[Debug.MEMINFO_TOTAL] * 1024;
45     }
46 
47     /**
48      * Amount of RAM that is not being used for anything.
49      */
50     @UnsupportedAppUsage
getFreeSize()51     public long getFreeSize() {
52         return mInfos[Debug.MEMINFO_FREE] * 1024;
53     }
54 
55     /**
56      * Amount of RAM that the kernel is being used for caches, not counting caches
57      * that are mapped in to processes.
58      */
59     @UnsupportedAppUsage
getCachedSize()60     public long getCachedSize() {
61         return getCachedSizeKb() * 1024;
62     }
63 
64     /**
65      * Amount of RAM that is in use by the kernel for actual allocations.
66      */
getKernelUsedSize()67     public long getKernelUsedSize() {
68         return getKernelUsedSizeKb() * 1024;
69     }
70 
71     /**
72      * Total amount of RAM available to the kernel.
73      */
getTotalSizeKb()74     public long getTotalSizeKb() {
75         return mInfos[Debug.MEMINFO_TOTAL];
76     }
77 
78     /**
79      * Amount of RAM that is not being used for anything.
80      */
getFreeSizeKb()81     public long getFreeSizeKb() {
82         return mInfos[Debug.MEMINFO_FREE];
83     }
84 
85     /**
86      * Amount of RAM that the kernel is being used for caches, not counting caches
87      * that are mapped in to processes.
88      */
getCachedSizeKb()89     public long getCachedSizeKb() {
90         return mInfos[Debug.MEMINFO_BUFFERS] + mInfos[Debug.MEMINFO_SLAB_RECLAIMABLE]
91                 + mInfos[Debug.MEMINFO_CACHED] - mInfos[Debug.MEMINFO_MAPPED];
92     }
93 
94     /**
95      * Amount of RAM that is in use by the kernel for actual allocations.
96      */
getKernelUsedSizeKb()97     public long getKernelUsedSizeKb() {
98         return mInfos[Debug.MEMINFO_SHMEM] + mInfos[Debug.MEMINFO_SLAB_UNRECLAIMABLE]
99                 + mInfos[Debug.MEMINFO_VM_ALLOC_USED] + mInfos[Debug.MEMINFO_PAGE_TABLES]
100                 + mInfos[Debug.MEMINFO_KERNEL_STACK];
101     }
102 
getSwapTotalSizeKb()103     public long getSwapTotalSizeKb() {
104         return mInfos[Debug.MEMINFO_SWAP_TOTAL];
105     }
106 
getSwapFreeSizeKb()107     public long getSwapFreeSizeKb() {
108         return mInfos[Debug.MEMINFO_SWAP_FREE];
109     }
110 
getZramTotalSizeKb()111     public long getZramTotalSizeKb() {
112         return mInfos[Debug.MEMINFO_ZRAM_TOTAL];
113     }
114 
115     @UnsupportedAppUsage
getRawInfo()116     public long[] getRawInfo() {
117         return mInfos;
118     }
119 }
120