From 90a36726b7553a1e7efd2f4ecbe01d7e1b3e7a67 Mon Sep 17 00:00:00 2001 From: Vasu Nori Date: Mon, 12 Apr 2010 12:49:09 -0700 Subject: [PATCH] let apps set statement-cache size. 1. we should let apps set their statement-cache size. right n ow it is 250 is the statement-cache size for all apps and that is wasting a lot of memory. each prepared statement is averaging about 1k-5K, depending upon the complexity of sql and schema. mnake default 25 and let apps increase the size, if they need to. 2. in "adb bugreport" info, print stats on the statement-cache hits/missies and size (in number of statement cached). this will help us understand how statement-cache is being used Change-Id: Ic53a2842ef75823757492778158fcb8d5b4a28bb --- api/current.xml | 24 +++++++++++ core/java/android/app/ActivityThread.java | 9 ++-- .../database/sqlite/SQLiteDatabase.java | 41 +++++++------------ .../android/database/sqlite/SQLiteDebug.java | 9 +++- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/api/current.xml b/api/current.xml index 07d66a51a4d75..56dedaf3be59f 100644 --- a/api/current.xml +++ b/api/current.xml @@ -54401,6 +54401,19 @@ + + + + + + 0) { pw.println(" DATABASES"); - printRow(pw, " %8s %8s %10s %s", "Pagesize", "Dbsize", "Lookaside", "Dbname"); + printRow(pw, " %4s %6s %8s %14s %s", "pgsz", "dbsz", "lkaside", "cache", + "Dbname"); for (int i = 0; i < N; i++) { DbStats dbStats = stats.dbStats.get(i); printRow(pw, DB_INFO_FORMAT, dbStats.pageSize, dbStats.dbSize, - dbStats.lookaside, dbStats.dbName); + dbStats.lookaside, dbStats.cache, dbStats.dbName); } } diff --git a/core/java/android/database/sqlite/SQLiteDatabase.java b/core/java/android/database/sqlite/SQLiteDatabase.java index 3b7416e467bb1..01c8a38c76b9c 100644 --- a/core/java/android/database/sqlite/SQLiteDatabase.java +++ b/core/java/android/database/sqlite/SQLiteDatabase.java @@ -268,10 +268,15 @@ public class SQLiteDatabase extends SQLiteClosable { */ /* package */ Map mCompiledQueries = Maps.newHashMap(); /** - * @hide + * absolute max value that can set by {@link #setMaxSqlCacheSize(int)} + * size of each prepared-statement is between 1K - 6K, depending on the complexity of the + * sql statement & schema. */ - public static final int MAX_SQL_CACHE_SIZE = 250; - private int mMaxSqlCacheSize = MAX_SQL_CACHE_SIZE; // max cache size per Database instance + public static final int MAX_SQL_CACHE_SIZE = 100; + + // default statement-cache size per database connection ( = instance of this class) + private int mMaxSqlCacheSize = 25; + private int mCacheFullWarnings; private static final int MAX_WARNINGS_ON_CACHESIZE_CONDITION = 1; @@ -1975,14 +1980,6 @@ public class SQLiteDatabase extends SQLiteClosable { * mapping is NOT replaced with the new mapping). */ /* package */ void addToCompiledQueries(String sql, SQLiteCompiledSql compiledStatement) { - if (mMaxSqlCacheSize == 0) { - // for this database, there is no cache of compiled sql. - if (SQLiteDebug.DEBUG_SQL_CACHE) { - Log.v(TAG, "|NOT adding_sql_to_cache|" + getPath() + "|" + sql); - } - return; - } - SQLiteCompiledSql compiledSql = null; synchronized(mCompiledQueries) { // don't insert the new mapping if a mapping already exists @@ -2036,13 +2033,6 @@ public class SQLiteDatabase extends SQLiteClosable { SQLiteCompiledSql compiledStatement = null; boolean cacheHit; synchronized(mCompiledQueries) { - if (mMaxSqlCacheSize == 0) { - // for this database, there is no cache of compiled sql. - if (SQLiteDebug.DEBUG_SQL_CACHE) { - Log.v(TAG, "|cache NOT found|" + getPath()); - } - return null; - } cacheHit = (compiledStatement = mCompiledQueries.get(sql)) != null; } if (cacheHit) { @@ -2099,19 +2089,17 @@ public class SQLiteDatabase extends SQLiteClosable { } /** - * set the max size of the compiled sql cache for this database after purging the cache. + * set the max size of the prepared-statement cache for this database. * (size of the cache = number of compiled-sql-statements stored in the cache). * - * max cache size can ONLY be increased from its current size (default = 0). + * max cache size can ONLY be increased from its current size (default = 10). * if this method is called with smaller size than the current value of mMaxSqlCacheSize, * then IllegalStateException is thrown * * synchronized because we don't want t threads to change cache size at the same time. - * @param cacheSize the size of the cache. can be (0 to MAX_SQL_CACHE_SIZE) - * @throws IllegalStateException if input cacheSize > MAX_SQL_CACHE_SIZE or < 0 or - * < the value set with previous setMaxSqlCacheSize() call. - * - * @hide + * @param cacheSize the size of the cache. can be (0 to {@link #MAX_SQL_CACHE_SIZE}) + * @throws IllegalStateException if input cacheSize > {@link #MAX_SQL_CACHE_SIZE} or + * > the value set with previous setMaxSqlCacheSize() call. */ public synchronized void setMaxSqlCacheSize(int cacheSize) { if (cacheSize > MAX_SQL_CACHE_SIZE || cacheSize < 0) { @@ -2176,7 +2164,8 @@ public class SQLiteDatabase extends SQLiteClosable { } if (pageCount > 0) { dbStatsList.add(new DbStats(dbName, pageCount, db.getPageSize(), - lookasideUsed)); + lookasideUsed, db.mNumCacheHits, db.mNumCacheMisses, + db.mCompiledQueries.size())); } } } diff --git a/core/java/android/database/sqlite/SQLiteDebug.java b/core/java/android/database/sqlite/SQLiteDebug.java index a4db6d9c75368..2970c9ea7a903 100644 --- a/core/java/android/database/sqlite/SQLiteDebug.java +++ b/core/java/android/database/sqlite/SQLiteDebug.java @@ -134,11 +134,16 @@ public final class SQLiteDebug { /** documented here http://www.sqlite.org/c3ref/c_dbstatus_lookaside_used.html */ public int lookaside; - public DbStats(String dbName, long pageCount, long pageSize, int lookaside) { + /** statement cache stats: hits/misses/cachesize */ + public String cache; + + public DbStats(String dbName, long pageCount, long pageSize, int lookaside, + int hits, int misses, int cachesize) { this.dbName = dbName; - this.pageSize = pageSize; + this.pageSize = pageSize / 1024; dbSize = (pageCount * pageSize) / 1024; this.lookaside = lookaside; + this.cache = hits + "/" + misses + "/" + cachesize; } }