• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
16  DurationColumn,
17  ProcessIdColumn,
18  SchedIdColumn,
19  SliceIdColumn,
20  StandardColumn,
21  ThreadIdColumn,
22  ThreadStateIdColumn,
23  TimestampColumn,
24} from '../../components/widgets/sql/table/columns';
25import {TableColumn} from '../../components/widgets/sql/table/table_column';
26import {SqlTableDescription} from '../../components/widgets/sql/table/table_description';
27
28// Handles the access to all of the Perfetto SQL modules accessible to Trace
29//  Processor.
30export interface SqlModules {
31  // Returns all tables/views between all loaded Perfetto SQL modules.
32  listTables(): SqlTable[];
33
34  // Returns names of all tables/views between all loaded Perfetto SQL modules.
35  listTablesNames(): string[];
36
37  // Returns Perfetto SQL table/view if it was loaded in one of the Perfetto
38  // SQL module.
39  getTable(tableName: string): SqlTable | undefined;
40
41  // Returns module that contains Perfetto SQL table/view if it was loaded in one of the Perfetto
42  // SQL module.
43  getModuleForTable(tableName: string): SqlModule | undefined;
44
45  findAllTablesWithLinkedId(tableAndColumn: TableAndColumn): SqlTable[];
46}
47
48// Handles the access to a specific Perfetto SQL Package. Package consists of
49// Perfetto SQL modules.
50export interface SqlPackage {
51  readonly name: string;
52  readonly modules: SqlModule[];
53
54  // Returns all tables/views in this package.
55  listTables(): SqlTable[];
56
57  // Returns names of all tables/views in this package.
58  listTablesNames(): string[];
59
60  getTable(tableName: string): SqlTable | undefined;
61
62  // Returns sqlModule containing table with provided name.
63  getModuleForTable(tableName: string): SqlModule | undefined;
64
65  // Returns sqlTableDescription of the table with provided name.
66  getSqlTableDescription(tableName: string): SqlTableDescription | undefined;
67}
68
69// Handles the access to a specific Perfetto SQL module.
70export interface SqlModule {
71  readonly includeKey: string;
72  readonly dataObjects: SqlTable[];
73  readonly functions: SqlFunction[];
74  readonly tableFunctions: SqlTableFunction[];
75  readonly macros: SqlMacro[];
76
77  // Returns sqlTable with provided name.
78  getTable(tableName: string): SqlTable | undefined;
79
80  // Returns sqlTableDescription of the table with provided name.
81  getSqlTableDescription(tableName: string): SqlTableDescription | undefined;
82}
83
84// The definition of Perfetto SQL table/view.
85export interface SqlTable {
86  readonly name: string;
87  readonly includeKey?: string;
88  readonly description: string;
89  readonly type: string;
90  readonly columns: SqlColumn[];
91
92  readonly idColumn: SqlColumn | undefined;
93  readonly linkedIdColumns: SqlColumn[];
94  readonly joinIdColumns: SqlColumn[];
95
96  // Returns all columns as TableColumns.
97  getTableColumns(): TableColumn[];
98
99  getIdColumns(): SqlColumn[];
100  getJoinIdColumns(): SqlColumn[];
101
102  getIdTables(): TableAndColumn[];
103  getJoinIdTables(): TableAndColumn[];
104}
105
106// The definition of Perfetto SQL function.
107export interface SqlFunction {
108  readonly name: string;
109  readonly description: string;
110  readonly args: SqlArgument[];
111  readonly returnType: string;
112  readonly returnDesc: string;
113}
114
115// The definition of Perfetto SQL table function.
116export interface SqlTableFunction {
117  readonly name: string;
118  readonly description: string;
119  readonly args: SqlArgument[];
120  readonly returnCols: SqlColumn[];
121}
122
123// The definition of Perfetto SQL macro.
124export interface SqlMacro {
125  readonly name: string;
126  readonly description: string;
127  readonly args: SqlArgument[];
128  readonly returnType: string;
129}
130
131// The definition of Perfetto SQL column.
132export interface SqlColumn {
133  readonly name: string;
134  readonly description?: string;
135  readonly type: SqlType;
136}
137
138// The definition of Perfetto SQL argument. Can be used for functions, table
139// functions or macros.
140export interface SqlArgument {
141  readonly name: string;
142  readonly description: string;
143  readonly type: string;
144}
145
146export interface TableAndColumn {
147  table: string;
148  column: string;
149
150  isEqual(o: TableAndColumn): boolean;
151}
152
153export interface SqlType {
154  readonly name: string;
155  readonly shortName: string;
156  readonly tableAndColumn?: TableAndColumn;
157}
158
159export function createTableColumnFromPerfettoSql(
160  col: SqlColumn,
161  tableName: string,
162): TableColumn {
163  if (col.type.shortName === 'timestamp') {
164    return new TimestampColumn(col.name);
165  }
166  if (col.type.shortName === 'duration') {
167    return new DurationColumn(col.name);
168  }
169
170  if (col.type.shortName === 'id') {
171    switch (tableName.toLowerCase()) {
172      case 'slice':
173        return new SliceIdColumn(col.name, {type: 'id'});
174      case 'thread':
175        return new ThreadIdColumn(col.name, {type: 'id'});
176      case 'process':
177        return new ProcessIdColumn(col.name, {type: 'id'});
178      case 'thread_state':
179        return new ThreadStateIdColumn(col.name);
180      case 'sched':
181        return new SchedIdColumn(col.name);
182    }
183    return new StandardColumn(col.name);
184  }
185
186  if (col.type.shortName === 'joinid') {
187    if (col.type.tableAndColumn === undefined) {
188      return new StandardColumn(col.name);
189    }
190    switch (col.type.tableAndColumn.table.toLowerCase()) {
191      case 'slice':
192        return new SliceIdColumn(col.name);
193      case 'thread':
194        return new ThreadIdColumn(col.name);
195      case 'process':
196        return new ProcessIdColumn(col.name);
197      case 'thread_state':
198        return new ThreadStateIdColumn(col.name);
199      case 'sched':
200        return new SchedIdColumn(col.name);
201    }
202  }
203
204  return new StandardColumn(col.name);
205}
206