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