1-- 2-- Copyright 2020 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'); 18 19CREATE VIEW IF NOT EXISTS java_heap_histogram_output AS 20WITH 21-- Base histogram table 22heap_obj_histograms AS ( 23 SELECT 24 o.upid, 25 o.graph_sample_ts, 26 IFNULL(c.deobfuscated_name, c.name) AS type_name, 27 COUNT(1) obj_count, 28 SUM(CASE o.reachable WHEN TRUE THEN 1 ELSE 0 END) reachable_obj_count 29 FROM heap_graph_object o JOIN heap_graph_class c ON o.type_id = c.id 30 GROUP BY 1, 2, 3 31), 32-- Group by to build the repeated field by upid, ts 33heap_obj_histogram_count_protos AS ( 34 SELECT 35 upid, 36 graph_sample_ts, 37 RepeatedField(JavaHeapHistogram_TypeCount( 38 'type_name', type_name, 39 'obj_count', obj_count, 40 'reachable_obj_count', reachable_obj_count 41 )) AS count_protos 42 FROM heap_obj_histograms 43 GROUP BY 1, 2 44), 45-- Group by to build the repeated field by upid 46heap_obj_histogram_sample_protos AS ( 47 SELECT 48 upid, 49 RepeatedField(JavaHeapHistogram_Sample( 50 'ts', graph_sample_ts, 51 'type_count', count_protos 52 )) AS sample_protos 53 FROM heap_obj_histogram_count_protos 54 GROUP BY 1 55) 56SELECT JavaHeapHistogram( 57 'instance_stats', RepeatedField(JavaHeapHistogram_InstanceStats( 58 'upid', upid, 59 'process', process_metadata.metadata, 60 'samples', sample_protos 61 ))) 62FROM heap_obj_histogram_sample_protos JOIN process_metadata USING (upid); 63