android_os_Debug.cpp revision d24b8183b93e781080b2c16c487e60d51c12da31
1/*
2 * Copyright (C) 2007 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#include "JNIHelp.h"
18#include "jni.h"
19#include "utils/misc.h"
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include <time.h>
26#include <sys/time.h>
27
28#ifdef HAVE_MALLOC_H
29#include <malloc.h>
30#endif
31
32namespace android
33{
34
35static jfieldID dalvikPss_field;
36static jfieldID dalvikPrivateDirty_field;
37static jfieldID dalvikSharedDirty_field;
38static jfieldID nativePss_field;
39static jfieldID nativePrivateDirty_field;
40static jfieldID nativeSharedDirty_field;
41static jfieldID otherPss_field;
42static jfieldID otherPrivateDirty_field;
43static jfieldID otherSharedDirty_field;
44
45struct stats_t {
46    int dalvikPss;
47    int dalvikPrivateDirty;
48    int dalvikSharedDirty;
49
50    int nativePss;
51    int nativePrivateDirty;
52    int nativeSharedDirty;
53
54    int otherPss;
55    int otherPrivateDirty;
56    int otherSharedDirty;
57};
58
59#define BINDER_STATS "/proc/binder/stats"
60
61static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
62{
63#ifdef HAVE_MALLOC_H
64    struct mallinfo info = mallinfo();
65    return (jlong) info.usmblks;
66#else
67    return -1;
68#endif
69}
70
71static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
72{
73#ifdef HAVE_MALLOC_H
74    struct mallinfo info = mallinfo();
75    return (jlong) info.uordblks;
76#else
77    return -1;
78#endif
79}
80
81static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
82{
83#ifdef HAVE_MALLOC_H
84    struct mallinfo info = mallinfo();
85    return (jlong) info.fordblks;
86#else
87    return -1;
88#endif
89}
90
91static void read_mapinfo(FILE *fp, stats_t* stats)
92{
93    char line[1024];
94    int len;
95    bool skip, done = false;
96
97    unsigned start = 0, size = 0, resident = 0, pss = 0;
98    unsigned shared_clean = 0, shared_dirty = 0;
99    unsigned private_clean = 0, private_dirty = 0;
100    unsigned referenced = 0;
101    unsigned temp;
102
103    int isNativeHeap;
104    int isDalvikHeap;
105    int isSqliteHeap;
106
107    if(fgets(line, 1024, fp) == 0) return;
108
109    while (!done) {
110        isNativeHeap = 0;
111        isDalvikHeap = 0;
112        isSqliteHeap = 0;
113        skip = false;
114
115        len = strlen(line);
116        if (len < 1) return;
117        line[--len] = 0;
118
119        /* ignore guard pages */
120        if (len > 18 && line[18] == '-') skip = true;
121
122        start = strtoul(line, 0, 16);
123
124        if (strstr("[heap]", line)) {
125            isNativeHeap = 1;
126        } else if (strstr("/dalvik-LinearAlloc", line)) {
127            isDalvikHeap = 1;
128        } else if (strstr("/mspace/dalvik-heap", line)) {
129            isDalvikHeap = 1;
130        } else if (strstr("/dalvik-heap-bitmap/", line)) {
131            isDalvikHeap = 1;
132        } else if (strstr("/tmp/sqlite-heap", line)) {
133            isSqliteHeap = 1;
134        }
135
136        while (true) {
137            if (fgets(line, 1024, fp) == 0) {
138                done = true;
139                break;
140            }
141
142            if (sscanf(line, "Size: %d kB", &temp) == 1) {
143                size = temp;
144            } else if (sscanf(line, "Rss: %d kB", &temp) == 1) {
145                resident = temp;
146            } else if (sscanf(line, "Pss: %d kB", &temp) == 1) {
147                pss = temp;
148            } else if (sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
149                shared_clean = temp;
150            } else if (sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
151                shared_dirty = temp;
152            } else if (sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
153                private_clean = temp;
154            } else if (sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
155                private_dirty = temp;
156            } else if (sscanf(line, "Referenced: %d kB", &temp) == 1) {
157                referenced = temp;
158            } else if (strlen(line) > 40 && line[8] == '-' && line[17] == ' ') {
159                // looks like a new mapping
160                // example: "0000a000-00232000 rwxp 0000a000 00:00 0          [heap]"
161                break;
162            }
163        }
164
165        if (!skip) {
166            if (isNativeHeap) {
167                stats->nativePss += pss;
168                stats->nativePrivateDirty += private_dirty;
169                stats->nativeSharedDirty += shared_dirty;
170            } else if (isDalvikHeap) {
171                stats->dalvikPss += pss;
172                stats->dalvikPrivateDirty += private_dirty;
173                stats->dalvikSharedDirty += shared_dirty;
174            } else if ( isSqliteHeap) {
175                // ignore
176            } else {
177                stats->otherPss += pss;
178                stats->otherPrivateDirty += shared_dirty;
179                stats->otherSharedDirty += private_dirty;
180            }
181        }
182    }
183}
184
185static void load_maps(int pid, stats_t* stats)
186{
187    char tmp[128];
188    FILE *fp;
189
190    sprintf(tmp, "/proc/%d/smaps", pid);
191    fp = fopen(tmp, "r");
192    if (fp == 0) return;
193
194    read_mapinfo(fp, stats);
195    fclose(fp);
196}
197
198static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
199{
200    stats_t stats;
201    memset(&stats, 0, sizeof(stats_t));
202
203    load_maps(getpid(), &stats);
204
205    env->SetIntField(object, dalvikPss_field, stats.dalvikPss);
206    env->SetIntField(object, dalvikPrivateDirty_field, stats.dalvikPrivateDirty);
207    env->SetIntField(object, dalvikSharedDirty_field, stats.dalvikSharedDirty);
208
209    env->SetIntField(object, nativePss_field, stats.nativePss);
210    env->SetIntField(object, nativePrivateDirty_field, stats.nativePrivateDirty);
211    env->SetIntField(object, nativeSharedDirty_field, stats.nativeSharedDirty);
212
213    env->SetIntField(object, otherPss_field, stats.otherPss);
214    env->SetIntField(object, otherPrivateDirty_field, stats.otherPrivateDirty);
215    env->SetIntField(object, otherSharedDirty_field, stats.otherSharedDirty);
216}
217
218static jint read_binder_stat(const char* stat)
219{
220    FILE* fp = fopen(BINDER_STATS, "r");
221    if (fp == NULL) {
222        return -1;
223    }
224
225    char line[1024];
226
227    char compare[128];
228    int len = snprintf(compare, 128, "proc %d", getpid());
229
230    // loop until we have the block that represents this process
231    do {
232        if (fgets(line, 1024, fp) == 0) {
233            return -1;
234        }
235    } while (strncmp(compare, line, len));
236
237    // now that we have this process, read until we find the stat that we are looking for
238    len = snprintf(compare, 128, "  %s: ", stat);
239
240    do {
241        if (fgets(line, 1024, fp) == 0) {
242            return -1;
243        }
244    } while (strncmp(compare, line, len));
245
246    // we have the line, now increment the line ptr to the value
247    char* ptr = line + len;
248    return atoi(ptr);
249}
250
251static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
252{
253    return read_binder_stat("bcTRANSACTION");
254}
255
256static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
257{
258    return read_binder_stat("brTRANSACTION");
259}
260
261// these are implemented in android_util_Binder.cpp
262jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
263jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
264jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
265
266/*
267 * JNI registration.
268 */
269
270static JNINativeMethod gMethods[] = {
271    { "getNativeHeapSize",      "()J",
272            (void*) android_os_Debug_getNativeHeapSize },
273    { "getNativeHeapAllocatedSize", "()J",
274            (void*) android_os_Debug_getNativeHeapAllocatedSize },
275    { "getNativeHeapFreeSize",  "()J",
276            (void*) android_os_Debug_getNativeHeapFreeSize },
277    { "getMemoryInfo",          "(Landroid/os/Debug$MemoryInfo;)V",
278            (void*) android_os_Debug_getDirtyPages },
279    { "getBinderSentTransactions", "()I",
280            (void*) android_os_Debug_getBinderSentTransactions },
281    { "getBinderReceivedTransactions", "()I",
282            (void*) android_os_getBinderReceivedTransactions },
283    { "getBinderLocalObjectCount", "()I",
284            (void*)android_os_Debug_getLocalObjectCount },
285    { "getBinderProxyObjectCount", "()I",
286            (void*)android_os_Debug_getProxyObjectCount },
287    { "getBinderDeathObjectCount", "()I",
288            (void*)android_os_Debug_getDeathObjectCount },
289};
290
291int register_android_os_Debug(JNIEnv *env)
292{
293    jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
294
295    dalvikPss_field = env->GetFieldID(clazz, "dalvikPss", "I");
296    dalvikPrivateDirty_field = env->GetFieldID(clazz, "dalvikPrivateDirty", "I");
297    dalvikSharedDirty_field = env->GetFieldID(clazz, "dalvikSharedDirty", "I");
298
299    nativePss_field = env->GetFieldID(clazz, "nativePss", "I");
300    nativePrivateDirty_field = env->GetFieldID(clazz, "nativePrivateDirty", "I");
301    nativeSharedDirty_field = env->GetFieldID(clazz, "nativeSharedDirty", "I");
302
303    otherPss_field = env->GetFieldID(clazz, "otherPss", "I");
304    otherPrivateDirty_field = env->GetFieldID(clazz, "otherPrivateDirty", "I");
305    otherSharedDirty_field = env->GetFieldID(clazz, "otherSharedDirty", "I");
306
307    return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
308}
309
310};
311