• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.ContentProviderClient',
27    'android.os.Binder',
28    'android.os.BinderProxy',
29    'android.os.Parcel',
30    'com.android.server.am.ConnectionRecord',
31    'com.android.server.am.PendingIntentRecord')
32  UNION ALL
33  SELECT child.id, parent.category
34  FROM heap_graph_class child JOIN cls_visitor parent ON parent.cls_id = child.superclass_id
35)
36SELECT * FROM cls_visitor;
37
38DROP TABLE IF EXISTS heap_obj_histograms;
39CREATE TABLE heap_obj_histograms AS
40SELECT
41  o.upid,
42  o.graph_sample_ts,
43  o.type_id AS cls_id,
44  COUNT(1) AS obj_count,
45  SUM(IIF(o.reachable, 1, 0)) AS reachable_obj_count,
46  SUM(self_size) / 1024 AS size_kb,
47  SUM(IIF(o.reachable, self_size, 0)) / 1024 AS reachable_size_kb,
48  SUM(native_size) / 1024 AS native_size_kb,
49  SUM(IIF(o.reachable, native_size, 0)) / 1024 AS reachable_native_size_kb
50FROM heap_graph_object o
51GROUP BY 1, 2, 3
52ORDER BY 1, 2, 3;
53
54DROP VIEW IF EXISTS java_heap_histogram_output;
55CREATE VIEW java_heap_histogram_output AS
56WITH
57-- Group by to build the repeated field by upid, ts
58heap_obj_histogram_count_protos AS (
59  SELECT
60    upid,
61    graph_sample_ts,
62    RepeatedField(JavaHeapHistogram_TypeCount(
63      'type_name', IFNULL(c.deobfuscated_name, c.name),
64      'category', category,
65      'obj_count', obj_count,
66      'reachable_obj_count', reachable_obj_count,
67      'size_kb', size_kb,
68      'reachable_size_kb', reachable_size_kb,
69      'native_size_kb', native_size_kb,
70      'reachable_native_size_kb', reachable_native_size_kb
71    )) AS count_protos
72  FROM heap_obj_histograms hist
73  JOIN heap_graph_class c ON hist.cls_id = c.id
74  LEFT JOIN android_special_classes special USING(cls_id)
75  GROUP BY 1, 2
76),
77-- Group by to build the repeated field by upid
78heap_obj_histogram_sample_protos AS (
79  SELECT
80    upid,
81    RepeatedField(JavaHeapHistogram_Sample(
82      'ts', graph_sample_ts,
83      'type_count', count_protos
84    )) AS sample_protos
85  FROM heap_obj_histogram_count_protos
86  GROUP BY 1
87)
88SELECT JavaHeapHistogram(
89  'instance_stats', RepeatedField(JavaHeapHistogram_InstanceStats(
90    'upid', upid,
91    'process', process_metadata.metadata,
92    'samples', sample_protos
93  )))
94FROM heap_obj_histogram_sample_protos JOIN process_metadata USING (upid);
95