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 TABLE IF EXISTS heap_obj_histograms; 44CREATE TABLE heap_obj_histograms AS 45 SELECT 46 o.upid, 47 o.graph_sample_ts, 48 o.type_id cls_id, 49 COUNT(1) obj_count, 50 SUM(IIF(o.reachable, 1, 0)) reachable_obj_count, 51 SUM(self_size) / 1024 size_kb, 52 SUM(IIF(o.reachable, self_size, 0)) / 1024 reachable_size_kb, 53 SUM(native_size) / 1024 native_size_kb, 54 SUM(IIF(o.reachable, native_size, 0)) / 1024 reachable_native_size_kb 55 FROM heap_graph_object o 56 GROUP BY 1, 2, 3 57 ORDER BY 1, 2, 3; 58 59DROP VIEW IF EXISTS java_heap_histogram_output; 60CREATE VIEW java_heap_histogram_output AS 61WITH 62-- Group by to build the repeated field by upid, ts 63heap_obj_histogram_count_protos AS ( 64 SELECT 65 upid, 66 graph_sample_ts, 67 RepeatedField(JavaHeapHistogram_TypeCount( 68 'type_name', IFNULL(c.deobfuscated_name, c.name), 69 'category', category, 70 'obj_count', obj_count, 71 'reachable_obj_count', reachable_obj_count, 72 'size_kb', size_kb, 73 'reachable_size_kb', reachable_size_kb, 74 'native_size_kb', native_size_kb, 75 'reachable_native_size_kb', reachable_native_size_kb 76 )) AS count_protos 77 FROM heap_obj_histograms hist 78 JOIN heap_graph_class c ON hist.cls_id = c.id 79 LEFT JOIN android_special_classes special USING(cls_id) 80 GROUP BY 1, 2 81), 82-- Group by to build the repeated field by upid 83heap_obj_histogram_sample_protos AS ( 84 SELECT 85 upid, 86 RepeatedField(JavaHeapHistogram_Sample( 87 'ts', graph_sample_ts, 88 'type_count', count_protos 89 )) AS sample_protos 90 FROM heap_obj_histogram_count_protos 91 GROUP BY 1 92) 93SELECT JavaHeapHistogram( 94 'instance_stats', RepeatedField(JavaHeapHistogram_InstanceStats( 95 'upid', upid, 96 'process', process_metadata.metadata, 97 'samples', sample_protos 98 ))) 99FROM heap_obj_histogram_sample_protos JOIN process_metadata USING (upid); 100