Merge "cts: test profileability of platform processes"
diff --git a/test/cts/Android.bp b/test/cts/Android.bp
index 3eef756..1400a6e 100644
--- a/test/cts/Android.bp
+++ b/test/cts/Android.bp
@@ -17,9 +17,11 @@
     "reporter_test_cts.cc",
     "traced_perf_test_cts.cc",
     ":perfetto_protos_perfetto_config_cpp_gen",
+    ":perfetto_protos_perfetto_common_cpp_gen"
   ],
   generated_headers: [
     "perfetto_protos_perfetto_config_cpp_gen_headers",
+    "perfetto_protos_perfetto_common_cpp_gen_headers",
   ],
   static_libs: [
     "libgmock",
diff --git a/test/cts/BUILD.gn b/test/cts/BUILD.gn
index e821de8..9294cdb 100644
--- a/test/cts/BUILD.gn
+++ b/test/cts/BUILD.gn
@@ -26,6 +26,7 @@
     "../../gn:gtest_and_gmock",
     "../../include/perfetto/ext/tracing/core",
     "../../protos/perfetto/config:cpp",
+    "../../protos/perfetto/config/process_stats:cpp",
     "../../protos/perfetto/config/profiling:cpp",
     "../../protos/perfetto/trace:cpp",
     "../../protos/perfetto/trace/profiling:cpp",
diff --git a/test/cts/heapprofd_test_cts.cc b/test/cts/heapprofd_test_cts.cc
index 510bc7b..fb5e78f 100644
--- a/test/cts/heapprofd_test_cts.cc
+++ b/test/cts/heapprofd_test_cts.cc
@@ -28,6 +28,7 @@
 #include "test/gtest_and_gmock.h"
 #include "test/test_helper.h"
 
+#include "protos/perfetto/config/process_stats/process_stats_config.gen.h"
 #include "protos/perfetto/config/profiling/heapprofd_config.gen.h"
 #include "protos/perfetto/trace/profiling/profile_common.gen.h"
 #include "protos/perfetto/trace/profiling/profile_packet.gen.h"
@@ -210,9 +211,7 @@
   ASSERT_TRUE(found_alloc);
 }
 
-// Check that `packets` contain some allocations performed by
-// kJavaAllocActivity.
-void AssertExpectedArtAllocsPresent(
+void AssertHasSampledAllocs(
     const std::vector<protos::gen::TracePacket>& packets) {
   ASSERT_GT(packets.size(), 0u);
 
@@ -322,7 +321,7 @@
   std::string app_name = "android.perfetto.cts.app.debuggable";
   const auto& packets = ProfileRuntime(app_name, kJavaAllocActivity,
                                        /*heap_names=*/{"com.android.art"});
-  AssertExpectedArtAllocsPresent(packets);
+  AssertHasSampledAllocs(packets);
   StopApp(app_name);
 }
 
@@ -330,9 +329,60 @@
   std::string app_name = "android.perfetto.cts.app.debuggable";
   const auto& packets = ProfileStartup(app_name, kJavaAllocActivity,
                                        /*heap_names=*/{"com.android.art"});
-  AssertExpectedArtAllocsPresent(packets);
+  AssertHasSampledAllocs(packets);
   StopApp(app_name);
 }
 
+TEST(HeapprofdCtsTest, ProfilePlatformProcess) {
+  int target_pid = PidForProcessName("/system/bin/traced_probes");
+  ASSERT_GT(target_pid, 0) << "failed to find pid for target process";
+
+  // Construct config.
+  TraceConfig trace_config;
+  trace_config.add_buffers()->set_size_kb(20 * 1024);
+  trace_config.set_duration_ms(3000);
+  trace_config.set_data_source_stop_timeout_ms(8000);
+  trace_config.set_unique_session_name(RandomSessionName().c_str());
+
+  // process.stats to cause work in traced_probes
+  protos::gen::ProcessStatsConfig ps_config;
+  ps_config.set_proc_stats_poll_ms(100);
+  ps_config.set_record_thread_names(true);
+
+  auto* ds_config = trace_config.add_data_sources()->mutable_config();
+  ds_config->set_name("linux.process_stats");
+  ds_config->set_process_stats_config_raw(ps_config.SerializeAsString());
+
+  // profile native heap of traced_probes
+  protos::gen::HeapprofdConfig heapprofd_config;
+  heapprofd_config.set_sampling_interval_bytes(kTestSamplingInterval);
+  heapprofd_config.add_pid(static_cast<uint64_t>(target_pid));
+  heapprofd_config.set_block_client(true);
+
+  ds_config = trace_config.add_data_sources()->mutable_config();
+  ds_config->set_name("android.heapprofd");
+  ds_config->set_heapprofd_config_raw(heapprofd_config.SerializeAsString());
+
+  // Collect trace.
+  base::TestTaskRunner task_runner;
+  TestHelper helper(&task_runner);
+  helper.ConnectConsumer();
+  helper.WaitForConsumerConnect();
+
+  helper.StartTracing(trace_config);
+  helper.WaitForTracingDisabled(15000 /*ms*/);
+  helper.ReadData();
+  helper.WaitForReadData();
+  auto packets = helper.trace();
+
+  int target_pid_after = PidForProcessName("/system/bin/traced_probes");
+  ASSERT_EQ(target_pid, target_pid_after) << "traced_probes died during test";
+
+  if (IsUserBuild())
+    AssertNoProfileContents(packets);
+  else
+    AssertHasSampledAllocs(packets);
+}
+
 }  // namespace
 }  // namespace perfetto
