| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
| 28 |
public MemInfoReader() { |
| 29 |
} |
| 30 |
|
| 31 |
@UnsupportedAppUsage |
| 32 |
public void readMemInfo() { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); |
| 37 |
try { |
| 38 |
Debug.getMemInfo(mInfos); |
| 39 |
} finally { |
| 40 |
StrictMode.setThreadPolicy(savedPolicy); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@UnsupportedAppUsage |
| 48 |
public long getTotalSize() { |
| 49 |
return mInfos[Debug.MEMINFO_TOTAL] * 1024; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 56 |
public long getFreeSize() { |
| 57 |
return mInfos[Debug.MEMINFO_FREE] * 1024; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 65 |
public long getCachedSize() { |
| 66 |
return getCachedSizeKb() * 1024; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public long getKernelUsedSize() { |
| 73 |
return getKernelUsedSizeKb() * 1024; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public long getTotalSizeKb() { |
| 80 |
return mInfos[Debug.MEMINFO_TOTAL]; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public long getFreeSizeKb() { |
| 87 |
return mInfos[Debug.MEMINFO_FREE]; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public long getCachedSizeKb() { |
| 95 |
long kReclaimable = mInfos[Debug.MEMINFO_KRECLAIMABLE]; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 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 |
|
| 109 |
|
| 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 |
|
| 119 |
public long getSwapTotalSizeKb() { |
| 120 |
return mInfos[Debug.MEMINFO_SWAP_TOTAL]; |
| 121 |
} |
| 122 |
|
| 123 |
public long getSwapFreeSizeKb() { |
| 124 |
return mInfos[Debug.MEMINFO_SWAP_FREE]; |
| 125 |
} |
| 126 |
|
| 127 |
public long getZramTotalSizeKb() { |
| 128 |
return mInfos[Debug.MEMINFO_ZRAM_TOTAL]; |
| 129 |
} |
| 130 |
|
| 131 |
@UnsupportedAppUsage |
| 132 |
public long[] getRawInfo() { |
| 133 |
return mInfos; |
| 134 |
} |
| 135 |
} |