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

Commit e877f4d9 authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

Minor updates to BoundServiceSession

Moving the ownership of the update thread to PSC to simplify
BoundServiceSession.
Moving the updates to be on OomAdjuster thread which should be more
performant than BackgroundThread.

Test: atest FrameworksMockingServicesTests:BoundServiceSessionTests

Flag: EXEMPT refactor

Bug: 416596684
Change-Id: I7664e3d3bc2f8d6ba8df451822df803e0e048ff4
parent 816c6637
Loading
Loading
Loading
Loading
+1 −5
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.server.am;

import android.app.IBinderSession;
import android.os.Handler;
import android.os.IBinder;
import android.os.Trace;
import android.util.ArrayMap;
@@ -26,7 +25,6 @@ import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.IntPair;

import java.lang.ref.WeakReference;
@@ -48,7 +46,6 @@ public class BoundServiceSession implements IBinderSession {
    // for when we enable a BinderProxy pointing to this from outside of the system process.
    private final WeakReference<ConnectionRecord> mConnection;
    private final BiConsumer<ConnectionRecord, Boolean> mProcessStateUpdater;
    private final Handler mBackgroundHandler;
    private final String mDebugName;

    @VisibleForTesting
@@ -61,7 +58,6 @@ public class BoundServiceSession implements IBinderSession {

    BoundServiceSession(BiConsumer<ConnectionRecord, Boolean> processStateUpdater,
            WeakReference<ConnectionRecord> weakCr, String debugName) {
        mBackgroundHandler = BackgroundThread.getHandler();
        mProcessStateUpdater = processStateUpdater;
        mConnection = weakCr;
        mDebugName = debugName;
@@ -87,7 +83,7 @@ public class BoundServiceSession implements IBinderSession {
                    + " already gone. Possibly the service has unbound.");
            return;
        }
        mBackgroundHandler.post(() -> mProcessStateUpdater.accept(strongCr, mTotal > 0));
        mProcessStateUpdater.accept(strongCr, mTotal > 0);
    }

    private void logTraceInstant(String message) {
+4 −2
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.app.ActivityManager;
import android.app.ActivityManagerInternal;
import android.content.Context;
import android.content.pm.ServiceInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManagerInternal;
import android.util.Slog;
@@ -57,13 +58,14 @@ public class ProcessStateController {
            CachedAppOptimizer cachedAppOptimizer, OomAdjuster.Injector oomAdjInjector) {
        mOomAdjuster = new OomAdjusterImpl(ams, processList, activeUids, handlerThread,
                mGlobalState, cachedAppOptimizer, oomAdjInjector);
        mServiceBinderCallUpdater = (cr, hasOngoingCalls) -> {
        final Handler serviceHandler = new Handler(handlerThread.getLooper());
        mServiceBinderCallUpdater = (cr, hasOngoingCalls) -> serviceHandler.post(() -> {
            synchronized (ams) {
                if (cr.setOngoingCalls(hasOngoingCalls)) {
                    runUpdate(cr.binding.client, OOM_ADJ_REASON_SERVICE_BINDER_CALL);
                }
            }
        };
        });
    }

    /**
+0 −26
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.server.am;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;

import static org.junit.Assert.assertEquals;
@@ -27,18 +26,10 @@ import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import android.os.Handler;
import android.platform.test.annotations.Presubmit;

import com.android.internal.os.BackgroundThread;
import com.android.modules.utils.testing.ExtendedMockitoRule;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.quality.Strictness;

import java.lang.ref.WeakReference;
import java.util.function.BiConsumer;
@@ -53,26 +44,9 @@ import java.util.function.BiConsumer;
public class BoundServiceSessionTests {
    private static final String TEST_DEBUG_NAME = "test_bound_service_session";

    @Rule
    public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this)
            .setStrictness(Strictness.WARN)
            .mockStatic(BackgroundThread.class)
            .build();

    private final ConnectionRecord mMockConnectionRecord = mock(ConnectionRecord.class);
    private final BiConsumer<ConnectionRecord, Boolean> mMockConsumer = mock(BiConsumer.class);

    @Before
    public void stubHandler() {
        final Handler syncHandler = mock(Handler.class);
        when(syncHandler.post(any(Runnable.class))).thenAnswer(inv -> {
            final Runnable r = inv.getArgument(0);
            r.run();
            return true;
        });
        doReturn(syncHandler).when(() -> BackgroundThread.getHandler());
    }

    private BoundServiceSession getNewBoundServiceSessionForTest() {
        return new BoundServiceSession(mMockConsumer, new WeakReference<>(mMockConnectionRecord),
                TEST_DEBUG_NAME);