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

Commit 48733910 authored by Shai Barack's avatar Shai Barack
Browse files

Aggregate prepared statement cache stats from individual connections.

Previously, prepared statement cache statistics were tracked globally within `SQLiteConnectionPool`. This change moves the tracking to individual `SQLiteConnection` instances and aggregates these statistics when `collectDbStats` is called on the pool. This provides more accurate per-connection stats and a better overall picture of cache usage across all connections in the pool.

This also corrects the definition of prepared statement cache.

Bug: 440440268
Flag: EXEMPT refactor
Change-Id: Ic2a399a8d033a5a9ca1a02a7ab9ce0598ac44ae8
parent 3bd6739d
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -141,11 +141,6 @@ public class SQLiteDatabasePerfTest {
            }
        }

        Log.d("testSelectMemory",
                "cacheMissRate: " + mDatabase.getStatementCacheMissRate()
                        + "Total Statements: " + mDatabase.getTotalPreparedStatements()
                        + ". Misses: " + mDatabase.getTotalStatementCacheMisses());

        // Make sure caching is working and our miss rate should definitely be less than 100%
        // however, we would expect this number to be actually closer to 0.
        assertTrue(mDatabase.getStatementCacheMissRate() < 1);
+15 −5
Original line number Diff line number Diff line
@@ -1080,7 +1080,6 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
     * Return a {@link #PreparedStatement}, possibly from the cache.
     */
    private PreparedStatement acquirePreparedStatementLI(String sql) {
        ++mPool.mTotalPrepareStatements;
        PreparedStatement statement = mPreparedStatementCache.getStatement(sql);
        long seqNum = mPreparedStatementCache.getLastSeqNum();

@@ -1105,7 +1104,6 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
                skipCache = true;
            }
        }
        ++mPool.mTotalPrepareStatementCacheMiss;
        final long statementPtr = mPreparedStatementCache.createStatement(sql);
        seqNum = mPreparedStatementCache.getLastSeqNum();
        try {
@@ -1441,9 +1439,21 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
        } else {
            label = mConfiguration.path + " (" + mConnectionId + ")";
        }
        return new DbStats(label, pageCount, pageSize, lookaside,
                mPreparedStatementCache.hitCount(), mPreparedStatementCache.missCount(),
                mPreparedStatementCache.size(), false);
        DbStats dbStats = new DbStats(label, pageCount, pageSize, lookaside, 0, 0, 0, false);
        dbStats.addCacheStatsFrom(this);
        return dbStats;
    }

    int getPreparedStatementCacheHitCount() {
        return mPreparedStatementCache.hitCount();
    }

    int getPreparedStatementCacheMissCount() {
        return mPreparedStatementCache.missCount();
    }

    int getPreparedStatementCacheSize() {
        return mPreparedStatementCache.size();
    }

    @Override
+22 −10
Original line number Diff line number Diff line
@@ -109,10 +109,6 @@ public final class SQLiteConnectionPool implements Closeable {
            new ArrayList<SQLiteConnection>();
    private SQLiteConnection mAvailablePrimaryConnection;

    // Prepare statement cache statistics
    public int mTotalPrepareStatementCacheMiss = 0;
    public int mTotalPrepareStatements = 0;

    @GuardedBy("mLock")
    private IdleConnectionHandler mIdleConnectionHandler;

@@ -528,9 +524,15 @@ public final class SQLiteConnectionPool implements Closeable {
            }

            // Global pool stats
            DbStats poolStats = new DbStats(mConfiguration.path, 0, 0, 0,
                    mTotalPrepareStatements - mTotalPrepareStatementCacheMiss,
                    mTotalPrepareStatementCacheMiss, mTotalPrepareStatements, true);
            DbStats poolStats = new DbStats(mConfiguration.path, 0, 0, 0, 0, 0, 0, true);

            if (mAvailablePrimaryConnection != null) {
                poolStats.addCacheStatsFrom(mAvailablePrimaryConnection);
            }
            for (SQLiteConnection connection : mAvailableNonPrimaryConnections) {
                poolStats.addCacheStatsFrom(connection);
            }

            dbStatsList.add(poolStats);
        }
    }
@@ -1246,11 +1248,21 @@ public final class SQLiteConnectionPool implements Closeable {
    /** @hide */
    @NeverCompile
    public double getStatementCacheMissRate() {
        if (mTotalPrepareStatements == 0) {
            // no statements executed thus no miss rate.
        int cacheHits = 0;
        int cacheMisses = 0;

        if (mAvailablePrimaryConnection != null) {
            cacheHits += mAvailablePrimaryConnection.getPreparedStatementCacheHitCount();
            cacheMisses += mAvailablePrimaryConnection.getPreparedStatementCacheMissCount();
        }
        for (SQLiteConnection connection : mAvailableNonPrimaryConnections) {
            cacheHits += connection.getPreparedStatementCacheHitCount();
            cacheMisses += connection.getPreparedStatementCacheMissCount();
        }
        if (cacheMisses == 0) {
            return 0;
        }
        return (double) mTotalPrepareStatementCacheMiss / (double) mTotalPrepareStatements;
        return (double) cacheMisses / (double) (cacheHits + cacheMisses);
    }

    public long getTotalStatementsTime() {
+0 −16
Original line number Diff line number Diff line
@@ -2725,22 +2725,6 @@ public final class SQLiteDatabase extends SQLiteClosable {
        return connectionPools;
    }

    /** @hide */
    @NeverCompile
    public int getTotalPreparedStatements() {
        throwIfNotOpenLocked();

        return mConnectionPoolLocked.mTotalPrepareStatements;
    }

    /** @hide */
    @NeverCompile
    public int getTotalStatementCacheMisses() {
        throwIfNotOpenLocked();

        return mConnectionPoolLocked.mTotalPrepareStatementCacheMiss;
    }

    /**
     * Dump detailed information about all open databases in the current process.
     * Used by bug report.
+9 −3
Original line number Diff line number Diff line
@@ -182,11 +182,11 @@ public final class SQLiteDebug {
        public int lookaside;

        /** @hide */
        final public int cacheHits;
        public int cacheHits;
        /** @hide */
        final public int cacheMisses;
        public int cacheMisses;
        /** @hide */
        final public int cacheSize;
        public int cacheSize;

        /** true if connection specific stats or whole connection pool if false */
        public final boolean arePoolStats;
@@ -202,6 +202,12 @@ public final class SQLiteDebug {
            this.cacheSize = cachesize;
            this.arePoolStats = arePoolStats;
        }

        void addCacheStatsFrom(SQLiteConnection connection) {
            cacheHits += connection.getPreparedStatementCacheHitCount();
            cacheMisses += connection.getPreparedStatementCacheMissCount();
            cacheSize += connection.getPreparedStatementCacheSize();
        }
    }

    /**