The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 21 | import android.os.Build; |
| 22 | import android.os.SystemProperties; |
Fred Quintana | c4516a7 | 2009-09-03 12:14:06 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 24 | import android.util.Printer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Provides debugging info about all SQLite databases running in the current process. |
Vasu Nori | 4dd4ab4 | 2010-01-29 16:24:23 -0800 | [diff] [blame] | 28 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | * {@hide} |
| 30 | */ |
| 31 | public final class SQLiteDebug { |
| 32 | /** |
| 33 | * Controls the printing of SQL statements as they are executed. |
| 34 | */ |
Fred Quintana | c4516a7 | 2009-09-03 12:14:06 -0700 | [diff] [blame] | 35 | public static final boolean DEBUG_SQL_STATEMENTS = |
| 36 | Log.isLoggable("SQLiteStatements", Log.VERBOSE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
Vasu Nori | 3ef94e2 | 2010-02-05 14:49:04 -0800 | [diff] [blame] | 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 | /** |
Vasu Nori | 5a03f36 | 2009-10-20 15:16:35 -0700 | [diff] [blame] | 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 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | * Controls the stack trace reporting of active cursors being |
| 53 | * finalized. |
| 54 | */ |
Fred Quintana | c4516a7 | 2009-09-03 12:14:06 -0700 | [diff] [blame] | 55 | public static final boolean DEBUG_ACTIVE_CURSOR_FINALIZATION = |
| 56 | Log.isLoggable("SQLiteCursorClosing", Log.VERBOSE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
Vasu Nori | 4dd4ab4 | 2010-01-29 16:24:23 -0800 | [diff] [blame] | 59 | * Controls the tracking of time spent holding the database lock. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | */ |
Fred Quintana | c4516a7 | 2009-09-03 12:14:06 -0700 | [diff] [blame] | 61 | public static final boolean DEBUG_LOCK_TIME_TRACKING = |
| 62 | Log.isLoggable("SQLiteLockTime", Log.VERBOSE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
Vasu Nori | 4dd4ab4 | 2010-01-29 16:24:23 -0800 | [diff] [blame] | 65 | * Controls the printing of stack traces when tracking the time spent holding the database lock. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | */ |
Fred Quintana | c4516a7 | 2009-09-03 12:14:06 -0700 | [diff] [blame] | 67 | public static final boolean DEBUG_LOCK_TIME_TRACKING_STACK_TRACE = |
| 68 | Log.isLoggable("SQLiteLockStackTrace", Log.VERBOSE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 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 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | * Contains statistics about the active pagers in the current process. |
Vasu Nori | 4dd4ab4 | 2010-01-29 16:24:23 -0800 | [diff] [blame] | 94 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | * @see #getPagerStats(PagerStats) |
| 96 | */ |
| 97 | public static class PagerStats { |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 98 | /** The total number of bytes in all pagers in the current process |
| 99 | * @deprecated not used any longer |
| 100 | */ |
| 101 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | public long totalBytes; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 103 | /** The number of bytes in referenced pages in all pagers in the current process |
| 104 | * @deprecated not used any longer |
| 105 | * */ |
| 106 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | public long referencedBytes; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 108 | /** The number of bytes in all database files opened in the current process |
| 109 | * @deprecated not used any longer |
| 110 | */ |
| 111 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | public long databaseBytes; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 113 | /** The number of pagers opened in the current process |
| 114 | * @deprecated not used any longer |
| 115 | */ |
| 116 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | public int numPagers; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 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 |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 146 | */ |
| 147 | public static class DbStats { |
| 148 | /** name of the database */ |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 149 | public String dbName; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 150 | |
| 151 | /** the page size for the database */ |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 152 | public long pageSize; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 153 | |
| 154 | /** the database size */ |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 155 | public long dbSize; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 156 | |
| 157 | /** documented here http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html */ |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 158 | public int lookaside; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 159 | |
Vasu Nori | 90a36726 | 2010-04-12 12:49:09 -0700 | [diff] [blame] | 160 | /** statement cache stats: hits/misses/cachesize */ |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 161 | public String cache; |
Vasu Nori | 90a36726 | 2010-04-12 12:49:09 -0700 | [diff] [blame] | 162 | |
| 163 | public DbStats(String dbName, long pageCount, long pageSize, int lookaside, |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 164 | int hits, int misses, int cachesize) { |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 165 | this.dbName = dbName; |
Vasu Nori | 90a36726 | 2010-04-12 12:49:09 -0700 | [diff] [blame] | 166 | this.pageSize = pageSize / 1024; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 167 | dbSize = (pageCount * pageSize) / 1024; |
| 168 | this.lookaside = lookaside; |
Vasu Nori | 90a36726 | 2010-04-12 12:49:09 -0700 | [diff] [blame] | 169 | this.cache = hits + "/" + misses + "/" + cachesize; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 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 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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(); |
Vasu Nori | 4dd4ab4 | 2010-01-29 16:24:23 -0800 | [diff] [blame] | 201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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(); |
Vasu Nori | 4dd4ab4 | 2010-01-29 16:24:23 -0800 | [diff] [blame] | 207 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | } |