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

Commit 6ae88d0e authored by Nicolo' Mazzucato's avatar Nicolo' Mazzucato
Browse files

Log DeviceStateManager callbacks in client processes

In systemui it seems there are several callbacks registered, and some of them take a lot of time, adding delays to the unfold flow.

At the same time, we have a few traces where also the Service side side takes a lot (>100ms). Adding some traces for debuggable builds

Flag: None
Bug: 310584951
Test: recorded a perfetto trace
Change-Id: I006037430c4b7d4f806b93e04ab28dc432cb91ed
parent 73b6d7ab
Loading
Loading
Loading
Loading
+24 −2
Original line number Diff line number Diff line
@@ -22,9 +22,11 @@ import android.annotation.RequiresPermission;
import android.content.Context;
import android.hardware.devicestate.DeviceStateManager.DeviceStateCallback;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.Trace;
import android.util.ArrayMap;

import com.android.internal.annotations.GuardedBy;
@@ -45,6 +47,8 @@ import java.util.concurrent.Executor;
@VisibleForTesting(visibility = Visibility.PACKAGE)
public final class DeviceStateManagerGlobal {
    private static DeviceStateManagerGlobal sInstance;
    private static final String TAG = "DeviceStateManagerGlobal";
    private static final boolean DEBUG = Build.IS_DEBUGGABLE;

    /**
     * Returns an instance of {@link DeviceStateManagerGlobal}. May return {@code null} if a
@@ -400,11 +404,29 @@ public final class DeviceStateManagerGlobal {
        }

        void notifyBaseStateChanged(int newBaseState) {
            mExecutor.execute(() -> mDeviceStateCallback.onBaseStateChanged(newBaseState));
            execute("notifyBaseStateChanged",
                    () -> mDeviceStateCallback.onBaseStateChanged(newBaseState));
        }

        void notifyStateChanged(int newDeviceState) {
            mExecutor.execute(() -> mDeviceStateCallback.onStateChanged(newDeviceState));
            execute("notifyStateChanged",
                    () -> mDeviceStateCallback.onStateChanged(newDeviceState));
        }

        private void execute(String traceName, Runnable r) {
            mExecutor.execute(() -> {
                if (DEBUG) {
                    Trace.beginSection(
                            mDeviceStateCallback.getClass().getSimpleName() + "#" + traceName);
                }
                try {
                    r.run();
                } finally {
                    if (DEBUG) {
                        Trace.endSection();
                    }
                }
            });
        }
    }

+10 −0
Original line number Diff line number Diff line
@@ -1114,12 +1114,22 @@ public final class DeviceStateManagerService extends SystemService {

        public void notifyDeviceStateInfoAsync(@NonNull DeviceStateInfo info) {
            mHandler.post(() -> {
                boolean tracingEnabled = Trace.isTagEnabled(Trace.TRACE_TAG_SYSTEM_SERVER);
                if (tracingEnabled) { // To avoid creating the string when not needed.
                    Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER,
                            "notifyDeviceStateInfoAsync(pid=" + mPid + ")");
                }
                try {
                    mCallback.onDeviceStateInfoChanged(info);
                } catch (RemoteException ex) {
                    Slog.w(TAG, "Failed to notify process " + mPid + " that device state changed.",
                            ex);
                }
                finally {
                    if (tracingEnabled) {
                        Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
                    }
                }
            });
        }