1// Copyright (C) 2024 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// 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
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14import {Trace} from '../../../public/trace';
15
16/**
17 * Represents data for a Full trace metric
18 * Eg.- perfetto_ft_launcher-missed_sf_frames-mean
19 * ft here stands for full trace
20 */
21export interface FullTraceMetricData {
22  /** Process name (e.g., com.google.android.apps.nexuslauncher) */
23  process: string;
24
25  /** Jank type (e.g., app or sf missed frame) */
26  jankType: JankType;
27}
28
29/**
30 * Represents data for a CUJ scoped metric
31 * Eg.- perfetto_cuj_launcher-RECENTS_SCROLLING-counter_metrics-missed_sf_frames-mean
32 */
33export interface CujScopedMetricData {
34  /** Process name (e.g., com.google.android.apps.nexuslauncher) */
35  process: string;
36
37  /** Cuj interaction name (e.g., RECENTS_SCROLLING) */
38  cujName: string;
39
40  /** Jank type (e.g., app or sf missed frame) */
41  jankType: JankType;
42}
43
44/**
45 * Represents data for a Blocking Call metric
46 * Eg.- perfetto_android_blocking_call-cuj-name-com.google.android.apps.nexuslauncher-name-TASKBAR_EXPAND-blocking_calls-name-animation-total_dur_ms-mean
47 */
48export interface BlockingCallMetricData {
49  /** Process name (e.g., com.google.android.apps.nexuslauncher) */
50  process: string;
51
52  /** Cuj interaction name (e.g., TASKBAR_EXPAND) */
53  cujName: string;
54
55  /** Blocking Call name (e.g., animation) */
56  blockingCallName: string;
57
58  /** aggregation type (e.g., total_dur_ms-mean) */
59  aggregation: string;
60}
61
62/** Represents a cuj to be pinned. */
63export interface CujMetricData {
64  cujName: string;
65}
66
67// Common MetricData for all handler. If new needed then add here.
68export type MetricData =
69  | FullTraceMetricData
70  | CujScopedMetricData
71  | BlockingCallMetricData
72  | CujMetricData;
73
74// Common JankType for cujScoped and fullTrace metrics
75export type JankType = 'sf_frames' | 'app_frames' | 'frames';
76
77/**
78 * Common interface for debug track handlers
79 */
80export interface MetricHandler {
81  /**
82   * Match metric key & return parsed data if successful.
83   *
84   * @param {string} metricKey The metric key to match.
85   * @returns {MetricData | undefined} Parsed data or undefined if no match.
86   */
87  match(metricKey: string): MetricData | undefined;
88
89  /**
90   * Add debug track for parsed metric data.
91   *
92   * @param {MetricData} metricData The parsed metric data.
93   * @param {Trace} ctx context for trace methods and properties
94   * @returns {void}
95   */
96  addMetricTrack(metricData: MetricData, ctx: Trace): void;
97}
98
99// Pair for matching metric and its handler
100export type MetricHandlerMatch = {
101  metricData: MetricData;
102  metricHandler: MetricHandler;
103};
104
105/**
106 * Expand process name for specific system processes
107 *
108 * @param {string} metricProcessName Name of the processes
109 * @returns {string} Either the same or expanded name for abbreviated process names
110 */
111export function expandProcessName(metricProcessName: string): string {
112  if (metricProcessName.includes('systemui')) {
113    return 'com.android.systemui';
114  } else if (metricProcessName.includes('launcher')) {
115    return 'com.google.android.apps.nexuslauncher';
116  } else if (metricProcessName.includes('surfaceflinger')) {
117    return '/system/bin/surfaceflinger';
118  } else {
119    return metricProcessName;
120  }
121}
122