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 {Analytics} from 'logging/analytics'; 18import {Trace, TraceEntry} from './trace'; 19import {TracePosition} from './trace_position'; 20import {TraceTypeUtils} from './trace_type'; 21 22export class TraceEntryFinder { 23 static findCorrespondingEntry<T>( 24 trace: Trace<T>, 25 position: TracePosition, 26 ): TraceEntry<T> | undefined { 27 if (trace.lengthEntries === 0) { 28 return undefined; 29 } 30 31 if (trace.isDumpWithoutTimestamp()) { 32 // always display dumps regardless of the current trace position 33 return trace.getEntry(0); 34 } 35 36 if (position.entry?.getFullTrace() === trace.getEntry(0).getFullTrace()) { 37 return position.entry as TraceEntry<T>; 38 } 39 40 if (position.frame !== undefined && trace.hasFrameInfo()) { 41 try { 42 const frame = trace.getFrame(position.frame); 43 if (frame.lengthEntries > 0) { 44 return frame.getEntry(0); 45 } 46 } catch (e) { 47 console.warn(`Could not retrieve frame: ${(e as Error).message}`); 48 Analytics.Error.logFrameMapError((e as Error).message); 49 } 50 } 51 52 if (position.entry) { 53 const entryTraceType = position.entry.getFullTrace().type; 54 const timestamp = position.entry.getTimestamp(); 55 if (TraceTypeUtils.compareByUiPipelineOrder(entryTraceType, trace.type)) { 56 return ( 57 trace.findFirstGreaterEntry(timestamp) ?? 58 trace.findFirstGreaterOrEqualEntry(timestamp) 59 ); 60 } else { 61 return ( 62 trace.findLastLowerEntry(timestamp) ?? 63 trace.findLastLowerOrEqualEntry(timestamp) 64 ); 65 } 66 } 67 68 return trace.findLastLowerOrEqualEntry(position.timestamp); 69 } 70} 71