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'); 25SELECT RUN_METRIC('android/power_drain_in_watts.sql'); 26 27-- SPAN_JOIN does not yet support non-integer partitions so add an integer 28-- column that corresponds to the power rail name. 29DROP TABLE IF EXISTS power_rail_name_mapping; 30CREATE TABLE power_rail_name_mapping AS 31SELECT DISTINCT name, 32 ROW_NUMBER() OVER() AS idx 33FROM drain_in_watts; 34 35DROP VIEW IF EXISTS mapped_drain_in_watts; 36CREATE VIEW mapped_drain_in_watts AS 37SELECT d.name, ts, dur, drain_w, idx 38FROM drain_in_watts d 39 JOIN power_rail_name_mapping p ON d.name = p.name; 40 41DROP TABLE IF EXISTS real_{{input}}_power; 42CREATE VIRTUAL TABLE real_{{input}}_power USING SPAN_JOIN( 43 {{input}}, 44 mapped_drain_in_watts PARTITIONED idx 45); 46 47-- Actual power usage for chrome across the categorised slices contained in the 48-- input table broken down by subsystem. 49DROP VIEW IF EXISTS {{output}}; 50CREATE VIEW {{output}} AS 51SELECT s.id, 52 ts, 53 dur, 54 {{category}}, 55 subsystem, 56 joules, 57 joules / dur * 1e9 AS drain_w 58FROM ( 59 SELECT id, 60 subsystem, 61 SUM(drain_w * dur / 1e9) AS joules 62 FROM real_{{input}}_power 63 JOIN power_counters 64 WHERE real_{{input}}_power.name = power_counters.name 65 GROUP BY id, 66 subsystem 67 ) p 68 JOIN {{input}} s 69WHERE s.id = p.id 70ORDER BY s.id; 71 72SELECT id, 73 subsystem, 74 SUM(drain_w * dur / 1e9) AS joules 75 FROM real_{{input}}_power 76 JOIN power_counters 77 WHERE real_{{input}}_power.name = power_counters.name 78 GROUP BY id, 79 subsystem 80