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 | |
Fyodor Kupolov | b773312 | 2017-07-24 16:13:50 -0700 | [diff] [blame] | 19 | import android.annotation.TestApi; |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 20 | import android.os.Build; |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 21 | import android.os.Process; |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 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 | |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 26 | import dalvik.annotation.compat.UnsupportedAppUsage; |
| 27 | |
Fyodor Kupolov | b773312 | 2017-07-24 16:13:50 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Provides debugging info about all SQLite databases running in the current process. |
Vasu Nori | 4dd4ab4 | 2010-01-29 16:24:23 -0800 | [diff] [blame] | 32 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | * {@hide} |
| 34 | */ |
Fyodor Kupolov | 6fe565e | 2018-04-06 14:48:05 -0700 | [diff] [blame] | 35 | @TestApi |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | public final class SQLiteDebug { |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 37 | private static native void nativeGetPagerStats(PagerStats stats); |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | /** |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 40 | * Inner class to avoid getting the value frozen in zygote. |
Jeff Brown | 638eff7 | 2012-03-01 10:21:29 -0800 | [diff] [blame] | 41 | * |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 42 | * {@hide} |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 43 | */ |
Makoto Onuki | a9be33f | 2019-05-31 16:03:50 -0700 | [diff] [blame] | 44 | public static final class NoPreloadHolder { |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Controls the printing of informational SQL log messages. |
| 47 | * |
| 48 | * Enable using "adb shell setprop log.tag.SQLiteLog VERBOSE". |
| 49 | */ |
| 50 | public static final boolean DEBUG_SQL_LOG = |
| 51 | Log.isLoggable("SQLiteLog", Log.VERBOSE); |
Jeff Brown | e5360fb | 2011-10-31 17:48:13 -0700 | [diff] [blame] | 52 | |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Controls the printing of SQL statements as they are executed. |
| 55 | * |
| 56 | * Enable using "adb shell setprop log.tag.SQLiteStatements VERBOSE". |
| 57 | */ |
| 58 | public static final boolean DEBUG_SQL_STATEMENTS = |
| 59 | Log.isLoggable("SQLiteStatements", Log.VERBOSE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Controls the printing of wall-clock time taken to execute SQL statements |
| 63 | * as they are executed. |
| 64 | * |
| 65 | * Enable using "adb shell setprop log.tag.SQLiteTime VERBOSE". |
| 66 | */ |
| 67 | public static final boolean DEBUG_SQL_TIME = |
| 68 | Log.isLoggable("SQLiteTime", Log.VERBOSE); |
Vasu Nori | 3ef94e2 | 2010-02-05 14:49:04 -0800 | [diff] [blame] | 69 | |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * True to enable database performance testing instrumentation. |
| 73 | */ |
| 74 | public static final boolean DEBUG_LOG_SLOW_QUERIES = Build.IS_DEBUGGABLE; |
| 75 | |
| 76 | private static final String SLOW_QUERY_THRESHOLD_PROP = "db.log.slow_query_threshold"; |
| 77 | |
| 78 | private static final String SLOW_QUERY_THRESHOLD_UID_PROP = |
| 79 | SLOW_QUERY_THRESHOLD_PROP + "." + Process.myUid(); |
| 80 | |
| 81 | /** |
Makoto Onuki | 6cb2033 | 2018-08-07 15:57:13 -0700 | [diff] [blame] | 82 | * Whether to add detailed information to slow query log. |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 83 | */ |
Makoto Onuki | 6cb2033 | 2018-08-07 15:57:13 -0700 | [diff] [blame] | 84 | public static final boolean DEBUG_LOG_DETAILED = Build.IS_DEBUGGABLE |
| 85 | && SystemProperties.getBoolean("db.log.detailed", false); |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 86 | } |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 87 | |
Jeff Brown | baefdfa | 2012-03-05 10:33:13 -0800 | [diff] [blame] | 88 | private SQLiteDebug() { |
| 89 | } |
| 90 | |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Determines whether a query should be logged. |
| 93 | * |
| 94 | * Reads the "db.log.slow_query_threshold" system property, which can be changed |
| 95 | * by the user at any time. If the value is zero, then all queries will |
Jeff Brown | 638eff7 | 2012-03-01 10:21:29 -0800 | [diff] [blame] | 96 | * be considered slow. If the value does not exist or is negative, then no queries will |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 97 | * be considered slow. |
| 98 | * |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 99 | * To enable it for a specific UID, "db.log.slow_query_threshold.UID" could also be used. |
| 100 | * |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 101 | * This value can be changed dynamically while the system is running. |
Jeff Brown | 638eff7 | 2012-03-01 10:21:29 -0800 | [diff] [blame] | 102 | * For example, "adb shell setprop db.log.slow_query_threshold 200" will |
| 103 | * log all queries that take 200ms or longer to run. |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 104 | * @hide |
| 105 | */ |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 106 | public static boolean shouldLogSlowQuery(long elapsedTimeMillis) { |
| 107 | final int slowQueryMillis = Math.min( |
Makoto Onuki | a9be33f | 2019-05-31 16:03:50 -0700 | [diff] [blame] | 108 | SystemProperties.getInt(NoPreloadHolder.SLOW_QUERY_THRESHOLD_PROP, |
| 109 | Integer.MAX_VALUE), |
| 110 | SystemProperties.getInt(NoPreloadHolder.SLOW_QUERY_THRESHOLD_UID_PROP, |
Makoto Onuki | a761d2b | 2018-08-01 15:57:45 -0700 | [diff] [blame] | 111 | Integer.MAX_VALUE)); |
| 112 | return elapsedTimeMillis >= slowQueryMillis; |
Jeff Brown | 89101cd9 | 2011-10-27 21:24:54 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | * Contains statistics about the active pagers in the current process. |
Vasu Nori | 4dd4ab4 | 2010-01-29 16:24:23 -0800 | [diff] [blame] | 117 | * |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 118 | * @see #nativeGetPagerStats(PagerStats) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | */ |
| 120 | public static class PagerStats { |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 121 | |
| 122 | @UnsupportedAppUsage |
| 123 | public PagerStats() { |
| 124 | } |
| 125 | |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 126 | /** the current amount of memory checked out by sqlite using sqlite3_malloc(). |
| 127 | * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html |
| 128 | */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 129 | @UnsupportedAppUsage |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 130 | public int memoryUsed; |
| 131 | |
| 132 | /** the number of bytes of page cache allocation which could not be sattisfied by the |
| 133 | * SQLITE_CONFIG_PAGECACHE buffer and where forced to overflow to sqlite3_malloc(). |
| 134 | * The returned value includes allocations that overflowed because they where too large |
| 135 | * (they were larger than the "sz" parameter to SQLITE_CONFIG_PAGECACHE) and allocations |
| 136 | * that overflowed because no space was left in the page cache. |
| 137 | * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html |
| 138 | */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 139 | @UnsupportedAppUsage |
Jeff Brown | 2a293b6 | 2012-01-19 14:02:22 -0800 | [diff] [blame] | 140 | public int pageCacheOverflow; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 141 | |
| 142 | /** records the largest memory allocation request handed to sqlite3. |
| 143 | * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html |
| 144 | */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 145 | @UnsupportedAppUsage |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 146 | public int largestMemAlloc; |
| 147 | |
| 148 | /** a list of {@link DbStats} - one for each main database opened by the applications |
| 149 | * running on the android device |
| 150 | */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 151 | @UnsupportedAppUsage |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 152 | public ArrayList<DbStats> dbStats; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * contains statistics about a database |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 157 | */ |
| 158 | public static class DbStats { |
| 159 | /** name of the database */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 160 | @UnsupportedAppUsage |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 161 | public String dbName; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 162 | |
| 163 | /** the page size for the database */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 164 | @UnsupportedAppUsage |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 165 | public long pageSize; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 166 | |
| 167 | /** the database size */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 168 | @UnsupportedAppUsage |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 169 | public long dbSize; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 170 | |
Fyodor Kupolov | b773312 | 2017-07-24 16:13:50 -0700 | [diff] [blame] | 171 | /** |
| 172 | * Number of lookaside slots: http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 173 | @UnsupportedAppUsage |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 174 | public int lookaside; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 175 | |
Vasu Nori | 90a36726 | 2010-04-12 12:49:09 -0700 | [diff] [blame] | 176 | /** statement cache stats: hits/misses/cachesize */ |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 177 | public String cache; |
Vasu Nori | 90a36726 | 2010-04-12 12:49:09 -0700 | [diff] [blame] | 178 | |
| 179 | public DbStats(String dbName, long pageCount, long pageSize, int lookaside, |
Vasu Nori | 00e4017 | 2010-11-29 11:03:23 -0800 | [diff] [blame] | 180 | int hits, int misses, int cachesize) { |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 181 | this.dbName = dbName; |
Vasu Nori | 90a36726 | 2010-04-12 12:49:09 -0700 | [diff] [blame] | 182 | this.pageSize = pageSize / 1024; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 183 | dbSize = (pageCount * pageSize) / 1024; |
| 184 | this.lookaside = lookaside; |
Vasu Nori | 90a36726 | 2010-04-12 12:49:09 -0700 | [diff] [blame] | 185 | this.cache = hits + "/" + misses + "/" + cachesize; |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * return all pager and database stats for the current process. |
| 191 | * @return {@link PagerStats} |
| 192 | */ |
Artur Satayev | f0b7d0b | 2019-11-04 11:16:45 +0000 | [diff] [blame] | 193 | @UnsupportedAppUsage |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 194 | public static PagerStats getDatabaseInfo() { |
| 195 | PagerStats stats = new PagerStats(); |
Jeff Brown | 254fba8 | 2012-01-19 17:06:44 -0800 | [diff] [blame] | 196 | nativeGetPagerStats(stats); |
Vasu Nori | c384920 | 2010-03-09 10:47:25 -0800 | [diff] [blame] | 197 | stats.dbStats = SQLiteDatabase.getDbStats(); |
| 198 | return stats; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 202 | * Dumps detailed information about all databases used by the process. |
| 203 | * @param printer The printer for dumping database state. |
Jeff Brown | a9be415 | 2012-01-18 15:29:57 -0800 | [diff] [blame] | 204 | * @param args Command-line arguments supplied to dumpsys dbinfo |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 205 | */ |
| 206 | public static void dump(Printer printer, String[] args) { |
Makoto Onuki | ee93ad2 | 2018-10-18 16:24:13 -0700 | [diff] [blame] | 207 | dump(printer, args, false); |
| 208 | } |
| 209 | |
| 210 | /** @hide */ |
| 211 | public static void dump(Printer printer, String[] args, boolean isSystem) { |
Jeff Brown | a9be415 | 2012-01-18 15:29:57 -0800 | [diff] [blame] | 212 | boolean verbose = false; |
| 213 | for (String arg : args) { |
| 214 | if (arg.equals("-v")) { |
| 215 | verbose = true; |
| 216 | } |
| 217 | } |
| 218 | |
Makoto Onuki | ee93ad2 | 2018-10-18 16:24:13 -0700 | [diff] [blame] | 219 | SQLiteDatabase.dumpAll(printer, verbose, isSystem); |
Jeff Brown | 6754ba2 | 2011-12-14 20:20:01 -0800 | [diff] [blame] | 220 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | } |