• 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 java.io.FileInputStream;
20 
21 import android.os.StrictMode;
22 
23 public class MemInfoReader {
24     byte[] mBuffer = new byte[1024];
25 
26     private long mTotalSize;
27     private long mFreeSize;
28     private long mCachedSize;
29 
matchText(byte[] buffer, int index, String text)30     private boolean matchText(byte[] buffer, int index, String text) {
31         int N = text.length();
32         if ((index+N) >= buffer.length) {
33             return false;
34         }
35         for (int i=0; i<N; i++) {
36             if (buffer[index+i] != text.charAt(i)) {
37                 return false;
38             }
39         }
40         return true;
41     }
42 
extractMemValue(byte[] buffer, int index)43     private long extractMemValue(byte[] buffer, int index) {
44         while (index < buffer.length && buffer[index] != '\n') {
45             if (buffer[index] >= '0' && buffer[index] <= '9') {
46                 int start = index;
47                 index++;
48                 while (index < buffer.length && buffer[index] >= '0'
49                     && buffer[index] <= '9') {
50                     index++;
51                 }
52                 String str = new String(buffer, 0, start, index-start);
53                 return ((long)Integer.parseInt(str)) * 1024;
54             }
55             index++;
56         }
57         return 0;
58     }
59 
readMemInfo()60     public void readMemInfo() {
61         // Permit disk reads here, as /proc/meminfo isn't really "on
62         // disk" and should be fast.  TODO: make BlockGuard ignore
63         // /proc/ and /sys/ files perhaps?
64         StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
65         try {
66             mTotalSize = 0;
67             mFreeSize = 0;
68             mCachedSize = 0;
69             FileInputStream is = new FileInputStream("/proc/meminfo");
70             int len = is.read(mBuffer);
71             is.close();
72             final int BUFLEN = mBuffer.length;
73             int count = 0;
74             for (int i=0; i<len && count < 3; i++) {
75                 if (matchText(mBuffer, i, "MemTotal")) {
76                     i += 8;
77                     mTotalSize = extractMemValue(mBuffer, i);
78                     count++;
79                 } else if (matchText(mBuffer, i, "MemFree")) {
80                     i += 7;
81                     mFreeSize = extractMemValue(mBuffer, i);
82                     count++;
83                 } else if (matchText(mBuffer, i, "Cached")) {
84                     i += 6;
85                     mCachedSize = extractMemValue(mBuffer, i);
86                     count++;
87                 }
88                 while (i < BUFLEN && mBuffer[i] != '\n') {
89                     i++;
90                 }
91             }
92         } catch (java.io.FileNotFoundException e) {
93         } catch (java.io.IOException e) {
94         } finally {
95             StrictMode.setThreadPolicy(savedPolicy);
96         }
97     }
98 
getTotalSize()99     public long getTotalSize() {
100         return mTotalSize;
101     }
102 
getFreeSize()103     public long getFreeSize() {
104         return mFreeSize;
105     }
106 
getCachedSize()107     public long getCachedSize() {
108         return mCachedSize;
109     }
110 }
111