blob: 873b2a1ba5af982906c460603f5da7845f94f43c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
24#include <cutils/mspace.h>
25#include <utils/Log.h>
26
27#include <sqlite3.h>
28
29// From mem_mspace.c in libsqlite
30extern "C" mspace sqlite3_get_mspace();
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032namespace android {
33
Vasu Noric3849202010-03-09 10:47:25 -080034static jfieldID gMemoryUsedField;
35static jfieldID gPageCacheOverfloField;
36static jfieldID gLargestMemAllocField;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38
39#define USE_MSPACE 0
40
41static void getPagerStats(JNIEnv *env, jobject clazz, jobject statsObj)
42{
Vasu Noric3849202010-03-09 10:47:25 -080043 int memoryUsed;
44 int pageCacheOverflo;
45 int largestMemAlloc;
46 int unused;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Vasu Noric3849202010-03-09 10:47:25 -080048 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &memoryUsed, &unused, 0);
49 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &unused, &largestMemAlloc, 0);
50 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &pageCacheOverflo, &unused, 0);
51 env->SetIntField(statsObj, gMemoryUsedField, memoryUsed);
52 env->SetIntField(statsObj, gPageCacheOverfloField, pageCacheOverflo);
53 env->SetIntField(statsObj, gLargestMemAllocField, largestMemAlloc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054}
55
56static jlong getHeapSize(JNIEnv *env, jobject clazz)
57{
58#if !NO_MALLINFO
59 struct mallinfo info = mspace_mallinfo(sqlite3_get_mspace());
60 struct mallinfo info = dlmallinfo();
61 return (jlong) info.usmblks;
62#elif USE_MSPACE
63 mspace space = sqlite3_get_mspace();
64 if (space != 0) {
65 return mspace_footprint(space);
66 } else {
67 return 0;
68 }
69#else
70 return 0;
71#endif
72}
73
74static jlong getHeapAllocatedSize(JNIEnv *env, jobject clazz)
75{
76#if !NO_MALLINFO
77 struct mallinfo info = mspace_mallinfo(sqlite3_get_mspace());
78 return (jlong) info.uordblks;
79#else
80 return sqlite3_memory_used();
81#endif
82}
83
84static jlong getHeapFreeSize(JNIEnv *env, jobject clazz)
85{
86#if !NO_MALLINFO
87 struct mallinfo info = mspace_mallinfo(sqlite3_get_mspace());
88 return (jlong) info.fordblks;
89#else
90 return getHeapSize(env, clazz) - sqlite3_memory_used();
91#endif
92}
93
94static int read_mapinfo(FILE *fp,
95 int *sharedPages, int *privatePages)
96{
97 char line[1024];
98 int len;
99 int skip;
100
101 unsigned start = 0, size = 0, resident = 0;
102 unsigned shared_clean = 0, shared_dirty = 0;
103 unsigned private_clean = 0, private_dirty = 0;
104 unsigned referenced = 0;
105
106 int isAnon = 0;
107 int isHeap = 0;
108
109again:
110 skip = 0;
111
112 if(fgets(line, 1024, fp) == 0) return 0;
113
114 len = strlen(line);
115 if (len < 1) return 0;
116 line[--len] = 0;
117
118 /* ignore guard pages */
119 if (line[18] == '-') skip = 1;
120
121 start = strtoul(line, 0, 16);
122
123 if (len > 50 && !strncmp(line + 49, "/tmp/sqlite-heap", strlen("/tmp/sqlite-heap"))) {
124 isHeap = 1;
125 }
126
127 if (fgets(line, 1024, fp) == 0) return 0;
128 if (sscanf(line, "Size: %d kB", &size) != 1) return 0;
129 if (fgets(line, 1024, fp) == 0) return 0;
130 if (sscanf(line, "Rss: %d kB", &resident) != 1) return 0;
131 if (fgets(line, 1024, fp) == 0) return 0;
132 if (sscanf(line, "Shared_Clean: %d kB", &shared_clean) != 1) return 0;
133 if (fgets(line, 1024, fp) == 0) return 0;
134 if (sscanf(line, "Shared_Dirty: %d kB", &shared_dirty) != 1) return 0;
135 if (fgets(line, 1024, fp) == 0) return 0;
136 if (sscanf(line, "Private_Clean: %d kB", &private_clean) != 1) return 0;
137 if (fgets(line, 1024, fp) == 0) return 0;
138 if (sscanf(line, "Private_Dirty: %d kB", &private_dirty) != 1) return 0;
139 if (fgets(line, 1024, fp) == 0) return 0;
140 if (sscanf(line, "Referenced: %d kB", &referenced) != 1) return 0;
141
142 if (skip) {
143 goto again;
144 }
145
146 if (isHeap) {
147 *sharedPages += shared_dirty;
148 *privatePages += private_dirty;
149 }
150 return 1;
151}
152
153static void load_maps(int pid, int *sharedPages, int *privatePages)
154{
155 char tmp[128];
156 FILE *fp;
157
158 sprintf(tmp, "/proc/%d/smaps", pid);
159 fp = fopen(tmp, "r");
160 if (fp == 0) return;
161
162 while (read_mapinfo(fp, sharedPages, privatePages) != 0) {
163 // Do nothing
164 }
165 fclose(fp);
166}
167
168static void getHeapDirtyPages(JNIEnv *env, jobject clazz, jintArray pages)
169{
170 int _pages[2];
171
172 _pages[0] = 0;
173 _pages[1] = 0;
174
175 load_maps(getpid(), &_pages[0], &_pages[1]);
176
177 // Convert from kbytes to 4K pages
178 _pages[0] /= 4;
179 _pages[1] /= 4;
180
181 env->SetIntArrayRegion(pages, 0, 2, _pages);
182}
183
184/*
185 * JNI registration.
186 */
187
188static JNINativeMethod gMethods[] =
189{
190 { "getPagerStats", "(Landroid/database/sqlite/SQLiteDebug$PagerStats;)V",
191 (void*) getPagerStats },
192 { "getHeapSize", "()J", (void*) getHeapSize },
193 { "getHeapAllocatedSize", "()J", (void*) getHeapAllocatedSize },
194 { "getHeapFreeSize", "()J", (void*) getHeapFreeSize },
195 { "getHeapDirtyPages", "([I)V", (void*) getHeapDirtyPages },
196};
197
198int register_android_database_SQLiteDebug(JNIEnv *env)
199{
200 jclass clazz;
201
202 clazz = env->FindClass("android/database/sqlite/SQLiteDebug$PagerStats");
203 if (clazz == NULL) {
204 LOGE("Can't find android/database/sqlite/SQLiteDebug$PagerStats");
205 return -1;
206 }
207
Vasu Noric3849202010-03-09 10:47:25 -0800208 gMemoryUsedField = env->GetFieldID(clazz, "memoryUsed", "I");
209 if (gMemoryUsedField == NULL) {
210 LOGE("Can't find memoryUsed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 return -1;
212 }
213
Vasu Noric3849202010-03-09 10:47:25 -0800214 gLargestMemAllocField = env->GetFieldID(clazz, "largestMemAlloc", "I");
215 if (gLargestMemAllocField == NULL) {
216 LOGE("Can't find largestMemAlloc");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 return -1;
218 }
219
Vasu Noric3849202010-03-09 10:47:25 -0800220 gPageCacheOverfloField = env->GetFieldID(clazz, "pageCacheOverflo", "I");
221 if (gPageCacheOverfloField == NULL) {
222 LOGE("Can't find pageCacheOverflo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 return -1;
224 }
225
226 return jniRegisterNativeMethods(env, "android/database/sqlite/SQLiteDebug",
227 gMethods, NELEM(gMethods));
228}
229
230} // namespace android