/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.android.internal.util; import static android.os.Trace.TRACE_TAG_APP; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL_UNLOCKED; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_EXPAND_PANEL; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_FACE_WAKE_AND_UNLOCK; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_FINGERPRINT_WAKE_AND_UNLOCK; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_FOLD_TO_AOD; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOAD_SHARE_SHEET; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOCKSCREEN_UNLOCK; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_REQUEST_IME_HIDDEN; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_REQUEST_IME_SHOWN; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_CAMERA_CHECK; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_SENSOR; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_BACK_ARROW; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_SELECTION_TOOLBAR; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_VOICE_INTERACTION; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_SMARTSPACE_DOORBELL; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_START_RECENTS_ANIMATION; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_SWITCH_DISPLAY_UNFOLD; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_TOGGLE_RECENTS; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_TURN_ON_SCREEN; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_UDFPS_ILLUMINATE; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_USER_SWITCH; import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__UNKNOWN_ACTION; import static com.android.internal.util.LatencyTracker.ActionProperties.ENABLE_SUFFIX; import static com.android.internal.util.LatencyTracker.ActionProperties.LEGACY_TRACE_THRESHOLD_SUFFIX; import static com.android.internal.util.LatencyTracker.ActionProperties.SAMPLE_INTERVAL_SUFFIX; import static com.android.internal.util.LatencyTracker.ActionProperties.TRACE_THRESHOLD_SUFFIX; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; import android.content.Context; import android.os.Build; import android.os.ConditionVariable; import android.os.SystemClock; import android.os.Trace; import android.provider.DeviceConfig; import android.text.TextUtils; import android.util.EventLog; import android.util.Log; import android.util.SparseArray; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.logging.EventLogTags; import com.android.internal.os.BackgroundThread; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Locale; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; /** * Class to track various latencies in SystemUI. It then writes the latency to statsd and also * outputs it to logcat so these latencies can be captured by tests and then used for dashboards. *
* This is currently only in Keyguard so it can be shared between SystemUI and Keyguard, but * eventually we'd want to merge these two packages together so Keyguard can use common classes * that are shared with SystemUI. */ public class LatencyTracker { private static final String TAG = "LatencyTracker"; public static final String SETTINGS_ENABLED_KEY = "enabled"; private static final String SETTINGS_SAMPLING_INTERVAL_KEY = "sampling_interval"; private static final boolean DEBUG = false; /** Default to being enabled on debug builds. */ private static final boolean DEFAULT_ENABLED = Build.IS_DEBUGGABLE; /** Default to collecting data for 1/5 of all actions (randomly sampled). */ private static final int DEFAULT_SAMPLING_INTERVAL = 5; /** * Time it takes until the first frame of the notification panel to be displayed while expanding */ public static final int ACTION_EXPAND_PANEL = 0; /** * Time it takes until the first frame of recents is drawn after invoking it with the button. */ public static final int ACTION_TOGGLE_RECENTS = 1; /** * Time between we get a fingerprint acquired signal until we start with the unlock animation */ public static final int ACTION_FINGERPRINT_WAKE_AND_UNLOCK = 2; /** * Time it takes to check PIN/Pattern/Password. */ public static final int ACTION_CHECK_CREDENTIAL = 3; /** * Time it takes to check fully PIN/Pattern/Password, i.e. that's the time spent including the * actions to unlock a user. */ public static final int ACTION_CHECK_CREDENTIAL_UNLOCKED = 4; /** * Time it takes to turn on the screen. */ public static final int ACTION_TURN_ON_SCREEN = 5; /** * Time it takes to rotate the screen. */ public static final int ACTION_ROTATE_SCREEN = 6; /* * Time between we get a face acquired signal until we start with the unlock animation */ public static final int ACTION_FACE_WAKE_AND_UNLOCK = 7; /** * Time between the swipe-up gesture and window drawn of recents activity. */ public static final int ACTION_START_RECENTS_ANIMATION = 8; /** * Time it takes to for the camera based algorithm to rotate the screen. */ public static final int ACTION_ROTATE_SCREEN_CAMERA_CHECK = 9; /** * Time it takes the sensor to detect rotation. */ public static final int ACTION_ROTATE_SCREEN_SENSOR = 10; /** * Time it takes to start unlock animation . */ public static final int ACTION_LOCKSCREEN_UNLOCK = 11; /** * Time it takes to switch users. */ public static final int ACTION_USER_SWITCH = 12; /** * Time it takes to turn on the inner screen for a foldable device. */ public static final int ACTION_SWITCH_DISPLAY_UNFOLD = 13; /** * Time it takes for a UDFPS sensor to appear ready after it is touched. */ public static final int ACTION_UDFPS_ILLUMINATE = 14; /** * Time it takes for the gesture back affordance arrow to show up. */ public static final int ACTION_SHOW_BACK_ARROW = 15; /** * Time it takes for loading share sheet. */ public static final int ACTION_LOAD_SHARE_SHEET = 16; /** * Time it takes for showing the selection toolbar. */ public static final int ACTION_SHOW_SELECTION_TOOLBAR = 17; /** * Time it takes to show AOD display after folding the device. */ public static final int ACTION_FOLD_TO_AOD = 18; /** * Time it takes to show the {@link android.service.voice.VoiceInteractionSession} system UI * after a {@link android.hardware.soundtrigger3.ISoundTriggerHw} voice trigger. */ public static final int ACTION_SHOW_VOICE_INTERACTION = 19; /** * Time it takes to request IME shown animation. */ public static final int ACTION_REQUEST_IME_SHOWN = 20; /** * Time it takes to request IME hidden animation. */ public static final int ACTION_REQUEST_IME_HIDDEN = 21; /** * Time it takes to load the animation frames in smart space doorbell card. * It measures the duration from the images uris are passed into the view * to all the frames are loaded. *
* A long latency makes the doorbell animation looks janky until all the frames are loaded. */ public static final int ACTION_SMARTSPACE_DOORBELL = 22; private static final int[] ACTIONS_ALL = { ACTION_EXPAND_PANEL, ACTION_TOGGLE_RECENTS, ACTION_FINGERPRINT_WAKE_AND_UNLOCK, ACTION_CHECK_CREDENTIAL, ACTION_CHECK_CREDENTIAL_UNLOCKED, ACTION_TURN_ON_SCREEN, ACTION_ROTATE_SCREEN, ACTION_FACE_WAKE_AND_UNLOCK, ACTION_START_RECENTS_ANIMATION, ACTION_ROTATE_SCREEN_CAMERA_CHECK, ACTION_ROTATE_SCREEN_SENSOR, ACTION_LOCKSCREEN_UNLOCK, ACTION_USER_SWITCH, ACTION_SWITCH_DISPLAY_UNFOLD, ACTION_UDFPS_ILLUMINATE, ACTION_SHOW_BACK_ARROW, ACTION_LOAD_SHARE_SHEET, ACTION_SHOW_SELECTION_TOOLBAR, ACTION_FOLD_TO_AOD, ACTION_SHOW_VOICE_INTERACTION, ACTION_REQUEST_IME_SHOWN, ACTION_REQUEST_IME_HIDDEN, ACTION_SMARTSPACE_DOORBELL, }; /** @hide */ @IntDef({ ACTION_EXPAND_PANEL, ACTION_TOGGLE_RECENTS, ACTION_FINGERPRINT_WAKE_AND_UNLOCK, ACTION_CHECK_CREDENTIAL, ACTION_CHECK_CREDENTIAL_UNLOCKED, ACTION_TURN_ON_SCREEN, ACTION_ROTATE_SCREEN, ACTION_FACE_WAKE_AND_UNLOCK, ACTION_START_RECENTS_ANIMATION, ACTION_ROTATE_SCREEN_CAMERA_CHECK, ACTION_ROTATE_SCREEN_SENSOR, ACTION_LOCKSCREEN_UNLOCK, ACTION_USER_SWITCH, ACTION_SWITCH_DISPLAY_UNFOLD, ACTION_UDFPS_ILLUMINATE, ACTION_SHOW_BACK_ARROW, ACTION_LOAD_SHARE_SHEET, ACTION_SHOW_SELECTION_TOOLBAR, ACTION_FOLD_TO_AOD, ACTION_SHOW_VOICE_INTERACTION, ACTION_REQUEST_IME_SHOWN, ACTION_REQUEST_IME_HIDDEN, ACTION_SMARTSPACE_DOORBELL, }) @Retention(RetentionPolicy.SOURCE) public @interface Action { } @VisibleForTesting public static final int[] STATSD_ACTION = new int[] { UIACTION_LATENCY_REPORTED__ACTION__ACTION_EXPAND_PANEL, UIACTION_LATENCY_REPORTED__ACTION__ACTION_TOGGLE_RECENTS, UIACTION_LATENCY_REPORTED__ACTION__ACTION_FINGERPRINT_WAKE_AND_UNLOCK, UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL, UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL_UNLOCKED, UIACTION_LATENCY_REPORTED__ACTION__ACTION_TURN_ON_SCREEN, UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN, UIACTION_LATENCY_REPORTED__ACTION__ACTION_FACE_WAKE_AND_UNLOCK, UIACTION_LATENCY_REPORTED__ACTION__ACTION_START_RECENTS_ANIMATION, UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_CAMERA_CHECK, UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_SENSOR, UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOCKSCREEN_UNLOCK, UIACTION_LATENCY_REPORTED__ACTION__ACTION_USER_SWITCH, UIACTION_LATENCY_REPORTED__ACTION__ACTION_SWITCH_DISPLAY_UNFOLD, UIACTION_LATENCY_REPORTED__ACTION__ACTION_UDFPS_ILLUMINATE, UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_BACK_ARROW, UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOAD_SHARE_SHEET, UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_SELECTION_TOOLBAR, UIACTION_LATENCY_REPORTED__ACTION__ACTION_FOLD_TO_AOD, UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_VOICE_INTERACTION, UIACTION_LATENCY_REPORTED__ACTION__ACTION_REQUEST_IME_SHOWN, UIACTION_LATENCY_REPORTED__ACTION__ACTION_REQUEST_IME_HIDDEN, UIACTION_LATENCY_REPORTED__ACTION__ACTION_SMARTSPACE_DOORBELL, }; private static LatencyTracker sLatencyTracker; private final Object mLock = new Object(); @GuardedBy("mLock") private final SparseArray