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

Commit 90142c95 authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Adding a system property to log slow queries

Change-Id: I4d139e222319f56169924e14aa967f07b233c00e
parent a4fe2aca
Loading
Loading
Loading
Loading
+26 −9
Original line number Diff line number Diff line
@@ -22,10 +22,11 @@ import android.database.DatabaseUtils;
import android.database.SQLException;
import android.os.Debug;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.Config;
import android.util.Log;
import android.util.EventLog;
import android.util.Log;

import java.io.File;
import java.util.HashMap;
@@ -221,6 +222,10 @@ public class SQLiteDatabase extends SQLiteClosable {
    // that logging is not enabled.
    /* package */ final boolean mLogStats;

    // System property that enables logging of slow queries. Specify the threshold in ms.
    private static final String LOG_SLOW_QUERIES_PROPERTY = "db.log.slow_query_threshold";
    private final int mSlowQueryThreshold;

    /**
     * @param closable
     */
@@ -1202,20 +1207,29 @@ public class SQLiteDatabase extends SQLiteClosable {
            String editTable) {
        long timeStart = 0;

        if (Config.LOGV) {
        if (Config.LOGV || mSlowQueryThreshold != -1) {
            timeStart = System.currentTimeMillis();
        }

        SQLiteCursorDriver driver = new SQLiteDirectCursorDriver(this, sql, editTable);

        Cursor cursor = null;
        try {
            return driver.query(
            cursor = driver.query(
                    cursorFactory != null ? cursorFactory : mFactory,
                    selectionArgs);
        } finally {
            if (Config.LOGV) {
            if (Config.LOGV || mSlowQueryThreshold != -1) {

                // Force query execution
                if (cursor != null) {
                    cursor.moveToFirst();
                    cursor.moveToPosition(-1);
                }

                long duration = System.currentTimeMillis() - timeStart;

                if (Config.LOGV || duration >= mSlowQueryThreshold) {
                    Log.v(SQLiteCursor.TAG,
                          "query (" + duration + " ms): " + driver.toString() + ", args are "
                                  + (selectionArgs != null
@@ -1224,6 +1238,8 @@ public class SQLiteDatabase extends SQLiteClosable {
                }
            }
        }
        return cursor;
    }

    /**
     * Runs the provided SQL and returns a cursor over the result set.
@@ -1671,6 +1687,7 @@ public class SQLiteDatabase extends SQLiteClosable {
        mFlags = flags;
        mPath = path;
        mLogStats = "1".equals(android.os.SystemProperties.get("db.logstats"));
        mSlowQueryThreshold = SystemProperties.getInt(LOG_SLOW_QUERIES_PROPERTY, -1);

        mLeakedException = new IllegalStateException(path +
            " SQLiteDatabase created and never closed");