Home | History | Annotate | Download | only in jni
      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 namespace android {
     32 
     33 static struct {
     34     jfieldID memoryUsed;
     35     jfieldID pageCacheOverflow;
     36     jfieldID largestMemAlloc;
     37 } gSQLiteDebugPagerStatsClassInfo;
     38 
     39 static void nativeGetPagerStats(JNIEnv *env, jobject clazz, jobject statsObj)
     40 {
     41     int memoryUsed;
     42     int pageCacheOverflow;
     43     int largestMemAlloc;
     44     int unused;
     45 
     46     sqlite3_status(SQLITE_STATUS_MEMORY_USED, &memoryUsed, &unused, 0);
     47     sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &unused, &largestMemAlloc, 0);
     48     sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &pageCacheOverflow, &unused, 0);
     49     env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.memoryUsed, memoryUsed);
     50     env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.pageCacheOverflow,
     51             pageCacheOverflow);
     52     env->SetIntField(statsObj, gSQLiteDebugPagerStatsClassInfo.largestMemAlloc, largestMemAlloc);
     53 }
     54 
     55 /*
     56  * JNI registration.
     57  */
     58 
     59 static JNINativeMethod gMethods[] =
     60 {
     61     { "nativeGetPagerStats", "(Landroid/database/sqlite/SQLiteDebug$PagerStats;)V",
     62             (void*) nativeGetPagerStats },
     63 };
     64 
     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 
     73 int register_android_database_SQLiteDebug(JNIEnv *env)
     74 {
     75     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");
     84 
     85     return AndroidRuntime::registerNativeMethods(env, "android/database/sqlite/SQLiteDebug",
     86             gMethods, NELEM(gMethods));
     87 }
     88 
     89 } // namespace android
     90