Merge "tp: Enable DISTINCT in-house DISTINCT in Perfetto" into main
diff --git a/Android.bp b/Android.bp
index 73b4ae5..4fe5a6a 100644
--- a/Android.bp
+++ b/Android.bp
@@ -5237,6 +5237,7 @@
"protos/perfetto/metrics/android/android_trusty_workqueues.proto",
"protos/perfetto/metrics/android/anr_metric.proto",
"protos/perfetto/metrics/android/app_process_starts_metric.proto",
+ "protos/perfetto/metrics/android/auto_metric.proto",
"protos/perfetto/metrics/android/batt_metric.proto",
"protos/perfetto/metrics/android/binder_metric.proto",
"protos/perfetto/metrics/android/camera_metric.proto",
@@ -5328,6 +5329,7 @@
"protos/perfetto/metrics/android/android_trusty_workqueues.proto",
"protos/perfetto/metrics/android/anr_metric.proto",
"protos/perfetto/metrics/android/app_process_starts_metric.proto",
+ "protos/perfetto/metrics/android/auto_metric.proto",
"protos/perfetto/metrics/android/batt_metric.proto",
"protos/perfetto/metrics/android/binder_metric.proto",
"protos/perfetto/metrics/android/camera_metric.proto",
@@ -5403,6 +5405,7 @@
"protos/perfetto/metrics/android/android_trusty_workqueues.proto",
"protos/perfetto/metrics/android/anr_metric.proto",
"protos/perfetto/metrics/android/app_process_starts_metric.proto",
+ "protos/perfetto/metrics/android/auto_metric.proto",
"protos/perfetto/metrics/android/batt_metric.proto",
"protos/perfetto/metrics/android/binder_metric.proto",
"protos/perfetto/metrics/android/camera_metric.proto",
@@ -12961,6 +12964,7 @@
srcs: [
"src/trace_processor/perfetto_sql/stdlib/android/anrs.sql",
"src/trace_processor/perfetto_sql/stdlib/android/app_process_starts.sql",
+ "src/trace_processor/perfetto_sql/stdlib/android/auto/multiuser.sql",
"src/trace_processor/perfetto_sql/stdlib/android/battery.sql",
"src/trace_processor/perfetto_sql/stdlib/android/battery_stats.sql",
"src/trace_processor/perfetto_sql/stdlib/android/binder.sql",
@@ -13463,6 +13467,7 @@
"src/trace_redaction/filter_sched_waking_events.cc",
"src/trace_redaction/filter_task_rename.cc",
"src/trace_redaction/find_package_uid.cc",
+ "src/trace_redaction/modify_process_trees.cc",
"src/trace_redaction/optimize_timeline.cc",
"src/trace_redaction/populate_allow_lists.cc",
"src/trace_redaction/process_thread_timeline.cc",
diff --git a/BUILD b/BUILD
index 2f53ea5..5d1e5be 100644
--- a/BUILD
+++ b/BUILD
@@ -2412,6 +2412,14 @@
],
)
+# GN target: //src/trace_processor/perfetto_sql/stdlib/android/auto:auto
+perfetto_filegroup(
+ name = "src_trace_processor_perfetto_sql_stdlib_android_auto_auto",
+ srcs = [
+ "src/trace_processor/perfetto_sql/stdlib/android/auto/multiuser.sql",
+ ],
+)
+
# GN target: //src/trace_processor/perfetto_sql/stdlib/android/frames:frames
perfetto_filegroup(
name = "src_trace_processor_perfetto_sql_stdlib_android_frames_frames",
@@ -2662,6 +2670,7 @@
name = "src_trace_processor_perfetto_sql_stdlib_stdlib",
deps = [
":src_trace_processor_perfetto_sql_stdlib_android_android",
+ ":src_trace_processor_perfetto_sql_stdlib_android_auto_auto",
":src_trace_processor_perfetto_sql_stdlib_android_frames_frames",
":src_trace_processor_perfetto_sql_stdlib_android_startup_startup",
":src_trace_processor_perfetto_sql_stdlib_chrome_chrome_sql",
@@ -4462,6 +4471,7 @@
"protos/perfetto/metrics/android/android_trusty_workqueues.proto",
"protos/perfetto/metrics/android/anr_metric.proto",
"protos/perfetto/metrics/android/app_process_starts_metric.proto",
+ "protos/perfetto/metrics/android/auto_metric.proto",
"protos/perfetto/metrics/android/batt_metric.proto",
"protos/perfetto/metrics/android/binder_metric.proto",
"protos/perfetto/metrics/android/camera_metric.proto",
diff --git a/include/perfetto/ext/base/flat_hash_map.h b/include/perfetto/ext/base/flat_hash_map.h
index 0be192d..f4046ea 100644
--- a/include/perfetto/ext/base/flat_hash_map.h
+++ b/include/perfetto/ext/base/flat_hash_map.h
@@ -23,7 +23,6 @@
#include "perfetto/ext/base/utils.h"
#include <algorithm>
-#include <functional>
#include <limits>
namespace perfetto {
@@ -45,6 +44,11 @@
// tsl::robin_map: 931,403,397 ns 243.622M insertions/s
// absl::flat_hash_map: 998,013,459 ns 227.379M insertions/s
// FollyF14FastMap: 1,181,480,602 ns 192.074M insertions/s
+//
+// TODO(primiano): the table regresses for heavy insert+erase workloads since we
+// don't clean up tombstones outside of resizes. In the limit, the entire
+// table's capacity is made up of values/tombstones, so each search has to
+// exhaustively scan the full capacity.
// The structs below define the probing algorithm used to probe slots upon a
// collision. They are guaranteed to visit all slots as our table size is always
diff --git a/include/perfetto/ext/base/hash.h b/include/perfetto/ext/base/hash.h
index 548003e..7809ed7 100644
--- a/include/perfetto/ext/base/hash.h
+++ b/include/perfetto/ext/base/hash.h
@@ -62,7 +62,7 @@
// Allow hashing anything that has a |data| field, a |size| field,
// and has the kHashable trait (e.g., base::StringView).
- template <typename T, typename = std::enable_if<T::kHashable>>
+ template <typename T, typename = std::enable_if_t<T::kHashable>>
void Update(const T& t) {
Update(t.data(), t.size());
}
diff --git a/protos/perfetto/metrics/android/BUILD.gn b/protos/perfetto/metrics/android/BUILD.gn
index 2bb8c0e..af7aa8f 100644
--- a/protos/perfetto/metrics/android/BUILD.gn
+++ b/protos/perfetto/metrics/android/BUILD.gn
@@ -33,6 +33,7 @@
"android_trusty_workqueues.proto",
"anr_metric.proto",
"app_process_starts_metric.proto",
+ "auto_metric.proto",
"batt_metric.proto",
"binder_metric.proto",
"camera_metric.proto",
diff --git a/protos/perfetto/metrics/android/auto_metric.proto b/protos/perfetto/metrics/android/auto_metric.proto
new file mode 100644
index 0000000..d7043fe
--- /dev/null
+++ b/protos/perfetto/metrics/android/auto_metric.proto
@@ -0,0 +1,51 @@
+/*
+ * 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 at
+ *
+ * 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.
+ */
+
+syntax = "proto2";
+
+package perfetto.protos;
+
+// Metrics for Multiuser events, such as switching users.
+message AndroidAutoMultiuserMetric {
+ message EventData {
+ // Id of the user the system has been switched to
+ optional int32 user_id = 1;
+
+ // The end event name for which the duration_ms is measured
+ optional string start_event = 2;
+
+ // The end event name for which the duration_ms is measured
+ optional string end_event = 3;
+
+ // Duration of the event (in milliseconds).
+ optional int64 duration_ms = 4;
+
+ // Previous user resource usage info during user switch
+ message UserData {
+ // Id of the user the system has been switched from
+ optional int32 user_id = 1;
+
+ optional int64 total_cpu_time_ms = 2;
+
+ optional int64 total_memory_usage_kb = 3;
+ }
+
+ optional UserData previous_user_info = 5;
+ }
+
+ // Metrics for a user switch.
+ repeated EventData user_switch = 1;
+}
diff --git a/protos/perfetto/metrics/metrics.proto b/protos/perfetto/metrics/metrics.proto
index e5a9553..4348187 100644
--- a/protos/perfetto/metrics/metrics.proto
+++ b/protos/perfetto/metrics/metrics.proto
@@ -22,6 +22,7 @@
import "protos/perfetto/metrics/android/android_boot.proto";
import "protos/perfetto/metrics/android/android_boot_unagg.proto";
import "protos/perfetto/metrics/android/android_garbage_collection_unagg_metric.proto";
+import "protos/perfetto/metrics/android/auto_metric.proto";
import "protos/perfetto/metrics/android/sysui_notif_shade_list_builder_metric.proto";
import "protos/perfetto/metrics/android/sysui_update_notif_on_ui_mode_changed_metric.proto";
import "protos/perfetto/metrics/android/android_frame_timeline_metric.proto";
@@ -298,7 +299,7 @@
// Multiuser - metrics for switching users.
// Specific for Android Auto
- optional AndroidMultiuserMetric android_auto_multiuser = 64;
+ optional AndroidAutoMultiuserMetric android_auto_multiuser = 64;
// All blocking calls (e.g. binder calls) for a trace.
optional AndroidBlockingCallsUnagg android_blocking_calls_unagg = 65;
diff --git a/protos/perfetto/metrics/perfetto_merged_metrics.proto b/protos/perfetto/metrics/perfetto_merged_metrics.proto
index a4da778..8f736b3 100644
--- a/protos/perfetto/metrics/perfetto_merged_metrics.proto
+++ b/protos/perfetto/metrics/perfetto_merged_metrics.proto
@@ -474,6 +474,42 @@
}
// End of protos/perfetto/metrics/android/anr_metric.proto
+// Begin of protos/perfetto/metrics/android/auto_metric.proto
+
+// Metrics for Multiuser events, such as switching users.
+message AndroidAutoMultiuserMetric {
+ message EventData {
+ // Id of the user the system has been switched to
+ optional int32 user_id = 1;
+
+ // The end event name for which the duration_ms is measured
+ optional string start_event = 2;
+
+ // The end event name for which the duration_ms is measured
+ optional string end_event = 3;
+
+ // Duration of the event (in milliseconds).
+ optional int64 duration_ms = 4;
+
+ // Previous user resource usage info during user switch
+ message UserData {
+ // Id of the user the system has been switched from
+ optional int32 user_id = 1;
+
+ optional int64 total_cpu_time_ms = 2;
+
+ optional int64 total_memory_usage_kb = 3;
+ }
+
+ optional UserData previous_user_info = 5;
+ }
+
+ // Metrics for a user switch.
+ repeated EventData user_switch = 1;
+}
+
+// End of protos/perfetto/metrics/android/auto_metric.proto
+
// Begin of protos/perfetto/metrics/android/batt_metric.proto
message AndroidBatteryMetric {
@@ -2822,7 +2858,7 @@
// Multiuser - metrics for switching users.
// Specific for Android Auto
- optional AndroidMultiuserMetric android_auto_multiuser = 64;
+ optional AndroidAutoMultiuserMetric android_auto_multiuser = 64;
// All blocking calls (e.g. binder calls) for a trace.
optional AndroidBlockingCallsUnagg android_blocking_calls_unagg = 65;
diff --git a/python/perfetto/trace_processor/metrics.descriptor b/python/perfetto/trace_processor/metrics.descriptor
index 59b0f2a..9c1dfa0 100644
--- a/python/perfetto/trace_processor/metrics.descriptor
+++ b/python/perfetto/trace_processor/metrics.descriptor
Binary files differ
diff --git a/src/trace_processor/importers/ftrace/binder_tracker.cc b/src/trace_processor/importers/ftrace/binder_tracker.cc
index 29d0c2c..48cdf58 100644
--- a/src/trace_processor/importers/ftrace/binder_tracker.cc
+++ b/src/trace_processor/importers/ftrace/binder_tracker.cc
@@ -296,7 +296,7 @@
transaction.args_inserter = args_inserter;
transaction.send_track_id = track_id;
transaction.send_slice_id = insert_slice();
- outstanding_transactions_.Insert(transaction_id, std::move(transaction));
+ outstanding_transactions_[transaction_id] = std::move(transaction);
auto* frame = GetTidTopFrame(tid);
if (frame) {
if (frame->state == TxnFrame::kSndAfterBC_TRANSACTION) {
@@ -315,18 +315,16 @@
void BinderTracker::TransactionReceived(int64_t ts,
uint32_t pid,
int32_t transaction_id) {
- const OutstandingTransaction* opt_transaction =
- outstanding_transactions_.Find(transaction_id);
- if (!opt_transaction) {
+ auto it = outstanding_transactions_.find(transaction_id);
+ if (it == outstanding_transactions_.end()) {
// If we don't know what type of transaction it is, we don't know how to
// insert the slice.
// TODO(lalitm): maybe we should insert a dummy slice anyway - seems like
// a questionable idea to just ignore these completely.
return;
}
-
- OutstandingTransaction transaction(std::move(*opt_transaction));
- outstanding_transactions_.Erase(transaction_id);
+ OutstandingTransaction transaction(std::move(it->second));
+ outstanding_transactions_.erase(it);
UniqueTid utid = context_->process_tracker->GetOrCreateThread(pid);
TrackId track_id = context_->track_tracker->InternThreadTrack(utid);
diff --git a/src/trace_processor/importers/ftrace/binder_tracker.h b/src/trace_processor/importers/ftrace/binder_tracker.h
index f1d682c..5912904 100644
--- a/src/trace_processor/importers/ftrace/binder_tracker.h
+++ b/src/trace_processor/importers/ftrace/binder_tracker.h
@@ -95,6 +95,8 @@
bool utid_stacks_empty() const { return utid_stacks_.size() == 0; }
private:
+ TraceProcessorContext* const context_;
+
struct OutstandingTransaction {
bool is_reply = false;
bool is_oneway = false;
@@ -102,10 +104,10 @@
std::optional<TrackId> send_track_id;
std::optional<SliceId> send_slice_id;
};
+ // TODO(rsavitski): switch back to FlatHashMap once the latter's perf is fixed
+ // for insert+erase heavy workfloads.
+ std::unordered_map<int32_t, OutstandingTransaction> outstanding_transactions_;
- TraceProcessorContext* const context_;
-
- base::FlatHashMap<int32_t, OutstandingTransaction> outstanding_transactions_;
struct TxnFrame {
// The state of this thread at this stack level.
enum State : uint32_t;
diff --git a/src/trace_processor/metrics/sql/android/android_auto_multiuser.sql b/src/trace_processor/metrics/sql/android/android_auto_multiuser.sql
index 3b12305..2be5ef0 100644
--- a/src/trace_processor/metrics/sql/android/android_auto_multiuser.sql
+++ b/src/trace_processor/metrics/sql/android/android_auto_multiuser.sql
@@ -14,51 +14,26 @@
-- limitations under the License.
--
-INCLUDE PERFETTO MODULE android.startup.startups;
-
--- Collect the last ts for user switch event.
--- The metric should represent time elapsed between
--- the latest user start and the latest carlauncher startup.
-DROP VIEW IF EXISTS auto_multiuser_events;
-CREATE PERFETTO VIEW auto_multiuser_events AS
-SELECT
- user_start_time_ns AS event_start_time_ns,
- launcher_end_time_ns AS event_end_time_ns
-FROM
- (
- SELECT MAX(slice.ts) as user_start_time_ns
- FROM slice
- WHERE (
- slice.name GLOB "UserController.startUser*"
- AND slice.name NOT GLOB "UserController.startUser-10*"
- )
- ),
- (
- SELECT ts_end AS launcher_end_time_ns
- FROM android_startups
- WHERE (package = 'com.android.car.carlauncher')
- );
-
--- Precompute user switch duration time.
--- Take only positive duration values(user start ts < carlauncher start ts)
--- If there are potential duplicates in carlauncher startup,
--- take the smallest value. It represents the closest carlaucnher startup
-DROP TABLE IF EXISTS android_auto_multiuser_timing;
-CREATE PERFETTO TABLE android_auto_multiuser_timing AS
-SELECT
- cast_int!((event_end_time_ns - event_start_time_ns) / 1e6 + 0.5) as duration_ms
-FROM
- auto_multiuser_events
-WHERE duration_ms > 0
-ORDER BY duration_ms ASC
-LIMIT 1;
+INCLUDE PERFETTO MODULE android.auto.multiuser;
+INCLUDE PERFETTO MODULE time.conversion;
DROP VIEW IF EXISTS android_auto_multiuser_output;
CREATE PERFETTO VIEW android_auto_multiuser_output AS
-SELECT AndroidMultiuserMetric (
- 'user_switch', AndroidMultiuserMetric_EventData(
- 'duration_ms', (
- SELECT duration_ms FROM android_auto_multiuser_timing
+SELECT AndroidAutoMultiuserMetric(
+ 'user_switch', (
+ SELECT RepeatedField(
+ AndroidAutoMultiuserMetric_EventData(
+ 'user_id', cast_int!(event_start_user_id),
+ 'start_event', event_start_name,
+ 'end_event', event_end_name,
+ 'duration_ms', time_to_ms(duration),
+ 'previous_user_info', AndroidAutoMultiuserMetric_EventData_UserData(
+ 'user_id', user_id,
+ 'total_cpu_time_ms', time_to_ms(total_cpu_time),
+ 'total_memory_usage_kb', total_memory_usage_kb
+ )
+ )
)
+ FROM android_auto_multiuser_timing_with_previous_user_resource_usage
)
);
\ No newline at end of file
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
index 786882e..bd2f18d 100644
--- a/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
+++ b/src/trace_processor/perfetto_sql/stdlib/android/BUILD.gn
@@ -16,6 +16,7 @@
perfetto_sql_source_set("android") {
deps = [
+ "auto",
"frames",
"startup",
]
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/auto/BUILD.gn b/src/trace_processor/perfetto_sql/stdlib/android/auto/BUILD.gn
new file mode 100644
index 0000000..bf52d69
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/android/auto/BUILD.gn
@@ -0,0 +1,21 @@
+# 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 at
+#
+# 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.
+
+import("../../../../../../gn/perfetto_sql.gni")
+
+perfetto_sql_source_set("auto") {
+ sources = [
+ "multiuser.sql",
+ ]
+}
diff --git a/src/trace_processor/perfetto_sql/stdlib/android/auto/multiuser.sql b/src/trace_processor/perfetto_sql/stdlib/android/auto/multiuser.sql
new file mode 100644
index 0000000..ea95dcc
--- /dev/null
+++ b/src/trace_processor/perfetto_sql/stdlib/android/auto/multiuser.sql
@@ -0,0 +1,173 @@
+--
+-- 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 android.startup.startups;
+
+-- Time elapsed between the latest user start
+-- and the specific end event
+-- like package startup(ex carlauncher) or previous user stop.
+CREATE PERFETTO TABLE android_auto_multiuser_timing(
+ -- Id of the started android user
+ event_start_user_id STRING,
+ -- Start event time
+ event_start_time INT,
+ -- End event time
+ event_end_time INT,
+ -- End event name
+ event_end_name STRING,
+ -- Start event name
+ event_start_name STRING,
+ -- User switch duration from start event
+ -- to end event
+ duration LONG
+) AS
+-- The last ts for user switch event.
+WITH auto_multiuser_user_start AS (
+ SELECT
+ slice.name as event_start_name,
+ slice.ts AS user_start_time,
+ -- Ex.: UserController.startUser-11-fg-start-mode-1
+ -- User is enclosed in dashes and will be at most 2 characters(10, 11, etc.)
+ SUBSTR(name, INSTR(name, '-') + 1, 2) as started_user_id
+ FROM slice
+ WHERE (
+ slice.name GLOB "UserController.startUser*"
+ AND slice.name NOT GLOB "UserController.startUser-10*"
+ )
+ ORDER BY ts DESC
+ LIMIT 1
+)
+SELECT
+ started_user_id AS event_start_user_id,
+ user_start_time AS event_start_time,
+ event_end_time,
+ event_end_name,
+ event_start_name,
+ (event_end_time - user_start_time) as duration
+FROM (
+ SELECT
+ ts_end AS event_end_time,
+ package as event_end_name
+ FROM android_startups
+ UNION
+ SELECT
+ slice.ts as event_end_time,
+ slice.name as event_end_name
+ FROM slice
+ WHERE slice.name GLOB "finishUserStopped-10*"
+) as a
+JOIN auto_multiuser_user_start as b
+ON a.event_end_time > b.user_start_time;
+
+-- Previous user(user 10) total CPU time
+CREATE PERFETTO VIEW _android_auto_user_10_total_cpu_time AS
+SELECT
+ SUM(dur) as total_cpu_time,
+ (uid - android_appid) / 100000 as user_id,
+ event_end_name
+FROM sched_slice
+ JOIN thread USING (utid)
+ JOIN process USING (upid),
+ android_auto_multiuser_timing
+WHERE
+ user_id = 10
+ AND ts >= android_auto_multiuser_timing.event_start_time
+ AND ts <= android_auto_multiuser_timing.event_end_time
+GROUP BY event_end_name;
+
+-- Previous user(user 10) total memory usage
+CREATE PERFETTO VIEW _android_auto_user_10_total_memory AS
+WITH filtered_process AS (
+ SELECT
+ c.ts,
+ c.value,
+ p.name AS proc_name,
+ (uid - android_appid) / 100000 as user_id,
+ event_end_name
+ FROM counter AS c
+ LEFT JOIN process_counter_track AS t
+ ON c.track_id = t.id
+ LEFT JOIN process AS p USING (upid),
+ android_auto_multiuser_timing
+ WHERE
+ t.name GLOB "mem.rss"
+ AND user_id = 10
+ AND c.ts >= android_auto_multiuser_timing.event_start_time
+ AND c.ts <= android_auto_multiuser_timing.event_end_time
+),
+process_rss AS (
+ SELECT
+ *,
+ ifnull(
+ lag(value) OVER (PARTITION BY proc_name, event_end_name ORDER BY ts), value
+ ) AS prev_value
+ FROM filtered_process
+),
+per_process_allocations AS (
+ SELECT
+ proc_name,
+ SUM(value - prev_value) / 1e3 AS alloc_value_kb,
+ user_id,
+ event_end_name
+ FROM process_rss
+ WHERE value - prev_value > 0
+ GROUP BY proc_name, event_end_name
+ ORDER BY alloc_value_kb DESC
+)
+SELECT
+ cast_int!(SUM(alloc_value_kb)) AS total_memory_usage_kb,
+ user_id,
+ event_end_name
+FROM per_process_allocations
+GROUP BY event_end_name;
+
+-- This table extends `android_auto_multiuser_timing` table with previous user resource usage.
+CREATE PERFETTO VIEW android_auto_multiuser_timing_with_previous_user_resource_usage(
+ -- Start user id
+ event_start_user_id STRING,
+ -- Start event time
+ event_start_time INT,
+ -- End event time
+ event_end_time INT,
+ -- End event name
+ event_end_name STRING,
+ -- Start event name
+ event_start_name STRING,
+ -- User switch duration from start event
+ -- to end event
+ duration LONG,
+ -- User id
+ user_id INT,
+ -- Total CPU time for a user
+ total_cpu_time LONG,
+ -- Total memory user for a user
+ total_memory_usage_kb LONG
+) AS
+SELECT
+ a.event_start_user_id,
+ a.event_start_time,
+ a.event_end_time,
+ a.event_end_name,
+ a.event_start_name,
+ a.duration,
+ b.user_id,
+ b.total_cpu_time,
+ c.total_memory_usage_kb
+FROM android_auto_multiuser_timing as a
+LEFT JOIN _android_auto_user_10_total_cpu_time as b
+ ON a.event_end_name = b.event_end_name
+LEFT JOIN _android_auto_user_10_total_memory as c
+ ON a.event_end_name = c.event_end_name;
\ No newline at end of file
diff --git a/src/trace_redaction/BUILD.gn b/src/trace_redaction/BUILD.gn
index 037545d..8ff31a3 100644
--- a/src/trace_redaction/BUILD.gn
+++ b/src/trace_redaction/BUILD.gn
@@ -47,6 +47,8 @@
"find_package_uid.cc",
"find_package_uid.h",
"frame_cookie.h",
+ "modify_process_trees.cc",
+ "modify_process_trees.h",
"optimize_timeline.cc",
"optimize_timeline.h",
"populate_allow_lists.cc",
diff --git a/src/trace_redaction/modify_process_trees.cc b/src/trace_redaction/modify_process_trees.cc
new file mode 100644
index 0000000..099890b
--- /dev/null
+++ b/src/trace_redaction/modify_process_trees.cc
@@ -0,0 +1,114 @@
+/*
+ * 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 at
+ *
+ * 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.
+ */
+
+#include "src/trace_redaction/modify_process_trees.h"
+
+#include <string>
+
+#include "perfetto/base/status.h"
+#include "perfetto/protozero/field.h"
+#include "perfetto/protozero/scattered_heap_buffer.h"
+#include "src/trace_redaction/proto_util.h"
+#include "src/trace_redaction/trace_redaction_framework.h"
+
+#include "protos/perfetto/trace/ps/process_tree.pbzero.h"
+#include "protos/perfetto/trace/trace_packet.pbzero.h"
+
+namespace perfetto::trace_redaction {
+
+base::Status ModifyProcessTree::VerifyContext(const Context&) const {
+ return base::OkStatus();
+}
+
+base::Status ModifyProcessTree::Transform(const Context& context,
+ std::string* packet) const {
+ protozero::ProtoDecoder decoder(*packet);
+
+ auto process_tree =
+ decoder.FindField(protos::pbzero::TracePacket::kProcessTreeFieldNumber);
+
+ if (!process_tree.valid()) {
+ return base::OkStatus();
+ }
+
+ auto timestamp =
+ decoder.FindField(protos::pbzero::TracePacket::kTimestampFieldNumber);
+
+ protozero::HeapBuffered<protos::pbzero::TracePacket> packet_message;
+
+ for (auto field = decoder.ReadField(); field.valid();
+ field = decoder.ReadField()) {
+ if (field.id() == protos::pbzero::TracePacket::kProcessTreeFieldNumber) {
+ TransformProcessTree(context, timestamp, field,
+ packet_message->set_process_tree());
+ } else {
+ proto_util::AppendField(field, packet_message.get());
+ }
+ }
+
+ packet->assign(packet_message.SerializeAsString());
+
+ return base::OkStatus();
+}
+
+void ModifyProcessTree::TransformProcess(
+ const Context&,
+ const protozero::Field&,
+ const protozero::Field& process,
+ protos::pbzero::ProcessTree* process_tree) const {
+ PERFETTO_DCHECK(process.id() ==
+ protos::pbzero::ProcessTree::kProcessesFieldNumber);
+ proto_util::AppendField(process, process_tree);
+}
+
+void ModifyProcessTree::TransformThread(
+ const Context&,
+ const protozero::Field&,
+ const protozero::Field& thread,
+ protos::pbzero::ProcessTree* process_tree) const {
+ PERFETTO_DCHECK(thread.id() ==
+ protos::pbzero::ProcessTree::kThreadsFieldNumber);
+ proto_util::AppendField(thread, process_tree);
+}
+
+void ModifyProcessTree::TransformProcessTree(
+ const Context& context,
+ const protozero::Field& timestamp,
+ const protozero::Field& process_tree,
+ protos::pbzero::ProcessTree* message) const {
+ protozero::ProtoDecoder decoder(process_tree.as_bytes());
+
+ for (auto field = decoder.ReadField(); field.valid();
+ field = decoder.ReadField()) {
+ switch (field.id()) {
+ case protos::pbzero::ProcessTree::kProcessesFieldNumber:
+ TransformProcess(context, timestamp, field, message);
+ break;
+
+ case protos::pbzero::ProcessTree::kThreadsFieldNumber:
+ TransformThread(context, timestamp, field, message);
+ break;
+
+ default:
+ proto_util::AppendField(field, message);
+ break;
+ }
+ }
+
+ // TODO(vaage): Call the handler to add extra fields to the process tree.
+}
+
+} // namespace perfetto::trace_redaction
diff --git a/src/trace_redaction/modify_process_trees.h b/src/trace_redaction/modify_process_trees.h
new file mode 100644
index 0000000..e36223e
--- /dev/null
+++ b/src/trace_redaction/modify_process_trees.h
@@ -0,0 +1,70 @@
+/*
+ * 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 at
+ *
+ * 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.
+ */
+
+#ifndef SRC_TRACE_REDACTION_MODIFY_PROCESS_TREES_H_
+#define SRC_TRACE_REDACTION_MODIFY_PROCESS_TREES_H_
+
+#include <string>
+
+#include "perfetto/base/status.h"
+#include "src/trace_redaction/trace_redaction_framework.h"
+
+#include "protos/perfetto/trace/ps/process_tree.pbzero.h"
+
+namespace perfetto::trace_redaction {
+
+// Walk through process trees, calling process and thread handlers to add new
+// process and threads messages to the process tree. If the default handler is
+// not replaced, the thread/process will be added to the parent.
+class ModifyProcessTree : public TransformPrimitive {
+ public:
+ base::Status Transform(const Context& context,
+ std::string* packet) const override;
+
+ protected:
+ // Verifies that the context contains required values. No-op by default.
+ virtual base::Status VerifyContext(const Context& context) const;
+
+ // Modifies a process before adding it back to the process tree. Appends the
+ // field to the process tree without modification by default.
+ virtual void TransformProcess(
+ const Context& context,
+ const protozero::Field& timestamp,
+ const protozero::Field& process,
+ protos::pbzero::ProcessTree* process_tree) const;
+
+ // Modifies a thread before adding it back to the process tree. Appends the
+ // field to the process tree without modification by default.
+ virtual void TransformThread(
+ const Context& context,
+ const protozero::Field& timestamp,
+ const protozero::Field& thread,
+ protos::pbzero::ProcessTree* process_trees) const;
+
+ // TODO(vaage): Add a handler that is called the process tree is populated so
+ // that fields can be added to process tree (e.g. creating new threads -
+ // needed for thread merging).
+
+ private:
+ void TransformProcessTree(const Context& context,
+ const protozero::Field& timestamp,
+ const protozero::Field& process_tree,
+ protos::pbzero::ProcessTree* message) const;
+};
+
+} // namespace perfetto::trace_redaction
+
+#endif // SRC_TRACE_REDACTION_MODIFY_PROCESS_TREES_H_
diff --git a/src/trace_redaction/populate_allow_lists.cc b/src/trace_redaction/populate_allow_lists.cc
index b2c9431..11d895b 100644
--- a/src/trace_redaction/populate_allow_lists.cc
+++ b/src/trace_redaction/populate_allow_lists.cc
@@ -84,24 +84,38 @@
context->trace_packet_allow_list.insert(item);
}
+ // FTRACE EVENT NOTES
+ //
+ // Dma events (kDmaHeapStatFieldNumber) are global events and are not
+ // emitted within a process context (they are centrally allocated by the
+ // HAL process). We drop them for now as we don't have the required
+ // attribution info in the trace.
+ //
+ // ION events (e.g. kIonBufferCreateFieldNumber, kIonHeapGrowFieldNumber,
+ // etc.) are global events are not emitted within a process context (they
+ // are centrally allocated by the HAL process). We drop them for now as we
+ // don't have the required attribution info in the trace.
+ //
+ // TODO(vaage): The allowed rss stat events (i.e. kRssStatFieldNumber,
+ // kRssStatThrottledFieldNumber) are process-scoped. It is non-trivial to
+ // merge events, so all events outside of the target package should be
+ // dropped.
+ //
+ // TODO(vaage): kSchedBlockedReasonFieldNumber contains two pids, an outer
+ // and inner pid. A primitive is needed to further redact these events.
+
std::initializer_list<uint32_t> ftrace_events = {
- protos::pbzero::FtraceEvent::kSchedSwitchFieldNumber,
protos::pbzero::FtraceEvent::kCpuFrequencyFieldNumber,
protos::pbzero::FtraceEvent::kCpuIdleFieldNumber,
+ protos::pbzero::FtraceEvent::kPrintFieldNumber,
+ protos::pbzero::FtraceEvent::kRssStatFieldNumber,
+ protos::pbzero::FtraceEvent::kRssStatThrottledFieldNumber,
protos::pbzero::FtraceEvent::kSchedBlockedReasonFieldNumber,
+ protos::pbzero::FtraceEvent::kSchedProcessFreeFieldNumber,
+ protos::pbzero::FtraceEvent::kSchedSwitchFieldNumber,
protos::pbzero::FtraceEvent::kSchedWakingFieldNumber,
protos::pbzero::FtraceEvent::kTaskNewtaskFieldNumber,
protos::pbzero::FtraceEvent::kTaskRenameFieldNumber,
- protos::pbzero::FtraceEvent::kSchedProcessFreeFieldNumber,
- protos::pbzero::FtraceEvent::kRssStatFieldNumber,
- protos::pbzero::FtraceEvent::kIonHeapShrinkFieldNumber,
- protos::pbzero::FtraceEvent::kIonHeapGrowFieldNumber,
- protos::pbzero::FtraceEvent::kIonStatFieldNumber,
- protos::pbzero::FtraceEvent::kIonBufferCreateFieldNumber,
- protos::pbzero::FtraceEvent::kIonBufferDestroyFieldNumber,
- protos::pbzero::FtraceEvent::kDmaHeapStatFieldNumber,
- protos::pbzero::FtraceEvent::kRssStatThrottledFieldNumber,
- protos::pbzero::FtraceEvent::kPrintFieldNumber,
};
for (auto item : ftrace_events) {
diff --git a/src/trace_redaction/redact_ftrace_event.cc b/src/trace_redaction/redact_ftrace_event.cc
index 6c09236..a16a90d 100644
--- a/src/trace_redaction/redact_ftrace_event.cc
+++ b/src/trace_redaction/redact_ftrace_event.cc
@@ -30,68 +30,76 @@
base::Status RedactFtraceEvent::Transform(const Context& context,
std::string* packet) const {
- protozero::ConstBytes packet_bytes = {
- reinterpret_cast<const uint8_t*>(packet->data()), packet->size()};
-
protozero::HeapBuffered<protos::pbzero::TracePacket> message;
- RedactPacket(context, packet_bytes, message.get());
+ protozero::ProtoDecoder decoder(*packet);
+
+ // Treat FtraceEvents (bundle) as a special case.
+ for (auto f = decoder.ReadField(); f.valid(); f = decoder.ReadField()) {
+ if (f.id() == protos::pbzero::TracePacket::kFtraceEventsFieldNumber) {
+ RedactEvents(context, f, message->set_ftrace_events());
+ } else {
+ proto_util::AppendField(f, message.get());
+ }
+ }
+
packet->assign(message.SerializeAsString());
return base::OkStatus();
}
-// Iterate over every field in a packet, treating FtraceEvents (bundle) as a
-// special case.
-void RedactFtraceEvent::RedactPacket(
- const Context& context,
- protozero::ConstBytes bytes,
- protos::pbzero::TracePacket* message) const {
- protozero::ProtoDecoder decoder(bytes);
-
- for (auto field = decoder.ReadField(); field.valid();
- field = decoder.ReadField()) {
- if (field.id() == protos::pbzero::TracePacket::kFtraceEventsFieldNumber) {
- RedactEvents(context, field.as_bytes(), message->set_ftrace_events());
- } else {
- proto_util::AppendField(field, message);
- }
- }
-}
-
// Iterate over every field in FtraceEvents (bundle), treating FtraceEvent as a
// special case (calls the correct redaction).
void RedactFtraceEvent::RedactEvents(
const Context& context,
- protozero::ConstBytes bytes,
+ protozero::Field bundle,
protos::pbzero::FtraceEventBundle* message) const {
- protozero::ProtoDecoder decoder(bytes);
+ PERFETTO_DCHECK(bundle.id() ==
+ protos::pbzero::TracePacket::kFtraceEventsFieldNumber);
- for (auto field = decoder.ReadField(); field.valid();
- field = decoder.ReadField()) {
- if (field.id() == protos::pbzero::FtraceEventBundle::kEventFieldNumber) {
- RedactEvent(context, field.as_bytes(), message->add_event());
+ // There is only one bundle per packet, so creating the bundle decoder is an
+ // "okay" expense.
+ protos::pbzero::FtraceEventBundle::Decoder bundle_decoder(bundle.as_bytes());
+
+ // Even through we have `bundle_decoder` create a simpler decoder to iterate
+ // over every field.
+ protozero::ProtoDecoder decoder(bundle.as_bytes());
+
+ // Treat FtraceEvent as a special case.
+ for (auto f = decoder.ReadField(); f.valid(); f = decoder.ReadField()) {
+ if (f.id() == protos::pbzero::FtraceEventBundle::kEventFieldNumber) {
+ RedactEvent(context, bundle_decoder, f, message->add_event());
} else {
- proto_util::AppendField(field, message);
+ proto_util::AppendField(f, message);
}
}
}
void RedactFtraceEvent::RedactEvent(
const Context& context,
- protozero::ConstBytes bytes,
+ const protos::pbzero::FtraceEventBundle::Decoder& bundle,
+ protozero::Field event,
protos::pbzero::FtraceEvent* message) const {
- protozero::ProtoDecoder event(bytes);
+ PERFETTO_DCHECK(event.id() ==
+ protos::pbzero::FtraceEventBundle::kEventFieldNumber);
- for (auto field = event.ReadField(); field.valid();
- field = event.ReadField()) {
- auto* mod = redactions_.Find(field.id());
+ // A modifier can/will change the decoder by calling ReadField(). To avoid a
+ // modifier from interfering with the this function's loop, a reusable decoder
+ // is used for each modifier call.
+ protozero::ProtoDecoder outer_decoder(event.as_bytes());
+ protozero::ProtoDecoder inner_decoder(event.as_bytes());
+ // If there is a handler for a field, treat it as a special case.
+ for (auto f = outer_decoder.ReadField(); f.valid();
+ f = outer_decoder.ReadField()) {
+ auto* mod = redactions_.Find(f.id());
if (mod && mod->get()) {
- protos::pbzero::FtraceEvent::Decoder event_decoder(bytes);
- mod->get()->Redact(context, event_decoder, field.as_bytes(), message);
+ // Reset the decoder so that it appears like a "new" decoder to the
+ // modifier.
+ inner_decoder.Reset();
+ mod->get()->Redact(context, bundle, inner_decoder, message);
} else {
- proto_util::AppendField(field, message);
+ proto_util::AppendField(f, message);
}
}
}
diff --git a/src/trace_redaction/redact_ftrace_event.h b/src/trace_redaction/redact_ftrace_event.h
index fe282cf..9de6cc6 100644
--- a/src/trace_redaction/redact_ftrace_event.h
+++ b/src/trace_redaction/redact_ftrace_event.h
@@ -18,11 +18,16 @@
#define SRC_TRACE_REDACTION_REDACT_FTRACE_EVENT_H_
#include <cstdint>
+#include <memory>
+#include <string>
+#include "perfetto/base/status.h"
#include "perfetto/ext/base/flat_hash_map.h"
+#include "perfetto/protozero/field.h"
#include "src/trace_redaction/trace_redaction_framework.h"
#include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
+#include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
namespace perfetto::trace_redaction {
@@ -33,11 +38,14 @@
public:
virtual ~FtraceEventRedaction();
- // Write a new version of the event to the message.
+ // Write at most one field from `event` to `event_message`. This relies on the
+ // honor system; other redactions may be registered on other values.
+ //
+ // - event: effectively "protos::pbzero::FtraceEvent::Decoder"
virtual base::Status Redact(
const Context& context,
- const protos::pbzero::FtraceEvent::Decoder& event,
- protozero::ConstBytes bytes,
+ const protos::pbzero::FtraceEventBundle::Decoder& bundle,
+ protozero::ProtoDecoder& event,
protos::pbzero::FtraceEvent* event_message) const = 0;
};
@@ -46,23 +54,21 @@
base::Status Transform(const Context& context,
std::string* packet) const override;
- // Add a new redaction. T must extend FtraceEventRedaction.
- template <uint32_t field_id, typename T>
+ // Add a new redaction. T must extend FtraceEventRedaction. This relies on the
+ // honor system; no more than one redaction can be mapped to a field.
+ template <uint32_t field_id, class T>
void emplace_back() {
redactions_.Insert(field_id, std::make_unique<T>());
}
private:
- void RedactPacket(const Context& context,
- protozero::ConstBytes bytes,
- protos::pbzero::TracePacket* message) const;
-
void RedactEvents(const Context& context,
- protozero::ConstBytes bytes,
+ protozero::Field bundle,
protos::pbzero::FtraceEventBundle* message) const;
void RedactEvent(const Context& context,
- protozero::ConstBytes bytes,
+ const protos::pbzero::FtraceEventBundle::Decoder& bundle,
+ protozero::Field event,
protos::pbzero::FtraceEvent* message) const;
base::FlatHashMap<uint32_t, std::unique_ptr<FtraceEventRedaction>>
diff --git a/src/trace_redaction/redact_process_free.cc b/src/trace_redaction/redact_process_free.cc
index 7294d68..8f5a83e 100644
--- a/src/trace_redaction/redact_process_free.cc
+++ b/src/trace_redaction/redact_process_free.cc
@@ -44,20 +44,19 @@
// of this, the timeline is not needed.
base::Status RedactProcessFree::Redact(
const Context&,
- const protos::pbzero::FtraceEvent::Decoder&,
- protozero::ConstBytes bytes,
+ const protos::pbzero::FtraceEventBundle::Decoder&,
+ protozero::ProtoDecoder& event,
protos::pbzero::FtraceEvent* event_message) const {
- // SchedProcessFreeFtraceEvent
- protozero::ProtoDecoder process_free_decoder(bytes);
-
- // There must be pid. If there's no pid, the safest option is to drop it.
- auto pid = process_free_decoder.FindField(
- protos::pbzero::SchedProcessFreeFtraceEvent::kPidFieldNumber);
-
- if (!pid.valid()) {
- return base::OkStatus();
+ auto sched_process_free = event.FindField(
+ protos::pbzero::FtraceEvent::kSchedProcessFreeFieldNumber);
+ if (!sched_process_free.valid()) {
+ return base::ErrStatus(
+ "RedactProcessFree: was used for unsupported field type");
}
+ // SchedProcessFreeFtraceEvent
+ protozero::ProtoDecoder process_free_decoder(sched_process_free.as_bytes());
+
auto* process_free_message = event_message->set_sched_process_free();
// Replace the comm with an empty string instead of dropping the comm field.
diff --git a/src/trace_redaction/redact_process_free.h b/src/trace_redaction/redact_process_free.h
index 8d24153..19e954c 100644
--- a/src/trace_redaction/redact_process_free.h
+++ b/src/trace_redaction/redact_process_free.h
@@ -31,8 +31,8 @@
base::Status Redact(
const Context& context,
- const protos::pbzero::FtraceEvent::Decoder& event,
- protozero::ConstBytes bytes,
+ const protos::pbzero::FtraceEventBundle::Decoder& bundle,
+ protozero::ProtoDecoder& event,
protos::pbzero::FtraceEvent* event_message) const override;
};
diff --git a/src/trace_redaction/redact_process_free_unittest.cc b/src/trace_redaction/redact_process_free_unittest.cc
index ee119ca..04064ae 100644
--- a/src/trace_redaction/redact_process_free_unittest.cc
+++ b/src/trace_redaction/redact_process_free_unittest.cc
@@ -29,66 +29,72 @@
namespace perfetto::trace_redaction {
-class RedactProcessFreeTest : public testing::Test {};
+class RedactProcessFreeTest : public testing::Test {
+ protected:
+ void SetUp() override {
+ auto* source_event = bundle.add_event();
+ source_event->set_timestamp(123456789);
+ source_event->set_pid(10);
+ }
-TEST_F(RedactProcessFreeTest, ClearsComm) {
- protos::gen::FtraceEvent source_event;
- source_event.set_timestamp(123456789);
- source_event.set_pid(10);
+ base::Status Redact(protos::pbzero::FtraceEvent* event_message) {
+ RedactProcessFree redact;
+ Context context;
- auto* process_free = source_event.mutable_sched_process_free();
+ auto bundle_str = bundle.SerializeAsString();
+ protos::pbzero::FtraceEventBundle::Decoder bundle_decoder(bundle_str);
+
+ auto event_str = bundle.event().back().SerializeAsString();
+ protos::pbzero::FtraceEvent::Decoder event_decoder(event_str);
+
+ return redact.Redact(context, bundle_decoder, event_decoder, event_message);
+ }
+
+ protos::gen::FtraceEventBundle bundle;
+};
+
+// A free event will always test as "not active". So the comm value should
+// always be replaced with an empty string.
+TEST_F(RedactProcessFreeTest, ClearsCommValue) {
+ auto* process_free =
+ bundle.mutable_event()->back().mutable_sched_process_free();
process_free->set_comm("comm-a");
process_free->set_pid(11);
- RedactProcessFree redact;
- Context context;
-
- protos::pbzero::FtraceEvent::Decoder event_decoder(
- source_event.SerializeAsString());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
- auto result =
- redact.Redact(context, event_decoder, event_decoder.sched_switch(),
- event_message.get());
+ auto result = Redact(event_message.get());
ASSERT_OK(result) << result.c_message();
protos::gen::FtraceEvent redacted_event;
redacted_event.ParseFromString(event_message.SerializeAsString());
// No process free event should have been added to the ftrace event.
- ASSERT_FALSE(redacted_event.has_sched_process_free());
+ ASSERT_TRUE(redacted_event.has_sched_process_free());
+ ASSERT_TRUE(redacted_event.sched_process_free().has_comm());
+ ASSERT_TRUE(redacted_event.sched_process_free().comm().empty());
}
-// Even if there is no pid in the process free event, the process free event
-// should still exist but no comm value should be present.
+// Even if there is no pid in the process free event, the comm value should be
+// replaced with an empty string.
TEST_F(RedactProcessFreeTest, NoPidClearsEvent) {
- protos::gen::FtraceEvent source_event;
- source_event.set_timestamp(123456789);
- source_event.set_pid(10);
-
- // Don't add a pid. This should stop the process free event from being added
- // to the event message.
- auto* process_free = source_event.mutable_sched_process_free();
+ // Don't add a pid. This should have no change in behaviour.
+ auto* process_free =
+ bundle.mutable_event()->back().mutable_sched_process_free();
process_free->set_comm("comm-a");
- RedactProcessFree redact;
- Context context;
-
- protos::pbzero::FtraceEvent::Decoder event_decoder(
- source_event.SerializeAsString());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
- // Even if the process free event has been dropped, there should be no
- // resulting error.
- auto result =
- redact.Redact(context, event_decoder, event_decoder.sched_switch(),
- event_message.get());
+ auto result = Redact(event_message.get());
ASSERT_OK(result) << result.c_message();
protos::gen::FtraceEvent redacted_event;
redacted_event.ParseFromString(event_message.SerializeAsString());
- ASSERT_FALSE(redacted_event.has_sched_process_free());
+ // No process free event should have been added to the ftrace event.
+ ASSERT_TRUE(redacted_event.has_sched_process_free());
+ ASSERT_TRUE(redacted_event.sched_process_free().has_comm());
+ ASSERT_TRUE(redacted_event.sched_process_free().comm().empty());
}
} // namespace perfetto::trace_redaction
diff --git a/src/trace_redaction/redact_sched_switch.cc b/src/trace_redaction/redact_sched_switch.cc
index 361993d..8dca030 100644
--- a/src/trace_redaction/redact_sched_switch.cc
+++ b/src/trace_redaction/redact_sched_switch.cc
@@ -24,6 +24,20 @@
namespace perfetto::trace_redaction {
+namespace {
+
+protozero::ConstChars SanitizeCommValue(const Context& context,
+ ProcessThreadTimeline::Slice slice,
+ protozero::Field field) {
+ if (NormalizeUid(slice.uid) == NormalizeUid(context.package_uid.value())) {
+ return field.as_string();
+ }
+
+ return {};
+}
+
+} // namespace
+
// Redact sched switch trace events in an ftrace event bundle:
//
// event {
@@ -49,8 +63,8 @@
base::Status RedactSchedSwitch::Redact(
const Context& context,
- const protos::pbzero::FtraceEvent::Decoder& event,
- protozero::ConstBytes bytes,
+ const protos::pbzero::FtraceEventBundle::Decoder&,
+ protozero::ProtoDecoder& event,
protos::pbzero::FtraceEvent* event_message) const {
if (!context.package_uid.has_value()) {
return base::ErrStatus("RedactSchedSwitch: missing package uid");
@@ -60,11 +74,31 @@
return base::ErrStatus("RedactSchedSwitch: missing timeline");
}
- protos::pbzero::SchedSwitchFtraceEvent::Decoder sched_switch(bytes);
+ // The timestamp is needed to do the timeline look-up. If the packet has no
+ // timestamp, don't add the sched switch event. This is the safest option.
+ auto timestamp =
+ event.FindField(protos::pbzero::FtraceEvent::kTimestampFieldNumber);
+ if (!timestamp.valid()) {
+ return base::OkStatus();
+ }
+
+ auto sched_switch =
+ event.FindField(protos::pbzero::FtraceEvent::kSchedSwitchFieldNumber);
+ if (!sched_switch.valid()) {
+ return base::ErrStatus(
+ "RedactSchedSwitch: was used for unsupported field type");
+ }
+
+ protozero::ProtoDecoder sched_switch_decoder(sched_switch.as_bytes());
+
+ auto prev_pid = sched_switch_decoder.FindField(
+ protos::pbzero::SchedSwitchFtraceEvent::kPrevPidFieldNumber);
+ auto next_pid = sched_switch_decoder.FindField(
+ protos::pbzero::SchedSwitchFtraceEvent::kNextPidFieldNumber);
// There must be a prev pid and a next pid. Otherwise, the event is invalid.
// Dropping the event is the safest option.
- if (!sched_switch.has_prev_pid() || !sched_switch.has_next_pid()) {
+ if (!prev_pid.valid() || !next_pid.valid()) {
return base::OkStatus();
}
@@ -72,30 +106,21 @@
auto sched_switch_message = event_message->set_sched_switch();
auto prev_slice =
- context.timeline->Search(event.timestamp(), sched_switch.prev_pid());
+ context.timeline->Search(timestamp.as_uint64(), prev_pid.as_int32());
auto next_slice =
- context.timeline->Search(event.timestamp(), sched_switch.next_pid());
+ context.timeline->Search(timestamp.as_uint64(), next_pid.as_int32());
- // To read the fields, move the read head back to the start.
- sched_switch.Reset();
-
- for (auto field = sched_switch.ReadField(); field.valid();
- field = sched_switch.ReadField()) {
+ for (auto field = sched_switch_decoder.ReadField(); field.valid();
+ field = sched_switch_decoder.ReadField()) {
switch (field.id()) {
case protos::pbzero::SchedSwitchFtraceEvent::kNextCommFieldNumber:
- if (next_slice.uid == context.package_uid) {
- sched_switch_message->set_next_comm(field.as_string());
- } else {
- sched_switch_message->set_next_comm("");
- }
+ sched_switch_message->set_next_comm(
+ SanitizeCommValue(context, next_slice, field));
break;
case protos::pbzero::SchedSwitchFtraceEvent::kPrevCommFieldNumber:
- if (prev_slice.uid == context.package_uid) {
- sched_switch_message->set_prev_comm(field.as_string());
- } else {
- sched_switch_message->set_prev_comm("");
- }
+ sched_switch_message->set_prev_comm(
+ SanitizeCommValue(context, prev_slice, field));
break;
default:
diff --git a/src/trace_redaction/redact_sched_switch.h b/src/trace_redaction/redact_sched_switch.h
index e9d70cb..bcfa30d 100644
--- a/src/trace_redaction/redact_sched_switch.h
+++ b/src/trace_redaction/redact_sched_switch.h
@@ -31,8 +31,8 @@
base::Status Redact(
const Context& context,
- const protos::pbzero::FtraceEvent::Decoder& event,
- protozero::ConstBytes bytes,
+ const protos::pbzero::FtraceEventBundle::Decoder& bundle,
+ protozero::ProtoDecoder& event,
protos::pbzero::FtraceEvent* event_message) const override;
};
diff --git a/src/trace_redaction/redact_sched_switch_unittest.cc b/src/trace_redaction/redact_sched_switch_unittest.cc
index f6a4c13..72b65f4 100644
--- a/src/trace_redaction/redact_sched_switch_unittest.cc
+++ b/src/trace_redaction/redact_sched_switch_unittest.cc
@@ -22,7 +22,6 @@
#include "protos/perfetto/trace/ftrace/ftrace_event.gen.h"
#include "protos/perfetto/trace/ftrace/ftrace_event_bundle.gen.h"
#include "protos/perfetto/trace/ftrace/sched.gen.h"
-#include "protos/perfetto/trace/ftrace/sched.pbzero.h"
#include "protos/perfetto/trace/trace.gen.h"
#include "protos/perfetto/trace/trace_packet.gen.h"
@@ -46,36 +45,54 @@
class RedactSchedSwitchTest : public testing::Test {
protected:
void SetUp() override {
- timeline_ = std::make_unique<ProcessThreadTimeline>();
- timeline_->Append(
- ProcessThreadTimeline::Event::Open(0, kPidA, kNoParent, kUidA));
- timeline_->Append(
- ProcessThreadTimeline::Event::Open(0, kPidB, kNoParent, kUidB));
- timeline_->Sort();
+ auto* event = bundle_.add_event();
- protozero::HeapBuffered<protos::pbzero::FtraceEvent> event;
event->set_timestamp(123456789);
event->set_pid(kPidA);
- auto* sched_switch = event->set_sched_switch();
- sched_switch->set_prev_comm(kCommA.data(), kCommA.size());
+ auto* sched_switch = event->mutable_sched_switch();
+ sched_switch->set_prev_comm(std::string(kCommA));
sched_switch->set_prev_pid(kPidA);
- sched_switch->set_next_comm(kCommB.data(), kCommB.size());
+ sched_switch->set_next_comm(std::string(kCommB));
sched_switch->set_next_pid(kPidB);
+ }
- event_string_ = event.SerializeAsString();
+ base::Status Redact(const Context& context,
+ protos::pbzero::FtraceEvent* event_message) {
+ RedactSchedSwitch redact;
+
+ auto bundle_str = bundle_.SerializeAsString();
+ protos::pbzero::FtraceEventBundle::Decoder bundle_decoder(bundle_str);
+
+ auto event_str = bundle_.event().back().SerializeAsString();
+ protos::pbzero::FtraceEvent::Decoder event_decoder(event_str);
+
+ return redact.Redact(context, bundle_decoder, event_decoder, event_message);
}
const std::string& event_string() const { return event_string_; }
- std::unique_ptr<ProcessThreadTimeline> timeline() {
- return std::move(timeline_);
+ // This test breaks the rules for task_newtask and the timeline. The
+ // timeline will report the task existing before the new task event. This
+ // should not happen in the field, but it makes the test more robust.
+ std::unique_ptr<ProcessThreadTimeline> CreatePopulatedTimeline() {
+ auto timeline = std::make_unique<ProcessThreadTimeline>();
+
+ timeline->Append(
+ ProcessThreadTimeline::Event::Open(0, kPidA, kNoParent, kUidA));
+ timeline->Append(
+ ProcessThreadTimeline::Event::Open(0, kPidB, kNoParent, kUidB));
+ timeline->Sort();
+
+ return timeline;
}
private:
std::string event_string_;
std::unique_ptr<ProcessThreadTimeline> timeline_;
+
+ protos::gen::FtraceEventBundle bundle_;
};
TEST_F(RedactSchedSwitchTest, RejectMissingPackageUid) {
@@ -84,12 +101,8 @@
Context context;
context.timeline = std::make_unique<ProcessThreadTimeline>();
- protos::pbzero::FtraceEvent::Decoder event_decoder(event_string());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
-
- auto result =
- redact.Redact(context, event_decoder, event_decoder.sched_switch(),
- event_message.get());
+ auto result = Redact(context, event_message.get());
ASSERT_FALSE(result.ok());
}
@@ -99,12 +112,8 @@
Context context;
context.package_uid = kUidA;
- protos::pbzero::FtraceEvent::Decoder event_decoder(event_string());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
-
- auto result =
- redact.Redact(context, event_decoder, event_decoder.sched_switch(),
- event_message.get());
+ auto result = Redact(context, event_message.get());
ASSERT_FALSE(result.ok());
}
@@ -112,18 +121,14 @@
RedactSchedSwitch redact;
Context context;
- context.timeline = timeline();
+ context.timeline = CreatePopulatedTimeline();
// Neither pid is connected to the target package (see timeline
// initialization).
context.package_uid = kUidC;
- protos::pbzero::FtraceEvent::Decoder event_decoder(event_string());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
-
- auto result =
- redact.Redact(context, event_decoder, event_decoder.sched_switch(),
- event_message.get());
+ auto result = Redact(context, event_message.get());
ASSERT_OK(result) << result.c_message();
protos::gen::FtraceEvent event;
@@ -143,18 +148,15 @@
RedactSchedSwitch redact;
Context context;
- context.timeline = timeline();
+ context.timeline = CreatePopulatedTimeline();
// Only next pid is connected to the target package (see timeline
// initialization).
context.package_uid = kUidB;
- protos::pbzero::FtraceEvent::Decoder event_decoder(event_string());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
+ auto result = Redact(context, event_message.get());
- auto result =
- redact.Redact(context, event_decoder, event_decoder.sched_switch(),
- event_message.get());
ASSERT_OK(result) << result.c_message();
protos::gen::FtraceEvent event;
@@ -174,18 +176,14 @@
RedactSchedSwitch redact;
Context context;
- context.timeline = timeline();
+ context.timeline = CreatePopulatedTimeline();
// Only prev pid is connected to the target package (see timeline
// initialization).
context.package_uid = kUidA;
- protos::pbzero::FtraceEvent::Decoder event_decoder(event_string());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
-
- auto result =
- redact.Redact(context, event_decoder, event_decoder.sched_switch(),
- event_message.get());
+ auto result = Redact(context, event_message.get());
ASSERT_OK(result) << result.c_message();
protos::gen::FtraceEvent event;
diff --git a/src/trace_redaction/redact_task_newtask.cc b/src/trace_redaction/redact_task_newtask.cc
index cb6a41c..c942612 100644
--- a/src/trace_redaction/redact_task_newtask.cc
+++ b/src/trace_redaction/redact_task_newtask.cc
@@ -16,8 +16,6 @@
#include "src/trace_redaction/redact_task_newtask.h"
-#include <string>
-
#include "src/trace_redaction/proto_util.h"
#include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
@@ -26,6 +24,20 @@
namespace perfetto::trace_redaction {
+namespace {
+
+protozero::ConstChars SanitizeCommValue(const Context& context,
+ ProcessThreadTimeline::Slice slice,
+ protozero::Field field) {
+ if (NormalizeUid(slice.uid) == NormalizeUid(context.package_uid.value())) {
+ return field.as_string();
+ }
+
+ return {};
+}
+
+} // namespace
+
// Redact sched switch trace events in an ftrace event bundle:
//
// event {
@@ -43,8 +55,8 @@
// equal to "event.task_newtask.pid" (a thread cannot start itself).
base::Status RedactTaskNewTask::Redact(
const Context& context,
- const protos::pbzero::FtraceEvent::Decoder& event,
- protozero::ConstBytes bytes,
+ const protos::pbzero::FtraceEventBundle::Decoder&,
+ protozero::ProtoDecoder& event,
protos::pbzero::FtraceEvent* event_message) const {
if (!context.package_uid.has_value()) {
return base::ErrStatus("RedactTaskNewTask: missing package uid");
@@ -54,14 +66,24 @@
return base::ErrStatus("RedactTaskNewTask: missing timeline");
}
- // There must be a pid. If not, the message is meaningless and can be dropped.
- if (!event.has_timestamp()) {
+ // The timestamp is needed to do the timeline look-up. If the packet has no
+ // timestamp, don't add the sched switch event. This is the safest option.
+ auto timestamp =
+ event.FindField(protos::pbzero::FtraceEvent::kTimestampFieldNumber);
+ if (!timestamp.valid()) {
return base::OkStatus();
}
- protozero::ProtoDecoder new_task(bytes);
+ auto new_task =
+ event.FindField(protos::pbzero::FtraceEvent::kTaskNewtaskFieldNumber);
+ if (!new_task.valid()) {
+ return base::ErrStatus(
+ "RedactTaskNewTask: was used for unsupported field type");
+ }
- auto pid = new_task.FindField(
+ protozero::ProtoDecoder new_task_decoder(new_task.as_bytes());
+
+ auto pid = new_task_decoder.FindField(
protos::pbzero::TaskNewtaskFtraceEvent::kPidFieldNumber);
if (!pid.valid()) {
@@ -71,20 +93,16 @@
// Avoid making the message until we know that we have prev and next pids.
auto* new_task_message = event_message->set_task_newtask();
- auto slice = context.timeline->Search(event.timestamp(), pid.as_int32());
+ auto slice = context.timeline->Search(timestamp.as_uint64(), pid.as_int32());
- for (auto field = new_task.ReadField(); field.valid();
- field = new_task.ReadField()) {
+ for (auto field = new_task_decoder.ReadField(); field.valid();
+ field = new_task_decoder.ReadField()) {
+ // Perfetto view (ui.perfetto.dev) crashes if the comm value is missing.
+ // To work around this, the comm value is replaced with an empty string.
+ // This appears to work.
if (field.id() ==
protos::pbzero::TaskNewtaskFtraceEvent::kCommFieldNumber) {
- if (slice.uid == context.package_uid) {
- proto_util::AppendField(field, new_task_message);
- } else {
- // Perfetto view (ui.perfetto.dev) crashes if the comm value is missing.
- // To work around this, the comm value is replaced with an empty string.
- // This appears to work.
- new_task_message->set_comm("");
- }
+ new_task_message->set_comm(SanitizeCommValue(context, slice, field));
} else {
proto_util::AppendField(field, new_task_message);
}
diff --git a/src/trace_redaction/redact_task_newtask.h b/src/trace_redaction/redact_task_newtask.h
index 51d384c..51c1124 100644
--- a/src/trace_redaction/redact_task_newtask.h
+++ b/src/trace_redaction/redact_task_newtask.h
@@ -31,8 +31,8 @@
base::Status Redact(
const Context& context,
- const protos::pbzero::FtraceEvent::Decoder& event,
- protozero::ConstBytes bytes,
+ const protos::pbzero::FtraceEventBundle::Decoder& bundle,
+ protozero::ProtoDecoder& event,
protos::pbzero::FtraceEvent* event_message) const override;
};
diff --git a/src/trace_redaction/redact_task_newtask_unittest.cc b/src/trace_redaction/redact_task_newtask_unittest.cc
index d09f044..33886f6 100644
--- a/src/trace_redaction/redact_task_newtask_unittest.cc
+++ b/src/trace_redaction/redact_task_newtask_unittest.cc
@@ -16,11 +16,15 @@
#include "src/trace_redaction/redact_task_newtask.h"
#include "perfetto/protozero/scattered_heap_buffer.h"
+#include "protos/perfetto/trace/ftrace/task.gen.h"
#include "test/gtest_and_gmock.h"
#include "protos/perfetto/trace/ftrace/ftrace_event.gen.h"
-#include "protos/perfetto/trace/ftrace/task.gen.h"
-#include "protos/perfetto/trace/ftrace/task.pbzero.h"
+#include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
+#include "protos/perfetto/trace/ftrace/ftrace_event_bundle.gen.h"
+#include "protos/perfetto/trace/ftrace/sched.gen.h"
+#include "protos/perfetto/trace/trace.gen.h"
+#include "protos/perfetto/trace/trace_packet.gen.h"
namespace perfetto::trace_redaction {
@@ -40,37 +44,52 @@
class RedactTaskNewTaskTest : public testing::Test {
protected:
void SetUp() override {
- timeline_ = std::make_unique<ProcessThreadTimeline>();
- timeline_->Append(
- ProcessThreadTimeline::Event::Open(0, kPidA, kNoParent, kUidA));
- timeline_->Append(
- ProcessThreadTimeline::Event::Open(0, kPidB, kNoParent, kUidB));
- timeline_->Sort();
+ auto* event = bundle_.add_event();
- // This test breaks the rules for task_newtask and the timeline. The
- // timeline will report the task existing before the new task event. This
- // should not happen in the field, but it makes the test more robust.
- protozero::HeapBuffered<protos::pbzero::FtraceEvent> event;
event->set_timestamp(123456789);
event->set_pid(kPidA);
- auto* new_task = event->set_task_newtask();
- new_task->set_comm(kCommA.data(), kCommA.size());
+ auto* new_task = event->mutable_task_newtask();
+ new_task->set_comm(std::string(kCommA));
new_task->set_pid(kPidA);
+ }
- event_string_ = event.SerializeAsString();
+ base::Status Redact(const Context& context,
+ protos::pbzero::FtraceEvent* event_message) {
+ RedactTaskNewTask redact;
+
+ auto bundle_str = bundle_.SerializeAsString();
+ protos::pbzero::FtraceEventBundle::Decoder bundle_decoder(bundle_str);
+
+ auto event_str = bundle_.event().back().SerializeAsString();
+ protos::pbzero::FtraceEvent::Decoder event_decoder(event_str);
+
+ return redact.Redact(context, bundle_decoder, event_decoder, event_message);
}
const std::string& event_string() const { return event_string_; }
- std::unique_ptr<ProcessThreadTimeline> timeline() {
- return std::move(timeline_);
+ // This test breaks the rules for task_newtask and the timeline. The
+ // timeline will report the task existing before the new task event. This
+ // should not happen in the field, but it makes the test more robust.
+ std::unique_ptr<ProcessThreadTimeline> CreatePopulatedTimeline() {
+ auto timeline = std::make_unique<ProcessThreadTimeline>();
+
+ timeline->Append(
+ ProcessThreadTimeline::Event::Open(0, kPidA, kNoParent, kUidA));
+ timeline->Append(
+ ProcessThreadTimeline::Event::Open(0, kPidB, kNoParent, kUidB));
+ timeline->Sort();
+
+ return timeline;
}
private:
std::string event_string_;
std::unique_ptr<ProcessThreadTimeline> timeline_;
+
+ protos::gen::FtraceEventBundle bundle_;
};
TEST_F(RedactTaskNewTaskTest, RejectMissingPackageUid) {
@@ -82,9 +101,7 @@
protos::pbzero::FtraceEvent::Decoder event_decoder(event_string());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
- auto result =
- redact.Redact(context, event_decoder, event_decoder.task_newtask(),
- event_message.get());
+ auto result = Redact(context, event_message.get());
ASSERT_FALSE(result.ok());
}
@@ -97,9 +114,7 @@
protos::pbzero::FtraceEvent::Decoder event_decoder(event_string());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
- auto result =
- redact.Redact(context, event_decoder, event_decoder.task_newtask(),
- event_message.get());
+ auto result = Redact(context, event_message.get());
ASSERT_FALSE(result.ok());
}
@@ -110,14 +125,12 @@
// keep its comm value.
Context context;
context.package_uid = kUidA;
- context.timeline = timeline();
+ context.timeline = CreatePopulatedTimeline();
protos::pbzero::FtraceEvent::Decoder event_decoder(event_string());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
- auto result =
- redact.Redact(context, event_decoder, event_decoder.task_newtask(),
- event_message.get());
+ auto result = Redact(context, event_message.get());
ASSERT_TRUE(result.ok());
protos::gen::FtraceEvent redacted_event;
@@ -135,14 +148,12 @@
// lose its comm value.
Context context;
context.package_uid = kUidB;
- context.timeline = timeline();
+ context.timeline = CreatePopulatedTimeline();
protos::pbzero::FtraceEvent::Decoder event_decoder(event_string());
protozero::HeapBuffered<protos::pbzero::FtraceEvent> event_message;
- auto result =
- redact.Redact(context, event_decoder, event_decoder.task_newtask(),
- event_message.get());
+ auto result = Redact(context, event_message.get());
ASSERT_TRUE(result.ok());
protos::gen::FtraceEvent redacted_event;
diff --git a/src/trace_redaction/scrub_process_trees.cc b/src/trace_redaction/scrub_process_trees.cc
index d8baf99..989d094 100644
--- a/src/trace_redaction/scrub_process_trees.cc
+++ b/src/trace_redaction/scrub_process_trees.cc
@@ -16,9 +16,6 @@
#include "src/trace_redaction/scrub_process_trees.h"
-#include <cstdint>
-#include <string>
-
#include "perfetto/base/status.h"
#include "perfetto/protozero/field.h"
#include "perfetto/protozero/scattered_heap_buffer.h"
@@ -29,149 +26,90 @@
#include "protos/perfetto/trace/trace_packet.pbzero.h"
namespace perfetto::trace_redaction {
+
namespace {
-constexpr auto kThreadsFieldNumber =
- protos::pbzero::ProcessTree::kThreadsFieldNumber;
-constexpr auto kTimestampFieldNumber =
- protos::pbzero::TracePacket::kTimestampFieldNumber;
-constexpr auto kProcessTreeFieldNumber =
- protos::pbzero::TracePacket::kProcessTreeFieldNumber;
-constexpr auto kProcessesFieldNumber =
- protos::pbzero::ProcessTree::kProcessesFieldNumber;
-
-// Skips the cmdline fields.
-void ClearProcessName(protozero::ConstBytes bytes,
- protos::pbzero::ProcessTree::Process* message) {
- protozero::ProtoDecoder decoder(bytes);
-
- for (auto field = decoder.ReadField(); field; field = decoder.ReadField()) {
- if (field.id() !=
- protos::pbzero::ProcessTree::Process::kCmdlineFieldNumber) {
- proto_util::AppendField(field, message);
- }
- }
-}
-
-void ScrubProcess(protozero::Field field,
- const ProcessThreadTimeline& timeline,
- uint64_t now,
- uint64_t uid,
- protos::pbzero::ProcessTree* message) {
- if (field.id() != kProcessesFieldNumber) {
- PERFETTO_FATAL(
- "ScrubProcess() should only be called with a ProcessTree::Processes");
+// Appends a value to the message if (and only if) the pid belongs to the target
+// package.
+void TryAppendPid(const Context& context,
+ const protozero::Field& timestamp,
+ const protozero::Field& pid,
+ const protozero::Field& value,
+ protozero::Message* message) {
+ // All valid processes with have a time and pid/tid values. However, if
+ // they're missing values, the trace is corrupt. To avoid making this work by
+ // dropping too much data, drop the cmdline for all processes.
+ if (!timestamp.valid() || !pid.valid()) {
+ return;
}
- protos::pbzero::ProcessTree::Process::Decoder decoder(field.as_bytes());
- auto slice = timeline.Search(now, decoder.pid());
+ auto slice = context.timeline->Search(timestamp.as_uint64(), pid.as_int32());
- if (NormalizeUid(slice.uid) == NormalizeUid(uid)) {
- proto_util::AppendField(field, message);
- } else {
- ClearProcessName(field.as_bytes(), message->add_processes());
- }
-}
-
-// The thread name is unused, but it's safer to remove it.
-void ClearThreadName(protozero::ConstBytes bytes,
- protos::pbzero::ProcessTree::Thread* message) {
- protozero::ProtoDecoder decoder(bytes);
-
- for (auto field = decoder.ReadField(); field; field = decoder.ReadField()) {
- if (field.id() != protos::pbzero::ProcessTree::Thread::kNameFieldNumber) {
- proto_util::AppendField(field, message);
- }
- }
-}
-
-void ScrubThread(protozero::Field field,
- const ProcessThreadTimeline& timeline,
- uint64_t now,
- uint64_t uid,
- protos::pbzero::ProcessTree* message) {
- if (field.id() != kThreadsFieldNumber) {
- PERFETTO_FATAL(
- "ScrubThread() should only be called with a ProcessTree::Threads");
+ // Only keep the target process cmdline.
+ if (NormalizeUid(slice.uid) != NormalizeUid(context.package_uid.value())) {
+ return;
}
- protos::pbzero::ProcessTree::Thread::Decoder thread_decoder(field.as_bytes());
- auto slice = timeline.Search(now, thread_decoder.tid());
-
- if (NormalizeUid(slice.uid) == NormalizeUid(uid)) {
- proto_util::AppendField(field, message);
- } else {
- ClearThreadName(field.as_bytes(), message->add_threads());
- }
+ proto_util::AppendField(value, message);
}
} // namespace
-base::Status ScrubProcessTrees::Transform(const Context& context,
- std::string* packet) const {
+base::Status ScrubProcessTrees::VerifyContext(const Context& context) const {
if (!context.package_uid.has_value()) {
return base::ErrStatus("ScrubProcessTrees: missing package uid.");
}
- if (context.timeline == nullptr) {
+ if (!context.timeline) {
return base::ErrStatus("ScrubProcessTrees: missing timeline.");
}
- protozero::ProtoDecoder decoder(*packet);
-
- if (!decoder.FindField(kProcessTreeFieldNumber).valid()) {
- return base::OkStatus();
- }
-
- auto timestamp_field = decoder.FindField(kTimestampFieldNumber);
-
- if (!timestamp_field.valid()) {
- return base::ErrStatus("ScrubProcessTrees: trace packet missing timestamp");
- }
-
- auto timestamp = timestamp_field.as_uint64();
-
- auto uid = context.package_uid.value();
-
- const auto& timeline = *context.timeline.get();
-
- protozero::HeapBuffered<protos::pbzero::TracePacket> message;
-
- for (auto packet_field = decoder.ReadField(); packet_field.valid();
- packet_field = decoder.ReadField()) {
- if (packet_field.id() != kProcessTreeFieldNumber) {
- proto_util::AppendField(packet_field, message.get());
- continue;
- }
-
- auto* process_tree_message = message->set_process_tree();
-
- protozero::ProtoDecoder process_tree_decoder(packet_field.as_bytes());
-
- for (auto process_tree_field = process_tree_decoder.ReadField();
- process_tree_field.valid();
- process_tree_field = process_tree_decoder.ReadField()) {
- switch (process_tree_field.id()) {
- case kProcessesFieldNumber:
- ScrubProcess(process_tree_field, timeline, timestamp, uid,
- process_tree_message);
- break;
-
- case kThreadsFieldNumber:
- ScrubThread(process_tree_field, timeline, timestamp, uid,
- process_tree_message);
- break;
-
- default:
- proto_util::AppendField(process_tree_field, process_tree_message);
- break;
- }
- }
- }
-
- packet->assign(message.SerializeAsString());
-
return base::OkStatus();
}
+void ScrubProcessTrees::TransformProcess(
+ const Context& context,
+ const protozero::Field& timestamp,
+ const protozero::Field& process,
+ protos::pbzero::ProcessTree* process_tree) const {
+ protozero::ProtoDecoder decoder(process.as_bytes());
+
+ auto pid =
+ decoder.FindField(protos::pbzero::ProcessTree::Process::kPidFieldNumber);
+
+ auto* process_message = process_tree->add_processes();
+
+ for (auto field = decoder.ReadField(); field.valid();
+ field = decoder.ReadField()) {
+ if (field.id() ==
+ protos::pbzero::ProcessTree::Process::kCmdlineFieldNumber) {
+ TryAppendPid(context, timestamp, pid, field, process_message);
+ } else {
+ proto_util::AppendField(field, process_message);
+ }
+ }
+}
+
+void ScrubProcessTrees::TransformThread(
+ const Context& context,
+ const protozero::Field& timestamp,
+ const protozero::Field& thread,
+ protos::pbzero::ProcessTree* process_tree) const {
+ protozero::ProtoDecoder decoder(thread.as_bytes());
+
+ auto tid =
+ decoder.FindField(protos::pbzero::ProcessTree::Thread::kTidFieldNumber);
+
+ auto* thread_message = process_tree->add_threads();
+
+ for (auto field = decoder.ReadField(); field.valid();
+ field = decoder.ReadField()) {
+ if (field.id() == protos::pbzero::ProcessTree::Thread::kNameFieldNumber) {
+ TryAppendPid(context, timestamp, tid, field, thread_message);
+ } else {
+ proto_util::AppendField(field, thread_message);
+ }
+ }
+}
+
} // namespace perfetto::trace_redaction
diff --git a/src/trace_redaction/scrub_process_trees.h b/src/trace_redaction/scrub_process_trees.h
index 61cb85a..7c2b07a 100644
--- a/src/trace_redaction/scrub_process_trees.h
+++ b/src/trace_redaction/scrub_process_trees.h
@@ -17,19 +17,32 @@
#ifndef SRC_TRACE_REDACTION_SCRUB_PROCESS_TREES_H_
#define SRC_TRACE_REDACTION_SCRUB_PROCESS_TREES_H_
-#include <string>
-
#include "perfetto/base/status.h"
+#include "perfetto/protozero/field.h"
+#include "src/trace_redaction/modify_process_trees.h"
#include "src/trace_redaction/trace_redaction_framework.h"
+#include "protos/perfetto/trace/ps/process_tree.pbzero.h"
+
namespace perfetto::trace_redaction {
// Removes process names and thread names from process_trees if their pids/tids
// are not connected to the target package.
-class ScrubProcessTrees final : public TransformPrimitive {
- public:
- base::Status Transform(const Context& context,
- std::string* packet) const override;
+class ScrubProcessTrees : public ModifyProcessTree {
+ protected:
+ base::Status VerifyContext(const Context& context) const override;
+
+ void TransformProcess(
+ const Context& context,
+ const protozero::Field& timestamp,
+ const protozero::Field& process,
+ protos::pbzero::ProcessTree* process_trees) const override;
+
+ void TransformThread(
+ const Context& context,
+ const protozero::Field& timestamp,
+ const protozero::Field& thread,
+ protos::pbzero::ProcessTree* process_tree) const override;
};
} // namespace perfetto::trace_redaction
diff --git a/test/trace_processor/diff_tests/metrics/android/android_auto_multiuser.textproto b/test/trace_processor/diff_tests/metrics/android/android_auto_multiuser.textproto
new file mode 100644
index 0000000..c33839e
--- /dev/null
+++ b/test/trace_processor/diff_tests/metrics/android/android_auto_multiuser.textproto
@@ -0,0 +1,127 @@
+packet {
+ timestamp: 1
+ process_tree {
+ processes {
+ pid: 10
+ uid: 1000010
+ cmdline: "dummy:1"
+ }
+ processes {
+ pid: 11
+ uid: 1000010
+ cmdline: "dummy:2"
+ }
+ }
+}
+packet {
+ ftrace_events {
+ event {
+ timestamp: 1000000000
+ print {
+ buf: "S|5993|UserController.startUser-10-fg-start-mode-1|0\n"
+ }
+ }
+ }
+}
+packet {
+ ftrace_events {
+ event {
+ timestamp: 2000000000
+ print {
+ buf: "S|2608|launching: com.android.car.carlauncher|0\n"
+ }
+ }
+ }
+}
+packet {
+ ftrace_events {
+ cpu: 1
+ event {
+ timestamp: 3000000001
+ pid: 10
+ sched_switch {
+ prev_comm: "dummy:1"
+ prev_pid: 10
+ prev_state: 2
+ next_comm: "dummy:2"
+ next_pid: 11
+ }
+ }
+ }
+}
+packet {
+ ftrace_events {
+ cpu: 1
+ event {
+ timestamp: 3010000000
+ pid: 11
+ sched_switch {
+ prev_comm: "dummy:2"
+ prev_pid: 11
+ prev_state: 2
+ next_comm: "dummy:1"
+ next_pid: 10
+ }
+ }
+ }
+}
+packet {
+ timestamp: 3000000001
+ process_stats {
+ processes {
+ pid: 10
+ vm_rss_kb: 1000
+ }
+ }
+}
+packet {
+ timestamp: 3000000002
+ process_stats {
+ processes {
+ pid: 10
+ vm_rss_kb: 2000
+ }
+ }
+}
+packet {
+ timestamp: 3000000001
+ process_stats {
+ processes {
+ pid: 11
+ vm_rss_kb: 1000
+ }
+ }
+}
+packet {
+ timestamp: 3000000002
+ process_stats {
+ processes {
+ pid: 11
+ vm_rss_kb: 2000
+ }
+ }
+}
+packet {
+ ftrace_events {
+ cpu: 1
+ event {
+ timestamp: 3000000000
+ pid: 1
+ print {
+ buf: "S|5993|UserController.startUser-11-fg-start-mode-1|0\n"
+ }
+ }
+ }
+}
+packet {
+ ftrace_events {
+ cpu: 1
+ event {
+ timestamp: 4000000000
+ pid: 1
+ print {
+ buf: "S|2609|launching: com.android.car.carlauncher|0\n"
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/test/trace_processor/diff_tests/metrics/android/tests.py b/test/trace_processor/diff_tests/metrics/android/tests.py
index da5559e..a1c2448 100644
--- a/test/trace_processor/diff_tests/metrics/android/tests.py
+++ b/test/trace_processor/diff_tests/metrics/android/tests.py
@@ -299,10 +299,36 @@
out=TextProto(r"""
android_auto_multiuser {
user_switch {
- duration_ms: 3878
+ user_id: 11
+ start_event: "UserController.startUser-11-fg-start-mode-1"
+ end_event: "com.android.car.carlauncher"
+ duration_ms: 3877
+ previous_user_info {
+ }
}
}
"""))
+
+ def test_android_auto_multiuser_switch_with_previous_user_data(self):
+ return DiffTestBlueprint(
+ trace=Path("android_auto_multiuser.textproto"),
+ query=Metric('android_auto_multiuser'),
+ out=TextProto(r"""
+ android_auto_multiuser {
+ user_switch {
+ user_id: 11
+ start_event: "UserController.startUser-11-fg-start-mode-1"
+ end_event: "com.android.car.carlauncher"
+ duration_ms: 999
+ previous_user_info {
+ user_id: 10
+ total_cpu_time_ms: 9
+ total_memory_usage_kb: 2048
+ }
+ }
+ }
+ """))
+
def test_android_oom_adjuster(self):
return DiffTestBlueprint(
trace=DataPath('android_postboot_unlock.pftrace'),
diff --git a/ui/src/frontend/base_slice_track.ts b/ui/src/frontend/base_slice_track.ts
index 329e441..39c38e5 100644
--- a/ui/src/frontend/base_slice_track.ts
+++ b/ui/src/frontend/base_slice_track.ts
@@ -356,8 +356,8 @@
id
${extraCols ? ',' + extraCols : ''}
from (${this.getSqlSource()})
- where dur = -1
group by 1
+ having dur = -1
`);
}
const incomplete = new Array<CastInternal<T['slice']>>(queryRes.numRows());