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

Commit 52406197 authored by mrulhania's avatar mrulhania
Browse files

Log performance metric for unified appops sqlite

Logs read/write latency for sqlite database, also
logs database size.

Bug: 377584611
Test: presubmit
Test: manual
Flag: android.permission.flags.enable_all_sqlite_appops_accesses

Change-Id: Ieb6ee9b88526ac831c86e7de782e10a2ab483baf
parent 3ac08d27
Loading
Loading
Loading
Loading
+28 −1
Original line number Diff line number Diff line
@@ -28,10 +28,13 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteRawStatement;
import android.os.SystemClock;
import android.os.Trace;
import android.util.IntArray;
import android.util.Slog;

import com.android.internal.util.FrameworkStatsLog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
@@ -78,10 +81,12 @@ class AppOpHistoryDbHelper extends SQLiteOpenHelper {
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    void insertAppOpHistory(@NonNull List<AggregatedAppOpAccessEvent> appOpEvents) {
    void insertAppOpHistory(@NonNull List<AggregatedAppOpAccessEvent> appOpEvents,
            int writeSource) {
        if (appOpEvents.isEmpty()) {
            return;
        }
        long startTime = SystemClock.elapsedRealtime();
        Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER,
                "AppOpHistoryDbHelper_" + mAggregationTimeWindow + "_Write");
        try {
@@ -140,6 +145,18 @@ class AppOpHistoryDbHelper extends SQLiteOpenHelper {
            Slog.e(LOG_TAG, "Couldn't insert app op records in " + mDatabaseFile.getName(), ex);
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
            long writeTimeMillis = SystemClock.elapsedRealtime() - startTime;
            FrameworkStatsLog.write(FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED, /* read_time= */
                    -1, writeTimeMillis,
                    mDatabaseFile.length(), getDatabaseType(mAggregationTimeWindow), writeSource);
        }
    }

    private int getDatabaseType(AggregationTimeWindow aggregationTimeWindow) {
        if (aggregationTimeWindow == AggregationTimeWindow.SHORT) {
            return FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED__DATABASE_TYPE__DB_SHORT_INTERVAL;
        } else {
            return FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED__DATABASE_TYPE__DB_LONG_INTERVAL;
        }
    }

@@ -156,6 +173,7 @@ class AppOpHistoryDbHelper extends SQLiteOpenHelper {
        String sql = AppOpHistoryQueryHelper.buildSqlQuery(
                AppOpHistoryTable.SELECT_TABLE_DATA, conditions, orderByColumn, ascending, limit);

        long startTime = SystemClock.elapsedRealtime();
        try {
            SQLiteDatabase db = getReadableDatabase();
            db.beginTransactionReadOnly();
@@ -171,6 +189,15 @@ class AppOpHistoryDbHelper extends SQLiteOpenHelper {
            }
        } catch (Exception ex) {
            Slog.e(LOG_TAG, "Couldn't read app op records from " + mDatabaseFile.getName(), ex);
        } finally {
            long readTimeMillis = SystemClock.elapsedRealtime() - startTime;
            FrameworkStatsLog.write(FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED,
                    readTimeMillis, /* write_time= */ -1,
                    mDatabaseFile.length(), getDatabaseType(mAggregationTimeWindow),
                    FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_UNKNOWN);
        }
        if (HistoricalRegistry.DEBUG) {
            Slog.i(LOG_TAG, "Read " + results.size() + " records from " + mDatabaseFile.getName());
        }
        return results;
    }
+23 −9
Original line number Diff line number Diff line
@@ -23,6 +23,11 @@ import static android.app.AppOpsManager.ATTRIBUTION_FLAG_TRUSTED;
import static android.app.AppOpsManager.flagsToString;
import static android.app.AppOpsManager.getUidStateName;

import static com.android.internal.util.FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_CACHE_FULL;
import static com.android.internal.util.FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_MIGRATION;
import static com.android.internal.util.FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_PERIODIC;
import static com.android.internal.util.FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_READ;
import static com.android.internal.util.FrameworkStatsLog.SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_SHUTDOWN;
import static com.android.server.appop.HistoricalRegistry.AggregationTimeWindow;

import android.annotation.NonNull;
@@ -211,9 +216,11 @@ public class AppOpHistoryHelper {
        IntArray opCodes = AppOpHistoryQueryHelper.getAppOpCodes(filter, opNamesFilter);
        // flush the cache into database before read.
        if (opCodes != null) {
            mDbHelper.insertAppOpHistory(mCache.evict(opCodes));
            mDbHelper.insertAppOpHistory(mCache.evict(opCodes),
                    SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_READ);
        } else {
            mDbHelper.insertAppOpHistory(mCache.evictAll());
            mDbHelper.insertAppOpHistory(mCache.evictAll(),
                    SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_READ);
        }
        // Adjust begin & end time to time window's boundary.
        beginTimeMillis = Math.max(discretizeTimestamp(beginTimeMillis),
@@ -237,7 +244,8 @@ public class AppOpHistoryHelper {

    void shutdown() {
        mSqliteWriteHandler.removeAllPendingMessages();
        mDbHelper.insertAppOpHistory(mCache.evictAll());
        mDbHelper.insertAppOpHistory(mCache.evictAll(),
                SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_SHUTDOWN);
        mDbHelper.close();
    }

@@ -262,7 +270,8 @@ public class AppOpHistoryHelper {
    }

    void migrateDiscreteAppOpHistory(List<AggregatedAppOpAccessEvent> appOpEvents) {
        mDbHelper.insertAppOpHistory(appOpEvents);
        mDbHelper.insertAppOpHistory(appOpEvents,
                SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_MIGRATION);
    }

    @VisibleForTesting
@@ -284,9 +293,11 @@ public class AppOpHistoryHelper {
        IntArray opCodes = AppOpHistoryQueryHelper.getAppOpCodes(filter, opNamesFilter);
        // flush the cache into database before read.
        if (opCodes != null) {
            mDbHelper.insertAppOpHistory(mCache.evict(opCodes));
            mDbHelper.insertAppOpHistory(mCache.evict(opCodes),
                    SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_READ);
        } else {
            mDbHelper.insertAppOpHistory(mCache.evictAll());
            mDbHelper.insertAppOpHistory(mCache.evictAll(),
                    SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_READ);
        }
        // Adjust begin & end time to time window's boundary.
        beginTimeMillis = Math.max(discretizeTimestamp(beginTimeMillis),
@@ -323,7 +334,8 @@ public class AppOpHistoryHelper {
            @AppOpsManager.HistoricalOpsRequestFilter int filter, @NonNull SimpleDateFormat sdf,
            @NonNull Date date, int limit) {
        // flush caches to the database
        mDbHelper.insertAppOpHistory(mCache.evictAll());
        mDbHelper.insertAppOpHistory(mCache.evictAll(),
                SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_READ);
        long currentTime = System.currentTimeMillis();
        long beginTimeMillis = discretizeTimestamp(currentTime - mHistoryRetentionMillis);
        IntArray opCodes = new IntArray();
@@ -381,7 +393,8 @@ public class AppOpHistoryHelper {
            switch (msg.what) {
                case WRITE_DATABASE_PERIODIC -> {
                    try {
                        mDbHelper.insertAppOpHistory(mCache.evict());
                        mDbHelper.insertAppOpHistory(mCache.evict(),
                                SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_PERIODIC);
                    } finally {
                        ensurePeriodicJobsAreScheduled();
                    }
@@ -397,7 +410,8 @@ public class AppOpHistoryHelper {
                                evictedEvents.addAll(mCache.evictAll());
                            }
                        }
                        mDbHelper.insertAppOpHistory(evictedEvents);
                        mDbHelper.insertAppOpHistory(evictedEvents,
                                SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_CACHE_FULL);
                    } finally {
                        ensurePeriodicJobsAreScheduled();
                    }