Skip to content

Commit

Permalink
Merge "Move screen state and doze into stdlib." into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon MacMullen authored and Gerrit Code Review committed Jan 20, 2025
2 parents 75c4b8d + 217dde0 commit 5239f9e
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 178 deletions.
2 changes: 2 additions & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -13782,6 +13782,7 @@ genrule {
"src/trace_processor/perfetto_sql/stdlib/android/auto/multiuser.sql",
"src/trace_processor/perfetto_sql/stdlib/android/battery.sql",
"src/trace_processor/perfetto_sql/stdlib/android/battery/charging_states.sql",
"src/trace_processor/perfetto_sql/stdlib/android/battery/doze.sql",
"src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql",
"src/trace_processor/perfetto_sql/stdlib/android/binder.sql",
"src/trace_processor/perfetto_sql/stdlib/android/binder_breakdown.sql",
Expand Down Expand Up @@ -13822,6 +13823,7 @@ genrule {
"src/trace_processor/perfetto_sql/stdlib/android/oom_adjuster.sql",
"src/trace_processor/perfetto_sql/stdlib/android/power_rails.sql",
"src/trace_processor/perfetto_sql/stdlib/android/process_metadata.sql",
"src/trace_processor/perfetto_sql/stdlib/android/screen_state.sql",
"src/trace_processor/perfetto_sql/stdlib/android/screenshots.sql",
"src/trace_processor/perfetto_sql/stdlib/android/services.sql",
"src/trace_processor/perfetto_sql/stdlib/android/slices.sql",
Expand Down
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3011,6 +3011,7 @@ perfetto_filegroup(
name = "src_trace_processor_perfetto_sql_stdlib_android_battery_battery",
srcs = [
"src/trace_processor/perfetto_sql/stdlib/android/battery/charging_states.sql",
"src/trace_processor/perfetto_sql/stdlib/android/battery/doze.sql",
],
)

Expand Down Expand Up @@ -3127,6 +3128,7 @@ perfetto_filegroup(
"src/trace_processor/perfetto_sql/stdlib/android/oom_adjuster.sql",
"src/trace_processor/perfetto_sql/stdlib/android/power_rails.sql",
"src/trace_processor/perfetto_sql/stdlib/android/process_metadata.sql",
"src/trace_processor/perfetto_sql/stdlib/android/screen_state.sql",
"src/trace_processor/perfetto_sql/stdlib/android/screenshots.sql",
"src/trace_processor/perfetto_sql/stdlib/android/services.sql",
"src/trace_processor/perfetto_sql/stdlib/android/slices.sql",
Expand Down
1 change: 1 addition & 0 deletions src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ perfetto_sql_source_set("android") {
"oom_adjuster.sql",
"power_rails.sql",
"process_metadata.sql",
"screen_state.sql",
"screenshots.sql",
"services.sql",
"slices.sql",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
import("../../../../../../gn/perfetto_sql.gni")

perfetto_sql_source_set("battery") {
sources = [ "charging_states.sql" ]
sources = [
"charging_states.sql",
"doze.sql",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
-- 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.

INCLUDE PERFETTO MODULE counters.intervals;

-- Device charging states.
Expand All @@ -23,49 +24,53 @@ CREATE PERFETTO TABLE android_charging_states(
ts TIMESTAMP,
-- Duration of the device charging state.
dur DURATION,
-- One of: charging, discharging, not_charging, full, unknown.
short_charging_state STRING,
-- Device charging state, one of: Charging, Discharging, Not charging
-- (when the charger is present but battery is not charging),
-- Full, Unknown
charging_state STRING
) AS SELECT
id,
ts,
dur,
charging_state
FROM (
WITH
_counter AS (
SELECT counter.id, ts, 0 AS track_id, value
FROM counter
JOIN counter_track
ON counter_track.id = counter.track_id
WHERE counter_track.name = 'BatteryStatus'
)
SELECT
id,
ts,
dur,
CASE
value
-- 0 and 1 are both 'Unknown'
WHEN 2 THEN 'Charging'
WHEN 3 THEN 'Discharging'
-- special case when charger is present but battery isn't charging
WHEN 4 THEN 'Not charging'
WHEN 5 THEN 'Full'
ELSE 'Unknown'
END AS charging_state
FROM counter_leading_intervals !(_counter)
WHERE dur > 0
-- Either the above select statement is populated or the
-- select statement after the union is populated but not both.
UNION
-- When the trace does not have a slice in the charging state track then
-- we will assume that the charging state for the entire trace is Unknown.
-- This ensures that we still have job data even if the charging state is
-- not known. The following statement will only ever return a single row.
SELECT 1, TRACE_START(), TRACE_DUR(), 'Unknown'
WHERE NOT EXISTS (
SELECT * FROM _counter
) AND TRACE_DUR() > 0
);
) AS
WITH
_counter AS (
SELECT counter.id, ts, 0 AS track_id, value
FROM counter
JOIN counter_track
ON counter_track.id = counter.track_id
WHERE counter_track.name = 'BatteryStatus'
)
SELECT
id,
ts,
dur,
CASE
value
WHEN 2 THEN 'charging'
WHEN 3 THEN 'discharging'
WHEN 4 THEN 'not_charging'
WHEN 5 THEN 'full'
ELSE 'unknown'
END AS short_charging_state,
CASE
value
-- 0 and 1 are both 'Unknown'
WHEN 2 THEN 'Charging'
WHEN 3 THEN 'Discharging'
-- special case when charger is present but battery isn't charging
WHEN 4 THEN 'Not charging'
WHEN 5 THEN 'Full'
ELSE 'Unknown'
END AS charging_state
FROM counter_leading_intervals !(_counter)
WHERE dur > 0
-- Either the above select statement is populated or the
-- select statement after the union is populated but not both.
UNION
-- When the trace does not have a slice in the charging state track then
-- we will assume that the charging state for the entire trace is Unknown.
-- This ensures that we still have job data even if the charging state is
-- not known. The following statement will only ever return a single row.
SELECT 1, TRACE_START(), TRACE_DUR(), 'unknown', 'Unknown'
WHERE NOT EXISTS (
SELECT * FROM _counter
) AND TRACE_DUR() > 0;
96 changes: 96 additions & 0 deletions src/trace_processor/perfetto_sql/stdlib/android/battery/doze.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
-- Copyright 2025 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
--
-- https://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.

INCLUDE PERFETTO MODULE counters.intervals;

-- Light idle states. This is the state machine that quickly detects the
-- device is unused and restricts background activity.
-- See https://developer.android.com/training/monitoring-device-state/doze-standby
CREATE PERFETTO TABLE android_light_idle_state(
-- ID
id LONG,
-- Timestamp.
ts TIMESTAMP,
-- Duration.
dur DURATION,
-- Description of the light idle state.
light_idle_state STRING
) AS
WITH _counter AS (
SELECT counter.id, ts, 0 AS track_id, value
FROM counter
JOIN counter_track ON counter_track.id = counter.track_id
WHERE name = 'DozeLightState'
)
SELECT
id,
ts,
dur,
CASE value
-- device is used or on power
WHEN 0 THEN 'active'
-- device is waiting to go idle
WHEN 1 THEN 'inactive'
-- device is idle
WHEN 4 THEN 'idle'
-- waiting for connectivity before maintenance
WHEN 5 THEN 'waiting_for_network'
-- maintenance running
WHEN 6 THEN 'idle_maintenance'
-- device has gone deep idle, light idle state irrelevant
WHEN 7 THEN 'override'
ELSE 'unmapped'
END AS light_idle_state
FROM counter_leading_intervals!(_counter);

-- Deep idle states. This is the state machine that more slowly detects deeper
-- levels of device unuse and restricts background activity further.
-- See https://developer.android.com/training/monitoring-device-state/doze-standby
CREATE PERFETTO TABLE android_deep_idle_state(
-- ID
id LONG,
-- Timestamp.
ts TIMESTAMP,
-- Duration.
dur DURATION,
-- Description of the deep idle state.
deep_idle_state STRING
) AS
WITH _counter AS (
SELECT counter.id, ts, 0 AS track_id, value
FROM counter
JOIN counter_track ON counter_track.id = counter.track_id
WHERE name = 'DozeDeepState'
)
SELECT
id,
ts,
dur,
CASE value
WHEN 0 THEN 'active'
WHEN 1 THEN 'inactive'
-- waiting for next idle period
WHEN 2 THEN 'idle_pending'
-- device is sensing motion
WHEN 3 THEN 'sensing'
-- device is finding location
WHEN 4 THEN 'locating'
WHEN 5 THEN 'idle'
WHEN 6 THEN 'idle_maintenance'
-- inactive, should go straight to idle without motion / location
-- sensing.
WHEN 7 THEN 'quick_doze_delay'
ELSE 'unmapped'
END AS deep_idle_state
FROM counter_leading_intervals!(_counter);
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,9 @@

INCLUDE PERFETTO MODULE counters.intervals;
INCLUDE PERFETTO MODULE android.battery.charging_states;
INCLUDE PERFETTO MODULE android.screen_state;
INCLUDE PERFETTO MODULE intervals.intersect;

CREATE PERFETTO TABLE _screen_states AS
SELECT
id,
ts,
dur,
screen_state
FROM (
WITH _screen_state_span AS (
SELECT *
FROM counter_leading_intervals!((
SELECT counter.id, ts, 0 AS track_id, value
FROM counter
JOIN counter_track ON counter_track.id = counter.track_id
WHERE name = 'ScreenState'
))) SELECT
id,
ts,
dur,
CASE value
WHEN 1 THEN 'Screen off'
WHEN 2 THEN 'Screen on'
WHEN 3 THEN 'Always-on display (doze)'
ELSE 'Unknown'
END AS screen_state
FROM _screen_state_span
WHERE dur > 0
-- Either the above select statement is populated or the
-- select statement after the union is populated but not both.
UNION
-- When the trace does not have a slice in the screen state track then
-- we will assume that the screen state for the entire trace is Unknown.
-- This ensures that we still have job data even if the screen state is
-- not known. The following statement will only ever return a single row.
SELECT 1, TRACE_START() as ts, TRACE_DUR() as dur, 'Unknown'
WHERE NOT EXISTS (
SELECT * FROM _screen_state_span
) AND TRACE_DUR() > 0
);

CREATE PERFETTO TABLE _job_states AS
SELECT
t.id as track_id,
Expand Down Expand Up @@ -182,11 +144,11 @@ SELECT
c.charging_state,
s.screen_state
FROM _interval_intersect!(
(android_charging_states, _screen_states),
(android_charging_states, android_screen_state),
()
) ii
JOIN android_charging_states c ON c.id = ii.id_0
JOIN _screen_states s ON s.id = ii.id_1;
JOIN android_screen_state s ON s.id = ii.id_1;

-- This table returns constraint changes that a
-- job will go through in a single trace.
Expand Down
Loading

0 comments on commit 5239f9e

Please sign in to comment.