Improve cached kernel memory accounting using meminfo KReclaimable field

KReclaimable field from /proc/meminfo is designed to represent total amount
of memory that kernel can reclaim when needed. This includes reclaimable
memory consumed by slab and reported in SReclaimable field as well as ION
pools. By using KReclaimable instead of SReclaimable we will account for
ION pools currently reported under "Lost RAM" category in dumpsys meminfo
report.

Bug: 138148041
Test: dumpsys meminfo
Change-Id: Ifdea234d05639db93074ab598b81db5ff5b43612
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
diff --git a/core/java/com/android/internal/util/MemInfoReader.java b/core/java/com/android/internal/util/MemInfoReader.java
index c1d129b..362bc92 100644
--- a/core/java/com/android/internal/util/MemInfoReader.java
+++ b/core/java/com/android/internal/util/MemInfoReader.java
@@ -91,7 +91,15 @@
      * that are mapped in to processes.
      */
     public long getCachedSizeKb() {
-        return mInfos[Debug.MEMINFO_BUFFERS] + mInfos[Debug.MEMINFO_SLAB_RECLAIMABLE]
+        long kReclaimable = mInfos[Debug.MEMINFO_KRECLAIMABLE];
+
+        // Note: MEMINFO_KRECLAIMABLE includes MEMINFO_SLAB_RECLAIMABLE and ION pools.
+        // Fall back to using MEMINFO_SLAB_RECLAIMABLE in case of older kernels that do
+        // not include KReclaimable meminfo field.
+        if (kReclaimable == 0) {
+            kReclaimable = mInfos[Debug.MEMINFO_SLAB_RECLAIMABLE];
+        }
+        return mInfos[Debug.MEMINFO_BUFFERS] + kReclaimable
                 + mInfos[Debug.MEMINFO_CACHED] - mInfos[Debug.MEMINFO_MAPPED];
     }