blob: 029bb4a872490ae2b6fd68961e8b30d825a37cb0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Vasu Noric3849202010-03-09 10:47:25 -080019import java.util.ArrayList;
20
Jeff Brown89101cd92011-10-27 21:24:54 -070021import android.os.Build;
22import android.os.SystemProperties;
Fred Quintanac4516a72009-09-03 12:14:06 -070023import android.util.Log;
Jeff Brown6754ba22011-12-14 20:20:01 -080024import android.util.Printer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
26/**
27 * Provides debugging info about all SQLite databases running in the current process.
Vasu Nori4dd4ab42010-01-29 16:24:23 -080028 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 * {@hide}
30 */
31public final class SQLiteDebug {
32 /**
33 * Controls the printing of SQL statements as they are executed.
34 */
Fred Quintanac4516a72009-09-03 12:14:06 -070035 public static final boolean DEBUG_SQL_STATEMENTS =
36 Log.isLoggable("SQLiteStatements", Log.VERBOSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38 /**
Vasu Nori3ef94e22010-02-05 14:49:04 -080039 * 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 Nori5a03f362009-10-20 15:16:35 -070046 * 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 Project9066cfe2009-03-03 19:31:44 -080052 * Controls the stack trace reporting of active cursors being
53 * finalized.
54 */
Fred Quintanac4516a72009-09-03 12:14:06 -070055 public static final boolean DEBUG_ACTIVE_CURSOR_FINALIZATION =
56 Log.isLoggable("SQLiteCursorClosing", Log.VERBOSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
58 /**
Vasu Nori4dd4ab42010-01-29 16:24:23 -080059 * Controls the tracking of time spent holding the database lock.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 */
Fred Quintanac4516a72009-09-03 12:14:06 -070061 public static final boolean DEBUG_LOCK_TIME_TRACKING =
62 Log.isLoggable("SQLiteLockTime", Log.VERBOSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
64 /**
Vasu Nori4dd4ab42010-01-29 16:24:23 -080065 * Controls the printing of stack traces when tracking the time spent holding the database lock.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 */
Fred Quintanac4516a72009-09-03 12:14:06 -070067 public static final boolean DEBUG_LOCK_TIME_TRACKING_STACK_TRACE =
68 Log.isLoggable("SQLiteLockStackTrace", Log.VERBOSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
70 /**
Jeff Brown89101cd92011-10-27 21:24:54 -070071 * 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 Project9066cfe2009-03-03 19:31:44 -080093 * Contains statistics about the active pagers in the current process.
Vasu Nori4dd4ab42010-01-29 16:24:23 -080094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * @see #getPagerStats(PagerStats)
96 */
97 public static class PagerStats {
Vasu Noric3849202010-03-09 10:47:25 -080098 /** 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 Project9066cfe2009-03-03 19:31:44 -0800102 public long totalBytes;
Vasu Noric3849202010-03-09 10:47:25 -0800103 /** 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 Project9066cfe2009-03-03 19:31:44 -0800107 public long referencedBytes;
Vasu Noric3849202010-03-09 10:47:25 -0800108 /** 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 Project9066cfe2009-03-03 19:31:44 -0800112 public long databaseBytes;
Vasu Noric3849202010-03-09 10:47:25 -0800113 /** The number of pagers opened in the current process
114 * @deprecated not used any longer
115 */
116 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public int numPagers;
Vasu Noric3849202010-03-09 10:47:25 -0800118
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 Noric3849202010-03-09 10:47:25 -0800146 */
147 public static class DbStats {
148 /** name of the database */
Vasu Nori00e40172010-11-29 11:03:23 -0800149 public String dbName;
Vasu Noric3849202010-03-09 10:47:25 -0800150
151 /** the page size for the database */
Vasu Nori00e40172010-11-29 11:03:23 -0800152 public long pageSize;
Vasu Noric3849202010-03-09 10:47:25 -0800153
154 /** the database size */
Vasu Nori00e40172010-11-29 11:03:23 -0800155 public long dbSize;
Vasu Noric3849202010-03-09 10:47:25 -0800156
157 /** documented here http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html */
Vasu Nori00e40172010-11-29 11:03:23 -0800158 public int lookaside;
Vasu Noric3849202010-03-09 10:47:25 -0800159
Vasu Nori90a367262010-04-12 12:49:09 -0700160 /** statement cache stats: hits/misses/cachesize */
Vasu Nori00e40172010-11-29 11:03:23 -0800161 public String cache;
Vasu Nori90a367262010-04-12 12:49:09 -0700162
163 public DbStats(String dbName, long pageCount, long pageSize, int lookaside,
Vasu Nori00e40172010-11-29 11:03:23 -0800164 int hits, int misses, int cachesize) {
Vasu Noric3849202010-03-09 10:47:25 -0800165 this.dbName = dbName;
Vasu Nori90a367262010-04-12 12:49:09 -0700166 this.pageSize = pageSize / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800167 dbSize = (pageCount * pageSize) / 1024;
168 this.lookaside = lookaside;
Vasu Nori90a367262010-04-12 12:49:09 -0700169 this.cache = hits + "/" + misses + "/" + cachesize;
Vasu Noric3849202010-03-09 10:47:25 -0800170 }
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 Project9066cfe2009-03-03 19:31:44 -0800182 }
183
184 /**
Jeff Brown6754ba22011-12-14 20:20:01 -0800185 * 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 Project9066cfe2009-03-03 19:31:44 -0800192 * 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 Nori4dd4ab42010-01-29 16:24:23 -0800201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 /**
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 Nori4dd4ab42010-01-29 16:24:23 -0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 /**
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 Project9066cfe2009-03-03 19:31:44 -0800234}