1// Copyright (C) 2021 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 {Plugin, PluginContextTrace, PluginDescriptor} from '../../public'; 16import {ThreadSliceTrack} from '../../frontend/thread_slice_track'; 17import {NUM, NUM_NULL, STR} from '../../trace_processor/query_result'; 18import {COUNTER_TRACK_KIND} from '../counter'; 19import {TraceProcessorCounterTrack} from '../counter/trace_processor_counter_track'; 20import {THREAD_SLICE_TRACK_KIND} from '../../public'; 21 22class AnnotationPlugin implements Plugin { 23 async onTraceLoad(ctx: PluginContextTrace): Promise<void> { 24 await this.addAnnotationTracks(ctx); 25 await this.addAnnotationCounterTracks(ctx); 26 } 27 28 private async addAnnotationTracks(ctx: PluginContextTrace) { 29 const {engine} = ctx; 30 31 const result = await engine.query(` 32 select id, name 33 from annotation_slice_track 34 order by name 35 `); 36 37 const it = result.iter({ 38 id: NUM, 39 name: STR, 40 }); 41 42 for (; it.valid(); it.next()) { 43 const id = it.id; 44 const name = it.name; 45 46 ctx.registerTrack({ 47 uri: `perfetto.Annotation#${id}`, 48 displayName: name, 49 kind: THREAD_SLICE_TRACK_KIND, 50 tags: { 51 metric: true, 52 }, 53 trackFactory: ({trackKey}) => { 54 return new ThreadSliceTrack( 55 { 56 engine: ctx.engine, 57 trackKey, 58 }, 59 id, 60 0, 61 'annotation_slice', 62 ); 63 }, 64 }); 65 } 66 } 67 68 private async addAnnotationCounterTracks(ctx: PluginContextTrace) { 69 const {engine} = ctx; 70 const counterResult = await engine.query(` 71 SELECT 72 id, 73 name, 74 min_value as minValue, 75 max_value as maxValue 76 FROM annotation_counter_track`); 77 78 const counterIt = counterResult.iter({ 79 id: NUM, 80 name: STR, 81 minValue: NUM_NULL, 82 maxValue: NUM_NULL, 83 }); 84 85 for (; counterIt.valid(); counterIt.next()) { 86 const trackId = counterIt.id; 87 const name = counterIt.name; 88 89 ctx.registerTrack({ 90 uri: `perfetto.Annotation#counter${trackId}`, 91 displayName: name, 92 kind: COUNTER_TRACK_KIND, 93 tags: { 94 metric: true, 95 }, 96 trackFactory: (trackCtx) => { 97 return new TraceProcessorCounterTrack({ 98 engine: ctx.engine, 99 trackKey: trackCtx.trackKey, 100 trackId, 101 rootTable: 'annotation_counter', 102 }); 103 }, 104 }); 105 } 106 } 107} 108 109export const plugin: PluginDescriptor = { 110 pluginId: 'perfetto.Annotation', 111 plugin: AnnotationPlugin, 112}; 113