1-- 2-- Copyright 2019 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 17DROP VIEW IF EXISTS ion_timeline; 18CREATE VIEW ion_timeline AS 19SELECT 20 ts, 21 LEAD(ts, 1, (SELECT end_ts FROM trace_bounds)) 22 OVER(PARTITION BY track_id ORDER BY ts) - ts AS dur, 23 CASE name 24 WHEN 'mem.ion' THEN 'all' 25 ELSE SUBSTR(name, 9) 26 END AS heap_name, 27 track_id, 28 value 29FROM counter JOIN counter_track 30 ON counter.track_id = counter_track.id 31WHERE (name LIKE 'mem.ion.%' OR name = 'mem.ion'); 32 33DROP VIEW IF EXISTS ion_heap_stats; 34CREATE VIEW ion_heap_stats AS 35SELECT 36 heap_name, 37 SUM(value * dur) / SUM(dur) AS avg_size, 38 MIN(value) AS min_size, 39 MAX(value) AS max_size 40FROM ion_timeline 41GROUP BY 1; 42 43DROP VIEW IF EXISTS ion_raw_allocs; 44CREATE VIEW ion_raw_allocs AS 45SELECT 46 CASE name 47 WHEN 'mem.ion_change' THEN 'all' 48 ELSE SUBSTR(name, 16) 49 END AS heap_name, 50 ts, 51 value AS instant_value, 52 SUM(value) OVER win AS value 53FROM counter c JOIN thread_counter_track t ON c.track_id = t.id 54WHERE (name LIKE 'mem.ion_change.%' OR name = 'mem.ion_change') AND value > 0 55WINDOW win AS ( 56 PARTITION BY name ORDER BY ts 57 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 58); 59 60DROP VIEW IF EXISTS ion_alloc_stats; 61CREATE VIEW ion_alloc_stats AS 62SELECT 63 heap_name, 64 SUM(instant_value) AS total_alloc_size_bytes 65FROM ion_raw_allocs 66GROUP BY 1; 67 68-- We need to group by ts here as we can have two ion events from 69-- different processes occurring at the same timestamp. We take the 70-- max as this will take both allocations into account at that 71-- timestamp. 72DROP VIEW IF EXISTS android_ion_event; 73CREATE VIEW android_ion_event AS 74SELECT 75 'counter' AS track_type, 76 printf('ION allocations (heap: %s)', heap_name) AS track_name, 77 ts, 78 MAX(value) AS value 79FROM ion_raw_allocs 80GROUP BY 1, 2, 3; 81 82DROP VIEW IF EXISTS android_ion_output; 83CREATE VIEW android_ion_output AS 84SELECT AndroidIonMetric( 85 'buffer', RepeatedField( 86 AndroidIonMetric_Buffer( 87 'name', heap_name, 88 'avg_size_bytes', avg_size, 89 'min_size_bytes', min_size, 90 'max_size_bytes', max_size, 91 'total_alloc_size_bytes', total_alloc_size_bytes 92 ) 93 )) 94FROM ion_heap_stats JOIN ion_alloc_stats USING (heap_name); 95