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 linux.memory.general; 17 18-- All memory counters tables. 19 20CREATE PERFETTO VIEW _anon_rss AS 21SELECT 22 ts, 23 dur, 24 upid, 25 value AS anon_rss_val 26FROM _all_counters_per_process 27WHERE 28 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 38 name = 'mem.rss.file'; 39 40CREATE PERFETTO VIEW _shmem_rss AS 41SELECT 42 ts, 43 dur, 44 upid, 45 value AS shmem_rss_val 46FROM _all_counters_per_process 47WHERE 48 name = 'mem.rss.shmem'; 49 50CREATE PERFETTO VIEW _swap AS 51SELECT 52 ts, 53 dur, 54 upid, 55 value AS swap_val 56FROM _all_counters_per_process 57WHERE 58 name = 'mem.swap'; 59 60-- Span joins 61 62CREATE VIRTUAL TABLE _anon_swap_sj USING SPAN_OUTER_JOIN ( 63 _anon_rss PARTITIONED upid, 64 _swap PARTITIONED upid); 65 66CREATE VIRTUAL TABLE _anon_swap_file_sj USING SPAN_OUTER_JOIN ( 67 _anon_swap_sj PARTITIONED upid, 68 _file_rss PARTITIONED upid 69); 70 71CREATE VIRTUAL TABLE _rss_swap_sj USING 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, 79 dur, 80 upid, 81 cast_int!(anon_rss_val) AS anon_rss, 82 cast_int!(file_rss_val) AS file_rss, 83 cast_int!(shmem_rss_val) AS shmem_rss, 84 cast_int!(swap_val) AS swap 85FROM _rss_swap_sj; 86 87-- Memory metrics timeline for each process. 88CREATE PERFETTO VIEW memory_rss_and_swap_per_process ( 89 -- Timestamp 90 ts TIMESTAMP, 91 -- Duration 92 dur DURATION, 93 -- Upid of the process 94 upid JOINID(process.id), 95 -- Pid of the process 96 pid LONG, 97 -- Name of the process 98 process_name STRING, 99 -- Anon RSS counter value 100 anon_rss LONG, 101 -- File RSS counter value 102 file_rss LONG, 103 -- Shared memory RSS counter value 104 shmem_rss LONG, 105 -- Total RSS value. Sum of `anon_rss`, `file_rss` and `shmem_rss`. Returns 106 -- value even if one of the values is NULL. 107 rss LONG, 108 -- Swap counter value 109 swap LONG, 110 -- Sum or `anon_rss` and `swap`. Returns value even if one of the values is 111 -- NULL. 112 anon_rss_and_swap LONG, 113 -- Sum or `rss` and `swap`. Returns value even if one of the values is NULL. 114 rss_and_swap LONG 115) AS 116SELECT 117 ts, 118 dur, 119 upid, 120 pid, 121 name AS process_name, 122 anon_rss, 123 file_rss, 124 shmem_rss, 125 -- We do COALESCE only on `shmem_rss` and `swap`, as it can be expected all 126 -- process start to emit anon rss and file rss events (you'll need to at 127 -- least read code and have some memory to work with) - so the NULLs are real 128 -- values. But it is possible that you will never swap or never use shmem, 129 -- so those values are expected to often be NULLs, which shouldn't propagate 130 -- into the values like `anon_and_swap` or `rss`. 131 file_rss + anon_rss + coalesce(shmem_rss, 0) AS rss, 132 swap, 133 anon_rss + coalesce(swap, 0) AS anon_rss_and_swap, 134 anon_rss + file_rss + coalesce(shmem_rss, 0) + coalesce(swap, 0) AS rss_and_swap 135FROM _memory_rss_and_swap_per_process_table 136JOIN process 137 USING (upid); 138