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
17DROP TABLE IF EXISTS {{table_name}}_by_priority_stats;
18
19CREATE TABLE {{table_name}}_by_priority_stats (
20  process_name TEXT,
21  priority TEXT,
22  min_value REAL,
23  max_value REAL,
24  avg_value REAL,
25  PRIMARY KEY (process_name, priority)
26);
27
28INSERT INTO {{table_name}}_by_priority_stats
29SELECT
30  process.name AS process_name,
31  CASE
32    WHEN oom_score_val < -900 THEN 'native'
33    WHEN oom_score_val < -800 THEN 'system'
34    WHEN oom_score_val < -700 THEN 'persistent'
35    WHEN oom_score_val <  0   THEN 'persistent_service'
36    WHEN oom_score_val <  100 THEN 'foreground'
37    WHEN oom_score_val <  200 THEN 'visible'
38    WHEN oom_score_val <  300 THEN 'perceptible'
39    WHEN oom_score_val <  400 THEN 'backup'
40    WHEN oom_score_val <  500 THEN 'heavy_weight'
41    WHEN oom_score_val <  600 THEN 'service_a'
42    WHEN oom_score_val <  700 THEN 'home'
43    WHEN oom_score_val <  800 THEN 'prev'
44    WHEN oom_score_val <  900 THEN 'service_b'
45    ELSE 'cached'
46  END AS priority,
47  MIN(span.{{table_name}}_val) AS min_value,
48  MAX(span.{{table_name}}_val) AS max_value,
49  SUM(span.{{table_name}}_val * span.dur) / SUM(span.dur) AS avg_value
50FROM {{table_name}}_by_oom_span AS span JOIN process USING(upid)
51WHERE process.name IS NOT NULL
52GROUP BY 1, 2
53ORDER BY 1, 2;
54
55DROP VIEW IF EXISTS {{table_name}}_by_priority_stats_proto;
56
57CREATE VIEW {{table_name}}_by_priority_stats_proto AS
58SELECT
59  process_name,
60  priority,
61  AndroidMemoryMetric_Counter(
62    'min', min_value,
63    'max', max_value,
64    'avg', avg_value
65  ) AS proto
66FROM {{table_name}}_by_priority_stats;
67