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 19DROP TABLE IF EXISTS android_special_classes; 20CREATE TABLE android_special_classes AS 21WITH RECURSIVE cls_visitor(cls_id, category) AS ( 22 SELECT id, name FROM heap_graph_class WHERE name IN ( 23 'android.view.View', 24 'android.app.Activity', 25 'android.app.Fragment', 26 'android.app.Service', 27 'android.content.ContentProvider', 28 'android.content.BroadcastReceiver', 29 'android.content.Context', 30 'android.content.Intent', 31 'android.content.res.ApkAssets', 32 'android.os.Handler', 33 'android.os.Parcel', 34 'android.graphics.Bitmap', 35 'android.graphics.BaseCanvas', 36 'com.android.server.am.PendingIntentRecord') 37 UNION ALL 38 SELECT child.id, parent.category 39 FROM heap_graph_class child JOIN cls_visitor parent ON parent.cls_id = child.superclass_id 40) 41SELECT * FROM cls_visitor; 42 43DROP VIEW IF EXISTS java_heap_histogram_output; 44CREATE VIEW java_heap_histogram_output AS 45WITH 46-- Base histogram table 47heap_obj_histograms AS ( 48 SELECT 49 o.upid, 50 o.graph_sample_ts, 51 IFNULL(c.deobfuscated_name, c.name) AS type_name, 52 special.category, 53 COUNT(1) obj_count, 54 SUM(CASE o.reachable WHEN TRUE THEN 1 ELSE 0 END) reachable_obj_count 55 FROM heap_graph_object o 56 JOIN heap_graph_class c ON o.type_id = c.id 57 LEFT JOIN android_special_classes special ON special.cls_id = c.id 58 GROUP BY 1, 2, 3, 4 59 ORDER BY 6 DESC 60), 61-- Group by to build the repeated field by upid, ts 62heap_obj_histogram_count_protos AS ( 63 SELECT 64 upid, 65 graph_sample_ts, 66 RepeatedField(JavaHeapHistogram_TypeCount( 67 'type_name', type_name, 68 'category', category, 69 'obj_count', obj_count, 70 'reachable_obj_count', reachable_obj_count 71 )) AS count_protos 72 FROM heap_obj_histograms 73 GROUP BY 1, 2 74), 75-- Group by to build the repeated field by upid 76heap_obj_histogram_sample_protos AS ( 77 SELECT 78 upid, 79 RepeatedField(JavaHeapHistogram_Sample( 80 'ts', graph_sample_ts, 81 'type_count', count_protos 82 )) AS sample_protos 83 FROM heap_obj_histogram_count_protos 84 GROUP BY 1 85) 86SELECT JavaHeapHistogram( 87 'instance_stats', RepeatedField(JavaHeapHistogram_InstanceStats( 88 'upid', upid, 89 'process', process_metadata.metadata, 90 'samples', sample_protos 91 ))) 92FROM heap_obj_histogram_sample_protos JOIN process_metadata USING (upid); 93