Home | History | Annotate | Download | only in sqlite
      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 package android.database.sqlite;
     18 
     19 import java.util.ArrayList;
     20 
     21 import android.util.Log;
     22 
     23 /**
     24  * Provides debugging info about all SQLite databases running in the current process.
     25  *
     26  * {@hide}
     27  */
     28 public final class SQLiteDebug {
     29     /**
     30      * Controls the printing of SQL statements as they are executed.
     31      */
     32     public static final boolean DEBUG_SQL_STATEMENTS =
     33             Log.isLoggable("SQLiteStatements", Log.VERBOSE);
     34 
     35     /**
     36      * Controls the printing of wall-clock time taken to execute SQL statements
     37      * as they are executed.
     38      */
     39     public static final boolean DEBUG_SQL_TIME =
     40             Log.isLoggable("SQLiteTime", Log.VERBOSE);
     41 
     42     /**
     43      * Controls the printing of compiled-sql-statement cache stats.
     44      */
     45     public static final boolean DEBUG_SQL_CACHE =
     46             Log.isLoggable("SQLiteCompiledSql", Log.VERBOSE);
     47 
     48     /**
     49      * Controls the stack trace reporting of active cursors being
     50      * finalized.
     51      */
     52     public static final boolean DEBUG_ACTIVE_CURSOR_FINALIZATION =
     53             Log.isLoggable("SQLiteCursorClosing", Log.VERBOSE);
     54 
     55     /**
     56      * Controls the tracking of time spent holding the database lock.
     57      */
     58     public static final boolean DEBUG_LOCK_TIME_TRACKING =
     59             Log.isLoggable("SQLiteLockTime", Log.VERBOSE);
     60 
     61     /**
     62      * Controls the printing of stack traces when tracking the time spent holding the database lock.
     63      */
     64     public static final boolean DEBUG_LOCK_TIME_TRACKING_STACK_TRACE =
     65             Log.isLoggable("SQLiteLockStackTrace", Log.VERBOSE);
     66 
     67     /**
     68      * Contains statistics about the active pagers in the current process.
     69      *
     70      * @see #getPagerStats(PagerStats)
     71      */
     72     public static class PagerStats {
     73         /** The total number of bytes in all pagers in the current process
     74          * @deprecated not used any longer
     75          */
     76         @Deprecated
     77         public long totalBytes;
     78         /** The number of bytes in referenced pages in all pagers in the current process
     79          * @deprecated not used any longer
     80          * */
     81         @Deprecated
     82         public long referencedBytes;
     83         /** The number of bytes in all database files opened in the current process
     84          * @deprecated not used any longer
     85          */
     86         @Deprecated
     87         public long databaseBytes;
     88         /** The number of pagers opened in the current process
     89          * @deprecated not used any longer
     90          */
     91         @Deprecated
     92         public int numPagers;
     93 
     94         /** the current amount of memory checked out by sqlite using sqlite3_malloc().
     95          * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html
     96          */
     97         public int memoryUsed;
     98 
     99         /** the number of bytes of page cache allocation which could not be sattisfied by the
    100          * SQLITE_CONFIG_PAGECACHE buffer and where forced to overflow to sqlite3_malloc().
    101          * The returned value includes allocations that overflowed because they where too large
    102          * (they were larger than the "sz" parameter to SQLITE_CONFIG_PAGECACHE) and allocations
    103          * that overflowed because no space was left in the page cache.
    104          * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html
    105          */
    106         public int pageCacheOverflo;
    107 
    108         /** records the largest memory allocation request handed to sqlite3.
    109          * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html
    110          */
    111         public int largestMemAlloc;
    112 
    113         /** a list of {@link DbStats} - one for each main database opened by the applications
    114          * running on the android device
    115          */
    116         public ArrayList<DbStats> dbStats;
    117     }
    118 
    119     /**
    120      * contains statistics about a database
    121      */
    122     public static class DbStats {
    123         /** name of the database */
    124         public String dbName;
    125 
    126         /** the page size for the database */
    127         public long pageSize;
    128 
    129         /** the database size */
    130         public long dbSize;
    131 
    132         /** documented here http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html */
    133         public int lookaside;
    134 
    135         public DbStats(String dbName, long pageCount, long pageSize, int lookaside) {
    136             this.dbName = dbName;
    137             this.pageSize = pageSize / 1024;
    138             dbSize = (pageCount * pageSize) / 1024;
    139             this.lookaside = lookaside;
    140         }
    141     }
    142 
    143     /**
    144      * return all pager and database stats for the current process.
    145      * @return {@link PagerStats}
    146      */
    147     public static PagerStats getDatabaseInfo() {
    148         PagerStats stats = new PagerStats();
    149         getPagerStats(stats);
    150         stats.dbStats = SQLiteDatabase.getDbStats();
    151         return stats;
    152     }
    153 
    154     /**
    155      * Gathers statistics about all pagers in the current process.
    156      */
    157     public static native void getPagerStats(PagerStats stats);
    158 
    159     /**
    160      * Returns the size of the SQLite heap.
    161      * @return The size of the SQLite heap in bytes.
    162      */
    163     public static native long getHeapSize();
    164 
    165     /**
    166      * Returns the amount of allocated memory in the SQLite heap.
    167      * @return The allocated size in bytes.
    168      */
    169     public static native long getHeapAllocatedSize();
    170 
    171     /**
    172      * Returns the amount of free memory in the SQLite heap.
    173      * @return The freed size in bytes.
    174      */
    175     public static native long getHeapFreeSize();
    176 
    177     /**
    178      * Determines the number of dirty belonging to the SQLite
    179      * heap segments of this process.  pages[0] returns the number of
    180      * shared pages, pages[1] returns the number of private pages
    181      */
    182     public static native void getHeapDirtyPages(int[] pages);
    183 
    184     private static int sNumActiveCursorsFinalized = 0;
    185 
    186     /**
    187      * Returns the number of active cursors that have been finalized. This depends on the GC having
    188      * run but is still useful for tests.
    189      */
    190     public static int getNumActiveCursorsFinalized() {
    191         return sNumActiveCursorsFinalized;
    192     }
    193 
    194     static synchronized void notifyActiveCursorFinalized() {
    195         sNumActiveCursorsFinalized++;
    196     }
    197 }
    198