• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1--
2-- Copyright 2022 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 IMPORT('android.startup.startups');
18
19-- Create the base CPU span join table.
20SELECT RUN_METRIC('android/android_cpu_agg.sql');
21SELECT RUN_METRIC('android/cpu_info.sql');
22
23-- Create a span join safe launches view; since both views
24-- being span joined have an "id" column, we need to rename
25-- the id column for launches to disambiguate the two.
26DROP VIEW IF EXISTS android_launches_span_join_safe;
27CREATE VIEW android_launches_span_join_safe AS
28SELECT ts, dur, startup_id
29FROM android_startups;
30
31DROP VIEW IF EXISTS launches_span_join_safe;
32CREATE VIEW launches_span_join_safe AS
33SELECT startup_id AS launch_id, * FROM android_launches_span_join_safe;
34
35-- Span join the CPU table with the launches table to get the
36-- breakdown per-cpu.
37DROP TABLE IF EXISTS cpu_freq_sched_per_thread_per_launch;
38CREATE VIRTUAL TABLE cpu_freq_sched_per_thread_per_launch
39USING SPAN_JOIN(
40  android_launches_span_join_safe,
41  cpu_freq_sched_per_thread PARTITIONED cpu
42);
43
44-- Materialized to avoid span-joining once per core type.
45DROP TABLE IF EXISTS mcycles_per_core_type_per_launch;
46CREATE TABLE mcycles_per_core_type_per_launch AS
47SELECT
48  startup_id,
49  IFNULL(core_type_per_cpu.core_type, 'unknown') AS core_type,
50  CAST(SUM(dur * freq_khz / 1000) / 1e9 AS INT) AS mcycles
51FROM cpu_freq_sched_per_thread_per_launch
52LEFT JOIN core_type_per_cpu USING (cpu)
53WHERE utid != 0
54GROUP BY 1, 2;
55
56-- Given a launch id and core type, returns the number of mcycles consumed
57-- on CPUs of that core type during the launch.
58SELECT CREATE_FUNCTION(
59  'MCYCLES_FOR_LAUNCH_AND_CORE_TYPE(startup_id INT, core_type STRING)',
60  'INT',
61  '
62    SELECT mcycles
63    FROM mcycles_per_core_type_per_launch m
64    WHERE m.startup_id = $startup_id AND m.core_type = $core_type
65  '
66);
67
68-- Contains the process using the most mcycles during the launch
69-- *excluding the process being started*.
70-- Materialized to avoid span-joining once per launch.
71DROP TABLE IF EXISTS top_mcyles_process_excluding_started_per_launch;
72CREATE TABLE top_mcyles_process_excluding_started_per_launch AS
73WITH mcycles_per_launch_and_process AS MATERIALIZED (
74  SELECT
75    startup_id,
76    upid,
77    CAST(SUM(dur * freq_khz / 1000) / 1e9 AS INT) AS mcycles
78  FROM cpu_freq_sched_per_thread_per_launch c
79  JOIN thread USING (utid)
80  JOIN process USING (upid)
81  WHERE
82    utid != 0
83    AND upid NOT IN (
84      SELECT upid
85      FROM android_startup_processes l
86    )
87  GROUP BY startup_id, upid
88)
89SELECT *
90FROM (
91  SELECT
92    *,
93    ROW_NUMBER() OVER (PARTITION BY startup_id ORDER BY mcycles DESC) AS mcycles_rank
94  FROM mcycles_per_launch_and_process
95)
96WHERE mcycles_rank <= 5;
97
98-- Given a launch id, returns the name of the processes consuming the most
99-- mcycles during the launch excluding the process being started.
100SELECT CREATE_FUNCTION(
101  'N_MOST_ACTIVE_PROCESS_NAMES_FOR_LAUNCH(startup_id INT)',
102  'STRING',
103  '
104    SELECT RepeatedField(process_name)
105    FROM (
106      SELECT IFNULL(process.name, "[NULL]") AS process_name
107      FROM top_mcyles_process_excluding_started_per_launch
108      JOIN process USING (upid)
109      WHERE startup_id = $startup_id
110      ORDER BY mcycles DESC
111    );
112  '
113);
114
115-- Given a launch id, returns the most active process name.
116SELECT CREATE_FUNCTION(
117  'MOST_ACTIVE_PROCESS_FOR_LAUNCH(startup_id INT)',
118  'STRING',
119  '
120    SELECT process.name AS process_name
121    FROM top_mcyles_process_excluding_started_per_launch
122    JOIN process USING (upid)
123    WHERE startup_id = $startup_id
124    ORDER BY mcycles DESC LIMIT 1;
125  '
126);
127