blob: 18cdb054e6480edf97392de0b134d67bbdaa3913 [file] [log] [blame]
Filip Gruszczynski77d94482015-12-11 13:59:52 -08001package com.android.server.am;
2
Jorim Jaggi3878ca32017-02-02 17:13:05 -08003import static android.app.ActivityManager.START_SUCCESS;
4import static android.app.ActivityManager.START_TASK_TO_FRONT;
Michal Karpinski201bc0c2018-07-20 15:32:00 +01005import static android.app.ActivityManager.processStateAmToProto;
Bryce Lee6c605092017-10-12 11:14:49 -07006import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale3382ab12017-07-27 08:55:03 -07007import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
8import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Vishnu Nair132ee832018-09-28 15:00:05 -07009import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070010import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
11import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Vishnu Nair132ee832018-09-28 15:00:05 -070012
Michal Karpinski201bc0c2018-07-20 15:32:00 +010013import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_ACTIVITY_START;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080014import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION;
Jorim Jaggi515dd682017-05-05 15:05:07 +020015import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_BIND_APPLICATION_DELAY_MS;
Todd Kennedy50d946c12017-03-17 13:55:38 -070016import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_CALLING_PACKAGE_NAME;
Jorim Jaggi172e99f2017-10-20 14:33:18 +020017import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_CANCELLED;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080018import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DELAY_MS;
19import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS;
Todd Kennedy50d946c12017-03-17 13:55:38 -070020import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_IS_EPHEMERAL;
Jorim Jaggi4d27b842017-08-17 17:22:26 +020021import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_PROCESS_RUNNING;
22import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN;
23import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN_MS;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080024import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS;
25import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010026import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_FLAGS;
27import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_IS_FULLSCREEN;
28import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_IS_NO_DISPLAY;
29import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_IS_VISIBLE;
30import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_IS_VISIBLE_IGNORING_KEYGUARD;
Vishnu Nair132ee832018-09-28 15:00:05 -070031import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_LAUNCH_MODE;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010032import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_MILLIS_SINCE_LAST_LAUNCH;
33import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_MILLIS_SINCE_LAST_VISIBLE;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010034import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_PROCESS_NAME;
35import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_REAL_ACTIVITY;
36import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_RESULT_TO_PKG_NAME;
37import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_RESULT_TO_SHORT_COMPONENT_NAME;
38import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_SHORT_COMPONENT_NAME;
39import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_ACTIVITY_RECORD_TARGET_ACTIVITY;
40import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_PACKAGE_NAME;
41import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_UID;
42import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_UID_HAS_ANY_VISIBLE_WINDOW;
43import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_UID_PROC_STATE;
Jorim Jaggicdfc04e2017-04-28 19:06:24 +020044import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CLASS_NAME;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010045import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_COMING_FROM_PENDING_INTENT;
Todd Kennedy50d946c12017-03-17 13:55:38 -070046import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_INSTANT_APP_LAUNCH_TOKEN;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010047import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_INTENT_ACTION;
48import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_CUR_PROC_STATE;
49import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_CLIENT_ACTIVITIES;
50import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_FOREGROUND_ACTIVITIES;
51import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_FOREGROUND_SERVICES;
52import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_OVERLAY_UI;
53import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_TOP_UI;
54import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_MILLIS_SINCE_FG_INTERACTION;
55import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_MILLIS_SINCE_LAST_INTERACTION_EVENT;
56import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_MILLIS_SINCE_UNIMPORTANT;
57import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_PENDING_UI_CLEAN;
58import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_PROCESS_NAME;
59import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_REAL_CALLING_UID;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010060import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_REAL_CALLING_UID_HAS_ANY_VISIBLE_WINDOW;
Vishnu Nair132ee832018-09-28 15:00:05 -070061import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_REAL_CALLING_UID_PROC_STATE;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010062import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TARGET_PACKAGE_NAME;
63import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TARGET_SHORT_COMPONENT_NAME;
64import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TARGET_UID;
65import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TARGET_UID_HAS_ANY_VISIBLE_WINDOW;
66import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TARGET_UID_PROC_STATE;
67import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TARGET_WHITELIST_TAG;
Calin Juravle759fbda2018-02-20 19:52:30 +000068import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PACKAGE_OPTIMIZATION_COMPILATION_FILTER;
Vishnu Nair132ee832018-09-28 15:00:05 -070069import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PACKAGE_OPTIMIZATION_COMPILATION_REASON;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080070import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_COLD_LAUNCH;
71import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_HOT_LAUNCH;
Jorim Jaggi4d27b842017-08-17 17:22:26 +020072import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE;
73import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_REPORTED_DRAWN_WITH_BUNDLE;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080074import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_WARM_LAUNCH;
Jorim Jaggi172e99f2017-10-20 14:33:18 +020075import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_METRICS;
Jorim Jaggif9704102016-05-05 19:14:22 -070076import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
77import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Vishnu Nair132ee832018-09-28 15:00:05 -070078import static com.android.server.am.EventLogTags.AM_ACTIVITY_LAUNCH_TIME;
Ng Zhi Anbbefdec2018-01-30 17:12:39 -080079import static com.android.server.am.MemoryStatUtil.MemoryStat;
Rajeev Kumarbfcd9202018-03-02 22:42:13 +000080import static com.android.server.am.MemoryStatUtil.readMemoryStatFromFilesystem;
Vishnu Nair132ee832018-09-28 15:00:05 -070081import static com.android.server.wm.ActivityTaskManagerInternal.APP_TRANSITION_TIMEOUT;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080082
Filip Gruszczynski77d94482015-12-11 13:59:52 -080083import android.content.Context;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010084import android.content.Intent;
Calin Juravle759fbda2018-02-20 19:52:30 +000085import android.content.pm.ApplicationInfo;
86import android.content.pm.dex.ArtManagerInternal;
87import android.content.pm.dex.PackageOptimizationInfo;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080088import android.metrics.LogMaker;
Jorim Jaggi172e99f2017-10-20 14:33:18 +020089import android.os.Handler;
90import android.os.Looper;
91import android.os.Message;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080092import android.os.SystemClock;
Vishnu Nair132ee832018-09-28 15:00:05 -070093import android.os.Trace;
94import android.util.EventLog;
95import android.util.Log;
Jorim Jaggi172e99f2017-10-20 14:33:18 +020096import android.util.Slog;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080097import android.util.SparseArray;
98import android.util.SparseIntArray;
Olivier Gaillardaed7f122017-12-12 14:26:22 +000099import android.util.StatsLog;
Vishnu Nair132ee832018-09-28 15:00:05 -0700100import android.util.TimeUtils;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800101
102import com.android.internal.logging.MetricsLogger;
Ng Zhi An83473542018-02-20 09:02:14 -0800103import com.android.internal.os.BackgroundThread;
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200104import com.android.internal.os.SomeArgs;
Calin Juravle759fbda2018-02-20 19:52:30 +0000105import com.android.server.LocalServices;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800106
107/**
Vishnu Nair132ee832018-09-28 15:00:05 -0700108 * Listens to activity launches, transitions, visibility changes and window drawn callbacks to
109 * determine app launch times and draw delays. Source of truth for activity metrics and provides
110 * data for Tron, logcat, event logs and {@link android.app.WaitResult}.
111 *
112 * Tests:
113 * atest SystemMetricsFunctionalTests
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800114 */
115class ActivityMetricsLogger {
Jorim Jaggif9704102016-05-05 19:14:22 -0700116
117 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityMetricsLogger" : TAG_AM;
118
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800119 // Window modes we are interested in logging. If we ever introduce a new type, we need to add
120 // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array.
121 private static final int WINDOW_STATE_STANDARD = 0;
122 private static final int WINDOW_STATE_SIDE_BY_SIDE = 1;
123 private static final int WINDOW_STATE_FREEFORM = 2;
Winson Chung83471632016-12-13 11:02:12 -0800124 private static final int WINDOW_STATE_ASSISTANT = 3;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800125 private static final int WINDOW_STATE_INVALID = -1;
126
Jorim Jaggi275561a2016-02-23 10:11:02 -0500127 private static final long INVALID_START_TIME = -1;
Vishnu Nair132ee832018-09-28 15:00:05 -0700128 private static final int INVALID_DELAY = -1;
129 private static final int INVALID_TRANSITION_TYPE = -1;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500130
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200131 private static final int MSG_CHECK_VISIBILITY = 0;
132
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800133 // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
134 // time we log.
135 private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = {
Winson Chung83471632016-12-13 11:02:12 -0800136 "window_time_0", "window_time_1", "window_time_2", "window_time_3"};
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800137
138 private int mWindowState = WINDOW_STATE_STANDARD;
139 private long mLastLogTimeSecs;
140 private final ActivityStackSupervisor mSupervisor;
141 private final Context mContext;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800142 private final MetricsLogger mMetricsLogger = new MetricsLogger();
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800143
Jorim Jaggi275561a2016-02-23 10:11:02 -0500144 private long mCurrentTransitionStartTime = INVALID_START_TIME;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200145 private long mLastTransitionStartTime = INVALID_START_TIME;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800146
147 private int mCurrentTransitionDeviceUptime;
148 private int mCurrentTransitionDelayMs;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500149 private boolean mLoggedTransitionStarting;
150
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100151 private final SparseArray<WindowingModeTransitionInfo> mWindowingModeTransitionInfo =
152 new SparseArray<>();
153 private final SparseArray<WindowingModeTransitionInfo> mLastWindowingModeTransitionInfo =
154 new SparseArray<>();
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200155 private final H mHandler;
Calin Juravle759fbda2018-02-20 19:52:30 +0000156
157 private ArtManagerInternal mArtManagerInternal;
Vishnu Nair132ee832018-09-28 15:00:05 -0700158 private boolean mDrawingTraceActive;
159 private final StringBuilder mStringBuilder = new StringBuilder();
Calin Juravle759fbda2018-02-20 19:52:30 +0000160
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200161 private final class H extends Handler {
162
163 public H(Looper looper) {
164 super(looper);
165 }
166
167 @Override
168 public void handleMessage(Message msg) {
169 switch (msg.what) {
170 case MSG_CHECK_VISIBILITY:
171 final SomeArgs args = (SomeArgs) msg.obj;
172 checkVisibility((TaskRecord) args.arg1, (ActivityRecord) args.arg2);
173 break;
174 }
175 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000176 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800177
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100178 private final class WindowingModeTransitionInfo {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800179 private ActivityRecord launchedActivity;
180 private int startResult;
181 private boolean currentTransitionProcessRunning;
Vishnu Nair132ee832018-09-28 15:00:05 -0700182 /** Elapsed time from when we launch an activity to when its windows are drawn. */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800183 private int windowsDrawnDelayMs;
Vishnu Nair132ee832018-09-28 15:00:05 -0700184 private int startingWindowDelayMs = INVALID_DELAY;
185 private int bindApplicationDelayMs = INVALID_DELAY;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800186 private int reason = APP_TRANSITION_TIMEOUT;
187 private boolean loggedWindowsDrawn;
188 private boolean loggedStartingWindowDrawn;
Vishnu Nair132ee832018-09-28 15:00:05 -0700189 private boolean launchTraceActive;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800190 }
191
Vishnu Nair132ee832018-09-28 15:00:05 -0700192 final class WindowingModeTransitionInfoSnapshot {
Calin Juravle759fbda2018-02-20 19:52:30 +0000193 final private ApplicationInfo applicationInfo;
Wale Ogunwalef6733932018-06-27 05:14:34 -0700194 final private WindowProcessController processRecord;
Vishnu Nair132ee832018-09-28 15:00:05 -0700195 final String packageName;
196 final String launchedActivityName;
Calin Juravle759fbda2018-02-20 19:52:30 +0000197 final private String launchedActivityLaunchedFromPackage;
198 final private String launchedActivityLaunchToken;
199 final private String launchedActivityAppRecordRequiredAbi;
Vishnu Nair132ee832018-09-28 15:00:05 -0700200 final String launchedActivityShortComponentName;
Ng Zhi An83473542018-02-20 09:02:14 -0800201 final private String processName;
Calin Juravle759fbda2018-02-20 19:52:30 +0000202 final private int reason;
203 final private int startingWindowDelayMs;
204 final private int bindApplicationDelayMs;
Vishnu Nair132ee832018-09-28 15:00:05 -0700205 final int windowsDrawnDelayMs;
206 final int type;
207 final int userId;
208 /**
209 * Elapsed time from when we launch an activity to when the app reported it was
210 * fully drawn. If this is not reported then the value is set to INVALID_DELAY.
211 */
212 final int windowsFullyDrawnDelayMs;
213 final int activityRecordIdHashCode;
Calin Juravle759fbda2018-02-20 19:52:30 +0000214
215 private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700216 this(info, info.launchedActivity);
217 }
218
219 private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info,
220 ActivityRecord launchedActivity) {
221 this(info, launchedActivity, INVALID_DELAY);
222 }
223
224 private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info,
225 ActivityRecord launchedActivity, int windowsFullyDrawnDelayMs) {
226 applicationInfo = launchedActivity.appInfo;
227 packageName = launchedActivity.packageName;
228 launchedActivityName = launchedActivity.info.name;
229 launchedActivityLaunchedFromPackage = launchedActivity.launchedFromPackage;
230 launchedActivityLaunchToken = launchedActivity.info.launchToken;
231 launchedActivityAppRecordRequiredAbi = launchedActivity.app == null
Calin Juravle759fbda2018-02-20 19:52:30 +0000232 ? null
Wale Ogunwale9e4f3e02018-05-17 09:35:39 -0700233 : info.launchedActivity.app.getRequiredAbi();
Calin Juravle759fbda2018-02-20 19:52:30 +0000234 reason = info.reason;
235 startingWindowDelayMs = info.startingWindowDelayMs;
236 bindApplicationDelayMs = info.bindApplicationDelayMs;
237 windowsDrawnDelayMs = info.windowsDrawnDelayMs;
238 type = getTransitionType(info);
Ng Zhi An83473542018-02-20 09:02:14 -0800239 processRecord = findProcessForActivity(info.launchedActivity);
240 processName = info.launchedActivity.processName;
Vishnu Nair132ee832018-09-28 15:00:05 -0700241 userId = launchedActivity.userId;
242 launchedActivityShortComponentName = launchedActivity.shortComponentName;
243 activityRecordIdHashCode = System.identityHashCode(launchedActivity);
244 this.windowsFullyDrawnDelayMs = windowsFullyDrawnDelayMs;
Calin Juravle759fbda2018-02-20 19:52:30 +0000245 }
246 }
247
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200248 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context, Looper looper) {
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800249 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
250 mSupervisor = supervisor;
251 mContext = context;
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200252 mHandler = new H(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800253 }
254
255 void logWindowState() {
256 final long now = SystemClock.elapsedRealtime() / 1000;
257 if (mWindowState != WINDOW_STATE_INVALID) {
258 // We log even if the window state hasn't changed, because the user might remain in
259 // home/fullscreen move forever and we would like to track this kind of behavior
260 // too.
261 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
262 (int) (now - mLastLogTimeSecs));
263 }
264 mLastLogTimeSecs = now;
265
Wale Ogunwale926aade2017-08-29 11:24:37 -0700266 mWindowState = WINDOW_STATE_INVALID;
Andrii Kulian5f750bc2018-07-17 08:57:23 -0700267 ActivityStack stack = mSupervisor.getTopDisplayFocusedStack();
lumarkf6c4a982018-06-15 15:43:12 +0800268 if (stack == null) {
269 return;
270 }
271
Wale Ogunwale926aade2017-08-29 11:24:37 -0700272 if (stack.isActivityTypeAssistant()) {
273 mWindowState = WINDOW_STATE_ASSISTANT;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800274 return;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800275 }
Wale Ogunwale926aade2017-08-29 11:24:37 -0700276
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700277 int windowingMode = stack.getWindowingMode();
278 if (windowingMode == WINDOWING_MODE_PINNED) {
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800279 stack = mSupervisor.findStackBehind(stack);
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700280 windowingMode = stack.getWindowingMode();
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800281 }
Wale Ogunwale926aade2017-08-29 11:24:37 -0700282 switch (windowingMode) {
283 case WINDOWING_MODE_FULLSCREEN:
284 mWindowState = WINDOW_STATE_STANDARD;
285 break;
286 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY:
287 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY:
288 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
289 break;
Bryce Lee6c605092017-10-12 11:14:49 -0700290 case WINDOWING_MODE_FREEFORM:
Wale Ogunwale926aade2017-08-29 11:24:37 -0700291 mWindowState = WINDOW_STATE_FREEFORM;
292 break;
293 default:
294 if (windowingMode != WINDOWING_MODE_UNDEFINED) {
295 throw new IllegalStateException("Unknown windowing mode for stack=" + stack
296 + " windowingMode=" + windowingMode);
297 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800298 }
299 }
Jorim Jaggi275561a2016-02-23 10:11:02 -0500300
301 /**
302 * Notifies the tracker at the earliest possible point when we are starting to launch an
303 * activity.
304 */
305 void notifyActivityLaunching() {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800306 if (!isAnyTransitionActive()) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200307 if (DEBUG_METRICS) Slog.i(TAG, "notifyActivityLaunching");
Alison Cichowlasb7f67ab2017-04-25 18:04:40 -0400308 mCurrentTransitionStartTime = SystemClock.uptimeMillis();
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200309 mLastTransitionStartTime = mCurrentTransitionStartTime;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800310 }
Jorim Jaggi275561a2016-02-23 10:11:02 -0500311 }
312
313 /**
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700314 * Notifies the tracker that the activity is actually launching.
315 *
316 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
317 * launch
318 * @param launchedActivity the activity that is being launched
319 */
320 void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700321 final WindowProcessController processRecord = findProcessForActivity(launchedActivity);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700322 final boolean processRunning = processRecord != null;
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700323
324 // We consider this a "process switch" if the process of the activity that gets launched
325 // didn't have an activity that was in started state. In this case, we assume that lot
326 // of caches might be purged so the time until it produces the first frame is very
327 // interesting.
328 final boolean processSwitch = processRecord == null
Wale Ogunwalef6733932018-06-27 05:14:34 -0700329 || !processRecord.hasStartedActivity(launchedActivity);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700330
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800331 notifyActivityLaunched(resultCode, launchedActivity, processRunning, processSwitch);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700332 }
333
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700334 /**
Jorim Jaggi275561a2016-02-23 10:11:02 -0500335 * Notifies the tracker the the activity is actually launching.
336 *
337 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
338 * launch
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800339 * @param launchedActivity the activity being launched
Jorim Jaggi275561a2016-02-23 10:11:02 -0500340 * @param processRunning whether the process that will contains the activity is already running
Jorim Jaggibe67c902016-04-12 00:53:16 -0700341 * @param processSwitch whether the process that will contain the activity didn't have any
342 * activity that was stopped, i.e. the started activity is "switching"
343 * processes
Jorim Jaggi275561a2016-02-23 10:11:02 -0500344 */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800345 private void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity,
Jorim Jaggibe67c902016-04-12 00:53:16 -0700346 boolean processRunning, boolean processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500347
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200348 if (DEBUG_METRICS) Slog.i(TAG, "notifyActivityLaunched"
349 + " resultCode=" + resultCode
350 + " launchedActivity=" + launchedActivity
351 + " processRunning=" + processRunning
352 + " processSwitch=" + processSwitch);
353
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800354 // If we are already in an existing transition, only update the activity name, but not the
355 // other attributes.
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100356 final int windowingMode = launchedActivity != null
357 ? launchedActivity.getWindowingMode()
358 : WINDOWING_MODE_UNDEFINED;
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200359
360 if (mCurrentTransitionStartTime == INVALID_START_TIME) {
361 return;
362 }
363
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100364 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800365 if (launchedActivity != null && info != null) {
366 info.launchedActivity = launchedActivity;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500367 return;
368 }
369
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100370 final boolean otherWindowModesLaunching =
371 mWindowingModeTransitionInfo.size() > 0 && info == null;
Jorim Jaggi54cff642018-03-15 15:51:32 +0100372 if ((!isLoggableResultCode(resultCode) || launchedActivity == null || !processSwitch
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100373 || windowingMode == WINDOWING_MODE_UNDEFINED) && !otherWindowModesLaunching) {
Alison Cichowlas803054d2016-12-13 14:38:01 -0500374
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800375 // Failed to launch or it was not a process switch, so we don't care about the timing.
Vishnu Nair132ee832018-09-28 15:00:05 -0700376 reset(true /* abort */, info);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800377 return;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100378 } else if (otherWindowModesLaunching) {
379 // Don't log this windowing mode but continue with the other windowing modes.
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800380 return;
381 }
382
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200383 if (DEBUG_METRICS) Slog.i(TAG, "notifyActivityLaunched successful");
384
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100385 final WindowingModeTransitionInfo newInfo = new WindowingModeTransitionInfo();
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800386 newInfo.launchedActivity = launchedActivity;
387 newInfo.currentTransitionProcessRunning = processRunning;
388 newInfo.startResult = resultCode;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100389 mWindowingModeTransitionInfo.put(windowingMode, newInfo);
390 mLastWindowingModeTransitionInfo.put(windowingMode, newInfo);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800391 mCurrentTransitionDeviceUptime = (int) (SystemClock.uptimeMillis() / 1000);
Vishnu Nair132ee832018-09-28 15:00:05 -0700392 startTraces(newInfo);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500393 }
394
395 /**
Jorim Jaggi54cff642018-03-15 15:51:32 +0100396 * @return True if we should start logging an event for an activity start that returned
397 * {@code resultCode} and that we'll indeed get a windows drawn event.
398 */
399 private boolean isLoggableResultCode(int resultCode) {
400 return resultCode == START_SUCCESS || resultCode == START_TASK_TO_FRONT;
401 }
402
403 /**
Jorim Jaggi275561a2016-02-23 10:11:02 -0500404 * Notifies the tracker that all windows of the app have been drawn.
405 */
Vishnu Nair132ee832018-09-28 15:00:05 -0700406 WindowingModeTransitionInfoSnapshot notifyWindowsDrawn(int windowingMode, long timestamp) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100407 if (DEBUG_METRICS) Slog.i(TAG, "notifyWindowsDrawn windowingMode=" + windowingMode);
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200408
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100409 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800410 if (info == null || info.loggedWindowsDrawn) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700411 return null;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500412 }
Sudheer Shankac766db02017-06-12 10:37:29 -0700413 info.windowsDrawnDelayMs = calculateDelay(timestamp);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800414 info.loggedWindowsDrawn = true;
Vishnu Nair132ee832018-09-28 15:00:05 -0700415 final WindowingModeTransitionInfoSnapshot infoSnapshot =
416 new WindowingModeTransitionInfoSnapshot(info);
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100417 if (allWindowsDrawn() && mLoggedTransitionStarting) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700418 reset(false /* abort */, info);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500419 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700420 return infoSnapshot;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500421 }
422
423 /**
424 * Notifies the tracker that the starting window was drawn.
425 */
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100426 void notifyStartingWindowDrawn(int windowingMode, long timestamp) {
427 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800428 if (info == null || info.loggedStartingWindowDrawn) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500429 return;
430 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800431 info.loggedStartingWindowDrawn = true;
Sudheer Shankac766db02017-06-12 10:37:29 -0700432 info.startingWindowDelayMs = calculateDelay(timestamp);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500433 }
434
435 /**
436 * Notifies the tracker that the app transition is starting.
437 *
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100438 * @param windowingModeToReason A map from windowing mode to a reason integer, which must be on
Vishnu Nair132ee832018-09-28 15:00:05 -0700439 * of ActivityTaskManagerInternal.APP_TRANSITION_* reasons.
Jorim Jaggi275561a2016-02-23 10:11:02 -0500440 */
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100441 void notifyTransitionStarting(SparseIntArray windowingModeToReason, long timestamp) {
Jorim Jaggid8a57772017-04-14 16:50:42 -0700442 if (!isAnyTransitionActive() || mLoggedTransitionStarting) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500443 return;
444 }
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200445 if (DEBUG_METRICS) Slog.i(TAG, "notifyTransitionStarting");
Sudheer Shankac766db02017-06-12 10:37:29 -0700446 mCurrentTransitionDelayMs = calculateDelay(timestamp);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500447 mLoggedTransitionStarting = true;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100448 for (int index = windowingModeToReason.size() - 1; index >= 0; index--) {
449 final int windowingMode = windowingModeToReason.keyAt(index);
450 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(
451 windowingMode);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800452 if (info == null) {
453 continue;
454 }
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100455 info.reason = windowingModeToReason.valueAt(index);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800456 }
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100457 if (allWindowsDrawn()) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700458 reset(false /* abort */, null /* WindowingModeTransitionInfo */);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500459 }
460 }
461
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200462 /**
463 * Notifies the tracker that the visibility of an app is changing.
464 *
465 * @param activityRecord the app that is changing its visibility
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200466 */
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200467 void notifyVisibilityChanged(ActivityRecord activityRecord) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100468 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(
469 activityRecord.getWindowingMode());
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200470 if (info == null) {
471 return;
472 }
473 if (info.launchedActivity != activityRecord) {
474 return;
475 }
476 final TaskRecord t = activityRecord.getTask();
477 final SomeArgs args = SomeArgs.obtain();
478 args.arg1 = t;
479 args.arg2 = activityRecord;
480 mHandler.obtainMessage(MSG_CHECK_VISIBILITY, args).sendToTarget();
481 }
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200482
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200483 private void checkVisibility(TaskRecord t, ActivityRecord r) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700484 synchronized (mSupervisor.mService.mGlobalLock) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200485
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100486 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(
487 r.getWindowingMode());
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200488
489 // If we have an active transition that's waiting on a certain activity that will be
490 // invisible now, we'll never get onWindowsDrawn, so abort the transition if necessary.
491 if (info != null && !t.isVisible()) {
492 if (DEBUG_METRICS) Slog.i(TAG, "notifyVisibilityChanged to invisible"
493 + " activity=" + r);
494 logAppTransitionCancel(info);
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100495 mWindowingModeTransitionInfo.remove(r.getWindowingMode());
496 if (mWindowingModeTransitionInfo.size() == 0) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700497 reset(true /* abort */, info);
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200498 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700499 stopFullyDrawnTraceIfNeeded();
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200500 }
501 }
502 }
503
Jorim Jaggi515dd682017-05-05 15:05:07 +0200504 /**
505 * Notifies the tracker that we called immediately before we call bindApplication on the client.
506 *
507 * @param app The client into which we'll call bindApplication.
508 */
509 void notifyBindApplication(ProcessRecord app) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100510 for (int i = mWindowingModeTransitionInfo.size() - 1; i >= 0; i--) {
511 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.valueAt(i);
Jorim Jaggi515dd682017-05-05 15:05:07 +0200512
513 // App isn't attached to record yet, so match with info.
514 if (info.launchedActivity.appInfo == app.info) {
515 info.bindApplicationDelayMs = calculateCurrentDelay();
516 }
517 }
518 }
519
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100520 private boolean allWindowsDrawn() {
521 for (int index = mWindowingModeTransitionInfo.size() - 1; index >= 0; index--) {
522 if (!mWindowingModeTransitionInfo.valueAt(index).loggedWindowsDrawn) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800523 return false;
524 }
525 }
526 return true;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500527 }
528
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800529 private boolean isAnyTransitionActive() {
530 return mCurrentTransitionStartTime != INVALID_START_TIME
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100531 && mWindowingModeTransitionInfo.size() > 0;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800532 }
533
Vishnu Nair132ee832018-09-28 15:00:05 -0700534 private void reset(boolean abort, WindowingModeTransitionInfo info) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200535 if (DEBUG_METRICS) Slog.i(TAG, "reset abort=" + abort);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800536 if (!abort && isAnyTransitionActive()) {
537 logAppTransitionMultiEvents();
538 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700539 stopLaunchTrace(info);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500540 mCurrentTransitionStartTime = INVALID_START_TIME;
Vishnu Nair132ee832018-09-28 15:00:05 -0700541 mCurrentTransitionDelayMs = INVALID_DELAY;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500542 mLoggedTransitionStarting = false;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100543 mWindowingModeTransitionInfo.clear();
Jorim Jaggi275561a2016-02-23 10:11:02 -0500544 }
545
546 private int calculateCurrentDelay() {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500547 // Shouldn't take more than 25 days to launch an app, so int is fine here.
Alison Cichowlasb7f67ab2017-04-25 18:04:40 -0400548 return (int) (SystemClock.uptimeMillis() - mCurrentTransitionStartTime);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500549 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800550
Sudheer Shankac766db02017-06-12 10:37:29 -0700551 private int calculateDelay(long timestamp) {
552 // Shouldn't take more than 25 days to launch an app, so int is fine here.
553 return (int) (timestamp - mCurrentTransitionStartTime);
554 }
555
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100556 private void logAppTransitionCancel(WindowingModeTransitionInfo info) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200557 final int type = getTransitionType(info);
Vishnu Nair132ee832018-09-28 15:00:05 -0700558 if (type == INVALID_TRANSITION_TYPE) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200559 return;
560 }
561 final LogMaker builder = new LogMaker(APP_TRANSITION_CANCELLED);
562 builder.setPackageName(info.launchedActivity.packageName);
563 builder.setType(type);
564 builder.addTaggedData(FIELD_CLASS_NAME, info.launchedActivity.info.name);
565 mMetricsLogger.write(builder);
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000566 StatsLog.write(
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800567 StatsLog.APP_START_CANCELED,
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000568 info.launchedActivity.appInfo.uid,
569 info.launchedActivity.packageName,
570 convertAppStartTransitionType(type),
571 info.launchedActivity.info.name);
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200572 }
573
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800574 private void logAppTransitionMultiEvents() {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200575 if (DEBUG_METRICS) Slog.i(TAG, "logging transition events");
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100576 for (int index = mWindowingModeTransitionInfo.size() - 1; index >= 0; index--) {
577 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.valueAt(index);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800578 final int type = getTransitionType(info);
Vishnu Nair132ee832018-09-28 15:00:05 -0700579 if (type == INVALID_TRANSITION_TYPE) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800580 return;
581 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000582
583 // Take a snapshot of the transition info before sending it to the handler for logging.
584 // This will avoid any races with other operations that modify the ActivityRecord.
585 final WindowingModeTransitionInfoSnapshot infoSnapshot =
586 new WindowingModeTransitionInfoSnapshot(info);
Ng Zhi An83473542018-02-20 09:02:14 -0800587 final int currentTransitionDeviceUptime = mCurrentTransitionDeviceUptime;
588 final int currentTransitionDelayMs = mCurrentTransitionDelayMs;
589 BackgroundThread.getHandler().post(() -> logAppTransition(
590 currentTransitionDeviceUptime, currentTransitionDelayMs, infoSnapshot));
Vishnu Nair132ee832018-09-28 15:00:05 -0700591 BackgroundThread.getHandler().post(() -> logAppDisplayed(infoSnapshot));
Calin Juravle759fbda2018-02-20 19:52:30 +0000592
593 info.launchedActivity.info.launchToken = null;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800594 }
595 }
596
Ng Zhi An83473542018-02-20 09:02:14 -0800597 // This gets called on a background thread without holding the activity manager lock.
Calin Juravle759fbda2018-02-20 19:52:30 +0000598 private void logAppTransition(int currentTransitionDeviceUptime, int currentTransitionDelayMs,
599 WindowingModeTransitionInfoSnapshot info) {
600 final LogMaker builder = new LogMaker(APP_TRANSITION);
601 builder.setPackageName(info.packageName);
602 builder.setType(info.type);
603 builder.addTaggedData(FIELD_CLASS_NAME, info.launchedActivityName);
604 final boolean isInstantApp = info.applicationInfo.isInstantApp();
605 if (info.launchedActivityLaunchedFromPackage != null) {
606 builder.addTaggedData(APP_TRANSITION_CALLING_PACKAGE_NAME,
607 info.launchedActivityLaunchedFromPackage);
608 }
609 String launchToken = info.launchedActivityLaunchToken;
610 if (launchToken != null) {
611 builder.addTaggedData(FIELD_INSTANT_APP_LAUNCH_TOKEN, launchToken);
612 }
613 builder.addTaggedData(APP_TRANSITION_IS_EPHEMERAL, isInstantApp ? 1 : 0);
614 builder.addTaggedData(APP_TRANSITION_DEVICE_UPTIME_SECONDS,
615 currentTransitionDeviceUptime);
616 builder.addTaggedData(APP_TRANSITION_DELAY_MS, currentTransitionDelayMs);
617 builder.setSubtype(info.reason);
Vishnu Nair132ee832018-09-28 15:00:05 -0700618 if (info.startingWindowDelayMs != INVALID_DELAY) {
Calin Juravle759fbda2018-02-20 19:52:30 +0000619 builder.addTaggedData(APP_TRANSITION_STARTING_WINDOW_DELAY_MS,
620 info.startingWindowDelayMs);
621 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700622 if (info.bindApplicationDelayMs != INVALID_DELAY) {
Calin Juravle759fbda2018-02-20 19:52:30 +0000623 builder.addTaggedData(APP_TRANSITION_BIND_APPLICATION_DELAY_MS,
624 info.bindApplicationDelayMs);
625 }
626 builder.addTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS, info.windowsDrawnDelayMs);
627 final ArtManagerInternal artManagerInternal = getArtManagerInternal();
628 final PackageOptimizationInfo packageOptimizationInfo =
629 (artManagerInternal == null) || (info.launchedActivityAppRecordRequiredAbi == null)
630 ? PackageOptimizationInfo.createWithNoInfo()
631 : artManagerInternal.getPackageOptimizationInfo(
632 info.applicationInfo,
633 info.launchedActivityAppRecordRequiredAbi);
634 builder.addTaggedData(PACKAGE_OPTIMIZATION_COMPILATION_REASON,
635 packageOptimizationInfo.getCompilationReason());
636 builder.addTaggedData(PACKAGE_OPTIMIZATION_COMPILATION_FILTER,
637 packageOptimizationInfo.getCompilationFilter());
638 mMetricsLogger.write(builder);
639 StatsLog.write(
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800640 StatsLog.APP_START_OCCURRED,
Calin Juravle759fbda2018-02-20 19:52:30 +0000641 info.applicationInfo.uid,
642 info.packageName,
643 convertAppStartTransitionType(info.type),
644 info.launchedActivityName,
645 info.launchedActivityLaunchedFromPackage,
646 isInstantApp,
647 currentTransitionDeviceUptime * 1000,
648 info.reason,
649 currentTransitionDelayMs,
650 info.startingWindowDelayMs,
651 info.bindApplicationDelayMs,
652 info.windowsDrawnDelayMs,
653 launchToken,
654 packageOptimizationInfo.getCompilationReason(),
655 packageOptimizationInfo.getCompilationFilter());
Ng Zhi An83473542018-02-20 09:02:14 -0800656 logAppStartMemoryStateCapture(info);
Calin Juravle759fbda2018-02-20 19:52:30 +0000657 }
658
Vishnu Nair132ee832018-09-28 15:00:05 -0700659 private void logAppDisplayed(WindowingModeTransitionInfoSnapshot info) {
660 if (info.type != TYPE_TRANSITION_WARM_LAUNCH && info.type != TYPE_TRANSITION_COLD_LAUNCH) {
661 return;
662 }
663
664 EventLog.writeEvent(AM_ACTIVITY_LAUNCH_TIME,
665 info.userId, info.activityRecordIdHashCode, info.launchedActivityShortComponentName,
666 info.windowsDrawnDelayMs);
667
668 StringBuilder sb = mStringBuilder;
669 sb.setLength(0);
670 sb.append("Displayed ");
671 sb.append(info.launchedActivityShortComponentName);
672 sb.append(": ");
673 TimeUtils.formatDuration(info.windowsDrawnDelayMs, sb);
674 Log.i(TAG, sb.toString());
675 }
676
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000677 private int convertAppStartTransitionType(int tronType) {
678 if (tronType == TYPE_TRANSITION_COLD_LAUNCH) {
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800679 return StatsLog.APP_START_OCCURRED__TYPE__COLD;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000680 }
681 if (tronType == TYPE_TRANSITION_WARM_LAUNCH) {
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800682 return StatsLog.APP_START_OCCURRED__TYPE__WARM;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000683 }
684 if (tronType == TYPE_TRANSITION_HOT_LAUNCH) {
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800685 return StatsLog.APP_START_OCCURRED__TYPE__HOT;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000686 }
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800687 return StatsLog.APP_START_OCCURRED__TYPE__UNKNOWN;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000688 }
689
Vishnu Nair132ee832018-09-28 15:00:05 -0700690 WindowingModeTransitionInfoSnapshot logAppTransitionReportedDrawn(ActivityRecord r,
691 boolean restoredFromBundle) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100692 final WindowingModeTransitionInfo info = mLastWindowingModeTransitionInfo.get(
693 r.getWindowingMode());
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200694 if (info == null) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700695 return null;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200696 }
697 final LogMaker builder = new LogMaker(APP_TRANSITION_REPORTED_DRAWN);
698 builder.setPackageName(r.packageName);
699 builder.addTaggedData(FIELD_CLASS_NAME, r.info.name);
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000700 long startupTimeMs = SystemClock.uptimeMillis() - mLastTransitionStartTime;
701 builder.addTaggedData(APP_TRANSITION_REPORTED_DRAWN_MS, startupTimeMs);
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200702 builder.setType(restoredFromBundle
703 ? TYPE_TRANSITION_REPORTED_DRAWN_WITH_BUNDLE
704 : TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE);
705 builder.addTaggedData(APP_TRANSITION_PROCESS_RUNNING,
706 info.currentTransitionProcessRunning ? 1 : 0);
707 mMetricsLogger.write(builder);
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000708 StatsLog.write(
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800709 StatsLog.APP_START_FULLY_DRAWN,
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000710 info.launchedActivity.appInfo.uid,
711 info.launchedActivity.packageName,
712 restoredFromBundle
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800713 ? StatsLog.APP_START_FULLY_DRAWN__TYPE__WITH_BUNDLE
714 : StatsLog.APP_START_FULLY_DRAWN__TYPE__WITHOUT_BUNDLE,
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000715 info.launchedActivity.info.name,
716 info.currentTransitionProcessRunning,
717 startupTimeMs);
Vishnu Nair132ee832018-09-28 15:00:05 -0700718 stopFullyDrawnTraceIfNeeded();
719 final WindowingModeTransitionInfoSnapshot infoSnapshot =
720 new WindowingModeTransitionInfoSnapshot(info, r, (int) startupTimeMs);
721 BackgroundThread.getHandler().post(() -> logAppFullyDrawn(infoSnapshot));
722 return infoSnapshot;
723 }
724
725 private void logAppFullyDrawn(WindowingModeTransitionInfoSnapshot info) {
726 if (info.type != TYPE_TRANSITION_WARM_LAUNCH && info.type != TYPE_TRANSITION_COLD_LAUNCH) {
727 return;
728 }
729
730 StringBuilder sb = mStringBuilder;
731 sb.setLength(0);
732 sb.append("Fully drawn ");
733 sb.append(info.launchedActivityShortComponentName);
734 sb.append(": ");
735 TimeUtils.formatDuration(info.windowsFullyDrawnDelayMs, sb);
736 Log.i(TAG, sb.toString());
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200737 }
738
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100739 void logActivityStart(Intent intent, ProcessRecord callerApp, ActivityRecord r,
740 int callingUid, String callingPackage, int callingUidProcState,
741 boolean callingUidHasAnyVisibleWindow,
742 int realCallingUid, int realCallingUidProcState,
743 boolean realCallingUidHasAnyVisibleWindow,
744 int targetUid, String targetPackage, int targetUidProcState,
745 boolean targetUidHasAnyVisibleWindow, String targetWhitelistTag,
746 boolean comingFromPendingIntent) {
747
748 final long nowElapsed = SystemClock.elapsedRealtime();
749 final long nowUptime = SystemClock.uptimeMillis();
750 final LogMaker builder = new LogMaker(ACTION_ACTIVITY_START);
751 builder.setTimestamp(System.currentTimeMillis());
752 builder.addTaggedData(FIELD_CALLING_UID, callingUid);
753 builder.addTaggedData(FIELD_CALLING_PACKAGE_NAME, callingPackage);
754 builder.addTaggedData(FIELD_CALLING_UID_PROC_STATE,
755 processStateAmToProto(callingUidProcState));
756 builder.addTaggedData(FIELD_CALLING_UID_HAS_ANY_VISIBLE_WINDOW,
757 callingUidHasAnyVisibleWindow ? 1 : 0);
758 builder.addTaggedData(FIELD_REAL_CALLING_UID, realCallingUid);
759 builder.addTaggedData(FIELD_REAL_CALLING_UID_PROC_STATE,
760 processStateAmToProto(realCallingUidProcState));
761 builder.addTaggedData(FIELD_REAL_CALLING_UID_HAS_ANY_VISIBLE_WINDOW,
762 realCallingUidHasAnyVisibleWindow ? 1 : 0);
763 builder.addTaggedData(FIELD_TARGET_UID, targetUid);
764 builder.addTaggedData(FIELD_TARGET_PACKAGE_NAME, targetPackage);
765 builder.addTaggedData(FIELD_TARGET_UID_PROC_STATE,
766 processStateAmToProto(targetUidProcState));
767 builder.addTaggedData(FIELD_TARGET_UID_HAS_ANY_VISIBLE_WINDOW,
768 targetUidHasAnyVisibleWindow ? 1 : 0);
769 builder.addTaggedData(FIELD_TARGET_WHITELIST_TAG, targetWhitelistTag);
770 builder.addTaggedData(FIELD_TARGET_SHORT_COMPONENT_NAME, r.shortComponentName);
771 builder.addTaggedData(FIELD_COMING_FROM_PENDING_INTENT, comingFromPendingIntent ? 1 : 0);
772 builder.addTaggedData(FIELD_INTENT_ACTION, intent.getAction());
773 if (callerApp != null) {
774 builder.addTaggedData(FIELD_PROCESS_RECORD_PROCESS_NAME, callerApp.processName);
775 builder.addTaggedData(FIELD_PROCESS_RECORD_CUR_PROC_STATE,
776 processStateAmToProto(callerApp.curProcState));
777 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_CLIENT_ACTIVITIES,
778 callerApp.hasClientActivities ? 1 : 0);
779 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_FOREGROUND_SERVICES,
780 callerApp.hasForegroundServices() ? 1 : 0);
781 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_FOREGROUND_ACTIVITIES,
782 callerApp.foregroundActivities ? 1 : 0);
783 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_TOP_UI, callerApp.hasTopUi ? 1 : 0);
784 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_OVERLAY_UI,
785 callerApp.hasOverlayUi ? 1 : 0);
786 builder.addTaggedData(FIELD_PROCESS_RECORD_PENDING_UI_CLEAN,
787 callerApp.pendingUiClean ? 1 : 0);
788 if (callerApp.interactionEventTime != 0) {
789 builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_LAST_INTERACTION_EVENT,
790 (nowElapsed - callerApp.interactionEventTime));
791 }
792 if (callerApp.fgInteractionTime != 0) {
793 builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_FG_INTERACTION,
794 (nowElapsed - callerApp.fgInteractionTime));
795 }
796 if (callerApp.whenUnimportant != 0) {
797 builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_UNIMPORTANT,
798 (nowUptime - callerApp.whenUnimportant));
799 }
800 }
801 builder.addTaggedData(FIELD_ACTIVITY_RECORD_LAUNCH_MODE, r.info.launchMode);
802 builder.addTaggedData(FIELD_ACTIVITY_RECORD_TARGET_ACTIVITY, r.info.targetActivity);
803 builder.addTaggedData(FIELD_ACTIVITY_RECORD_FLAGS, r.info.flags);
804 builder.addTaggedData(FIELD_ACTIVITY_RECORD_REAL_ACTIVITY, r.realActivity.toShortString());
805 builder.addTaggedData(FIELD_ACTIVITY_RECORD_SHORT_COMPONENT_NAME, r.shortComponentName);
806 builder.addTaggedData(FIELD_ACTIVITY_RECORD_PROCESS_NAME, r.processName);
807 builder.addTaggedData(FIELD_ACTIVITY_RECORD_IS_FULLSCREEN, r.fullscreen ? 1 : 0);
808 builder.addTaggedData(FIELD_ACTIVITY_RECORD_IS_NO_DISPLAY, r.noDisplay ? 1 : 0);
809 if (r.lastVisibleTime != 0) {
810 builder.addTaggedData(FIELD_ACTIVITY_RECORD_MILLIS_SINCE_LAST_VISIBLE,
811 (nowUptime - r.lastVisibleTime));
812 }
813 if (r.resultTo != null) {
814 builder.addTaggedData(FIELD_ACTIVITY_RECORD_RESULT_TO_PKG_NAME, r.resultTo.packageName);
815 builder.addTaggedData(FIELD_ACTIVITY_RECORD_RESULT_TO_SHORT_COMPONENT_NAME,
816 r.resultTo.shortComponentName);
817 }
818 builder.addTaggedData(FIELD_ACTIVITY_RECORD_IS_VISIBLE, r.visible ? 1 : 0);
819 builder.addTaggedData(FIELD_ACTIVITY_RECORD_IS_VISIBLE_IGNORING_KEYGUARD,
820 r.visibleIgnoringKeyguard ? 1 : 0);
821 if (r.lastLaunchTime != 0) {
822 builder.addTaggedData(FIELD_ACTIVITY_RECORD_MILLIS_SINCE_LAST_LAUNCH,
823 (nowUptime - r.lastLaunchTime));
824 }
825 mMetricsLogger.write(builder);
826 }
827
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100828 private int getTransitionType(WindowingModeTransitionInfo info) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800829 if (info.currentTransitionProcessRunning) {
830 if (info.startResult == START_SUCCESS) {
831 return TYPE_TRANSITION_WARM_LAUNCH;
832 } else if (info.startResult == START_TASK_TO_FRONT) {
833 return TYPE_TRANSITION_HOT_LAUNCH;
834 }
835 } else if (info.startResult == START_SUCCESS) {
836 return TYPE_TRANSITION_COLD_LAUNCH;
837 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700838 return INVALID_TRANSITION_TYPE;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800839 }
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800840
Ng Zhi An83473542018-02-20 09:02:14 -0800841 private void logAppStartMemoryStateCapture(WindowingModeTransitionInfoSnapshot info) {
842 if (info.processRecord == null) {
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800843 if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture processRecord null");
844 return;
845 }
846
Wale Ogunwalef6733932018-06-27 05:14:34 -0700847 final int pid = info.processRecord.getPid();
Ng Zhi An83473542018-02-20 09:02:14 -0800848 final int uid = info.applicationInfo.uid;
Rajeev Kumarbfcd9202018-03-02 22:42:13 +0000849 final MemoryStat memoryStat = readMemoryStatFromFilesystem(uid, pid);
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800850 if (memoryStat == null) {
851 if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture memoryStat null");
852 return;
853 }
854
855 StatsLog.write(
856 StatsLog.APP_START_MEMORY_STATE_CAPTURED,
857 uid,
Ng Zhi An83473542018-02-20 09:02:14 -0800858 info.processName,
859 info.launchedActivityName,
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800860 memoryStat.pgfault,
861 memoryStat.pgmajfault,
862 memoryStat.rssInBytes,
863 memoryStat.cacheInBytes,
864 memoryStat.swapInBytes);
865 }
866
Wale Ogunwalef6733932018-06-27 05:14:34 -0700867 private WindowProcessController findProcessForActivity(ActivityRecord launchedActivity) {
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800868 return launchedActivity != null
Wale Ogunwalef6733932018-06-27 05:14:34 -0700869 ? mSupervisor.mService.mProcessNames.get(
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700870 launchedActivity.processName, launchedActivity.appInfo.uid)
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800871 : null;
872 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000873
874 private ArtManagerInternal getArtManagerInternal() {
875 if (mArtManagerInternal == null) {
876 // Note that this may be null.
877 // ArtManagerInternal is registered during PackageManagerService
878 // initialization which happens after ActivityManagerService.
879 mArtManagerInternal = LocalServices.getService(ArtManagerInternal.class);
880 }
881 return mArtManagerInternal;
882 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700883
884 /**
885 * Starts traces for app launch and draw times. We stop the fully drawn trace if its already
886 * active since the app may not have reported fully drawn in the previous launch.
887 *
888 * See {@link android.app.Activity#reportFullyDrawn()}
889 *
890 * @param info
891 * */
892 private void startTraces(WindowingModeTransitionInfo info) {
893 if (info == null) {
894 return;
895 }
896 stopFullyDrawnTraceIfNeeded();
897 int transitionType = getTransitionType(info);
898 if (!info.launchTraceActive && transitionType == TYPE_TRANSITION_WARM_LAUNCH
899 || transitionType == TYPE_TRANSITION_COLD_LAUNCH) {
900 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: "
901 + info.launchedActivity.packageName, 0);
902 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
903 mDrawingTraceActive = true;
904 info.launchTraceActive = true;
905 }
906 }
907
908 private void stopLaunchTrace(WindowingModeTransitionInfo info) {
909 if (info == null) {
910 return;
911 }
912 if (info.launchTraceActive) {
913 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: "
914 + info.launchedActivity.packageName, 0);
915 info.launchTraceActive = false;
916 }
917 }
918
919 void stopFullyDrawnTraceIfNeeded() {
920 if (mDrawingTraceActive) {
921 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "drawing", 0);
922 mDrawingTraceActive = false;
923 }
924 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800925}