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 17-- This is a templated metric that takes 3 parameters: 18-- input: name of a table/view which must have columns: id, ts, dur and a 19-- "category" column 20-- output: name of the view that will be created 21-- category: name of the category column in the input table, which will be 22-- preserved in the output 23 24SELECT RUN_METRIC('chrome/chrome_processes.sql'); 25 26-- CPU time slices for Chrome threads. 27DROP VIEW IF EXISTS chrome_cpu_slices; 28CREATE VIEW chrome_cpu_slices AS 29SELECT counters.id AS counter_id, 30 track_id, 31 ts, 32 lead(ts) OVER ( 33 PARTITION BY track_id 34 ORDER BY ts 35 ) - ts AS dur, 36 CAST( 37 lead(value) OVER ( 38 PARTITION BY track_id 39 ORDER BY ts 40 ) - value AS "INT64" 41 ) AS cpu_dur 42FROM counters, 43 ( 44 SELECT thread_counter_track.id 45 FROM chrome_thread 46 JOIN thread_counter_track ON chrome_thread.utid = thread_counter_track.utid 47 ) AS t 48WHERE t.id = track_id; 49 50DROP TABLE IF EXISTS {{input}}_cpu_time; 51CREATE VIRTUAL TABLE {{input}}_cpu_time USING SPAN_JOIN( 52 {{input}}, 53 chrome_cpu_slices PARTITIONED track_id 54); 55 56-- View containing the CPU time used (across all cores) for each category slice 57-- from input. 58-- This will slightly overestimate the CPU time for some category slices as the 59-- cpu time slices don't always line up with the category slices. However the 60-- CPU slices are small enough this makes very little difference. 61DROP VIEW IF EXISTS {{output}}; 62CREATE VIEW {{output}} AS 63SELECT s.id, 64 s.ts, 65 s.dur, 66 s.{{category}}, 67 SUM(cpu_dur) AS cpu_dur 68FROM {{input}}_cpu_time r 69JOIN {{input}} s 70WHERE r.id = s.id 71GROUP BY r.id; 72