Skip to content

Commit

Permalink
Merge changes from topic "interval_intersect_and_optimizations" into …
Browse files Browse the repository at this point in the history
…main

* changes:
  stdlib: Use interval_intersect to create system_state
  stdlib: Prepend NULL dummy slices per CPU for Wattson
  stdlib: Separate cpu_idle|freq|freq_idle
  • Loading branch information
Samuel Wu authored and Gerrit Code Review committed Oct 3, 2024
2 parents 6f096dc + 25e08ce commit c3b42d9
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 200 deletions.
2 changes: 2 additions & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -13615,6 +13615,8 @@ genrule {
"src/trace_processor/perfetto_sql/stdlib/viz/summary/tracks.sql",
"src/trace_processor/perfetto_sql/stdlib/viz/threads.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/arm_dsu.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/cpu_freq.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/cpu_freq_idle.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/cpu_idle.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/cpu_split.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/curves/device.sql",
Expand Down
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3010,6 +3010,8 @@ perfetto_filegroup(
name = "src_trace_processor_perfetto_sql_stdlib_wattson_wattson",
srcs = [
"src/trace_processor/perfetto_sql/stdlib/wattson/arm_dsu.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/cpu_freq.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/cpu_freq_idle.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/cpu_idle.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/cpu_split.sql",
"src/trace_processor/perfetto_sql/stdlib/wattson/curves/device.sql",
Expand Down
2 changes: 2 additions & 0 deletions src/trace_processor/perfetto_sql/stdlib/wattson/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import("../../../../../gn/perfetto_sql.gni")
perfetto_sql_source_set("wattson") {
sources = [
"arm_dsu.sql",
"cpu_freq.sql",
"cpu_freq_idle.sql",
"cpu_idle.sql",
"cpu_split.sql",
"curves/device.sql",
Expand Down
54 changes: 54 additions & 0 deletions src/trace_processor/perfetto_sql/stdlib/wattson/cpu_freq.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--
-- Copyright 2024 The Android Open Source Project
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

INCLUDE PERFETTO MODULE linux.cpu.frequency;
INCLUDE PERFETTO MODULE wattson.device_infos;

CREATE PERFETTO TABLE _adjusted_cpu_freq AS
WITH _cpu_freq AS (
SELECT
ts,
dur,
freq,
cf.ucpu as cpu,
d_map.policy
FROM cpu_frequency_counters as cf
JOIN _dev_cpu_policy_map as d_map
ON cf.ucpu = d_map.cpu
),
-- Get first freq transition per CPU
first_cpu_freq_slices AS (
SELECT ts, cpu FROM _cpu_freq
GROUP BY cpu
ORDER by ts ASC
)
-- Prepend NULL slices up to first freq events on a per CPU basis
SELECT
-- Construct slices from first cpu ts up to first freq event for each cpu
trace_start() as ts,
first_slices.ts - trace_start() as dur,
NULL as freq,
first_slices.cpu,
d_map.policy
FROM first_cpu_freq_slices as first_slices
JOIN _dev_cpu_policy_map as d_map ON first_slices.cpu = d_map.cpu
UNION ALL
SELECT
ts,
dur,
freq,
cpu,
policy
FROM _cpu_freq;
75 changes: 75 additions & 0 deletions src/trace_processor/perfetto_sql/stdlib/wattson/cpu_freq_idle.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
--
-- Copyright 2024 The Android Open Source Project
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.

INCLUDE PERFETTO MODULE wattson.cpu_freq;
INCLUDE PERFETTO MODULE wattson.cpu_idle;
INCLUDE PERFETTO MODULE wattson.curves.utils;
INCLUDE PERFETTO MODULE wattson.device_infos;

-- Helper macro for using Perfetto table with interval intersect
CREATE PERFETTO MACRO _ii_subquery(tab TableOrSubquery)
RETURNS TableOrSubquery AS (SELECT _auto_id AS id, * FROM $tab);

-- Wattson estimation is valid from when first CPU0 frequency appears
CREATE PERFETTO TABLE _valid_window
AS
WITH window_start AS (
SELECT ts as start_ts
FROM _adjusted_cpu_freq
WHERE cpu = 0 AND freq IS NOT NULL
ORDER BY ts ASC
LIMIT 1
),
window AS (
SELECT start_ts as ts, trace_end() - start_ts as dur
FROM window_start
)
SELECT *, 0 as cpu FROM window
UNION ALL
SELECT *, 1 as cpu FROM window
UNION ALL
SELECT *, 2 as cpu FROM window
UNION ALL
SELECT *, 3 as cpu FROM window
UNION ALL
SELECT *, 4 as cpu FROM window
UNION ALL
SELECT *, 5 as cpu FROM window
UNION ALL
SELECT *, 6 as cpu FROM window
UNION ALL
SELECT *, 7 as cpu FROM window;

-- Start matching CPUs with 1D curves based on combination of freq and idle
CREATE PERFETTO TABLE _idle_freq_materialized
AS
SELECT
ii.ts, ii.dur, ii.cpu, freq.policy, freq.freq, idle.idle, lut.curve_value
FROM _interval_intersect!(
(
_ii_subquery!(_valid_window),
_ii_subquery!(_adjusted_cpu_freq),
_ii_subquery!(_adjusted_deep_idle)
),
(cpu)
) ii
JOIN _adjusted_cpu_freq AS freq ON freq._auto_id = id_1
JOIN _adjusted_deep_idle AS idle ON idle._auto_id = id_2
-- Left join since some CPUs may only match the 2D LUT
LEFT JOIN _filtered_curves_1d lut ON
freq.policy = lut.policy AND
freq.freq = lut.freq_khz AND
idle.idle = lut.idle;

39 changes: 31 additions & 8 deletions src/trace_processor/perfetto_sql/stdlib/wattson/cpu_idle.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ INCLUDE PERFETTO MODULE counters.intervals;
INCLUDE PERFETTO MODULE wattson.device_infos;

-- Get the corresponding deep idle time offset based on device and CPU.
CREATE PERFETTO TABLE _filtered_deep_idle_offsets
AS
CREATE PERFETTO VIEW _filtered_deep_idle_offsets AS
SELECT cpu, offset_ns
FROM _device_cpu_deep_idle_offsets as offsets
JOIN _wattson_device as device
Expand All @@ -27,8 +26,7 @@ ON offsets.device = device.name;
-- Adjust duration of active portion to be slightly longer to account for
-- overhead cost of transitioning out of deep idle. This is done because the
-- device is active and consumes power for longer than the logs actually report.
CREATE PERFETTO TABLE _adjusted_deep_idle
AS
CREATE PERFETTO TABLE _adjusted_deep_idle AS
WITH
idle_prev AS (
SELECT
Expand Down Expand Up @@ -61,11 +59,36 @@ WITH
idle
FROM idle_prev
JOIN _filtered_deep_idle_offsets USING (cpu)
),
_cpu_idle AS (
SELECT
ts,
LEAD(ts, 1, trace_end()) OVER (PARTITION BY cpu ORDER by ts) - ts as dur,
cpu,
cast_int!(IIF(idle = 4294967295, -1, idle)) AS idle
FROM idle_mod
),
-- Get first idle transition per CPU
first_cpu_idle_slices AS (
SELECT ts, cpu FROM _cpu_idle
GROUP BY cpu
ORDER by ts ASC
)
-- Prepend NULL slices up to first idle events on a per CPU basis
SELECT
-- Construct slices from first cpu ts up to first freq event for each cpu
trace_start() as ts,
first_slices.ts - trace_start() as dur,
first_slices.cpu,
NULL as idle
FROM first_cpu_idle_slices as first_slices
WHERE dur > 0
UNION ALL
SELECT
ts,
LEAD(ts, 1, trace_end()) OVER (PARTITION BY cpu ORDER by ts) - ts as dur,
dur,
cpu,
cast_int!(IIF(idle = 4294967295, -1, idle)) AS idle
FROM idle_mod;

idle
FROM _cpu_idle
-- Some durations are 0 post-adjustment and won't work with interval intersect
WHERE dur > 0;
Loading

0 comments on commit c3b42d9

Please sign in to comment.