Skip to content

Commit 254fba8

Browse files
author
Jeff Brown
committed
Clean up SQLiteDebug natives.
In particular, ensure that the database is initialized. Change-Id: Ifa69a9dfa9d008af79beadbd1a25e90d0d29e66c
1 parent 48a4789 commit 254fba8

File tree

2 files changed

+39
-47
lines changed

2 files changed

+39
-47
lines changed

core/java/android/database/sqlite/SQLiteDebug.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* {@hide}
3030
*/
3131
public final class SQLiteDebug {
32+
private static native void nativeGetPagerStats(PagerStats stats);
33+
3234
/**
3335
* Controls the printing of informational SQL log messages.
3436
*/
@@ -73,7 +75,7 @@ public static final boolean shouldLogSlowQuery(long elapsedTimeMillis) {
7375
/**
7476
* Contains statistics about the active pagers in the current process.
7577
*
76-
* @see #getPagerStats(PagerStats)
78+
* @see #nativeGetPagerStats(PagerStats)
7779
*/
7880
public static class PagerStats {
7981
/** the current amount of memory checked out by sqlite using sqlite3_malloc().
@@ -136,7 +138,8 @@ public DbStats(String dbName, long pageCount, long pageSize, int lookaside,
136138
*/
137139
public static PagerStats getDatabaseInfo() {
138140
PagerStats stats = new PagerStats();
139-
getPagerStats(stats);
141+
SQLiteGlobal.initializeOnce();
142+
nativeGetPagerStats(stats);
140143
stats.dbStats = SQLiteDatabase.getDbStats();
141144
return stats;
142145
}
@@ -156,9 +159,4 @@ public static void dump(Printer printer, String[] args) {
156159

157160
SQLiteDatabase.dumpAll(printer, verbose);
158161
}
159-
160-
/**
161-
* Gathers statistics about all pagers in the current process.
162-
*/
163-
public static native void getPagerStats(PagerStats stats);
164162
}

core/jni/android_database_SQLiteDebug.cpp

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,29 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include <JNIHelp.h>
17+
#define LOG_TAG "SQLiteDebug"
18+
1819
#include <jni.h>
19-
#include <utils/misc.h>
20+
#include <JNIHelp.h>
21+
#include <android_runtime/AndroidRuntime.h>
22+
2023
#include <stdio.h>
2124
#include <stdlib.h>
2225
#include <string.h>
2326
#include <unistd.h>
24-
#include <cutils/mspace.h>
2527
#include <utils/Log.h>
2628

2729
#include <sqlite3.h>
2830

2931
namespace android {
3032

31-
static jfieldID gMemoryUsedField;
32-
static jfieldID gPageCacheOverflowField;
33-
static jfieldID gLargestMemAllocField;
34-
33+
static struct {
34+
jfieldID memoryUsed;
35+
jfieldID pageCacheOverflow;
36+
jfieldID largestMemAlloc;
37+
} gSQLiteDebugPagerStatsClassInfo;
3538

36-
#define USE_MSPACE 0
37-
38-
static void getPagerStats(JNIEnv *env, jobject clazz, jobject statsObj)
39+
static void nativeGetPagerStats(JNIEnv *env, jobject clazz, jobject statsObj)
3940
{
4041
int memoryUsed;
4142
int pageCacheOverflow;
@@ -45,9 +46,10 @@ static void getPagerStats(JNIEnv *env, jobject clazz, jobject statsObj)
4546
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &memoryUsed, &unused, 0);
4647
sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &unused, &largestMemAlloc, 0);
4748
sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &pageCacheOverflow, &unused, 0);
48-
env->SetIntField(statsObj, gMemoryUsedField, memoryUsed);
49-
env->SetIntField(statsObj, gPageCacheOverflowField, pageCacheOverflow);
50-
env->SetIntField(statsObj, gLargestMemAllocField, largestMemAlloc);
49+
env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.memoryUsed, memoryUsed);
50+
env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.pageCacheOverflow,
51+
pageCacheOverflow);
52+
env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.largestMemAlloc, largestMemAlloc);
5153
}
5254

5355
/*
@@ -56,39 +58,31 @@ static void getPagerStats(JNIEnv *env, jobject clazz, jobject statsObj)
5658

5759
static JNINativeMethod gMethods[] =
5860
{
59-
{ "getPagerStats", "(Landroid/database/sqlite/SQLiteDebug$PagerStats;)V",
60-
(void*) getPagerStats },
61+
{ "nativeGetPagerStats", "(Landroid/database/sqlite/SQLiteDebug$PagerStats;)V",
62+
(void*) nativeGetPagerStats },
6163
};
6264

65+
#define FIND_CLASS(var, className) \
66+
var = env->FindClass(className); \
67+
LOG_FATAL_IF(! var, "Unable to find class " className);
68+
69+
#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
70+
var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
71+
LOG_FATAL_IF(! var, "Unable to find field " fieldName);
72+
6373
int register_android_database_SQLiteDebug(JNIEnv *env)
6474
{
6575
jclass clazz;
76+
FIND_CLASS(clazz, "android/database/sqlite/SQLiteDebug$PagerStats");
77+
78+
GET_FIELD_ID(gSQLiteDebugPagerStatsClassInfo.memoryUsed, clazz,
79+
"memoryUsed", "I");
80+
GET_FIELD_ID(gSQLiteDebugPagerStatsClassInfo.largestMemAlloc, clazz,
81+
"largestMemAlloc", "I");
82+
GET_FIELD_ID(gSQLiteDebugPagerStatsClassInfo.pageCacheOverflow, clazz,
83+
"pageCacheOverflow", "I");
6684

67-
clazz = env->FindClass("android/database/sqlite/SQLiteDebug$PagerStats");
68-
if (clazz == NULL) {
69-
ALOGE("Can't find android/database/sqlite/SQLiteDebug$PagerStats");
70-
return -1;
71-
}
72-
73-
gMemoryUsedField = env->GetFieldID(clazz, "memoryUsed", "I");
74-
if (gMemoryUsedField == NULL) {
75-
ALOGE("Can't find memoryUsed");
76-
return -1;
77-
}
78-
79-
gLargestMemAllocField = env->GetFieldID(clazz, "largestMemAlloc", "I");
80-
if (gLargestMemAllocField == NULL) {
81-
ALOGE("Can't find largestMemAlloc");
82-
return -1;
83-
}
84-
85-
gPageCacheOverflowField = env->GetFieldID(clazz, "pageCacheOverflow", "I");
86-
if (gPageCacheOverflowField == NULL) {
87-
ALOGE("Can't find pageCacheOverflow");
88-
return -1;
89-
}
90-
91-
return jniRegisterNativeMethods(env, "android/database/sqlite/SQLiteDebug",
85+
return AndroidRuntime::registerNativeMethods(env, "android/database/sqlite/SQLiteDebug",
9286
gMethods, NELEM(gMethods));
9387
}
9488

0 commit comments

Comments
 (0)