# Device State: Top App
# select id, ts, dur, name from (__query_slice_track__long_battery_tracing_Device_State_Top_app)
--> 简便方法
"""
INCLUDE PERFETTO MODULE android.battery_stats;
select * from android_battery_stats_event_slices where track_name='battery_stats.top';
"""
--> 原始数据
"""
CREATE PERFETTO VIEW android_battery_stats_event_slices (
-- Start of a new battery state.
ts TIMESTAMP,
-- The duration the state was active, -1 for incomplete slices.
dur DURATION,
-- The same as `dur`, but extends to trace end for incomplete slices.
safe_dur DURATION,
-- The name of the counter track.
track_name STRING,
-- String value.
str_value STRING,
-- Int value.
int_value LONG
) AS
WITH
event_markers AS (
SELECT
ts,
track.name AS track_name,
str_split(slice.name, '=', 1) AS key,
substr(slice.name, 1, 1) = '+' AS start
FROM slice
JOIN track
ON slice.track_id = track.id
WHERE
track_name GLOB 'battery_stats.*' AND substr(slice.name, 1, 1) IN ('+', '-')
),
with_neighbors AS (
SELECT
*,
lag(ts) OVER (PARTITION BY track_name, key ORDER BY ts) AS last_ts,
lead(ts) OVER (PARTITION BY track_name, key ORDER BY ts) AS next_ts
FROM event_markers
),
-- Note: query performance depends on the ability to push down filters on
-- the track_name. It would be more clear below to have two queries and union
-- them, but doing so prevents push down through the above window functions.
event_spans AS (
SELECT
track_name,
key,
iif(start, ts, trace_start()) AS ts,
iif(start, next_ts, ts) AS end_ts
FROM with_neighbors
-- For the majority of events, we take the `start` event and compute the dur
-- based on next_ts. In the off chance we get an end event with no prior
-- start (matched by the second half of this where), we can create an event
-- starting from the beginning of the trace ending at the current event.
WHERE
(
start OR last_ts IS NULL
)
)
SELECT
ts,
coalesce(end_ts - ts, -1) AS dur,
coalesce(end_ts, trace_end()) - ts AS safe_dur,
track_name,
str_split(key, '"', 1) AS str_value,
cast_int!(str_split(key, ':', 0)) AS int_value
FROM event_spans;
select * from android_battery_stats_event_slices where track_name='battery_stats.top';
"""
# Android APP Startups
# select id, ts, dur, name from (__query_slice_track__android_startups)
-->
"""
INCLUDE PERFETTO MODULE android.startup.startups;
SELECT * FROM android_startups;
"""