blob: c1e7305ee1444d09b3b8b24fd23048034643de42 [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
Jeff Brown254fba82012-01-19 17:06:44 -080017#define LOG_TAG "SQLiteDebug"
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <jni.h>
Jeff Brown254fba82012-01-19 17:06:44 -080020#include <JNIHelp.h>
21#include <android_runtime/AndroidRuntime.h>
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <utils/Log.h>
28
29#include <sqlite3.h>
30
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031namespace android {
32
Jeff Brown254fba82012-01-19 17:06:44 -080033static struct {
34 jfieldID memoryUsed;
35 jfieldID pageCacheOverflow;
36 jfieldID largestMemAlloc;
37} gSQLiteDebugPagerStatsClassInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Jeff Brown254fba82012-01-19 17:06:44 -080039static void nativeGetPagerStats(JNIEnv *env, jobject clazz, jobject statsObj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040{
Vasu Noric3849202010-03-09 10:47:25 -080041 int memoryUsed;
Jeff Brown2a293b62012-01-19 14:02:22 -080042 int pageCacheOverflow;
Vasu Noric3849202010-03-09 10:47:25 -080043 int largestMemAlloc;
44 int unused;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
Vasu Noric3849202010-03-09 10:47:25 -080046 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &memoryUsed, &unused, 0);
47 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &unused, &largestMemAlloc, 0);
Jeff Brown2a293b62012-01-19 14:02:22 -080048 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &pageCacheOverflow, &unused, 0);
Jeff Brown254fba82012-01-19 17:06:44 -080049 env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.memoryUsed, memoryUsed);
50 env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.pageCacheOverflow,
51 pageCacheOverflow);
52 env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.largestMemAlloc, largestMemAlloc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053}
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/*
56 * JNI registration.
57 */
58
59static JNINativeMethod gMethods[] =
60{
Jeff Brown254fba82012-01-19 17:06:44 -080061 { "nativeGetPagerStats", "(Landroid/database/sqlite/SQLiteDebug$PagerStats;)V",
62 (void*) nativeGetPagerStats },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063};
64
Jeff Brown254fba82012-01-19 17:06:44 -080065#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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073int register_android_database_SQLiteDebug(JNIEnv *env)
74{
75 jclass clazz;
Jeff Brown254fba82012-01-19 17:06:44 -080076 FIND_CLASS(clazz, "android/database/sqlite/SQLiteDebug$PagerStats");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
Jeff Brown254fba82012-01-19 17:06:44 -080078 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");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Jeff Brown254fba82012-01-19 17:06:44 -080085 return AndroidRuntime::registerNativeMethods(env, "android/database/sqlite/SQLiteDebug",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 gMethods, NELEM(gMethods));
87}
88
89} // namespace android