• 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 size 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 {TPDuration, TPTime, tpTimeToCode} from '../common/time';
16
17import {globals, SliceDetails} from './globals';
18import {Panel} from './panel';
19
20// To display process or thread, we want to concatenate their name with ID, but
21// either can be undefined and all the cases need to be considered carefully to
22// avoid `undefined undefined` showing up in the UI. This function does such
23// concatenation.
24//
25// Result can be undefined if both name and process are, in this case result is
26// not going to be displayed in the UI.
27function getDisplayName(name: string|undefined, id: number|undefined): string|
28    undefined {
29  if (name === undefined) {
30    return id === undefined ? undefined : `${id}`;
31  } else {
32    return id === undefined ? name : `${name} ${id}`;
33  }
34}
35
36export abstract class SlicePanel extends Panel {
37  protected computeDuration(ts: TPTime, dur: TPDuration): string {
38    return dur === -1n ? `${globals.state.traceTime.end - ts} (Did not end)` :
39                         tpTimeToCode(dur);
40  }
41
42  protected getProcessThreadDetails(sliceInfo: SliceDetails) {
43    return new Map<string, string|undefined>([
44      ['Thread', getDisplayName(sliceInfo.threadName, sliceInfo.tid)],
45      ['Process', getDisplayName(sliceInfo.processName, sliceInfo.pid)],
46      ['User ID', sliceInfo.uid ? String(sliceInfo.uid) : undefined],
47      ['Package name', sliceInfo.packageName],
48      [
49        'Version code',
50        sliceInfo.versionCode ? String(sliceInfo.versionCode) : undefined,
51      ],
52    ]);
53  }
54}
55