• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1-- Copyright 2025 The Android Open Source Project
2--
3-- Licensed under the Apache License, Version 2.0 (the "License");
4-- you may not use this file except in compliance with the License.
5-- You may obtain a copy of the License at
6--
7--     https://www.apache.org/licenses/LICENSE-2.0
8--
9-- Unless required by applicable law or agreed to in writing, software
10-- distributed under the License is distributed on an "AS IS" BASIS,
11-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-- See the License for the specific language governing permissions and
13-- limitations under the License.
14--
15
16INCLUDE PERFETTO MODULE android.suspend;
17
18CREATE PERFETTO TABLE _android_kernel_wakelocks_base AS
19WITH
20  raw AS (
21    SELECT
22      -- Move back by one period here since our ts represents wakelock counts up
23      -- to that point, but counters project forward from their ts.
24      lag(ts) OVER (PARTITION BY track_id ORDER BY ts) AS ts,
25      ts AS next_ts,
26      name,
27      extract_arg(dimension_arg_set_id, 'wakelock_type') AS type,
28      value,
29      lag(value) OVER (PARTITION BY track_id ORDER BY ts) AS lag_value
30    FROM track AS t
31    JOIN counter AS c
32      ON t.id = c.track_id
33    WHERE
34      t.type = 'android_kernel_wakelock'
35  )
36SELECT
37  ts,
38  ts AS original_ts,
39  next_ts - ts AS dur,
40  name,
41  hash(name) AS name_int,
42  type,
43  value - lag_value AS held_dur
44FROM raw;
45
46CREATE VIRTUAL TABLE _android_kernel_wakelocks_joined USING span_join (_android_kernel_wakelocks_base partitioned name_int, android_suspend_state);
47
48-- Table of kernel (or native) wakelocks with held duration.
49--
50-- Subtracts suspended time from each period to calculate the
51-- fraction of awake time for which the wakelock was held.
52CREATE PERFETTO TABLE android_kernel_wakelocks (
53  -- Timestamp.
54  ts TIMESTAMP,
55  -- Duration.
56  dur DURATION,
57  -- Kernel or native wakelock name.
58  name STRING,
59  -- 'kernel' or 'native'.
60  type STRING,
61  -- Time the wakelock was held.
62  held_dur DURATION,
63  -- Fraction of awake (not suspended) time the wakelock was held.
64  held_ratio DOUBLE
65) AS
66WITH
67  base AS (
68    SELECT
69      original_ts AS ts,
70      name,
71      type,
72      held_dur,
73      sum(dur) AS dur,
74      sum(iif(power_state = 'awake', dur, 0)) AS awake_dur
75    FROM _android_kernel_wakelocks_joined
76    WHERE
77      power_state = 'awake'
78    GROUP BY
79      1,
80      2,
81      3,
82      4
83  )
84SELECT
85  ts,
86  dur,
87  name,
88  type,
89  cast_int!(held_dur) AS held_dur,
90  max(min(held_dur / awake_dur, 1.0), 0.0) AS held_ratio
91FROM base;
92