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