Debian Codegraph @

core/java/com/android/internal/util/LatencyTracker.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15 package com.android.internal.util;
16
17 import static android.os.Trace.TRACE_TAG_APP;
18
19 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL;
20 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL_UNLOCKED;
21 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_EXPAND_PANEL;
22 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_FACE_WAKE_AND_UNLOCK;
23 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_FINGERPRINT_WAKE_AND_UNLOCK;
24 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_FOLD_TO_AOD;
25 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOAD_SHARE_SHEET;
26 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOCKSCREEN_UNLOCK;
27 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN;
28 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_CAMERA_CHECK;
29 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_SENSOR;
30 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_BACK_ARROW;
31 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_SELECTION_TOOLBAR;
32 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_VOICE_INTERACTION;
33 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_START_RECENTS_ANIMATION;
34 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_SWITCH_DISPLAY_UNFOLD;
35 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_TOGGLE_RECENTS;
36 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_TURN_ON_SCREEN;
37 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_UDFPS_ILLUMINATE;
38 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__ACTION_USER_SWITCH;
39 import static com.android.internal.util.FrameworkStatsLog.UIACTION_LATENCY_REPORTED__ACTION__UNKNOWN_ACTION;
40 import static com.android.internal.util.LatencyTracker.ActionProperties.ENABLE_SUFFIX;
41 import static com.android.internal.util.LatencyTracker.ActionProperties.LEGACY_TRACE_THRESHOLD_SUFFIX;
42 import static com.android.internal.util.LatencyTracker.ActionProperties.SAMPLE_INTERVAL_SUFFIX;
43 import static com.android.internal.util.LatencyTracker.ActionProperties.TRACE_THRESHOLD_SUFFIX;
44
45 import android.annotation.IntDef;
46 import android.annotation.NonNull;
47 import android.annotation.Nullable;
48 import android.content.Context;
49 import android.os.Build;
50 import android.os.ConditionVariable;
51 import android.os.SystemClock;
52 import android.os.Trace;
53 import android.provider.DeviceConfig;
54 import android.text.TextUtils;
55 import android.util.EventLog;
56 import android.util.Log;
57 import android.util.SparseArray;
58
59 import com.android.internal.annotations.GuardedBy;
60 import com.android.internal.annotations.VisibleForTesting;
61 import com.android.internal.logging.EventLogTags;
62 import com.android.internal.os.BackgroundThread;
63
64 import java.lang.annotation.Retention;
65 import java.lang.annotation.RetentionPolicy;
66 import java.util.Locale;
67 import java.util.concurrent.ThreadLocalRandom;
68 import java.util.concurrent.TimeUnit;
69
70 /**
71 * Class to track various latencies in SystemUI. It then writes the latency to statsd and also
72 * outputs it to logcat so these latencies can be captured by tests and then used for dashboards.
73 * <p>
74 * This is currently only in Keyguard so it can be shared between SystemUI and Keyguard, but
75 * eventually we'd want to merge these two packages together so Keyguard can use common classes
76 * that are shared with SystemUI.
77 */
78 public class LatencyTracker {
79 private static final String TAG = "LatencyTracker";
80 public static final String SETTINGS_ENABLED_KEY = "enabled";
81 private static final String SETTINGS_SAMPLING_INTERVAL_KEY = "sampling_interval";
82 private static final boolean DEBUG = false;
83 /** Default to being enabled on debug builds. */
84 private static final boolean DEFAULT_ENABLED = Build.IS_DEBUGGABLE;
85 /** Default to collecting data for 1/5 of all actions (randomly sampled). */
86 private static final int DEFAULT_SAMPLING_INTERVAL = 5;
87
88 /**
89 * Time it takes until the first frame of the notification panel to be displayed while expanding
90 */
91 public static final int ACTION_EXPAND_PANEL = 0;
92
93 /**
94 * Time it takes until the first frame of recents is drawn after invoking it with the button.
95 */
96 public static final int ACTION_TOGGLE_RECENTS = 1;
97
98 /**
99 * Time between we get a fingerprint acquired signal until we start with the unlock animation
100 */
101 public static final int ACTION_FINGERPRINT_WAKE_AND_UNLOCK = 2;
102
103 /**
104 * Time it takes to check PIN/Pattern/Password.
105 */
106 public static final int ACTION_CHECK_CREDENTIAL = 3;
107
108 /**
109 * Time it takes to check fully PIN/Pattern/Password, i.e. that's the time spent including the
110 * actions to unlock a user.
111 */
112 public static final int ACTION_CHECK_CREDENTIAL_UNLOCKED = 4;
113
114 /**
115 * Time it takes to turn on the screen.
116 */
117 public static final int ACTION_TURN_ON_SCREEN = 5;
118
119 /**
120 * Time it takes to rotate the screen.
121 */
122 public static final int ACTION_ROTATE_SCREEN = 6;
123
124 /*
125 * Time between we get a face acquired signal until we start with the unlock animation
126 */
127 public static final int ACTION_FACE_WAKE_AND_UNLOCK = 7;
128
129 /**
130 * Time between the swipe-up gesture and window drawn of recents activity.
131 */
132 public static final int ACTION_START_RECENTS_ANIMATION = 8;
133
134 /**
135 * Time it takes to for the camera based algorithm to rotate the screen.
136 */
137 public static final int ACTION_ROTATE_SCREEN_CAMERA_CHECK = 9;
138
139 /**
140 * Time it takes the sensor to detect rotation.
141 */
142 public static final int ACTION_ROTATE_SCREEN_SENSOR = 10;
143
144 /**
145 * Time it takes to start unlock animation .
146 */
147 public static final int ACTION_LOCKSCREEN_UNLOCK = 11;
148
149 /**
150 * Time it takes to switch users.
151 */
152 public static final int ACTION_USER_SWITCH = 12;
153
154 /**
155 * Time it takes to turn on the inner screen for a foldable device.
156 */
157 public static final int ACTION_SWITCH_DISPLAY_UNFOLD = 13;
158
159 /**
160 * Time it takes for a UDFPS sensor to appear ready after it is touched.
161 */
162 public static final int ACTION_UDFPS_ILLUMINATE = 14;
163
164 /**
165 * Time it takes for the gesture back affordance arrow to show up.
166 */
167 public static final int ACTION_SHOW_BACK_ARROW = 15;
168
169 /**
170 * Time it takes for loading share sheet.
171 */
172 public static final int ACTION_LOAD_SHARE_SHEET = 16;
173
174 /**
175 * Time it takes for showing the selection toolbar.
176 */
177 public static final int ACTION_SHOW_SELECTION_TOOLBAR = 17;
178
179 /**
180 * Time it takes to show AOD display after folding the device.
181 */
182 public static final int ACTION_FOLD_TO_AOD = 18;
183
184 /**
185 * Time it takes to show the {@link android.service.voice.VoiceInteractionSession} system UI
186 * after a {@link android.hardware.soundtrigger3.ISoundTriggerHw} voice trigger.
187 */
188 public static final int ACTION_SHOW_VOICE_INTERACTION = 19;
189
190 private static final int[] ACTIONS_ALL = {
191 ACTION_EXPAND_PANEL,
192 ACTION_TOGGLE_RECENTS,
193 ACTION_FINGERPRINT_WAKE_AND_UNLOCK,
194 ACTION_CHECK_CREDENTIAL,
195 ACTION_CHECK_CREDENTIAL_UNLOCKED,
196 ACTION_TURN_ON_SCREEN,
197 ACTION_ROTATE_SCREEN,
198 ACTION_FACE_WAKE_AND_UNLOCK,
199 ACTION_START_RECENTS_ANIMATION,
200 ACTION_ROTATE_SCREEN_CAMERA_CHECK,
201 ACTION_ROTATE_SCREEN_SENSOR,
202 ACTION_LOCKSCREEN_UNLOCK,
203 ACTION_USER_SWITCH,
204 ACTION_SWITCH_DISPLAY_UNFOLD,
205 ACTION_UDFPS_ILLUMINATE,
206 ACTION_SHOW_BACK_ARROW,
207 ACTION_LOAD_SHARE_SHEET,
208 ACTION_SHOW_SELECTION_TOOLBAR,
209 ACTION_FOLD_TO_AOD,
210 ACTION_SHOW_VOICE_INTERACTION,
211 };
212
213 /** @hide */
214 @IntDef({
215 ACTION_EXPAND_PANEL,
216 ACTION_TOGGLE_RECENTS,
217 ACTION_FINGERPRINT_WAKE_AND_UNLOCK,
218 ACTION_CHECK_CREDENTIAL,
219 ACTION_CHECK_CREDENTIAL_UNLOCKED,
220 ACTION_TURN_ON_SCREEN,
221 ACTION_ROTATE_SCREEN,
222 ACTION_FACE_WAKE_AND_UNLOCK,
223 ACTION_START_RECENTS_ANIMATION,
224 ACTION_ROTATE_SCREEN_CAMERA_CHECK,
225 ACTION_ROTATE_SCREEN_SENSOR,
226 ACTION_LOCKSCREEN_UNLOCK,
227 ACTION_USER_SWITCH,
228 ACTION_SWITCH_DISPLAY_UNFOLD,
229 ACTION_UDFPS_ILLUMINATE,
230 ACTION_SHOW_BACK_ARROW,
231 ACTION_LOAD_SHARE_SHEET,
232 ACTION_SHOW_SELECTION_TOOLBAR,
233 ACTION_FOLD_TO_AOD,
234 ACTION_SHOW_VOICE_INTERACTION,
235 })
236 @Retention(RetentionPolicy.SOURCE)
237 public @interface Action {
238 }
239
240 @VisibleForTesting
241 public static final int[] STATSD_ACTION = new int[] {
242 UIACTION_LATENCY_REPORTED__ACTION__ACTION_EXPAND_PANEL,
243 UIACTION_LATENCY_REPORTED__ACTION__ACTION_TOGGLE_RECENTS,
244 UIACTION_LATENCY_REPORTED__ACTION__ACTION_FINGERPRINT_WAKE_AND_UNLOCK,
245 UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL,
246 UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL_UNLOCKED,
247 UIACTION_LATENCY_REPORTED__ACTION__ACTION_TURN_ON_SCREEN,
248 UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN,
249 UIACTION_LATENCY_REPORTED__ACTION__ACTION_FACE_WAKE_AND_UNLOCK,
250 UIACTION_LATENCY_REPORTED__ACTION__ACTION_START_RECENTS_ANIMATION,
251 UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_CAMERA_CHECK,
252 UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_SENSOR,
253 UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOCKSCREEN_UNLOCK,
254 UIACTION_LATENCY_REPORTED__ACTION__ACTION_USER_SWITCH,
255 UIACTION_LATENCY_REPORTED__ACTION__ACTION_SWITCH_DISPLAY_UNFOLD,
256 UIACTION_LATENCY_REPORTED__ACTION__ACTION_UDFPS_ILLUMINATE,
257 UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_BACK_ARROW,
258 UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOAD_SHARE_SHEET,
259 UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_SELECTION_TOOLBAR,
260 UIACTION_LATENCY_REPORTED__ACTION__ACTION_FOLD_TO_AOD,
261 UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_VOICE_INTERACTION,
262 };
263
264 private static LatencyTracker sLatencyTracker;
265
266 private final Object mLock = new Object();
267 @GuardedBy("mLock")
268 private final SparseArray<Session> mSessions = new SparseArray<>();
269 @GuardedBy("mLock")
270 private final SparseArray<ActionProperties> mActionPropertiesMap = new SparseArray<>();
271 @GuardedBy("mLock")
272 private boolean mEnabled;
273 @VisibleForTesting
274 public final ConditionVariable mDeviceConfigPropertiesUpdated = new ConditionVariable();
275
276 public static LatencyTracker getInstance(Context context) {
277 if (sLatencyTracker == null) {
278 synchronized (LatencyTracker.class) {
279 if (sLatencyTracker == null) {
280 sLatencyTracker = new LatencyTracker();
281 }
282 }
283 }
284 return sLatencyTracker;
285 }
286
287 @VisibleForTesting
288 public LatencyTracker() {
289 mEnabled = DEFAULT_ENABLED;
290
291 // Post initialization to the background in case we're running on the main thread.
292 BackgroundThread.getHandler().post(() -> this.updateProperties(
293 DeviceConfig.getProperties(DeviceConfig.NAMESPACE_LATENCY_TRACKER)));
294 DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_LATENCY_TRACKER,
295 BackgroundThread.getExecutor(), this::updateProperties);
296 }
297
298 private void updateProperties(DeviceConfig.Properties properties) {
299 synchronized (mLock) {
300 int samplingInterval = properties.getInt(SETTINGS_SAMPLING_INTERVAL_KEY,
301 DEFAULT_SAMPLING_INTERVAL);
302 mEnabled = properties.getBoolean(SETTINGS_ENABLED_KEY, DEFAULT_ENABLED);
303 for (int action : ACTIONS_ALL) {
304 String actionName = getNameOfAction(STATSD_ACTION[action]).toLowerCase(Locale.ROOT);
305 int legacyActionTraceThreshold = properties.getInt(
306 actionName + LEGACY_TRACE_THRESHOLD_SUFFIX, -1);
307 mActionPropertiesMap.put(action, new ActionProperties(action,
308 properties.getBoolean(actionName + ENABLE_SUFFIX, mEnabled),
309 properties.getInt(actionName + SAMPLE_INTERVAL_SUFFIX, samplingInterval),
310 properties.getInt(actionName + TRACE_THRESHOLD_SUFFIX,
311 legacyActionTraceThreshold)));
312 }
313 if (DEBUG) {
314 Log.d(TAG, "updated action properties: " + mActionPropertiesMap);
315 }
316 }
317 mDeviceConfigPropertiesUpdated.open();
318 }
319
320 /**
321 * A helper method to translate action type to name.
322 *
323 * @param atomsProtoAction the action type defined in AtomsProto.java
324 * @return the name of the action
325 */
326 public static String getNameOfAction(int atomsProtoAction) {
327 // Defined in AtomsProto.java
328 switch (atomsProtoAction) {
329 case UIACTION_LATENCY_REPORTED__ACTION__UNKNOWN_ACTION:
330 return "UNKNOWN";
331 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_EXPAND_PANEL:
332 return "ACTION_EXPAND_PANEL";
333 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_TOGGLE_RECENTS:
334 return "ACTION_TOGGLE_RECENTS";
335 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_FINGERPRINT_WAKE_AND_UNLOCK:
336 return "ACTION_FINGERPRINT_WAKE_AND_UNLOCK";
337 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL:
338 return "ACTION_CHECK_CREDENTIAL";
339 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_CHECK_CREDENTIAL_UNLOCKED:
340 return "ACTION_CHECK_CREDENTIAL_UNLOCKED";
341 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_TURN_ON_SCREEN:
342 return "ACTION_TURN_ON_SCREEN";
343 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN:
344 return "ACTION_ROTATE_SCREEN";
345 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_FACE_WAKE_AND_UNLOCK:
346 return "ACTION_FACE_WAKE_AND_UNLOCK";
347 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_START_RECENTS_ANIMATION:
348 return "ACTION_START_RECENTS_ANIMATION";
349 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_CAMERA_CHECK:
350 return "ACTION_ROTATE_SCREEN_CAMERA_CHECK";
351 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_ROTATE_SCREEN_SENSOR:
352 return "ACTION_ROTATE_SCREEN_SENSOR";
353 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOCKSCREEN_UNLOCK:
354 return "ACTION_LOCKSCREEN_UNLOCK";
355 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_USER_SWITCH:
356 return "ACTION_USER_SWITCH";
357 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_SWITCH_DISPLAY_UNFOLD:
358 return "ACTION_SWITCH_DISPLAY_UNFOLD";
359 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_UDFPS_ILLUMINATE:
360 return "ACTION_UDFPS_ILLUMINATE";
361 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_BACK_ARROW:
362 return "ACTION_SHOW_BACK_ARROW";
363 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_LOAD_SHARE_SHEET:
364 return "ACTION_LOAD_SHARE_SHEET";
365 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_SELECTION_TOOLBAR:
366 return "ACTION_SHOW_SELECTION_TOOLBAR";
367 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_FOLD_TO_AOD:
368 return "ACTION_FOLD_TO_AOD";
369 case UIACTION_LATENCY_REPORTED__ACTION__ACTION_SHOW_VOICE_INTERACTION:
370 return "ACTION_SHOW_VOICE_INTERACTION";
371 default:
372 throw new IllegalArgumentException("Invalid action");
373 }
374 }
375
376 private static String getTraceNameOfAction(@Action int action, String tag) {
377 if (TextUtils.isEmpty(tag)) {
378 return "L<" + getNameOfAction(STATSD_ACTION[action]) + ">";
379 } else {
380 return "L<" + getNameOfAction(STATSD_ACTION[action]) + "::" + tag + ">";
381 }
382 }
383
384 private static String getTraceTriggerNameForAction(@Action int action) {
385 return "com.android.telemetry.latency-tracker-" + getNameOfAction(STATSD_ACTION[action]);
386 }
387
388 /**
389 * @deprecated Use {@link #isEnabled(Context, int)}
390 */
391 @Deprecated
392 public static boolean isEnabled(Context ctx) {
393 return getInstance(ctx).isEnabled();
394 }
395
396 /**
397 * @deprecated Used {@link #isEnabled(int)}
398 */
399 @Deprecated
400 public boolean isEnabled() {
401 synchronized (mLock) {
402 return mEnabled;
403 }
404 }
405
406 public static boolean isEnabled(Context ctx, int action) {
407 return getInstance(ctx).isEnabled(action);
408 }
409
410 public boolean isEnabled(int action) {
411 synchronized (mLock) {
412 ActionProperties actionProperties = mActionPropertiesMap.get(action);
413 if (actionProperties != null) {
414 return actionProperties.isEnabled();
415 }
416 return false;
417 }
418 }
419
420 /**
421 * Notifies that an action is starting. <s>This needs to be called from the main thread.</s>
422 *
423 * @param action The action to start. One of the ACTION_* values.
424 */
425 public void onActionStart(@Action int action) {
426 onActionStart(action, null);
427 }
428
429 /**
430 * Notifies that an action is starting. <s>This needs to be called from the main thread.</s>
431 *
432 * @param action The action to start. One of the ACTION_* values.
433 * @param tag The brief description of the action.
434 */
435 public void onActionStart(@Action int action, String tag) {
436 synchronized (mLock) {
437 if (!isEnabled()) {
438 return;
439 }
440 // skip if the action is already instrumenting.
441 if (mSessions.get(action) != null) {
442 return;
443 }
444 Session session = new Session(action, tag);
445 session.begin(() -> onActionCancel(action));
446 mSessions.put(action, session);
447
448 if (DEBUG) {
449 Log.d(TAG, "onActionStart: " + session.name() + ", start=" + session.mStartRtc);
450 }
451 }
452 }
453
454 /**
455 * Notifies that an action has ended. <s>This needs to be called from the main thread.</s>
456 *
457 * @param action The action to end. One of the ACTION_* values.
458 */
459 public void onActionEnd(@Action int action) {
460 synchronized (mLock) {
461 if (!isEnabled()) {
462 return;
463 }
464 Session session = mSessions.get(action);
465 if (session == null) {
466 return;
467 }
468 session.end();
469 mSessions.delete(action);
470 logAction(action, session.duration());
471
472 if (DEBUG) {
473 Log.d(TAG, "onActionEnd:" + session.name() + ", duration=" + session.duration());
474 }
475 }
476 }
477
478 /**
479 * Notifies that an action has canceled. <s>This needs to be called from the main thread.</s>
480 *
481 * @param action The action to cancel. One of the ACTION_* values.
482 * @hide
483 */
484 public void onActionCancel(@Action int action) {
485 synchronized (mLock) {
486 Session session = mSessions.get(action);
487 if (session == null) {
488 return;
489 }
490 session.cancel();
491 mSessions.delete(action);
492
493 if (DEBUG) {
494 Log.d(TAG, "onActionCancel: " + session.name());
495 }
496 }
497 }
498
499 /**
500 * Logs an action that has started and ended. This needs to be called from the main thread.
501 *
502 * @param action The action to end. One of the ACTION_* values.
503 * @param duration The duration of the action in ms.
504 */
505 public void logAction(@Action int action, int duration) {
506 boolean shouldSample;
507 int traceThreshold;
508 synchronized (mLock) {
509 ActionProperties actionProperties = mActionPropertiesMap.get(action);
510 if (actionProperties == null) {
511 return;
512 }
513 int nextRandNum = ThreadLocalRandom.current().nextInt(
514 actionProperties.getSamplingInterval());
515 shouldSample = nextRandNum == 0;
516 traceThreshold = actionProperties.getTraceThreshold();
517 }
518
519 if (traceThreshold > 0 && duration >= traceThreshold) {
520 PerfettoTrigger.trigger(getTraceTriggerNameForAction(action));
521 }
522
523 logActionDeprecated(action, duration, shouldSample);
524 }
525
526 /**
527 * Logs an action that has started and ended. This needs to be called from the main thread.
528 *
529 * @param action The action to end. One of the ACTION_* values.
530 * @param duration The duration of the action in ms.
531 * @param writeToStatsLog Whether to write the measured latency to FrameworkStatsLog.
532 */
533 public static void logActionDeprecated(
534 @Action int action, int duration, boolean writeToStatsLog) {
535 Log.i(TAG, getNameOfAction(STATSD_ACTION[action]) + " latency=" + duration);
536 EventLog.writeEvent(EventLogTags.SYSUI_LATENCY, action, duration);
537
538 if (writeToStatsLog) {
539 FrameworkStatsLog.write(
540 FrameworkStatsLog.UI_ACTION_LATENCY_REPORTED, STATSD_ACTION[action], duration);
541 }
542 }
543
544 static class Session {
545 @Action
546 private final int mAction;
547 private final String mTag;
548 private final String mName;
549 private Runnable mTimeoutRunnable;
550 private long mStartRtc = -1;
551 private long mEndRtc = -1;
552
553 Session(@Action int action, @Nullable String tag) {
554 mAction = action;
555 mTag = tag;
556 mName = TextUtils.isEmpty(mTag)
557 ? getNameOfAction(STATSD_ACTION[mAction])
558 : getNameOfAction(STATSD_ACTION[mAction]) + "::" + mTag;
559 }
560
561 String name() {
562 return mName;
563 }
564
565 String traceName() {
566 return getTraceNameOfAction(mAction, mTag);
567 }
568
569 void begin(@NonNull Runnable timeoutAction) {
570 mStartRtc = SystemClock.elapsedRealtime();
571 Trace.asyncTraceBegin(TRACE_TAG_APP, traceName(), 0);
572
573 // start counting timeout.
574 mTimeoutRunnable = timeoutAction;
575 BackgroundThread.getHandler()
576 .postDelayed(mTimeoutRunnable, TimeUnit.SECONDS.toMillis(15));
577 }
578
579 void end() {
580 mEndRtc = SystemClock.elapsedRealtime();
581 Trace.asyncTraceEnd(TRACE_TAG_APP, traceName(), 0);
582 BackgroundThread.getHandler().removeCallbacks(mTimeoutRunnable);
583 mTimeoutRunnable = null;
584 }
585
586 void cancel() {
587 Trace.asyncTraceEnd(TRACE_TAG_APP, traceName(), 0);
588 BackgroundThread.getHandler().removeCallbacks(mTimeoutRunnable);
589 mTimeoutRunnable = null;
590 }
591
592 int duration() {
593 return (int) (mEndRtc - mStartRtc);
594 }
595 }
596
597 @VisibleForTesting
598 static class ActionProperties {
599 static final String ENABLE_SUFFIX = "_enable";
600 static final String SAMPLE_INTERVAL_SUFFIX = "_sample_interval";
601 // TODO: migrate all usages of the legacy trace theshold property
602 static final String LEGACY_TRACE_THRESHOLD_SUFFIX = "";
603 static final String TRACE_THRESHOLD_SUFFIX = "_trace_threshold";
604
605 @Action
606 private final int mAction;
607 private final boolean mEnabled;
608 private final int mSamplingInterval;
609 private final int mTraceThreshold;
610
611 ActionProperties(
612 @Action int action,
613 boolean enabled,
614 int samplingInterval,
615 int traceThreshold) {
616 this.mAction = action;
617 com.android.internal.util.AnnotationValidations.validate(
618 Action.class, null, mAction);
619 this.mEnabled = enabled;
620 this.mSamplingInterval = samplingInterval;
621 this.mTraceThreshold = traceThreshold;
622 }
623
624 @Action
625 int getAction() {
626 return mAction;
627 }
628
629 boolean isEnabled() {
630 return mEnabled;
631 }
632
633 int getSamplingInterval() {
634 return mSamplingInterval;
635 }
636
637 int getTraceThreshold() {
638 return mTraceThreshold;
639 }
640
641 @Override
642 public String toString() {
643 return "ActionProperties{"
644 + " mAction=" + mAction
645 + ", mEnabled=" + mEnabled
646 + ", mSamplingInterval=" + mSamplingInterval
647 + ", mTraceThreshold=" + mTraceThreshold
648 + "}";
649 }
650 }
651 }