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