Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 1 | package com.android.server.wm; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 2 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 3 | import static android.app.ActivityManager.START_SUCCESS; |
| 4 | import static android.app.ActivityManager.START_TASK_TO_FRONT; |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 5 | import static android.app.ActivityManager.processStateAmToProto; |
Vishnu Nair | bb9ab4b | 2018-12-13 10:29:46 -0800 | [diff] [blame] | 6 | import static android.app.WaitResult.LAUNCH_STATE_COLD; |
| 7 | import static android.app.WaitResult.LAUNCH_STATE_HOT; |
| 8 | import static android.app.WaitResult.LAUNCH_STATE_WARM; |
Bryce Lee | 6c60509 | 2017-10-12 11:14:49 -0700 | [diff] [blame] | 9 | import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM; |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 10 | import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN; |
| 11 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 12 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY; |
Wale Ogunwale | 926aade | 2017-08-29 11:24:37 -0700 | [diff] [blame] | 13 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY; |
| 14 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 15 | |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 16 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_ACTIVITY_START; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 17 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION; |
Jorim Jaggi | 515dd68 | 2017-05-05 15:05:07 +0200 | [diff] [blame] | 18 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_BIND_APPLICATION_DELAY_MS; |
Todd Kennedy | 50d946c1 | 2017-03-17 13:55:38 -0700 | [diff] [blame] | 19 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_CALLING_PACKAGE_NAME; |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 20 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_CANCELLED; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 21 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DELAY_MS; |
| 22 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_DEVICE_UPTIME_SECONDS; |
Todd Kennedy | 50d946c1 | 2017-03-17 13:55:38 -0700 | [diff] [blame] | 23 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_IS_EPHEMERAL; |
Jorim Jaggi | 4d27b84 | 2017-08-17 17:22:26 +0200 | [diff] [blame] | 24 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_PROCESS_RUNNING; |
| 25 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN; |
| 26 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_REPORTED_DRAWN_MS; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 27 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_STARTING_WINDOW_DELAY_MS; |
| 28 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS; |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 29 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_PACKAGE_NAME; |
| 30 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_UID; |
| 31 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_UID_HAS_ANY_VISIBLE_WINDOW; |
| 32 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CALLING_UID_PROC_STATE; |
Jorim Jaggi | cdfc04e | 2017-04-28 19:06:24 +0200 | [diff] [blame] | 33 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_CLASS_NAME; |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 34 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_COMING_FROM_PENDING_INTENT; |
Todd Kennedy | 50d946c1 | 2017-03-17 13:55:38 -0700 | [diff] [blame] | 35 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_INSTANT_APP_LAUNCH_TOKEN; |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 36 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_INTENT_ACTION; |
| 37 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_CUR_PROC_STATE; |
| 38 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_CLIENT_ACTIVITIES; |
| 39 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_FOREGROUND_ACTIVITIES; |
| 40 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_FOREGROUND_SERVICES; |
| 41 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_OVERLAY_UI; |
| 42 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_HAS_TOP_UI; |
| 43 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_MILLIS_SINCE_FG_INTERACTION; |
| 44 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_MILLIS_SINCE_LAST_INTERACTION_EVENT; |
| 45 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_MILLIS_SINCE_UNIMPORTANT; |
| 46 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_PENDING_UI_CLEAN; |
| 47 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_PROCESS_RECORD_PROCESS_NAME; |
| 48 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_REAL_CALLING_UID; |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 49 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_REAL_CALLING_UID_HAS_ANY_VISIBLE_WINDOW; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 50 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_REAL_CALLING_UID_PROC_STATE; |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 51 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.FIELD_TARGET_SHORT_COMPONENT_NAME; |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 52 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PACKAGE_OPTIMIZATION_COMPILATION_FILTER; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 53 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PACKAGE_OPTIMIZATION_COMPILATION_REASON; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 54 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_COLD_LAUNCH; |
| 55 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_HOT_LAUNCH; |
Jorim Jaggi | 4d27b84 | 2017-08-17 17:22:26 +0200 | [diff] [blame] | 56 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE; |
| 57 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_REPORTED_DRAWN_WITH_BUNDLE; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 58 | import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.TYPE_TRANSITION_WARM_LAUNCH; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 59 | import static com.android.server.am.EventLogTags.AM_ACTIVITY_LAUNCH_TIME; |
Ng Zhi An | bbefdec | 2018-01-30 17:12:39 -0800 | [diff] [blame] | 60 | import static com.android.server.am.MemoryStatUtil.MemoryStat; |
Rajeev Kumar | bfcd920 | 2018-03-02 22:42:13 +0000 | [diff] [blame] | 61 | import static com.android.server.am.MemoryStatUtil.readMemoryStatFromFilesystem; |
Vishnu Nair | a62534b | 2018-11-09 09:13:22 -0800 | [diff] [blame] | 62 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_METRICS; |
| 63 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM; |
| 64 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 65 | import static com.android.server.wm.ActivityTaskManagerInternal.APP_TRANSITION_TIMEOUT; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 66 | |
Vishnu Nair | bb9ab4b | 2018-12-13 10:29:46 -0800 | [diff] [blame] | 67 | import android.app.WaitResult; |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 68 | import android.app.WindowConfiguration.WindowingMode; |
Michal Karpinski | 319069b | 2019-03-27 15:46:50 +0000 | [diff] [blame] | 69 | import android.content.ComponentName; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 70 | import android.content.Context; |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 71 | import android.content.Intent; |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 72 | import android.content.pm.ApplicationInfo; |
| 73 | import android.content.pm.dex.ArtManagerInternal; |
| 74 | import android.content.pm.dex.PackageOptimizationInfo; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 75 | import android.metrics.LogMaker; |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 76 | import android.os.Handler; |
| 77 | import android.os.Looper; |
| 78 | import android.os.Message; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 79 | import android.os.SystemClock; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 80 | import android.os.Trace; |
| 81 | import android.util.EventLog; |
| 82 | import android.util.Log; |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 83 | import android.util.Slog; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 84 | import android.util.SparseArray; |
| 85 | import android.util.SparseIntArray; |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 86 | import android.util.StatsLog; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 87 | import android.util.TimeUtils; |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 88 | import android.util.proto.ProtoOutputStream; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 89 | |
Vishnu Nair | bb9ab4b | 2018-12-13 10:29:46 -0800 | [diff] [blame] | 90 | import com.android.internal.annotations.VisibleForTesting; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 91 | import com.android.internal.logging.MetricsLogger; |
Ng Zhi An | 8347354 | 2018-02-20 09:02:14 -0800 | [diff] [blame] | 92 | import com.android.internal.os.BackgroundThread; |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 93 | import com.android.internal.os.SomeArgs; |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 94 | import com.android.server.LocalServices; |
Yan Wang | 9eac5ec | 2019-09-30 15:42:18 -0700 | [diff] [blame] | 95 | import java.util.concurrent.TimeUnit; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 98 | * 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. Takaoka | 28bc370 | 2019-04-23 11:01:48 +0900 | [diff] [blame] | 103 | * atest CtsWindowManagerDeviceTestCases:ActivityMetricsLoggerTests |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 104 | */ |
| 105 | class ActivityMetricsLogger { |
Jorim Jaggi | f970410 | 2016-05-05 19:14:22 -0700 | [diff] [blame] | 106 | |
Wale Ogunwale | 9887561 | 2018-10-12 07:53:02 -0700 | [diff] [blame] | 107 | private static final String TAG = TAG_WITH_CLASS_NAME ? "ActivityMetricsLogger" : TAG_ATM; |
Jorim Jaggi | f970410 | 2016-05-05 19:14:22 -0700 | [diff] [blame] | 108 | |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 109 | // 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 Chung | 8347163 | 2016-12-13 11:02:12 -0800 | [diff] [blame] | 114 | private static final int WINDOW_STATE_ASSISTANT = 3; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 115 | private static final int WINDOW_STATE_INVALID = -1; |
| 116 | |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 117 | private static final long INVALID_START_TIME = -1; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 118 | private static final int INVALID_DELAY = -1; |
| 119 | private static final int INVALID_TRANSITION_TYPE = -1; |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 120 | |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 121 | private static final int MSG_CHECK_VISIBILITY = 0; |
| 122 | |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 123 | // 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 Chung | 8347163 | 2016-12-13 11:02:12 -0800 | [diff] [blame] | 126 | "window_time_0", "window_time_1", "window_time_2", "window_time_3"}; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 127 | |
| 128 | private int mWindowState = WINDOW_STATE_STANDARD; |
| 129 | private long mLastLogTimeSecs; |
| 130 | private final ActivityStackSupervisor mSupervisor; |
| 131 | private final Context mContext; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 132 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 133 | |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 134 | // set to INVALID_START_TIME in reset. |
| 135 | // set to valid value in notifyActivityLaunching |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 136 | private long mCurrentTransitionStartTimeNs = INVALID_START_TIME; |
| 137 | private long mLastTransitionStartTimeNs = INVALID_START_TIME; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 138 | |
| 139 | private int mCurrentTransitionDeviceUptime; |
| 140 | private int mCurrentTransitionDelayMs; |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 141 | |
| 142 | /** If the any app transitions have been logged as starting, after the latest reset. */ |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 143 | private boolean mLoggedTransitionStarting; |
| 144 | |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 145 | /** Map : @WindowingMode int => WindowingModeTransitionInfo */ |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 146 | private final SparseArray<WindowingModeTransitionInfo> mWindowingModeTransitionInfo = |
| 147 | new SparseArray<>(); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 148 | /** Map : @WindowingMode int => WindowingModeTransitionInfo */ |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 149 | private final SparseArray<WindowingModeTransitionInfo> mLastWindowingModeTransitionInfo = |
| 150 | new SparseArray<>(); |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 151 | private final H mHandler; |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 152 | |
| 153 | private ArtManagerInternal mArtManagerInternal; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 154 | private final StringBuilder mStringBuilder = new StringBuilder(); |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 155 | |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 156 | /** |
| 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 Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 160 | private final LaunchObserverRegistryImpl mLaunchObserver; |
| 161 | @VisibleForTesting static final int LAUNCH_OBSERVER_ACTIVITY_RECORD_PROTO_CHUNK_SIZE = 512; |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 162 | |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 163 | 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 Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 178 | } |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 179 | |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 180 | private final class WindowingModeTransitionInfo { |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 181 | /** The latest activity to have been launched. */ |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 182 | private ActivityRecord launchedActivity; |
| 183 | private int startResult; |
| 184 | private boolean currentTransitionProcessRunning; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 185 | /** Elapsed time from when we launch an activity to when its windows are drawn. */ |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 186 | private int windowsDrawnDelayMs; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 187 | private int startingWindowDelayMs = INVALID_DELAY; |
| 188 | private int bindApplicationDelayMs = INVALID_DELAY; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 189 | private int reason = APP_TRANSITION_TIMEOUT; |
Riddle Hsu | 99f2acd | 2019-10-22 18:41:51 +0800 | [diff] [blame] | 190 | // TODO(b/132736359) The number may need to consider the visibility change. |
| 191 | private int numUndrawnActivities = 1; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 192 | private boolean loggedStartingWindowDrawn; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 193 | private boolean launchTraceActive; |
Riddle Hsu | 1c5d72e | 2019-10-09 20:54:26 +0800 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Remembers the latest launched activity to represent the final transition. This also |
| 197 | * increments the number of activities that should be drawn, so a consecutive launching |
| 198 | * sequence can be coalesced as one event. |
| 199 | */ |
| 200 | void setLatestLaunchedActivity(ActivityRecord r) { |
| 201 | if (launchedActivity == r) { |
| 202 | return; |
| 203 | } |
| 204 | launchedActivity = r; |
Riddle Hsu | 1c5d72e | 2019-10-09 20:54:26 +0800 | [diff] [blame] | 205 | } |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 208 | final class WindowingModeTransitionInfoSnapshot { |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 209 | final private ApplicationInfo applicationInfo; |
Wale Ogunwale | f673393 | 2018-06-27 05:14:34 -0700 | [diff] [blame] | 210 | final private WindowProcessController processRecord; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 211 | final String packageName; |
| 212 | final String launchedActivityName; |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 213 | final private String launchedActivityLaunchedFromPackage; |
| 214 | final private String launchedActivityLaunchToken; |
| 215 | final private String launchedActivityAppRecordRequiredAbi; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 216 | final String launchedActivityShortComponentName; |
Ng Zhi An | 8347354 | 2018-02-20 09:02:14 -0800 | [diff] [blame] | 217 | final private String processName; |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 218 | final private int reason; |
| 219 | final private int startingWindowDelayMs; |
| 220 | final private int bindApplicationDelayMs; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 221 | final int windowsDrawnDelayMs; |
| 222 | final int type; |
| 223 | final int userId; |
| 224 | /** |
| 225 | * Elapsed time from when we launch an activity to when the app reported it was |
| 226 | * fully drawn. If this is not reported then the value is set to INVALID_DELAY. |
| 227 | */ |
| 228 | final int windowsFullyDrawnDelayMs; |
| 229 | final int activityRecordIdHashCode; |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 230 | |
| 231 | private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info) { |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 232 | this(info, info.launchedActivity); |
| 233 | } |
| 234 | |
| 235 | private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info, |
| 236 | ActivityRecord launchedActivity) { |
| 237 | this(info, launchedActivity, INVALID_DELAY); |
| 238 | } |
| 239 | |
| 240 | private WindowingModeTransitionInfoSnapshot(WindowingModeTransitionInfo info, |
| 241 | ActivityRecord launchedActivity, int windowsFullyDrawnDelayMs) { |
Andrii Kulian | eceebbf | 2019-06-26 17:36:51 -0700 | [diff] [blame] | 242 | applicationInfo = launchedActivity.info.applicationInfo; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 243 | packageName = launchedActivity.packageName; |
| 244 | launchedActivityName = launchedActivity.info.name; |
| 245 | launchedActivityLaunchedFromPackage = launchedActivity.launchedFromPackage; |
| 246 | launchedActivityLaunchToken = launchedActivity.info.launchToken; |
| 247 | launchedActivityAppRecordRequiredAbi = launchedActivity.app == null |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 248 | ? null |
Vishnu Nair | af0ea31 | 2018-10-15 16:23:55 -0700 | [diff] [blame] | 249 | : launchedActivity.app.getRequiredAbi(); |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 250 | reason = info.reason; |
| 251 | startingWindowDelayMs = info.startingWindowDelayMs; |
| 252 | bindApplicationDelayMs = info.bindApplicationDelayMs; |
| 253 | windowsDrawnDelayMs = info.windowsDrawnDelayMs; |
| 254 | type = getTransitionType(info); |
Vishnu Nair | af0ea31 | 2018-10-15 16:23:55 -0700 | [diff] [blame] | 255 | processRecord = findProcessForActivity(launchedActivity); |
| 256 | processName = launchedActivity.processName; |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 257 | userId = launchedActivity.mUserId; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 258 | launchedActivityShortComponentName = launchedActivity.shortComponentName; |
| 259 | activityRecordIdHashCode = System.identityHashCode(launchedActivity); |
| 260 | this.windowsFullyDrawnDelayMs = windowsFullyDrawnDelayMs; |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 261 | } |
Vishnu Nair | bb9ab4b | 2018-12-13 10:29:46 -0800 | [diff] [blame] | 262 | |
| 263 | @WaitResult.LaunchState int getLaunchState() { |
| 264 | switch (type) { |
| 265 | case TYPE_TRANSITION_WARM_LAUNCH: |
| 266 | return LAUNCH_STATE_WARM; |
| 267 | case TYPE_TRANSITION_HOT_LAUNCH: |
| 268 | return LAUNCH_STATE_HOT; |
| 269 | case TYPE_TRANSITION_COLD_LAUNCH: |
| 270 | return LAUNCH_STATE_COLD; |
| 271 | default: |
| 272 | return -1; |
| 273 | } |
| 274 | } |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 277 | ActivityMetricsLogger(ActivityStackSupervisor supervisor, Context context, Looper looper) { |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 278 | mLastLogTimeSecs = SystemClock.elapsedRealtime() / 1000; |
| 279 | mSupervisor = supervisor; |
| 280 | mContext = context; |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 281 | mHandler = new H(looper); |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 282 | mLaunchObserver = new LaunchObserverRegistryImpl(looper); |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void logWindowState() { |
| 286 | final long now = SystemClock.elapsedRealtime() / 1000; |
| 287 | if (mWindowState != WINDOW_STATE_INVALID) { |
| 288 | // We log even if the window state hasn't changed, because the user might remain in |
| 289 | // home/fullscreen move forever and we would like to track this kind of behavior |
| 290 | // too. |
| 291 | MetricsLogger.count(mContext, TRON_WINDOW_STATE_VARZ_STRINGS[mWindowState], |
| 292 | (int) (now - mLastLogTimeSecs)); |
| 293 | } |
| 294 | mLastLogTimeSecs = now; |
| 295 | |
Wale Ogunwale | 926aade | 2017-08-29 11:24:37 -0700 | [diff] [blame] | 296 | mWindowState = WINDOW_STATE_INVALID; |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 297 | ActivityStack stack = |
| 298 | mSupervisor.mRootActivityContainer.getTopDisplayFocusedStack(); |
lumark | f6c4a98 | 2018-06-15 15:43:12 +0800 | [diff] [blame] | 299 | if (stack == null) { |
| 300 | return; |
| 301 | } |
| 302 | |
Wale Ogunwale | 926aade | 2017-08-29 11:24:37 -0700 | [diff] [blame] | 303 | if (stack.isActivityTypeAssistant()) { |
| 304 | mWindowState = WINDOW_STATE_ASSISTANT; |
Filip Gruszczynski | caae14e | 2015-12-16 14:40:04 -0800 | [diff] [blame] | 305 | return; |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 306 | } |
Wale Ogunwale | 926aade | 2017-08-29 11:24:37 -0700 | [diff] [blame] | 307 | |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 308 | @WindowingMode int windowingMode = stack.getWindowingMode(); |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 309 | if (windowingMode == WINDOWING_MODE_PINNED) { |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 310 | stack = mSupervisor.mRootActivityContainer.findStackBehind(stack); |
Wale Ogunwale | 3382ab1 | 2017-07-27 08:55:03 -0700 | [diff] [blame] | 311 | windowingMode = stack.getWindowingMode(); |
Filip Gruszczynski | caae14e | 2015-12-16 14:40:04 -0800 | [diff] [blame] | 312 | } |
Wale Ogunwale | 926aade | 2017-08-29 11:24:37 -0700 | [diff] [blame] | 313 | switch (windowingMode) { |
| 314 | case WINDOWING_MODE_FULLSCREEN: |
| 315 | mWindowState = WINDOW_STATE_STANDARD; |
| 316 | break; |
| 317 | case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: |
| 318 | case WINDOWING_MODE_SPLIT_SCREEN_SECONDARY: |
| 319 | mWindowState = WINDOW_STATE_SIDE_BY_SIDE; |
| 320 | break; |
Bryce Lee | 6c60509 | 2017-10-12 11:14:49 -0700 | [diff] [blame] | 321 | case WINDOWING_MODE_FREEFORM: |
Wale Ogunwale | 926aade | 2017-08-29 11:24:37 -0700 | [diff] [blame] | 322 | mWindowState = WINDOW_STATE_FREEFORM; |
| 323 | break; |
| 324 | default: |
| 325 | if (windowingMode != WINDOWING_MODE_UNDEFINED) { |
| 326 | throw new IllegalStateException("Unknown windowing mode for stack=" + stack |
| 327 | + " windowingMode=" + windowingMode); |
| 328 | } |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 329 | } |
| 330 | } |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 331 | |
| 332 | /** |
| 333 | * Notifies the tracker at the earliest possible point when we are starting to launch an |
| 334 | * activity. |
| 335 | */ |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 336 | void notifyActivityLaunching(Intent intent) { |
| 337 | if (DEBUG_METRICS) { |
| 338 | Slog.i(TAG, String.format("notifyActivityLaunching: active:%b, intent:%s", |
| 339 | isAnyTransitionActive(), |
| 340 | intent)); |
| 341 | } |
| 342 | |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 343 | if (mCurrentTransitionStartTimeNs == INVALID_START_TIME) { |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 344 | |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 345 | mCurrentTransitionStartTimeNs = SystemClock.elapsedRealtimeNanos(); |
| 346 | mLastTransitionStartTimeNs = mCurrentTransitionStartTimeNs; |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 347 | |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 348 | launchObserverNotifyIntentStarted(intent, mCurrentTransitionStartTimeNs); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 349 | } |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /** |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 353 | * Notifies the tracker that the activity is actually launching. |
| 354 | * |
| 355 | * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the |
| 356 | * launch |
| 357 | * @param launchedActivity the activity that is being launched |
| 358 | */ |
| 359 | void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity) { |
Wale Ogunwale | f673393 | 2018-06-27 05:14:34 -0700 | [diff] [blame] | 360 | final WindowProcessController processRecord = findProcessForActivity(launchedActivity); |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 361 | final boolean processRunning = processRecord != null; |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 362 | |
| 363 | // We consider this a "process switch" if the process of the activity that gets launched |
| 364 | // didn't have an activity that was in started state. In this case, we assume that lot |
| 365 | // of caches might be purged so the time until it produces the first frame is very |
| 366 | // interesting. |
| 367 | final boolean processSwitch = processRecord == null |
Wale Ogunwale | f673393 | 2018-06-27 05:14:34 -0700 | [diff] [blame] | 368 | || !processRecord.hasStartedActivity(launchedActivity); |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 369 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 370 | notifyActivityLaunched(resultCode, launchedActivity, processRunning, processSwitch); |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Jorim Jaggi | 1e630c0 | 2016-05-16 12:13:13 -0700 | [diff] [blame] | 373 | /** |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 374 | * Notifies the tracker the the activity is actually launching. |
| 375 | * |
| 376 | * @param resultCode one of the ActivityManager.START_* flags, indicating the result of the |
| 377 | * launch |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 378 | * @param launchedActivity the activity being launched |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 379 | * @param processRunning whether the process that will contains the activity is already running |
Jorim Jaggi | be67c90 | 2016-04-12 00:53:16 -0700 | [diff] [blame] | 380 | * @param processSwitch whether the process that will contain the activity didn't have any |
| 381 | * activity that was stopped, i.e. the started activity is "switching" |
| 382 | * processes |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 383 | */ |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 384 | private void notifyActivityLaunched(int resultCode, ActivityRecord launchedActivity, |
Jorim Jaggi | be67c90 | 2016-04-12 00:53:16 -0700 | [diff] [blame] | 385 | boolean processRunning, boolean processSwitch) { |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 386 | |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 387 | if (DEBUG_METRICS) Slog.i(TAG, "notifyActivityLaunched" |
| 388 | + " resultCode=" + resultCode |
| 389 | + " launchedActivity=" + launchedActivity |
| 390 | + " processRunning=" + processRunning |
| 391 | + " processSwitch=" + processSwitch); |
| 392 | |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 393 | // If we are already in an existing transition, only update the activity name, but not the |
| 394 | // other attributes. |
| 395 | final @WindowingMode int windowingMode = launchedActivity != null |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 396 | ? launchedActivity.getWindowingMode() |
| 397 | : WINDOWING_MODE_UNDEFINED; |
Vishnu Nair | f8accc5 | 2018-10-11 10:19:54 -0700 | [diff] [blame] | 398 | final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode); |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 399 | if (mCurrentTransitionStartTimeNs == INVALID_START_TIME) { |
Vishnu Nair | f8accc5 | 2018-10-11 10:19:54 -0700 | [diff] [blame] | 400 | // No transition is active ignore this launch. |
Jorim Jaggi | cdfc04e | 2017-04-28 19:06:24 +0200 | [diff] [blame] | 401 | return; |
| 402 | } |
| 403 | |
Vishnu Nair | 9ba3165 | 2018-11-13 14:34:05 -0800 | [diff] [blame] | 404 | if (launchedActivity != null && launchedActivity.mDrawn) { |
Vishnu Nair | f8accc5 | 2018-10-11 10:19:54 -0700 | [diff] [blame] | 405 | // Launched activity is already visible. We cannot measure windows drawn delay. |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 406 | abort(info, "launched activity already visible"); |
Vishnu Nair | f8accc5 | 2018-10-11 10:19:54 -0700 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 410 | if (launchedActivity != null && info != null) { |
Vishnu Nair | f8accc5 | 2018-10-11 10:19:54 -0700 | [diff] [blame] | 411 | // If we are already in an existing transition, only update the activity name, but not |
| 412 | // the other attributes. |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 413 | |
| 414 | // Coalesce multiple (trampoline) activities from a single sequence together. |
Riddle Hsu | 1c5d72e | 2019-10-09 20:54:26 +0800 | [diff] [blame] | 415 | info.setLatestLaunchedActivity(launchedActivity); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 416 | return; |
| 417 | } |
| 418 | |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 419 | final boolean otherWindowModesLaunching = |
| 420 | mWindowingModeTransitionInfo.size() > 0 && info == null; |
Jorim Jaggi | 54cff64 | 2018-03-15 15:51:32 +0100 | [diff] [blame] | 421 | if ((!isLoggableResultCode(resultCode) || launchedActivity == null || !processSwitch |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 422 | || windowingMode == WINDOWING_MODE_UNDEFINED) && !otherWindowModesLaunching) { |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 423 | // Failed to launch or it was not a process switch, so we don't care about the timing. |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 424 | abort(info, "failed to launch or not a process switch"); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 425 | return; |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 426 | } else if (otherWindowModesLaunching) { |
| 427 | // Don't log this windowing mode but continue with the other windowing modes. |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 428 | return; |
| 429 | } |
| 430 | |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 431 | if (DEBUG_METRICS) Slog.i(TAG, "notifyActivityLaunched successful"); |
| 432 | |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 433 | // A new launch sequence [with the windowingMode] has begun. |
| 434 | // Start tracking it. |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 435 | final WindowingModeTransitionInfo newInfo = new WindowingModeTransitionInfo(); |
Riddle Hsu | 1c5d72e | 2019-10-09 20:54:26 +0800 | [diff] [blame] | 436 | newInfo.setLatestLaunchedActivity(launchedActivity); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 437 | newInfo.currentTransitionProcessRunning = processRunning; |
| 438 | newInfo.startResult = resultCode; |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 439 | mWindowingModeTransitionInfo.put(windowingMode, newInfo); |
| 440 | mLastWindowingModeTransitionInfo.put(windowingMode, newInfo); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 441 | mCurrentTransitionDeviceUptime = (int) (SystemClock.uptimeMillis() / 1000); |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 442 | startTraces(newInfo); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 443 | launchObserverNotifyActivityLaunched(newInfo); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
Jorim Jaggi | 54cff64 | 2018-03-15 15:51:32 +0100 | [diff] [blame] | 447 | * @return True if we should start logging an event for an activity start that returned |
| 448 | * {@code resultCode} and that we'll indeed get a windows drawn event. |
| 449 | */ |
| 450 | private boolean isLoggableResultCode(int resultCode) { |
| 451 | return resultCode == START_SUCCESS || resultCode == START_TASK_TO_FRONT; |
| 452 | } |
| 453 | |
| 454 | /** |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 455 | * Notifies the tracker that all windows of the app have been drawn. |
| 456 | */ |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 457 | WindowingModeTransitionInfoSnapshot notifyWindowsDrawn(@WindowingMode int windowingMode, |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 458 | long timestampNs) { |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 459 | if (DEBUG_METRICS) Slog.i(TAG, "notifyWindowsDrawn windowingMode=" + windowingMode); |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 460 | |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 461 | final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode); |
Riddle Hsu | 1c5d72e | 2019-10-09 20:54:26 +0800 | [diff] [blame] | 462 | if (info == null || info.numUndrawnActivities == 0) { |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 463 | return null; |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 464 | } |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 465 | info.windowsDrawnDelayMs = calculateDelay(timestampNs); |
Riddle Hsu | 1c5d72e | 2019-10-09 20:54:26 +0800 | [diff] [blame] | 466 | info.numUndrawnActivities--; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 467 | final WindowingModeTransitionInfoSnapshot infoSnapshot = |
| 468 | new WindowingModeTransitionInfoSnapshot(info); |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 469 | if (allWindowsDrawn() && mLoggedTransitionStarting) { |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 470 | reset(false /* abort */, info, "notifyWindowsDrawn - all windows drawn", timestampNs); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 471 | } |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 472 | return infoSnapshot; |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Notifies the tracker that the starting window was drawn. |
| 477 | */ |
Yan Wang | b0b2006 | 2019-10-30 12:47:09 -0700 | [diff] [blame] | 478 | void notifyStartingWindowDrawn(@WindowingMode int windowingMode, long timestampNs) { |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 479 | final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get(windowingMode); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 480 | if (info == null || info.loggedStartingWindowDrawn) { |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 481 | return; |
| 482 | } |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 483 | info.loggedStartingWindowDrawn = true; |
Yan Wang | b0b2006 | 2019-10-30 12:47:09 -0700 | [diff] [blame] | 484 | info.startingWindowDelayMs = calculateDelay(timestampNs); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Notifies the tracker that the app transition is starting. |
| 489 | * |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 490 | * @param windowingModeToReason A map from windowing mode to a reason integer, which must be on |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 491 | * of ActivityTaskManagerInternal.APP_TRANSITION_* reasons. |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 492 | */ |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 493 | void notifyTransitionStarting(SparseIntArray windowingModeToReason, long timestampNs) { |
Jorim Jaggi | d8a5777 | 2017-04-14 16:50:42 -0700 | [diff] [blame] | 494 | if (!isAnyTransitionActive() || mLoggedTransitionStarting) { |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 495 | // Ignore calls to this made after a reset and prior to notifyActivityLaunching. |
| 496 | |
| 497 | // Ignore any subsequent notifyTransitionStarting until the next reset. |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 498 | return; |
| 499 | } |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 500 | if (DEBUG_METRICS) Slog.i(TAG, "notifyTransitionStarting"); |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 501 | mCurrentTransitionDelayMs = calculateDelay(timestampNs); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 502 | mLoggedTransitionStarting = true; |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 503 | |
| 504 | WindowingModeTransitionInfo foundInfo = null; |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 505 | for (int index = windowingModeToReason.size() - 1; index >= 0; index--) { |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 506 | final @WindowingMode int windowingMode = windowingModeToReason.keyAt(index); |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 507 | final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get( |
| 508 | windowingMode); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 509 | if (info == null) { |
| 510 | continue; |
| 511 | } |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 512 | info.reason = windowingModeToReason.valueAt(index); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 513 | foundInfo = info; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 514 | } |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 515 | if (allWindowsDrawn()) { |
Vishnu Nair | a62534b | 2018-11-09 09:13:22 -0800 | [diff] [blame] | 516 | // abort metrics collection if we cannot find a matching transition. |
| 517 | final boolean abortMetrics = foundInfo == null; |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 518 | reset(abortMetrics, foundInfo, "notifyTransitionStarting - all windows drawn", |
| 519 | timestampNs /* timestampNs */); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Jorim Jaggi | cdfc04e | 2017-04-28 19:06:24 +0200 | [diff] [blame] | 523 | /** |
| 524 | * Notifies the tracker that the visibility of an app is changing. |
| 525 | * |
| 526 | * @param activityRecord the app that is changing its visibility |
Jorim Jaggi | cdfc04e | 2017-04-28 19:06:24 +0200 | [diff] [blame] | 527 | */ |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 528 | void notifyVisibilityChanged(ActivityRecord activityRecord) { |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 529 | final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get( |
| 530 | activityRecord.getWindowingMode()); |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 531 | if (info == null) { |
| 532 | return; |
| 533 | } |
| 534 | if (info.launchedActivity != activityRecord) { |
| 535 | return; |
| 536 | } |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 537 | final TaskRecord t = activityRecord.getTaskRecord(); |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 538 | final SomeArgs args = SomeArgs.obtain(); |
| 539 | args.arg1 = t; |
| 540 | args.arg2 = activityRecord; |
| 541 | mHandler.obtainMessage(MSG_CHECK_VISIBILITY, args).sendToTarget(); |
| 542 | } |
Jorim Jaggi | cdfc04e | 2017-04-28 19:06:24 +0200 | [diff] [blame] | 543 | |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 544 | /** @return {@code true} if the given task has an activity will be drawn. */ |
| 545 | private static boolean hasActivityToBeDrawn(TaskRecord t) { |
Wale Ogunwale | 1a06f15 | 2019-10-11 11:26:30 +0200 | [diff] [blame] | 546 | for (int i = t.getChildCount() - 1; i >= 0; --i) { |
| 547 | final ActivityRecord r = t.getChildAt(i); |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 548 | if (r.visible && !r.mDrawn && !r.finishing) { |
Vishnu Nair | b1de42d | 2019-01-28 09:49:51 -0800 | [diff] [blame] | 549 | return true; |
| 550 | } |
| 551 | } |
| 552 | return false; |
| 553 | } |
| 554 | |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 555 | private void checkVisibility(TaskRecord t, ActivityRecord r) { |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 556 | synchronized (mSupervisor.mService.mGlobalLock) { |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 557 | |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 558 | final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.get( |
| 559 | r.getWindowingMode()); |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 560 | |
| 561 | // If we have an active transition that's waiting on a certain activity that will be |
| 562 | // invisible now, we'll never get onWindowsDrawn, so abort the transition if necessary. |
Vishnu Nair | d15063b | 2019-06-11 08:08:25 -0700 | [diff] [blame] | 563 | |
| 564 | // We have no active transitions. |
| 565 | if (info == null) { |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | // The notified activity whose visibility changed is no longer the launched activity. |
| 570 | // We can still wait to get onWindowsDrawn. |
| 571 | if (info.launchedActivity != r) { |
| 572 | return; |
| 573 | } |
| 574 | |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 575 | // If the task of the launched activity contains any activity to be drawn, then the |
| 576 | // window drawn event should report later to complete the transition. Otherwise all |
| 577 | // activities in this task may be finished, invisible or drawn, so the transition event |
| 578 | // should be cancelled. |
| 579 | if (hasActivityToBeDrawn(t)) { |
Vishnu Nair | d15063b | 2019-06-11 08:08:25 -0700 | [diff] [blame] | 580 | return; |
| 581 | } |
| 582 | |
| 583 | if (DEBUG_METRICS) Slog.i(TAG, "notifyVisibilityChanged to invisible activity=" + r); |
| 584 | logAppTransitionCancel(info); |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 585 | // Abort if this is the only one active transition. |
| 586 | if (mWindowingModeTransitionInfo.size() == 1 |
| 587 | && mWindowingModeTransitionInfo.get(r.getWindowingMode()) != null) { |
| 588 | abort(info, "notifyVisibilityChanged to invisible"); |
Jorim Jaggi | cdfc04e | 2017-04-28 19:06:24 +0200 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
Jorim Jaggi | 515dd68 | 2017-05-05 15:05:07 +0200 | [diff] [blame] | 593 | /** |
| 594 | * Notifies the tracker that we called immediately before we call bindApplication on the client. |
| 595 | * |
Wale Ogunwale | 31913b5 | 2018-10-13 08:29:31 -0700 | [diff] [blame] | 596 | * @param appInfo The client into which we'll call bindApplication. |
Jorim Jaggi | 515dd68 | 2017-05-05 15:05:07 +0200 | [diff] [blame] | 597 | */ |
Wale Ogunwale | 31913b5 | 2018-10-13 08:29:31 -0700 | [diff] [blame] | 598 | void notifyBindApplication(ApplicationInfo appInfo) { |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 599 | for (int i = mWindowingModeTransitionInfo.size() - 1; i >= 0; i--) { |
| 600 | final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.valueAt(i); |
Jorim Jaggi | 515dd68 | 2017-05-05 15:05:07 +0200 | [diff] [blame] | 601 | |
| 602 | // App isn't attached to record yet, so match with info. |
Andrii Kulian | eceebbf | 2019-06-26 17:36:51 -0700 | [diff] [blame] | 603 | if (info.launchedActivity.info.applicationInfo == appInfo) { |
Jorim Jaggi | 515dd68 | 2017-05-05 15:05:07 +0200 | [diff] [blame] | 604 | info.bindApplicationDelayMs = calculateCurrentDelay(); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Riddle Hsu | 1c5d72e | 2019-10-09 20:54:26 +0800 | [diff] [blame] | 609 | @VisibleForTesting |
| 610 | boolean allWindowsDrawn() { |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 611 | for (int index = mWindowingModeTransitionInfo.size() - 1; index >= 0; index--) { |
Riddle Hsu | 1c5d72e | 2019-10-09 20:54:26 +0800 | [diff] [blame] | 612 | if (mWindowingModeTransitionInfo.valueAt(index).numUndrawnActivities != 0) { |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 613 | return false; |
| 614 | } |
| 615 | } |
| 616 | return true; |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 617 | } |
| 618 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 619 | private boolean isAnyTransitionActive() { |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 620 | return mCurrentTransitionStartTimeNs != INVALID_START_TIME |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 621 | && mWindowingModeTransitionInfo.size() > 0; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 624 | /** Aborts tracking of current launch metrics. */ |
| 625 | private void abort(WindowingModeTransitionInfo info, String cause) { |
| 626 | reset(true /* abort */, info, cause, 0L /* timestampNs */); |
| 627 | } |
| 628 | |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 629 | private void reset(boolean abort, WindowingModeTransitionInfo info, String cause, |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 630 | long timestampNs) { |
| 631 | final boolean isAnyTransitionActive = isAnyTransitionActive(); |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 632 | if (DEBUG_METRICS) { |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 633 | Slog.i(TAG, "reset abort=" + abort + " cause=" + cause + " timestamp=" + timestampNs |
| 634 | + " active=" + isAnyTransitionActive); |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 635 | } |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 636 | if (!abort && isAnyTransitionActive) { |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 637 | logAppTransitionMultiEvents(); |
| 638 | } |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 639 | stopLaunchTrace(info); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 640 | |
| 641 | // Ignore reset-after reset. |
Riddle Hsu | 1063b51 | 2019-10-22 21:12:23 +0800 | [diff] [blame] | 642 | if (isAnyTransitionActive) { |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 643 | // LaunchObserver callbacks. |
| 644 | if (abort) { |
| 645 | launchObserverNotifyActivityLaunchCancelled(info); |
| 646 | } else { |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 647 | launchObserverNotifyActivityLaunchFinished(info, timestampNs); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 648 | } |
| 649 | } else { |
| 650 | launchObserverNotifyIntentFailed(); |
| 651 | } |
| 652 | |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 653 | mCurrentTransitionStartTimeNs = INVALID_START_TIME; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 654 | mCurrentTransitionDelayMs = INVALID_DELAY; |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 655 | mLoggedTransitionStarting = false; |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 656 | mWindowingModeTransitionInfo.clear(); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | private int calculateCurrentDelay() { |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 660 | // Shouldn't take more than 25 days to launch an app, so int is fine here. |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 661 | return (int) TimeUnit.NANOSECONDS |
| 662 | .toMillis(SystemClock.elapsedRealtimeNanos() - mCurrentTransitionStartTimeNs); |
Jorim Jaggi | 275561a | 2016-02-23 10:11:02 -0500 | [diff] [blame] | 663 | } |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 664 | |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 665 | private int calculateDelay(long timestampNs) { |
Sudheer Shanka | c766db0 | 2017-06-12 10:37:29 -0700 | [diff] [blame] | 666 | // Shouldn't take more than 25 days to launch an app, so int is fine here. |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 667 | return (int) TimeUnit.NANOSECONDS.toMillis(timestampNs - |
| 668 | mCurrentTransitionStartTimeNs); |
Sudheer Shanka | c766db0 | 2017-06-12 10:37:29 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 671 | private void logAppTransitionCancel(WindowingModeTransitionInfo info) { |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 672 | final int type = getTransitionType(info); |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 673 | if (type == INVALID_TRANSITION_TYPE) { |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 674 | return; |
| 675 | } |
| 676 | final LogMaker builder = new LogMaker(APP_TRANSITION_CANCELLED); |
| 677 | builder.setPackageName(info.launchedActivity.packageName); |
| 678 | builder.setType(type); |
| 679 | builder.addTaggedData(FIELD_CLASS_NAME, info.launchedActivity.info.name); |
| 680 | mMetricsLogger.write(builder); |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 681 | StatsLog.write( |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 682 | StatsLog.APP_START_CANCELED, |
Andrii Kulian | eceebbf | 2019-06-26 17:36:51 -0700 | [diff] [blame] | 683 | info.launchedActivity.info.applicationInfo.uid, |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 684 | info.launchedActivity.packageName, |
| 685 | convertAppStartTransitionType(type), |
| 686 | info.launchedActivity.info.name); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 687 | if (DEBUG_METRICS) { |
| 688 | Slog.i(TAG, String.format("APP_START_CANCELED(%s, %s, %s, %s)", |
Andrii Kulian | eceebbf | 2019-06-26 17:36:51 -0700 | [diff] [blame] | 689 | info.launchedActivity.info.applicationInfo.uid, |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 690 | info.launchedActivity.packageName, |
| 691 | convertAppStartTransitionType(type), |
| 692 | info.launchedActivity.info.name)); |
| 693 | } |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 696 | private void logAppTransitionMultiEvents() { |
Jorim Jaggi | 172e99f | 2017-10-20 14:33:18 +0200 | [diff] [blame] | 697 | if (DEBUG_METRICS) Slog.i(TAG, "logging transition events"); |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 698 | for (int index = mWindowingModeTransitionInfo.size() - 1; index >= 0; index--) { |
| 699 | final WindowingModeTransitionInfo info = mWindowingModeTransitionInfo.valueAt(index); |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 700 | final int type = getTransitionType(info); |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 701 | if (type == INVALID_TRANSITION_TYPE) { |
Vishnu Nair | 194c64f | 2019-04-12 13:51:40 -0700 | [diff] [blame] | 702 | if (DEBUG_METRICS) { |
| 703 | Slog.i(TAG, "invalid transition type" |
| 704 | + " processRunning=" + info.currentTransitionProcessRunning |
| 705 | + " startResult=" + info.startResult); |
| 706 | } |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 707 | return; |
| 708 | } |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 709 | |
| 710 | // Take a snapshot of the transition info before sending it to the handler for logging. |
| 711 | // This will avoid any races with other operations that modify the ActivityRecord. |
| 712 | final WindowingModeTransitionInfoSnapshot infoSnapshot = |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 713 | new WindowingModeTransitionInfoSnapshot(info); |
Ng Zhi An | 8347354 | 2018-02-20 09:02:14 -0800 | [diff] [blame] | 714 | final int currentTransitionDeviceUptime = mCurrentTransitionDeviceUptime; |
| 715 | final int currentTransitionDelayMs = mCurrentTransitionDelayMs; |
| 716 | BackgroundThread.getHandler().post(() -> logAppTransition( |
| 717 | currentTransitionDeviceUptime, currentTransitionDelayMs, infoSnapshot)); |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 718 | BackgroundThread.getHandler().post(() -> logAppDisplayed(infoSnapshot)); |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 719 | |
| 720 | info.launchedActivity.info.launchToken = null; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Ng Zhi An | 8347354 | 2018-02-20 09:02:14 -0800 | [diff] [blame] | 724 | // This gets called on a background thread without holding the activity manager lock. |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 725 | private void logAppTransition(int currentTransitionDeviceUptime, int currentTransitionDelayMs, |
| 726 | WindowingModeTransitionInfoSnapshot info) { |
| 727 | final LogMaker builder = new LogMaker(APP_TRANSITION); |
| 728 | builder.setPackageName(info.packageName); |
| 729 | builder.setType(info.type); |
| 730 | builder.addTaggedData(FIELD_CLASS_NAME, info.launchedActivityName); |
| 731 | final boolean isInstantApp = info.applicationInfo.isInstantApp(); |
| 732 | if (info.launchedActivityLaunchedFromPackage != null) { |
| 733 | builder.addTaggedData(APP_TRANSITION_CALLING_PACKAGE_NAME, |
| 734 | info.launchedActivityLaunchedFromPackage); |
| 735 | } |
| 736 | String launchToken = info.launchedActivityLaunchToken; |
| 737 | if (launchToken != null) { |
| 738 | builder.addTaggedData(FIELD_INSTANT_APP_LAUNCH_TOKEN, launchToken); |
| 739 | } |
| 740 | builder.addTaggedData(APP_TRANSITION_IS_EPHEMERAL, isInstantApp ? 1 : 0); |
| 741 | builder.addTaggedData(APP_TRANSITION_DEVICE_UPTIME_SECONDS, |
| 742 | currentTransitionDeviceUptime); |
| 743 | builder.addTaggedData(APP_TRANSITION_DELAY_MS, currentTransitionDelayMs); |
| 744 | builder.setSubtype(info.reason); |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 745 | if (info.startingWindowDelayMs != INVALID_DELAY) { |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 746 | builder.addTaggedData(APP_TRANSITION_STARTING_WINDOW_DELAY_MS, |
| 747 | info.startingWindowDelayMs); |
| 748 | } |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 749 | if (info.bindApplicationDelayMs != INVALID_DELAY) { |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 750 | builder.addTaggedData(APP_TRANSITION_BIND_APPLICATION_DELAY_MS, |
| 751 | info.bindApplicationDelayMs); |
| 752 | } |
| 753 | builder.addTaggedData(APP_TRANSITION_WINDOWS_DRAWN_DELAY_MS, info.windowsDrawnDelayMs); |
| 754 | final ArtManagerInternal artManagerInternal = getArtManagerInternal(); |
| 755 | final PackageOptimizationInfo packageOptimizationInfo = |
| 756 | (artManagerInternal == null) || (info.launchedActivityAppRecordRequiredAbi == null) |
| 757 | ? PackageOptimizationInfo.createWithNoInfo() |
| 758 | : artManagerInternal.getPackageOptimizationInfo( |
| 759 | info.applicationInfo, |
| 760 | info.launchedActivityAppRecordRequiredAbi); |
| 761 | builder.addTaggedData(PACKAGE_OPTIMIZATION_COMPILATION_REASON, |
| 762 | packageOptimizationInfo.getCompilationReason()); |
| 763 | builder.addTaggedData(PACKAGE_OPTIMIZATION_COMPILATION_FILTER, |
| 764 | packageOptimizationInfo.getCompilationFilter()); |
| 765 | mMetricsLogger.write(builder); |
| 766 | StatsLog.write( |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 767 | StatsLog.APP_START_OCCURRED, |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 768 | info.applicationInfo.uid, |
| 769 | info.packageName, |
| 770 | convertAppStartTransitionType(info.type), |
| 771 | info.launchedActivityName, |
| 772 | info.launchedActivityLaunchedFromPackage, |
| 773 | isInstantApp, |
| 774 | currentTransitionDeviceUptime * 1000, |
| 775 | info.reason, |
| 776 | currentTransitionDelayMs, |
| 777 | info.startingWindowDelayMs, |
| 778 | info.bindApplicationDelayMs, |
| 779 | info.windowsDrawnDelayMs, |
| 780 | launchToken, |
| 781 | packageOptimizationInfo.getCompilationReason(), |
| 782 | packageOptimizationInfo.getCompilationFilter()); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 783 | |
| 784 | if (DEBUG_METRICS) { |
| 785 | Slog.i(TAG, String.format("APP_START_OCCURRED(%s, %s, %s, %s, %s)", |
| 786 | info.applicationInfo.uid, |
| 787 | info.packageName, |
| 788 | convertAppStartTransitionType(info.type), |
| 789 | info.launchedActivityName, |
| 790 | info.launchedActivityLaunchedFromPackage)); |
| 791 | } |
| 792 | |
| 793 | |
Ng Zhi An | 8347354 | 2018-02-20 09:02:14 -0800 | [diff] [blame] | 794 | logAppStartMemoryStateCapture(info); |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 797 | private void logAppDisplayed(WindowingModeTransitionInfoSnapshot info) { |
| 798 | if (info.type != TYPE_TRANSITION_WARM_LAUNCH && info.type != TYPE_TRANSITION_COLD_LAUNCH) { |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | EventLog.writeEvent(AM_ACTIVITY_LAUNCH_TIME, |
| 803 | info.userId, info.activityRecordIdHashCode, info.launchedActivityShortComponentName, |
| 804 | info.windowsDrawnDelayMs); |
| 805 | |
| 806 | StringBuilder sb = mStringBuilder; |
| 807 | sb.setLength(0); |
| 808 | sb.append("Displayed "); |
| 809 | sb.append(info.launchedActivityShortComponentName); |
| 810 | sb.append(": "); |
| 811 | TimeUtils.formatDuration(info.windowsDrawnDelayMs, sb); |
| 812 | Log.i(TAG, sb.toString()); |
| 813 | } |
| 814 | |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 815 | private int convertAppStartTransitionType(int tronType) { |
| 816 | if (tronType == TYPE_TRANSITION_COLD_LAUNCH) { |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 817 | return StatsLog.APP_START_OCCURRED__TYPE__COLD; |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 818 | } |
| 819 | if (tronType == TYPE_TRANSITION_WARM_LAUNCH) { |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 820 | return StatsLog.APP_START_OCCURRED__TYPE__WARM; |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 821 | } |
| 822 | if (tronType == TYPE_TRANSITION_HOT_LAUNCH) { |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 823 | return StatsLog.APP_START_OCCURRED__TYPE__HOT; |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 824 | } |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 825 | return StatsLog.APP_START_OCCURRED__TYPE__UNKNOWN; |
Riddle Hsu | c48c891 | 2019-10-31 13:34:27 +0800 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | /** @return the last known window drawn delay of the given windowing mode. */ |
| 829 | int getLastDrawnDelayMs(@WindowingMode int windowingMode) { |
| 830 | final WindowingModeTransitionInfo info = mLastWindowingModeTransitionInfo.get( |
| 831 | windowingMode); |
| 832 | return info != null ? info.windowsDrawnDelayMs : INVALID_DELAY; |
| 833 | } |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 834 | |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 835 | WindowingModeTransitionInfoSnapshot logAppTransitionReportedDrawn(ActivityRecord r, |
| 836 | boolean restoredFromBundle) { |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 837 | final WindowingModeTransitionInfo info = mLastWindowingModeTransitionInfo.get( |
| 838 | r.getWindowingMode()); |
Jorim Jaggi | 4d27b84 | 2017-08-17 17:22:26 +0200 | [diff] [blame] | 839 | if (info == null) { |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 840 | return null; |
Jorim Jaggi | 4d27b84 | 2017-08-17 17:22:26 +0200 | [diff] [blame] | 841 | } |
Chris Wailes | 35c193c | 2018-10-09 18:12:00 -0700 | [diff] [blame] | 842 | |
| 843 | // Record the handling of the reportFullyDrawn callback in the trace system. This is not |
| 844 | // actually used to trace this function, but instead the logical task that this function |
| 845 | // fullfils (handling reportFullyDrawn() callbacks). |
| 846 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 847 | "ActivityManager:ReportingFullyDrawn " + info.launchedActivity.packageName); |
| 848 | |
Jorim Jaggi | 4d27b84 | 2017-08-17 17:22:26 +0200 | [diff] [blame] | 849 | final LogMaker builder = new LogMaker(APP_TRANSITION_REPORTED_DRAWN); |
| 850 | builder.setPackageName(r.packageName); |
| 851 | builder.addTaggedData(FIELD_CLASS_NAME, r.info.name); |
Yan Wang | 9eac5ec | 2019-09-30 15:42:18 -0700 | [diff] [blame] | 852 | long currentTimestampNs = SystemClock.elapsedRealtimeNanos(); |
| 853 | long startupTimeMs = |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 854 | TimeUnit.NANOSECONDS.toMillis(currentTimestampNs - mLastTransitionStartTimeNs); |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 855 | builder.addTaggedData(APP_TRANSITION_REPORTED_DRAWN_MS, startupTimeMs); |
Jorim Jaggi | 4d27b84 | 2017-08-17 17:22:26 +0200 | [diff] [blame] | 856 | builder.setType(restoredFromBundle |
| 857 | ? TYPE_TRANSITION_REPORTED_DRAWN_WITH_BUNDLE |
| 858 | : TYPE_TRANSITION_REPORTED_DRAWN_NO_BUNDLE); |
| 859 | builder.addTaggedData(APP_TRANSITION_PROCESS_RUNNING, |
| 860 | info.currentTransitionProcessRunning ? 1 : 0); |
| 861 | mMetricsLogger.write(builder); |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 862 | StatsLog.write( |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 863 | StatsLog.APP_START_FULLY_DRAWN, |
Andrii Kulian | eceebbf | 2019-06-26 17:36:51 -0700 | [diff] [blame] | 864 | info.launchedActivity.info.applicationInfo.uid, |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 865 | info.launchedActivity.packageName, |
| 866 | restoredFromBundle |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 867 | ? StatsLog.APP_START_FULLY_DRAWN__TYPE__WITH_BUNDLE |
| 868 | : StatsLog.APP_START_FULLY_DRAWN__TYPE__WITHOUT_BUNDLE, |
Olivier Gaillard | aed7f12 | 2017-12-12 14:26:22 +0000 | [diff] [blame] | 869 | info.launchedActivity.info.name, |
| 870 | info.currentTransitionProcessRunning, |
| 871 | startupTimeMs); |
Chris Wailes | 35c193c | 2018-10-09 18:12:00 -0700 | [diff] [blame] | 872 | |
| 873 | // Ends the trace started at the beginning of this function. This is located here to allow |
| 874 | // the trace slice to have a noticable duration. |
| 875 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 876 | |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 877 | final WindowingModeTransitionInfoSnapshot infoSnapshot = |
| 878 | new WindowingModeTransitionInfoSnapshot(info, r, (int) startupTimeMs); |
| 879 | BackgroundThread.getHandler().post(() -> logAppFullyDrawn(infoSnapshot)); |
Yan Wang | 9eac5ec | 2019-09-30 15:42:18 -0700 | [diff] [blame] | 880 | |
| 881 | // Notify reportFullyDrawn event. |
| 882 | launchObserverNotifyReportFullyDrawn(r, currentTimestampNs); |
| 883 | |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 884 | return infoSnapshot; |
| 885 | } |
| 886 | |
| 887 | private void logAppFullyDrawn(WindowingModeTransitionInfoSnapshot info) { |
| 888 | if (info.type != TYPE_TRANSITION_WARM_LAUNCH && info.type != TYPE_TRANSITION_COLD_LAUNCH) { |
| 889 | return; |
| 890 | } |
| 891 | |
| 892 | StringBuilder sb = mStringBuilder; |
| 893 | sb.setLength(0); |
| 894 | sb.append("Fully drawn "); |
| 895 | sb.append(info.launchedActivityShortComponentName); |
| 896 | sb.append(": "); |
| 897 | TimeUtils.formatDuration(info.windowsFullyDrawnDelayMs, sb); |
| 898 | Log.i(TAG, sb.toString()); |
Jorim Jaggi | 4d27b84 | 2017-08-17 17:22:26 +0200 | [diff] [blame] | 899 | } |
| 900 | |
Michal Karpinski | 4fd5b84 | 2019-01-28 15:13:32 +0000 | [diff] [blame] | 901 | void logAbortedBgActivityStart(Intent intent, WindowProcessController callerApp, |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 902 | int callingUid, String callingPackage, int callingUidProcState, |
| 903 | boolean callingUidHasAnyVisibleWindow, |
| 904 | int realCallingUid, int realCallingUidProcState, |
| 905 | boolean realCallingUidHasAnyVisibleWindow, |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 906 | boolean comingFromPendingIntent) { |
| 907 | |
| 908 | final long nowElapsed = SystemClock.elapsedRealtime(); |
| 909 | final long nowUptime = SystemClock.uptimeMillis(); |
| 910 | final LogMaker builder = new LogMaker(ACTION_ACTIVITY_START); |
| 911 | builder.setTimestamp(System.currentTimeMillis()); |
| 912 | builder.addTaggedData(FIELD_CALLING_UID, callingUid); |
| 913 | builder.addTaggedData(FIELD_CALLING_PACKAGE_NAME, callingPackage); |
| 914 | builder.addTaggedData(FIELD_CALLING_UID_PROC_STATE, |
| 915 | processStateAmToProto(callingUidProcState)); |
| 916 | builder.addTaggedData(FIELD_CALLING_UID_HAS_ANY_VISIBLE_WINDOW, |
| 917 | callingUidHasAnyVisibleWindow ? 1 : 0); |
| 918 | builder.addTaggedData(FIELD_REAL_CALLING_UID, realCallingUid); |
| 919 | builder.addTaggedData(FIELD_REAL_CALLING_UID_PROC_STATE, |
| 920 | processStateAmToProto(realCallingUidProcState)); |
| 921 | builder.addTaggedData(FIELD_REAL_CALLING_UID_HAS_ANY_VISIBLE_WINDOW, |
| 922 | realCallingUidHasAnyVisibleWindow ? 1 : 0); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 923 | builder.addTaggedData(FIELD_COMING_FROM_PENDING_INTENT, comingFromPendingIntent ? 1 : 0); |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 924 | if (intent != null) { |
| 925 | builder.addTaggedData(FIELD_INTENT_ACTION, intent.getAction()); |
Michal Karpinski | 319069b | 2019-03-27 15:46:50 +0000 | [diff] [blame] | 926 | ComponentName component = intent.getComponent(); |
| 927 | if (component != null) { |
| 928 | builder.addTaggedData(FIELD_TARGET_SHORT_COMPONENT_NAME, |
| 929 | component.flattenToShortString()); |
| 930 | } |
Michal Karpinski | 8596ded | 2018-11-14 14:43:48 +0000 | [diff] [blame] | 931 | } |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 932 | if (callerApp != null) { |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 933 | builder.addTaggedData(FIELD_PROCESS_RECORD_PROCESS_NAME, callerApp.mName); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 934 | builder.addTaggedData(FIELD_PROCESS_RECORD_CUR_PROC_STATE, |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 935 | processStateAmToProto(callerApp.getCurrentProcState())); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 936 | builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_CLIENT_ACTIVITIES, |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 937 | callerApp.hasClientActivities() ? 1 : 0); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 938 | builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_FOREGROUND_SERVICES, |
| 939 | callerApp.hasForegroundServices() ? 1 : 0); |
| 940 | builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_FOREGROUND_ACTIVITIES, |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 941 | callerApp.hasForegroundActivities() ? 1 : 0); |
| 942 | builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_TOP_UI, callerApp.hasTopUi() ? 1 : 0); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 943 | builder.addTaggedData(FIELD_PROCESS_RECORD_HAS_OVERLAY_UI, |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 944 | callerApp.hasOverlayUi() ? 1 : 0); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 945 | builder.addTaggedData(FIELD_PROCESS_RECORD_PENDING_UI_CLEAN, |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 946 | callerApp.hasPendingUiClean() ? 1 : 0); |
| 947 | if (callerApp.getInteractionEventTime() != 0) { |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 948 | builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_LAST_INTERACTION_EVENT, |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 949 | (nowElapsed - callerApp.getInteractionEventTime())); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 950 | } |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 951 | if (callerApp.getFgInteractionTime() != 0) { |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 952 | builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_FG_INTERACTION, |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 953 | (nowElapsed - callerApp.getFgInteractionTime())); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 954 | } |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 955 | if (callerApp.getWhenUnimportant() != 0) { |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 956 | builder.addTaggedData(FIELD_PROCESS_RECORD_MILLIS_SINCE_UNIMPORTANT, |
Wale Ogunwale | 342fbe9 | 2018-10-09 08:44:10 -0700 | [diff] [blame] | 957 | (nowUptime - callerApp.getWhenUnimportant())); |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 958 | } |
| 959 | } |
Michal Karpinski | 201bc0c | 2018-07-20 15:32:00 +0100 | [diff] [blame] | 960 | mMetricsLogger.write(builder); |
| 961 | } |
| 962 | |
Jorim Jaggi | 9b58f2d | 2018-02-19 17:48:44 +0100 | [diff] [blame] | 963 | private int getTransitionType(WindowingModeTransitionInfo info) { |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 964 | if (info.currentTransitionProcessRunning) { |
| 965 | if (info.startResult == START_SUCCESS) { |
| 966 | return TYPE_TRANSITION_WARM_LAUNCH; |
| 967 | } else if (info.startResult == START_TASK_TO_FRONT) { |
| 968 | return TYPE_TRANSITION_HOT_LAUNCH; |
| 969 | } |
Vishnu Nair | 194c64f | 2019-04-12 13:51:40 -0700 | [diff] [blame] | 970 | } else if (info.startResult == START_SUCCESS |
| 971 | || (info.startResult == START_TASK_TO_FRONT)) { |
| 972 | // TaskRecord may still exist when cold launching an activity and the start |
| 973 | // result will be set to START_TASK_TO_FRONT. Treat this as a COLD launch. |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 974 | return TYPE_TRANSITION_COLD_LAUNCH; |
| 975 | } |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 976 | return INVALID_TRANSITION_TYPE; |
Jorim Jaggi | 3878ca3 | 2017-02-02 17:13:05 -0800 | [diff] [blame] | 977 | } |
Ng Zhi An | bbefdec | 2018-01-30 17:12:39 -0800 | [diff] [blame] | 978 | |
Ng Zhi An | 8347354 | 2018-02-20 09:02:14 -0800 | [diff] [blame] | 979 | private void logAppStartMemoryStateCapture(WindowingModeTransitionInfoSnapshot info) { |
| 980 | if (info.processRecord == null) { |
Ng Zhi An | bbefdec | 2018-01-30 17:12:39 -0800 | [diff] [blame] | 981 | if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture processRecord null"); |
| 982 | return; |
| 983 | } |
| 984 | |
Wale Ogunwale | f673393 | 2018-06-27 05:14:34 -0700 | [diff] [blame] | 985 | final int pid = info.processRecord.getPid(); |
Ng Zhi An | 8347354 | 2018-02-20 09:02:14 -0800 | [diff] [blame] | 986 | final int uid = info.applicationInfo.uid; |
Rajeev Kumar | bfcd920 | 2018-03-02 22:42:13 +0000 | [diff] [blame] | 987 | final MemoryStat memoryStat = readMemoryStatFromFilesystem(uid, pid); |
Ng Zhi An | bbefdec | 2018-01-30 17:12:39 -0800 | [diff] [blame] | 988 | if (memoryStat == null) { |
| 989 | if (DEBUG_METRICS) Slog.i(TAG, "logAppStartMemoryStateCapture memoryStat null"); |
| 990 | return; |
| 991 | } |
| 992 | |
| 993 | StatsLog.write( |
| 994 | StatsLog.APP_START_MEMORY_STATE_CAPTURED, |
| 995 | uid, |
Ng Zhi An | 8347354 | 2018-02-20 09:02:14 -0800 | [diff] [blame] | 996 | info.processName, |
| 997 | info.launchedActivityName, |
Ng Zhi An | bbefdec | 2018-01-30 17:12:39 -0800 | [diff] [blame] | 998 | memoryStat.pgfault, |
| 999 | memoryStat.pgmajfault, |
| 1000 | memoryStat.rssInBytes, |
| 1001 | memoryStat.cacheInBytes, |
| 1002 | memoryStat.swapInBytes); |
| 1003 | } |
| 1004 | |
Wale Ogunwale | f673393 | 2018-06-27 05:14:34 -0700 | [diff] [blame] | 1005 | private WindowProcessController findProcessForActivity(ActivityRecord launchedActivity) { |
Ng Zhi An | bbefdec | 2018-01-30 17:12:39 -0800 | [diff] [blame] | 1006 | return launchedActivity != null |
Wale Ogunwale | f673393 | 2018-06-27 05:14:34 -0700 | [diff] [blame] | 1007 | ? mSupervisor.mService.mProcessNames.get( |
Andrii Kulian | eceebbf | 2019-06-26 17:36:51 -0700 | [diff] [blame] | 1008 | launchedActivity.processName, launchedActivity.info.applicationInfo.uid) |
Ng Zhi An | bbefdec | 2018-01-30 17:12:39 -0800 | [diff] [blame] | 1009 | : null; |
| 1010 | } |
Calin Juravle | 759fbda | 2018-02-20 19:52:30 +0000 | [diff] [blame] | 1011 | |
| 1012 | private ArtManagerInternal getArtManagerInternal() { |
| 1013 | if (mArtManagerInternal == null) { |
| 1014 | // Note that this may be null. |
| 1015 | // ArtManagerInternal is registered during PackageManagerService |
| 1016 | // initialization which happens after ActivityManagerService. |
| 1017 | mArtManagerInternal = LocalServices.getService(ArtManagerInternal.class); |
| 1018 | } |
| 1019 | return mArtManagerInternal; |
| 1020 | } |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 1021 | |
| 1022 | /** |
Chris Wailes | 35c193c | 2018-10-09 18:12:00 -0700 | [diff] [blame] | 1023 | * Starts traces for app launch. |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 1024 | * |
| 1025 | * @param info |
| 1026 | * */ |
| 1027 | private void startTraces(WindowingModeTransitionInfo info) { |
Ioannis Ilkos | 66e1cd8 | 2019-05-03 10:47:30 +0100 | [diff] [blame] | 1028 | if (!Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER) || info == null |
| 1029 | || info.launchTraceActive) { |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 1030 | return; |
| 1031 | } |
Ioannis Ilkos | 66e1cd8 | 2019-05-03 10:47:30 +0100 | [diff] [blame] | 1032 | Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " |
| 1033 | + info.launchedActivity.packageName, 0); |
| 1034 | info.launchTraceActive = true; |
Vishnu Nair | 132ee83 | 2018-09-28 15:00:05 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | private void stopLaunchTrace(WindowingModeTransitionInfo info) { |
| 1038 | if (info == null) { |
| 1039 | return; |
| 1040 | } |
| 1041 | if (info.launchTraceActive) { |
| 1042 | Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, "launching: " |
| 1043 | + info.launchedActivity.packageName, 0); |
| 1044 | info.launchTraceActive = false; |
| 1045 | } |
| 1046 | } |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1047 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1048 | public ActivityMetricsLaunchObserverRegistry getLaunchObserverRegistry() { |
| 1049 | return mLaunchObserver; |
| 1050 | } |
| 1051 | |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1052 | /** Notify the {@link ActivityMetricsLaunchObserver} that a new launch sequence has begun. */ |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 1053 | private void launchObserverNotifyIntentStarted(Intent intent, long timestampNs) { |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1054 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1055 | "MetricsLogger:launchObserverNotifyIntentStarted"); |
| 1056 | |
| 1057 | // Beginning a launch is timing sensitive and so should be observed as soon as possible. |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 1058 | mLaunchObserver.onIntentStarted(intent, timestampNs); |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1059 | |
| 1060 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * Notify the {@link ActivityMetricsLaunchObserver} that the previous launch sequence has |
| 1065 | * aborted due to intent failure (e.g. intent resolve failed or security error, etc) or |
| 1066 | * intent being delivered to the top running activity. |
| 1067 | */ |
| 1068 | private void launchObserverNotifyIntentFailed() { |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1069 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1070 | "MetricsLogger:launchObserverNotifyIntentFailed"); |
| 1071 | |
| 1072 | mLaunchObserver.onIntentFailed(); |
| 1073 | |
| 1074 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /** |
| 1078 | * Notify the {@link ActivityMetricsLaunchObserver} that the current launch sequence's activity |
| 1079 | * has started. |
| 1080 | */ |
| 1081 | private void launchObserverNotifyActivityLaunched(WindowingModeTransitionInfo info) { |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1082 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1083 | "MetricsLogger:launchObserverNotifyActivityLaunched"); |
| 1084 | |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1085 | @ActivityMetricsLaunchObserver.Temperature int temperature = |
| 1086 | convertTransitionTypeToLaunchObserverTemperature(getTransitionType(info)); |
| 1087 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1088 | // Beginning a launch is timing sensitive and so should be observed as soon as possible. |
| 1089 | mLaunchObserver.onActivityLaunched(convertActivityRecordToProto(info.launchedActivity), |
| 1090 | temperature); |
| 1091 | |
| 1092 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /** |
Yan Wang | 9eac5ec | 2019-09-30 15:42:18 -0700 | [diff] [blame] | 1096 | * Notifies the {@link ActivityMetricsLaunchObserver} the reportFullDrawn event. |
| 1097 | */ |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 1098 | private void launchObserverNotifyReportFullyDrawn(ActivityRecord r, long timestampNs) { |
Yan Wang | 9eac5ec | 2019-09-30 15:42:18 -0700 | [diff] [blame] | 1099 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1100 | "MetricsLogger:launchObserverNotifyReportFullyDrawn"); |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 1101 | mLaunchObserver.onReportFullyDrawn(convertActivityRecordToProto(r), timestampNs); |
Yan Wang | 9eac5ec | 2019-09-30 15:42:18 -0700 | [diff] [blame] | 1102 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 1103 | } |
| 1104 | |
| 1105 | /** |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1106 | * Notify the {@link ActivityMetricsLaunchObserver} that the current launch sequence is |
| 1107 | * cancelled. |
| 1108 | */ |
| 1109 | private void launchObserverNotifyActivityLaunchCancelled(WindowingModeTransitionInfo info) { |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1110 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1111 | "MetricsLogger:launchObserverNotifyActivityLaunchCancelled"); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1112 | |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1113 | final @ActivityMetricsLaunchObserver.ActivityRecordProto byte[] activityRecordProto = |
| 1114 | info != null ? convertActivityRecordToProto(info.launchedActivity) : null; |
| 1115 | |
| 1116 | mLaunchObserver.onActivityLaunchCancelled(activityRecordProto); |
| 1117 | |
| 1118 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * Notify the {@link ActivityMetricsLaunchObserver} that the current launch sequence's activity |
| 1123 | * has fully finished (successfully). |
| 1124 | */ |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 1125 | private void launchObserverNotifyActivityLaunchFinished(WindowingModeTransitionInfo info, |
| 1126 | long timestampNs) { |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1127 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1128 | "MetricsLogger:launchObserverNotifyActivityLaunchFinished"); |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1129 | |
Yan Wang | d47f90b | 2019-10-03 19:17:15 -0700 | [diff] [blame] | 1130 | mLaunchObserver |
| 1131 | .onActivityLaunchFinished(convertActivityRecordToProto(info.launchedActivity), |
| 1132 | timestampNs); |
Igor Murashkin | c0b47e4 | 2018-11-07 15:54:18 -0800 | [diff] [blame] | 1133 | |
| 1134 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 1135 | } |
| 1136 | |
| 1137 | @VisibleForTesting |
| 1138 | static @ActivityMetricsLaunchObserver.ActivityRecordProto byte[] |
| 1139 | convertActivityRecordToProto(ActivityRecord record) { |
| 1140 | // May take non-negligible amount of time to convert ActivityRecord into a proto, |
| 1141 | // so track the time. |
| 1142 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1143 | "MetricsLogger:convertActivityRecordToProto"); |
| 1144 | |
| 1145 | // There does not appear to be a way to 'reset' a ProtoOutputBuffer stream, |
| 1146 | // so create a new one every time. |
| 1147 | final ProtoOutputStream protoOutputStream = |
| 1148 | new ProtoOutputStream(LAUNCH_OBSERVER_ACTIVITY_RECORD_PROTO_CHUNK_SIZE); |
| 1149 | // Write this data out as the top-most ActivityRecordProto (i.e. it is not a sub-object). |
| 1150 | record.writeToProto(protoOutputStream); |
| 1151 | final byte[] bytes = protoOutputStream.getBytes(); |
| 1152 | |
| 1153 | Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); |
| 1154 | |
| 1155 | return bytes; |
Igor Murashkin | 212d06c | 2018-10-22 16:34:39 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | private static @ActivityMetricsLaunchObserver.Temperature int |
| 1159 | convertTransitionTypeToLaunchObserverTemperature(int transitionType) { |
| 1160 | switch (transitionType) { |
| 1161 | case TYPE_TRANSITION_WARM_LAUNCH: |
| 1162 | return ActivityMetricsLaunchObserver.TEMPERATURE_WARM; |
| 1163 | case TYPE_TRANSITION_HOT_LAUNCH: |
| 1164 | return ActivityMetricsLaunchObserver.TEMPERATURE_HOT; |
| 1165 | case TYPE_TRANSITION_COLD_LAUNCH: |
| 1166 | return ActivityMetricsLaunchObserver.TEMPERATURE_COLD; |
| 1167 | default: |
| 1168 | return -1; |
| 1169 | } |
| 1170 | } |
Filip Gruszczynski | 77d9448 | 2015-12-11 13:59:52 -0800 | [diff] [blame] | 1171 | } |