The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 17 | #define LOG_TAG "SQLiteDebug" |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <jni.h> |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 20 | #include <JNIHelp.h> |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | |
| 29 | #include <sqlite3.h> |
| 30 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 33 | static struct { |
| 34 | jfieldID memoryUsed; |
| 35 | jfieldID pageCacheOverflow; |
| 36 | jfieldID largestMemAlloc; |
| 37 | } gSQLiteDebugPagerStatsClassInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 39 | static void nativeGetPagerStats(JNIEnv *env, jobject clazz, jobject statsObj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | { |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 41 | int memoryUsed; |
Jeff Brown | 2a293b6 | 2012-01-19 14:02:22 -0800 | [diff] [blame] | 42 | int pageCacheOverflow; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 43 | int largestMemAlloc; |
| 44 | int unused; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 46 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &memoryUsed, &unused, 0); |
| 47 | sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &unused, &largestMemAlloc, 0); |
Jeff Brown | 2a293b6 | 2012-01-19 14:02:22 -0800 | [diff] [blame] | 48 | sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &pageCacheOverflow, &unused, 0); |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 49 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } |
| 54 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | /* |
| 56 | * JNI registration. |
| 57 | */ |
| 58 | |
| 59 | static JNINativeMethod gMethods[] = |
| 60 | { |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 61 | { "nativeGetPagerStats", "(Landroid/database/sqlite/SQLiteDebug$PagerStats;)V", |
| 62 | (void*) nativeGetPagerStats }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | int register_android_database_SQLiteDebug(JNIEnv *env) |
| 74 | { |
| 75 | jclass clazz; |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 76 | FIND_CLASS(clazz, "android/database/sqlite/SQLiteDebug$PagerStats"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 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"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 85 | return AndroidRuntime::registerNativeMethods(env, "android/database/sqlite/SQLiteDebug", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | gMethods, NELEM(gMethods)); |
| 87 | } |
| 88 | |
| 89 | } // namespace android |