Merge "Move AppStandbyController calls out of UsageStats lock" into rvc-dev am: 5d544ddff0

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11843909

Change-Id: I991138bdd83df7ff29ab75c6fc6aaf5c96f9b7e3
This commit is contained in:
Michael Wachenschwanz
2020-06-20 06:14:03 +00:00
committed by Automerger Merge Worker
5 changed files with 94 additions and 69 deletions

View File

@@ -71,7 +71,7 @@ public interface AppStandbyInternal {
*/
void postOneTimeCheckIdleStates();
void reportEvent(UsageEvents.Event event, long elapsedRealtime, int userId);
void reportEvent(UsageEvents.Event event, int userId);
void setLastJobRunTime(String packageName, int userId, long elapsedRealtime);
@@ -150,9 +150,7 @@ public interface AppStandbyInternal {
void clearCarrierPrivilegedApps();
void flushToDisk(int userId);
void flushDurationsToDisk();
void flushToDisk();
void initializeDefaultsForSystemApps(int userId);
@@ -162,7 +160,7 @@ public interface AppStandbyInternal {
void postReportExemptedSyncStart(String packageName, int userId);
void dumpUser(IndentingPrintWriter idpw, int userId, List<String> pkgs);
void dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs);
void dumpState(String[] args, PrintWriter pw);

View File

@@ -675,6 +675,14 @@ public class AppIdleHistory {
return Long.parseLong(value);
}
public void writeAppIdleTimes() {
final int size = mIdleHistory.size();
for (int i = 0; i < size; i++) {
writeAppIdleTimes(mIdleHistory.keyAt(i));
}
}
public void writeAppIdleTimes(int userId) {
FileOutputStream fos = null;
AtomicFile appIdleFile = new AtomicFile(getUserFile(userId));
@@ -743,8 +751,18 @@ public class AppIdleHistory {
}
}
public void dump(IndentingPrintWriter idpw, int userId, List<String> pkgs) {
idpw.println("App Standby States:");
public void dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs) {
final int numUsers = userIds.length;
for (int i = 0; i < numUsers; i++) {
idpw.println();
dumpUser(idpw, userIds[i], pkgs);
}
}
private void dumpUser(IndentingPrintWriter idpw, int userId, List<String> pkgs) {
idpw.print("User ");
idpw.print(userId);
idpw.println(" App Standby States:");
idpw.increaseIndent();
ArrayMap<String, AppUsageHistory> userHistory = mIdleHistory.get(userId);
final long elapsedRealtime = SystemClock.elapsedRealtime();

View File

@@ -866,7 +866,7 @@ public class AppStandbyController implements AppStandbyInternal {
}
@Override
public void reportEvent(UsageEvents.Event event, long elapsedRealtime, int userId) {
public void reportEvent(UsageEvents.Event event, int userId) {
if (!mAppIdleEnabled) return;
final int eventType = event.getEventType();
if ((eventType == UsageEvents.Event.ACTIVITY_RESUMED
@@ -880,6 +880,7 @@ public class AppStandbyController implements AppStandbyInternal {
final String pkg = event.getPackageName();
final List<UserHandle> linkedProfiles = getCrossProfileTargets(pkg, userId);
synchronized (mAppIdleLock) {
final long elapsedRealtime = mInjector.elapsedRealtime();
reportEventLocked(pkg, eventType, elapsedRealtime, userId);
final int size = linkedProfiles.size();
@@ -1630,18 +1631,11 @@ public class AppStandbyController implements AppStandbyInternal {
}
}
@Override
public void flushToDisk(int userId) {
synchronized (mAppIdleLock) {
mAppIdleHistory.writeAppIdleTimes(userId);
}
}
@Override
public void flushDurationsToDisk() {
// Persist elapsed and screen on time. If this fails for whatever reason, the apps will be
// considered not-idle, which is the safest outcome in such an event.
public void flushToDisk() {
synchronized (mAppIdleLock) {
mAppIdleHistory.writeAppIdleTimes();
mAppIdleHistory.writeAppIdleDurations();
}
}
@@ -1818,9 +1812,9 @@ public class AppStandbyController implements AppStandbyInternal {
}
@Override
public void dumpUser(IndentingPrintWriter idpw, int userId, List<String> pkgs) {
public void dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs) {
synchronized (mAppIdleLock) {
mAppIdleHistory.dump(idpw, userId, pkgs);
mAppIdleHistory.dumpUsers(idpw, userIds, pkgs);
}
}

View File

@@ -516,7 +516,7 @@ public class AppStandbyControllerTests {
UsageEvents.Event ev = new UsageEvents.Event();
ev.mPackage = packageName;
ev.mEventType = eventType;
controller.reportEvent(ev, elapsedTime, USER_ID);
controller.reportEvent(ev, USER_ID);
}
private int getStandbyBucket(AppStandbyController controller, String packageName) {

View File

@@ -681,6 +681,8 @@ public class UsageStatsService extends SystemService implements
reportEventToAllUserId(event);
flushToDiskLocked();
}
mAppStandby.flushToDisk();
}
/**
@@ -806,8 +808,6 @@ public class UsageStatsService extends SystemService implements
return;
}
final long elapsedRealtime = SystemClock.elapsedRealtime();
switch (event.mEventType) {
case Event.ACTIVITY_RESUMED:
FrameworkStatsLog.write(
@@ -914,9 +914,9 @@ public class UsageStatsService extends SystemService implements
return; // user was stopped or removed
}
service.reportEvent(event);
mAppStandby.reportEvent(event, elapsedRealtime, userId);
}
mAppStandby.reportEvent(event, userId);
}
/**
@@ -948,6 +948,7 @@ public class UsageStatsService extends SystemService implements
reportEventToAllUserId(event);
flushToDiskLocked();
}
mAppStandby.flushToDisk();
}
/**
@@ -957,9 +958,9 @@ public class UsageStatsService extends SystemService implements
synchronized (mLock) {
Slog.i(TAG, "Removing user " + userId + " and all data.");
mUserState.remove(userId);
mAppStandby.onUserRemoved(userId);
mAppTimeLimit.onUserRemoved(userId);
}
mAppStandby.onUserRemoved(userId);
// Cancel any scheduled jobs for this user since the user is being removed.
UsageStatsIdleService.cancelJob(getContext(), userId);
UsageStatsIdleService.cancelUpdateMappingsJob(getContext());
@@ -1158,10 +1159,7 @@ public class UsageStatsService extends SystemService implements
if (service != null) {
service.persistActiveStats();
}
mAppStandby.flushToDisk(userId);
}
mAppStandby.flushDurationsToDisk();
mHandler.removeMessages(MSG_FLUSH_TO_DISK);
}
@@ -1169,28 +1167,31 @@ public class UsageStatsService extends SystemService implements
* Called by the Binder stub.
*/
void dump(String[] args, PrintWriter pw) {
synchronized (mLock) {
IndentingPrintWriter idpw = new IndentingPrintWriter(pw, " ");
IndentingPrintWriter idpw = new IndentingPrintWriter(pw, " ");
boolean checkin = false;
boolean compact = false;
final ArrayList<String> pkgs = new ArrayList<>();
boolean checkin = false;
boolean compact = false;
final ArrayList<String> pkgs = new ArrayList<>();
if (args != null) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("--checkin".equals(arg)) {
checkin = true;
} else if ("-c".equals(arg)) {
compact = true;
} else if ("flush".equals(arg)) {
if (args != null) {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("--checkin".equals(arg)) {
checkin = true;
} else if ("-c".equals(arg)) {
compact = true;
} else if ("flush".equals(arg)) {
synchronized (mLock) {
flushToDiskLocked();
pw.println("Flushed stats to disk");
return;
} else if ("is-app-standby-enabled".equals(arg)) {
pw.println(mAppStandby.isAppIdleEnabled());
return;
} else if ("apptimelimit".equals(arg)) {
}
mAppStandby.flushToDisk();
pw.println("Flushed stats to disk");
return;
} else if ("is-app-standby-enabled".equals(arg)) {
pw.println(mAppStandby.isAppIdleEnabled());
return;
} else if ("apptimelimit".equals(arg)) {
synchronized (mLock) {
if (i + 1 >= args.length) {
mAppTimeLimit.dump(null, pw);
} else {
@@ -1199,8 +1200,10 @@ public class UsageStatsService extends SystemService implements
mAppTimeLimit.dump(remainingArgs, pw);
}
return;
} else if ("file".equals(arg)) {
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
}
} else if ("file".equals(arg)) {
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
synchronized (mLock) {
if (i + 1 >= args.length) {
// dump everything for all users
final int numUsers = mUserState.size();
@@ -1224,8 +1227,10 @@ public class UsageStatsService extends SystemService implements
}
}
return;
} else if ("database-info".equals(arg)) {
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
}
} else if ("database-info".equals(arg)) {
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
synchronized (mLock) {
if (i + 1 >= args.length) {
// dump info for all users
final int numUsers = mUserState.size();
@@ -1247,34 +1252,43 @@ public class UsageStatsService extends SystemService implements
}
}
return;
} else if ("appstandby".equals(arg)) {
mAppStandby.dumpState(args, pw);
return;
} else if ("stats-directory".equals(arg)) {
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
}
} else if ("appstandby".equals(arg)) {
mAppStandby.dumpState(args, pw);
return;
} else if ("stats-directory".equals(arg)) {
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
synchronized (mLock) {
final int userId = parseUserIdFromArgs(args, i, ipw);
if (userId != UserHandle.USER_NULL) {
ipw.println(new File(Environment.getDataSystemCeDirectory(userId),
"usagestats").getAbsolutePath());
}
return;
} else if ("mappings".equals(arg)) {
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
final int userId = parseUserIdFromArgs(args, i, ipw);
}
} else if ("mappings".equals(arg)) {
final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
final int userId = parseUserIdFromArgs(args, i, ipw);
synchronized (mLock) {
if (userId != UserHandle.USER_NULL) {
mUserState.get(userId).dumpMappings(ipw);
}
return;
} else if (arg != null && !arg.startsWith("-")) {
// Anything else that doesn't start with '-' is a pkg to filter
pkgs.add(arg);
}
} else if (arg != null && !arg.startsWith("-")) {
// Anything else that doesn't start with '-' is a pkg to filter
pkgs.add(arg);
}
}
}
final int[] userIds;
synchronized (mLock) {
final int userCount = mUserState.size();
userIds = new int[userCount];
for (int i = 0; i < userCount; i++) {
int userId = mUserState.keyAt(i);
final int userId = mUserState.keyAt(i);
userIds[i] = userId;
idpw.printPair("user", userId);
idpw.println();
idpw.increaseIndent();
@@ -1286,21 +1300,22 @@ public class UsageStatsService extends SystemService implements
idpw.println();
}
}
mAppStandby.dumpUser(idpw, userId, pkgs);
idpw.decreaseIndent();
}
if (CollectionUtils.isEmpty(pkgs)) {
pw.println();
mAppStandby.dumpState(args, pw);
}
idpw.println();
idpw.printPair("Usage Source", UsageStatsManager.usageSourceToString(mUsageSource));
idpw.println();
mAppTimeLimit.dump(null, pw);
}
mAppStandby.dumpUsers(idpw, userIds, pkgs);
if (CollectionUtils.isEmpty(pkgs)) {
pw.println();
mAppStandby.dumpState(args, pw);
}
}
private int parseUserIdFromArgs(String[] args, int index, IndentingPrintWriter ipw) {