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
17package com.android.internal.util;
18
19import android.os.Debug;
20import android.os.StrictMode;
21
22public final class MemInfoReader {
23    final long[] mInfos = new long[Debug.MEMINFO_COUNT];
24
25    public void readMemInfo() {
26        // Permit disk reads here, as /proc/meminfo isn't really "on
27        // disk" and should be fast.  TODO: make BlockGuard ignore
28        // /proc/ and /sys/ files perhaps?
29        StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
30        try {
31            Debug.getMemInfo(mInfos);
32        } finally {
33            StrictMode.setThreadPolicy(savedPolicy);
34        }
35    }
36
37    public long getTotalSize() {
38        return mInfos[Debug.MEMINFO_TOTAL] * 1024;
39    }
40
41    public long getFreeSize() {
42        return mInfos[Debug.MEMINFO_FREE] * 1024;
43    }
44
45    public long getCachedSize() {
46        return mInfos[Debug.MEMINFO_CACHED] * 1024;
47    }
48
49    public long getTotalSizeKb() {
50        return mInfos[Debug.MEMINFO_TOTAL];
51    }
52
53    public long getFreeSizeKb() {
54        return mInfos[Debug.MEMINFO_FREE];
55    }
56
57    public long getCachedSizeKb() {
58        return mInfos[Debug.MEMINFO_CACHED];
59    }
60
61    public long getBuffersSizeKb() {
62        return mInfos[Debug.MEMINFO_BUFFERS];
63    }
64
65    public long getShmemSizeKb() {
66        return mInfos[Debug.MEMINFO_SHMEM];
67    }
68
69    public long getSlabSizeKb() {
70        return mInfos[Debug.MEMINFO_SLAB];
71    }
72
73    public long getSwapTotalSizeKb() {
74        return mInfos[Debug.MEMINFO_SWAP_TOTAL];
75    }
76
77    public long getSwapFreeSizeKb() {
78        return mInfos[Debug.MEMINFO_SWAP_FREE];
79    }
80
81    public long getZramTotalSizeKb() {
82        return mInfos[Debug.MEMINFO_ZRAM_TOTAL];
83    }
84}
85