xref: /aosp_15_r20/external/perfetto/ui/src/components/tracks/named_slice_track.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2021 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 {getColorForSlice} from '../colorizer';
16import {TrackEventDetailsPanel} from '../../public/details_panel';
17import {TrackEventSelection} from '../../public/selection';
18import {Slice} from '../../public/track';
19import {LONG, NUM, STR, STR_NULL} from '../../trace_processor/query_result';
20import {
21  BASE_ROW,
22  BaseSliceTrack,
23  OnSliceClickArgs,
24  OnSliceOverArgs,
25  SLICE_FLAGS_INCOMPLETE,
26  SLICE_FLAGS_INSTANT,
27} from './base_slice_track';
28import {ThreadSliceDetailsPanel} from '../details/thread_slice_details_tab';
29import {TraceImpl} from '../../core/trace_impl';
30import {assertIsInstance} from '../../base/logging';
31import {SourceDataset, Dataset} from '../../trace_processor/dataset';
32import {formatDuration} from '../time_utils';
33import {Trace} from '../../public/trace';
34
35export const NAMED_ROW = {
36  // Base columns (tsq, ts, dur, id, depth).
37  ...BASE_ROW,
38
39  // Impl-specific columns.
40  name: STR_NULL,
41};
42export type NamedRow = typeof NAMED_ROW;
43
44export abstract class NamedSliceTrack<
45  SliceType extends Slice = Slice,
46  RowType extends NamedRow = NamedRow,
47> extends BaseSliceTrack<SliceType, RowType> {
48  constructor(trace: Trace, uri: string) {
49    super(trace, uri);
50  }
51
52  // Converts a SQL result row to an "Impl" Slice.
53  protected rowToSliceBase(row: RowType): Slice {
54    const baseSlice = super.rowToSliceBase(row);
55    // Ignore PIDs or numeric arguments when hashing.
56    const name = row.name ?? '';
57    const colorScheme = getColorForSlice(name);
58    return {...baseSlice, title: name, colorScheme};
59  }
60
61  onSliceOver(args: OnSliceOverArgs<SliceType>) {
62    const {title, dur, flags} = args.slice;
63    let duration;
64    if (flags & SLICE_FLAGS_INCOMPLETE) {
65      duration = 'Incomplete';
66    } else if (flags & SLICE_FLAGS_INSTANT) {
67      duration = 'Instant';
68    } else {
69      duration = formatDuration(this.trace, dur);
70    }
71    args.tooltip = [`${title} - [${duration}]`];
72  }
73
74  onSliceClick(args: OnSliceClickArgs<SliceType>) {
75    this.trace.selection.selectTrackEvent(this.uri, args.slice.id);
76  }
77
78  detailsPanel(_sel: TrackEventSelection): TrackEventDetailsPanel {
79    // Rationale for the assertIsInstance: ThreadSliceDetailsPanel requires a
80    // TraceImpl (because of flows) but here we must take a Trace interface,
81    // because this class is exposed to plugins (which see only Trace).
82    return new ThreadSliceDetailsPanel(assertIsInstance(this.trace, TraceImpl));
83  }
84
85  override getDataset(): Dataset | undefined {
86    return new SourceDataset({
87      src: this.getSqlSource(),
88      schema: {
89        id: NUM,
90        name: STR,
91        ts: LONG,
92        dur: LONG,
93      },
94    });
95  }
96}
97