blob: dd8c05ea9b87b6da16b3038cc3de0eaec55b40df [file] [log] [blame]
Filip Gruszczynski77d94482015-12-11 13:59:52 -08001package com.android.server.am;
2
Jorim Jaggi3878ca32017-02-02 17:13:05 -08003import static android.app.ActivityManager.START_SUCCESS;
4import static android.app.ActivityManager.START_TASK_TO_FRONT;
Winson Chung83471632016-12-13 11:02:12 -08005import static android.app.ActivityManager.StackId.ASSISTANT_STACK_ID;
Filip Gruszczynski77d94482015-12-11 13:59:52 -08006import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
7import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
8import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
Jorim Jaggi3878ca32017-02-02 17:13:05 -08009import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080010import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080011import static android.app.ActivityManagerInternal.APP_TRANSITION_TIMEOUT;
12import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION;
13import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_ACTIVITY_NAME;
Todd Kennedy50d946c12017-03-17 13:55:38 -070014import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_CALLING_PACKAGE_NAME;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080015import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DELAY_MS;
16import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS;
Todd Kennedy50d946c12017-03-17 13:55:38 -070017import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_IS_EPHEMERAL;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080018import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS;
19import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS;
Todd Kennedy50d946c12017-03-17 13:55:38 -070020import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_INSTANT_APP_LAUNCH_TOKEN;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080021import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_COLD_LAUNCH;
22import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_HOT_LAUNCH;
23import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_WARM_LAUNCH;
Jorim Jaggif9704102016-05-05 19:14:22 -070024import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
25import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -080026import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080027
28import android.app.ActivityManager.StackId;
29import android.content.Context;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080030import android.metrics.LogMaker;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080031import android.os.SystemClock;
Jorim Jaggif9704102016-05-05 19:14:22 -070032import android.util.Slog;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080033import android.util.SparseArray;
34import android.util.SparseIntArray;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080035
36import com.android.internal.logging.MetricsLogger;
37
Jorim Jaggi1e630c02016-05-16 12:13:13 -070038import java.util.ArrayList;
39
Filip Gruszczynski77d94482015-12-11 13:59:52 -080040/**
41 * Handles logging into Tron.
42 */
43class ActivityMetricsLogger {
Jorim Jaggif9704102016-05-05 19:14:22 -070044
45 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityMetricsLogger" : TAG_AM;
46
Filip Gruszczynski77d94482015-12-11 13:59:52 -080047 // Window modes we are interested in logging. If we ever introduce a new type, we need to add
48 // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array.
49 private static final int WINDOW_STATE_STANDARD = 0;
50 private static final int WINDOW_STATE_SIDE_BY_SIDE = 1;
51 private static final int WINDOW_STATE_FREEFORM = 2;
Winson Chung83471632016-12-13 11:02:12 -080052 private static final int WINDOW_STATE_ASSISTANT = 3;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080053 private static final int WINDOW_STATE_INVALID = -1;
54
Jorim Jaggi275561a2016-02-23 10:11:02 -050055 private static final long INVALID_START_TIME = -1;
56
Filip Gruszczynski77d94482015-12-11 13:59:52 -080057 // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
58 // time we log.
59 private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = {
Winson Chung83471632016-12-13 11:02:12 -080060 "window_time_0", "window_time_1", "window_time_2", "window_time_3"};
Filip Gruszczynski77d94482015-12-11 13:59:52 -080061
62 private int mWindowState = WINDOW_STATE_STANDARD;
63 private long mLastLogTimeSecs;
64 private final ActivityStackSupervisor mSupervisor;
65 private final Context mContext;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080066 private final MetricsLogger mMetricsLogger = new MetricsLogger();
Filip Gruszczynski77d94482015-12-11 13:59:52 -080067
Jorim Jaggi275561a2016-02-23 10:11:02 -050068 private long mCurrentTransitionStartTime = INVALID_START_TIME;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080069
70 private int mCurrentTransitionDeviceUptime;
71 private int mCurrentTransitionDelayMs;
Jorim Jaggi275561a2016-02-23 10:11:02 -050072 private boolean mLoggedTransitionStarting;
73
Jorim Jaggi3878ca32017-02-02 17:13:05 -080074 private final SparseArray<StackTransitionInfo> mStackTransitionInfo = new SparseArray<>();
75
76 private final class StackTransitionInfo {
77 private ActivityRecord launchedActivity;
78 private int startResult;
79 private boolean currentTransitionProcessRunning;
80 private int windowsDrawnDelayMs;
81 private int startingWindowDelayMs;
82 private int reason = APP_TRANSITION_TIMEOUT;
83 private boolean loggedWindowsDrawn;
84 private boolean loggedStartingWindowDrawn;
85 }
86
Filip Gruszczynski77d94482015-12-11 13:59:52 -080087 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context) {
88 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
89 mSupervisor = supervisor;
90 mContext = context;
91 }
92
93 void logWindowState() {
94 final long now = SystemClock.elapsedRealtime() / 1000;
95 if (mWindowState != WINDOW_STATE_INVALID) {
96 // We log even if the window state hasn't changed, because the user might remain in
97 // home/fullscreen move forever and we would like to track this kind of behavior
98 // too.
99 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
100 (int) (now - mLastLogTimeSecs));
101 }
102 mLastLogTimeSecs = now;
103
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800104 ActivityStack stack = mSupervisor.getStack(DOCKED_STACK_ID);
Wale Ogunwale8051c5c2016-03-04 10:27:32 -0800105 if (stack != null && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800106 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800107 return;
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800108 }
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800109 mWindowState = WINDOW_STATE_INVALID;
110 stack = mSupervisor.getFocusedStack();
111 if (stack.mStackId == PINNED_STACK_ID) {
112 stack = mSupervisor.findStackBehind(stack);
113 }
Matthew Ngae1ff4f2016-11-10 15:49:14 -0800114 if (StackId.isHomeOrRecentsStack(stack.mStackId)
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800115 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
116 mWindowState = WINDOW_STATE_STANDARD;
117 } else if (stack.mStackId == DOCKED_STACK_ID) {
Jorim Jaggif9704102016-05-05 19:14:22 -0700118 Slog.wtf(TAG, "Docked stack shouldn't be the focused stack, because it reported not"
119 + " being visible.");
120 mWindowState = WINDOW_STATE_INVALID;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800121 } else if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
122 mWindowState = WINDOW_STATE_FREEFORM;
Winson Chung83471632016-12-13 11:02:12 -0800123 } else if (stack.mStackId == ASSISTANT_STACK_ID) {
124 mWindowState = WINDOW_STATE_ASSISTANT;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -0800125 } else if (StackId.isStaticStack(stack.mStackId)) {
126 throw new IllegalStateException("Unknown stack=" + stack);
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800127 }
128 }
Jorim Jaggi275561a2016-02-23 10:11:02 -0500129
130 /**
131 * Notifies the tracker at the earliest possible point when we are starting to launch an
132 * activity.
133 */
134 void notifyActivityLaunching() {
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800135 if (!isAnyTransitionActive()) {
136 mCurrentTransitionStartTime = System.currentTimeMillis();
137 }
Jorim Jaggi275561a2016-02-23 10:11:02 -0500138 }
139
140 /**
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700141 * Notifies the tracker that the activity is actually launching.
142 *
143 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
144 * launch
145 * @param launchedActivity the activity that is being launched
146 */
147 void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity) {
148 final ProcessRecord processRecord = launchedActivity != null
149 ? mSupervisor.mService.mProcessNames.get(launchedActivity.processName,
150 launchedActivity.appInfo.uid)
151 : null;
152 final boolean processRunning = processRecord != null;
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700153
154 // We consider this a "process switch" if the process of the activity that gets launched
155 // didn't have an activity that was in started state. In this case, we assume that lot
156 // of caches might be purged so the time until it produces the first frame is very
157 // interesting.
158 final boolean processSwitch = processRecord == null
159 || !hasStartedActivity(processRecord, launchedActivity);
160
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800161 notifyActivityLaunched(resultCode, launchedActivity, processRunning, processSwitch);
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700162 }
163
164 private boolean hasStartedActivity(ProcessRecord record, ActivityRecord launchedActivity) {
165 final ArrayList<ActivityRecord> activities = record.activities;
166 for (int i = activities.size() - 1; i >= 0; i--) {
167 final ActivityRecord activity = activities.get(i);
168 if (launchedActivity == activity) {
169 continue;
170 }
171 if (!activity.stopped) {
172 return true;
173 }
174 }
175 return false;
176 }
177
178 /**
Jorim Jaggi275561a2016-02-23 10:11:02 -0500179 * Notifies the tracker the the activity is actually launching.
180 *
181 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
182 * launch
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800183 * @param launchedActivity the activity being launched
Jorim Jaggi275561a2016-02-23 10:11:02 -0500184 * @param processRunning whether the process that will contains the activity is already running
Jorim Jaggibe67c902016-04-12 00:53:16 -0700185 * @param processSwitch whether the process that will contain the activity didn't have any
186 * activity that was stopped, i.e. the started activity is "switching"
187 * processes
Jorim Jaggi275561a2016-02-23 10:11:02 -0500188 */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800189 private void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity,
Jorim Jaggibe67c902016-04-12 00:53:16 -0700190 boolean processRunning, boolean processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500191
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800192 // If we are already in an existing transition, only update the activity name, but not the
193 // other attributes.
194 final int stackId = launchedActivity != null && launchedActivity.getStack() != null
195 ? launchedActivity.getStack().mStackId
196 : INVALID_STACK_ID;
197 final StackTransitionInfo info = mStackTransitionInfo.get(stackId);
198 if (launchedActivity != null && info != null) {
199 info.launchedActivity = launchedActivity;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500200 return;
201 }
202
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800203 final boolean otherStacksLaunching = mStackTransitionInfo.size() > 0 && info == null;
204 if ((resultCode < 0 || launchedActivity == null || !processSwitch
205 || stackId == INVALID_STACK_ID) && !otherStacksLaunching) {
Alison Cichowlas803054d2016-12-13 14:38:01 -0500206
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800207 // Failed to launch or it was not a process switch, so we don't care about the timing.
208 reset(true /* abort */);
209 return;
210 } else if (otherStacksLaunching) {
211 // Don't log this stack but continue with the other stacks.
212 return;
213 }
214
215 final StackTransitionInfo newInfo = new StackTransitionInfo();
216 newInfo.launchedActivity = launchedActivity;
217 newInfo.currentTransitionProcessRunning = processRunning;
218 newInfo.startResult = resultCode;
219 mStackTransitionInfo.append(stackId, newInfo);
220 mCurrentTransitionDeviceUptime = (int) (SystemClock.uptimeMillis() / 1000);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500221 }
222
223 /**
224 * Notifies the tracker that all windows of the app have been drawn.
225 */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800226 void notifyWindowsDrawn(int stackId) {
227 final StackTransitionInfo info = mStackTransitionInfo.get(stackId);
228 if (info == null || info.loggedWindowsDrawn) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500229 return;
230 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800231 info.windowsDrawnDelayMs = calculateCurrentDelay();
232 info.loggedWindowsDrawn = true;
233 if (allStacksWindowsDrawn() && mLoggedTransitionStarting) {
234 reset(false /* abort */);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500235 }
236 }
237
238 /**
239 * Notifies the tracker that the starting window was drawn.
240 */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800241 void notifyStartingWindowDrawn(int stackId) {
242 final StackTransitionInfo info = mStackTransitionInfo.get(stackId);
243 if (info == null || info.loggedStartingWindowDrawn) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500244 return;
245 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800246 info.loggedStartingWindowDrawn = true;
247 info.startingWindowDelayMs = calculateCurrentDelay();
Jorim Jaggi275561a2016-02-23 10:11:02 -0500248 }
249
250 /**
251 * Notifies the tracker that the app transition is starting.
252 *
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800253 * @param stackIdReasons A map from stack id to a reason integer, which must be on of
254 * ActivityManagerInternal.APP_TRANSITION_* reasons.
Jorim Jaggi275561a2016-02-23 10:11:02 -0500255 */
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800256 void notifyTransitionStarting(SparseIntArray stackIdReasons) {
Jorim Jaggi453cae32017-03-16 15:41:40 +0100257 // TODO (b/36339388): Figure out why stackIdReasons can be null
258 if (stackIdReasons == null || !isAnyTransitionActive() || mLoggedTransitionStarting) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500259 return;
260 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800261 mCurrentTransitionDelayMs = calculateCurrentDelay();
Jorim Jaggi275561a2016-02-23 10:11:02 -0500262 mLoggedTransitionStarting = true;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800263 for (int index = stackIdReasons.size() - 1; index >= 0; index--) {
264 final int stackId = stackIdReasons.keyAt(index);
265 final StackTransitionInfo info = mStackTransitionInfo.get(stackId);
266 if (info == null) {
267 continue;
268 }
269 info.reason = stackIdReasons.valueAt(index);
270 }
271 if (allStacksWindowsDrawn()) {
272 reset(false /* abort */);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500273 }
274 }
275
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800276 private boolean allStacksWindowsDrawn() {
277 for (int index = mStackTransitionInfo.size() - 1; index >= 0; index--) {
278 if (!mStackTransitionInfo.valueAt(index).loggedWindowsDrawn) {
279 return false;
280 }
281 }
282 return true;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500283 }
284
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800285 private boolean isAnyTransitionActive() {
286 return mCurrentTransitionStartTime != INVALID_START_TIME
287 && mStackTransitionInfo.size() > 0;
288 }
289
290 private void reset(boolean abort) {
291 if (!abort && isAnyTransitionActive()) {
292 logAppTransitionMultiEvents();
293 }
Jorim Jaggi275561a2016-02-23 10:11:02 -0500294 mCurrentTransitionStartTime = INVALID_START_TIME;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800295 mCurrentTransitionDelayMs = -1;
Jorim Jaggi275561a2016-02-23 10:11:02 -0500296 mLoggedTransitionStarting = false;
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800297 mStackTransitionInfo.clear();
Jorim Jaggi275561a2016-02-23 10:11:02 -0500298 }
299
300 private int calculateCurrentDelay() {
301
302 // Shouldn't take more than 25 days to launch an app, so int is fine here.
303 return (int) (System.currentTimeMillis() - mCurrentTransitionStartTime);
304 }
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800305
306 private void logAppTransitionMultiEvents() {
307 for (int index = mStackTransitionInfo.size() - 1; index >= 0; index--) {
308 final StackTransitionInfo info = mStackTransitionInfo.valueAt(index);
309 final int type = getTransitionType(info);
310 if (type == -1) {
311 return;
312 }
313 final LogMaker builder = new LogMaker(APP_TRANSITION);
314 builder.setPackageName(info.launchedActivity.packageName);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800315 builder.setType(type);
Todd Kennedy50d946c12017-03-17 13:55:38 -0700316 builder.addTaggedData(APP_TRANSITION_ACTIVITY_NAME, info.launchedActivity.info.name);
317 if (info.launchedActivity.launchedFromPackage != null) {
318 builder.addTaggedData(APP_TRANSITION_CALLING_PACKAGE_NAME,
319 info.launchedActivity.launchedFromPackage);
320 }
321 if (info.launchedActivity.info.launchToken != null) {
322 builder.addTaggedData(FIELD_INSTANT_APP_LAUNCH_TOKEN,
323 info.launchedActivity.info.launchToken);
324 }
325 builder.addTaggedData(APP_TRANSITION_IS_EPHEMERAL,
326 info.launchedActivity.info.applicationInfo.isInstantApp() ? 1 : 0);
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800327 builder.addTaggedData(APP_TRANSITION_DEVICE_UPTIME_SECONDS,
328 mCurrentTransitionDeviceUptime);
329 builder.addTaggedData(APP_TRANSITION_DELAY_MS, mCurrentTransitionDelayMs);
330 builder.setSubtype(info.reason);
331 if (info.startingWindowDelayMs != -1) {
332 builder.addTaggedData(APP_TRANSITION_STARTING_WINDOW_DELAY_MS,
333 info.startingWindowDelayMs);
334 }
335 builder.addTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS, info.windowsDrawnDelayMs);
336 mMetricsLogger.write(builder);
337 }
338 }
339
340 private int getTransitionType(StackTransitionInfo info) {
341 if (info.currentTransitionProcessRunning) {
342 if (info.startResult == START_SUCCESS) {
343 return TYPE_TRANSITION_WARM_LAUNCH;
344 } else if (info.startResult == START_TASK_TO_FRONT) {
345 return TYPE_TRANSITION_HOT_LAUNCH;
346 }
347 } else if (info.startResult == START_SUCCESS) {
348 return TYPE_TRANSITION_COLD_LAUNCH;
349 }
350 return -1;
351 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800352}