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

Commit a2d918fc authored by Ryan Zuklie's avatar Ryan Zuklie
Browse files

Fix WakelockTracer clock mis-match.

Both events and write should use elapsedRealtimeNanos (nanos since boot
including time suspended). To help with this, I've moved both into the
tracer class (it had been in Notifier to allow grabbing the time before
posting to the handler, but that's no longer the case).

Bug: 422797438
Test: atest PowerServiceTests
Flag: com.android.server.power.feature.flags.enable_app_wakelock_data_source
Change-Id: I611dfe304ce14ada4f1e77358829e3620c2d6ffa
parent b94ffc19
Loading
Loading
Loading
Loading
+1 −12
Original line number Diff line number Diff line
@@ -1518,7 +1518,7 @@ public class Notifier {
        if (mWakelockTracer != null) {
            boolean isAcquire = eventType == WakelockEventType.ACQUIRE;
            mWakelockTracer.onWakelockEvent(isAcquire, tag, ownerUid, ownerPid, flags,
                    workSource, mInjector.nanoTime());
                    workSource);
        }

        if (mBatteryStatsInternal == null) {
@@ -1559,11 +1559,6 @@ public class Notifier {
         */
        long currentTimeMillis();

        /**
         * Gets the current time in nanoseconds
         */
        long nanoTime();

        /**
         * Gets the WakeLockLog object
         */
@@ -1592,12 +1587,6 @@ public class Notifier {
            return System.currentTimeMillis();
        }

        @Override
        public long nanoTime() {
            // This must be time since boot including suspend for Perfetto.
            return SystemClock.elapsedRealtimeNanos();
        }

        @Override
        public @NonNull WakeLockLog getWakeLockLog(Context context) {
            return new WakeLockLog(context);
+9 −8
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import static android.internal.perfetto.protos.TracePacketOuterClass.TracePacket

import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.os.WorkSource;
import android.tracing.perfetto.CreateIncrementalStateArgs;
import android.tracing.perfetto.DataSource;
@@ -100,9 +101,9 @@ final class WakelockTracer
     * are protected by the Perfetto mutex. All other accesses to instance state must be
     * guarded by the Perfetto mutex.
     *
     * Calls to onWakelockEvent should be strictly sequenced (nanoTime should monotonically
     * increase). This is accomplished in Notifier.java by enqueuing the changes on a handler
     * (which is FIFO) while holding the PowerManagerService lock.
     * Calls to onWakelockEvent should be strictly sequenced. A release event must be sent
     * after the acquire event. This is done in Notifier.java by recording events while
     * holding the PowerManagerService lock.
     *
     * @param acquired Whether or not this is a acquire or release event.
     * @param tag The wakelock tag provided by the application.
@@ -110,7 +111,6 @@ final class WakelockTracer
     * @param pid The pid of the process requesting the wakelock.
     * @param flags The wakelock flag bitmask (such as PowerManager.PARTIAL_WAKE_LOCK).
     * @param ws The Worksource attached to the wakelock (or null if there is none).
     * @param nanoTime The System.nanoTime() when the event occurred.
     */
    public void onWakelockEvent(
            boolean acquired,
@@ -118,12 +118,12 @@ final class WakelockTracer
            int uid,
            int pid,
            int flags,
            WorkSource ws,
            long nanoTime) {
            WorkSource ws) {
        trace((ctx) -> {
            try (Instance instance = ctx.getDataSourceInstanceLocked()) {
                if (instance == null) return;
                instance.onWakelockEvent(acquired, tag, uid, pid, flags, ws, nanoTime);
                instance.onWakelockEvent(acquired, tag, uid, pid, flags, ws,
                        SystemClock.elapsedRealtimeNanos());
            }
        });
    }
@@ -133,7 +133,8 @@ final class WakelockTracer
        trace((ctx) -> {
            try (Instance instance = ctx.getDataSourceInstanceLocked()) {
                if (instance != target) return;
                instance.write(ctx.newTracePacket(), ctx.getIncrementalState(), System.nanoTime());
                instance.write(ctx.newTracePacket(), ctx.getIncrementalState(),
                        SystemClock.elapsedRealtimeNanos());
            }
        });
    }
+2 −9
Original line number Diff line number Diff line
@@ -112,8 +112,6 @@ public class NotifierTest {
    private static final int OWNER_WORK_SOURCE_UID_2 = 3457;
    private static final int PID = 5678;

    private static final int FAKE_NANO_TIME = 123456;

    @Mock private BatterySaverStateMachine mBatterySaverStateMachineMock;
    @Mock private PowerManagerService.NativeWrapper mNativeWrapperMock;
    @Mock private Notifier mNotifierMock;
@@ -901,7 +899,7 @@ public class NotifierTest {

        // Tracing is done synchronously.
        verify(mWakelockTracer).onWakelockEvent(false, "wakelockTag", uid, pid,
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK, null, FAKE_NANO_TIME);
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK, null);

        // No interaction because we expect that to happen in async
        verifyNoMoreInteractions(mWakeLockLog, mBatteryStats, mAppOpsManager);
@@ -923,7 +921,7 @@ public class NotifierTest {

        // Tracing is done synchronously.
        verify(mWakelockTracer).onWakelockEvent(true, "wakelockTag", uid, pid,
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK, null, FAKE_NANO_TIME);
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK, null);

        // No interaction because we expect that to happen in async
        verifyNoMoreInteractions(mWakeLockLog, mBatteryStats, mAppOpsManager);
@@ -1329,11 +1327,6 @@ public class NotifierTest {
                        return 1;
                    }

                    @Override
                    public long nanoTime() {
                        return FAKE_NANO_TIME;
                    }

                    @Override
                    public @NonNull WakeLockLog getWakeLockLog(Context context) {
                        return mWakeLockLog;