• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 '../core/colorizer';
16import {STR_NULL} from '../trace_processor/query_result';
17
18import {
19  BASE_ROW,
20  BaseSliceTrack,
21  BaseSliceTrackTypes,
22  OnSliceClickArgs,
23  OnSliceOverArgs,
24  SLICE_FLAGS_INCOMPLETE,
25  SLICE_FLAGS_INSTANT,
26} from './base_slice_track';
27import {globals} from './globals';
28import {NewTrackArgs} from './track';
29import {renderDuration} from './widgets/duration';
30
31export const NAMED_ROW = {
32  // Base columns (tsq, ts, dur, id, depth).
33  ...BASE_ROW,
34
35  // Impl-specific columns.
36  name: STR_NULL,
37};
38export type NamedRow = typeof NAMED_ROW;
39
40export interface NamedSliceTrackTypes extends BaseSliceTrackTypes {
41  row: NamedRow;
42}
43
44export abstract class NamedSliceTrack<
45  T extends NamedSliceTrackTypes = NamedSliceTrackTypes,
46> extends BaseSliceTrack<T> {
47  constructor(args: NewTrackArgs) {
48    super(args);
49  }
50
51  // This is used by the base class to call iter().
52  getRowSpec(): T['row'] {
53    return NAMED_ROW;
54  }
55
56  // Converts a SQL result row to an "Impl" Slice.
57  rowToSlice(row: T['row']): T['slice'] {
58    const baseSlice = super.rowToSlice(row);
59    // Ignore PIDs or numeric arguments when hashing.
60    const name = row.name || '';
61    const colorScheme = getColorForSlice(name);
62    return {...baseSlice, title: name, colorScheme};
63  }
64
65  onSliceOver(args: OnSliceOverArgs<T['slice']>) {
66    const {title, dur, flags} = args.slice;
67    let duration;
68    if (flags & SLICE_FLAGS_INCOMPLETE) {
69      duration = 'Incomplete';
70    } else if (flags & SLICE_FLAGS_INSTANT) {
71      duration = 'Instant';
72    } else {
73      duration = renderDuration(dur);
74    }
75    args.tooltip = [`${title} - [${duration}]`];
76  }
77
78  onSliceClick(args: OnSliceClickArgs<T['slice']>) {
79    globals.setLegacySelection(
80      {
81        kind: 'SLICE',
82        id: args.slice.id,
83        trackKey: this.trackKey,
84        table: 'slice',
85      },
86      {
87        clearSearch: true,
88        pendingScrollId: undefined,
89        switchToCurrentSelectionTab: true,
90      },
91    );
92  }
93}
94