1 2-- Copyright 2024 The Android Open Source Project 3-- 4-- Licensed under the Apache License, Version 2.0 (the "License"); 5-- you may not use this file except in compliance with the License. 6-- You may obtain a copy of the License at 7-- 8-- https://www.apache.org/licenses/LICENSE-2.0 9-- 10-- Unless required by applicable law or agreed to in writing, software 11-- distributed under the License is distributed on an "AS IS" BASIS, 12-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13-- See the License for the specific language governing permissions and 14-- limitations under the License. 15 16INCLUDE PERFETTO MODULE wattson.curves.estimates; 17INCLUDE PERFETTO MODULE wattson.curves.idle_attribution; 18INCLUDE PERFETTO MODULE viz.summary.threads_w_processes; 19 20-- Take only the Wattson estimations that are in the window of interest 21DROP VIEW IF EXISTS _windowed_wattson; 22CREATE PERFETTO VIEW _windowed_wattson AS 23SELECT 24 ii.ts, 25 ii.dur, 26 ii.id_1 as period_id, 27 ss.cpu0_mw, 28 ss.cpu1_mw, 29 ss.cpu2_mw, 30 ss.cpu3_mw, 31 ss.cpu4_mw, 32 ss.cpu5_mw, 33 ss.cpu6_mw, 34 ss.cpu7_mw, 35 ss.dsu_scu_mw 36FROM _interval_intersect!( 37 ( 38 _ii_subquery!(_system_state_mw), 39 (SELECT ts, dur, period_id as id FROM {{window_table}}) 40 ), 41 () 42) ii 43JOIN _system_state_mw AS ss ON ss._auto_id = id_0; 44 45-- "Unpivot" the table so that table can by PARTITIONED BY cpu 46DROP TABLE IF EXISTS _unioned_windowed_wattson; 47CREATE PERFETTO TABLE _unioned_windowed_wattson AS 48 SELECT ts, dur, 0 as cpu, cpu0_mw as estimated_mw, period_id 49 FROM _windowed_wattson 50 WHERE EXISTS (SELECT cpu FROM _dev_cpu_policy_map WHERE 0 = cpu) 51 UNION ALL 52 SELECT ts, dur, 1 as cpu, cpu1_mw as estimated_mw, period_id 53 FROM _windowed_wattson 54 WHERE EXISTS (SELECT cpu FROM _dev_cpu_policy_map WHERE 1 = cpu) 55 UNION ALL 56 SELECT ts, dur, 2 as cpu, cpu2_mw as estimated_mw, period_id 57 FROM _windowed_wattson 58 WHERE EXISTS (SELECT cpu FROM _dev_cpu_policy_map WHERE 2 = cpu) 59 UNION ALL 60 SELECT ts, dur, 3 as cpu, cpu3_mw as estimated_mw, period_id 61 FROM _windowed_wattson 62 WHERE EXISTS (SELECT cpu FROM _dev_cpu_policy_map WHERE 3 = cpu) 63 UNION ALL 64 SELECT ts, dur, 4 as cpu, cpu4_mw as estimated_mw, period_id 65 FROM _windowed_wattson 66 WHERE EXISTS (SELECT cpu FROM _dev_cpu_policy_map WHERE 4 = cpu) 67 UNION ALL 68 SELECT ts, dur, 5 as cpu, cpu5_mw as estimated_mw, period_id 69 FROM _windowed_wattson 70 WHERE EXISTS (SELECT cpu FROM _dev_cpu_policy_map WHERE 5 = cpu) 71 UNION ALL 72 SELECT ts, dur, 6 as cpu, cpu6_mw as estimated_mw, period_id 73 FROM _windowed_wattson 74 WHERE EXISTS (SELECT cpu FROM _dev_cpu_policy_map WHERE 6 = cpu) 75 UNION ALL 76 SELECT ts, dur, 7 as cpu, cpu7_mw as estimated_mw, period_id 77 FROM _windowed_wattson 78 WHERE EXISTS (SELECT cpu FROM _dev_cpu_policy_map WHERE 7 = cpu) 79 UNION ALL 80 SELECT ts, dur, -1 as cpu, dsu_scu_mw as estimated_mw, period_id 81 FROM _windowed_wattson; 82 83DROP TABLE IF EXISTS _windowed_threads_system_state; 84CREATE PERFETTO TABLE _windowed_threads_system_state AS 85SELECT 86 ii.ts, 87 ii.dur, 88 ii.cpu, 89 uw.estimated_mw, 90 s.thread_name, 91 s.process_name, 92 s.tid, 93 s.pid, 94 s.utid, 95 uw.period_id 96FROM _interval_intersect!( 97 ( 98 _ii_subquery!(_unioned_windowed_wattson), 99 _ii_subquery!(_sched_w_thread_process_package_summary) 100 ), 101 (cpu) 102) ii 103JOIN _unioned_windowed_wattson AS uw ON uw._auto_id = id_0 104JOIN _sched_w_thread_process_package_summary AS s ON s._auto_id = id_1; 105 106-- Get idle overhead attribution per thread 107DROP VIEW IF EXISTS _per_thread_idle_attribution; 108CREATE PERFETTO VIEW _per_thread_idle_attribution AS 109SELECT 110 SUM(cost.idle_cost_mws) as idle_cost_mws, 111 cost.utid, 112 period_window.period_id 113FROM {{window_table}} AS period_window 114CROSS JOIN _filter_idle_attribution(period_window.ts, period_window.dur) AS cost 115GROUP BY utid, period_id; 116 117-- Group by unique thread ID and disregard CPUs, summing of power over all CPUs 118-- and all instances of the thread 119DROP VIEW IF EXISTS _wattson_thread_attribution; 120CREATE PERFETTO VIEW _wattson_thread_attribution AS 121SELECT 122 -- active time of thread divided by total time where Wattson is defined 123 SUM(estimated_mw * dur) / 1000000000 as estimated_mws, 124 ( 125 SUM(estimated_mw * dur) / (SELECT SUM(dur) from _windowed_wattson) 126 ) as estimated_mw, 127 -- Output zero idle cost for threads that don't cause wakeup 128 COALESCE(idle_cost_mws, 0) as idle_cost_mws, 129 thread_name, 130 -- Ensure that all threads have the process field 131 COALESCE(process_name, '') as process_name, 132 tid, 133 pid, 134 period_id 135FROM _windowed_threads_system_state 136LEFT JOIN _per_thread_idle_attribution USING (utid, period_id) 137GROUP BY utid, period_id 138ORDER BY estimated_mw DESC; 139 140-- Create proto format task attribution for each period 141DROP VIEW IF EXISTS _wattson_per_task; 142CREATE PERFETTO VIEW _wattson_per_task AS 143SELECT 144 period_id, 145 ( 146 SELECT RepeatedField( 147 AndroidWattsonTaskInfo( 148 'estimated_mws', ROUND(estimated_mws, 6), 149 'estimated_mw', ROUND(estimated_mw, 6), 150 'idle_transitions_mws', ROUND(idle_cost_mws, 6), 151 'total_mws', ROUND(estimated_mws + idle_cost_mws, 6), 152 'thread_name', thread_name, 153 'process_name', process_name, 154 'thread_id', tid, 155 'process_id', pid 156 ) 157 ) 158 ) as proto 159FROM _wattson_thread_attribution 160GROUP BY period_id; 161 162