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 {assertExists} from '../../base/logging'; 16import {TrackEventDetails, TrackEventSelection} from '../../public/selection'; 17import {getColorForSample} from '../../components/colorizer'; 18import { 19 BaseSliceTrack, 20 OnSliceClickArgs, 21} from '../../components/tracks/base_slice_track'; 22import {NAMED_ROW, NamedRow} from '../../components/tracks/named_slice_track'; 23import {NUM} from '../../trace_processor/query_result'; 24import {Slice} from '../../public/track'; 25import {CpuProfileSampleFlamegraphDetailsPanel} from './cpu_profile_details_panel'; 26import {Trace} from '../../public/trace'; 27 28interface CpuProfileRow extends NamedRow { 29 callsiteId: number; 30} 31 32export class CpuProfileTrack extends BaseSliceTrack<Slice, CpuProfileRow> { 33 constructor( 34 trace: Trace, 35 uri: string, 36 private utid: number, 37 ) { 38 super(trace, uri); 39 } 40 41 protected getRowSpec(): CpuProfileRow { 42 return {...NAMED_ROW, callsiteId: NUM}; 43 } 44 45 protected rowToSlice(row: CpuProfileRow): Slice { 46 const baseSlice = super.rowToSliceBase(row); 47 const name = assertExists(row.name); 48 const colorScheme = getColorForSample(row.callsiteId); 49 return {...baseSlice, title: name, colorScheme}; 50 } 51 52 onUpdatedSlices(slices: Slice[]) { 53 for (const slice of slices) { 54 slice.isHighlighted = slice === this.hoveredSlice; 55 } 56 } 57 58 getSqlSource(): string { 59 return ` 60 select 61 p.id, 62 ts, 63 0 as dur, 64 0 as depth, 65 'CPU Sample' as name, 66 callsite_id as callsiteId 67 from cpu_profile_stack_sample p 68 where utid = ${this.utid} 69 order by ts 70 `; 71 } 72 73 onSliceClick({slice}: OnSliceClickArgs<Slice>) { 74 this.trace.selection.selectTrackEvent(this.uri, slice.id); 75 } 76 77 async getSelectionDetails( 78 id: number, 79 ): Promise<TrackEventDetails | undefined> { 80 const baseDetails = await super.getSelectionDetails(id); 81 if (baseDetails === undefined) return undefined; 82 return {...baseDetails, utid: this.utid}; 83 } 84 85 detailsPanel(selection: TrackEventSelection) { 86 const {ts, utid} = selection; 87 return new CpuProfileSampleFlamegraphDetailsPanel( 88 this.trace, 89 ts, 90 assertExists(utid), 91 ); 92 } 93} 94