From d484256edc479e9e26b1e3c28da40e77becdc9f9 Mon Sep 17 00:00:00 2001 From: Amy Zhang Date: Mon, 25 Jan 2021 14:34:01 -0800 Subject: [PATCH] Update holder priority when new request coming into the TRM service Note that this CL also stops the TRM service from updating the client priority once the client called the updatePriority(int, int) to overwritten the client priority. Test: atest com.android.server.tv.tunerresourcemanager Bug: 176186326 Change-Id: I62be1dc736a13bbea888f03ab45f0209eae9c5c7 --- .../tunerresourcemanager/ClientProfile.java | 32 +++++++++------ .../TunerResourceManagerService.java | 39 +++++++++--------- .../TunerResourceManagerServiceTest.java | 40 +++++++++---------- 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/services/core/java/com/android/server/tv/tunerresourcemanager/ClientProfile.java b/services/core/java/com/android/server/tv/tunerresourcemanager/ClientProfile.java index 5723e1dcceb59..ee30fa2ac9286 100644 --- a/services/core/java/com/android/server/tv/tunerresourcemanager/ClientProfile.java +++ b/services/core/java/com/android/server/tv/tunerresourcemanager/ClientProfile.java @@ -50,8 +50,6 @@ 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. * @@ -89,6 +87,12 @@ public final class ClientProfile { */ private int mUsingCiCamId = INVALID_RESOURCE_ID; + /** + * If the priority is overwritten through + * {@link TunerResourceManagerService#setPriority(int, int)}. + */ + private boolean mIsPriorityOverwritten = false; + /** * Optional arbitrary priority value given by the client. * @@ -121,17 +125,10 @@ public final class ClientProfile { } /** - * Set the current isForeground status. + * If the client priority is overwrttien. */ - public void setForeground(boolean isForeground) { - mIsForeground = isForeground; - } - - /** - * Get the previous recorded isForeground status. - */ - public boolean isForeground() { - return mIsForeground; + public boolean isPriorityOverwritten() { + return mIsPriorityOverwritten; } public int getGroupId() { @@ -153,6 +150,17 @@ public final class ClientProfile { mPriority = priority; } + /** + * Overwrite the client priority. + */ + public void overwritePriority(int priority) { + if (priority < 0) { + return; + } + mIsPriorityOverwritten = true; + mPriority = priority; + } + public void setNiceValue(int niceValue) { mNiceValue = niceValue; } diff --git a/services/core/java/com/android/server/tv/tunerresourcemanager/TunerResourceManagerService.java b/services/core/java/com/android/server/tv/tunerresourcemanager/TunerResourceManagerService.java index 988582da53ea4..0c04b075485a2 100644 --- a/services/core/java/com/android/server/tv/tunerresourcemanager/TunerResourceManagerService.java +++ b/services/core/java/com/android/server/tv/tunerresourcemanager/TunerResourceManagerService.java @@ -507,9 +507,8 @@ public class TunerResourceManagerService extends SystemService implements IBinde .useCase(profile.useCase) .processId(pid) .build(); - clientProfile.setForeground(checkIsForeground(pid)); clientProfile.setPriority( - getClientPriority(profile.useCase, clientProfile.isForeground())); + getClientPriority(profile.useCase, checkIsForeground(pid))); addClientProfile(clientId[0], clientProfile, listener); } @@ -547,8 +546,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde return false; } - profile.setForeground(checkIsForeground(profile.getProcessId())); - profile.setPriority(priority); + profile.overwritePriority(priority); profile.setNiceValue(niceValue); return true; @@ -694,7 +692,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde } else if (grantingFrontendHandle == TunerResourceManager.INVALID_RESOURCE_HANDLE) { // Record the frontend id with the lowest client priority among all the // in use frontends when no available frontend has been found. - int priority = getOwnerClientPriority(fr.getOwnerClientId()); + int priority = updateAndGetOwnerClientPriority(fr.getOwnerClientId()); if (currentLowestPriority > priority) { inUseLowestPriorityFrHandle = fr.getHandle(); currentLowestPriority = priority; @@ -760,7 +758,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde } else { // Record the lnb id with the lowest client priority among all the // in use lnb when no available lnb has been found. - int priority = getOwnerClientPriority(lnb.getOwnerClientId()); + int priority = updateAndGetOwnerClientPriority(lnb.getOwnerClientId()); if (currentLowestPriority > priority) { inUseLowestPriorityLnbHandle = lnb.getHandle(); currentLowestPriority = priority; @@ -818,7 +816,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde } for (int ownerId : cas.getOwnerClientIds()) { // Record the client id with lowest priority that is using the current Cas system. - int priority = getOwnerClientPriority(ownerId); + int priority = updateAndGetOwnerClientPriority(ownerId); if (currentLowestPriority > priority) { lowestPriorityOwnerId = ownerId; currentLowestPriority = priority; @@ -867,7 +865,7 @@ public class TunerResourceManagerService extends SystemService implements IBinde } for (int ownerId : ciCam.getOwnerClientIds()) { // Record the client id with lowest priority that is using the current Cas system. - int priority = getOwnerClientPriority(ownerId); + int priority = updateAndGetOwnerClientPriority(ownerId); if (currentLowestPriority > priority) { lowestPriorityOwnerId = ownerId; currentLowestPriority = priority; @@ -966,18 +964,17 @@ public class TunerResourceManagerService extends SystemService implements IBinde } @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) { + // This mothod is to sync up the request/holder client's foreground/background status and update + // the client priority accordingly whenever a new resource request comes in. + protected void clientPriorityUpdateOnRequest(ClientProfile profile) { + if (profile.isPriorityOverwritten()) { // To avoid overriding the priority set through updateClientPriority API. return; } - requestProfile.setForeground(currentIsForeground); - requestProfile.setPriority( - getClientPriority(requestProfile.getUseCase(), currentIsForeground)); + int pid = profile.getProcessId(); + boolean currentIsForeground = checkIsForeground(pid); + profile.setPriority( + getClientPriority(profile.getUseCase(), currentIsForeground)); } @VisibleForTesting @@ -1154,13 +1151,15 @@ public class TunerResourceManagerService extends SystemService implements IBinde } /** - * Get the owner client's priority. + * Update and get the owner client's priority. * * @param clientId the owner client id. * @return the priority of the owner client. */ - private int getOwnerClientPriority(int clientId) { - return getClientProfile(clientId).getPriority(); + private int updateAndGetOwnerClientPriority(int clientId) { + ClientProfile profile = getClientProfile(clientId); + clientPriorityUpdateOnRequest(profile); + return profile.getPriority(); } @VisibleForTesting diff --git a/services/tests/servicestests/src/com/android/server/tv/tunerresourcemanager/TunerResourceManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/tv/tunerresourcemanager/TunerResourceManagerServiceTest.java index aadab6ea4fd9f..2f36c7fb90447 100644 --- a/services/tests/servicestests/src/com/android/server/tv/tunerresourcemanager/TunerResourceManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/tv/tunerresourcemanager/TunerResourceManagerServiceTest.java @@ -365,13 +365,13 @@ public class TunerResourceManagerServiceTest { mTunerResourceManagerService.registerClientProfileInternal( profiles[0], listener, clientId0); assertThat(clientId0[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId0[0]) - .setPriority(clientPriorities[0]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId0[0], clientPriorities[0], 0/*niceValue*/); mTunerResourceManagerService.registerClientProfileInternal( profiles[1], new TestResourcesReclaimListener(), clientId1); assertThat(clientId1[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId1[0]) - .setPriority(clientPriorities[1]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId1[0], clientPriorities[1], 0/*niceValue*/); // Init frontend resources. TunerFrontendInfo[] infos = new TunerFrontendInfo[2]; @@ -415,13 +415,13 @@ public class TunerResourceManagerServiceTest { mTunerResourceManagerService.registerClientProfileInternal( profiles[0], listener, clientId0); assertThat(clientId0[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId0[0]) - .setPriority(clientPriorities[0]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId0[0], clientPriorities[0], 0/*niceValue*/); mTunerResourceManagerService.registerClientProfileInternal( profiles[1], new TestResourcesReclaimListener(), clientId1); assertThat(clientId1[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId1[0]) - .setPriority(clientPriorities[1]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId1[0], clientPriorities[1], 0/*niceValue*/); // Init frontend resources. TunerFrontendInfo[] infos = new TunerFrontendInfo[2]; @@ -511,13 +511,13 @@ public class TunerResourceManagerServiceTest { mTunerResourceManagerService.registerClientProfileInternal( profiles[0], listener, clientId0); assertThat(clientId0[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId0[0]) - .setPriority(clientPriorities[0]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId0[0], clientPriorities[0], 0/*niceValue*/); mTunerResourceManagerService.registerClientProfileInternal( profiles[1], new TestResourcesReclaimListener(), clientId1); assertThat(clientId1[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId1[0]) - .setPriority(clientPriorities[1]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId1[0], clientPriorities[1], 0/*niceValue*/); // Init cas resources. mTunerResourceManagerService.updateCasInfoInternal(1 /*casSystemId*/, 2 /*maxSessionNum*/); @@ -567,13 +567,13 @@ public class TunerResourceManagerServiceTest { mTunerResourceManagerService.registerClientProfileInternal( profiles[0], listener, clientId0); assertThat(clientId0[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId0[0]) - .setPriority(clientPriorities[0]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId0[0], clientPriorities[0], 0/*niceValue*/); mTunerResourceManagerService.registerClientProfileInternal( profiles[1], new TestResourcesReclaimListener(), clientId1); assertThat(clientId1[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId1[0]) - .setPriority(clientPriorities[1]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId1[0], clientPriorities[1], 0/*niceValue*/); // Init cicam/cas resources. mTunerResourceManagerService.updateCasInfoInternal(1 /*casSystemId*/, 2 /*maxSessionNum*/); @@ -697,13 +697,13 @@ public class TunerResourceManagerServiceTest { mTunerResourceManagerService.registerClientProfileInternal( profiles[0], listener, clientId0); assertThat(clientId0[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId0[0]) - .setPriority(clientPriorities[0]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId0[0], clientPriorities[0], 0/*niceValue*/); mTunerResourceManagerService.registerClientProfileInternal( profiles[1], new TestResourcesReclaimListener(), clientId1); assertThat(clientId1[0]).isNotEqualTo(TunerResourceManagerService.INVALID_CLIENT_ID); - mTunerResourceManagerService.getClientProfile(clientId1[0]) - .setPriority(clientPriorities[1]); + mTunerResourceManagerService.updateClientPriorityInternal( + clientId1[0], clientPriorities[1], 0/*niceValue*/); // Init lnb resources. int[] lnbHandles = {1};