1-- 2-- Copyright 2019 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.battery; 17INCLUDE PERFETTO MODULE android.battery_stats; 18INCLUDE PERFETTO MODULE android.suspend; 19INCLUDE PERFETTO MODULE counters.intervals; 20 21DROP VIEW IF EXISTS battery_view; 22CREATE PERFETTO VIEW battery_view AS 23SELECT * FROM android_battery_charge; 24 25DROP TABLE IF EXISTS android_batt_wakelocks_merged; 26CREATE PERFETTO TABLE android_batt_wakelocks_merged AS 27SELECT 28 MIN(ts) AS ts, 29 MAX(ts_end) AS ts_end 30FROM ( 31 SELECT 32 *, 33 SUM(new_group) OVER (ORDER BY ts) AS group_id 34 FROM ( 35 SELECT 36 ts, 37 ts + dur AS ts_end, 38 -- There is a new group if there was a gap before this wakelock. 39 -- i.e. the max end timestamp of all preceding wakelocks is before 40 -- the start timestamp of this one. 41 -- The null check is for the first row which is always a new group. 42 IFNULL( 43 MAX(ts + dur) OVER ( 44 ORDER BY ts 45 ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING 46 ) < ts, 47 TRUE 48 ) AS new_group 49 FROM slice 50 WHERE slice.name GLOB 'WakeLock *' AND dur != -1 51 ) 52) 53GROUP BY group_id; 54 55-- TODO(simonmacm) remove this shim once no longer used internally 56DROP TABLE IF EXISTS suspend_slice_; 57CREATE PERFETTO TABLE suspend_slice_ AS 58SELECT ts, dur FROM android_suspend_state where power_state = 'suspended'; 59 60DROP TABLE IF EXISTS screen_state_span; 61CREATE PERFETTO TABLE screen_state_span AS 62WITH screen_state AS ( 63 SELECT counter.id, ts, 0 AS track_id, value 64 FROM counter 65 JOIN counter_track ON counter_track.id = counter.track_id 66 WHERE name = 'ScreenState' 67) 68SELECT * FROM counter_leading_intervals!(screen_state); 69 70DROP TABLE IF EXISTS screen_state_span_with_suspend; 71CREATE VIRTUAL TABLE screen_state_span_with_suspend 72USING span_join(screen_state_span, suspend_slice_); 73 74DROP VIEW IF EXISTS android_batt_output; 75CREATE PERFETTO VIEW android_batt_output AS 76SELECT AndroidBatteryMetric( 77 'battery_counters', ( 78 SELECT RepeatedField( 79 AndroidBatteryMetric_BatteryCounters( 80 'timestamp_ns', ts, 81 'charge_counter_uah', charge_uah, 82 'capacity_percent', capacity_percent, 83 'current_ua', current_ua, 84 'current_avg_ua', current_avg_ua 85 ) 86 ) 87 FROM android_battery_charge 88 ), 89 'battery_aggregates', ( 90 SELECT NULL_IF_EMPTY(AndroidBatteryMetric_BatteryAggregates( 91 'total_screen_off_ns', 92 SUM(CASE WHEN state = 1.0 AND tbl = 'total' THEN dur ELSE 0 END), 93 'total_screen_on_ns', 94 SUM(CASE WHEN state = 2.0 AND tbl = 'total' THEN dur ELSE 0 END), 95 'total_screen_doze_ns', 96 SUM(CASE WHEN state = 3.0 AND tbl = 'total' THEN dur ELSE 0 END), 97 'sleep_ns', 98 (SELECT SUM(dur) FROM suspend_slice_), 99 'sleep_screen_off_ns', 100 SUM(CASE WHEN state = 1.0 AND tbl = 'sleep' THEN dur ELSE 0 END), 101 'sleep_screen_on_ns', 102 SUM(CASE WHEN state = 2.0 AND tbl = 'sleep' THEN dur ELSE 0 END), 103 'sleep_screen_doze_ns', 104 SUM(CASE WHEN state = 3.0 AND tbl = 'sleep' THEN dur ELSE 0 END), 105 'total_wakelock_ns', 106 (SELECT SUM(ts_end - ts) FROM android_batt_wakelocks_merged) 107 )) 108 FROM ( 109 SELECT dur, value AS state, 'total' AS tbl 110 FROM screen_state_span 111 UNION ALL 112 SELECT dur, value AS state, 'sleep' AS tbl 113 FROM screen_state_span_with_suspend 114 ) 115 ), 116 'suspend_period', ( 117 SELECT RepeatedField( 118 AndroidBatteryMetric_SuspendPeriod( 119 'timestamp_ns', ts, 120 'duration_ns', dur 121 ) 122 ) 123 FROM suspend_slice_ 124 ) 125); 126