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 {Timestamp} from 'common/time'; 18import {AbstractParser} from 'parsers/legacy/abstract_parser'; 19import {EntryPropertiesTreeFactory} from 'parsers/transitions/entry_properties_tree_factory'; 20import root from 'protos/transitions/udc/json'; 21import {com} from 'protos/transitions/udc/static'; 22import {TraceType} from 'trace/trace_type'; 23import {PropertyTreeNode} from 'trace/tree_node/property_tree_node'; 24 25export class ParserTransitionsShell extends AbstractParser<PropertyTreeNode> { 26 private static readonly WmShellTransitionsTraceProto = root.lookupType( 27 'com.android.wm.shell.WmShellTransitionTraceProto', 28 ); 29 30 private realToBootTimeOffsetNs: bigint | undefined; 31 private handlerMapping: undefined | {[key: number]: string}; 32 33 override getTraceType(): TraceType { 34 return TraceType.SHELL_TRANSITION; 35 } 36 37 override getRealToBootTimeOffsetNs(): bigint | undefined { 38 return this.realToBootTimeOffsetNs; 39 } 40 41 override getRealToMonotonicTimeOffsetNs(): bigint | undefined { 42 return undefined; 43 } 44 45 override decodeTrace( 46 traceBuffer: Uint8Array, 47 ): com.android.wm.shell.ITransition[] { 48 const decodedProto = 49 ParserTransitionsShell.WmShellTransitionsTraceProto.decode( 50 traceBuffer, 51 ) as unknown as com.android.wm.shell.IWmShellTransitionTraceProto; 52 53 const timeOffset = BigInt( 54 decodedProto.realToElapsedTimeOffsetNanos?.toString() ?? '0', 55 ); 56 this.realToBootTimeOffsetNs = timeOffset !== 0n ? timeOffset : undefined; 57 58 this.handlerMapping = {}; 59 for (const mapping of decodedProto.handlerMappings ?? []) { 60 this.handlerMapping[mapping.id] = mapping.name; 61 } 62 63 return decodedProto.transitions ?? []; 64 } 65 66 override processDecodedEntry( 67 index: number, 68 entryProto: com.android.wm.shell.ITransition, 69 ): PropertyTreeNode { 70 return this.makePropertiesTree(entryProto); 71 } 72 73 protected override getTimestamp( 74 entry: com.android.wm.shell.ITransition, 75 ): Timestamp { 76 return entry.dispatchTimeNs 77 ? this.timestampConverter.makeTimestampFromBootTimeNs( 78 BigInt(entry.dispatchTimeNs.toString()), 79 ) 80 : this.timestampConverter.makeZeroTimestamp(); 81 } 82 83 protected getMagicNumber(): number[] | undefined { 84 return [0x09, 0x57, 0x4d, 0x53, 0x54, 0x52, 0x41, 0x43, 0x45]; // .WMSTRACE 85 } 86 87 private validateShellTransitionEntry( 88 entry: com.android.wm.shell.ITransition, 89 ) { 90 if (entry.id === 0) { 91 throw new Error('Shell Transitions entry needs non-null id'); 92 } 93 if ( 94 !entry.dispatchTimeNs && 95 !entry.mergeRequestTimeNs && 96 !entry.mergeTimeNs && 97 !entry.abortTimeNs 98 ) { 99 throw new Error( 100 'Shell Transitions entry requires at least one non-null timestamp', 101 ); 102 } 103 if (this.realToBootTimeOffsetNs === undefined) { 104 throw new Error('Shell Transitions trace missing realToBootTimeOffsetNs'); 105 } 106 if (this.handlerMapping === undefined) { 107 throw new Error('Shell Transitions trace missing handler mapping'); 108 } 109 } 110 111 private makePropertiesTree( 112 entryProto: com.android.wm.shell.ITransition, 113 ): PropertyTreeNode { 114 this.validateShellTransitionEntry(entryProto); 115 116 const shellEntryTree = EntryPropertiesTreeFactory.makeShellPropertiesTree({ 117 entry: entryProto, 118 realToBootTimeOffsetNs: this.realToBootTimeOffsetNs, 119 handlerMapping: this.handlerMapping, 120 timestampConverter: this.timestampConverter, 121 }); 122 const wmEntryTree = EntryPropertiesTreeFactory.makeWmPropertiesTree(); 123 124 return EntryPropertiesTreeFactory.makeTransitionPropertiesTree( 125 shellEntryTree, 126 wmEntryTree, 127 ); 128 } 129} 130