• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1--
2-- Copyright 2024 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
16INCLUDE PERFETTO MODULE cpu.freq;
17
18-- Returns the timestamp of the start of the partition that contains the |ts|.
19CREATE PERFETTO FUNCTION _partition_start(ts INT, size INT) RETURNS INT AS
20-- Division of two ints would result in floor(ts/size).
21SELECT ($ts/$size)*$size;
22
23-- Returns the number of partitions required to cover all of the trace
24-- timestamps.
25CREATE PERFETTO FUNCTION _partition_count(size INT) RETURNS INT AS
26SELECT
27    (_partition_start(TRACE_END(), $size) -
28    _partition_start(TRACE_START(), $size))/$size + 1;
29
30-- Returns a table of partitions with first partition containing the
31-- TRACE_START() and last one containing TRACE_END().
32CREATE PERFETTO FUNCTION _partitions(size INT)
33RETURNS TABLE (ts INT, ts_end INT) AS
34WITH no_ends AS (
35SELECT
36    _partition_start(TRACE_START(), $size) + (id * $size) AS ts
37-- We are using the sched table for source of ids. If the table is too small
38-- for specified size, the results would be invalid none the less.
39FROM sched
40LIMIT _partition_count($size))
41SELECT ts, ts + $size AS ts_end FROM no_ends;
42
43-- Partitions any |intervals| table with partitions defined in the |partitions|
44-- table.
45CREATE PERFETTO MACRO _interval_partitions(
46  -- Requires |ts| and |ts_end| columns.
47  partitions TableOrSubquery,
48  -- Requires |ts| and |ts_end| column.
49  intervals TableOrSubquery
50) RETURNS TableOrSubquery AS (
51SELECT
52  p.ts AS partition_ts,
53  IIF(i.ts_end < p.ts_end, i.ts_end, p.ts_end) AS ts_end,
54  IIF(i.ts < p.ts, p.ts, i.ts) AS ts
55FROM $intervals i
56JOIN $partitions p
57ON (p.ts <= i.ts AND i.ts < p.ts_end));
58
59-- Returns a table of utilization per given period.
60-- Utilization is calculated as sum of average utilization of each CPU in each
61-- period, which is defined as a multiply of |interval|. For this reason
62-- first and last period might have lower then real utilization.
63CREATE PERFETTO MACRO _cpu_avg_utilization_per_period(
64  -- Length of the period on which utilization should be averaged.
65  interval Expr,
66  -- Either sched table or its filtered down version.
67  sched_table TableOrSubquery
68)
69-- The returned table has the schema (ts UINT32, utilization DOUBLE,
70-- unnormalized_utilization DOUBLE).
71RETURNS TableOrSubquery AS (
72SELECT
73  partition_ts AS ts,
74  SUM(ts_end - ts)/(cast_double!($interval) * (SELECT MAX(cpu) + 1 FROM sched)) AS utilization,
75  SUM(ts_end - ts)/cast_double!($interval) AS unnormalized_utilization
76FROM _interval_partitions!(_partitions($interval), $sched_table)
77GROUP BY 1);
78
79CREATE PERFETTO VIEW _cpu_freq_for_metrics AS
80SELECT
81    id,
82    ts,
83    dur,
84    cpu,
85    freq
86FROM cpu_freq_counters;
87
88CREATE PERFETTO VIEW _sched_without_id AS
89SELECT ts, dur, utid, cpu
90FROM sched
91WHERE utid != 0 AND dur != -1;
92
93CREATE VIRTUAL TABLE _cpu_freq_per_thread_span_join
94USING SPAN_LEFT_JOIN(
95    _sched_without_id PARTITIONED cpu,
96    _cpu_freq_for_metrics PARTITIONED cpu);
97
98CREATE PERFETTO TABLE _cpu_freq_per_thread
99AS SELECT * FROM _cpu_freq_per_thread_span_join;
100
101