blob: e6c6b12e18c64bee1056ff86f85fd74c1c900933 [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;
190 private boolean loggedWindowsDrawn;
191 private boolean loggedStartingWindowDrawn;
Vishnu Nair132ee832018-09-28 15:00:05 -0700192 private boolean launchTraceActive;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800193 }
194
Vishnu Nair132ee832018-09-28 15:00:05 -0700195 final class WindowingModeTransitionInfoSnapshot {
Calin Juravle759fbda2018-02-20 19:52:30 +0000196 final private ApplicationInfo applicationInfo;
Wale Ogunwalef6733932018-06-27 05:14:34 -0700197 final private WindowProcessController processRecord;
Vishnu Nair132ee832018-09-28 15:00:05 -0700198 final String packageName;
199 final String launchedActivityName;
Calin Juravle759fbda2018-02-20 19:52:30 +0000200 final private String launchedActivityLaunchedFromPackage;
201 final private String launchedActivityLaunchToken;
202 final private String launchedActivityAppRecordRequiredAbi;
Vishnu Nair132ee832018-09-28 15:00:05 -0700203 final String launchedActivityShortComponentName;
Ng Zhi An83473542018-02-20 09:02:14 -0800204 final private String processName;
Calin Juravle759fbda2018-02-20 19:52:30 +0000205 final private int reason;
206 final private int startingWindowDelayMs;
207 final private int bindApplicationDelayMs;
Vishnu Nair132ee832018-09-28 15:00:05 -0700208 final int windowsDrawnDelayMs;
209 final int type;
210 final int userId;
211 /**
212 * Elapsed time from when we launch an activity to when the app reported it was
213 * fully drawn. If this is not reported then the value is set to INVALID_DELAY.
214 */
215 final int windowsFullyDrawnDelayMs;
216 final int activityRecordIdHashCode;
Calin Juravle759fbda2018-02-20 19:52:30 +0000217
218 private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700219 this(info, info.launchedActivity);
220 }
221
222 private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info,
223 ActivityRecord launchedActivity) {
224 this(info, launchedActivity, INVALID_DELAY);
225 }
226
227 private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info,
228 ActivityRecord launchedActivity, int windowsFullyDrawnDelayMs) {
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700229 applicationInfo = launchedActivity.info.applicationInfo;
Vishnu Nair132ee832018-09-28 15:00:05 -0700230 packageName = launchedActivity.packageName;
231 launchedActivityName = launchedActivity.info.name;
232 launchedActivityLaunchedFromPackage = launchedActivity.launchedFromPackage;
233 launchedActivityLaunchToken = launchedActivity.info.launchToken;
234 launchedActivityAppRecordRequiredAbi = launchedActivity.app == null
Calin Juravle759fbda2018-02-20 19:52:30 +0000235 ? null
Vishnu Nairaf0ea312018-10-15 16:23:55 -0700236 : launchedActivity.app.getRequiredAbi();
Calin Juravle759fbda2018-02-20 19:52:30 +0000237 reason = info.reason;
238 startingWindowDelayMs = info.startingWindowDelayMs;
239 bindApplicationDelayMs = info.bindApplicationDelayMs;
240 windowsDrawnDelayMs = info.windowsDrawnDelayMs;
241 type = getTransitionType(info);
Vishnu Nairaf0ea312018-10-15 16:23:55 -0700242 processRecord = findProcessForActivity(launchedActivity);
243 processName = launchedActivity.processName;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800244 userId = launchedActivity.mUserId;
Vishnu Nair132ee832018-09-28 15:00:05 -0700245 launchedActivityShortComponentName = launchedActivity.shortComponentName;
246 activityRecordIdHashCode = System.identityHashCode(launchedActivity);
247 this.windowsFullyDrawnDelayMs = windowsFullyDrawnDelayMs;
Calin Juravle759fbda2018-02-20 19:52:30 +0000248 }
Vishnu Nairbb9ab4b2018-12-13 10:29:46 -0800249
250 @WaitResult.LaunchState int getLaunchState() {
251 switch (type) {
252 case TYPE_TRANSITION_WARM_LAUNCH:
253 return LAUNCH_STATE_WARM;
254 case TYPE_TRANSITION_HOT_LAUNCH:
255 return LAUNCH_STATE_HOT;
256 case TYPE_TRANSITION_COLD_LAUNCH:
257 return LAUNCH_STATE_COLD;
258 default:
259 return -1;
260 }
261 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000262 }
263
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200264 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context, Looper looper) {
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800265 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
266 mSupervisor = supervisor;
267 mContext = context;
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200268 mHandler = new H(looper);
Igor Murashkinc0b47e42018-11-07 15:54:18 -0800269 mLaunchObserver = new LaunchObserverRegistryImpl(looper);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800270 }
271
272 void logWindowState() {
273 final long now = SystemClock.elapsedRealtime() / 1000;
274 if (mWindowState != WINDOW_STATE_INVALID) {
275 // We log even if the window state hasn't changed, because the user might remain in
276 // home/fullscreen move forever and we would like to track this kind of behavior
277 // too.
278 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
279 (int) (now - mLastLogTimeSecs));
280 }
281 mLastLogTimeSecs = now;
282
Wale Ogunwale926aade2017-08-29 11:24:37 -0700283 mWindowState = WINDOW_STATE_INVALID;
Wale Ogunwaled32da472018-11-16 07:19:28 -0800284 ActivityStack stack =
285 mSupervisor.mRootActivityContainer.getTopDisplayFocusedStack();
lumarkf6c4a982018-06-15 15:43:12 +0800286 if (stack == null) {
287 return;
288 }
289
Wale Ogunwale926aade2017-08-29 11:24:37 -0700290 if (stack.isActivityTypeAssistant()) {
291 mWindowState = WINDOW_STATE_ASSISTANT;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800292 return;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800293 }
Wale Ogunwale926aade2017-08-29 11:24:37 -0700294
Igor Murashkin212d06c2018-10-22 16:34:39 -0700295 @WindowingMode int windowingMode = stack.getWindowingMode();
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700296 if (windowingMode == WINDOWING_MODE_PINNED) {
Wale Ogunwaled32da472018-11-16 07:19:28 -0800297 stack = mSupervisor.mRootActivityContainer.findStackBehind(stack);
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700298 windowingMode = stack.getWindowingMode();
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800299 }
Wale Ogunwale926aade2017-08-29 11:24:37 -0700300 switch (windowingMode) {
301 case WINDOWING_MODE_FULLSCREEN:
302 mWindowState = WINDOW_STATE_STANDARD;
303 break;
304 case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY:
305 case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY:
306 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
307 break;
Bryce Lee6c605092017-10-12 11:14:49 -0700308 case WINDOWING_MODE_FREEFORM:
Wale Ogunwale926aade2017-08-29 11:24:37 -0700309 mWindowState = WINDOW_STATE_FREEFORM;
310 break;
311 default:
312 if (windowingMode != WINDOWING_MODE_UNDEFINED) {
313 throw new IllegalStateException("Unknown windowing mode for stack=" + stack
314 + " windowingMode=" + windowingMode);
315 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800316 }
317 }
Jorim Jaggi275561a2016-02-23 10:11:02 -0500318
319 /**
320 * Notifies the tracker at the earliest possible point when we are starting to launch an
321 * activity.
322 */
Igor Murashkin212d06c2018-10-22 16:34:39 -0700323 void notifyActivityLaunching(Intent intent) {
324 if (DEBUG_METRICS) {
325 Slog.i(TAG, String.format("notifyActivityLaunching: active:%b, intent:%s",
326 isAnyTransitionActive(),
327 intent));
328 }
329
Yan Wangd47f90b2019-10-03 19:17:15 -0700330 if (mCurrentTransitionStartTimeNs == INVALID_START_TIME) {
Igor Murashkin212d06c2018-10-22 16:34:39 -0700331
Yan Wangd47f90b2019-10-03 19:17:15 -0700332 mCurrentTransitionStartTimeNs = SystemClock.elapsedRealtimeNanos();
333 mLastTransitionStartTimeNs = mCurrentTransitionStartTimeNs;
Igor Murashkin212d06c2018-10-22 16:34:39 -0700334
Yan Wangd47f90b2019-10-03 19:17:15 -0700335 launchObserverNotifyIntentStarted(intent, mCurrentTransitionStartTimeNs);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800336 }
Jorim Jaggi275561a2016-02-23 10:11:02 -0500337 }
338
339 /**
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700340 * Notifies the tracker that the activity is actually launching.
341 *
342 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
343 * launch
344 * @param launchedActivity the activity that is being launched
345 */
346 void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity) {
Wale Ogunwalef6733932018-06-27 05:14:34 -0700347 final WindowProcessController processRecord = findProcessForActivity(launchedActivity);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700348 final boolean processRunning = processRecord != null;
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700349
350 // We consider this a "process switch" if the process of the activity that gets launched
351 // didn't have an activity that was in started state. In this case, we assume that lot
352 // of caches might be purged so the time until it produces the first frame is very
353 // interesting.
354 final boolean processSwitch = processRecord == null
Wale Ogunwalef6733932018-06-27 05:14:34 -0700355 || !processRecord.hasStartedActivity(launchedActivity);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700356
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800357 notifyActivityLaunched(resultCode, launchedActivity, processRunning, processSwitch);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700358 }
359
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700360 /**
Jorim Jaggi275561a2016-02-23 10:11:02 -0500361 * Notifies the tracker the the activity is actually launching.
362 *
363 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
364 * launch
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800365 * @param launchedActivity the activity being launched
Jorim Jaggi275561a2016-02-23 10:11:02 -0500366 * @param processRunning whether the process that will contains the activity is already running
Jorim Jaggibe67c902016-04-12 00:53:16 -0700367 * @param processSwitch whether the process that will contain the activity didn't have any
368 * activity that was stopped, i.e. the started activity is "switching"
369 * processes
Jorim Jaggi275561a2016-02-23 10:11:02 -0500370 */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800371 private void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity,
Jorim Jaggibe67c902016-04-12 00:53:16 -0700372 boolean processRunning, boolean processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500373
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200374 if (DEBUG_METRICS) Slog.i(TAG, "notifyActivityLaunched"
375 + " resultCode=" + resultCode
376 + " launchedActivity=" + launchedActivity
377 + " processRunning=" + processRunning
378 + " processSwitch=" + processSwitch);
379
Igor Murashkin212d06c2018-10-22 16:34:39 -0700380 // If we are already in an existing transition, only update the activity name, but not the
381 // other attributes.
382 final @WindowingMode int windowingMode = launchedActivity != null
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100383 ? launchedActivity.getWindowingMode()
384 : WINDOWING_MODE_UNDEFINED;
Vishnu Nairf8accc52018-10-11 10:19:54 -0700385 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode);
Yan Wangd47f90b2019-10-03 19:17:15 -0700386 if (mCurrentTransitionStartTimeNs == INVALID_START_TIME) {
Vishnu Nairf8accc52018-10-11 10:19:54 -0700387 // No transition is active ignore this launch.
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200388 return;
389 }
390
Vishnu Nair9ba31652018-11-13 14:34:05 -0800391 if (launchedActivity != null && launchedActivity.mDrawn) {
Vishnu Nairf8accc52018-10-11 10:19:54 -0700392 // Launched activity is already visible. We cannot measure windows drawn delay.
Yan Wangd47f90b2019-10-03 19:17:15 -0700393 reset(true /* abort */, info, "launched activity already visible",
394 0L /* timestampNs */);
Vishnu Nairf8accc52018-10-11 10:19:54 -0700395 return;
396 }
397
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800398 if (launchedActivity != null && info != null) {
Vishnu Nairf8accc52018-10-11 10:19:54 -0700399 // If we are already in an existing transition, only update the activity name, but not
400 // the other attributes.
Igor Murashkin212d06c2018-10-22 16:34:39 -0700401
402 // Coalesce multiple (trampoline) activities from a single sequence together.
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800403 info.launchedActivity = launchedActivity;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500404 return;
405 }
406
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100407 final boolean otherWindowModesLaunching =
408 mWindowingModeTransitionInfo.size() > 0 && info == null;
Jorim Jaggi54cff642018-03-15 15:51:32 +0100409 if ((!isLoggableResultCode(resultCode) || launchedActivity == null || !processSwitch
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100410 || windowingMode == WINDOWING_MODE_UNDEFINED) && !otherWindowModesLaunching) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800411 // Failed to launch or it was not a process switch, so we don't care about the timing.
Yan Wangd47f90b2019-10-03 19:17:15 -0700412 reset(true /* abort */, info, "failed to launch or not a process switch",
413 0L /* timestampNs */);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800414 return;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100415 } else if (otherWindowModesLaunching) {
416 // Don't log this windowing mode but continue with the other windowing modes.
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800417 return;
418 }
419
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200420 if (DEBUG_METRICS) Slog.i(TAG, "notifyActivityLaunched successful");
421
Igor Murashkin212d06c2018-10-22 16:34:39 -0700422 // A new launch sequence [with the windowingMode] has begun.
423 // Start tracking it.
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100424 final WindowingModeTransitionInfo newInfo = new WindowingModeTransitionInfo();
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800425 newInfo.launchedActivity = launchedActivity;
426 newInfo.currentTransitionProcessRunning = processRunning;
427 newInfo.startResult = resultCode;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100428 mWindowingModeTransitionInfo.put(windowingMode, newInfo);
429 mLastWindowingModeTransitionInfo.put(windowingMode, newInfo);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800430 mCurrentTransitionDeviceUptime = (int) (SystemClock.uptimeMillis() / 1000);
Vishnu Nair132ee832018-09-28 15:00:05 -0700431 startTraces(newInfo);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700432 launchObserverNotifyActivityLaunched(newInfo);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500433 }
434
435 /**
Jorim Jaggi54cff642018-03-15 15:51:32 +0100436 * @return True if we should start logging an event for an activity start that returned
437 * {@code resultCode} and that we'll indeed get a windows drawn event.
438 */
439 private boolean isLoggableResultCode(int resultCode) {
440 return resultCode == START_SUCCESS || resultCode == START_TASK_TO_FRONT;
441 }
442
443 /**
Jorim Jaggi275561a2016-02-23 10:11:02 -0500444 * Notifies the tracker that all windows of the app have been drawn.
445 */
Igor Murashkin212d06c2018-10-22 16:34:39 -0700446 WindowingModeTransitionInfoSnapshot notifyWindowsDrawn(@WindowingMode int windowingMode,
Yan Wangd47f90b2019-10-03 19:17:15 -0700447 long timestampNs) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100448 if (DEBUG_METRICS) Slog.i(TAG, "notifyWindowsDrawn windowingMode=" + windowingMode);
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200449
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100450 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800451 if (info == null || info.loggedWindowsDrawn) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700452 return null;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500453 }
Yan Wangd47f90b2019-10-03 19:17:15 -0700454 info.windowsDrawnDelayMs = calculateDelay(timestampNs);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800455 info.loggedWindowsDrawn = true;
Vishnu Nair132ee832018-09-28 15:00:05 -0700456 final WindowingModeTransitionInfoSnapshot infoSnapshot =
457 new WindowingModeTransitionInfoSnapshot(info);
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100458 if (allWindowsDrawn() && mLoggedTransitionStarting) {
Yan Wangd47f90b2019-10-03 19:17:15 -0700459 reset(false /* abort */, info, "notifyWindowsDrawn - all windows drawn",
460 timestampNs /* timestampNs */);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500461 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700462 return infoSnapshot;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500463 }
464
465 /**
466 * Notifies the tracker that the starting window was drawn.
467 */
Igor Murashkin212d06c2018-10-22 16:34:39 -0700468 void notifyStartingWindowDrawn(@WindowingMode int windowingMode, long timestamp) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100469 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800470 if (info == null || info.loggedStartingWindowDrawn) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500471 return;
472 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800473 info.loggedStartingWindowDrawn = true;
Sudheer Shankac766db02017-06-12 10:37:29 -0700474 info.startingWindowDelayMs = calculateDelay(timestamp);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500475 }
476
477 /**
478 * Notifies the tracker that the app transition is starting.
479 *
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100480 * @param windowingModeToReason A map from windowing mode to a reason integer, which must be on
Vishnu Nair132ee832018-09-28 15:00:05 -0700481 * of ActivityTaskManagerInternal.APP_TRANSITION_* reasons.
Jorim Jaggi275561a2016-02-23 10:11:02 -0500482 */
Yan Wangd47f90b2019-10-03 19:17:15 -0700483 void notifyTransitionStarting(SparseIntArray windowingModeToReason, long timestampNs) {
Jorim Jaggid8a57772017-04-14 16:50:42 -0700484 if (!isAnyTransitionActive() || mLoggedTransitionStarting) {
Igor Murashkin212d06c2018-10-22 16:34:39 -0700485 // Ignore calls to this made after a reset and prior to notifyActivityLaunching.
486
487 // Ignore any subsequent notifyTransitionStarting until the next reset.
Jorim Jaggi275561a2016-02-23 10:11:02 -0500488 return;
489 }
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200490 if (DEBUG_METRICS) Slog.i(TAG, "notifyTransitionStarting");
Yan Wangd47f90b2019-10-03 19:17:15 -0700491 mCurrentTransitionDelayMs = calculateDelay(timestampNs);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500492 mLoggedTransitionStarting = true;
Igor Murashkin212d06c2018-10-22 16:34:39 -0700493
494 WindowingModeTransitionInfo foundInfo = null;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100495 for (int index = windowingModeToReason.size() - 1; index >= 0; index--) {
Igor Murashkin212d06c2018-10-22 16:34:39 -0700496 final @WindowingMode int windowingMode = windowingModeToReason.keyAt(index);
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100497 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(
498 windowingMode);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800499 if (info == null) {
500 continue;
501 }
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100502 info.reason = windowingModeToReason.valueAt(index);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700503 foundInfo = info;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800504 }
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100505 if (allWindowsDrawn()) {
Vishnu Naira62534b2018-11-09 09:13:22 -0800506 // abort metrics collection if we cannot find a matching transition.
507 final boolean abortMetrics = foundInfo == null;
Yan Wangd47f90b2019-10-03 19:17:15 -0700508 reset(abortMetrics, foundInfo, "notifyTransitionStarting - all windows drawn",
509 timestampNs /* timestampNs */);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500510 }
511 }
512
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200513 /**
514 * Notifies the tracker that the visibility of an app is changing.
515 *
516 * @param activityRecord the app that is changing its visibility
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200517 */
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200518 void notifyVisibilityChanged(ActivityRecord activityRecord) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100519 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(
520 activityRecord.getWindowingMode());
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200521 if (info == null) {
522 return;
523 }
524 if (info.launchedActivity != activityRecord) {
525 return;
526 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800527 final TaskRecord t = activityRecord.getTaskRecord();
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200528 final SomeArgs args = SomeArgs.obtain();
529 args.arg1 = t;
530 args.arg2 = activityRecord;
531 mHandler.obtainMessage(MSG_CHECK_VISIBILITY, args).sendToTarget();
532 }
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200533
Vishnu Nairb1de42d2019-01-28 09:49:51 -0800534 private boolean hasVisibleNonFinishingActivity(TaskRecord t) {
Wale Ogunwale1a06f152019-10-11 11:26:30 +0200535 for (int i = t.getChildCount() - 1; i >= 0; --i) {
536 final ActivityRecord r = t.getChildAt(i);
Vishnu Nairb1de42d2019-01-28 09:49:51 -0800537 if (r.visible && !r.finishing) {
538 return true;
539 }
540 }
541 return false;
542 }
543
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200544 private void checkVisibility(TaskRecord t, ActivityRecord r) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700545 synchronized (mSupervisor.mService.mGlobalLock) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200546
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100547 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(
548 r.getWindowingMode());
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200549
550 // If we have an active transition that's waiting on a certain activity that will be
551 // invisible now, we'll never get onWindowsDrawn, so abort the transition if necessary.
Vishnu Naird15063b2019-06-11 08:08:25 -0700552
553 // We have no active transitions.
554 if (info == null) {
555 return;
556 }
557
558 // The notified activity whose visibility changed is no longer the launched activity.
559 // We can still wait to get onWindowsDrawn.
560 if (info.launchedActivity != r) {
561 return;
562 }
563
564 // Check if there is any activity in the task that is visible and not finishing. If the
565 // launched activity finished before it is drawn and if there is another activity in
566 // the task then that activity will be draw on screen.
567 if (hasVisibleNonFinishingActivity(t)) {
568 return;
569 }
570
571 if (DEBUG_METRICS) Slog.i(TAG, "notifyVisibilityChanged to invisible activity=" + r);
572 logAppTransitionCancel(info);
573 mWindowingModeTransitionInfo.remove(r.getWindowingMode());
574 if (mWindowingModeTransitionInfo.size() == 0) {
Yan Wangd47f90b2019-10-03 19:17:15 -0700575 reset(true /* abort */, info, "notifyVisibilityChanged to invisible",
576 0L /* timestampNs */);
Jorim Jaggicdfc04e2017-04-28 19:06:24 +0200577 }
578 }
579 }
580
Jorim Jaggi515dd682017-05-05 15:05:07 +0200581 /**
582 * Notifies the tracker that we called immediately before we call bindApplication on the client.
583 *
Wale Ogunwale31913b52018-10-13 08:29:31 -0700584 * @param appInfo The client into which we'll call bindApplication.
Jorim Jaggi515dd682017-05-05 15:05:07 +0200585 */
Wale Ogunwale31913b52018-10-13 08:29:31 -0700586 void notifyBindApplication(ApplicationInfo appInfo) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100587 for (int i = mWindowingModeTransitionInfo.size() - 1; i >= 0; i--) {
588 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.valueAt(i);
Jorim Jaggi515dd682017-05-05 15:05:07 +0200589
590 // App isn't attached to record yet, so match with info.
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700591 if (info.launchedActivity.info.applicationInfo == appInfo) {
Jorim Jaggi515dd682017-05-05 15:05:07 +0200592 info.bindApplicationDelayMs = calculateCurrentDelay();
593 }
594 }
595 }
596
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100597 private boolean allWindowsDrawn() {
598 for (int index = mWindowingModeTransitionInfo.size() - 1; index >= 0; index--) {
599 if (!mWindowingModeTransitionInfo.valueAt(index).loggedWindowsDrawn) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800600 return false;
601 }
602 }
603 return true;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500604 }
605
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800606 private boolean isAnyTransitionActive() {
Yan Wangd47f90b2019-10-03 19:17:15 -0700607 return mCurrentTransitionStartTimeNs != INVALID_START_TIME
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100608 && mWindowingModeTransitionInfo.size() > 0;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800609 }
610
Yan Wangd47f90b2019-10-03 19:17:15 -0700611 private void reset(boolean abort, WindowingModeTransitionInfo info, String cause,
612 long timestampNs) {
613 if (DEBUG_METRICS) {
614 Slog.i(TAG,
615 "reset abort=" + abort + ",cause=" + cause + ",timestamp=" + timestampNs);
616 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800617 if (!abort && isAnyTransitionActive()) {
618 logAppTransitionMultiEvents();
619 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700620 stopLaunchTrace(info);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700621
622 // Ignore reset-after reset.
623 if (isAnyTransitionActive()) {
624 // LaunchObserver callbacks.
625 if (abort) {
626 launchObserverNotifyActivityLaunchCancelled(info);
627 } else {
Yan Wangd47f90b2019-10-03 19:17:15 -0700628 launchObserverNotifyActivityLaunchFinished(info, timestampNs);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700629 }
630 } else {
631 launchObserverNotifyIntentFailed();
632 }
633
Yan Wangd47f90b2019-10-03 19:17:15 -0700634 mCurrentTransitionStartTimeNs = INVALID_START_TIME;
Vishnu Nair132ee832018-09-28 15:00:05 -0700635 mCurrentTransitionDelayMs = INVALID_DELAY;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500636 mLoggedTransitionStarting = false;
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100637 mWindowingModeTransitionInfo.clear();
Jorim Jaggi275561a2016-02-23 10:11:02 -0500638 }
639
640 private int calculateCurrentDelay() {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500641 // Shouldn't take more than 25 days to launch an app, so int is fine here.
Yan Wangd47f90b2019-10-03 19:17:15 -0700642 return (int) TimeUnit.NANOSECONDS
643 .toMillis(SystemClock.elapsedRealtimeNanos() - mCurrentTransitionStartTimeNs);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500644 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800645
Yan Wangd47f90b2019-10-03 19:17:15 -0700646 private int calculateDelay(long timestampNs) {
Sudheer Shankac766db02017-06-12 10:37:29 -0700647 // Shouldn't take more than 25 days to launch an app, so int is fine here.
Yan Wangd47f90b2019-10-03 19:17:15 -0700648 return (int) TimeUnit.NANOSECONDS.toMillis(timestampNs -
649 mCurrentTransitionStartTimeNs);
Sudheer Shankac766db02017-06-12 10:37:29 -0700650 }
651
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100652 private void logAppTransitionCancel(WindowingModeTransitionInfo info) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200653 final int type = getTransitionType(info);
Vishnu Nair132ee832018-09-28 15:00:05 -0700654 if (type == INVALID_TRANSITION_TYPE) {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200655 return;
656 }
657 final LogMaker builder = new LogMaker(APP_TRANSITION_CANCELLED);
658 builder.setPackageName(info.launchedActivity.packageName);
659 builder.setType(type);
660 builder.addTaggedData(FIELD_CLASS_NAME, info.launchedActivity.info.name);
661 mMetricsLogger.write(builder);
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000662 StatsLog.write(
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800663 StatsLog.APP_START_CANCELED,
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700664 info.launchedActivity.info.applicationInfo.uid,
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000665 info.launchedActivity.packageName,
666 convertAppStartTransitionType(type),
667 info.launchedActivity.info.name);
Igor Murashkin212d06c2018-10-22 16:34:39 -0700668 if (DEBUG_METRICS) {
669 Slog.i(TAG, String.format("APP_START_CANCELED(%s, %s, %s, %s)",
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700670 info.launchedActivity.info.applicationInfo.uid,
Igor Murashkin212d06c2018-10-22 16:34:39 -0700671 info.launchedActivity.packageName,
672 convertAppStartTransitionType(type),
673 info.launchedActivity.info.name));
674 }
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200675 }
676
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800677 private void logAppTransitionMultiEvents() {
Jorim Jaggi172e99f2017-10-20 14:33:18 +0200678 if (DEBUG_METRICS) Slog.i(TAG, "logging transition events");
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100679 for (int index = mWindowingModeTransitionInfo.size() - 1; index >= 0; index--) {
680 final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.valueAt(index);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800681 final int type = getTransitionType(info);
Vishnu Nair132ee832018-09-28 15:00:05 -0700682 if (type == INVALID_TRANSITION_TYPE) {
Vishnu Nair194c64f2019-04-12 13:51:40 -0700683 if (DEBUG_METRICS) {
684 Slog.i(TAG, "invalid transition type"
685 + " processRunning=" + info.currentTransitionProcessRunning
686 + " startResult=" + info.startResult);
687 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800688 return;
689 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000690
691 // Take a snapshot of the transition info before sending it to the handler for logging.
692 // This will avoid any races with other operations that modify the ActivityRecord.
693 final WindowingModeTransitionInfoSnapshot infoSnapshot =
Yan Wangd47f90b2019-10-03 19:17:15 -0700694 new WindowingModeTransitionInfoSnapshot(info);
Ng Zhi An83473542018-02-20 09:02:14 -0800695 final int currentTransitionDeviceUptime = mCurrentTransitionDeviceUptime;
696 final int currentTransitionDelayMs = mCurrentTransitionDelayMs;
697 BackgroundThread.getHandler().post(() -> logAppTransition(
698 currentTransitionDeviceUptime, currentTransitionDelayMs, infoSnapshot));
Vishnu Nair132ee832018-09-28 15:00:05 -0700699 BackgroundThread.getHandler().post(() -> logAppDisplayed(infoSnapshot));
Calin Juravle759fbda2018-02-20 19:52:30 +0000700
701 info.launchedActivity.info.launchToken = null;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800702 }
703 }
704
Ng Zhi An83473542018-02-20 09:02:14 -0800705 // This gets called on a background thread without holding the activity manager lock.
Calin Juravle759fbda2018-02-20 19:52:30 +0000706 private void logAppTransition(int currentTransitionDeviceUptime, int currentTransitionDelayMs,
707 WindowingModeTransitionInfoSnapshot info) {
708 final LogMaker builder = new LogMaker(APP_TRANSITION);
709 builder.setPackageName(info.packageName);
710 builder.setType(info.type);
711 builder.addTaggedData(FIELD_CLASS_NAME, info.launchedActivityName);
712 final boolean isInstantApp = info.applicationInfo.isInstantApp();
713 if (info.launchedActivityLaunchedFromPackage != null) {
714 builder.addTaggedData(APP_TRANSITION_CALLING_PACKAGE_NAME,
715 info.launchedActivityLaunchedFromPackage);
716 }
717 String launchToken = info.launchedActivityLaunchToken;
718 if (launchToken != null) {
719 builder.addTaggedData(FIELD_INSTANT_APP_LAUNCH_TOKEN, launchToken);
720 }
721 builder.addTaggedData(APP_TRANSITION_IS_EPHEMERAL, isInstantApp ? 1 : 0);
722 builder.addTaggedData(APP_TRANSITION_DEVICE_UPTIME_SECONDS,
723 currentTransitionDeviceUptime);
724 builder.addTaggedData(APP_TRANSITION_DELAY_MS, currentTransitionDelayMs);
725 builder.setSubtype(info.reason);
Vishnu Nair132ee832018-09-28 15:00:05 -0700726 if (info.startingWindowDelayMs != INVALID_DELAY) {
Calin Juravle759fbda2018-02-20 19:52:30 +0000727 builder.addTaggedData(APP_TRANSITION_STARTING_WINDOW_DELAY_MS,
728 info.startingWindowDelayMs);
729 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700730 if (info.bindApplicationDelayMs != INVALID_DELAY) {
Calin Juravle759fbda2018-02-20 19:52:30 +0000731 builder.addTaggedData(APP_TRANSITION_BIND_APPLICATION_DELAY_MS,
732 info.bindApplicationDelayMs);
733 }
734 builder.addTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS, info.windowsDrawnDelayMs);
735 final ArtManagerInternal artManagerInternal = getArtManagerInternal();
736 final PackageOptimizationInfo packageOptimizationInfo =
737 (artManagerInternal == null) || (info.launchedActivityAppRecordRequiredAbi == null)
738 ? PackageOptimizationInfo.createWithNoInfo()
739 : artManagerInternal.getPackageOptimizationInfo(
740 info.applicationInfo,
741 info.launchedActivityAppRecordRequiredAbi);
742 builder.addTaggedData(PACKAGE_OPTIMIZATION_COMPILATION_REASON,
743 packageOptimizationInfo.getCompilationReason());
744 builder.addTaggedData(PACKAGE_OPTIMIZATION_COMPILATION_FILTER,
745 packageOptimizationInfo.getCompilationFilter());
746 mMetricsLogger.write(builder);
747 StatsLog.write(
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800748 StatsLog.APP_START_OCCURRED,
Calin Juravle759fbda2018-02-20 19:52:30 +0000749 info.applicationInfo.uid,
750 info.packageName,
751 convertAppStartTransitionType(info.type),
752 info.launchedActivityName,
753 info.launchedActivityLaunchedFromPackage,
754 isInstantApp,
755 currentTransitionDeviceUptime * 1000,
756 info.reason,
757 currentTransitionDelayMs,
758 info.startingWindowDelayMs,
759 info.bindApplicationDelayMs,
760 info.windowsDrawnDelayMs,
761 launchToken,
762 packageOptimizationInfo.getCompilationReason(),
763 packageOptimizationInfo.getCompilationFilter());
Igor Murashkin212d06c2018-10-22 16:34:39 -0700764
765 if (DEBUG_METRICS) {
766 Slog.i(TAG, String.format("APP_START_OCCURRED(%s, %s, %s, %s, %s)",
767 info.applicationInfo.uid,
768 info.packageName,
769 convertAppStartTransitionType(info.type),
770 info.launchedActivityName,
771 info.launchedActivityLaunchedFromPackage));
772 }
773
774
Ng Zhi An83473542018-02-20 09:02:14 -0800775 logAppStartMemoryStateCapture(info);
Calin Juravle759fbda2018-02-20 19:52:30 +0000776 }
777
Vishnu Nair132ee832018-09-28 15:00:05 -0700778 private void logAppDisplayed(WindowingModeTransitionInfoSnapshot info) {
779 if (info.type != TYPE_TRANSITION_WARM_LAUNCH && info.type != TYPE_TRANSITION_COLD_LAUNCH) {
780 return;
781 }
782
783 EventLog.writeEvent(AM_ACTIVITY_LAUNCH_TIME,
784 info.userId, info.activityRecordIdHashCode, info.launchedActivityShortComponentName,
785 info.windowsDrawnDelayMs);
786
787 StringBuilder sb = mStringBuilder;
788 sb.setLength(0);
789 sb.append("Displayed ");
790 sb.append(info.launchedActivityShortComponentName);
791 sb.append(": ");
792 TimeUtils.formatDuration(info.windowsDrawnDelayMs, sb);
793 Log.i(TAG, sb.toString());
794 }
795
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000796 private int convertAppStartTransitionType(int tronType) {
797 if (tronType == TYPE_TRANSITION_COLD_LAUNCH) {
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800798 return StatsLog.APP_START_OCCURRED__TYPE__COLD;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000799 }
800 if (tronType == TYPE_TRANSITION_WARM_LAUNCH) {
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800801 return StatsLog.APP_START_OCCURRED__TYPE__WARM;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000802 }
803 if (tronType == TYPE_TRANSITION_HOT_LAUNCH) {
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800804 return StatsLog.APP_START_OCCURRED__TYPE__HOT;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000805 }
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800806 return StatsLog.APP_START_OCCURRED__TYPE__UNKNOWN;
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000807 }
808
Vishnu Nair132ee832018-09-28 15:00:05 -0700809 WindowingModeTransitionInfoSnapshot logAppTransitionReportedDrawn(ActivityRecord r,
810 boolean restoredFromBundle) {
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100811 final WindowingModeTransitionInfo info = mLastWindowingModeTransitionInfo.get(
812 r.getWindowingMode());
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200813 if (info == null) {
Vishnu Nair132ee832018-09-28 15:00:05 -0700814 return null;
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200815 }
Chris Wailes35c193c2018-10-09 18:12:00 -0700816
817 // Record the handling of the reportFullyDrawn callback in the trace system. This is not
818 // actually used to trace this function, but instead the logical task that this function
819 // fullfils (handling reportFullyDrawn() callbacks).
820 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
821 "ActivityManager:ReportingFullyDrawn " + info.launchedActivity.packageName);
822
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200823 final LogMaker builder = new LogMaker(APP_TRANSITION_REPORTED_DRAWN);
824 builder.setPackageName(r.packageName);
825 builder.addTaggedData(FIELD_CLASS_NAME, r.info.name);
Yan Wang9eac5ec2019-09-30 15:42:18 -0700826 long currentTimestampNs = SystemClock.elapsedRealtimeNanos();
827 long startupTimeMs =
Yan Wangd47f90b2019-10-03 19:17:15 -0700828 TimeUnit.NANOSECONDS.toMillis(currentTimestampNs - mLastTransitionStartTimeNs);
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000829 builder.addTaggedData(APP_TRANSITION_REPORTED_DRAWN_MS, startupTimeMs);
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200830 builder.setType(restoredFromBundle
831 ? TYPE_TRANSITION_REPORTED_DRAWN_WITH_BUNDLE
832 : TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE);
833 builder.addTaggedData(APP_TRANSITION_PROCESS_RUNNING,
834 info.currentTransitionProcessRunning ? 1 : 0);
835 mMetricsLogger.write(builder);
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000836 StatsLog.write(
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800837 StatsLog.APP_START_FULLY_DRAWN,
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700838 info.launchedActivity.info.applicationInfo.uid,
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000839 info.launchedActivity.packageName,
840 restoredFromBundle
Chenjie Yu5caaa9d2018-03-06 15:48:54 -0800841 ? StatsLog.APP_START_FULLY_DRAWN__TYPE__WITH_BUNDLE
842 : StatsLog.APP_START_FULLY_DRAWN__TYPE__WITHOUT_BUNDLE,
Olivier Gaillardaed7f122017-12-12 14:26:22 +0000843 info.launchedActivity.info.name,
844 info.currentTransitionProcessRunning,
845 startupTimeMs);
Chris Wailes35c193c2018-10-09 18:12:00 -0700846
847 // Ends the trace started at the beginning of this function. This is located here to allow
848 // the trace slice to have a noticable duration.
849 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
850
Vishnu Nair132ee832018-09-28 15:00:05 -0700851 final WindowingModeTransitionInfoSnapshot infoSnapshot =
852 new WindowingModeTransitionInfoSnapshot(info, r, (int) startupTimeMs);
853 BackgroundThread.getHandler().post(() -> logAppFullyDrawn(infoSnapshot));
Yan Wang9eac5ec2019-09-30 15:42:18 -0700854
855 // Notify reportFullyDrawn event.
856 launchObserverNotifyReportFullyDrawn(r, currentTimestampNs);
857
Vishnu Nair132ee832018-09-28 15:00:05 -0700858 return infoSnapshot;
859 }
860
861 private void logAppFullyDrawn(WindowingModeTransitionInfoSnapshot info) {
862 if (info.type != TYPE_TRANSITION_WARM_LAUNCH && info.type != TYPE_TRANSITION_COLD_LAUNCH) {
863 return;
864 }
865
866 StringBuilder sb = mStringBuilder;
867 sb.setLength(0);
868 sb.append("Fully drawn ");
869 sb.append(info.launchedActivityShortComponentName);
870 sb.append(": ");
871 TimeUtils.formatDuration(info.windowsFullyDrawnDelayMs, sb);
872 Log.i(TAG, sb.toString());
Jorim Jaggi4d27b842017-08-17 17:22:26 +0200873 }
874
Michal Karpinski4fd5b842019-01-28 15:13:32 +0000875 void logAbortedBgActivityStart(Intent intent, WindowProcessController callerApp,
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100876 int callingUid, String callingPackage, int callingUidProcState,
877 boolean callingUidHasAnyVisibleWindow,
878 int realCallingUid, int realCallingUidProcState,
879 boolean realCallingUidHasAnyVisibleWindow,
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100880 boolean comingFromPendingIntent) {
881
882 final long nowElapsed = SystemClock.elapsedRealtime();
883 final long nowUptime = SystemClock.uptimeMillis();
884 final LogMaker builder = new LogMaker(ACTION_ACTIVITY_START);
885 builder.setTimestamp(System.currentTimeMillis());
886 builder.addTaggedData(FIELD_CALLING_UID, callingUid);
887 builder.addTaggedData(FIELD_CALLING_PACKAGE_NAME, callingPackage);
888 builder.addTaggedData(FIELD_CALLING_UID_PROC_STATE,
889 processStateAmToProto(callingUidProcState));
890 builder.addTaggedData(FIELD_CALLING_UID_HAS_ANY_VISIBLE_WINDOW,
891 callingUidHasAnyVisibleWindow ? 1 : 0);
892 builder.addTaggedData(FIELD_REAL_CALLING_UID, realCallingUid);
893 builder.addTaggedData(FIELD_REAL_CALLING_UID_PROC_STATE,
894 processStateAmToProto(realCallingUidProcState));
895 builder.addTaggedData(FIELD_REAL_CALLING_UID_HAS_ANY_VISIBLE_WINDOW,
896 realCallingUidHasAnyVisibleWindow ? 1 : 0);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100897 builder.addTaggedData(FIELD_COMING_FROM_PENDING_INTENT, comingFromPendingIntent ? 1 : 0);
Michal Karpinski8596ded2018-11-14 14:43:48 +0000898 if (intent != null) {
899 builder.addTaggedData(FIELD_INTENT_ACTION, intent.getAction());
Michal Karpinski319069b2019-03-27 15:46:50 +0000900 ComponentName component = intent.getComponent();
901 if (component != null) {
902 builder.addTaggedData(FIELD_TARGET_SHORT_COMPONENT_NAME,
903 component.flattenToShortString());
904 }
Michal Karpinski8596ded2018-11-14 14:43:48 +0000905 }
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100906 if (callerApp != null) {
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700907 builder.addTaggedData(FIELD_PROCESS_RECORD_PROCESS_NAME, callerApp.mName);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100908 builder.addTaggedData(FIELD_PROCESS_RECORD_CUR_PROC_STATE,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700909 processStateAmToProto(callerApp.getCurrentProcState()));
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100910 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_CLIENT_ACTIVITIES,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700911 callerApp.hasClientActivities() ? 1 : 0);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100912 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_FOREGROUND_SERVICES,
913 callerApp.hasForegroundServices() ? 1 : 0);
914 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_FOREGROUND_ACTIVITIES,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700915 callerApp.hasForegroundActivities() ? 1 : 0);
916 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_TOP_UI, callerApp.hasTopUi() ? 1 : 0);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100917 builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_OVERLAY_UI,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700918 callerApp.hasOverlayUi() ? 1 : 0);
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100919 builder.addTaggedData(FIELD_PROCESS_RECORD_PENDING_UI_CLEAN,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700920 callerApp.hasPendingUiClean() ? 1 : 0);
921 if (callerApp.getInteractionEventTime() != 0) {
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100922 builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_LAST_INTERACTION_EVENT,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700923 (nowElapsed - callerApp.getInteractionEventTime()));
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100924 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700925 if (callerApp.getFgInteractionTime() != 0) {
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100926 builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_FG_INTERACTION,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700927 (nowElapsed - callerApp.getFgInteractionTime()));
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100928 }
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700929 if (callerApp.getWhenUnimportant() != 0) {
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100930 builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_UNIMPORTANT,
Wale Ogunwale342fbe92018-10-09 08:44:10 -0700931 (nowUptime - callerApp.getWhenUnimportant()));
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100932 }
933 }
Michal Karpinski201bc0c2018-07-20 15:32:00 +0100934 mMetricsLogger.write(builder);
935 }
936
Jorim Jaggi9b58f2d2018-02-19 17:48:44 +0100937 private int getTransitionType(WindowingModeTransitionInfo info) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800938 if (info.currentTransitionProcessRunning) {
939 if (info.startResult == START_SUCCESS) {
940 return TYPE_TRANSITION_WARM_LAUNCH;
941 } else if (info.startResult == START_TASK_TO_FRONT) {
942 return TYPE_TRANSITION_HOT_LAUNCH;
943 }
Vishnu Nair194c64f2019-04-12 13:51:40 -0700944 } else if (info.startResult == START_SUCCESS
945 || (info.startResult == START_TASK_TO_FRONT)) {
946 // TaskRecord may still exist when cold launching an activity and the start
947 // result will be set to START_TASK_TO_FRONT. Treat this as a COLD launch.
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800948 return TYPE_TRANSITION_COLD_LAUNCH;
949 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700950 return INVALID_TRANSITION_TYPE;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800951 }
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800952
Ng Zhi An83473542018-02-20 09:02:14 -0800953 private void logAppStartMemoryStateCapture(WindowingModeTransitionInfoSnapshot info) {
954 if (info.processRecord == null) {
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800955 if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture processRecord null");
956 return;
957 }
958
Wale Ogunwalef6733932018-06-27 05:14:34 -0700959 final int pid = info.processRecord.getPid();
Ng Zhi An83473542018-02-20 09:02:14 -0800960 final int uid = info.applicationInfo.uid;
Rajeev Kumarbfcd9202018-03-02 22:42:13 +0000961 final MemoryStat memoryStat = readMemoryStatFromFilesystem(uid, pid);
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800962 if (memoryStat == null) {
963 if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture memoryStat null");
964 return;
965 }
966
967 StatsLog.write(
968 StatsLog.APP_START_MEMORY_STATE_CAPTURED,
969 uid,
Ng Zhi An83473542018-02-20 09:02:14 -0800970 info.processName,
971 info.launchedActivityName,
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800972 memoryStat.pgfault,
973 memoryStat.pgmajfault,
974 memoryStat.rssInBytes,
975 memoryStat.cacheInBytes,
976 memoryStat.swapInBytes);
977 }
978
Wale Ogunwalef6733932018-06-27 05:14:34 -0700979 private WindowProcessController findProcessForActivity(ActivityRecord launchedActivity) {
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800980 return launchedActivity != null
Wale Ogunwalef6733932018-06-27 05:14:34 -0700981 ? mSupervisor.mService.mProcessNames.get(
Andrii Kulianeceebbf2019-06-26 17:36:51 -0700982 launchedActivity.processName, launchedActivity.info.applicationInfo.uid)
Ng Zhi Anbbefdec2018-01-30 17:12:39 -0800983 : null;
984 }
Calin Juravle759fbda2018-02-20 19:52:30 +0000985
986 private ArtManagerInternal getArtManagerInternal() {
987 if (mArtManagerInternal == null) {
988 // Note that this may be null.
989 // ArtManagerInternal is registered during PackageManagerService
990 // initialization which happens after ActivityManagerService.
991 mArtManagerInternal = LocalServices.getService(ArtManagerInternal.class);
992 }
993 return mArtManagerInternal;
994 }
Vishnu Nair132ee832018-09-28 15:00:05 -0700995
996 /**
Chris Wailes35c193c2018-10-09 18:12:00 -0700997 * Starts traces for app launch.
Vishnu Nair132ee832018-09-28 15:00:05 -0700998 *
999 * @param info
1000 * */
1001 private void startTraces(WindowingModeTransitionInfo info) {
Ioannis Ilkos66e1cd82019-05-03 10:47:30 +01001002 if (!Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER) || info == null
1003 || info.launchTraceActive) {
Vishnu Nair132ee832018-09-28 15:00:05 -07001004 return;
1005 }
Ioannis Ilkos66e1cd82019-05-03 10:47:30 +01001006 Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: "
1007 + info.launchedActivity.packageName, 0);
1008 info.launchTraceActive = true;
Vishnu Nair132ee832018-09-28 15:00:05 -07001009 }
1010
1011 private void stopLaunchTrace(WindowingModeTransitionInfo info) {
1012 if (info == null) {
1013 return;
1014 }
1015 if (info.launchTraceActive) {
1016 Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: "
1017 + info.launchedActivity.packageName, 0);
1018 info.launchTraceActive = false;
1019 }
1020 }
Igor Murashkin212d06c2018-10-22 16:34:39 -07001021
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001022 public ActivityMetricsLaunchObserverRegistry getLaunchObserverRegistry() {
1023 return mLaunchObserver;
1024 }
1025
Igor Murashkin212d06c2018-10-22 16:34:39 -07001026 /** Notify the {@link ActivityMetricsLaunchObserver} that a new launch sequence has begun. */
Yan Wangd47f90b2019-10-03 19:17:15 -07001027 private void launchObserverNotifyIntentStarted(Intent intent, long timestampNs) {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001028 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1029 "MetricsLogger:launchObserverNotifyIntentStarted");
1030
1031 // Beginning a launch is timing sensitive and so should be observed as soon as possible.
Yan Wangd47f90b2019-10-03 19:17:15 -07001032 mLaunchObserver.onIntentStarted(intent, timestampNs);
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001033
1034 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Igor Murashkin212d06c2018-10-22 16:34:39 -07001035 }
1036
1037 /**
1038 * Notify the {@link ActivityMetricsLaunchObserver} that the previous launch sequence has
1039 * aborted due to intent failure (e.g. intent resolve failed or security error, etc) or
1040 * intent being delivered to the top running activity.
1041 */
1042 private void launchObserverNotifyIntentFailed() {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001043 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1044 "MetricsLogger:launchObserverNotifyIntentFailed");
1045
1046 mLaunchObserver.onIntentFailed();
1047
1048 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Igor Murashkin212d06c2018-10-22 16:34:39 -07001049 }
1050
1051 /**
1052 * Notify the {@link ActivityMetricsLaunchObserver} that the current launch sequence's activity
1053 * has started.
1054 */
1055 private void launchObserverNotifyActivityLaunched(WindowingModeTransitionInfo info) {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001056 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1057 "MetricsLogger:launchObserverNotifyActivityLaunched");
1058
Igor Murashkin212d06c2018-10-22 16:34:39 -07001059 @ActivityMetricsLaunchObserver.Temperature int temperature =
1060 convertTransitionTypeToLaunchObserverTemperature(getTransitionType(info));
1061
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001062 // Beginning a launch is timing sensitive and so should be observed as soon as possible.
1063 mLaunchObserver.onActivityLaunched(convertActivityRecordToProto(info.launchedActivity),
1064 temperature);
1065
1066 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Igor Murashkin212d06c2018-10-22 16:34:39 -07001067 }
1068
1069 /**
Yan Wang9eac5ec2019-09-30 15:42:18 -07001070 * Notifies the {@link ActivityMetricsLaunchObserver} the reportFullDrawn event.
1071 */
Yan Wangd47f90b2019-10-03 19:17:15 -07001072 private void launchObserverNotifyReportFullyDrawn(ActivityRecord r, long timestampNs) {
Yan Wang9eac5ec2019-09-30 15:42:18 -07001073 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1074 "MetricsLogger:launchObserverNotifyReportFullyDrawn");
Yan Wangd47f90b2019-10-03 19:17:15 -07001075 mLaunchObserver.onReportFullyDrawn(convertActivityRecordToProto(r), timestampNs);
Yan Wang9eac5ec2019-09-30 15:42:18 -07001076 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
1077 }
1078
1079 /**
Igor Murashkin212d06c2018-10-22 16:34:39 -07001080 * Notify the {@link ActivityMetricsLaunchObserver} that the current launch sequence is
1081 * cancelled.
1082 */
1083 private void launchObserverNotifyActivityLaunchCancelled(WindowingModeTransitionInfo info) {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001084 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1085 "MetricsLogger:launchObserverNotifyActivityLaunchCancelled");
Igor Murashkin212d06c2018-10-22 16:34:39 -07001086
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001087 final @ActivityMetricsLaunchObserver.ActivityRecordProto byte[] activityRecordProto =
1088 info != null ? convertActivityRecordToProto(info.launchedActivity) : null;
1089
1090 mLaunchObserver.onActivityLaunchCancelled(activityRecordProto);
1091
1092 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Igor Murashkin212d06c2018-10-22 16:34:39 -07001093 }
1094
1095 /**
1096 * Notify the {@link ActivityMetricsLaunchObserver} that the current launch sequence's activity
1097 * has fully finished (successfully).
1098 */
Yan Wangd47f90b2019-10-03 19:17:15 -07001099 private void launchObserverNotifyActivityLaunchFinished(WindowingModeTransitionInfo info,
1100 long timestampNs) {
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001101 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1102 "MetricsLogger:launchObserverNotifyActivityLaunchFinished");
Igor Murashkin212d06c2018-10-22 16:34:39 -07001103
Yan Wangd47f90b2019-10-03 19:17:15 -07001104 mLaunchObserver
1105 .onActivityLaunchFinished(convertActivityRecordToProto(info.launchedActivity),
1106 timestampNs);
Igor Murashkinc0b47e42018-11-07 15:54:18 -08001107
1108 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
1109 }
1110
1111 @VisibleForTesting
1112 static @ActivityMetricsLaunchObserver.ActivityRecordProto byte[]
1113 convertActivityRecordToProto(ActivityRecord record) {
1114 // May take non-negligible amount of time to convert ActivityRecord into a proto,
1115 // so track the time.
1116 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
1117 "MetricsLogger:convertActivityRecordToProto");
1118
1119 // There does not appear to be a way to 'reset' a ProtoOutputBuffer stream,
1120 // so create a new one every time.
1121 final ProtoOutputStream protoOutputStream =
1122 new ProtoOutputStream(LAUNCH_OBSERVER_ACTIVITY_RECORD_PROTO_CHUNK_SIZE);
1123 // Write this data out as the top-most ActivityRecordProto (i.e. it is not a sub-object).
1124 record.writeToProto(protoOutputStream);
1125 final byte[] bytes = protoOutputStream.getBytes();
1126
1127 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
1128
1129 return bytes;
Igor Murashkin212d06c2018-10-22 16:34:39 -07001130 }
1131
1132 private static @ActivityMetricsLaunchObserver.Temperature int
1133 convertTransitionTypeToLaunchObserverTemperature(int transitionType) {
1134 switch (transitionType) {
1135 case TYPE_TRANSITION_WARM_LAUNCH:
1136 return ActivityMetricsLaunchObserver.TEMPERATURE_WARM;
1137 case TYPE_TRANSITION_HOT_LAUNCH:
1138 return ActivityMetricsLaunchObserver.TEMPERATURE_HOT;
1139 case TYPE_TRANSITION_COLD_LAUNCH:
1140 return ActivityMetricsLaunchObserver.TEMPERATURE_COLD;
1141 default:
1142 return -1;
1143 }
1144 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -08001145}