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 17-- Count packages by package UID. 18CREATE PERFETTO TABLE _uid_package_count AS 19SELECT 20 uid, 21 count(1) AS cnt 22FROM package_list 23GROUP BY 24 1; 25 26CREATE PERFETTO FUNCTION _android_package_for_process( 27 uid LONG, 28 uid_count LONG, 29 process_name STRING 30) 31RETURNS TABLE ( 32 package_name STRING, 33 version_code LONG, 34 debuggable BOOL 35) AS 36WITH 37 min_distance AS ( 38 SELECT 39 -- SQLite allows omitting the group-by for the MIN: the other columns 40 -- will match the row with the minimum value. 41 min(length($process_name) - length(package_name)), 42 package_name, 43 version_code, 44 debuggable 45 FROM package_list 46 WHERE 47 ( 48 ( 49 $uid = uid 50 AND ( 51 -- Either a unique match or process name is a prefix the package name 52 $uid_count = 1 53 OR $process_name GLOB package_name || '*' 54 ) 55 ) 56 OR ( 57 -- isolated processes can only be matched based on the name 58 $uid >= 90000 59 AND $uid < 100000 60 AND str_split($process_name, ':', 0) GLOB package_name || '*' 61 ) 62 ) 63 ) 64SELECT 65 package_name, 66 version_code, 67 debuggable 68FROM min_distance; 69 70-- Table containing the upids of all kernel tasks. 71CREATE PERFETTO TABLE _android_kernel_tasks ( 72 -- upid of the kernel task 73 upid LONG 74) AS 75SELECT 76 a.upid 77FROM process AS a 78LEFT JOIN process AS b 79 ON a.parent_upid = b.upid 80WHERE 81 b.name = 'kthreadd' OR a.pid = 0 OR b.pid = 0 82ORDER BY 83 1; 84 85-- Returns true if the process is a kernel task. 86CREATE PERFETTO FUNCTION android_is_kernel_task( 87 -- Queried process 88 upid LONG 89) 90-- True for kernel tasks 91RETURNS BOOL AS 92SELECT 93 EXISTS( 94 SELECT 95 TRUE 96 FROM _android_kernel_tasks 97 WHERE 98 upid = $upid 99 ); 100 101-- Data about packages running on the process. 102CREATE PERFETTO TABLE android_process_metadata ( 103 -- Process upid. 104 upid JOINID(process.id), 105 -- Process pid. 106 pid LONG, 107 -- Process name. 108 process_name STRING, 109 -- Android app UID. 110 uid LONG, 111 -- Whether the UID is shared by multiple packages. 112 shared_uid BOOL, 113 -- Android user id for multi-user devices 114 user_id LONG, 115 -- Name of the packages running in this process. 116 package_name STRING, 117 -- Package version code. 118 version_code LONG, 119 -- Whether package is debuggable. 120 debuggable LONG, 121 -- Whether the task is kernel or not 122 is_kernel_task BOOL 123) AS 124SELECT 125 process.upid, 126 process.pid, 127 -- workaround for b/169226092: the bug has been fixed it Android T, but 128 -- we support ingesting traces from older Android versions. 129 CASE 130 -- cmdline gets rewritten after fork, if these are still there we must 131 -- have seen a racy capture. 132 WHEN length(process.name) = 15 133 AND ( 134 process.cmdline IN ('zygote', 'zygote64', '<pre-initialized>') 135 OR process.cmdline GLOB '*' || process.name 136 ) 137 THEN process.cmdline 138 ELSE process.name 139 END AS process_name, 140 process.android_appid AS uid, 141 process.android_user_id AS user_id, 142 CASE WHEN _uid_package_count.cnt > 1 THEN TRUE ELSE NULL END AS shared_uid, 143 plist.package_name, 144 plist.version_code, 145 plist.debuggable, 146 android_is_kernel_task(process.upid) AS is_kernel_task 147FROM process 148LEFT JOIN _uid_package_count 149 ON process.android_appid = _uid_package_count.uid 150LEFT JOIN _android_package_for_process(process.android_appid, _uid_package_count.cnt, process.name) AS plist 151ORDER BY 152 upid; 153