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 {Trace} from '../../public/trace'; 16import {PerfettoPlugin} from '../../public/plugin'; 17import {createQuerySliceTrack} from '../../components/tracks/query_slice_track'; 18import {TrackNode} from '../../public/workspace'; 19 20export default class implements PerfettoPlugin { 21 static readonly id = 'com.example.ExampleNestedTracks'; 22 async onTraceLoad(ctx: Trace): Promise<void> { 23 const traceStartTime = ctx.traceInfo.start; 24 const traceDur = ctx.traceInfo.end - ctx.traceInfo.start; 25 await ctx.engine.query(` 26 create table example_events ( 27 id INTEGER PRIMARY KEY AUTOINCREMENT, 28 name TEXT, 29 ts INTEGER, 30 dur INTEGER, 31 arg INTEGER 32 ); 33 34 insert into example_events (name, ts, dur, arg) 35 values 36 ('Foo', ${traceStartTime}, ${traceDur}, 'aaa'), 37 ('Bar', ${traceStartTime}, ${traceDur / 2n}, 'bbb'), 38 ('Baz', ${traceStartTime}, ${traceDur / 3n}, 'bbb'); 39 `); 40 41 const title = 'Test Track'; 42 const uri = `com.example.ExampleNestedTracks#TestTrack`; 43 const track = await createQuerySliceTrack({ 44 trace: ctx, 45 uri, 46 data: { 47 sqlSource: 'select * from example_events', 48 }, 49 }); 50 ctx.tracks.registerTrack({ 51 uri, 52 title, 53 track, 54 }); 55 56 this.addNestedTracks(ctx, uri); 57 } 58 59 private addNestedTracks(ctx: Trace, uri: string): void { 60 const trackRoot = new TrackNode({uri, title: 'Root'}); 61 const track1 = new TrackNode({uri, title: '1'}); 62 const track2 = new TrackNode({uri, title: '2'}); 63 const track11 = new TrackNode({uri, title: '1.1'}); 64 const track12 = new TrackNode({uri, title: '1.2'}); 65 const track121 = new TrackNode({uri, title: '1.2.1'}); 66 const track21 = new TrackNode({uri, title: '2.1'}); 67 68 ctx.workspace.addChildInOrder(trackRoot); 69 trackRoot.addChildLast(track1); 70 trackRoot.addChildLast(track2); 71 track1.addChildLast(track11); 72 track1.addChildLast(track12); 73 track12.addChildLast(track121); 74 track2.addChildLast(track21); 75 } 76} 77