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