• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {Timestamp} from 'common/time/time';
18import {ParserTimestampConverter} from 'common/time/timestamp_converter';
19import {CoarseVersion} from 'trace/coarse_version';
20import {
21  CustomQueryParamTypeMap,
22  CustomQueryParserResultTypeMap,
23  CustomQueryType,
24} from 'trace/custom_query';
25import {AbsoluteEntryIndex, EntriesRange} from 'trace/index_types';
26import {Parser} from 'trace/parser';
27import {TraceFile} from 'trace/trace_file';
28import {TraceMetadata} from 'trace/trace_metadata';
29import {TraceType} from 'trace/trace_type';
30import {ParsingUtils} from './parsing_utils';
31
32export abstract class AbstractParser<
33  T extends object,
34  U extends object | bigint | number,
35> implements Parser<T>
36{
37  private timestamps: Timestamp[] | undefined;
38  protected traceFile: TraceFile;
39  protected decodedEntries: U[] = [];
40  protected timestampConverter: ParserTimestampConverter;
41  protected readonly metadata: TraceMetadata | undefined;
42
43  protected abstract getMagicNumber(): undefined | number[];
44  protected abstract decodeTrace(trace: Uint8Array): U[] | Promise<U[]>;
45  protected abstract getTimestamp(decodedEntry: U): Timestamp;
46  protected abstract processDecodedEntry(index: number, decodedEntry: U): T;
47
48  constructor(
49    trace: TraceFile,
50    timestampConverter: ParserTimestampConverter,
51    metadata?: TraceMetadata,
52  ) {
53    this.traceFile = trace;
54    this.timestampConverter = timestampConverter;
55    this.metadata = metadata;
56  }
57
58  async parse() {
59    const traceBuffer = new Uint8Array(await this.traceFile.file.arrayBuffer());
60    ParsingUtils.throwIfMagicNumberDoesNotMatch(
61      traceBuffer,
62      this.getMagicNumber(),
63    );
64    this.decodedEntries = await this.decodeTrace(traceBuffer);
65  }
66
67  getDescriptors(): string[] {
68    return [this.traceFile.getDescriptor()];
69  }
70
71  getLengthEntries(): number {
72    return this.decodedEntries.length;
73  }
74
75  createTimestamps() {
76    this.timestamps = this.decodeTimestamps();
77  }
78
79  getTimestamps(): undefined | Timestamp[] {
80    return this.timestamps;
81  }
82
83  getCoarseVersion(): CoarseVersion {
84    return CoarseVersion.LEGACY;
85  }
86
87  getEntry(index: AbsoluteEntryIndex): Promise<T> {
88    const entry = this.processDecodedEntry(index, this.decodedEntries[index]);
89    return Promise.resolve(entry);
90  }
91
92  customQuery<Q extends CustomQueryType>(
93    type: Q,
94    entriesRange: EntriesRange,
95    param?: CustomQueryParamTypeMap[Q],
96  ): Promise<CustomQueryParserResultTypeMap[Q]> {
97    throw new Error('Not implemented');
98  }
99
100  private decodeTimestamps(): Timestamp[] {
101    return this.decodedEntries.map((entry) => this.getTimestamp(entry));
102  }
103
104  abstract getTraceType(): TraceType;
105  abstract getRealToBootTimeOffsetNs(): bigint | undefined;
106  abstract getRealToMonotonicTimeOffsetNs(): bigint | undefined;
107}
108