Only log aborted activity background starts to TRON

And compute the relevant information only once, to be faster.

Bug: 123434182
Bug: 110956953
Test: atest WmTests:ActivityStarterTests
Change-Id: Id40653f01e3b96a667ad9a9642edee5d3d06db3c
This commit is contained in:
Michal Karpinski
2019-01-28 15:13:32 +00:00
parent bf1d8ab140
commit 4fd5b84341
3 changed files with 41 additions and 114 deletions

View File

@@ -841,13 +841,11 @@ class ActivityMetricsLogger {
Log.i(TAG, sb.toString()); Log.i(TAG, sb.toString());
} }
void logActivityStart(Intent intent, WindowProcessController callerApp, ActivityRecord r, void logAbortedBgActivityStart(Intent intent, WindowProcessController callerApp,
int callingUid, String callingPackage, int callingUidProcState, int callingUid, String callingPackage, int callingUidProcState,
boolean callingUidHasAnyVisibleWindow, boolean callingUidHasAnyVisibleWindow,
int realCallingUid, int realCallingUidProcState, int realCallingUid, int realCallingUidProcState,
boolean realCallingUidHasAnyVisibleWindow, boolean realCallingUidHasAnyVisibleWindow,
int targetUid, String targetPackage, int targetUidProcState,
boolean targetUidHasAnyVisibleWindow, String targetWhitelistTag,
boolean comingFromPendingIntent) { boolean comingFromPendingIntent) {
final long nowElapsed = SystemClock.elapsedRealtime(); final long nowElapsed = SystemClock.elapsedRealtime();
@@ -865,13 +863,6 @@ class ActivityMetricsLogger {
processStateAmToProto(realCallingUidProcState)); processStateAmToProto(realCallingUidProcState));
builder.addTaggedData(FIELD_REAL_CALLING_UID_HAS_ANY_VISIBLE_WINDOW, builder.addTaggedData(FIELD_REAL_CALLING_UID_HAS_ANY_VISIBLE_WINDOW,
realCallingUidHasAnyVisibleWindow ? 1 : 0); realCallingUidHasAnyVisibleWindow ? 1 : 0);
builder.addTaggedData(FIELD_TARGET_UID, targetUid);
builder.addTaggedData(FIELD_TARGET_PACKAGE_NAME, targetPackage);
builder.addTaggedData(FIELD_TARGET_UID_PROC_STATE,
processStateAmToProto(targetUidProcState));
builder.addTaggedData(FIELD_TARGET_UID_HAS_ANY_VISIBLE_WINDOW,
targetUidHasAnyVisibleWindow ? 1 : 0);
builder.addTaggedData(FIELD_TARGET_WHITELIST_TAG, targetWhitelistTag);
builder.addTaggedData(FIELD_COMING_FROM_PENDING_INTENT, comingFromPendingIntent ? 1 : 0); builder.addTaggedData(FIELD_COMING_FROM_PENDING_INTENT, comingFromPendingIntent ? 1 : 0);
if (intent != null) { if (intent != null) {
builder.addTaggedData(FIELD_INTENT_ACTION, intent.getAction()); builder.addTaggedData(FIELD_INTENT_ACTION, intent.getAction());
@@ -904,35 +895,6 @@ class ActivityMetricsLogger {
(nowUptime - callerApp.getWhenUnimportant())); (nowUptime - callerApp.getWhenUnimportant()));
} }
} }
if (r != null) {
builder.addTaggedData(FIELD_TARGET_SHORT_COMPONENT_NAME, r.shortComponentName);
builder.addTaggedData(FIELD_ACTIVITY_RECORD_LAUNCH_MODE, r.info.launchMode);
builder.addTaggedData(FIELD_ACTIVITY_RECORD_TARGET_ACTIVITY, r.info.targetActivity);
builder.addTaggedData(FIELD_ACTIVITY_RECORD_FLAGS, r.info.flags);
builder.addTaggedData(FIELD_ACTIVITY_RECORD_REAL_ACTIVITY,
r.mActivityComponent.toShortString());
builder.addTaggedData(FIELD_ACTIVITY_RECORD_SHORT_COMPONENT_NAME, r.shortComponentName);
builder.addTaggedData(FIELD_ACTIVITY_RECORD_PROCESS_NAME, r.processName);
builder.addTaggedData(FIELD_ACTIVITY_RECORD_IS_FULLSCREEN, r.fullscreen ? 1 : 0);
builder.addTaggedData(FIELD_ACTIVITY_RECORD_IS_NO_DISPLAY, r.noDisplay ? 1 : 0);
if (r.lastVisibleTime != 0) {
builder.addTaggedData(FIELD_ACTIVITY_RECORD_MILLIS_SINCE_LAST_VISIBLE,
(nowUptime - r.lastVisibleTime));
}
if (r.resultTo != null) {
builder.addTaggedData(FIELD_ACTIVITY_RECORD_RESULT_TO_PKG_NAME,
r.resultTo.packageName);
builder.addTaggedData(FIELD_ACTIVITY_RECORD_RESULT_TO_SHORT_COMPONENT_NAME,
r.resultTo.shortComponentName);
}
builder.addTaggedData(FIELD_ACTIVITY_RECORD_IS_VISIBLE, r.visible ? 1 : 0);
builder.addTaggedData(FIELD_ACTIVITY_RECORD_IS_VISIBLE_IGNORING_KEYGUARD,
r.visibleIgnoringKeyguard ? 1 : 0);
if (r.lastLaunchTime != 0) {
builder.addTaggedData(FIELD_ACTIVITY_RECORD_MILLIS_SINCE_LAST_LAUNCH,
(nowUptime - r.lastLaunchTime));
}
}
mMetricsLogger.write(builder); mMetricsLogger.write(builder);
} }

