Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 90a36726 authored by Vasu Nori's avatar Vasu Nori
Browse files

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
parent 275e0662
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -54401,6 +54401,19 @@
<parameter name="lockingEnabled" type="boolean">
</parameter>
</method>
<method name="setMaxSqlCacheSize"
 return="void"
 abstract="false"
 native="false"
 synchronized="true"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="cacheSize" type="int">
</parameter>
</method>
<method name="setMaximumSize"
 return="long"
 abstract="false"
@@ -54603,6 +54616,17 @@
 visibility="public"
>
</field>
<field name="MAX_SQL_CACHE_SIZE"
 type="int"
 transient="false"
 volatile="false"
 value="100"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="NO_LOCALIZED_COLLATORS"
 type="int"
 transient="false"
+5 −4
Original line number Diff line number Diff line
@@ -1447,7 +1447,7 @@ public final class ActivityThread {
        private static final String HEAP_COLUMN = "%17s %8s %8s %8s %8s";
        private static final String ONE_COUNT_COLUMN = "%17s %8d";
        private static final String TWO_COUNT_COLUMNS = "%17s %8d %17s %8d";
        private static final String DB_INFO_FORMAT = "  %8d %8d %10d  %s";
        private static final String DB_INFO_FORMAT = "  %4d %6d %8d %14s  %s";

        // Formatting for checkin service - update version if row format changes
        private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
@@ -1847,7 +1847,7 @@ public final class ActivityThread {
                for (int i = 0; i < stats.dbStats.size(); 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);
                    pw.print(',');
                }

@@ -1898,11 +1898,12 @@ public final class ActivityThread {
            int N = stats.dbStats.size();
            if (N > 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);
                }
            }

+15 −26
Original line number Diff line number Diff line
@@ -268,10 +268,15 @@ public class SQLiteDatabase extends SQLiteClosable {
     */
    /* package */ Map<String, SQLiteCompiledSql> 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()));
                }
            }
        }
+7 −2
Original line number Diff line number Diff line
@@ -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;
        }
    }