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