android_os_Debug.cpp revision 3001a035439d8134a7d70d796376d1dfbff3cdcd
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[17] == '-') skip = true;
121
122        start = strtoul(line, 0, 16);
123
124        if (strstr(line, "[heap]")) {
125            isNativeHeap = 1;
126        } else if (strstr(line, "/dalvik-LinearAlloc")) {
127            isDalvikHeap = 1;
128        } else if (strstr(line, "/mspace/dalvik-heap")) {
129            isDalvikHeap = 1;
130        } else if (strstr(line, "/dalvik-heap-bitmap/")) {
131            isDalvikHeap = 1;
132        } else if (strstr(line, "/tmp/sqlite-heap")) {
133            isSqliteHeap = 1;
134        }
135
136        //LOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
137        //    isSqliteHeap, line);
138
139        while (true) {
140            if (fgets(line, 1024, fp) == 0) {
141                done = true;
142                break;
143            }
144
145            if (sscanf(line, "Size: %d kB", &temp) == 1) {
146                size = temp;
147            } else if (sscanf(line, "Rss: %d kB", &temp) == 1) {
148                resident = temp;
149            } else if (sscanf(line, "Pss: %d kB", &temp) == 1) {
150                pss = temp;
151            } else if (sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
152                shared_clean = temp;
153            } else if (sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
154                shared_dirty = temp;
155            } else if (sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
156                private_clean = temp;
157            } else if (sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
158                private_dirty = temp;
159            } else if (sscanf(line, "Referenced: %d kB", &temp) == 1) {
160                referenced = temp;
161            } else if (strlen(line) > 40 && line[8] == '-' && line[17] == ' ') {
162                // looks like a new mapping
163                // example: "0000a000-00232000 rwxp 0000a000 00:00 0          [heap]"
164                break;
165            }
166        }
167
168        if (!skip) {
169            if (isNativeHeap) {
170                stats->nativePss += pss;
171                stats->nativePrivateDirty += private_dirty;
172                stats->nativeSharedDirty += shared_dirty;
173            } else if (isDalvikHeap) {
174                stats->dalvikPss += pss;
175                stats->dalvikPrivateDirty += private_dirty;
176                stats->dalvikSharedDirty += shared_dirty;
177            } else if ( isSqliteHeap) {
178                // ignore
179            } else {
180                stats->otherPss += pss;
181                stats->otherPrivateDirty += shared_dirty;
182                stats->otherSharedDirty += private_dirty;
183            }
184        }
185    }
186}
187
188static void load_maps(int pid, stats_t* stats)
189{
190    char tmp[128];
191    FILE *fp;
192
193    sprintf(tmp, "/proc/%d/smaps", pid);
194    fp = fopen(tmp, "r");
195    if (fp == 0) return;
196
197    read_mapinfo(fp, stats);
198    fclose(fp);
199}
200
201static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
202{
203    stats_t stats;
204    memset(&stats, 0, sizeof(stats_t));
205
206    load_maps(getpid(), &stats);
207
208    env->SetIntField(object, dalvikPss_field, stats.dalvikPss);
209    env->SetIntField(object, dalvikPrivateDirty_field, stats.dalvikPrivateDirty);
210    env->SetIntField(object, dalvikSharedDirty_field, stats.dalvikSharedDirty);
211
212    env->SetIntField(object, nativePss_field, stats.nativePss);
213    env->SetIntField(object, nativePrivateDirty_field, stats.nativePrivateDirty);
214    env->SetIntField(object, nativeSharedDirty_field, stats.nativeSharedDirty);
215
216    env->SetIntField(object, otherPss_field, stats.otherPss);
217    env->SetIntField(object, otherPrivateDirty_field, stats.otherPrivateDirty);
218    env->SetIntField(object, otherSharedDirty_field, stats.otherSharedDirty);
219}
220
221static jint read_binder_stat(const char* stat)
222{
223    FILE* fp = fopen(BINDER_STATS, "r");
224    if (fp == NULL) {
225        return -1;
226    }
227
228    char line[1024];
229
230    char compare[128];
231    int len = snprintf(compare, 128, "proc %d", getpid());
232
233    // loop until we have the block that represents this process
234    do {
235        if (fgets(line, 1024, fp) == 0) {
236            return -1;
237        }
238    } while (strncmp(compare, line, len));
239
240    // now that we have this process, read until we find the stat that we are looking for
241    len = snprintf(compare, 128, "  %s: ", stat);
242
243    do {
244        if (fgets(line, 1024, fp) == 0) {
245            return -1;
246        }
247    } while (strncmp(compare, line, len));
248
249    // we have the line, now increment the line ptr to the value
250    char* ptr = line + len;
251    return atoi(ptr);
252}
253
254static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
255{
256    return read_binder_stat("bcTRANSACTION");
257}
258
259static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
260{
261    return read_binder_stat("brTRANSACTION");
262}
263
264// these are implemented in android_util_Binder.cpp
265jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
266jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
267jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
268
269/*
270 * JNI registration.
271 */
272
273static JNINativeMethod gMethods[] = {
274    { "getNativeHeapSize",      "()J",
275            (void*) android_os_Debug_getNativeHeapSize },
276    { "getNativeHeapAllocatedSize", "()J",
277            (void*) android_os_Debug_getNativeHeapAllocatedSize },
278    { "getNativeHeapFreeSize",  "()J",
279            (void*) android_os_Debug_getNativeHeapFreeSize },
280    { "getMemoryInfo",          "(Landroid/os/Debug$MemoryInfo;)V",
281            (void*) android_os_Debug_getDirtyPages },
282    { "getBinderSentTransactions", "()I",
283            (void*) android_os_Debug_getBinderSentTransactions },
284    { "getBinderReceivedTransactions", "()I",
285            (void*) android_os_getBinderReceivedTransactions },
286    { "getBinderLocalObjectCount", "()I",
287            (void*)android_os_Debug_getLocalObjectCount },
288    { "getBinderProxyObjectCount", "()I",
289            (void*)android_os_Debug_getProxyObjectCount },
290    { "getBinderDeathObjectCount", "()I",
291            (void*)android_os_Debug_getDeathObjectCount },
292};
293
294int register_android_os_Debug(JNIEnv *env)
295{
296    jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
297
298    dalvikPss_field = env->GetFieldID(clazz, "dalvikPss", "I");
299    dalvikPrivateDirty_field = env->GetFieldID(clazz, "dalvikPrivateDirty", "I");
300    dalvikSharedDirty_field = env->GetFieldID(clazz, "dalvikSharedDirty", "I");
301
302    nativePss_field = env->GetFieldID(clazz, "nativePss", "I");
303    nativePrivateDirty_field = env->GetFieldID(clazz, "nativePrivateDirty", "I");
304    nativeSharedDirty_field = env->GetFieldID(clazz, "nativeSharedDirty", "I");
305
306    otherPss_field = env->GetFieldID(clazz, "otherPss", "I");
307    otherPrivateDirty_field = env->GetFieldID(clazz, "otherPrivateDirty", "I");
308    otherSharedDirty_field = env->GetFieldID(clazz, "otherSharedDirty", "I");
309
310    return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
311}
312
313};
314