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 {TimeUtils} from 'common/time_utils'; 18import {Timestamp, TimestampType} from 'trace/timestamp'; 19import {TraceFile} from 'trace/trace_file'; 20import {TraceTreeNode} from 'trace/trace_tree_node'; 21import {TraceType} from 'trace/trace_type'; 22import {AbstractParser} from './abstract_parser'; 23import {InputMethodManagerServiceTraceFileProto} from './proto_types'; 24 25class ParserInputMethodManagerService extends AbstractParser { 26 constructor(trace: TraceFile) { 27 super(trace); 28 this.realToElapsedTimeOffsetNs = undefined; 29 } 30 31 getTraceType(): TraceType { 32 return TraceType.INPUT_METHOD_MANAGER_SERVICE; 33 } 34 35 override getMagicNumber(): number[] { 36 return ParserInputMethodManagerService.MAGIC_NUMBER; 37 } 38 39 override decodeTrace(buffer: Uint8Array): any[] { 40 const decoded = InputMethodManagerServiceTraceFileProto.decode(buffer) as any; 41 if (Object.prototype.hasOwnProperty.call(decoded, 'realToElapsedTimeOffsetNanos')) { 42 this.realToElapsedTimeOffsetNs = BigInt(decoded.realToElapsedTimeOffsetNanos); 43 } else { 44 this.realToElapsedTimeOffsetNs = undefined; 45 } 46 return decoded.entry; 47 } 48 49 protected override getTimestamp(type: TimestampType, entryProto: any): undefined | Timestamp { 50 if (type === TimestampType.ELAPSED) { 51 return new Timestamp(TimestampType.ELAPSED, BigInt(entryProto.elapsedRealtimeNanos)); 52 } else if (type === TimestampType.REAL && this.realToElapsedTimeOffsetNs !== undefined) { 53 return new Timestamp( 54 type, 55 this.realToElapsedTimeOffsetNs + BigInt(entryProto.elapsedRealtimeNanos) 56 ); 57 } 58 return undefined; 59 } 60 61 protected override processDecodedEntry( 62 index: number, 63 timestampType: TimestampType, 64 entryProto: TraceTreeNode 65 ): TraceTreeNode { 66 if (entryProto.elapsedRealtimeNanos === undefined) { 67 throw Error('Missing elapsedRealtimeNanos on entry'); 68 } 69 70 let clockTimeNanos: bigint | undefined = undefined; 71 if ( 72 this.realToElapsedTimeOffsetNs !== undefined && 73 entryProto.elapsedRealtimeNanos !== undefined 74 ) { 75 clockTimeNanos = BigInt(entryProto.elapsedRealtimeNanos) + this.realToElapsedTimeOffsetNs; 76 } 77 78 const timestamp = Timestamp.from( 79 timestampType, 80 BigInt(entryProto.elapsedRealtimeNanos), 81 this.realToElapsedTimeOffsetNs 82 ); 83 84 return { 85 name: TimeUtils.format(timestamp) + ' - ' + entryProto.where, 86 kind: 'InputMethodManagerService entry', 87 children: [ 88 { 89 obj: entryProto.inputMethodManagerService, 90 kind: 'InputMethodManagerService', 91 name: '', 92 children: [], 93 stableId: 'managerservice', 94 id: 'managerservice', 95 }, 96 ], 97 obj: entryProto, 98 stableId: 'entry', 99 id: 'entry', 100 elapsedRealtimeNanos: entryProto.elapsedRealtimeNanos, 101 clockTimeNanos, 102 }; 103 } 104 105 private realToElapsedTimeOffsetNs: undefined | bigint; 106 private static readonly MAGIC_NUMBER = [0x09, 0x49, 0x4d, 0x4d, 0x54, 0x52, 0x41, 0x43, 0x45]; // .IMMTRACE 107} 108 109export {ParserInputMethodManagerService}; 110