View File

@@ -749,9 +749,15 @@ class ActivityStarter {
boolean abortBackgroundStart = false; boolean abortBackgroundStart = false;
if (!abort) { if (!abort) {
try {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
"shouldAbortBackgroundActivityStart");
abortBackgroundStart = shouldAbortBackgroundActivityStart(callingUid, callingPid, abortBackgroundStart = shouldAbortBackgroundActivityStart(callingUid, callingPid,
callingPackage, realCallingUid, callerApp, originatingPendingIntent, callingPackage, realCallingUid, callerApp, originatingPendingIntent,
allowBackgroundActivityStart, intent); allowBackgroundActivityStart, intent);
} finally {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
}
abort |= (abortBackgroundStart && !mService.isBackgroundActivityStartsEnabled()); abort |= (abortBackgroundStart && !mService.isBackgroundActivityStartsEnabled());
// TODO: remove this toast after feature development is done // TODO: remove this toast after feature development is done
if (abortBackgroundStart) { if (abortBackgroundStart) {
@@ -903,12 +909,6 @@ class ActivityStarter {
mService.onStartActivitySetDidAppSwitch(); mService.onStartActivitySetDidAppSwitch();
mController.doPendingActivityLaunches(false); mController.doPendingActivityLaunches(false);
// maybe log to TRON, but only if we haven't already in shouldAbortBackgroundActivityStart()
if (!abortBackgroundStart) {
maybeLogActivityStart(callingUid, callingPackage, realCallingUid, intent, callerApp, r,
originatingPendingIntent, false /*abortedStart*/);
}
return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags,
true /* doResume */, checkedOptions, inTask, outActivity); true /* doResume */, checkedOptions, inTask, outActivity);
} }
@@ -927,17 +927,31 @@ class ActivityStarter {
return false; return false;
} }
// don't abort if the callingUid is in the foreground or is a persistent system process // don't abort if the callingUid is in the foreground or is a persistent system process
final boolean isCallingUidForeground = mService.isUidForeground(callingUid); final int callingUidProcState = mService.getUidStateLocked(callingUid);
final boolean isCallingUidPersistentSystemProcess = isUidPersistentSystemProcess( final boolean callingUidHasAnyVisibleWindow =
callingUid); mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(callingUid);
final boolean isCallingUidForeground = callingUidHasAnyVisibleWindow
|| callingUidProcState == ActivityManager.PROCESS_STATE_TOP;
final boolean isCallingUidPersistentSystemProcess = (callingUid == Process.SYSTEM_UID)
|| callingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;
if (isCallingUidForeground || isCallingUidPersistentSystemProcess) { if (isCallingUidForeground || isCallingUidPersistentSystemProcess) {
return false; return false;
} }
// take realCallingUid into consideration // take realCallingUid into consideration
final boolean isRealCallingUidForeground = mService.isUidForeground( final int realCallingUidProcState = (callingUid == realCallingUid)
realCallingUid); ? callingUidProcState
final boolean isRealCallingUidPersistentSystemProcess = isUidPersistentSystemProcess( : mService.getUidStateLocked(realCallingUid);
realCallingUid); final boolean realCallingUidHasAnyVisibleWindow = (callingUid == realCallingUid)
? callingUidHasAnyVisibleWindow
: mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(realCallingUid);
final boolean isRealCallingUidForeground = (callingUid == realCallingUid)
? isCallingUidForeground
: realCallingUidHasAnyVisibleWindow
|| realCallingUidProcState == ActivityManager.PROCESS_STATE_TOP;
final boolean isRealCallingUidPersistentSystemProcess = (callingUid == realCallingUid)
? isCallingUidPersistentSystemProcess
: (realCallingUid == Process.SYSTEM_UID)
|| realCallingUidProcState <= ActivityManager.PROCESS_STATE_PERSISTENT_UI;
if (realCallingUid != callingUid) { if (realCallingUid != callingUid) {
// don't abort if the realCallingUid is in the foreground and callingUid isn't // don't abort if the realCallingUid is in the foreground and callingUid isn't
if (isRealCallingUidForeground) { if (isRealCallingUidForeground) {
@@ -975,61 +989,14 @@ class ActivityStarter {
+ "; isBgStartWhitelisted: " + allowBackgroundActivityStart + "; isBgStartWhitelisted: " + allowBackgroundActivityStart
+ "; intent: " + intent + "; intent: " + intent
+ "]"); + "]");
maybeLogActivityStart(callingUid, callingPackage, realCallingUid, intent, callerApp, // log aborted activity start to TRON
null /*r*/, originatingPendingIntent, true /*abortedStart*/); if (mService.isActivityStartsLoggingEnabled()) {
return true; mSupervisor.getActivityMetricsLogger().logAbortedBgActivityStart(intent, callerApp,
} callingUid, callingPackage, callingUidProcState, callingUidHasAnyVisibleWindow,
realCallingUid, realCallingUidProcState, realCallingUidHasAnyVisibleWindow,
/** Returns true if uid is in a persistent state. */
private boolean isUidPersistentSystemProcess(int uid) {
return (uid == Process.SYSTEM_UID)
|| (mService.getUidStateLocked(uid) <= ActivityManager.PROCESS_STATE_PERSISTENT_UI);
}
private void maybeLogActivityStart(int callingUid, String callingPackage, int realCallingUid,
Intent intent, WindowProcessController callerApp, ActivityRecord r,
PendingIntentRecord originatingPendingIntent, boolean abortedStart) {
boolean callerAppHasForegroundActivity =
callerApp != null && callerApp.hasForegroundActivities();
if (!mService.isActivityStartsLoggingEnabled() || callerAppHasForegroundActivity
|| (!abortedStart && r == null)) {
// skip logging in this case
return;
}
try {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "logActivityStart");
final int callingUidProcState = mService.getUidStateLocked(callingUid);
final boolean callingUidHasAnyVisibleWindow =
mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(callingUid);
final int realCallingUidProcState = (callingUid == realCallingUid)
? callingUidProcState
: mService.getUidStateLocked(realCallingUid);
final boolean realCallingUidHasAnyVisibleWindow = (callingUid == realCallingUid)
? callingUidHasAnyVisibleWindow
: mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(
realCallingUid);
final String targetPackage = (r != null) ? r.packageName : null;
final int targetUid = (r!= null) ? ((r.appInfo != null) ? r.appInfo.uid : -1) : -1;
final int targetUidProcState = mService.getUidStateLocked(targetUid);
final boolean targetUidHasAnyVisibleWindow = (targetUid != -1)
? mService.mWindowManager.mRoot.isAnyNonToastWindowVisibleForUid(targetUid)
: false;
final String targetWhitelistTag = (targetUid != -1)
? mService.getPendingTempWhitelistTagForUidLocked(targetUid)
: null;
mSupervisor.getActivityMetricsLogger().logActivityStart(intent, callerApp, r,
callingUid, callingPackage, callingUidProcState,
callingUidHasAnyVisibleWindow,
realCallingUid, realCallingUidProcState,
realCallingUidHasAnyVisibleWindow,
targetUid, targetPackage, targetUidProcState,
targetUidHasAnyVisibleWindow, targetWhitelistTag,
(originatingPendingIntent != null)); (originatingPendingIntent != null));
} finally {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
} }
return true;
} }
/** /**

View File

@@ -523,9 +523,8 @@ public class ActivityStarterTests extends ActivityTestsBase {
starter.setReason("testActivityStartsLogging_noLoggingWhenDisabled").execute(); starter.setReason("testActivityStartsLogging_noLoggingWhenDisabled").execute();
// verify logging wasn't done // verify logging wasn't done
verify(mActivityMetricsLogger, never()).logActivityStart(any(), any(), any(), anyInt(), verify(mActivityMetricsLogger, never()).logAbortedBgActivityStart(any(), any(), anyInt(),
any(), anyInt(), anyBoolean(), anyInt(), anyInt(), anyBoolean(), anyInt(), any(), any(), anyInt(), anyBoolean(), anyInt(), anyInt(), anyBoolean(), anyBoolean());
anyInt(), anyBoolean(), any(), anyBoolean());
} }
/** /**
@@ -546,10 +545,9 @@ public class ActivityStarterTests extends ActivityTestsBase {
starter.setReason("testActivityStartsLogging_logsWhenEnabled").execute(); starter.setReason("testActivityStartsLogging_logsWhenEnabled").execute();
// verify the above activity start was logged // verify the above activity start was logged
verify(mActivityMetricsLogger, times(1)).logActivityStart(any(), any(), any(), verify(mActivityMetricsLogger, times(1)).logAbortedBgActivityStart(any(), any(),
eq(FAKE_CALLING_UID), eq(FAKE_CALLING_PACKAGE), anyInt(), anyBoolean(), eq(FAKE_CALLING_UID), eq(FAKE_CALLING_PACKAGE), anyInt(), anyBoolean(),
eq(FAKE_REAL_CALLING_UID), anyInt(), anyBoolean(), anyInt(), eq(FAKE_REAL_CALLING_UID), anyInt(), anyBoolean(), eq(false));
any(), anyInt(), anyBoolean(), any(), eq(false));
} }
/** /**