Getting Started with PerfettoSQL Pipe Syntax

Warning: Pipe syntax is experimental and unstable. Do not rely on it in dashboards, scripts, or production queries. Anything can and will break without notice. To run the examples on this page in the Perfetto UI or trace_processor_shell, start your query with PERFETTO PRAGMA pipelines = 1;.

Related pages:


How Pipelines Work

A SQL query reads inside-out: SELECT comes first but runs last, and every extra step needs another subquery or CTE. A pipeline reads top to bottom, in the order the work happens:

  1. It starts with a source: FROM <table>, FROM (<subquery>), or INTERVAL INTERSECTION OF (...).
  2. Each |> stage takes the rows from the step before and passes new rows to the next.
PERFETTO PRAGMA pipelines = 1;

FROM slice
|> WHERE dur > 0
|> RENAME dur AS duration_ns
|> SELECT id, name, ts, duration_ns;

Saving a pipeline as a table

Run a pipeline as a query on its own, or save it as a PERFETTO TABLE and query that with SQL:

PERFETTO PRAGMA pipelines = 1;

CREATE PERFETTO TABLE named_slices AS
FROM slice
|> WHERE dur > 0
|> SELECT id, ts, dur AS duration_ns, name;

SELECT name, duration_ns
FROM named_slices
ORDER BY duration_ns DESC
LIMIT 10;

Intersecting Time Intervals: INTERVAL INTERSECTION OF

Many questions about a trace ask where two or more sets of intervals [ts, ts + dur) overlap:

  • What CPU frequency was each core running at during every scheduling slice?
  • Which slices were active while a thread was in a particular state?
  • How much power did each task use while the GPU was active?

INTERVAL INTERSECTION OF takes two or more interval sources and gives one row for every region where all of them overlap:

PERFETTO PRAGMA pipelines = 1;

CREATE PERFETTO TABLE running_sched AS
SELECT ts, dur, cpu, utid
FROM sched
WHERE dur > 0;

CREATE PERFETTO TABLE cpu_freq AS
SELECT
  c.ts,
  lead(c.ts) OVER (PARTITION BY t.cpu ORDER BY c.ts) - c.ts AS dur,
  t.cpu,
  c.value AS freq
FROM counter AS c
JOIN cpu_counter_track AS t ON c.track_id = t.id
WHERE t.name = 'cpufreq';

CREATE PERFETTO TABLE sched_with_freq AS
INTERVAL INTERSECTION OF (running_sched AS s, cpu_freq AS f) PER cpu
|> SELECT ts, dur, cpu, utid, freq;
running_sched (s):   [------- utid 10 -------)     [--- utid 20 ---)
cpu_freq (f):        [--- 1.8 GHz ---)[-------- 2.4 GHz --------)
                     -----------------------------------------------
Result regions:      [-- 10 @ 1.8G --)[- 10 -]     [-- 20 @ 2.4G --)
  • ts and dur are the overlapping region, clipped to where every input is active.
  • PER cpu only intersects rows on the same cpu.
  • Input columns carry through. Write them bare, as in utid and freq, or through the input's alias, as in s.utid. If two inputs have a column of the same name, the alias is needed.
  • An input's own bounds are s.ts and s.dur, before clipping.

Flattening Overlapping Intervals: INTERVAL FLATTEN

INTERVAL FLATTEN splits one set of overlapping intervals into segments and aggregates the rows active in each. Use it to count concurrent activity or sum a value across it.

Two tasks run during [0, 10) and [5, 15), with weights 2 and 3:

PERFETTO PRAGMA pipelines = 1;

FROM (
  SELECT 0 AS ts, 10 AS dur, 2 AS weight
  UNION ALL
  SELECT 5 AS ts, 10 AS dur, 3 AS weight
)
|> INTERVAL FLATTEN AGGREGATE COUNT(*) AS active, SUM(weight) AS total_weight;
tsduractivetotal_weight
0512
5525
10513

PER flattens each group on its own. To count overlapping slices on each track:

FROM slice
|> WHERE dur > 0
|> INTERVAL FLATTEN PER track_id AGGREGATE COUNT(*) AS active;

