• 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.startup.startups;
18
19-- Time elapsed between the latest user start
20-- and the specific end event
21-- like package startup(ex carlauncher) or previous user stop.
22CREATE PERFETTO TABLE android_auto_multiuser_timing(
23    -- Id of the started android user
24    event_start_user_id STRING,
25    -- Start event time
26    event_start_time INT,
27    -- End event time
28    event_end_time INT,
29    -- End event name
30    event_end_name STRING,
31    -- Start event name
32    event_start_name STRING,
33    -- User switch duration from start event
34    -- to end event
35    duration LONG
36) AS
37-- The last ts for user switch event.
38WITH auto_multiuser_user_start AS (
39    SELECT
40      slice.name as event_start_name,
41      slice.ts AS user_start_time,
42      -- Ex.: UserController.startUser-11-fg-start-mode-1
43      -- User is enclosed in dashes and will be at most 2 characters(10, 11, etc.)
44      SUBSTR(name, INSTR(name, '-') + 1, 2) as started_user_id
45    FROM slice
46    WHERE (
47        slice.name GLOB "UserController.startUser*"
48        AND slice.name NOT GLOB "UserController.startUser-10*"
49    )
50    ORDER BY ts DESC
51    LIMIT 1
52)
53SELECT
54  started_user_id AS event_start_user_id,
55  user_start_time AS event_start_time,
56  event_end_time,
57  event_end_name,
58  event_start_name,
59  (event_end_time - user_start_time) as duration
60FROM (
61  SELECT
62    ts_end AS event_end_time,
63    package as event_end_name
64  FROM android_startups
65  UNION
66  SELECT
67    slice.ts + slice.dur as event_end_time,
68    slice.name as event_end_name
69  FROM slice
70  WHERE slice.name GLOB "finishUserStopped-10*"
71) as a
72JOIN auto_multiuser_user_start as b
73ON a.event_end_time > b.user_start_time;
74
75-- Previous user(user 10) total CPU time
76CREATE PERFETTO VIEW _android_auto_user_10_total_cpu_time AS
77SELECT
78  SUM(dur) as total_cpu_time,
79  (uid - android_appid) / 100000 as user_id,
80  event_end_name
81FROM sched_slice
82  JOIN thread USING (utid)
83  JOIN process USING (upid),
84  android_auto_multiuser_timing
85WHERE
86  user_id = 10
87  AND ts >= android_auto_multiuser_timing.event_start_time
88  AND ts <= android_auto_multiuser_timing.event_end_time
89GROUP BY event_end_name;
90
91-- Previous user(user 10) total memory usage
92CREATE PERFETTO VIEW _android_auto_user_10_total_memory AS
93WITH filtered_process AS (
94    SELECT
95      c.ts,
96      c.value,
97      p.name AS proc_name,
98      (uid - android_appid) / 100000 as user_id,
99      event_end_name
100    FROM counter AS c
101      LEFT JOIN process_counter_track AS t
102        ON c.track_id = t.id
103      LEFT JOIN process AS p USING (upid),
104      android_auto_multiuser_timing
105    WHERE
106      t.name GLOB "mem.rss"
107      AND user_id = 10
108      AND c.ts >= android_auto_multiuser_timing.event_start_time
109      AND c.ts <= android_auto_multiuser_timing.event_end_time
110),
111process_rss AS (
112    SELECT
113      *,
114      ifnull(
115        lag(value) OVER (PARTITION BY proc_name, event_end_name ORDER BY ts), value
116      ) AS prev_value
117    FROM filtered_process
118),
119per_process_allocations AS (
120    SELECT
121      proc_name,
122      SUM(value - prev_value) / 1e3 AS alloc_value_kb,
123      user_id,
124      event_end_name
125    FROM process_rss
126    WHERE value - prev_value > 0
127    GROUP BY proc_name, event_end_name
128    ORDER BY alloc_value_kb DESC
129)
130SELECT
131  cast_int!(SUM(alloc_value_kb)) AS total_memory_usage_kb,
132  user_id,
133  event_end_name
134FROM per_process_allocations
135GROUP BY event_end_name;
136
137-- This table extends `android_auto_multiuser_timing` table with previous user resource usage.
138CREATE PERFETTO VIEW android_auto_multiuser_timing_with_previous_user_resource_usage(
139    -- Start user id
140    event_start_user_id STRING,
141    -- Start event time
142    event_start_time INT,
143    -- End event time
144    event_end_time INT,
145    -- End event name
146    event_end_name STRING,
147    -- Start event name
148    event_start_name STRING,
149    -- User switch duration from start event
150    -- to end event
151    duration LONG,
152    -- User id
153    user_id INT,
154    -- Total CPU time for a user
155    total_cpu_time LONG,
156    -- Total memory user for a user
157    total_memory_usage_kb LONG
158) AS
159SELECT
160    a.event_start_user_id,
161    a.event_start_time,
162    a.event_end_time,
163    a.event_end_name,
164    a.event_start_name,
165    a.duration,
166    b.user_id,
167    b.total_cpu_time,
168    c.total_memory_usage_kb
169FROM android_auto_multiuser_timing as a
170LEFT JOIN _android_auto_user_10_total_cpu_time as b
171    ON a.event_end_name = b.event_end_name
172LEFT JOIN _android_auto_user_10_total_memory as c
173    ON a.event_end_name = c.event_end_name;