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