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