The result has ts, dur, track_id and active; every other column is dropped. Gaps with no activity give no rows.

SUM adds the values of the active rows. It does not weight them by the segment's duration: for that, save the pipeline as a table and sum dur * total_weight in SQL.

See the syntax reference for zero-duration intervals, NULLs and the supported aggregates.


Filling Gaps: INTERVAL FILL GAPS

INTERVAL FILL GAPS keeps every input row and adds a row for each stretch of a background interval which no input covers. A filler row takes its values from the background, and NULL for columns the background does not have.

To mark when each CPU was idle between 0 and 10:

PERFETTO PRAGMA pipelines = 1;

CREATE PERFETTO TABLE spans AS
SELECT 2 AS ts, 3 AS dur, 0 AS cpu, 'run' AS state
UNION ALL SELECT 6, 2, 0, 'run'
UNION ALL SELECT 1, 4, 1, 'run';

FROM spans
|> INTERVAL FILL GAPS WITH (SELECT 0 AS ts, 10 AS dur, 'idle' AS state) PER cpu
|> ORDER BY cpu, ts;
tsdurcpustate
020idle
230run
510idle
620run
820idle
011idle
141run
551idle

Summing Over Trees: TREE ACCUMULATE

Traces hold many trees where each row has an id and a parent_id, and roots have parent_id IS NULL:

  • Nested slices in slice.
  • Callstacks from CPU profiling, perf and native heap profiles.
  • Java heap graph shortest-path trees.

TREE ACCUMULATE sums over such a tree in one stage. It keeps every column and adds the totals at the end:

  • UP sums each node with all its descendants.
  • DOWN sums each node with all its ancestors.
PERFETTO PRAGMA pipelines = 1;

CREATE PERFETTO TABLE tree AS
SELECT 0 AS id, NULL AS parent_id, 'root' AS name, 10 AS self
UNION ALL SELECT 1, 0, 'a', 20
UNION ALL SELECT 2, 0, 'b', 30
UNION ALL SELECT 3, 1, 'c', 40;

FROM tree
|> TREE ACCUMULATE UP SUM(self) AS subtree_total
|> TREE ACCUMULATE DOWN SUM(self) AS path_total
|> ORDER BY id
|> SELECT name, self, subtree_total, path_total;

For the tree root (10) -> a (20) -> c (40) and root (10) -> b (30):

nameselfsubtree_totalpath_total
root1010010
a206030
b303040
c404070

Grouping: AGGREGATE

AGGREGATE collapses each group of rows into one, as GROUP BY does in SQL:

FROM slice
|> WHERE dur > 0
|> AGGREGATE COUNT(*) AS slices, SUM(dur) AS total, MAX(dur) AS longest
   GROUP BY track_id;

The result has track_id, slices, total and longest. Without GROUP BY, every row goes into one group.


Filtering and Sorting Rows

  • |> WHERE keeps the rows that match. Today it supports comparing a column with a number, joined with AND:
    FROM slice
    |> WHERE dur > 0 AND depth = 0
    
  • |> DISTINCT drops repeated rows:
    FROM slice
    |> SELECT track_id
    |> DISTINCT
    
  • |> ORDER BY sorts by numeric columns:
    FROM slice
    |> ORDER BY dur DESC
    

Reshaping Columns

These stages change the columns without wrapping the query in another SELECT:

  • |> SELECT picks, reorders and renames columns. * EXCEPT (...) drops some, and * REPLACE (other_col AS col) swaps one in place:
    FROM slice
    |> SELECT * EXCEPT (arg_set_id, parent_id)
    
  • |> EXTEND adds columns at the end:
    FROM slice AS s
    |> EXTEND s.dur AS original_dur
    
  • |> DROP removes columns:
    FROM slice
    |> DROP category, track_id, arg_set_id
    
  • |> RENAME renames columns in place:
    FROM slice
    |> RENAME ts AS start_ts, dur AS duration_ns
    
  • |> SET replaces a column's values:
    FROM tree
    |> TREE ACCUMULATE UP SUM(self) AS total
    |> SET self = total
    |> DROP total
    

SELECT, EXTEND and SET also take expressions, such as a + 1 or 'label'.


Next Steps