diff --git a/test/cts/traced_perf_test_cts.cc b/test/cts/traced_perf_test_cts.cc
index d7bd60e..aaa2178 100644
--- a/test/cts/traced_perf_test_cts.cc
+++ b/test/cts/traced_perf_test_cts.cc
@@ -27,6 +27,8 @@
 #include "test/gtest_and_gmock.h"
 #include "test/test_helper.h"
 
+#include "protos/perfetto/common/perf_events.gen.h"
+#include "protos/perfetto/config/process_stats/process_stats_config.gen.h"
 #include "protos/perfetto/config/profiling/perf_event_config.gen.h"
 #include "protos/perfetto/trace/profiling/profile_common.gen.h"
 #include "protos/perfetto/trace/profiling/profile_packet.gen.h"
@@ -54,6 +56,20 @@
   return result;
 }
 
+std::vector<protos::gen::TracePacket> CollectTrace(
+    base::TestTaskRunner* task_runner,
+    const TraceConfig& trace_config) {
+  TestHelper helper(task_runner);
+  helper.ConnectConsumer();
+  helper.WaitForConsumerConnect();
+
+  helper.StartTracing(trace_config);
+  helper.WaitForTracingDisabled(15000 /*ms*/);
+  helper.ReadData();
+  helper.WaitForReadData();
+  return helper.trace();
+}
+
 std::vector<protos::gen::TracePacket> ProfileSystemWide(std::string app_name) {
   base::TestTaskRunner task_runner;
 
@@ -67,11 +83,7 @@
                    /*delay_ms=*/100);
   task_runner.RunUntilCheckpoint("target.app.running", 10000 /*ms*/);
 
-  // set up tracing
-  TestHelper helper(&task_runner);
-  helper.ConnectConsumer();
-  helper.WaitForConsumerConnect();
-
+  // build config
   TraceConfig trace_config;
   trace_config.add_buffers()->set_size_kb(20 * 1024);
   trace_config.set_duration_ms(3000);
@@ -83,18 +95,11 @@
   ds_config->set_target_buffer(0);
 
   protos::gen::PerfEventConfig perf_config;
-
   perf_config.set_all_cpus(true);
   perf_config.set_sampling_frequency(10);  // Hz
   ds_config->set_perf_event_config_raw(perf_config.SerializeAsString());
 
-  // start tracing
-  helper.StartTracing(trace_config);
-  helper.WaitForTracingDisabled(15000 /*ms*/);
-  helper.ReadData();
-  helper.WaitForReadData();
-
-  return helper.trace();
+  return CollectTrace(&task_runner, trace_config);
 }
 
 void AssertHasSampledStacksForPid(std::vector<protos::gen::TracePacket> packets,
@@ -216,5 +221,55 @@
   StopApp(app_name);
 }
 
+// Loads a platform process with work (we use traced_probes which runs as
+// AID_NOBODY), and profiles it.
+TEST(TracedPerfCtsTest, ProfilePlatformProcess) {
+  if (!HasPerfLsmHooks())
+    GTEST_SKIP() << "skipped due to lack of perf_event_open LSM hooks";
+
+  int target_pid = PidForProcessName("/system/bin/traced_probes");
+  ASSERT_GT(target_pid, 0) << "failed to find pid for target process";
+
+  // Construct config.
+  TraceConfig trace_config;
+  trace_config.add_buffers()->set_size_kb(20 * 1024);
+  trace_config.set_duration_ms(3000);
+  trace_config.set_data_source_stop_timeout_ms(8000);
+  trace_config.set_unique_session_name(RandomSessionName().c_str());
+
+  // process.stats to cause work in traced_probes
+  protos::gen::ProcessStatsConfig ps_config;
+  ps_config.set_proc_stats_poll_ms(100);
+  ps_config.set_record_thread_names(true);
+
+  auto* ds_config = trace_config.add_data_sources()->mutable_config();
+  ds_config->set_name("linux.process_stats");
+  ds_config->set_process_stats_config_raw(ps_config.SerializeAsString());
+
+  // profile traced_probes
+  protos::gen::PerfEventConfig perf_config;
+  auto* timebase = perf_config.mutable_timebase();
+  timebase->set_frequency(100);
+  auto* callstacks = perf_config.mutable_callstack_sampling();
+  auto* scope = callstacks->mutable_scope();
+  scope->add_target_pid(target_pid);
+
+  ds_config = trace_config.add_data_sources()->mutable_config();
+  ds_config->set_name("linux.perf");
+  ds_config->set_perf_event_config_raw(perf_config.SerializeAsString());
+
+  // Collect trace.
+  base::TestTaskRunner task_runner;
+  auto packets = CollectTrace(&task_runner, trace_config);
+
+  int target_pid_after = PidForProcessName("/system/bin/traced_probes");
+  ASSERT_EQ(target_pid, target_pid_after) << "traced_probes died during test";
+
+  if (!IsUserBuild())
+    AssertHasSampledStacksForPid(packets, target_pid);
+  else
+    AssertNoStacksForPid(packets, target_pid);
+}
+
 }  // namespace
 }  // namespace perfetto