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

Commit 8af4d0bb authored by Amy Zhang's avatar Amy Zhang
Browse files

Update client pid and priority on requesting resources

The previous implementation queried the client process id only once
during the client registration. But the pid could change before a
client request a resource.

We change the impl to update the pid and priority on request.

Note that the update only happens when the pid changes compared to
the one recorded during client registration or updateClientPriority
API call.

Test: atest com.android.server.tv.tunerresourcemanager
Bug: 176186326
Change-Id: I8d83f7a39d47dc85a00cc4d216080d7eebb66b94
parent 39a3fa46
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -50,6 +50,8 @@ public final class ClientProfile {
     */
    private final int mProcessId;

    private boolean mIsForeground;

    /**
     * All the clients that share the same resource would be under the same group id.
     *
@@ -113,6 +115,20 @@ public final class ClientProfile {
        return mProcessId;
    }

    /**
     * Set the current isForeground status.
     */
    public void setForeground(boolean isForeground) {
        mIsForeground = isForeground;
    }

    /**
     * Get the previous recorded isForeground status.
     */
    public boolean isForeground() {
        return mIsForeground;
    }

    public int getGroupId() {
        return mGroupId;
    }
+32 −7
Original line number Diff line number Diff line
@@ -460,7 +460,9 @@ public class TunerResourceManagerService extends SystemService implements IBinde
                                              .useCase(profile.useCase)
                                              .processId(pid)
                                              .build();
        clientProfile.setPriority(getClientPriority(profile.useCase, pid));
        clientProfile.setForeground(checkIsForeground(pid));
        clientProfile.setPriority(
                getClientPriority(profile.useCase, clientProfile.isForeground()));

        addClientProfile(clientId[0], clientProfile, listener);
    }
@@ -498,6 +500,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde
            return false;
        }

        profile.setForeground(checkIsForeground(profile.getProcessId()));
        profile.setPriority(priority);
        profile.setNiceValue(niceValue);

@@ -611,6 +614,10 @@ public class TunerResourceManagerService extends SystemService implements IBinde

        frontendHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        ClientProfile requestClient = getClientProfile(request.clientId);
        if (requestClient == null) {
            return false;
        }
        clientPriorityUpdateOnRequest(requestClient);
        int grantingFrontendHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        int inUseLowestPriorityFrHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        // Priority max value is 1000
@@ -684,6 +691,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde

        lnbHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        ClientProfile requestClient = getClientProfile(request.clientId);
        clientPriorityUpdateOnRequest(requestClient);
        int grantingLnbHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        int inUseLowestPriorityLnbHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        // Priority max value is 1000
@@ -742,6 +750,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde
        }
        casSessionHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        ClientProfile requestClient = getClientProfile(request.clientId);
        clientPriorityUpdateOnRequest(requestClient);
        int lowestPriorityOwnerId = -1;
        // Priority max value is 1000
        int currentLowestPriority = MAX_CLIENT_PRIORITY + 1;
@@ -797,8 +806,9 @@ public class TunerResourceManagerService extends SystemService implements IBinde
                ? Binder.getCallingPid() /*callingPid*/
                : mTvInputManager.getClientPid(holderProfile.tvInputSessionId); /*tvAppId*/

        int challengerPriority = getClientPriority(challengerProfile.useCase, challengerPid);
        int holderPriority = getClientPriority(holderProfile.useCase, holderPid);
        int challengerPriority = getClientPriority(
                challengerProfile.useCase, checkIsForeground(challengerPid));
        int holderPriority = getClientPriority(holderProfile.useCase, checkIsForeground(holderPid));
        return challengerPriority > holderPriority;
    }

@@ -842,6 +852,21 @@ public class TunerResourceManagerService extends SystemService implements IBinde
        return true;
    }

    @VisibleForTesting
    // This mothod is to sync up the request client's foreground/background status and update
    // the client priority accordingly whenever new resource request comes in.
    protected void clientPriorityUpdateOnRequest(ClientProfile requestProfile) {
        int pid = requestProfile.getProcessId();
        boolean currentIsForeground = checkIsForeground(pid);
        if (requestProfile.isForeground() == currentIsForeground) {
            // To avoid overriding the priority set through updateClientPriority API.
            return;
        }
        requestProfile.setForeground(currentIsForeground);
        requestProfile.setPriority(
                getClientPriority(requestProfile.getUseCase(), currentIsForeground));
    }

    @VisibleForTesting
    protected boolean requestDescramblerInternal(
            TunerDescramblerRequest request, int[] descramblerHandle) {
@@ -933,20 +958,20 @@ public class TunerResourceManagerService extends SystemService implements IBinde
    }

    @VisibleForTesting
    protected int getClientPriority(int useCase, int pid) {
    protected int getClientPriority(int useCase, boolean isForeground) {
        if (DEBUG) {
            Slog.d(TAG, "getClientPriority useCase=" + useCase
                    + ", pid=" + pid + ")");
                    + ", isForeground=" + isForeground + ")");
        }

        if (isForeground(pid)) {
        if (isForeground) {
            return mPriorityCongfig.getForegroundPriority(useCase);
        }
        return mPriorityCongfig.getBackgroundPriority(useCase);
    }

    @VisibleForTesting
    protected boolean isForeground(int pid) {
    protected boolean checkIsForeground(int pid) {
        if (mActivityManager == null) {
            return false;
        }
+7 −3
Original line number Diff line number Diff line
@@ -99,7 +99,7 @@ public class TunerResourceManagerServiceTest {
        when(mContextSpy.getSystemService(Context.TV_INPUT_SERVICE)).thenReturn(tvInputManager);
        mTunerResourceManagerService = new TunerResourceManagerService(mContextSpy) {
            @Override
            protected boolean isForeground(int pid) {
            protected boolean checkIsForeground(int pid) {
                return mIsForeground;
            }
        };
@@ -231,6 +231,10 @@ public class TunerResourceManagerServiceTest {

    @Test
    public void requestFrontendTest_ClientNotRegistered() {
        TunerFrontendInfo[] infos0 = new TunerFrontendInfo[1];
        infos0[0] =
                tunerFrontendInfo(0 /*id*/, FrontendSettings.TYPE_DVBT, 0 /*exclusiveGroupId*/);
        mTunerResourceManagerService.setFrontendInfoListInternal(infos0);
        TunerFrontendRequest request =
                tunerFrontendRequest(0 /*clientId*/, FrontendSettings.TYPE_DVBT);
        int[] frontendHandle = new int[1];
@@ -752,9 +756,9 @@ public class TunerResourceManagerServiceTest {
                resourceClientProfile(null /*sessionId*/,
                        TvInputService.PRIORITY_HINT_USE_CASE_TYPE_RECORD);
        int backgroundPlaybackPriority = mTunerResourceManagerService.getClientPriority(
                TvInputService.PRIORITY_HINT_USE_CASE_TYPE_PLAYBACK, 0);
                TvInputService.PRIORITY_HINT_USE_CASE_TYPE_PLAYBACK, mIsForeground);
        int backgroundRecordPriority = mTunerResourceManagerService.getClientPriority(
                TvInputService.PRIORITY_HINT_USE_CASE_TYPE_RECORD, 0);
                TvInputService.PRIORITY_HINT_USE_CASE_TYPE_RECORD, mIsForeground);
        assertThat(mTunerResourceManagerService.isHigherPriorityInternal(backgroundPlaybackProfile,
                backgroundRecordProfile)).isEqualTo(
                        (backgroundPlaybackPriority > backgroundRecordPriority));