SQLiteDebug.java revision 00e40171892c73295b6e7221ed83126731230b98
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 String dbName;
125
126        /** the page size for the database */
127        public long pageSize;
128
129        /** the database size */
130        public long dbSize;
131
132        /** documented here http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html */
133        public int lookaside;
134
135        /** statement cache stats: hits/misses/cachesize */
136        public String cache;
137
138        public DbStats(String dbName, long pageCount, long pageSize, int lookaside,
139            int hits, int misses, int cachesize) {
140            this.dbName = dbName;
141            this.pageSize = pageSize / 1024;
142            dbSize = (pageCount * pageSize) / 1024;
143            this.lookaside = lookaside;
144            this.cache = hits + "/" + misses + "/" + cachesize;
145        }
146    }
147
148    /**
149     * return all pager and database stats for the current process.
150     * @return {@link PagerStats}
151     */
152    public static PagerStats getDatabaseInfo() {
153        PagerStats stats = new PagerStats();
154        getPagerStats(stats);
155        stats.dbStats = SQLiteDatabase.getDbStats();
156        return stats;
157    }
158
159    /**
160     * Gathers statistics about all pagers in the current process.
161     */
162    public static native void getPagerStats(PagerStats stats);
163
164    /**
165     * Returns the size of the SQLite heap.
166     * @return The size of the SQLite heap in bytes.
167     */
168    public static native long getHeapSize();
169
170    /**
171     * Returns the amount of allocated memory in the SQLite heap.
172     * @return The allocated size in bytes.
173     */
174    public static native long getHeapAllocatedSize();
175
176    /**
177     * Returns the amount of free memory in the SQLite heap.
178     * @return The freed size in bytes.
179     */
180    public static native long getHeapFreeSize();
181
182    /**
183     * Determines the number of dirty belonging to the SQLite
184     * heap segments of this process.  pages[0] returns the number of
185     * shared pages, pages[1] returns the number of private pages
186     */
187    public static native void getHeapDirtyPages(int[] pages);
188
189    private static int sNumActiveCursorsFinalized = 0;
190
191    /**
192     * Returns the number of active cursors that have been finalized. This depends on the GC having
193     * run but is still useful for tests.
194     */
195    public static int getNumActiveCursorsFinalized() {
196        return sNumActiveCursorsFinalized;
197    }
198
199    static synchronized void notifyActiveCursorFinalized() {
200        sNumActiveCursorsFinalized++;
201    }
202}
203