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 17SELECT RUN_METRIC('android/process_oom_score.sql'); 18 19-- All LMK events ordered by timestamp 20DROP TABLE IF EXISTS lmk_events; 21CREATE TABLE lmk_events AS 22WITH raw_events AS ( 23 SELECT upid, MAX(ts) AS ts 24 FROM instant 25 JOIN process_track ON instant.track_id = process_track.id 26 WHERE instant.name = 'mem.lmk' 27 GROUP BY 1 28) 29SELECT 30 raw_events.ts, 31 raw_events.upid, 32 oom_scores.oom_score_val AS score 33FROM raw_events 34LEFT JOIN oom_score_span oom_scores 35 ON (raw_events.upid = oom_scores.upid 36 AND raw_events.ts >= oom_scores.ts 37 AND raw_events.ts < oom_scores.ts + oom_scores.dur) 38ORDER BY 1; 39 40DROP VIEW IF EXISTS android_lmk_event; 41CREATE VIEW android_lmk_event AS 42WITH raw_events AS ( 43 SELECT 44 ts, 45 LEAD(ts) OVER (ORDER BY ts) - ts AS dur, 46 CAST(value AS INTEGER) AS pid 47 FROM counter c 48 JOIN counter_track t ON t.id = c.track_id 49 WHERE t.name = 'kill_one_process' 50 UNION ALL 51 SELECT 52 slice.ts, 53 slice.dur, 54 CAST(STR_SPLIT(slice.name, ",", 1) AS INTEGER) AS pid 55 FROM slice 56 WHERE slice.name GLOB 'lmk,*' 57), 58lmks_with_proc_name AS ( 59 SELECT 60 *, 61 process.name AS process_name 62 FROM raw_events 63 LEFT JOIN process ON 64 process.pid = raw_events.pid 65 AND (raw_events.ts >= process.start_ts OR process.start_ts IS NULL) 66 AND (raw_events.ts < process.end_ts OR process.end_ts IS NULL) 67 WHERE raw_events.pid != 0 68) 69SELECT 70 'slice' AS track_type, 71 'Low Memory Kills (LMKs)' AS track_name, 72 ts, 73 dur, 74 CASE 75 WHEN process_name IS NULL THEN printf('Process %d', lmk.pid) 76 ELSE printf('%s (pid: %d)', process_name, lmk.pid) 77 END AS slice_name 78FROM lmks_with_proc_name AS lmk; 79 80DROP VIEW IF EXISTS android_lmk_output; 81CREATE VIEW android_lmk_output AS 82WITH lmk_counts AS ( 83 SELECT score, COUNT(1) AS count 84 FROM lmk_events 85 GROUP BY score 86) 87SELECT AndroidLmkMetric( 88 'total_count', (SELECT COUNT(1) FROM lmk_events), 89 'by_oom_score', ( 90 SELECT 91 RepeatedField(AndroidLmkMetric_ByOomScore( 92 'oom_score_adj', score, 93 'count', count 94 )) 95 FROM lmk_counts 96 WHERE score IS NOT NULL 97 ), 98 'oom_victim_count', ( 99 SELECT COUNT(1) 100 FROM instant 101 WHERE name = 'mem.oom_kill' 102 ) 103); 104