|
| 1 | +// Copyright (C) 2025 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 | + |
| 15 | +import {BaseCounterTrack} from '../../components/tracks/base_counter_track'; |
| 16 | +import {Trace} from '../../public/trace'; |
| 17 | +import {createUnionDataset, Dataset} from '../../trace_processor/dataset'; |
| 18 | +import {LONG} from '../../trace_processor/query_result'; |
| 19 | + |
| 20 | +export const SLICE_TRACK_SUMMARY_KIND = 'SliceTrackSummary'; |
| 21 | + |
| 22 | +export interface Config { |
| 23 | + pidForColor: number | bigint; |
| 24 | + upid: number | null; |
| 25 | + utid: number | null; |
| 26 | +} |
| 27 | + |
| 28 | +export class SliceTrackSummary extends BaseCounterTrack { |
| 29 | + constructor(trace: Trace, uri: string, _config: Config) { |
| 30 | + super(trace, uri); |
| 31 | + } |
| 32 | + |
| 33 | + async onInit() { |
| 34 | + // Include the intervals module |
| 35 | + await this.engine.query('INCLUDE PERFETTO MODULE intervals.overlap'); |
| 36 | + } |
| 37 | + |
| 38 | + getSqlSource(): string { |
| 39 | + // Get the TrackNode for this track |
| 40 | + const trackNode = this.trace.workspaces.currentWorkspace.getTrackByUri( |
| 41 | + this.uri, |
| 42 | + ); |
| 43 | + if (trackNode === undefined) { |
| 44 | + // If we can't find our track node, return empty query |
| 45 | + return 'select cast(0 as bigint) as ts, cast(0 as real) as value where 0'; |
| 46 | + } |
| 47 | + |
| 48 | + // Get all child track datasets |
| 49 | + const childDatasets: Dataset[] = []; |
| 50 | + for (const child of trackNode.children) { |
| 51 | + if (child.uri === undefined) continue; |
| 52 | + |
| 53 | + const childTrack = this.trace.tracks.getTrack(child.uri); |
| 54 | + if (childTrack === undefined) continue; |
| 55 | + |
| 56 | + const dataset = childTrack.renderer.getDataset?.(); |
| 57 | + if (dataset !== undefined) { |
| 58 | + childDatasets.push(dataset); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // If no child datasets, return empty query |
| 63 | + if (childDatasets.length === 0) { |
| 64 | + return 'select cast(0 as bigint) as ts, cast(0 as real) as value where 0'; |
| 65 | + } |
| 66 | + |
| 67 | + // Create a union of all child datasets |
| 68 | + const unionDataset = createUnionDataset(childDatasets).optimize(); |
| 69 | + |
| 70 | + // Check if the union dataset implements the required schema (ts and dur columns) |
| 71 | + const requiredSchema = {ts: LONG, dur: LONG}; |
| 72 | + if (!unionDataset.implements(requiredSchema)) { |
| 73 | + // Dataset doesn't have ts/dur, can't create summary |
| 74 | + return 'select cast(0 as bigint) as ts, cast(0 as real) as value where 0'; |
| 75 | + } |
| 76 | + |
| 77 | + // Use intervals_overlap_count to compute the number of active intervals |
| 78 | + // across all child tracks at any point in time |
| 79 | + return ` |
| 80 | + select ts, cast(value as real) as value |
| 81 | + from intervals_overlap_count!( |
| 82 | + ( |
| 83 | + select ts, dur |
| 84 | + from (${unionDataset.query()}) |
| 85 | + where dur != -1 |
| 86 | + ), |
| 87 | + ts, |
| 88 | + dur |
| 89 | + ) |
| 90 | + `; |
| 91 | + } |
| 92 | +} |
0 commit comments