• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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--
16
17INCLUDE PERFETTO MODULE android.statsd;
18
19-- Desktop Windows with durations they were open.
20CREATE PERFETTO TABLE android_desktop_mode_windows (
21  -- Window add timestamp; NULL if no add event in the trace.
22  raw_add_ts TIMESTAMP,
23  -- Window remove timestamp; NULL if no remove event in the trace.
24  raw_remove_ts TIMESTAMP,
25  -- Timestamp that the window was added; or trace_start() if no add event in the trace.
26  ts TIMESTAMP,
27  -- Furation the window was open; or until trace_end() if no remove event in the trace.
28  dur DURATION,
29  -- Desktop Window instance ID - unique per window.
30  instance_id LONG,
31  -- UID of the app running in the window.
32  uid LONG
33) AS
34WITH
35  atoms AS (
36    SELECT
37      ts,
38      extract_arg(arg_set_id, 'desktop_mode_session_task_update.task_event') AS type,
39      extract_arg(arg_set_id, 'desktop_mode_session_task_update.instance_id') AS instance_id,
40      extract_arg(arg_set_id, 'desktop_mode_session_task_update.uid') AS uid,
41      extract_arg(arg_set_id, 'desktop_mode_session_task_update.session_id') AS session_id
42    FROM android_statsd_atoms
43    WHERE
44      name = 'desktop_mode_session_task_update'
45  ),
46  dw_statsd_events_add AS (
47    SELECT
48      *
49    FROM atoms
50    WHERE
51      type = 'TASK_ADDED'
52  ),
53  dw_statsd_events_remove AS (
54    SELECT
55      *
56    FROM atoms
57    WHERE
58      type = 'TASK_REMOVED'
59  ),
60  dw_statsd_events_update_by_instance AS (
61    SELECT
62      instance_id,
63      session_id,
64      min(uid) AS uid
65    FROM atoms
66    WHERE
67      type = 'TASK_INFO_CHANGED'
68    GROUP BY
69      instance_id,
70      session_id
71  ),
72  dw_statsd_reset_event AS (
73    SELECT
74      ts
75    FROM atoms
76    WHERE
77      type = 'TASK_INIT_STATSD'
78    UNION
79    SELECT
80      trace_end()
81  ),
82  dw_windows AS (
83    SELECT
84      a.ts AS raw_add_ts,
85      r.ts AS raw_remove_ts,
86      -- Assume trace_start() if no add event found.
87      coalesce(a.ts, trace_start()) AS ts,
88      -- Assume next reset event or trace_end() if no remove event found.
89      coalesce(
90        r.ts,
91        (
92          SELECT
93            min(ts)
94          FROM dw_statsd_reset_event
95          WHERE
96            ts > coalesce(a.ts, trace_start())
97        )
98      ) - coalesce(a.ts, trace_start()) AS dur,
99      coalesce(a.instance_id, r.instance_id) AS instance_id,
100      coalesce(a.uid, r.uid) AS uid
101    FROM dw_statsd_events_add AS a
102    FULL JOIN dw_statsd_events_remove AS r
103      USING (instance_id, session_id)
104  ),
105  -- Assume window was open for the entire trace if we only see change events for the instance ID.
106  dw_windows_with_update_events AS (
107    SELECT
108      *
109    FROM dw_windows
110    UNION
111    SELECT
112      NULL,
113      NULL,
114      trace_start(),
115      trace_end() - trace_start(),
116      instance_id,
117      uid
118    FROM dw_statsd_events_update_by_instance
119    WHERE
120      NOT instance_id IN (
121        SELECT
122          instance_id
123        FROM dw_windows
124      )
125  )
126SELECT
127  *
128FROM dw_windows_with_update_events;
129