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 17SELECT RUN_METRIC('android/process_metadata.sql'); 18SELECT RUN_METRIC('android/process_mem.sql'); 19 20DROP VIEW IF EXISTS java_heap_stats_output; 21CREATE VIEW java_heap_stats_output AS 22WITH 23-- Base view 24base_stat_counts AS ( 25 SELECT 26 upid, 27 graph_sample_ts, 28 SUM(self_size) AS total_size, 29 SUM(native_size) AS total_native_size, 30 COUNT(1) AS total_obj_count, 31 SUM(IIF(reachable, self_size, 0)) AS reachable_size, 32 SUM(IIF(reachable, native_size, 0)) AS reachable_native_size, 33 SUM(IIF(reachable, 1, 0)) AS reachable_obj_count 34 FROM heap_graph_object 35 GROUP BY 1, 2 36), 37heap_roots AS ( 38 SELECT 39 upid, 40 graph_sample_ts, 41 root_type, 42 IFNULL(t.deobfuscated_name, t.name) AS type_name, 43 COUNT(1) AS obj_count 44 FROM heap_graph_object o 45 JOIN heap_graph_class t ON o.type_id = t.id 46 -- Classes are going to be particularly spammy and uninteresting 47 -- from a memory analysis perspective (compared e.g. to local jni roots) 48 WHERE root_type IS NOT NULL AND root_type != 'ROOT_STICKY_CLASS' 49 GROUP BY 1, 2, 3, 4 50 ORDER BY obj_count DESC 51), 52heap_roots_proto AS ( 53 SELECT 54 upid, 55 graph_sample_ts, 56 RepeatedField(JavaHeapStats_HeapRoots( 57 'root_type', root_type, 58 'type_name', type_name, 59 'obj_count', obj_count 60 )) AS roots 61 FROM heap_roots 62 GROUP BY 1, 2 63), 64base_stats AS ( 65 SELECT * FROM base_stat_counts JOIN heap_roots_proto USING (upid, graph_sample_ts) 66), 67-- Find closest value 68closest_anon_swap_oom AS ( 69 SELECT 70 upid, 71 graph_sample_ts, 72 ( 73 SELECT anon_swap_val 74 FROM ( 75 SELECT 76 ts, dur, 77 CAST(anon_and_swap_val AS INTEGER) AS anon_swap_val, 78 ABS(ts - base_stats.graph_sample_ts) AS diff 79 FROM anon_and_swap_span 80 WHERE upid = base_stats.upid) 81 WHERE 82 (graph_sample_ts >= ts AND graph_sample_ts < ts + dur) 83 -- If the first memory sample for the UPID comes *after* the heap profile 84 -- accept it if close (500ms) 85 OR (graph_sample_ts < ts AND diff <= 500 * 1e6) 86 ORDER BY diff LIMIT 1 87 ) AS anon_swap_val, 88 ( 89 SELECT oom_score_val 90 FROM ( 91 SELECT 92 ts, dur, 93 oom_score_val, 94 ABS(ts - base_stats.graph_sample_ts) AS diff 95 FROM oom_score_span 96 WHERE upid = base_stats.upid) 97 WHERE 98 (graph_sample_ts >= ts AND graph_sample_ts < ts + dur) 99 -- If the first memory sample for the UPID comes *after* the heap profile 100 -- accept it if close (500ms) 101 OR (graph_sample_ts < ts AND diff <= 500 * 1e6) 102 ORDER BY diff LIMIT 1 103 ) AS oom_score_val 104 FROM base_stats 105), 106-- Group by upid 107heap_graph_sample_protos AS ( 108 SELECT 109 base_stats.upid, 110 RepeatedField(JavaHeapStats_Sample( 111 'ts', graph_sample_ts, 112 'heap_size', total_size, 113 'heap_native_size', total_native_size, 114 'obj_count', total_obj_count, 115 'reachable_heap_size', reachable_size, 116 'reachable_heap_native_size', reachable_native_size, 117 'reachable_obj_count', reachable_obj_count, 118 'roots', roots, 119 'anon_rss_and_swap_size', closest_anon_swap_oom.anon_swap_val, 120 'oom_score_adj', closest_anon_swap_oom.oom_score_val 121 )) AS sample_protos 122 FROM base_stats 123 LEFT JOIN closest_anon_swap_oom USING (upid, graph_sample_ts) 124 GROUP BY 1 125) 126SELECT JavaHeapStats( 127 'instance_stats', RepeatedField(JavaHeapStats_InstanceStats( 128 'upid', upid, 129 'process', process_metadata.metadata, 130 'samples', sample_protos 131 ))) 132FROM heap_graph_sample_protos JOIN process_metadata USING (upid); 133