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.
14
15import {CujMetricData, MetricHandler} from './metricUtils';
16import {Trace} from '../../../public/trace';
17import {addJankCUJDebugTrack} from '../../dev.perfetto.AndroidCujs';
18
19/** Pins a single CUJ from CUJ scoped metrics. */
20class PinCujMetricHandler implements MetricHandler {
21  /**
22   * Matches metric key & return parsed data if successful.
23   *
24   * @param {string} metricKey The metric key to match.
25   * @returns {CujMetricData | undefined} Parsed data or undefined if no match.
26   */
27  public match(metricKey: string): CujMetricData | undefined {
28    const matcher = /perfetto_cuj_(?<process>.*)-(?<cujName>.*)-.*-missed_.*/;
29    const match = matcher.exec(metricKey);
30    if (!match?.groups) {
31      return undefined;
32    }
33    return {
34      cujName: match.groups.cujName,
35    };
36  }
37
38  /**
39   * Adds the debug tracks for cuj Scoped jank metrics
40   *
41   * @param {CujMetricData} metricData Parsed metric data for the cuj scoped jank
42   * @param {Trace} ctx PluginContextTrace for trace related properties and methods
43   * @returns {void} Adds one track for Jank CUJ slice and one for Janky CUJ frames
44   */
45  public async addMetricTrack(metricData: CujMetricData, ctx: Trace) {
46    this.pinSingleCuj(ctx, metricData.cujName);
47  }
48
49  private pinSingleCuj(ctx: Trace, cujName: string) {
50    const trackName = `Jank CUJ: ${cujName}`;
51    addJankCUJDebugTrack(ctx, trackName, cujName);
52  }
53}
54
55export const pinCujInstance = new PinCujMetricHandler();
56