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 android.suspend; 17 18INCLUDE PERFETTO MODULE time.conversion; 19 20INCLUDE PERFETTO MODULE wattson.device_infos; 21 22-- Creates the hotplug slice(s) for each CPU defined to be the region when CPU 23-- is off 24CREATE PERFETTO TABLE _cpu_hotplug_offline AS 25WITH 26 is_different_cpu AS ( 27 -- Set flag for when hotplug CPU processing is being done on separate CPU 28 SELECT 29 s.ts, 30 s.dur, 31 extract_arg(t.dimension_arg_set_id, 'cpu') AS hp_cpu, 32 extract_arg(t.dimension_arg_set_id, 'cpu') != extract_arg(s.arg_set_id, 'action_cpu') AS is_different_cpu 33 FROM slice AS s 34 JOIN track AS t 35 ON t.id = s.track_id 36 WHERE 37 t.type = 'cpu_hotplug' 38 ), 39 cpu_transitions AS ( 40 -- AP CPU is CPU being hotplugged out, and BP CPU is CPU that assists the AP 41 -- CPU in hotplugging. The BP CPU could be the AP CPU itself or a different 42 -- CPU. Find the transition points where the BP CPU changes between AP and BP. 43 SELECT 44 ts, 45 hp_cpu, 46 is_different_cpu, 47 lag(is_different_cpu) OVER (PARTITION BY hp_cpu) != is_different_cpu AS is_cpu_transitions 48 FROM is_different_cpu 49 ), 50 transitions_dur AS ( 51 -- Calculates duration between transitions from AP -> BP and BP -> AP 52 SELECT 53 ts, 54 lead(ts, 1, trace_end()) OVER (PARTITION BY hp_cpu) - ts AS dur, 55 hp_cpu AS cpu, 56 is_different_cpu 57 FROM cpu_transitions 58 WHERE 59 is_cpu_transitions 60 ) 61SELECT 62 ts, 63 dur, 64 cpu, 65 -- Sometimes the assignment of AP CPU during hotplugging creates short, 66 -- spurious "pockets" of hotplug events, so assign these slices that are 67 -- shorter than 100us as if they were on the same CPU. 68 iif(is_different_cpu AND dur < time_from_us(100), FALSE, is_different_cpu) AS is_different_cpu 69FROM transitions_dur; 70 71-- Fill gaps from beginning of trace to end of trace so that this table can be 72-- used by interval_intersect(). 73CREATE PERFETTO TABLE _gapless_hotplug_slices AS 74WITH 75 filled_gaps AS ( 76 -- First slice from trace_start() to first offline slice per CPU 77 SELECT 78 cpu, 79 trace_start() AS ts, 80 min(ts) - trace_start() AS dur, 81 FALSE AS offline 82 FROM _cpu_hotplug_offline 83 GROUP BY 84 cpu 85 UNION ALL 86 -- All online and offline regions as defined by cpuhp. This will have 87 -- continuous slices from somewhere in the middle to the end of the trace. 88 SELECT 89 cpu, 90 ts, 91 dur, 92 is_different_cpu AS offline 93 FROM _cpu_hotplug_offline 94 UNION ALL 95 -- Creates a single online slice spanning the entire trace for CPUs that are 96 -- never offline. This is needed for interval_intersect() to not delete 97 -- undefined time periods 98 SELECT 99 cpu, 100 trace_start() AS ts, 101 trace_dur() AS dur, 102 FALSE AS offline 103 FROM _dev_cpu_policy_map 104 WHERE 105 NOT cpu IN ( 106 SELECT 107 cpu 108 FROM _cpu_hotplug_offline 109 ) 110 ) 111SELECT 112 ts, 113 dur, 114 cpu, 115 offline 116FROM filled_gaps 117ORDER BY 118 cpu, 119 ts; 120 121-- Copies suspend state to each CPU defined, so that the suspend state can be 122-- partitioned by cpu during interval_intersect() 123CREATE PERFETTO TABLE _gapless_suspend_slices AS 124SELECT 125 cpu, 126 ts, 127 dur, 128 iif(power_state = 'suspended', TRUE, FALSE) AS suspended 129FROM _dev_cpu_policy_map 130CROSS JOIN android_suspend_state; 131