| // Copyright (C) 2024 The Android Open Source Project | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| import m from 'mithril'; | |
| import { | |
| type Aggregator, | |
| type AggregatorGridConfig, | |
| createAggregationData, | |
| } from '../../components/aggregation_adapter'; | |
| import type {AreaSelection} from '../../public/selection'; | |
| import type {Engine} from '../../trace_processor/engine'; | |
| import type {SqlValue} from '../../trace_processor/query_result'; | |
| import {createPerfettoTable} from '../../trace_processor/sql_utils'; | |
| import {RadioGroup} from '../../widgets/radio_group'; | |
| import { | |
| type WattsonTaskSummary, | |
| getWattsonTrackSelection, | |
| } from './task_summary'; | |
| import {formatPercentValue} from '../../components/aggregation_panel'; | |
| export class WattsonProcessSelectionAggregator implements Aggregator { | |
| readonly id = 'wattson_plugin_process_aggregation'; | |
| private scaleNumericData: boolean = false; | |
| constructor(private readonly taskSummary: WattsonTaskSummary) {} | |
| probe(area: AreaSelection) { | |
| const selection = getWattsonTrackSelection(area); | |
| if (selection.cpus.length === 0) return undefined; | |
| return { | |
| getGridConfig: () => this.getGridConfig(), | |
| prepareData: async (engine: Engine) => { | |
| await this.taskSummary.build(area, selection); | |
| const table = await createPerfettoTable({ | |
| engine, | |
| as: ` | |
| WITH base AS ( | |
| SELECT | |
| ROUND(SUM(estimated_mw), 3) as active_mw, | |
| ROUND(SUM(estimated_mws), 3) as active_mws, | |
| ROUND(SUM(idle_transitions_mws), 3) as idle_cost_mws, | |
| ROUND(SUM(total_mws), 3) as total_mws, | |
| pid, | |
| process_name, | |
| upid | |
| FROM wattson_plugin_thread_summary | |
| GROUP BY upid | |
| ) | |
| SELECT | |
| *, | |
| total_mws / (SUM(total_mws) OVER()) AS percent_of_total_energy | |
| FROM base | |
| `, | |
| }); | |
| return createAggregationData(table); | |
| }, | |
| }; | |
| } | |
| renderTopbarControls(): m.Children { | |
| return m( | |
| RadioGroup, | |
| { | |
| selectedValue: this.scaleNumericData ? 'uw' : 'mw', | |
| onValueChange: (value) => { | |
| this.scaleNumericData = value === 'uw'; | |
| }, | |
| title: 'Select power units', | |
| }, | |
| [ | |
| m(RadioGroup.Button, {value: 'uw'}, 'µW'), | |
| m(RadioGroup.Button, {value: 'mw'}, 'mW'), | |
| ], | |
| ); | |
| } | |
| private renderMilliwatts(value: SqlValue): m.Children { | |
| if (this.scaleNumericData && typeof value === 'number') { | |
| return value * 1000; | |
| } | |
| return String(value); | |
| } | |
| private getGridConfig(): AggregatorGridConfig { | |
| const powerUnits = this.scaleNumericData ? 'µW' : 'mW'; | |
| const energyUnits = this.scaleNumericData ? 'µWs' : 'mWs'; | |
| return { | |
| schema: { | |
| process_name: {title: 'Process Name', columnType: 'text'}, | |
| pid: {title: 'PID', columnType: 'identifier'}, | |
| active_mw: { | |
| title: `Active power (estimated ${powerUnits})`, | |
| columnType: 'quantitative', | |
| cellRenderer: (v) => this.renderMilliwatts(v), | |
| }, | |
| active_mws: { | |
| title: `Active energy (estimated ${energyUnits})`, | |
| columnType: 'quantitative', | |
| cellRenderer: (v) => this.renderMilliwatts(v), | |
| }, | |
| idle_cost_mws: { | |
| title: `Idle transitions overhead (estimated ${energyUnits})`, | |
| columnType: 'quantitative', | |
| cellRenderer: (v) => this.renderMilliwatts(v), | |
| }, | |
| total_mws: { | |
| title: `Total energy (estimated ${energyUnits})`, | |
| columnType: 'quantitative', | |
| cellRenderer: (v) => this.renderMilliwatts(v), | |
| }, | |
| percent_of_total_energy: { | |
| title: '% of total energy', | |
| columnType: 'quantitative', | |
| cellRenderer: formatPercentValue, | |
| }, | |
| }, | |
| initialColumns: [ | |
| {id: 'process_name', field: 'process_name'}, | |
| {id: 'pid', field: 'pid'}, | |
| {id: 'active_mw', field: 'active_mw', aggregate: 'SUM'}, | |
| {id: 'active_mws', field: 'active_mws', aggregate: 'SUM', sort: 'DESC'}, | |
| {id: 'idle_cost_mws', field: 'idle_cost_mws'}, | |
| {id: 'total_mws', field: 'total_mws', aggregate: 'SUM'}, | |
| {id: 'percent_of_total_energy', field: 'percent_of_total_energy'}, | |
| ], | |
| }; | |
| } | |
| getTabName() { | |
| return 'Wattson by process'; | |
| } | |
| } |