1// Copyright (C) 2024 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {NUM} from '../../trace_processor/query_result'; 16import {Trace} from '../../public/trace'; 17import {PerfettoPlugin} from '../../public/plugin'; 18 19// List of tracks to pin 20const TRACKS_TO_PIN: string[] = [ 21 'Actual Timeline', 22 'Expected Timeline', 23 'ndroid.systemui', 24 'IKeyguardService', 25 'Transition:', 26 'L<', 27 'UI Events', 28]; 29const SYSTEM_UI_PROCESS: string = 'com.android.systemui'; 30 31// Plugin that pins the tracks relevant to System UI 32export default class implements PerfettoPlugin { 33 static readonly id = 'dev.perfetto.PinSysUITracks'; 34 async onTraceLoad(ctx: Trace): Promise<void> { 35 // Find the upid for the sysui process 36 const result = await ctx.engine.query(` 37 INCLUDE PERFETTO MODULE android.process_metadata; 38 select 39 _process_available_info_summary.upid 40 from _process_available_info_summary 41 join process using(upid) 42 where process.name = 'com.android.systemui'; 43 `); 44 if (result.numRows() === 0) { 45 return; 46 } 47 const sysuiUpid = result.firstRow({ 48 upid: NUM, 49 }).upid; 50 51 ctx.commands.registerCommand({ 52 id: 'dev.perfetto.PinSysUITracks#PinSysUITracks', 53 name: 'Pin: System UI Related Tracks', 54 callback: () => { 55 ctx.workspace.flatTracks.forEach((track) => { 56 if (!track.uri) return; 57 // Ensure we only grab tracks that are in the SysUI process group 58 if (!track.uri.startsWith(`/process_${sysuiUpid}`)) return; 59 if ( 60 !TRACKS_TO_PIN.some((trackName) => 61 track.title.startsWith(trackName), 62 ) 63 ) { 64 return; 65 } 66 track.pin(); 67 }); 68 69 // expand the sysui process tracks group 70 ctx.workspace.flatTracks.forEach((track) => { 71 if (track.hasChildren && track.title.startsWith(SYSTEM_UI_PROCESS)) { 72 track.expand(); 73 } 74 }); 75 }, 76 }); 77 } 78} 79