1/* 2 * Copyright (C) 2023 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 {Parser} from 'trace/parser'; 18import {Timestamp, TimestampType} from 'trace/timestamp'; 19import {TraceType} from 'trace/trace_type'; 20 21export abstract class AbstractTracesParser<T> implements Parser<T> { 22 private timestampsSet: boolean = false; 23 private timestamps: Map<TimestampType, Timestamp[]> = new Map<TimestampType, Timestamp[]>(); 24 25 abstract parse(): Promise<void>; 26 27 abstract getDescriptors(): string[]; 28 29 abstract getTraceType(): TraceType; 30 31 abstract getEntry(index: number, timestampType: TimestampType): Promise<T>; 32 33 abstract getLengthEntries(): number; 34 35 getTimestamps(type: TimestampType): Timestamp[] | undefined { 36 return this.timestamps.get(type); 37 } 38 39 protected async parseTimestamps() { 40 for (const type of [TimestampType.ELAPSED, TimestampType.REAL]) { 41 const timestamps: Timestamp[] = []; 42 let areTimestampsValid = true; 43 44 for (let index = 0; index < this.getLengthEntries(); index++) { 45 const entry = await this.getEntry(index, type); 46 const timestamp = this.getTimestamp(type, entry); 47 if (timestamp === undefined) { 48 areTimestampsValid = false; 49 break; 50 } 51 timestamps.push(timestamp); 52 } 53 54 if (areTimestampsValid) { 55 this.timestamps.set(type, timestamps); 56 } 57 } 58 59 this.timestampsSet = true; 60 } 61 62 protected abstract getTimestamp(type: TimestampType, decodedEntry: any): undefined | Timestamp; 63} 64