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-- This metric assigns a power drain rate in milliampers to each sched slice. 17-- The power_profile table should be populated with device power profile data 18-- before running the metric. It has the following scheme: 19-- (device STRING, cpu INT, cluster INT, freq INT, power DOUBLE). 20-- 21-- Metric usage examples: 22-- 1) Compute the power cost of every thread in milliamper-seconds: 23-- SELECT 24-- utid, 25-- SUM(dur * COALESCE(power_ma, 0) / 1e9) AS power_mas 26-- FROM power_per_thread 27-- GROUP BY utid; 28-- 2) Compute the total power cost of all slices from a table 'my_slice': 29-- CREATE VIEW my_slice_utid AS 30-- SELECT ts, dur, utid 31-- FROM my_slice 32-- INNER JOIN thread_track ON track_id = thread_track.id; 33-- 34-- CREATE VIRTUAL TABLE my_slice_power 35-- USING SPAN_JOIN(power_per_thread PARTITIONED utid, 36-- my_slice_utid PARTITIONED utid); 37-- 38-- SELECT 39-- SUM(dur * COALESCE(power_ma, 0) / 1e9) as power_mas 40-- FROM my_slice_power; 41 42SELECT RUN_METRIC('android/android_cpu_agg.sql'); 43SELECT RUN_METRIC('android/power_profile_data.sql'); 44 45DROP VIEW IF EXISTS device; 46CREATE VIEW device AS 47WITH 48 after_first_slash(str) AS ( 49 SELECT SUBSTR(str_value, INSTR(str_value, '/') + 1) 50 FROM metadata 51 WHERE name = 'android_build_fingerprint' 52 ), 53 before_second_slash(str) AS ( 54 SELECT SUBSTR(str, 0, INSTR(str, '/')) 55 FROM after_first_slash 56 ) 57SELECT str AS name FROM before_second_slash; 58 59DROP VIEW IF EXISTS power_view; 60CREATE VIEW power_view AS 61SELECT 62 cpu_freq_view.cpu AS cpu, 63 ts, 64 dur, 65 power AS power_ma 66FROM cpu_freq_view 67JOIN power_profile ON ( 68 power_profile.device = (SELECT name FROM device) 69 AND power_profile.cpu = cpu_freq_view.cpu 70 AND power_profile.freq = cpu_freq_view.freq_khz 71); 72 73-- utid = 0 is a reserved value used to mark sched slices where CPU was idle. 74-- It doesn't correspond to any real thread. 75DROP VIEW IF EXISTS sched_real_threads; 76CREATE VIEW sched_real_threads AS 77SELECT * 78FROM sched 79WHERE utid != 0; 80 81DROP TABLE IF EXISTS power_per_thread; 82CREATE VIRTUAL TABLE power_per_thread 83USING SPAN_LEFT_JOIN(sched_real_threads PARTITIONED cpu, power_view PARTITIONED cpu); 84