blob: 3f166fe4ddfb3eec9a370ac9274e0f3441be6c06 [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
Alison Cichowlas803054d2016-12-13 14:38:01 -050020import com.android.internal.logging.LogBuilder;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080021import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010022import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080023
Jorim Jaggi1e630c02016-05-16 12:13:13 -070024import java.util.ArrayList;
25
Filip Gruszczynski77d94482015-12-11 13:59:52 -080026/**
27 * Handles logging into Tron.
28 */
29class ActivityMetricsLogger {
Jorim Jaggif9704102016-05-05 19:14:22 -070030
31 private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityMetricsLogger" : TAG_AM;
32
Filip Gruszczynski77d94482015-12-11 13:59:52 -080033 // Window modes we are interested in logging. If we ever introduce a new type, we need to add
34 // a value here and increase the {@link #TRON_WINDOW_STATE_VARZ_STRINGS} array.
35 private static final int WINDOW_STATE_STANDARD = 0;
36 private static final int WINDOW_STATE_SIDE_BY_SIDE = 1;
37 private static final int WINDOW_STATE_FREEFORM = 2;
38 private static final int WINDOW_STATE_INVALID = -1;
39
Jorim Jaggi275561a2016-02-23 10:11:02 -050040 private static final long INVALID_START_TIME = -1;
41
Filip Gruszczynski77d94482015-12-11 13:59:52 -080042 // Preallocated strings we are sending to tron, so we don't have to allocate a new one every
43 // time we log.
44 private static final String[] TRON_WINDOW_STATE_VARZ_STRINGS = {
Chris Wrenae255ee2016-01-04 16:20:17 -050045 "window_time_0", "window_time_1", "window_time_2"};
Filip Gruszczynski77d94482015-12-11 13:59:52 -080046
47 private int mWindowState = WINDOW_STATE_STANDARD;
48 private long mLastLogTimeSecs;
49 private final ActivityStackSupervisor mSupervisor;
50 private final Context mContext;
51
Jorim Jaggi275561a2016-02-23 10:11:02 -050052 private long mCurrentTransitionStartTime = INVALID_START_TIME;
53 private boolean mLoggedWindowsDrawn;
54 private boolean mLoggedStartingWindowDrawn;
55 private boolean mLoggedTransitionStarting;
56
Filip Gruszczynski77d94482015-12-11 13:59:52 -080057 ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context) {
58 mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000;
59 mSupervisor = supervisor;
60 mContext = context;
61 }
62
63 void logWindowState() {
64 final long now = SystemClock.elapsedRealtime() / 1000;
65 if (mWindowState != WINDOW_STATE_INVALID) {
66 // We log even if the window state hasn't changed, because the user might remain in
67 // home/fullscreen move forever and we would like to track this kind of behavior
68 // too.
69 MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState],
70 (int) (now - mLastLogTimeSecs));
71 }
72 mLastLogTimeSecs = now;
73
Filip Gruszczynski77d94482015-12-11 13:59:52 -080074 ActivityStack stack = mSupervisor.getStack(DOCKED_STACK_ID);
Wale Ogunwale8051c5c2016-03-04 10:27:32 -080075 if (stack != null && stack.getStackVisibilityLocked(null) != STACK_INVISIBLE) {
Filip Gruszczynski77d94482015-12-11 13:59:52 -080076 mWindowState = WINDOW_STATE_SIDE_BY_SIDE;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080077 return;
Filip Gruszczynski77d94482015-12-11 13:59:52 -080078 }
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080079 mWindowState = WINDOW_STATE_INVALID;
80 stack = mSupervisor.getFocusedStack();
81 if (stack.mStackId == PINNED_STACK_ID) {
82 stack = mSupervisor.findStackBehind(stack);
83 }
Matthew Ngae1ff4f2016-11-10 15:49:14 -080084 if (StackId.isHomeOrRecentsStack(stack.mStackId)
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080085 || stack.mStackId == FULLSCREEN_WORKSPACE_STACK_ID) {
86 mWindowState = WINDOW_STATE_STANDARD;
87 } else if (stack.mStackId == DOCKED_STACK_ID) {
Jorim Jaggif9704102016-05-05 19:14:22 -070088 Slog.wtf(TAG, "Docked stack shouldn't be the focused stack, because it reported not"
89 + " being visible.");
90 mWindowState = WINDOW_STATE_INVALID;
Filip Gruszczynskicaae14e2015-12-16 14:40:04 -080091 } else if (stack.mStackId == FREEFORM_WORKSPACE_STACK_ID) {
92 mWindowState = WINDOW_STATE_FREEFORM;
93 } else if (StackId.isStaticStack(stack.mStackId)) {
94 throw new IllegalStateException("Unknown stack=" + stack);
Filip Gruszczynski77d94482015-12-11 13:59:52 -080095 }
96 }
Jorim Jaggi275561a2016-02-23 10:11:02 -050097
98 /**
99 * Notifies the tracker at the earliest possible point when we are starting to launch an
100 * activity.
101 */
102 void notifyActivityLaunching() {
103 mCurrentTransitionStartTime = System.currentTimeMillis();
104 }
105
106 /**
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700107 * Notifies the tracker that the activity is actually launching.
108 *
109 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
110 * launch
111 * @param launchedActivity the activity that is being launched
112 */
113 void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity) {
114 final ProcessRecord processRecord = launchedActivity != null
115 ? mSupervisor.mService.mProcessNames.get(launchedActivity.processName,
116 launchedActivity.appInfo.uid)
117 : null;
118 final boolean processRunning = processRecord != null;
119 final String componentName = launchedActivity != null
120 ? launchedActivity.shortComponentName
121 : null;
122
123 // We consider this a "process switch" if the process of the activity that gets launched
124 // didn't have an activity that was in started state. In this case, we assume that lot
125 // of caches might be purged so the time until it produces the first frame is very
126 // interesting.
127 final boolean processSwitch = processRecord == null
128 || !hasStartedActivity(processRecord, launchedActivity);
129
130 notifyActivityLaunched(resultCode, componentName, processRunning, processSwitch);
131 }
132
133 private boolean hasStartedActivity(ProcessRecord record, ActivityRecord launchedActivity) {
134 final ArrayList<ActivityRecord> activities = record.activities;
135 for (int i = activities.size() - 1; i >= 0; i--) {
136 final ActivityRecord activity = activities.get(i);
137 if (launchedActivity == activity) {
138 continue;
139 }
140 if (!activity.stopped) {
141 return true;
142 }
143 }
144 return false;
145 }
146
147 /**
Jorim Jaggi275561a2016-02-23 10:11:02 -0500148 * Notifies the tracker the the activity is actually launching.
149 *
150 * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the
151 * launch
152 * @param componentName the component name of the activity being launched
153 * @param processRunning whether the process that will contains the activity is already running
Jorim Jaggibe67c902016-04-12 00:53:16 -0700154 * @param processSwitch whether the process that will contain the activity didn't have any
155 * activity that was stopped, i.e. the started activity is "switching"
156 * processes
Jorim Jaggi275561a2016-02-23 10:11:02 -0500157 */
Jorim Jaggi1e630c02016-05-16 12:13:13 -0700158 private void notifyActivityLaunched(int resultCode, @Nullable String componentName,
Jorim Jaggibe67c902016-04-12 00:53:16 -0700159 boolean processRunning, boolean processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500160
Jorim Jaggibe67c902016-04-12 00:53:16 -0700161 if (resultCode < 0 || componentName == null || !processSwitch) {
Jorim Jaggi275561a2016-02-23 10:11:02 -0500162
Jorim Jaggibe67c902016-04-12 00:53:16 -0700163 // 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 -0500164 reset();
165 return;
166 }
167
168 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_COMPONENT_NAME,
169 componentName);
170 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_PROCESS_RUNNING,
171 processRunning);
172 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS,
173 (int) (SystemClock.uptimeMillis() / 1000));
Alison Cichowlas803054d2016-12-13 14:38:01 -0500174
175 LogBuilder builder = new LogBuilder();
176 builder.addTaggedData(MetricsEvent.APP_TRANSITION_COMPONENT_NAME, componentName);
177 builder.addTaggedData(MetricsEvent.APP_TRANSITION_PROCESS_RUNNING, processRunning ? 1 : 0);
178 builder.addTaggedData(MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS,
179 SystemClock.uptimeMillis() / 1000);
180 MetricsLogger.action(builder);
Jorim Jaggi275561a2016-02-23 10:11:02 -0500181 }
182
183 /**
184 * Notifies the tracker that all windows of the app have been drawn.
185 */
186 void notifyWindowsDrawn() {
187 if (!isTransitionActive() || mLoggedWindowsDrawn) {
188 return;
189 }
190 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS,
191 calculateCurrentDelay());
192 mLoggedWindowsDrawn = true;
193 if (mLoggedTransitionStarting) {
194 reset();
195 }
196 }
197
198 /**
199 * Notifies the tracker that the starting window was drawn.
200 */
201 void notifyStartingWindowDrawn() {
202 if (!isTransitionActive() || mLoggedStartingWindowDrawn) {
203 return;
204 }
205 mLoggedStartingWindowDrawn = true;
206 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS,
207 calculateCurrentDelay());
208 }
209
210 /**
211 * Notifies the tracker that the app transition is starting.
212 *
213 * @param reason The reason why we started it. Must be on of
214 * ActivityManagerInternal.APP_TRANSITION_* reasons.
215 */
216 void notifyTransitionStarting(int reason) {
217 if (!isTransitionActive() || mLoggedTransitionStarting) {
218 return;
219 }
220 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_REASON, reason);
221 MetricsLogger.action(mContext, MetricsEvent.APP_TRANSITION_DELAY_MS,
222 calculateCurrentDelay());
223 mLoggedTransitionStarting = true;
224 if (mLoggedWindowsDrawn) {
225 reset();
226 }
227 }
228
229 private boolean isTransitionActive() {
230 return mCurrentTransitionStartTime != INVALID_START_TIME;
231 }
232
233 private void reset() {
234 mCurrentTransitionStartTime = INVALID_START_TIME;
235 mLoggedWindowsDrawn = false;
236 mLoggedTransitionStarting = false;
237 mLoggedStartingWindowDrawn = false;
238 }
239
240 private int calculateCurrentDelay() {
241
242 // Shouldn't take more than 25 days to launch an app, so int is fine here.
243 return (int) (System.currentTimeMillis() - mCurrentTransitionStartTime);
244 }
Filip Gruszczynski77d94482015-12-11 13:59:52 -0800245}