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 counters.intervals; 17 18-- Device charging states. 19CREATE PERFETTO TABLE android_charging_states ( 20 -- Alias of counter.id if a slice with charging state exists otherwise 21 -- there will be a single row where id = 1. 22 id LONG, 23 -- Timestamp at which the device charging state began. 24 ts TIMESTAMP, 25 -- Duration of the device charging state. 26 dur DURATION, 27 -- One of: charging, discharging, not_charging, full, unknown. 28 short_charging_state STRING, 29 -- Device charging state, one of: Charging, Discharging, Not charging 30 -- (when the charger is present but battery is not charging), 31 -- Full, Unknown 32 charging_state STRING 33) AS 34-- Either the first statement is populated or the select statement after the 35-- union is populated but not both. 36WITH 37 _counter AS ( 38 SELECT 39 counter.id, 40 ts, 41 0 AS track_id, 42 value 43 FROM counter 44 JOIN counter_track 45 ON counter_track.id = counter.track_id 46 WHERE 47 counter_track.name = 'BatteryStatus' 48 ) 49SELECT 50 id, 51 ts, 52 dur, 53 CASE value 54 WHEN 2 55 THEN 'charging' 56 WHEN 3 57 THEN 'discharging' 58 WHEN 4 59 THEN 'not_charging' 60 WHEN 5 61 THEN 'full' 62 ELSE 'unknown' 63 END AS short_charging_state, 64 CASE value 65 -- 0 and 1 are both 'Unknown' 66 WHEN 2 67 THEN 'Charging' 68 WHEN 3 69 THEN 'Discharging' 70 -- special case when charger is present but battery isn't charging 71 WHEN 4 72 THEN 'Not charging' 73 WHEN 5 74 THEN 'Full' 75 ELSE 'Unknown' 76 END AS charging_state 77FROM counter_leading_intervals !(_counter) 78WHERE 79 dur > 0 80UNION 81-- When the trace does not have a slice in the charging state track then 82-- we will assume that the charging state for the entire trace is Unknown. 83-- This ensures that we still have job data even if the charging state is 84-- not known. The following statement will only ever return a single row. 85SELECT 86 1, 87 trace_start(), 88 trace_dur(), 89 'unknown', 90 'Unknown' 91WHERE 92 NOT EXISTS( 93 SELECT 94 * 95 FROM _counter 96 ) AND trace_dur() > 0; 97