xref: /aosp_15_r20/external/perfetto/ui/src/components/sql_utils/process.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2023 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 {Engine} from '../../trace_processor/engine';
16import {NUM, NUM_NULL, STR_NULL} from '../../trace_processor/query_result';
17import {fromNumNull} from '../../trace_processor/sql_utils';
18import {Upid} from './core_types';
19
20// TODO(altimin): We should consider implementing some form of cache rather than querying
21// the data from trace processor each time.
22
23export interface ProcessInfo {
24  upid: Upid;
25  pid?: number;
26  name?: string;
27  uid?: number;
28  packageName?: string;
29  versionCode?: number;
30}
31
32export async function getProcessInfo(
33  engine: Engine,
34  upid: Upid,
35): Promise<ProcessInfo> {
36  const res = await engine.query(`
37    include perfetto module android.process_metadata;
38    select
39      p.upid,
40      p.pid,
41      p.name,
42      p.uid,
43      m.package_name as packageName,
44      m.version_code as versionCode
45    from process p
46    left join android_process_metadata m using (upid)
47    where upid = ${upid};
48  `);
49  const row = res.firstRow({
50    upid: NUM,
51    pid: NUM,
52    name: STR_NULL,
53    uid: NUM_NULL,
54    packageName: STR_NULL,
55    versionCode: NUM_NULL,
56  });
57  return {
58    upid,
59    pid: row.pid,
60    name: row.name ?? undefined,
61    uid: fromNumNull(row.uid),
62    packageName: row.packageName ?? undefined,
63    versionCode: fromNumNull(row.versionCode),
64  };
65}
66
67function getDisplayName(
68  name: string | undefined,
69  id: number | undefined,
70): string | undefined {
71  if (name === undefined) {
72    return id === undefined ? undefined : `${id}`;
73  }
74  return id === undefined ? name : `${name} [${id}]`;
75}
76
77export function getProcessName(info?: {
78  name?: string;
79  pid?: number;
80}): string | undefined {
81  return getDisplayName(info?.name, info?.pid);
82}
83