From d3e5227b246b0a4cfb42bff478d58d30b93a666e Mon Sep 17 00:00:00 2001 From: Anna Mayzner Date: Mon, 29 Jul 2024 07:20:46 +0000 Subject: [PATCH] stdlib: Add linux_kernel_threads Change-Id: I53c5ce15508b1a165aa542113ab49117e59e90fd --- Android.bp | 1 + BUILD | 3 + CHANGELOG | 1 + .../perfetto_sql/stdlib/linux/BUILD.gn | 2 +- .../perfetto_sql/stdlib/linux/threads.sql | 59 +++++++++++++++++++ .../diff_tests/include_index.py | 2 + .../diff_tests/stdlib/linux/tests.py | 47 +++++++++++++++ 7 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 src/trace_processor/perfetto_sql/stdlib/linux/threads.sql create mode 100644 test/trace_processor/diff_tests/stdlib/linux/tests.py diff --git a/Android.bp b/Android.bp index 4cc0adff43..5f091a0a9e 100644 --- a/Android.bp +++ b/Android.bp @@ -13341,6 +13341,7 @@ genrule { "src/trace_processor/perfetto_sql/stdlib/linux/memory/high_watermark.sql", "src/trace_processor/perfetto_sql/stdlib/linux/memory/process.sql", "src/trace_processor/perfetto_sql/stdlib/linux/perf/samples.sql", + "src/trace_processor/perfetto_sql/stdlib/linux/threads.sql", "src/trace_processor/perfetto_sql/stdlib/metasql/column_list.sql", "src/trace_processor/perfetto_sql/stdlib/metasql/table_list.sql", "src/trace_processor/perfetto_sql/stdlib/pkvm/hypervisor.sql", diff --git a/BUILD b/BUILD index d0bb252977..de49651fd3 100644 --- a/BUILD +++ b/BUILD @@ -2780,6 +2780,9 @@ perfetto_filegroup( # GN target: //src/trace_processor/perfetto_sql/stdlib/linux:linux perfetto_filegroup( name = "src_trace_processor_perfetto_sql_stdlib_linux_linux", + srcs = [ + "src/trace_processor/perfetto_sql/stdlib/linux/threads.sql", + ], ) # GN target: //src/trace_processor/perfetto_sql/stdlib/metasql:metasql diff --git a/CHANGELOG b/CHANGELOG index e3884768ac..cca571213d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -27,6 +27,7 @@ Unreleased: * Moved `memory.heap_graph_dominator_tree` to `android.memory.heap_graph.dominator_tree`. This is to allow for the addition of more modules related to heap graphs. + * Added `linux_kernel_threads` table to `linux.threads` module. Trace Processor: * Added `CREATE PERFETTO INDEX` to add sqlite-like indexes to Perfetto tables. Has the same API as `CREATE INDEX`. diff --git a/src/trace_processor/perfetto_sql/stdlib/linux/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/linux/BUILD.gn index e0fd42f5f7..23371dfc43 100644 --- a/src/trace_processor/perfetto_sql/stdlib/linux/BUILD.gn +++ b/src/trace_processor/perfetto_sql/stdlib/linux/BUILD.gn @@ -15,7 +15,7 @@ import("../../../../../gn/perfetto_sql.gni") perfetto_sql_source_set("linux") { - sources = [] + sources = [ "threads.sql" ] deps = [ "cpu", "memory", diff --git a/src/trace_processor/perfetto_sql/stdlib/linux/threads.sql b/src/trace_processor/perfetto_sql/stdlib/linux/threads.sql new file mode 100644 index 0000000000..2bcbda2140 --- /dev/null +++ b/src/trace_processor/perfetto_sql/stdlib/linux/threads.sql @@ -0,0 +1,59 @@ +-- +-- 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. + +-- All kernel threads of the trace. As kernel threads are processes, provides +-- also process data. +CREATE PERFETTO TABLE linux_kernel_threads( + -- Upid of kernel thread. Alias of |process.upid|. + upid INT, + -- Utid of kernel thread. Alias of |thread.utid|. + utid INT, + -- Pid of kernel thread. Alias of |process.pid|. + pid INT, + -- Tid of kernel thread. Alias of |process.pid|. + tid INT, + -- Name of kernel process. Alias of |process.name|. + process_name STRING, + -- Name of kernel thread. Alias of |thread.name|. + thread_name STRING, + -- Machine id of kernel thread. If NULL then it's a single machine trace. + -- Alias of |process.machine_id|. + machine_id INT +) AS +WITH pid_2 AS ( + SELECT upid, pid, name, machine_id + FROM process + WHERE pid = 2 +), +parent_pid_2 AS ( + SELECT p.upid, p.pid, p.name, p.machine_id + FROM process p + JOIN pid_2 ON p.parent_upid = pid_2.upid +) +SELECT + upid, + utid, + pid, + tid, + p.name AS process_name, + t.name AS thread_name, + p.machine_id +FROM +( + SELECT * FROM parent_pid_2 + UNION + SELECT * FROM pid_2 +) p +JOIN thread t USING (upid); \ No newline at end of file diff --git a/test/trace_processor/diff_tests/include_index.py b/test/trace_processor/diff_tests/include_index.py index 57705ad85b..d6a1960afc 100644 --- a/test/trace_processor/diff_tests/include_index.py +++ b/test/trace_processor/diff_tests/include_index.py @@ -122,6 +122,7 @@ from diff_tests.stdlib.intervals.tests import StdlibIntervals from diff_tests.stdlib.linux.cpu import LinuxCpu from diff_tests.stdlib.linux.memory import Memory +from diff_tests.stdlib.linux.tests import LinuxTests from diff_tests.stdlib.metasql.column_list import ColumnListTests from diff_tests.stdlib.metasql.table_list import TableListTests from diff_tests.stdlib.pkvm.tests import Pkvm @@ -282,6 +283,7 @@ def fetch_all_diff_tests(index_path: str) -> List['testing.TestCase']: *AndroidStdlib(index_path, 'stdlib/android', 'AndroidStdlib').fetch(), *CpuClusters(index_path, 'stdlib/android', 'CpuClusters').fetch(), *LinuxCpu(index_path, 'stdlib/linux/cpu', 'LinuxCpu').fetch(), + *LinuxTests(index_path, 'stdlib/linux', 'LinuxTests').fetch(), *DominatorTree(index_path, 'stdlib/graphs', 'DominatorTree').fetch(), *CriticalPathTests(index_path, 'stdlib/graphs', 'CriticalPath').fetch(), *GraphScanTests(index_path, 'stdlib/graphs', 'GraphScan').fetch(), diff --git a/test/trace_processor/diff_tests/stdlib/linux/tests.py b/test/trace_processor/diff_tests/stdlib/linux/tests.py new file mode 100644 index 0000000000..5be22eb82c --- /dev/null +++ b/test/trace_processor/diff_tests/stdlib/linux/tests.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# Copyright (C) 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 a +# +# http://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. + +from python.generators.diff_tests.testing import Path, DataPath, Metric, Systrace +from python.generators.diff_tests.testing import Csv, Json, TextProto, BinaryProto +from python.generators.diff_tests.testing import DiffTestBlueprint +from python.generators.diff_tests.testing import TestSuite +from python.generators.diff_tests.testing import PrintProfileProto + + +class LinuxTests(TestSuite): + + def test_kernel_threads(self): + return DiffTestBlueprint( + trace=DataPath('android_postboot_unlock.pftrace'), + query=""" + INCLUDE PERFETTO MODULE linux.threads; + + SELECT upid, utid, pid, tid, process_name, thread_name + FROM linux_kernel_threads + ORDER by utid LIMIT 10; + """, + out=Csv(""" + "upid","utid","pid","tid","process_name","thread_name" + 7,14,510,510,"sugov:0","sugov:0" + 89,23,1365,1365,"com.google.usf.","com.google.usf." + 87,37,1249,1249,"irq/357-dwc3","irq/357-dwc3" + 31,38,6,6,"kworker/u16:0","kworker/u16:0" + 11,42,511,511,"sugov:4","sugov:4" + 83,43,1152,1152,"irq/502-fts_ts","irq/502-fts_ts" + 93,44,2374,2374,"csf_sync_update","csf_sync_update" + 18,45,2379,2379,"csf_kcpu_0","csf_kcpu_0" + 12,47,247,247,"decon0_kthread","decon0_kthread" + 65,48,159,159,"spi0","spi0" + """)) \ No newline at end of file