• 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
16INCLUDE PERFETTO MODULE android.oom_adjuster;
17INCLUDE PERFETTO MODULE memory.linux.general;
18
19-- All memory counters tables.
20
21CREATE PERFETTO VIEW _anon_rss AS
22SELECT
23  ts,
24  dur,
25  upid,
26  value AS anon_rss_val
27FROM _all_counters_per_process
28WHERE name = 'mem.rss.anon';
29
30CREATE PERFETTO VIEW _file_rss AS
31SELECT
32  ts,
33  dur,
34  upid,
35  value AS file_rss_val
36FROM _all_counters_per_process
37WHERE name = 'mem.rss.file';
38
39CREATE PERFETTO VIEW _shmem_rss AS
40SELECT
41  ts,
42  dur,
43  upid,
44  value AS shmem_rss_val
45FROM _all_counters_per_process
46WHERE name = 'mem.rss.shmem';
47
48CREATE PERFETTO VIEW _swap AS
49SELECT
50  ts,
51  dur,
52  upid,
53  value AS swap_val
54FROM _all_counters_per_process
55WHERE name = 'mem.swap';
56
57-- Span joins
58
59CREATE VIRTUAL TABLE _anon_swap_sj
60USING SPAN_OUTER_JOIN(
61  _anon_rss PARTITIONED upid,
62  _swap PARTITIONED upid);
63
64CREATE VIRTUAL TABLE _anon_swap_file_sj
65USING SPAN_OUTER_JOIN(
66  _anon_swap_sj PARTITIONED upid,
67  _file_rss PARTITIONED upid
68);
69
70CREATE VIRTUAL TABLE _rss_swap_sj
71USING SPAN_OUTER_JOIN(
72  _anon_swap_file_sj PARTITIONED upid,
73  _shmem_rss PARTITIONED upid
74);
75
76CREATE PERFETTO TABLE _memory_rss_and_swap_per_process_table AS
77SELECT
78  ts, dur, upid,
79  cast_int!(anon_rss_val) AS anon_rss,
80  cast_int!(file_rss_val) AS file_rss,
81  cast_int!(shmem_rss_val) AS shmem_rss,
82  cast_int!(swap_val) AS swap
83FROM _rss_swap_sj;
84
85
86-- Memory metrics timeline for each process.
87CREATE PERFETTO VIEW memory_rss_and_swap_per_process(
88  -- Timestamp
89  ts INT,
90  -- Duration
91  dur INT,
92  -- Upid of the process
93  upid INT,
94  -- Pid of the process
95  pid INT,
96  -- Name of the process
97  process_name STRING,
98  -- Anon RSS counter value
99  anon_rss INT,
100  -- File RSS counter value
101  file_rss INT,
102  -- Shared memory RSS counter value
103  shmem_rss INT,
104  -- Total RSS value. Sum of `anon_rss`, `file_rss` and `shmem_rss`. Returns
105  -- value even if one of the values is NULL.
106  rss INT,
107  -- Swap counter value
108  swap INT,
109  -- Sum or `anon_rss` and `swap`. Returns value even if one of the values is
110  -- NULL.
111  anon_rss_and_swap INT,
112  -- Sum or `rss` and `swap`. Returns value even if one of the values is NULL.
113  rss_and_swap INT
114) AS
115SELECT
116  ts,
117  dur,
118  upid,
119  pid,
120  name AS process_name,
121  anon_rss,
122  file_rss,
123  shmem_rss,
124  -- We do COALESCE only on `shmem_rss` and `swap`, as it can be expected all
125  -- process start to emit anon rss and file rss events (you'll need to at
126  -- least read code and have some memory to work with) - so the NULLs are real
127  --  values. But it is possible that you will never swap or never use shmem,
128  -- so those values are expected to often be NULLs, which shouldn't propagate
129  -- into the values like `anon_and_swap` or `rss`.
130  file_rss + anon_rss + COALESCE(shmem_rss, 0) AS rss,
131  swap,
132  anon_rss + COALESCE(swap, 0) AS anon_rss_and_swap,
133  anon_rss + file_rss  + COALESCE(shmem_rss, 0) + COALESCE(swap, 0) AS rss_and_swap
134FROM _memory_rss_and_swap_per_process_table
135JOIN process USING (upid);
136
137-- OOM score tables
138
139CREATE VIRTUAL TABLE _mem_ooms_sj
140USING SPAN_OUTER_JOIN(
141  android_oom_adj_intervals PARTITIONED upid,
142  _memory_rss_and_swap_per_process_table PARTITIONED upid);
143
144-- Process memory and it's OOM adjuster scores. Detects transitions, each new
145-- interval means that either the memory or OOM adjuster score of the process changed.
146CREATE PERFETTO TABLE memory_oom_score_with_rss_and_swap_per_process(
147  -- Timestamp the oom_adj score or memory of the process changed
148  ts INT,
149  -- Duration until the next oom_adj score or memory change of the process.
150  dur INT,
151  -- oom adjuster score of the process.
152  score INT,
153  -- oom adjuster bucket of the process.
154  bucket STRING,
155  -- Upid of the process having an oom_adj update.
156  upid INT,
157  -- Name of the process having an oom_adj update.
158  process_name STRING,
159  -- Pid of the process having an oom_adj update.
160  pid INT,
161  -- Slice of the latest oom_adj update in the system_server. Alias of
162  -- `slice.id`.
163  oom_adj_id INT,
164  -- Timestamp of the latest oom_adj update in the system_server.
165  oom_adj_ts INT,
166  -- Duration of the latest oom_adj update in the system_server.
167  oom_adj_dur INT,
168  -- Track of the latest oom_adj update in the system_server. Alias of
169  -- `track.id`.
170  oom_adj_track_id INT,
171  -- Thread name of the latest oom_adj update in the system_server.
172  oom_adj_thread_name STRING,
173  -- Reason for the latest oom_adj update in the system_server.
174  oom_adj_reason STRING,
175  -- Trigger for the latest oom_adj update in the system_server.
176  oom_adj_trigger STRING,
177  -- Anon RSS counter value
178  anon_rss INT,
179  -- File RSS counter value
180  file_rss INT,
181  -- Shared memory RSS counter value
182  shmem_rss INT,
183  -- Total RSS value. Sum of `anon_rss`, `file_rss` and `shmem_rss`. Returns
184  -- value even if one of the values is NULL.
185  rss INT,
186  -- Swap counter value
187  swap INT,
188  -- Sum or `anon_rss` and `swap`. Returns value even if one of the values is
189  -- NULL.
190  anon_rss_and_swap INT,
191  -- Sum or `rss` and `swap`. Returns value even if one of the values is NULL.
192  rss_and_swap INT
193) AS
194SELECT
195  ts,
196  dur,
197  score,
198  bucket,
199  upid,
200  process_name,
201  pid,
202  oom_adj_id,
203  oom_adj_ts,
204  oom_adj_dur,
205  oom_adj_track_id,
206  oom_adj_thread_name,
207  oom_adj_reason,
208  oom_adj_trigger,
209  anon_rss,
210  file_rss,
211  shmem_rss,
212  file_rss + anon_rss + COALESCE(shmem_rss, 0) AS rss,
213  swap,
214  anon_rss + COALESCE(swap, 0) AS anon_rss_and_swap,
215  anon_rss + file_rss  + COALESCE(shmem_rss, 0) + COALESCE(swap, 0) AS rss_and_swap
216FROM _mem_ooms_sj
217JOIN process USING (upid);
218