SQLiteDebug.java revision 624002b0d5b5bd812ca457fdfde0ead2b908eb7a
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.util.Log;
22
23/**
24 * Provides debugging info about all SQLite databases running in the current process.
25 *
26 * {@hide}
27 */
28public final class SQLiteDebug {
29    /**
30     * Controls the printing of SQL statements as they are executed.
31     */
32    public static final boolean DEBUG_SQL_STATEMENTS =
33            Log.isLoggable("SQLiteStatements", Log.VERBOSE);
34
35    /**
36     * Controls the printing of wall-clock time taken to execute SQL statements
37     * as they are executed.
38     */
39    public static final boolean DEBUG_SQL_TIME =
40            Log.isLoggable("SQLiteTime", Log.VERBOSE);
41
42    /**
43     * Controls the printing of compiled-sql-statement cache stats.
44     */
45    public static final boolean DEBUG_SQL_CACHE =
46            Log.isLoggable("SQLiteCompiledSql", Log.VERBOSE);
47
48    /**
49     * Controls the stack trace reporting of active cursors being
50     * finalized.
51     */
52    public static final boolean DEBUG_ACTIVE_CURSOR_FINALIZATION =
53            Log.isLoggable("SQLiteCursorClosing", Log.VERBOSE);
54
55    /**
56     * Controls the tracking of time spent holding the database lock.
57     */
58    public static final boolean DEBUG_LOCK_TIME_TRACKING =
59            Log.isLoggable("SQLiteLockTime", Log.VERBOSE);
60
61    /**
62     * Controls the printing of stack traces when tracking the time spent holding the database lock.
63     */
64    public static final boolean DEBUG_LOCK_TIME_TRACKING_STACK_TRACE =
65            Log.isLoggable("SQLiteLockStackTrace", Log.VERBOSE);
66
67    /**
68     * Contains statistics about the active pagers in the current process.
69     *
70     * @see #getPagerStats(PagerStats)
71     */
72    public static class PagerStats {
73        /** The total number of bytes in all pagers in the current process
74         * @deprecated not used any longer
75         */
76        @Deprecated
77        public long totalBytes;
78        /** The number of bytes in referenced pages in all pagers in the current process
79         * @deprecated not used any longer
80         * */
81        @Deprecated
82        public long referencedBytes;
83        /** The number of bytes in all database files opened in the current process
84         * @deprecated not used any longer
85         */
86        @Deprecated
87        public long databaseBytes;
88        /** The number of pagers opened in the current process
89         * @deprecated not used any longer
90         */
91        @Deprecated
92        public int numPagers;
93
94        /** the current amount of memory checked out by sqlite using sqlite3_malloc().
95         * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html
96         */
97        public int memoryUsed;
98
99        /** the number of bytes of page cache allocation which could not be sattisfied by the
100         * SQLITE_CONFIG_PAGECACHE buffer and where forced to overflow to sqlite3_malloc().
101         * The returned value includes allocations that overflowed because they where too large
102         * (they were larger than the "sz" parameter to SQLITE_CONFIG_PAGECACHE) and allocations
103         * that overflowed because no space was left in the page cache.
104         * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html
105         */
106        public int pageCacheOverflo;
107
108        /** records the largest memory allocation request handed to sqlite3.
109         * documented at http://www.sqlite.org/c3ref/c_status_malloc_size.html
110         */
111        public int largestMemAlloc;
112
113        /** a list of {@link DbStats} - one for each main database opened by the applications
114         * running on the android device
115         */
116        public ArrayList<DbStats> dbStats;
117    }
118
119    /**
120     * contains statistics about a database
121     */
122    public static class DbStats {
123        /** name of the database */
124        public final String dbName;
125
126        /** the page size for the database */
127        public final long pageSize;
128
129        /** the database size */
130        public final long dbSize;
131
132        /** documented here http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html */
133        public final int lookaside;
134
135        /** statement cache stats: hits/misses/cachesize */
136        public final String cache;
137
138        /** database dump of 'useful info for debugging only */
139        public final ArrayList<String> dataDump;
140
141        public DbStats(String dbName, long pageCount, long pageSize, int lookaside,
142            int hits, int misses, int cachesize, ArrayList<String> data) {
143            this.dbName = dbName;
144            this.pageSize = pageSize / 1024;
145            dbSize = (pageCount * pageSize) / 1024;
146            this.lookaside = lookaside;
147            this.cache = hits + "/" + misses + "/" + cachesize;
148            this.dataDump = data;
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        getPagerStats(stats);
159        stats.dbStats = SQLiteDatabase.getDbStats();
160        return stats;
161    }
162
163    /**
164     * Gathers statistics about all pagers in the current process.
165     */
166    public static native void getPagerStats(PagerStats stats);
167
168    /**
169     * Returns the size of the SQLite heap.
170     * @return The size of the SQLite heap in bytes.
171     */
172    public static native long getHeapSize();
173
174    /**
175     * Returns the amount of allocated memory in the SQLite heap.
176     * @return The allocated size in bytes.
177     */
178    public static native long getHeapAllocatedSize();
179
180    /**
181     * Returns the amount of free memory in the SQLite heap.
182     * @return The freed size in bytes.
183     */
184    public static native long getHeapFreeSize();
185
186    /**
187     * Determines the number of dirty belonging to the SQLite
188     * heap segments of this process.  pages[0] returns the number of
189     * shared pages, pages[1] returns the number of private pages
190     */
191    public static native void getHeapDirtyPages(int[] pages);
192
193    private static int sNumActiveCursorsFinalized = 0;
194
195    /**
196     * Returns the number of active cursors that have been finalized. This depends on the GC having
197     * run but is still useful for tests.
198     */
199    public static int getNumActiveCursorsFinalized() {
200        return sNumActiveCursorsFinalized;
201    }
202
203    static synchronized void notifyActiveCursorFinalized() {
204        sNumActiveCursorsFinalized++;
205    }
206}
207