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