xref: /aosp_15_r20/external/perfetto/ui/src/components/tracks/visualized_args_track.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 m from 'mithril';
16import {Button} from '../../widgets/button';
17import {Icons} from '../../base/semantic_icons';
18import {ThreadSliceTrack} from './thread_slice_track';
19import {uuidv4Sql} from '../../base/uuid';
20import {createView} from '../../trace_processor/sql_utils';
21import {Trace} from '../../public/trace';
22
23export interface VisualizedArgsTrackAttrs {
24  readonly uri: string;
25  readonly trace: Trace;
26  readonly trackId: number;
27  readonly maxDepth: number;
28  readonly argName: string;
29  readonly onClose: () => void;
30}
31
32export class VisualizedArgsTrack extends ThreadSliceTrack {
33  private readonly viewName: string;
34  private readonly argName: string;
35  private readonly onClose: () => void;
36
37  constructor({
38    uri,
39    trace,
40    trackId,
41    maxDepth,
42    argName,
43    onClose,
44  }: VisualizedArgsTrackAttrs) {
45    const uuid = uuidv4Sql();
46    const escapedArgName = argName.replace(/[^a-zA-Z]/g, '_');
47    const viewName = `__arg_visualisation_helper_${escapedArgName}_${uuid}_slice`;
48
49    super(trace, uri, trackId, maxDepth, viewName);
50    this.viewName = viewName;
51    this.argName = argName;
52    this.onClose = onClose;
53  }
54
55  async onInit() {
56    return await createView(
57      this.engine,
58      this.viewName,
59      `
60        with slice_with_arg as (
61          select
62            slice.id,
63            slice.track_id,
64            slice.ts,
65            slice.dur,
66            slice.thread_dur,
67            NULL as cat,
68            args.display_value as name
69          from slice
70          join args using (arg_set_id)
71          where args.key='${this.argName}'
72        )
73        select
74          *,
75          (select count()
76          from ancestor_slice(s1.id) s2
77          join slice_with_arg s3 on s2.id=s3.id
78          ) as depth
79        from slice_with_arg s1
80        order by id
81      `,
82    );
83  }
84
85  getTrackShellButtons(): m.Children {
86    return m(Button, {
87      onclick: () => this.onClose(),
88      icon: Icons.Close,
89      title: 'Close all visualised args tracks for this arg',
90      compact: true,
91    });
92  }
93}
94