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 #define LOG_TAG "SQLiteDebug"
18 
19 #include <jni.h>
20 #include <JNIHelp.h>
21 #include <android_runtime/AndroidRuntime.h>
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <utils/Log.h>
28 
29 #include <sqlite3.h>
30 
31 #include "core_jni_helpers.h"
32 
33 namespace android {
34 
35 static struct {
36     jfieldID memoryUsed;
37     jfieldID pageCacheOverflow;
38     jfieldID largestMemAlloc;
39 } gSQLiteDebugPagerStatsClassInfo;
40 
nativeGetPagerStats(JNIEnv * env,jobject clazz,jobject statsObj)41 static void nativeGetPagerStats(JNIEnv *env, jobject clazz, jobject statsObj)
42 {
43     int memoryUsed;
44     int pageCacheOverflow;
45     int largestMemAlloc;
46     int unused;
47 
48     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, &pageCacheOverflow, &unused, 0);
51     env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.memoryUsed, memoryUsed);
52     env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.pageCacheOverflow,
53             pageCacheOverflow);
54     env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.largestMemAlloc, largestMemAlloc);
55 }
56 
57 /*
58  * JNI registration.
59  */
60 
61 static const JNINativeMethod gMethods[] =
62 {
63     { "nativeGetPagerStats", "(Landroid/database/sqlite/SQLiteDebug$PagerStats;)V",
64             (void*) nativeGetPagerStats },
65 };
66 
register_android_database_SQLiteDebug(JNIEnv * env)67 int register_android_database_SQLiteDebug(JNIEnv *env)
68 {
69     jclass clazz = FindClassOrDie(env, "android/database/sqlite/SQLiteDebug$PagerStats");
70 
71     gSQLiteDebugPagerStatsClassInfo.memoryUsed = GetFieldIDOrDie(env, clazz, "memoryUsed", "I");
72     gSQLiteDebugPagerStatsClassInfo.largestMemAlloc = GetFieldIDOrDie(env, clazz,
73             "largestMemAlloc", "I");
74     gSQLiteDebugPagerStatsClassInfo.pageCacheOverflow = GetFieldIDOrDie(env, clazz,
75             "pageCacheOverflow", "I");
76 
77     return RegisterMethodsOrDie(env, "android/database/sqlite/SQLiteDebug",
78             gMethods, NELEM(gMethods));
79 }
80 
81 } // namespace android
82