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/* Create the file RSS table. */ 17CREATE VIEW file_rss AS 18SELECT ts, 19 LEAD(ts, 1, ts) OVER(PARTITION BY track_id ORDER BY ts) - ts as dur, 20 upid, 21 value AS file 22FROM counter 23JOIN process_counter_track 24 ON counter.track_id = process_counter_track.id 25WHERE process_counter_track.name IN ("mem.rss.file", "rss_stat.mm_filepages"); 26 27/* Create the anon RSS table. */ 28create view anon_rss as 29select ts, 30 LEAD(ts, 1, ts) OVER(PARTITION BY track_id ORDER BY ts) - ts as dur, 31 upid, 32 value as anon 33FROM counter 34JOIN process_counter_track 35 ON counter.track_id = process_counter_track.id 36where process_counter_track.name in ("mem.rss.anon", "rss_stat.mm_anonpages"); 37 38/* Create the oom adj table. */ 39create view oom_adj as 40select ts, 41 LEAD(ts, 1, ts) OVER(PARTITION BY track_id ORDER BY ts) - ts as dur, 42 upid, 43 value as oom_score_adj 44FROM counter 45JOIN process_counter_track 46 ON counter.track_id = process_counter_track.id 47where process_counter_track.name = 'oom_score_adj'; 48 49/* Harmonise the three tables above into a single table. */ 50CREATE VIRTUAL TABLE anon_file USING span_join(anon_rss PARTITIONED upid, file_rss PARTITIONED upid); 51 52CREATE VIRTUAL TABLE anon_file_oom USING span_join(anon_file PARTITIONED upid, oom_adj PARTITIONED upid); 53 54/* For each span, compute the RSS (for a given upid) for each category of oom_adj */ 55CREATE VIEW rss_spans_for_oom_upid AS 56SELECT ts, 57 dur, 58 upid, 59 CASE WHEN oom_score_adj < 0 THEN file + anon ELSE 0 END as rss_oom_lt_zero, 60 CASE WHEN oom_score_adj <= 0 THEN file + anon ELSE 0 END as rss_oom_eq_zero, 61 CASE WHEN oom_score_adj < 20 THEN file + anon ELSE 0 END as rss_fg, 62 CASE WHEN oom_score_adj < 700 THEN file + anon ELSE 0 END as rss_bg, 63 CASE WHEN oom_score_adj < 900 THEN file + anon ELSE 0 END as rss_low_bg, 64 file + anon as rss_cached 65FROM anon_file_oom; 66 67/* Convert the raw RSS values to the change in RSS (for a given upid) over time */ 68CREATE VIEW rss_spans_rss_change AS 69SELECT ts, 70 dur, 71 upid, 72 rss_oom_lt_zero - lag(rss_oom_lt_zero, 1, 0) OVER win as rss_oom_lt_zero_diff, 73 rss_oom_eq_zero - lag(rss_oom_eq_zero, 1, 0) OVER win as rss_oom_eq_zero_diff, 74 rss_fg - lag(rss_fg, 1, 0) OVER win as rss_fg_diff, 75 rss_bg - lag(rss_bg, 1, 0) OVER win as rss_bg_diff, 76 rss_low_bg - lag(rss_low_bg, 1, 0) OVER win as rss_low_bg_diff, 77 rss_cached - lag(rss_cached, 1, 0) OVER win as rss_cached_diff 78FROM rss_spans_for_oom_upid 79WINDOW win AS (PARTITION BY upid ORDER BY ts); 80 81/* 82 * Compute a rolling sum of anon + file for each category of process state. 83 * (note: we no longer consider upid in the windows which means we are now 84 * computing a rolling sum for all the processes). 85 */ 86CREATE VIEW output AS 87SELECT ts, 88 lead(ts, 1, ts + dur) over win - ts as dur, 89 SUM(rss_oom_lt_zero_diff) OVER win as rss_oom_lt_zero, 90 SUM(rss_oom_eq_zero_diff) OVER win as rss_oom_eq_zero, 91 SUM(rss_fg_diff) OVER win as rss_fg, 92 SUM(rss_bg_diff) OVER win as rss_bg, 93 SUM(rss_low_bg_diff) OVER win as rss_low_bg, 94 SUM(rss_cached_diff) OVER win as rss_cached 95FROM rss_spans_rss_change 96WINDOW win as (ORDER BY ts) 97ORDER BY ts; 98 99/* 100 * Print out the final result (note: dur = 0 is excluded to account for times 101 * where multiple processes report a new memory value at the same timestamp) 102 */ 103SELECT * 104FROM output 105WHERE dur > 0 106