blob: 32dec96c027ee2bec7588618989ec962461e79a3 [file] [log] [blame]
Filip Gruszczynski77d94482015-12-11 13:59:52 -08001package com.android.server.am;
2
3import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
4import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
5import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
6import static android.app.ActivityManager.StackId.HOME_STACK_ID;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -08007import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Matthew Ngae1ff4f2016-11-10 15:49:14 -08008import static android.app.ActivityManager.StackId.RECENTS_STACK_ID;
9
Jorim Jaggif9704102016-05-05 19:14:22 -070010import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
11import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0e381e22016-01-14 16:31:33 -080012import static com.android.server.am.ActivityStack.STACK_INVISIBLE;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080013
Jorim Jaggi275561a2016-02-23 10:11:02 -050014import android.annotation.Nullable;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080015import android.app.ActivityManager.StackId;
16import android.content.Context;
17import android.os.SystemClock;
Jorim Jaggif9704102016-05-05 19:14:22 -070018import android.util.Slog;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080019
20import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010021import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080022
Jorim Jaggi1e630c02016-05-16 12:13:13 -070023import java.util.ArrayList;
24
Filip Gruszczynski77d94482015-12-11 13:59:52 -080025/**
26 * Handles logging into Tron.
27 */
28class ActivityMetricsLogger {
Jorim Jaggif9704102016-05-05 19:14:22 -070029
30 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityMetricsLogger" : TAG_AM;
31
Filip Gruszczynski77d94482015-12-11 13:59:52 -080032 // Window modes we are interested in logging. If we ever introduce a new type, we need to add
33 // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array.
34 private static final int WINDOW_STATE_STANDARD = 0;
35 private static final int WINDOW_STATE_SIDE_BY_SIDE = 1;
36 private static final int WINDOW_STATE_FREEFORM = 2;
37 private static final int WINDOW_STATE_INVALID = -1;
38
Jorim Jaggi275561a2016-02-23 10:11:02 -050039 private static final long INVALID_START_TIME = -1;
40
Filip Gruszczynski77d94482015-12-11 13:59:52 -080041 // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
42 // time we log.
43 private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = {
Chris Wrenae255ee2016-01-04 16:20:17 -050044 "window_time_0", "window_time_1", "window_time_2"};
Filip Gruszczynski77d94482015-12-11 13:59:52 -080045
46 private int mWindowState = WINDOW_STATE_STANDARD;
47 private long mLastLogTimeSecs;
48 private final ActivityStackSupervisor mSupervisor;
49 private final Context mContext;
50
Jorim Jaggi275561a2016-02-23 10:11:02 -050051 private long mCurrentTransitionStartTime = INVALID_START_TIME;
52 private boolean mLoggedWindowsDrawn;
53 private boolean mLoggedStartingWindowDrawn;
54 private boolean mLoggedTransitionStarting;
55
Filip Gruszczynski77d94482015-12-11 13:59:52 -080056 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context) {
57 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
58 mSupervisor = supervisor;
59 mContext = context;
60 }
61
62 void logWindowState() {
63 final long now = SystemClock.elapsedRealtime() / 1000;
64 if (mWindowState != WINDOW_STATE_INVALID) {
65 // We log even if the window state hasn't changed, because the user might remain in
66 // home/fullscreen move forever and we would like to track this kind of behavior
67 // too.
68 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
69 (int) (now - mLastLogTimeSecs));
70 }
71 mLastLogTimeSecs = now;
72
Filip Gruszczynski77d94482015-12-11 13:59:52 -080073 ActivityStack stack = mSupervisor.getStack(DOCKED_STACK_ID);
Wale Ogunwale8051c5c2016-03-04 10:27:32 -080074 if (stack != null && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Filip Gruszczynski77d94482015-12-11 13:59:52 -080075 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080076 return;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080077 }
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080078 mWindowState = WINDOW_STATE_INVALID;
79 stack = mSupervisor.getFocusedStack();
80 if (stack.mStackId == PINNED_STACK_ID) {
81 stack = mSupervisor.findStackBehind(stack);
82 }
Matthew Ngae1ff4f2016-11-10 15:49:14 -080083 if (StackId.isHomeOrRecentsStack(stack.mStackId)
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080084 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
85 mWindowState = WINDOW_STATE_STANDARD;
86 } else if (stack.mStackId == DOCKED_STACK_ID) {
Jorim Jaggif9704102016-05-05 19:14:22 -070087 Slog.wtf(TAG, "Docked stack shouldn't be the focused stack, because it reported not"
88 + " being visible.");
89 mWindowState = WINDOW_STATE_INVALID;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080090 } else if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
91 mWindowState = WINDOW_STATE_FREEFORM;
92 } else if (StackId.isStaticStack(stack.mStackId)) {
93 throw new IllegalStateException("Unknown stack=" + stack);
Filip Gruszczynski77d94482015-12-11 13:59:52 -080094 }
95 }
Jorim Jaggi275561a2016-02-23 10:11:02 -050096
97 /**
98 * Notifies the tracker at the earliest possible point when we are starting to launch an
99 * activity.
100 */
101 void notifyActivityLaunching() {
102 mCurrentTransitionStartTime = System.currentTimeMillis();
103 }
104
105 /**
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700106 * Notifies the tracker that the activity is actually launching.
107 *
108 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
109 * launch
110 * @param launchedActivity the activity that is being launched
111 */
112 void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity) {
113 final ProcessRecord processRecord = launchedActivity != null
114 ? mSupervisor.mService.mProcessNames.get(launchedActivity.processName,
115 launchedActivity.appInfo.uid)
116 : null;
117 final boolean processRunning = processRecord != null;
118 final String componentName = launchedActivity != null
119 ? launchedActivity.shortComponentName
120 : null;
121
122 // We consider this a "process switch" if the process of the activity that gets launched
123 // didn't have an activity that was in started state. In this case, we assume that lot
124 // of caches might be purged so the time until it produces the first frame is very
125 // interesting.
126 final boolean processSwitch = processRecord == null
127 || !hasStartedActivity(processRecord, launchedActivity);
128
129 notifyActivityLaunched(resultCode, componentName, processRunning, processSwitch);
130 }
131
132 private boolean hasStartedActivity(ProcessRecord record, ActivityRecord launchedActivity) {
133 final ArrayList<ActivityRecord> activities = record.activities;
134 for (int i = activities.size() - 1; i >= 0; i--) {
135 final ActivityRecord activity = activities.get(i);
136 if (launchedActivity == activity) {
137 continue;
138 }
139 if (!activity.stopped) {
140 return true;
141 }
142 }
143 return false;
144 }
145
146 /**
Jorim Jaggi275561a2016-02-23 10:11:02 -0500147 * Notifies the tracker the the activity is actually launching.
148 *
149 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
150 * launch
151 * @param componentName the component name of the activity being launched
152 * @param processRunning whether the process that will contains the activity is already running
Jorim Jaggibe67c902016-04-12 00:53:16 -0700153 * @param processSwitch whether the process that will contain the activity didn't have any
154 * activity that was stopped, i.e. the started activity is "switching"
155 * processes
Jorim Jaggi275561a2016-02-23 10:11:02 -0500156 */
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700157 private void notifyActivityLaunched(int resultCode, @Nullable String componentName,
Jorim Jaggibe67c902016-04-12 00:53:16 -0700158 boolean processRunning, boolean processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500159
Jorim Jaggibe67c902016-04-12 00:53:16 -0700160 if (resultCode < 0 || componentName == null || !processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500161
Jorim Jaggibe67c902016-04-12 00:53:16 -0700162 // Failed to launch or it was not a process switch, so we don't care about the timing.
Jorim Jaggi275561a2016-02-23 10:11:02 -0500163 reset();
164 return;
165 }
166
167 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_COMPONENT_NAME,
168 componentName);
169 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_PROCESS_RUNNING,
170 processRunning);
171 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS,
172 (int) (SystemClock.uptimeMillis() / 1000));
173 }
174
175 /**
176 * Notifies the tracker that all windows of the app have been drawn.
177 */
178 void notifyWindowsDrawn() {
179 if (!isTransitionActive() || mLoggedWindowsDrawn) {
180 return;
181 }
182 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS,
183 calculateCurrentDelay());
184 mLoggedWindowsDrawn = true;
185 if (mLoggedTransitionStarting) {
186 reset();
187 }
188 }
189
190 /**
191 * Notifies the tracker that the starting window was drawn.
192 */
193 void notifyStartingWindowDrawn() {
194 if (!isTransitionActive() || mLoggedStartingWindowDrawn) {
195 return;
196 }
197 mLoggedStartingWindowDrawn = true;
198 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS,
199 calculateCurrentDelay());
200 }
201
202 /**
203 * Notifies the tracker that the app transition is starting.
204 *
205 * @param reason The reason why we started it. Must be on of
206 * ActivityManagerInternal.APP_TRANSITION_* reasons.
207 */
208 void notifyTransitionStarting(int reason) {
209 if (!isTransitionActive() || mLoggedTransitionStarting) {
210 return;
211 }
212 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_REASON, reason);
213 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_DELAY_MS,
214 calculateCurrentDelay());
215 mLoggedTransitionStarting = true;
216 if (mLoggedWindowsDrawn) {
217 reset();
218 }
219 }
220
221 private boolean isTransitionActive() {
222 return mCurrentTransitionStartTime != INVALID_START_TIME;
223 }
224
225 private void reset() {
226 mCurrentTransitionStartTime = INVALID_START_TIME;
227 mLoggedWindowsDrawn = false;
228 mLoggedTransitionStarting = false;
229 mLoggedStartingWindowDrawn = false;
230 }
231
232 private int calculateCurrentDelay() {
233
234 // Shouldn't take more than 25 days to launch an app, so int is fine here.
235 return (int) (System.currentTimeMillis() - mCurrentTransitionStartTime);
236 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800237}