xref: /aosp_15_r20/external/perfetto/ui/src/components/sql_utils/core_types.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 {Brand} from '../../base/brand';
16
17// Type-safe aliases for various flavours of ints Trace Processor exposes
18// (e.g. timestamp or ids into a given SQL table) and functions to work with
19// them.
20//
21// These rely on TypeScript's type branding: extending a number with additional
22// compile-time-only type information, which prevents "implicit" conversions
23// between different ids.
24
25// Unique id for a process, id into |process| table.
26export type Upid = number & {
27  __type: 'Upid';
28};
29
30export function asUpid(v: number): Upid;
31export function asUpid(v?: number): Upid | undefined;
32export function asUpid(v?: number): Upid | undefined {
33  return v as Upid | undefined;
34}
35
36// Unique id for a thread, id into |thread| table.
37export type Utid = number & {
38  __type: 'Utid';
39};
40
41export function asUtid(v: number): Utid;
42export function asUtid(v?: number): Utid | undefined;
43export function asUtid(v?: number): Utid | undefined {
44  return v as Utid | undefined;
45}
46
47// Id into |slice| SQL table.
48export type SliceSqlId = number & {
49  __type: 'SliceSqlId';
50};
51
52export function asSliceSqlId(v: number): SliceSqlId;
53export function asSliceSqlId(v?: number): SliceSqlId | undefined;
54export function asSliceSqlId(v?: number): SliceSqlId | undefined {
55  return v as SliceSqlId | undefined;
56}
57
58// Id into |sched| SQL table.
59export type SchedSqlId = number & {
60  __type: 'SchedSqlId';
61};
62
63export function asSchedSqlId(v: number): SchedSqlId;
64export function asSchedSqlId(v?: number): SchedSqlId | undefined;
65export function asSchedSqlId(v?: number): SchedSqlId | undefined {
66  return v as SchedSqlId | undefined;
67}
68
69// Id into |thread_state| SQL table.
70export type ThreadStateSqlId = number & {
71  __type: 'ThreadStateSqlId';
72};
73
74export function asThreadStateSqlId(v: number): ThreadStateSqlId;
75export function asThreadStateSqlId(v?: number): ThreadStateSqlId | undefined;
76export function asThreadStateSqlId(v?: number): ThreadStateSqlId | undefined {
77  return v as ThreadStateSqlId | undefined;
78}
79
80export type ArgSetId = Brand<number, 'ArgSetId'>;
81
82export function asArgSetId(v: number): ArgSetId;
83export function asArgSetId(v?: number): ArgSetId | undefined;
84export function asArgSetId(v?: number): ArgSetId | undefined {
85  return v as ArgSetId | undefined;
86}
87
88// Id into |args| SQL table.
89export type ArgsId = Brand<number, 'ArgsId'>;
90
91export function asArgId(v: number): ArgsId;
92export function asArgId(v?: number): ArgsId | undefined;
93export function asArgId(v?: number): ArgsId | undefined {
94  return v as ArgsId | undefined;
95}
96