blob: c506e274ef6457429710d56bc341e79eee4ace07 [file] [log] [blame]
Wale Ogunwale59507092018-10-29 09:00:30 -07001package com.android.server.wm;
Filip Gruszczynski77d94482015-12-11 13:59:52 -08002
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;
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -08006import static android.app.WaitResult.LAUNCH_STATE_COLD;
7import static android.app.WaitResult.LAUNCH_STATE_HOT;
8import static android.app.WaitResult.LAUNCH_STATE_WARM;
Bryce Lee6c605092017-10-12 11:14:49 -07009import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale3382ab12017-07-27 08:55:03 -070010import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
11import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Vishnu Nair132ee832018-09-28 15:00:05 -070012import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwale926aade2017-08-29 11:24:37 -070013import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
14import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Vishnu Nair132ee832018-09-28 15:00:05 -070015
Michal Karpinski201bc0c2018-07-20 15:32:00 +010016import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_ACTIVITY_START;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080017import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION;
Jorim Jaggi515dd682017-05-05 15:05:07 +020018import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_BIND_APPLICATION_DELAY_MS;
Todd Kennedy50d946c12017-03-17 13:55:38 -070019import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_CALLING_PACKAGE_NAME;
Jorim Jaggi172e99f2017-10-20 14:33:18 +020020import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_CANCELLED;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080021import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DELAY_MS;
22import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS;
Todd Kennedy50d946c12017-03-17 13:55:38 -070023import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_IS_EPHEMERAL;
Jorim Jaggi4d27b842017-08-17 17:22:26 +020024import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_PROCESS_RUNNING;
25import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN;
26import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN_MS;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080027import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS;
28import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010029import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_PACKAGE_NAME;
30import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_UID;
31import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_UID_HAS_ANY_VISIBLE_WINDOW;
32import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_UID_PROC_STATE;
Jorim Jaggicdfc04e2017-04-28 19:06:24 +020033import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CLASS_NAME;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010034import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_COMING_FROM_PENDING_INTENT;
Todd Kennedy50d946c12017-03-17 13:55:38 -070035import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_INSTANT_APP_LAUNCH_TOKEN;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010036import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_INTENT_ACTION;
37import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_CUR_PROC_STATE;
38import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_CLIENT_ACTIVITIES;
39import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_FOREGROUND_ACTIVITIES;
40import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_FOREGROUND_SERVICES;
41import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_OVERLAY_UI;
42import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_TOP_UI;
43import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_MILLIS_SINCE_FG_INTERACTION;
44import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_MILLIS_SINCE_LAST_INTERACTION_EVENT;
45import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_MILLIS_SINCE_UNIMPORTANT;
46import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_PENDING_UI_CLEAN;
47import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_PROCESS_NAME;
48import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_REAL_CALLING_UID;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010049import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_REAL_CALLING_UID_HAS_ANY_VISIBLE_WINDOW;
Vishnu Nair132ee832018-09-28 15:00:05 -070050import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_REAL_CALLING_UID_PROC_STATE;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010051import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TARGET_SHORT_COMPONENT_NAME;
Calin Juravle759fbda2018-02-20 19:52:30 +000052import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PACKAGE_OPTIMIZATION_COMPILATION_FILTER;
Vishnu Nair132ee832018-09-28 15:00:05 -070053import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PACKAGE_OPTIMIZATION_COMPILATION_REASON;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080054import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_COLD_LAUNCH;
55import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_HOT_LAUNCH;
Jorim Jaggi4d27b842017-08-17 17:22:26 +020056import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE;
57import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_REPORTED_DRAWN_WITH_BUNDLE;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080058import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_WARM_LAUNCH;
Vishnu Nair132ee832018-09-28 15:00:05 -070059import static com.android.server.am.EventLogTags.AM_ACTIVITY_LAUNCH_TIME;
Ng Zhi Anbbefdec2018-01-30 17:12:39 -080060import static com.android.server.am.MemoryStatUtil.MemoryStat;
Rajeev Kumarbfcd9202018-03-02 22:42:13 +000061import static com.android.server.am.MemoryStatUtil.readMemoryStatFromFilesystem;
Vishnu Naira62534b2018-11-09 09:13:22 -080062import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_METRICS;
63import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
64import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
Vishnu Nair132ee832018-09-28 15:00:05 -070065import static com.android.server.wm.ActivityTaskManagerInternal.APP_TRANSITION_TIMEOUT;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080066
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -080067import android.app.WaitResult;
Igor Murashkin212d06c2018-10-22 16:34:39 -070068import android.app.WindowConfiguration.WindowingMode;
Michal Karpinski319069b2019-03-27 15:46:50 +000069import android.content.ComponentName;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080070import android.content.Context;
Michal Karpinski201bc0c2018-07-20 15:32:00 +010071import android.content.Intent;
Calin Juravle759fbda2018-02-20 19:52:30 +000072import android.content.pm.ApplicationInfo;
73import android.content.pm.dex.ArtManagerInternal;
74import android.content.pm.dex.PackageOptimizationInfo;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080075import android.metrics.LogMaker;
Jorim Jaggi172e99f2017-10-20 14:33:18 +020076import android.os.Handler;
77import android.os.Looper;
78import android.os.Message;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080079import android.os.SystemClock;
Vishnu Nair132ee832018-09-28 15:00:05 -070080import android.os.Trace;
81import android.util.EventLog;
82import android.util.Log;
Jorim Jaggi172e99f2017-10-20 14:33:18 +020083import android.util.Slog;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080084import android.util.SparseArray;
85import android.util.SparseIntArray;
Olivier Gaillardaed7f122017-12-12 14:26:22 +000086import android.util.StatsLog;
Vishnu Nair132ee832018-09-28 15:00:05 -070087import android.util.TimeUtils;
Igor Murashkinc0b47e42018-11-07 15:54:18 -080088import android.util.proto.ProtoOutputStream;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080089
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -080090import com.android.internal.annotations.VisibleForTesting;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080091import com.android.internal.logging.MetricsLogger;
Ng Zhi An83473542018-02-20 09:02:14 -080092import com.android.internal.os.BackgroundThread;
Jorim Jaggi172e99f2017-10-20 14:33:18 +020093import com.android.internal.os.SomeArgs;
Calin Juravle759fbda2018-02-20 19:52:30 +000094import com.android.server.LocalServices;
Yan Wang9eac5ec2019-09-30 15:42:18 -070095import java.util.concurrent.TimeUnit;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080096
97/**
Vishnu Nair132ee832018-09-28 15:00:05 -070098 * Listens to activity launches, transitions, visibility changes and window drawn callbacks to
99 * determine app launch times and draw delays. Source of truth for activity metrics and provides
100 * data for Tron, logcat, event logs and {@link android.app.WaitResult}.
101 *
102 * Tests:
Tadashi G. Takaoka28bc3702019-04-23 11:01:48 +0900103 * atest CtsWindowManagerDeviceTestCases:ActivityMetricsLoggerTests
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800104 */
105class ActivityMetricsLogger {
Jorim Jaggif9704102016-05-05 19:14:22 -0700106
Wale Ogunwale98875612018-10-12 07:53:02 -0700107 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityMetricsLogger" : TAG_ATM;
Jorim Jaggif9704102016-05-05 19:14:22 -0700108
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800109 // Window modes we are interested in logging. If we ever introduce a new type, we need to add
110 // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array.
111 private static final int WINDOW_STATE_STANDARD = 0;
112 private static final int WINDOW_STATE_SIDE_BY_SIDE = 1;
113 private static final int WINDOW_STATE_FREEFORM = 2;
Winson Chung83471632016-12-13 11:02:12 -0800114 private static final int WINDOW_STATE_ASSISTANT = 3;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800115 private static final int WINDOW_STATE_INVALID = -1;
116
Jorim Jaggi275561a2016-02-23 10:11:02 -0500117 private static final long INVALID_START_TIME = -1;
Vishnu Nair132ee832018-09-28 15:00:05 -0700118 private static final int INVALID_DELAY = -1;
119 private static final int INVALID_TRANSITION_TYPE = -1;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500120
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200121 private static final int MSG_CHECK_VISIBILITY = 0;
122
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800123 // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
124 // time we log.
125 private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = {
Winson Chung83471632016-12-13 11:02:12 -0800126 "window_time_0", "window_time_1", "window_time_2", "window_time_3"};
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800127
128 private int mWindowState = WINDOW_STATE_STANDARD;
129 private long mLastLogTimeSecs;
130 private final ActivityStackSupervisor mSupervisor;
131 private final Context mContext;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800132 private final MetricsLogger mMetricsLogger = new MetricsLogger();
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800133
Igor Murashkin212d06c2018-10-22 16:34:39 -0700134 // set to INVALID_START_TIME in reset.
135 // set to valid value in notifyActivityLaunching
Yan Wangd47f90b2019-10-03 19:17:15 -0700136 private long mCurrentTransitionStartTimeNs = INVALID_START_TIME;
137 private long mLastTransitionStartTimeNs = INVALID_START_TIME;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800138
139 private int mCurrentTransitionDeviceUptime;
140 private int mCurrentTransitionDelayMs;
Igor Murashkin212d06c2018-10-22 16:34:39 -0700141
142 /** If the any app transitions have been logged as starting, after the latest reset. */
Jorim Jaggi275561a2016-02-23 10:11:02 -0500143 private boolean mLoggedTransitionStarting;
144
Igor Murashkin212d06c2018-10-22 16:34:39 -0700145 /** Map : @WindowingMode int => WindowingModeTransitionInfo */
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100146 private final SparseArray<WindowingModeTransitionInfo> mWindowingModeTransitionInfo =
147 new SparseArray<>();
Igor Murashkin212d06c2018-10-22 16:34:39 -0700148 /** Map : @WindowingMode int => WindowingModeTransitionInfo */
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100149 private final SparseArray<WindowingModeTransitionInfo> mLastWindowingModeTransitionInfo =
150 new SparseArray<>();
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200151 private final H mHandler;
Calin Juravle759fbda2018-02-20 19:52:30 +0000152
153 private ArtManagerInternal mArtManagerInternal;
Vishnu Nair132ee832018-09-28 15:00:05 -0700154 private final StringBuilder mStringBuilder = new StringBuilder();
Calin Juravle759fbda2018-02-20 19:52:30 +0000155
Igor Murashkin212d06c2018-10-22 16:34:39 -0700156 /**
157 * Due to the global single concurrent launch sequence, all calls to this observer must be made
158 * in-order on the same thread to fulfill the "happens-before" guarantee in LaunchObserver.
159 */
Igor Murashkinc0b47e42018-11-07 15:54:18 -0800160 private final LaunchObserverRegistryImpl mLaunchObserver;
161 @VisibleForTesting static final int LAUNCH_OBSERVER_ACTIVITY_RECORD_PROTO_CHUNK_SIZE = 512;
Igor Murashkin212d06c2018-10-22 16:34:39 -0700162
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200163 private final class H extends Handler {
164
165 public H(Looper looper) {
166 super(looper);
167 }
168
169 @Override
170 public void handleMessage(Message msg) {
171 switch (msg.what) {
172 case MSG_CHECK_VISIBILITY:
173 final SomeArgs args = (SomeArgs) msg.obj;
174 checkVisibility((TaskRecord) args.arg1, (ActivityRecord) args.arg2);
175 break;
176 }
177 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000178 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800179
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100180 private final class WindowingModeTransitionInfo {
Igor Murashkin212d06c2018-10-22 16:34:39 -0700181 /** The latest activity to have been launched. */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800182 private ActivityRecord launchedActivity;
183 private int startResult;
184 private boolean currentTransitionProcessRunning;
Vishnu Nair132ee832018-09-28 15:00:05 -0700185 /** Elapsed time from when we launch an activity to when its windows are drawn. */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800186 private int windowsDrawnDelayMs;
Vishnu Nair132ee832018-09-28 15:00:05 -0700187 private int startingWindowDelayMs = INVALID_DELAY;
188 private int bindApplicationDelayMs = INVALID_DELAY;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800189 private int reason = APP_TRANSITION_TIMEOUT;
Riddle Hsu99f2acd2019-10-22 18:41:51 +0800190 // TODO(b/132736359) The number may need to consider the visibility change.
191 private int numUndrawnActivities = 1;
Riddle Hsuc1f8f9c2019-10-25 16:54:33 +0800192 /** Non-null if the application has reported drawn but its window hasn't. */
193 private Runnable pendingFullyDrawn;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800194 private boolean loggedStartingWindowDrawn;
Vishnu Nair132ee832018-09-28 15:00:05 -0700195 private boolean launchTraceActive;
Riddle Hsu1c5d72e2019-10-09 20:54:26 +0800196
197 /**
198 * Remembers the latest launched activity to represent the final transition. This also
199 * increments the number of activities that should be drawn, so a consecutive launching
200 * sequence can be coalesced as one event.
201 */
202 void setLatestLaunchedActivity(ActivityRecord r) {
203 if (launchedActivity == r) {
204 return;
205 }
206 launchedActivity = r;
Riddle Hsu1c5d72e2019-10-09 20:54:26 +0800207 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800208 }
209
Vishnu Nair132ee832018-09-28 15:00:05 -0700210 final class WindowingModeTransitionInfoSnapshot {
Calin Juravle759fbda2018-02-20 19:52:30 +0000211 final private ApplicationInfo applicationInfo;
Wale Ogunwalef6733932018-06-27 05:14:34 -0700212 final private WindowProcessController processRecord;
Vishnu Nair132ee832018-09-28 15:00:05 -0700213 final String packageName;
214 final String launchedActivityName;
Calin Juravle759fbda2018-02-20 19:52:30 +0000215 final private String launchedActivityLaunchedFromPackage;
216 final private String launchedActivityLaunchToken;
217 final private String launchedActivityAppRecordRequiredAbi;
Vishnu Nair132ee832018-09-28 15:00:05 -0700218 final String launchedActivityShortComponentName;
Ng Zhi An83473542018-02-20 09:02:14 -0800219 final private String processName;
Calin Juravle759fbda2018-02-20 19:52:30 +0000220 final private int reason;
221 final private int startingWindowDelayMs;
222 final private int bindApplicationDelayMs;
Vishnu Nair132ee832018-09-28 15:00:05 -0700223 final int windowsDrawnDelayMs;
224 final int type;
225 final int userId;
226 /**
227 * Elapsed time from when we launch an activity to when the app reported it was
228 * fully drawn. If this is not reported then the value is set to INVALID_DELAY.
229 */
230 final int windowsFullyDrawnDelayMs;
231 final int activityRecordIdHashCode;
Calin Juravle759fbda2018-02-20 19:52:30 +0000232
233 private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700234 this(info, info.launchedActivity);
235 }
236
237 private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info,
238 ActivityRecord launchedActivity) {
239 this(info, launchedActivity, INVALID_DELAY);
240 }
241
242 private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info,
243 ActivityRecord launchedActivity, int windowsFullyDrawnDelayMs) {
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700244 applicationInfo = launchedActivity.info.applicationInfo;
Vishnu Nair132ee832018-09-28 15:00:05 -0700245 packageName = launchedActivity.packageName;
246 launchedActivityName = launchedActivity.info.name;
247 launchedActivityLaunchedFromPackage = launchedActivity.launchedFromPackage;
248 launchedActivityLaunchToken = launchedActivity.info.launchToken;
249 launchedActivityAppRecordRequiredAbi = launchedActivity.app == null
Calin Juravle759fbda2018-02-20 19:52:30 +0000250 ? null
Vishnu Nairaf0ea312018-10-15 16:23:55 -0700251 : launchedActivity.app.getRequiredAbi();
Calin Juravle759fbda2018-02-20 19:52:30 +0000252 reason = info.reason;
253 startingWindowDelayMs = info.startingWindowDelayMs;
254 bindApplicationDelayMs = info.bindApplicationDelayMs;
255 windowsDrawnDelayMs = info.windowsDrawnDelayMs;
256 type = getTransitionType(info);
Vishnu Nairaf0ea312018-10-15 16:23:55 -0700257 processRecord = findProcessForActivity(launchedActivity);
258 processName = launchedActivity.processName;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800259 userId = launchedActivity.mUserId;
Vishnu Nair132ee832018-09-28 15:00:05 -0700260 launchedActivityShortComponentName = launchedActivity.shortComponentName;
261 activityRecordIdHashCode = System.identityHashCode(launchedActivity);
262 this.windowsFullyDrawnDelayMs = windowsFullyDrawnDelayMs;
Calin Juravle759fbda2018-02-20 19:52:30 +0000263 }
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -0800264
265 @WaitResult.LaunchState int getLaunchState() {
266 switch (type) {
267 case TYPE_TRANSITION_WARM_LAUNCH:
268 return LAUNCH_STATE_WARM;
269 case TYPE_TRANSITION_HOT_LAUNCH:
270 return LAUNCH_STATE_HOT;
271 case TYPE_TRANSITION_COLD_LAUNCH:
272 return LAUNCH_STATE_COLD;
273 default:
274 return -1;
275 }
276 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000277 }
278
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200279 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context, Looper looper) {
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800280 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
281 mSupervisor = supervisor;
282 mContext = context;
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200283 mHandler = new H(looper);
Igor Murashkinc0b47e42018-11-07 15:54:18 -0800284 mLaunchObserver = new LaunchObserverRegistryImpl(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800285 }
286
287 void logWindowState() {
288 final long now = SystemClock.elapsedRealtime() / 1000;
289 if (mWindowState != WINDOW_STATE_INVALID) {
290 // We log even if the window state hasn't changed, because the user might remain in
291 // home/fullscreen move forever and we would like to track this kind of behavior
292 // too.
293 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
294 (int) (now - mLastLogTimeSecs));
295 }
296 mLastLogTimeSecs = now;
297
Wale Ogunwale926aade2017-08-29 11:24:37 -0700298 mWindowState = WINDOW_STATE_INVALID;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800299 ActivityStack stack =
300 mSupervisor.mRootActivityContainer.getTopDisplayFocusedStack();
lumarkf6c4a982018-06-15 15:43:12 +0800301 if (stack == null) {
302 return;
303 }
304
Wale Ogunwale926aade2017-08-29 11:24:37 -0700305 if (stack.isActivityTypeAssistant()) {
306 mWindowState = WINDOW_STATE_ASSISTANT;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800307 return;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800308 }
Wale Ogunwale926aade2017-08-29 11:24:37 -0700309
Igor Murashkin212d06c2018-10-22 16:34:39 -0700310 @WindowingMode int windowingMode = stack.getWindowingMode();
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700311 if (windowingMode == WINDOWING_MODE_PINNED) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800312 stack = mSupervisor.mRootActivityContainer.findStackBehind(stack);
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700313 windowingMode = stack.getWindowingMode();
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800314 }
Wale Ogunwale926aade2017-08-29 11:24:37 -0700315 switch (windowingMode) {
316 case WINDOWING_MODE_FULLSCREEN:
317 mWindowState = WINDOW_STATE_STANDARD;
318 break;
319 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY:
320 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY:
321 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
322 break;
Bryce Lee6c605092017-10-12 11:14:49 -0700323 case WINDOWING_MODE_FREEFORM:
Wale Ogunwale926aade2017-08-29 11:24:37 -0700324 mWindowState = WINDOW_STATE_FREEFORM;
325 break;
326 default:
327 if (windowingMode != WINDOWING_MODE_UNDEFINED) {
328 throw new IllegalStateException("Unknown windowing mode for stack=" + stack
329 + " windowingMode=" + windowingMode);
330 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800331 }
332 }
Jorim Jaggi275561a2016-02-23 10:11:02 -0500333
334 /**
335 * Notifies the tracker at the earliest possible point when we are starting to launch an
336 * activity.
337 */
Igor Murashkin212d06c2018-10-22 16:34:39 -0700338 void notifyActivityLaunching(Intent intent) {
339 if (DEBUG_METRICS) {
340 Slog.i(TAG, String.format("notifyActivityLaunching: active:%b, intent:%s",
341 isAnyTransitionActive(),
342 intent));
343 }
344
Yan Wangd47f90b2019-10-03 19:17:15 -0700345 if (mCurrentTransitionStartTimeNs == INVALID_START_TIME) {
Igor Murashkin212d06c2018-10-22 16:34:39 -0700346
Yan Wangd47f90b2019-10-03 19:17:15 -0700347 mCurrentTransitionStartTimeNs = SystemClock.elapsedRealtimeNanos();
348 mLastTransitionStartTimeNs = mCurrentTransitionStartTimeNs;
Igor Murashkin212d06c2018-10-22 16:34:39 -0700349
Yan Wangd47f90b2019-10-03 19:17:15 -0700350 launchObserverNotifyIntentStarted(intent, mCurrentTransitionStartTimeNs);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800351 }
Jorim Jaggi275561a2016-02-23 10:11:02 -0500352 }
353
354 /**
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700355 * Notifies the tracker that the activity is actually launching.
356 *
357 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
358 * launch
359 * @param launchedActivity the activity that is being launched
360 */
361 void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700362 final WindowProcessController processRecord = findProcessForActivity(launchedActivity);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700363 final boolean processRunning = processRecord != null;
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700364
365 // We consider this a "process switch" if the process of the activity that gets launched
366 // didn't have an activity that was in started state. In this case, we assume that lot
367 // of caches might be purged so the time until it produces the first frame is very
368 // interesting.
369 final boolean processSwitch = processRecord == null
Wale Ogunwalef6733932018-06-27 05:14:34 -0700370 || !processRecord.hasStartedActivity(launchedActivity);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700371
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800372 notifyActivityLaunched(resultCode, launchedActivity, processRunning, processSwitch);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700373 }
374
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700375 /**
Jorim Jaggi275561a2016-02-23 10:11:02 -0500376 * Notifies the tracker the the activity is actually launching.
377 *
378 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
379 * launch
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800380 * @param launchedActivity the activity being launched
Jorim Jaggi275561a2016-02-23 10:11:02 -0500381 * @param processRunning whether the process that will contains the activity is already running
Jorim Jaggibe67c902016-04-12 00:53:16 -0700382 * @param processSwitch whether the process that will contain the activity didn't have any
383 * activity that was stopped, i.e. the started activity is "switching"
384 * processes
Jorim Jaggi275561a2016-02-23 10:11:02 -0500385 */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800386 private void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity,
Jorim Jaggibe67c902016-04-12 00:53:16 -0700387 boolean processRunning, boolean processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500388
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200389 if (DEBUG_METRICS) Slog.i(TAG, "notifyActivityLaunched"
390 + " resultCode=" + resultCode
391 + " launchedActivity=" + launchedActivity
392 + " processRunning=" + processRunning
393 + " processSwitch=" + processSwitch);
394
Igor Murashkin212d06c2018-10-22 16:34:39 -0700395 // If we are already in an existing transition, only update the activity name, but not the
396 // other attributes.
397 final @WindowingMode int windowingMode = launchedActivity != null
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100398 ? launchedActivity.getWindowingMode()
399 : WINDOWING_MODE_UNDEFINED;
Vishnu Nairf8accc52018-10-11 10:19:54 -0700400 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode);
Yan Wangd47f90b2019-10-03 19:17:15 -0700401 if (mCurrentTransitionStartTimeNs == INVALID_START_TIME) {
Vishnu Nairf8accc52018-10-11 10:19:54 -0700402 // No transition is active ignore this launch.
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200403 return;
404 }
405
Vishnu Nair9ba31652018-11-13 14:34:05 -0800406 if (launchedActivity != null && launchedActivity.mDrawn) {
Vishnu Nairf8accc52018-10-11 10:19:54 -0700407 // Launched activity is already visible. We cannot measure windows drawn delay.
Riddle Hsu1063b512019-10-22 21:12:23 +0800408 abort(info, "launched activity already visible");
Vishnu Nairf8accc52018-10-11 10:19:54 -0700409 return;
410 }
411
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800412 if (launchedActivity != null && info != null) {
Vishnu Nairf8accc52018-10-11 10:19:54 -0700413 // If we are already in an existing transition, only update the activity name, but not
414 // the other attributes.
Igor Murashkin212d06c2018-10-22 16:34:39 -0700415
416 // Coalesce multiple (trampoline) activities from a single sequence together.
Riddle Hsu1c5d72e2019-10-09 20:54:26 +0800417 info.setLatestLaunchedActivity(launchedActivity);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500418 return;
419 }
420
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100421 final boolean otherWindowModesLaunching =
422 mWindowingModeTransitionInfo.size() > 0 && info == null;
Jorim Jaggi54cff642018-03-15 15:51:32 +0100423 if ((!isLoggableResultCode(resultCode) || launchedActivity == null || !processSwitch
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100424 || windowingMode == WINDOWING_MODE_UNDEFINED) && !otherWindowModesLaunching) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800425 // Failed to launch or it was not a process switch, so we don't care about the timing.
Riddle Hsu1063b512019-10-22 21:12:23 +0800426 abort(info, "failed to launch or not a process switch");
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800427 return;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100428 } else if (otherWindowModesLaunching) {
429 // Don't log this windowing mode but continue with the other windowing modes.
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800430 return;
431 }
432
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200433 if (DEBUG_METRICS) Slog.i(TAG, "notifyActivityLaunched successful");
434
Igor Murashkin212d06c2018-10-22 16:34:39 -0700435 // A new launch sequence [with the windowingMode] has begun.
436 // Start tracking it.
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100437 final WindowingModeTransitionInfo newInfo = new WindowingModeTransitionInfo();
Riddle Hsu1c5d72e2019-10-09 20:54:26 +0800438 newInfo.setLatestLaunchedActivity(launchedActivity);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800439 newInfo.currentTransitionProcessRunning = processRunning;
440 newInfo.startResult = resultCode;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100441 mWindowingModeTransitionInfo.put(windowingMode, newInfo);
442 mLastWindowingModeTransitionInfo.put(windowingMode, newInfo);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800443 mCurrentTransitionDeviceUptime = (int) (SystemClock.uptimeMillis() / 1000);
Vishnu Nair132ee832018-09-28 15:00:05 -0700444 startTraces(newInfo);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700445 launchObserverNotifyActivityLaunched(newInfo);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500446 }
447
448 /**
Jorim Jaggi54cff642018-03-15 15:51:32 +0100449 * @return True if we should start logging an event for an activity start that returned
450 * {@code resultCode} and that we'll indeed get a windows drawn event.
451 */
452 private boolean isLoggableResultCode(int resultCode) {
453 return resultCode == START_SUCCESS || resultCode == START_TASK_TO_FRONT;
454 }
455
456 /**
Jorim Jaggi275561a2016-02-23 10:11:02 -0500457 * Notifies the tracker that all windows of the app have been drawn.
458 */
Igor Murashkin212d06c2018-10-22 16:34:39 -0700459 WindowingModeTransitionInfoSnapshot notifyWindowsDrawn(@WindowingMode int windowingMode,
Yan Wangd47f90b2019-10-03 19:17:15 -0700460 long timestampNs) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100461 if (DEBUG_METRICS) Slog.i(TAG, "notifyWindowsDrawn windowingMode=" + windowingMode);
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200462
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100463 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode);
Riddle Hsu1c5d72e2019-10-09 20:54:26 +0800464 if (info == null || info.numUndrawnActivities == 0) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700465 return null;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500466 }
Yan Wangd47f90b2019-10-03 19:17:15 -0700467 info.windowsDrawnDelayMs = calculateDelay(timestampNs);
Riddle Hsu1c5d72e2019-10-09 20:54:26 +0800468 info.numUndrawnActivities--;
Vishnu Nair132ee832018-09-28 15:00:05 -0700469 final WindowingModeTransitionInfoSnapshot infoSnapshot =
470 new WindowingModeTransitionInfoSnapshot(info);
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100471 if (allWindowsDrawn() && mLoggedTransitionStarting) {
Riddle Hsu1063b512019-10-22 21:12:23 +0800472 reset(false /* abort */, info, "notifyWindowsDrawn - all windows drawn", timestampNs);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500473 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700474 return infoSnapshot;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500475 }
476
477 /**
478 * Notifies the tracker that the starting window was drawn.
479 */
Yan Wangb0b20062019-10-30 12:47:09 -0700480 void notifyStartingWindowDrawn(@WindowingMode int windowingMode, long timestampNs) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100481 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800482 if (info == null || info.loggedStartingWindowDrawn) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500483 return;
484 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800485 info.loggedStartingWindowDrawn = true;
Yan Wangb0b20062019-10-30 12:47:09 -0700486 info.startingWindowDelayMs = calculateDelay(timestampNs);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500487 }
488
489 /**
490 * Notifies the tracker that the app transition is starting.
491 *
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100492 * @param windowingModeToReason A map from windowing mode to a reason integer, which must be on
Vishnu Nair132ee832018-09-28 15:00:05 -0700493 * of ActivityTaskManagerInternal.APP_TRANSITION_* reasons.
Jorim Jaggi275561a2016-02-23 10:11:02 -0500494 */
Yan Wangd47f90b2019-10-03 19:17:15 -0700495 void notifyTransitionStarting(SparseIntArray windowingModeToReason, long timestampNs) {
Jorim Jaggid8a57772017-04-14 16:50:42 -0700496 if (!isAnyTransitionActive() || mLoggedTransitionStarting) {
Igor Murashkin212d06c2018-10-22 16:34:39 -0700497 // Ignore calls to this made after a reset and prior to notifyActivityLaunching.
498
499 // Ignore any subsequent notifyTransitionStarting until the next reset.
Jorim Jaggi275561a2016-02-23 10:11:02 -0500500 return;
501 }
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200502 if (DEBUG_METRICS) Slog.i(TAG, "notifyTransitionStarting");
Yan Wangd47f90b2019-10-03 19:17:15 -0700503 mCurrentTransitionDelayMs = calculateDelay(timestampNs);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500504 mLoggedTransitionStarting = true;
Igor Murashkin212d06c2018-10-22 16:34:39 -0700505
506 WindowingModeTransitionInfo foundInfo = null;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100507 for (int index = windowingModeToReason.size() - 1; index >= 0; index--) {
Igor Murashkin212d06c2018-10-22 16:34:39 -0700508 final @WindowingMode int windowingMode = windowingModeToReason.keyAt(index);
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100509 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(
510 windowingMode);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800511 if (info == null) {
512 continue;
513 }
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100514 info.reason = windowingModeToReason.valueAt(index);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700515 foundInfo = info;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800516 }
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100517 if (allWindowsDrawn()) {
Vishnu Naira62534b2018-11-09 09:13:22 -0800518 // abort metrics collection if we cannot find a matching transition.
519 final boolean abortMetrics = foundInfo == null;
Yan Wangd47f90b2019-10-03 19:17:15 -0700520 reset(abortMetrics, foundInfo, "notifyTransitionStarting - all windows drawn",
521 timestampNs /* timestampNs */);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500522 }
523 }
524
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200525 /**
526 * Notifies the tracker that the visibility of an app is changing.
527 *
528 * @param activityRecord the app that is changing its visibility
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200529 */
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200530 void notifyVisibilityChanged(ActivityRecord activityRecord) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100531 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(
532 activityRecord.getWindowingMode());
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200533 if (info == null) {
534 return;
535 }
536 if (info.launchedActivity != activityRecord) {
537 return;
538 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800539 final TaskRecord t = activityRecord.getTaskRecord();
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200540 final SomeArgs args = SomeArgs.obtain();
541 args.arg1 = t;
542 args.arg2 = activityRecord;
543 mHandler.obtainMessage(MSG_CHECK_VISIBILITY, args).sendToTarget();
544 }
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200545
Riddle Hsu1063b512019-10-22 21:12:23 +0800546 /** @return {@code true} if the given task has an activity will be drawn. */
547 private static boolean hasActivityToBeDrawn(TaskRecord t) {
Wale Ogunwale1a06f152019-10-11 11:26:30 +0200548 for (int i = t.getChildCount() - 1; i >= 0; --i) {
549 final ActivityRecord r = t.getChildAt(i);
Riddle Hsu1063b512019-10-22 21:12:23 +0800550 if (r.visible && !r.mDrawn && !r.finishing) {
Vishnu Nairb1de42d2019-01-28 09:49:51 -0800551 return true;
552 }
553 }
554 return false;
555 }
556
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200557 private void checkVisibility(TaskRecord t, ActivityRecord r) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700558 synchronized (mSupervisor.mService.mGlobalLock) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200559
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100560 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(
561 r.getWindowingMode());
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200562
563 // If we have an active transition that's waiting on a certain activity that will be
564 // invisible now, we'll never get onWindowsDrawn, so abort the transition if necessary.
Vishnu Naird15063b2019-06-11 08:08:25 -0700565
566 // We have no active transitions.
567 if (info == null) {
568 return;
569 }
570
571 // The notified activity whose visibility changed is no longer the launched activity.
572 // We can still wait to get onWindowsDrawn.
573 if (info.launchedActivity != r) {
574 return;
575 }
576
Riddle Hsu1063b512019-10-22 21:12:23 +0800577 // If the task of the launched activity contains any activity to be drawn, then the
578 // window drawn event should report later to complete the transition. Otherwise all
579 // activities in this task may be finished, invisible or drawn, so the transition event
580 // should be cancelled.
581 if (hasActivityToBeDrawn(t)) {
Vishnu Naird15063b2019-06-11 08:08:25 -0700582 return;
583 }
584
585 if (DEBUG_METRICS) Slog.i(TAG, "notifyVisibilityChanged to invisible activity=" + r);
586 logAppTransitionCancel(info);
Riddle Hsu1063b512019-10-22 21:12:23 +0800587 // Abort if this is the only one active transition.
588 if (mWindowingModeTransitionInfo.size() == 1
589 && mWindowingModeTransitionInfo.get(r.getWindowingMode()) != null) {
590 abort(info, "notifyVisibilityChanged to invisible");
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200591 }
592 }
593 }
594
Jorim Jaggi515dd682017-05-05 15:05:07 +0200595 /**
596 * Notifies the tracker that we called immediately before we call bindApplication on the client.
597 *
Wale Ogunwale31913b52018-10-13 08:29:31 -0700598 * @param appInfo The client into which we'll call bindApplication.
Jorim Jaggi515dd682017-05-05 15:05:07 +0200599 */
Wale Ogunwale31913b52018-10-13 08:29:31 -0700600 void notifyBindApplication(ApplicationInfo appInfo) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100601 for (int i = mWindowingModeTransitionInfo.size() - 1; i >= 0; i--) {
602 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.valueAt(i);
Jorim Jaggi515dd682017-05-05 15:05:07 +0200603
604 // App isn't attached to record yet, so match with info.
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700605 if (info.launchedActivity.info.applicationInfo == appInfo) {
Jorim Jaggi515dd682017-05-05 15:05:07 +0200606 info.bindApplicationDelayMs = calculateCurrentDelay();
607 }
608 }
609 }
610
Riddle Hsu1c5d72e2019-10-09 20:54:26 +0800611 @VisibleForTesting
612 boolean allWindowsDrawn() {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100613 for (int index = mWindowingModeTransitionInfo.size() - 1; index >= 0; index--) {
Riddle Hsu1c5d72e2019-10-09 20:54:26 +0800614 if (mWindowingModeTransitionInfo.valueAt(index).numUndrawnActivities != 0) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800615 return false;
616 }
617 }
618 return true;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500619 }
620
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800621 private boolean isAnyTransitionActive() {
Yan Wangd47f90b2019-10-03 19:17:15 -0700622 return mCurrentTransitionStartTimeNs != INVALID_START_TIME
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100623 && mWindowingModeTransitionInfo.size() > 0;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800624 }
625
Riddle Hsu1063b512019-10-22 21:12:23 +0800626 /** Aborts tracking of current launch metrics. */
627 private void abort(WindowingModeTransitionInfo info, String cause) {
628 reset(true /* abort */, info, cause, 0L /* timestampNs */);
629 }
630
Yan Wangd47f90b2019-10-03 19:17:15 -0700631 private void reset(boolean abort, WindowingModeTransitionInfo info, String cause,
Riddle Hsu1063b512019-10-22 21:12:23 +0800632 long timestampNs) {
633 final boolean isAnyTransitionActive = isAnyTransitionActive();
Yan Wangd47f90b2019-10-03 19:17:15 -0700634 if (DEBUG_METRICS) {
Riddle Hsu1063b512019-10-22 21:12:23 +0800635 Slog.i(TAG, "reset abort=" + abort + " cause=" + cause + " timestamp=" + timestampNs
636 + " active=" + isAnyTransitionActive);
Yan Wangd47f90b2019-10-03 19:17:15 -0700637 }
Riddle Hsu1063b512019-10-22 21:12:23 +0800638 if (!abort && isAnyTransitionActive) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800639 logAppTransitionMultiEvents();
640 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700641 stopLaunchTrace(info);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700642
643 // Ignore reset-after reset.
Riddle Hsu1063b512019-10-22 21:12:23 +0800644 if (isAnyTransitionActive) {
Igor Murashkin212d06c2018-10-22 16:34:39 -0700645 // LaunchObserver callbacks.
646 if (abort) {
647 launchObserverNotifyActivityLaunchCancelled(info);
648 } else {
Yan Wangd47f90b2019-10-03 19:17:15 -0700649 launchObserverNotifyActivityLaunchFinished(info, timestampNs);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700650 }
651 } else {
652 launchObserverNotifyIntentFailed();
653 }
654
Yan Wangd47f90b2019-10-03 19:17:15 -0700655 mCurrentTransitionStartTimeNs = INVALID_START_TIME;
Vishnu Nair132ee832018-09-28 15:00:05 -0700656 mCurrentTransitionDelayMs = INVALID_DELAY;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500657 mLoggedTransitionStarting = false;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100658 mWindowingModeTransitionInfo.clear();
Jorim Jaggi275561a2016-02-23 10:11:02 -0500659 }
660
661 private int calculateCurrentDelay() {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500662 // Shouldn't take more than 25 days to launch an app, so int is fine here.
Yan Wangd47f90b2019-10-03 19:17:15 -0700663 return (int) TimeUnit.NANOSECONDS
664 .toMillis(SystemClock.elapsedRealtimeNanos() - mCurrentTransitionStartTimeNs);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500665 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800666
Yan Wangd47f90b2019-10-03 19:17:15 -0700667 private int calculateDelay(long timestampNs) {
Sudheer Shankac766db02017-06-12 10:37:29 -0700668 // Shouldn't take more than 25 days to launch an app, so int is fine here.
Yan Wangd47f90b2019-10-03 19:17:15 -0700669 return (int) TimeUnit.NANOSECONDS.toMillis(timestampNs -
670 mCurrentTransitionStartTimeNs);
Sudheer Shankac766db02017-06-12 10:37:29 -0700671 }
672
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100673 private void logAppTransitionCancel(WindowingModeTransitionInfo info) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200674 final int type = getTransitionType(info);
Vishnu Nair132ee832018-09-28 15:00:05 -0700675 if (type == INVALID_TRANSITION_TYPE) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200676 return;
677 }
678 final LogMaker builder = new LogMaker(APP_TRANSITION_CANCELLED);
679 builder.setPackageName(info.launchedActivity.packageName);
680 builder.setType(type);
681 builder.addTaggedData(FIELD_CLASS_NAME, info.launchedActivity.info.name);
682 mMetricsLogger.write(builder);
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000683 StatsLog.write(
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800684 StatsLog.APP_START_CANCELED,
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700685 info.launchedActivity.info.applicationInfo.uid,
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000686 info.launchedActivity.packageName,
687 convertAppStartTransitionType(type),
688 info.launchedActivity.info.name);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700689 if (DEBUG_METRICS) {
690 Slog.i(TAG, String.format("APP_START_CANCELED(%s, %s, %s, %s)",
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700691 info.launchedActivity.info.applicationInfo.uid,
Igor Murashkin212d06c2018-10-22 16:34:39 -0700692 info.launchedActivity.packageName,
693 convertAppStartTransitionType(type),
694 info.launchedActivity.info.name));
695 }
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200696 }
697
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800698 private void logAppTransitionMultiEvents() {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200699 if (DEBUG_METRICS) Slog.i(TAG, "logging transition events");
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100700 for (int index = mWindowingModeTransitionInfo.size() - 1; index >= 0; index--) {
701 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.valueAt(index);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800702 final int type = getTransitionType(info);
Vishnu Nair132ee832018-09-28 15:00:05 -0700703 if (type == INVALID_TRANSITION_TYPE) {
Vishnu Nair194c64f2019-04-12 13:51:40 -0700704 if (DEBUG_METRICS) {
705 Slog.i(TAG, "invalid transition type"
706 + " processRunning=" + info.currentTransitionProcessRunning
707 + " startResult=" + info.startResult);
708 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800709 return;
710 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000711
712 // Take a snapshot of the transition info before sending it to the handler for logging.
713 // This will avoid any races with other operations that modify the ActivityRecord.
714 final WindowingModeTransitionInfoSnapshot infoSnapshot =
Yan Wangd47f90b2019-10-03 19:17:15 -0700715 new WindowingModeTransitionInfoSnapshot(info);
Ng Zhi An83473542018-02-20 09:02:14 -0800716 final int currentTransitionDeviceUptime = mCurrentTransitionDeviceUptime;
717 final int currentTransitionDelayMs = mCurrentTransitionDelayMs;
718 BackgroundThread.getHandler().post(() -> logAppTransition(
719 currentTransitionDeviceUptime, currentTransitionDelayMs, infoSnapshot));
Vishnu Nair132ee832018-09-28 15:00:05 -0700720 BackgroundThread.getHandler().post(() -> logAppDisplayed(infoSnapshot));
Riddle Hsuc1f8f9c2019-10-25 16:54:33 +0800721 if (info.pendingFullyDrawn != null) {
722 info.pendingFullyDrawn.run();
723 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000724
725 info.launchedActivity.info.launchToken = null;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800726 }
727 }
728
Ng Zhi An83473542018-02-20 09:02:14 -0800729 // This gets called on a background thread without holding the activity manager lock.
Calin Juravle759fbda2018-02-20 19:52:30 +0000730 private void logAppTransition(int currentTransitionDeviceUptime, int currentTransitionDelayMs,
731 WindowingModeTransitionInfoSnapshot info) {
732 final LogMaker builder = new LogMaker(APP_TRANSITION);
733 builder.setPackageName(info.packageName);
734 builder.setType(info.type);
735 builder.addTaggedData(FIELD_CLASS_NAME, info.launchedActivityName);
736 final boolean isInstantApp = info.applicationInfo.isInstantApp();
737 if (info.launchedActivityLaunchedFromPackage != null) {
738 builder.addTaggedData(APP_TRANSITION_CALLING_PACKAGE_NAME,
739 info.launchedActivityLaunchedFromPackage);
740 }
741 String launchToken = info.launchedActivityLaunchToken;
742 if (launchToken != null) {
743 builder.addTaggedData(FIELD_INSTANT_APP_LAUNCH_TOKEN, launchToken);
744 }
745 builder.addTaggedData(APP_TRANSITION_IS_EPHEMERAL, isInstantApp ? 1 : 0);
746 builder.addTaggedData(APP_TRANSITION_DEVICE_UPTIME_SECONDS,
747 currentTransitionDeviceUptime);
748 builder.addTaggedData(APP_TRANSITION_DELAY_MS, currentTransitionDelayMs);
749 builder.setSubtype(info.reason);
Vishnu Nair132ee832018-09-28 15:00:05 -0700750 if (info.startingWindowDelayMs != INVALID_DELAY) {
Calin Juravle759fbda2018-02-20 19:52:30 +0000751 builder.addTaggedData(APP_TRANSITION_STARTING_WINDOW_DELAY_MS,
752 info.startingWindowDelayMs);
753 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700754 if (info.bindApplicationDelayMs != INVALID_DELAY) {
Calin Juravle759fbda2018-02-20 19:52:30 +0000755 builder.addTaggedData(APP_TRANSITION_BIND_APPLICATION_DELAY_MS,
756 info.bindApplicationDelayMs);
757 }
758 builder.addTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS, info.windowsDrawnDelayMs);
759 final ArtManagerInternal artManagerInternal = getArtManagerInternal();
760 final PackageOptimizationInfo packageOptimizationInfo =
761 (artManagerInternal == null) || (info.launchedActivityAppRecordRequiredAbi == null)
762 ? PackageOptimizationInfo.createWithNoInfo()
763 : artManagerInternal.getPackageOptimizationInfo(
764 info.applicationInfo,
765 info.launchedActivityAppRecordRequiredAbi);
766 builder.addTaggedData(PACKAGE_OPTIMIZATION_COMPILATION_REASON,
767 packageOptimizationInfo.getCompilationReason());
768 builder.addTaggedData(PACKAGE_OPTIMIZATION_COMPILATION_FILTER,
769 packageOptimizationInfo.getCompilationFilter());
770 mMetricsLogger.write(builder);
771 StatsLog.write(
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800772 StatsLog.APP_START_OCCURRED,
Calin Juravle759fbda2018-02-20 19:52:30 +0000773 info.applicationInfo.uid,
774 info.packageName,
775 convertAppStartTransitionType(info.type),
776 info.launchedActivityName,
777 info.launchedActivityLaunchedFromPackage,
778 isInstantApp,
779 currentTransitionDeviceUptime * 1000,
780 info.reason,
781 currentTransitionDelayMs,
782 info.startingWindowDelayMs,
783 info.bindApplicationDelayMs,
784 info.windowsDrawnDelayMs,
785 launchToken,
786 packageOptimizationInfo.getCompilationReason(),
787 packageOptimizationInfo.getCompilationFilter());
Igor Murashkin212d06c2018-10-22 16:34:39 -0700788
789 if (DEBUG_METRICS) {
790 Slog.i(TAG, String.format("APP_START_OCCURRED(%s, %s, %s, %s, %s)",
791 info.applicationInfo.uid,
792 info.packageName,
793 convertAppStartTransitionType(info.type),
794 info.launchedActivityName,
795 info.launchedActivityLaunchedFromPackage));
796 }
797
798
Ng Zhi An83473542018-02-20 09:02:14 -0800799 logAppStartMemoryStateCapture(info);
Calin Juravle759fbda2018-02-20 19:52:30 +0000800 }
801
Vishnu Nair132ee832018-09-28 15:00:05 -0700802 private void logAppDisplayed(WindowingModeTransitionInfoSnapshot info) {
803 if (info.type != TYPE_TRANSITION_WARM_LAUNCH && info.type != TYPE_TRANSITION_COLD_LAUNCH) {
804 return;
805 }
806
807 EventLog.writeEvent(AM_ACTIVITY_LAUNCH_TIME,
808 info.userId, info.activityRecordIdHashCode, info.launchedActivityShortComponentName,
809 info.windowsDrawnDelayMs);
810
811 StringBuilder sb = mStringBuilder;
812 sb.setLength(0);
813 sb.append("Displayed ");
814 sb.append(info.launchedActivityShortComponentName);
815 sb.append(": ");
816 TimeUtils.formatDuration(info.windowsDrawnDelayMs, sb);
817 Log.i(TAG, sb.toString());
818 }
819
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000820 private int convertAppStartTransitionType(int tronType) {
821 if (tronType == TYPE_TRANSITION_COLD_LAUNCH) {
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800822 return StatsLog.APP_START_OCCURRED__TYPE__COLD;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000823 }
824 if (tronType == TYPE_TRANSITION_WARM_LAUNCH) {
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800825 return StatsLog.APP_START_OCCURRED__TYPE__WARM;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000826 }
827 if (tronType == TYPE_TRANSITION_HOT_LAUNCH) {
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800828 return StatsLog.APP_START_OCCURRED__TYPE__HOT;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000829 }
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800830 return StatsLog.APP_START_OCCURRED__TYPE__UNKNOWN;
Riddle Hsuc48c8912019-10-31 13:34:27 +0800831 }
832
833 /** @return the last known window drawn delay of the given windowing mode. */
834 int getLastDrawnDelayMs(@WindowingMode int windowingMode) {
835 final WindowingModeTransitionInfo info = mLastWindowingModeTransitionInfo.get(
836 windowingMode);
837 return info != null ? info.windowsDrawnDelayMs : INVALID_DELAY;
838 }
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000839
Vishnu Nair132ee832018-09-28 15:00:05 -0700840 WindowingModeTransitionInfoSnapshot logAppTransitionReportedDrawn(ActivityRecord r,
841 boolean restoredFromBundle) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100842 final WindowingModeTransitionInfo info = mLastWindowingModeTransitionInfo.get(
843 r.getWindowingMode());
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200844 if (info == null) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700845 return null;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200846 }
Riddle Hsuc1f8f9c2019-10-25 16:54:33 +0800847 if (info.numUndrawnActivities > 0 && info.pendingFullyDrawn == null) {
848 // There are still undrawn activities, postpone reporting fully drawn until all of its
849 // windows are drawn. So that is closer to an usable state.
850 info.pendingFullyDrawn = () -> {
851 logAppTransitionReportedDrawn(r, restoredFromBundle);
852 info.pendingFullyDrawn = null;
853 };
854 return null;
855 }
Chris Wailes35c193c2018-10-09 18:12:00 -0700856
857 // Record the handling of the reportFullyDrawn callback in the trace system. This is not
858 // actually used to trace this function, but instead the logical task that this function
859 // fullfils (handling reportFullyDrawn() callbacks).
860 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
861 "ActivityManager:ReportingFullyDrawn " + info.launchedActivity.packageName);
862
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200863 final LogMaker builder = new LogMaker(APP_TRANSITION_REPORTED_DRAWN);
864 builder.setPackageName(r.packageName);
865 builder.addTaggedData(FIELD_CLASS_NAME, r.info.name);
Riddle Hsuc1f8f9c2019-10-25 16:54:33 +0800866 final long currentTimestampNs = SystemClock.elapsedRealtimeNanos();
867 final long startupTimeMs = info.pendingFullyDrawn != null
868 ? info.windowsDrawnDelayMs
869 : TimeUnit.NANOSECONDS.toMillis(currentTimestampNs - mLastTransitionStartTimeNs);
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000870 builder.addTaggedData(APP_TRANSITION_REPORTED_DRAWN_MS, startupTimeMs);
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200871 builder.setType(restoredFromBundle
872 ? TYPE_TRANSITION_REPORTED_DRAWN_WITH_BUNDLE
873 : TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE);
874 builder.addTaggedData(APP_TRANSITION_PROCESS_RUNNING,
875 info.currentTransitionProcessRunning ? 1 : 0);
876 mMetricsLogger.write(builder);
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000877 StatsLog.write(
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800878 StatsLog.APP_START_FULLY_DRAWN,
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700879 info.launchedActivity.info.applicationInfo.uid,
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000880 info.launchedActivity.packageName,
881 restoredFromBundle
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800882 ? StatsLog.APP_START_FULLY_DRAWN__TYPE__WITH_BUNDLE
883 : StatsLog.APP_START_FULLY_DRAWN__TYPE__WITHOUT_BUNDLE,
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000884 info.launchedActivity.info.name,
885 info.currentTransitionProcessRunning,
886 startupTimeMs);
Chris Wailes35c193c2018-10-09 18:12:00 -0700887
888 // Ends the trace started at the beginning of this function. This is located here to allow
889 // the trace slice to have a noticable duration.
890 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
891
Vishnu Nair132ee832018-09-28 15:00:05 -0700892 final WindowingModeTransitionInfoSnapshot infoSnapshot =
893 new WindowingModeTransitionInfoSnapshot(info, r, (int) startupTimeMs);
894 BackgroundThread.getHandler().post(() -> logAppFullyDrawn(infoSnapshot));
Yan Wang9eac5ec2019-09-30 15:42:18 -0700895
896 // Notify reportFullyDrawn event.
897 launchObserverNotifyReportFullyDrawn(r, currentTimestampNs);
898
Vishnu Nair132ee832018-09-28 15:00:05 -0700899 return infoSnapshot;
900 }
901
902 private void logAppFullyDrawn(WindowingModeTransitionInfoSnapshot info) {
903 if (info.type != TYPE_TRANSITION_WARM_LAUNCH && info.type != TYPE_TRANSITION_COLD_LAUNCH) {
904 return;
905 }
906
907 StringBuilder sb = mStringBuilder;
908 sb.setLength(0);
909 sb.append("Fully drawn ");
910 sb.append(info.launchedActivityShortComponentName);
911 sb.append(": ");
912 TimeUtils.formatDuration(info.windowsFullyDrawnDelayMs, sb);
913 Log.i(TAG, sb.toString());
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200914 }
915
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000916 void logAbortedBgActivityStart(Intent intent, WindowProcessController callerApp,
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100917 int callingUid, String callingPackage, int callingUidProcState,
918 boolean callingUidHasAnyVisibleWindow,
919 int realCallingUid, int realCallingUidProcState,
920 boolean realCallingUidHasAnyVisibleWindow,
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100921 boolean comingFromPendingIntent) {
922
923 final long nowElapsed = SystemClock.elapsedRealtime();
924 final long nowUptime = SystemClock.uptimeMillis();
925 final LogMaker builder = new LogMaker(ACTION_ACTIVITY_START);
926 builder.setTimestamp(System.currentTimeMillis());
927 builder.addTaggedData(FIELD_CALLING_UID, callingUid);
928 builder.addTaggedData(FIELD_CALLING_PACKAGE_NAME, callingPackage);
929 builder.addTaggedData(FIELD_CALLING_UID_PROC_STATE,
930 processStateAmToProto(callingUidProcState));
931 builder.addTaggedData(FIELD_CALLING_UID_HAS_ANY_VISIBLE_WINDOW,
932 callingUidHasAnyVisibleWindow ? 1 : 0);
933 builder.addTaggedData(FIELD_REAL_CALLING_UID, realCallingUid);
934 builder.addTaggedData(FIELD_REAL_CALLING_UID_PROC_STATE,
935 processStateAmToProto(realCallingUidProcState));
936 builder.addTaggedData(FIELD_REAL_CALLING_UID_HAS_ANY_VISIBLE_WINDOW,
937 realCallingUidHasAnyVisibleWindow ? 1 : 0);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100938 builder.addTaggedData(FIELD_COMING_FROM_PENDING_INTENT, comingFromPendingIntent ? 1 : 0);
Michal Karpinski8596ded2018-11-14 14:43:48 +0000939 if (intent != null) {
940 builder.addTaggedData(FIELD_INTENT_ACTION, intent.getAction());
Michal Karpinski319069b2019-03-27 15:46:50 +0000941 ComponentName component = intent.getComponent();
942 if (component != null) {
943 builder.addTaggedData(FIELD_TARGET_SHORT_COMPONENT_NAME,
944 component.flattenToShortString());
945 }
Michal Karpinski8596ded2018-11-14 14:43:48 +0000946 }
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100947 if (callerApp != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700948 builder.addTaggedData(FIELD_PROCESS_RECORD_PROCESS_NAME, callerApp.mName);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100949 builder.addTaggedData(FIELD_PROCESS_RECORD_CUR_PROC_STATE,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700950 processStateAmToProto(callerApp.getCurrentProcState()));
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100951 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_CLIENT_ACTIVITIES,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700952 callerApp.hasClientActivities() ? 1 : 0);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100953 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_FOREGROUND_SERVICES,
954 callerApp.hasForegroundServices() ? 1 : 0);
955 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_FOREGROUND_ACTIVITIES,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700956 callerApp.hasForegroundActivities() ? 1 : 0);
957 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_TOP_UI, callerApp.hasTopUi() ? 1 : 0);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100958 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_OVERLAY_UI,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700959 callerApp.hasOverlayUi() ? 1 : 0);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100960 builder.addTaggedData(FIELD_PROCESS_RECORD_PENDING_UI_CLEAN,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700961 callerApp.hasPendingUiClean() ? 1 : 0);
962 if (callerApp.getInteractionEventTime() != 0) {
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100963 builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_LAST_INTERACTION_EVENT,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700964 (nowElapsed - callerApp.getInteractionEventTime()));
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100965 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700966 if (callerApp.getFgInteractionTime() != 0) {
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100967 builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_FG_INTERACTION,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700968 (nowElapsed - callerApp.getFgInteractionTime()));
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100969 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700970 if (callerApp.getWhenUnimportant() != 0) {
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100971 builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_UNIMPORTANT,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700972 (nowUptime - callerApp.getWhenUnimportant()));
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100973 }
974 }
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100975 mMetricsLogger.write(builder);
976 }
977
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100978 private int getTransitionType(WindowingModeTransitionInfo info) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800979 if (info.currentTransitionProcessRunning) {
980 if (info.startResult == START_SUCCESS) {
981 return TYPE_TRANSITION_WARM_LAUNCH;
982 } else if (info.startResult == START_TASK_TO_FRONT) {
983 return TYPE_TRANSITION_HOT_LAUNCH;
984 }
Vishnu Nair194c64f2019-04-12 13:51:40 -0700985 } else if (info.startResult == START_SUCCESS
986 || (info.startResult == START_TASK_TO_FRONT)) {
987 // TaskRecord may still exist when cold launching an activity and the start
988 // result will be set to START_TASK_TO_FRONT. Treat this as a COLD launch.
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800989 return TYPE_TRANSITION_COLD_LAUNCH;
990 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700991 return INVALID_TRANSITION_TYPE;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800992 }
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800993
Ng Zhi An83473542018-02-20 09:02:14 -0800994 private void logAppStartMemoryStateCapture(WindowingModeTransitionInfoSnapshot info) {
995 if (info.processRecord == null) {
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800996 if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture processRecord null");
997 return;
998 }
999
Wale Ogunwalef6733932018-06-27 05:14:34 -07001000 final int pid = info.processRecord.getPid();
Ng Zhi An83473542018-02-20 09:02:14 -08001001 final int uid = info.applicationInfo.uid;
Rajeev Kumarbfcd9202018-03-02 22:42:13 +00001002 final MemoryStat memoryStat = readMemoryStatFromFilesystem(uid, pid);
Ng Zhi Anbbefdec2018-01-30 17:12:39 -08001003 if (memoryStat == null) {
1004 if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture memoryStat null");
1005 return;
1006 }
1007
1008 StatsLog.write(
1009 StatsLog.APP_START_MEMORY_STATE_CAPTURED,
1010 uid,
Ng Zhi An83473542018-02-20 09:02:14 -08001011 info.processName,
1012 info.launchedActivityName,
Ng Zhi Anbbefdec2018-01-30 17:12:39 -08001013 memoryStat.pgfault,
1014 memoryStat.pgmajfault,
1015 memoryStat.rssInBytes,
1016 memoryStat.cacheInBytes,
1017 memoryStat.swapInBytes);
1018 }
1019
Wale Ogunwalef6733932018-06-27 05:14:34 -07001020 private WindowProcessController findProcessForActivity(ActivityRecord launchedActivity) {
Ng Zhi Anbbefdec2018-01-30 17:12:39 -08001021 return launchedActivity != null
Wale Ogunwalef6733932018-06-27 05:14:34 -07001022 ? mSupervisor.mService.mProcessNames.get(
Andrii Kulianeceebbf2019-06-26 17:36:51 -07001023 launchedActivity.processName, launchedActivity.info.applicationInfo.uid)
Ng Zhi Anbbefdec2018-01-30 17:12:39 -08001024 : null;
1025 }
Calin Juravle759fbda2018-02-20 19:52:30 +00001026
1027 private ArtManagerInternal getArtManagerInternal() {
1028 if (mArtManagerInternal == null) {
1029 // Note that this may be null.
1030 // ArtManagerInternal is registered during PackageManagerService
1031 // initialization which happens after ActivityManagerService.
1032 mArtManagerInternal = LocalServices.getService(ArtManagerInternal.class);
1033 }
1034 return mArtManagerInternal;
1035 }
Vishnu Nair132ee832018-09-28 15:00:05 -07001036
1037 /**
Chris Wailes35c193c2018-10-09 18:12:00 -07001038 * Starts traces for app launch.
Vishnu Nair132ee832018-09-28 15:00:05 -07001039 *
1040 * @param info
1041 * */
1042 private void startTraces(WindowingModeTransitionInfo info) {
Ioannis Ilkos66e1cd82019-05-03 10:47:30 +01001043 if (!Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER) || info == null
1044 || info.launchTraceActive) {
Vishnu Nair132ee832018-09-28 15:00:05 -07001045 return;
1046 }
Ioannis Ilkos66e1cd82019-05-03 10:47:30 +01001047 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: "
1048 + info.launchedActivity.packageName, 0);
1049 info.launchTraceActive = true;
Vishnu Nair132ee832018-09-28 15:00:05 -07001050 }
1051
1052 private void stopLaunchTrace(WindowingModeTransitionInfo info) {
1053 if (info == null) {
1054 return;
1055 }
1056 if (info.launchTraceActive) {
1057 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: "
1058 + info.launchedActivity.packageName, 0);
1059 info.launchTraceActive = false;
1060 }
1061 }
Igor Murashkin212d06c2018-10-22 16:34:39 -07001062
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001063 public ActivityMetricsLaunchObserverRegistry getLaunchObserverRegistry() {
1064 return mLaunchObserver;
1065 }
1066
Igor Murashkin212d06c2018-10-22 16:34:39 -07001067 /** Notify the {@link ActivityMetricsLaunchObserver} that a new launch sequence has begun. */
Yan Wangd47f90b2019-10-03 19:17:15 -07001068 private void launchObserverNotifyIntentStarted(Intent intent, long timestampNs) {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001069 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1070 "MetricsLogger:launchObserverNotifyIntentStarted");
1071
1072 // Beginning a launch is timing sensitive and so should be observed as soon as possible.
Yan Wangd47f90b2019-10-03 19:17:15 -07001073 mLaunchObserver.onIntentStarted(intent, timestampNs);
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001074
1075 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Igor Murashkin212d06c2018-10-22 16:34:39 -07001076 }
1077
1078 /**
1079 * Notify the {@link ActivityMetricsLaunchObserver} that the previous launch sequence has
1080 * aborted due to intent failure (e.g. intent resolve failed or security error, etc) or
1081 * intent being delivered to the top running activity.
1082 */
1083 private void launchObserverNotifyIntentFailed() {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001084 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1085 "MetricsLogger:launchObserverNotifyIntentFailed");
1086
1087 mLaunchObserver.onIntentFailed();
1088
1089 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Igor Murashkin212d06c2018-10-22 16:34:39 -07001090 }
1091
1092 /**
1093 * Notify the {@link ActivityMetricsLaunchObserver} that the current launch sequence's activity
1094 * has started.
1095 */
1096 private void launchObserverNotifyActivityLaunched(WindowingModeTransitionInfo info) {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001097 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1098 "MetricsLogger:launchObserverNotifyActivityLaunched");
1099
Igor Murashkin212d06c2018-10-22 16:34:39 -07001100 @ActivityMetricsLaunchObserver.Temperature int temperature =
1101 convertTransitionTypeToLaunchObserverTemperature(getTransitionType(info));
1102
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001103 // Beginning a launch is timing sensitive and so should be observed as soon as possible.
1104 mLaunchObserver.onActivityLaunched(convertActivityRecordToProto(info.launchedActivity),
1105 temperature);
1106
1107 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Igor Murashkin212d06c2018-10-22 16:34:39 -07001108 }
1109
1110 /**
Yan Wang9eac5ec2019-09-30 15:42:18 -07001111 * Notifies the {@link ActivityMetricsLaunchObserver} the reportFullDrawn event.
1112 */
Yan Wangd47f90b2019-10-03 19:17:15 -07001113 private void launchObserverNotifyReportFullyDrawn(ActivityRecord r, long timestampNs) {
Yan Wang9eac5ec2019-09-30 15:42:18 -07001114 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1115 "MetricsLogger:launchObserverNotifyReportFullyDrawn");
Yan Wangd47f90b2019-10-03 19:17:15 -07001116 mLaunchObserver.onReportFullyDrawn(convertActivityRecordToProto(r), timestampNs);
Yan Wang9eac5ec2019-09-30 15:42:18 -07001117 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
1118 }
1119
1120 /**
Igor Murashkin212d06c2018-10-22 16:34:39 -07001121 * Notify the {@link ActivityMetricsLaunchObserver} that the current launch sequence is
1122 * cancelled.
1123 */
1124 private void launchObserverNotifyActivityLaunchCancelled(WindowingModeTransitionInfo info) {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001125 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1126 "MetricsLogger:launchObserverNotifyActivityLaunchCancelled");
Igor Murashkin212d06c2018-10-22 16:34:39 -07001127
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001128 final @ActivityMetricsLaunchObserver.ActivityRecordProto byte[] activityRecordProto =
1129 info != null ? convertActivityRecordToProto(info.launchedActivity) : null;
1130
1131 mLaunchObserver.onActivityLaunchCancelled(activityRecordProto);
1132
1133 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Igor Murashkin212d06c2018-10-22 16:34:39 -07001134 }
1135
1136 /**
1137 * Notify the {@link ActivityMetricsLaunchObserver} that the current launch sequence's activity
1138 * has fully finished (successfully).
1139 */
Yan Wangd47f90b2019-10-03 19:17:15 -07001140 private void launchObserverNotifyActivityLaunchFinished(WindowingModeTransitionInfo info,
1141 long timestampNs) {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001142 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1143 "MetricsLogger:launchObserverNotifyActivityLaunchFinished");
Igor Murashkin212d06c2018-10-22 16:34:39 -07001144
Yan Wangd47f90b2019-10-03 19:17:15 -07001145 mLaunchObserver
1146 .onActivityLaunchFinished(convertActivityRecordToProto(info.launchedActivity),
1147 timestampNs);
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001148
1149 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
1150 }
1151
1152 @VisibleForTesting
1153 static @ActivityMetricsLaunchObserver.ActivityRecordProto byte[]
1154 convertActivityRecordToProto(ActivityRecord record) {
1155 // May take non-negligible amount of time to convert ActivityRecord into a proto,
1156 // so track the time.
1157 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1158 "MetricsLogger:convertActivityRecordToProto");
1159
1160 // There does not appear to be a way to 'reset' a ProtoOutputBuffer stream,
1161 // so create a new one every time.
1162 final ProtoOutputStream protoOutputStream =
1163 new ProtoOutputStream(LAUNCH_OBSERVER_ACTIVITY_RECORD_PROTO_CHUNK_SIZE);
1164 // Write this data out as the top-most ActivityRecordProto (i.e. it is not a sub-object).
1165 record.writeToProto(protoOutputStream);
1166 final byte[] bytes = protoOutputStream.getBytes();
1167
1168 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
1169
1170 return bytes;
Igor Murashkin212d06c2018-10-22 16:34:39 -07001171 }
1172
1173 private static @ActivityMetricsLaunchObserver.Temperature int
1174 convertTransitionTypeToLaunchObserverTemperature(int transitionType) {
1175 switch (transitionType) {
1176 case TYPE_TRANSITION_WARM_LAUNCH:
1177 return ActivityMetricsLaunchObserver.TEMPERATURE_WARM;
1178 case TYPE_TRANSITION_HOT_LAUNCH:
1179 return ActivityMetricsLaunchObserver.TEMPERATURE_HOT;
1180 case TYPE_TRANSITION_COLD_LAUNCH:
1181 return ActivityMetricsLaunchObserver.TEMPERATURE_COLD;
1182 default:
1183 return -1;
1184 }
1185 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -08001186}