diff options
author | 2024-11-01 16:33:13 +0000 | |
---|---|---|
committer | 2024-11-01 16:33:13 +0000 | |
commit | 0b497fae7f4813e1e830f9199cad0e828a19052a (patch) | |
tree | b8ffc9e5a942fdb26cec17889ca4dc72e04f92bf | |
parent | 0f1123b5f69dede84b57dd603df868fc23a18500 (diff) | |
parent | 2b1b5cc74837c1ba73d2055e1417dcf289a44fa0 (diff) |
Merge "Cache getProfileIds to reduce high volume of binder calls." into main
-rw-r--r-- | core/java/android/os/UserManager.java | 50 | ||||
-rw-r--r-- | services/core/java/com/android/server/pm/UserManagerService.java | 42 |
2 files changed, 80 insertions, 12 deletions
diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java index fa99f3563de9..4bc8fe0a974c 100644 --- a/core/java/android/os/UserManager.java +++ b/core/java/android/os/UserManager.java @@ -5502,10 +5502,14 @@ public class UserManager { Manifest.permission.CREATE_USERS, Manifest.permission.QUERY_USERS}, conditional = true) public @NonNull int[] getProfileIds(@UserIdInt int userId, boolean enabledOnly) { - try { - return mService.getProfileIds(userId, enabledOnly); - } catch (RemoteException re) { - throw re.rethrowFromSystemServer(); + if (android.multiuser.Flags.cacheProfileIdsReadOnly()) { + return enabledOnly ? getEnabledProfileIds(userId) : getProfileIdsWithDisabled(userId); + } else { + try { + return mService.getProfileIds(userId, enabledOnly); + } catch (RemoteException re) { + throw re.rethrowFromSystemServer(); + } } } @@ -5518,8 +5522,14 @@ public class UserManager { Manifest.permission.MANAGE_USERS, Manifest.permission.CREATE_USERS, Manifest.permission.QUERY_USERS}, conditional = true) + @CachedProperty(api = "user_manager_users") public int[] getProfileIdsWithDisabled(@UserIdInt int userId) { - return getProfileIds(userId, false /* enabledOnly */); + if (android.multiuser.Flags.cacheProfileIdsReadOnly()) { + return UserManagerCache.getProfileIdsWithDisabled( + (Integer userIdentifuer) -> mService.getProfileIds(userIdentifuer, false), userId); + } else { + return getProfileIds(userId, false /* enabledOnly */); + } } /** @@ -5530,8 +5540,21 @@ public class UserManager { Manifest.permission.MANAGE_USERS, Manifest.permission.CREATE_USERS, Manifest.permission.QUERY_USERS}, conditional = true) + @CachedProperty(api = "user_manager_users_enabled") public int[] getEnabledProfileIds(@UserIdInt int userId) { - return getProfileIds(userId, true /* enabledOnly */); + if (android.multiuser.Flags.cacheProfileIdsReadOnly()) { + return UserManagerCache.getEnabledProfileIds( + (Integer userIdentifuer) -> mService.getProfileIds(userIdentifuer, true), userId); + } else { + return getProfileIds(userId, true /* enabledOnly */); + } + } + + /** @hide */ + public static final void invalidateEnabledProfileIds() { + if (android.multiuser.Flags.cacheProfileIdsReadOnly()) { + UserManagerCache.invalidateEnabledProfileIds(); + } } /** @@ -6443,6 +6466,21 @@ public class UserManager { if (android.multiuser.Flags.cacheProfileParentReadOnly()) { UserManagerCache.invalidateProfileParent(); } + invalidateEnabledProfileIds(); + } + + /** + * Invalidate caches when related to specific user info flags change. + * + * @param flag a combination of FLAG_ constants, from the list in + * {@link UserInfo#UserInfoFlag}, whose value has changed and the associated + * invalidations must therefore be performed. + * @hide + */ + public static final void invalidateOnUserInfoFlagChange(@UserInfoFlag int flags) { + if ((flags & UserInfo.FLAG_DISABLED) > 0) { + invalidateEnabledProfileIds(); + } } /** diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java index 6c03214a2610..7ecfe7f64ffe 100644 --- a/services/core/java/com/android/server/pm/UserManagerService.java +++ b/services/core/java/com/android/server/pm/UserManagerService.java @@ -1222,7 +1222,7 @@ public class UserManagerService extends IUserManager.Stub { // Mark the user for removal. addRemovingUserIdLocked(ui.id); ui.partial = true; - ui.flags |= UserInfo.FLAG_DISABLED; + addUserInfoFlags(ui, UserInfo.FLAG_DISABLED); } /* Prunes out any partially created or partially removed users. */ @@ -1264,7 +1264,7 @@ public class UserManagerService extends IUserManager.Stub { if (ui.preCreated) { preCreatedUsers.add(ui); addRemovingUserIdLocked(ui.id); - ui.flags |= UserInfo.FLAG_DISABLED; + addUserInfoFlags(ui, UserInfo.FLAG_DISABLED); ui.partial = true; } } @@ -2120,7 +2120,7 @@ public class UserManagerService extends IUserManager.Stub { info = getUserInfoLU(userId); if (info != null && !info.isEnabled()) { wasUserDisabled = true; - info.flags ^= UserInfo.FLAG_DISABLED; + removeUserInfoFlags(info, UserInfo.FLAG_DISABLED); writeUserLP(getUserDataLU(info.id)); } } @@ -2130,6 +2130,36 @@ public class UserManagerService extends IUserManager.Stub { } } + /** + * This method is for monitoring flag changes on users flags and invalidate cache relevant to + * the change. The method add flags and invalidateOnUserInfoFlagChange for the flags which + * has changed. + * @param userInfo of existing user in mUsers list + * @param flags to be added to userInfo + */ + private void addUserInfoFlags(UserInfo userInfo, @UserInfoFlag int flags) { + int diff = ~userInfo.flags & flags; + if (diff > 0) { + userInfo.flags |= diff; + UserManager.invalidateOnUserInfoFlagChange(diff); + } + } + + /** + * This method is for monitoring flag changes on users flags and invalidate cache relevant to + * the change. The method remove flags and invalidateOnUserInfoFlagChange for the flags which + * has changed. + * @param userInfo of existing user in mUsers list + * @param flags to be removed from userInfo + */ + private void removeUserInfoFlags(UserInfo userInfo, @UserInfoFlag int flags) { + int diff = userInfo.flags & flags; + if (diff > 0) { + userInfo.flags ^= diff; + UserManager.invalidateOnUserInfoFlagChange(diff); + } + } + @Override public void setUserAdmin(@UserIdInt int userId) { checkManageUserAndAcrossUsersFullPermission("set user admin"); @@ -6245,7 +6275,7 @@ public class UserManagerService extends IUserManager.Stub { userData.info.guestToRemove = true; // Mark it as disabled, so that it isn't returned any more when // profiles are queried. - userData.info.flags |= UserInfo.FLAG_DISABLED; + addUserInfoFlags(userData.info, UserInfo.FLAG_DISABLED); writeUserLP(userData); } } finally { @@ -6390,7 +6420,7 @@ public class UserManagerService extends IUserManager.Stub { } // Mark it as disabled, so that it isn't returned any more when // profiles are queried. - userData.info.flags |= UserInfo.FLAG_DISABLED; + addUserInfoFlags(userData.info, UserInfo.FLAG_DISABLED); writeUserLP(userData); } @@ -7789,7 +7819,7 @@ public class UserManagerService extends IUserManager.Stub { if (userInfo != null && userInfo.isEphemeral()) { // Do not allow switching back to the ephemeral user again as the user is going // to be deleted. - userInfo.flags |= UserInfo.FLAG_DISABLED; + addUserInfoFlags(userInfo, UserInfo.FLAG_DISABLED); if (userInfo.isGuest()) { // Indicate that the guest will be deleted after it stops. userInfo.guestToRemove = true; |