SQLiteDebug.java (6142B)
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 ** Modified to support SQLite extensions by the SQLite developers: 18 ** sqlite-dev@sqlite.org. 19 */ 20 21 package org.sqlite.database.sqlite; 22 23 import java.util.ArrayList; 24 25 import android.os.Build; 26 /* import android.os.SystemProperties; */ 27 import android.util.Log; 28 import android.util.Printer; 29 30 /** 31 * Provides debugging info about all SQLite databases running in the current process. 32 * 33 * {@hide} 34 */ 35 public final class SQLiteDebug { 36 private static native void nativeGetPagerStats(PagerStats stats); 37 38 /** 39 * Controls the printing of informational SQL log messages. 40 * 41 * Enable using "adb shell setprop log.tag.SQLiteLog VERBOSE". 42 */ 43 public static final boolean DEBUG_SQL_LOG = 44 Log.isLoggable("SQLiteLog", Log.VERBOSE); 45 46 /** 47 * Controls the printing of SQL statements as they are executed. 48 * 49 * Enable using "adb shell setprop log.tag.SQLiteStatements VERBOSE". 50 */ 51 public static final boolean DEBUG_SQL_STATEMENTS = 52 Log.isLoggable("SQLiteStatements", Log.VERBOSE); 53 54 /** 55 * Controls the printing of wall-clock time taken to execute SQL statements 56 * as they are executed. 57 * 58 * Enable using "adb shell setprop log.tag.SQLiteTime VERBOSE". 59 */ 60 public static final boolean DEBUG_SQL_TIME = 61 Log.isLoggable("SQLiteTime", Log.VERBOSE); 62 63 /** 64 * True to enable database performance testing instrumentation. 65 * @hide 66 */ 67 public static final boolean DEBUG_LOG_SLOW_QUERIES = false; 68 69 private SQLiteDebug() { 70 } 71 72 /** 73 * Determines whether a query should be logged. 74 * 75 * Reads the "db.log.slow_query_threshold" system property, which can be changed 76 * by the user at any time. If the value is zero, then all queries will 77 * be considered slow. If the value does not exist or is negative, then no queries will 78 * be considered slow. 79 * 80 * This value can be changed dynamically while the system is running. 81 * For example, "adb shell setprop db.log.slow_query_threshold 200" will 82 * log all queries that take 200ms or longer to run. 83 * @hide 84 */ 85 public static final boolean shouldLogSlowQuery(long elapsedTimeMillis) { 86 int slowQueryMillis = Integer.parseInt( 87 System.getProperty("db.log.slow_query_threshold", "10000") 88 ); 89 return slowQueryMillis >= 0 && elapsedTimeMillis >= slowQueryMillis; 90 } 91 92 /** 93 * Contains statistics about the active pagers in the current process. 94 * 95 * @see #nativeGetPagerStats(PagerStats) 96 */ 97 public static class PagerStats { 98 /** the current amount of memory checked out by sqlite using sqlite3_malloc(). 99 * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html 100 */ 101 public int memoryUsed; 102 103 /** the number of bytes of page cache allocation which could not be sattisfied by the 104 * SQLITE_CONFIG_PAGECACHE buffer and where forced to overflow to sqlite3_malloc(). 105 * The returned value includes allocations that overflowed because they where too large 106 * (they were larger than the "sz" parameter to SQLITE_CONFIG_PAGECACHE) and allocations 107 * that overflowed because no space was left in the page cache. 108 * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html 109 */ 110 public int pageCacheOverflow; 111 112 /** records the largest memory allocation request handed to sqlite3. 113 * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html 114 */ 115 public int largestMemAlloc; 116 117 /** a list of {@link DbStats} - one for each main database opened by the applications 118 * running on the android device 119 */ 120 public ArrayList<DbStats> dbStats; 121 } 122 123 /** 124 * contains statistics about a database 125 */ 126 public static class DbStats { 127 /** name of the database */ 128 public String dbName; 129 130 /** the page size for the database */ 131 public long pageSize; 132 133 /** the database size */ 134 public long dbSize; 135 136 /** documented here http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html */ 137 public int lookaside; 138 139 /** statement cache stats: hits/misses/cachesize */ 140 public String cache; 141 142 public DbStats(String dbName, long pageCount, long pageSize, int lookaside, 143 int hits, int misses, int cachesize) { 144 this.dbName = dbName; 145 this.pageSize = pageSize / 1024; 146 dbSize = (pageCount * pageSize) / 1024; 147 this.lookaside = lookaside; 148 this.cache = hits + "/" + misses + "/" + cachesize; 149 } 150 } 151 152 /** 153 * return all pager and database stats for the current process. 154 * @return {@link PagerStats} 155 */ 156 public static PagerStats getDatabaseInfo() { 157 PagerStats stats = new PagerStats(); 158 nativeGetPagerStats(stats); 159 stats.dbStats = SQLiteDatabase.getDbStats(); 160 return stats; 161 } 162 163 /** 164 * Dumps detailed information about all databases used by the process. 165 * @param printer The printer for dumping database state. 166 * @param args Command-line arguments supplied to dumpsys dbinfo 167 */ 168 public static void dump(Printer printer, String[] args) { 169 boolean verbose = false; 170 for (String arg : args) { 171 if (arg.equals("-v")) { 172 verbose = true; 173 } 174 } 175 176 SQLiteDatabase.dumpAll(printer, verbose); 177 } 178 }