• 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';
18import {ParserTimestampConverter} from 'common/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 {TraceType} from 'trace/trace_type';
29import {ParsingUtils} from './parsing_utils';
30
31export abstract class AbstractParser<T extends object = object>
32  implements Parser<T>
33{
34  private timestamps: Timestamp[] | undefined;
35  protected traceFile: TraceFile;
36  protected decodedEntries: any[] = [];
37  protected timestampConverter: ParserTimestampConverter;
38
39  protected abstract getMagicNumber(): undefined | number[];
40  protected abstract decodeTrace(trace: Uint8Array): any[];
41  protected abstract getTimestamp(decodedEntry: any): Timestamp;
42  protected abstract processDecodedEntry(index: number, decodedEntry: any): any;
43
44  constructor(trace: TraceFile, timestampConverter: ParserTimestampConverter) {
45    this.traceFile = trace;
46    this.timestampConverter = timestampConverter;
47  }
48
49  async parse() {
50    const traceBuffer = new Uint8Array(await this.traceFile.file.arrayBuffer());
51    ParsingUtils.throwIfMagicNumberDoesNotMatch(
52      traceBuffer,
53      this.getMagicNumber(),
54    );
55    this.decodedEntries = this.decodeTrace(traceBuffer);
56  }
57
58  getDescriptors(): string[] {
59    return [this.traceFile.getDescriptor()];
60  }
61
62  getLengthEntries(): number {
63    return this.decodedEntries.length;
64  }
65
66  createTimestamps() {
67    this.timestamps = this.decodeTimestamps();
68  }
69
70  getTimestamps(): undefined | Timestamp[] {
71    return this.timestamps;
72  }
73
74  getCoarseVersion(): CoarseVersion {
75    return CoarseVersion.LEGACY;
76  }
77
78  getEntry(index: AbsoluteEntryIndex): Promise<T> {
79    const entry = this.processDecodedEntry(index, this.decodedEntries[index]);
80    return Promise.resolve(entry);
81  }
82
83  customQuery<Q extends CustomQueryType>(
84    type: Q,
85    entriesRange: EntriesRange,
86    param?: CustomQueryParamTypeMap[Q],
87  ): Promise<CustomQueryParserResultTypeMap[Q]> {
88    throw new Error('Not implemented');
89  }
90
91  private decodeTimestamps(): Timestamp[] {
92    return this.decodedEntries.map((entry) => this.getTimestamp(entry));
93  }
94
95  abstract getTraceType(): TraceType;
96  abstract getRealToBootTimeOffsetNs(): bigint | undefined;
97  abstract getRealToMonotonicTimeOffsetNs(): bigint | undefined;
98}
99