Commit d84a78f8 authored by Stephen Nusko's avatar Stephen Nusko Committed by Commit Bot

Enable system perfetto producer on all posix builds.

It remains disabled by default by the tracing flag
--enable-features=kEnablePerfettoSystemTracing, but when enabled will
attempt to connect to the result of GetProducerSocket().

This will be useful for people who want system wide tracing on linux
from crashed browser processes since the tracing service will be alive
even after Chrome dies.

Bug: 1007310
Change-Id: I62e67bcdc9ff720da2a59497a6d3ebf855413b8f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1953718
Auto-Submit: Stephen Nusko <nuskos@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#734144}
parent 95dc5cee
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
#include "services/tracing/public/cpp/tracing_features.h" #include "services/tracing/public/cpp/tracing_features.h"
#include "third_party/zlib/zlib.h" #include "third_party/zlib/zlib.h"
#if defined(OS_ANDROID) #if defined(OS_POSIX)
// Used by PerfettoSystemBackgroundScenario, nogncheck is required because this // Used by PerfettoSystemBackgroundScenario, nogncheck is required because this
// is only included and used on Android. // is only included and used on Android.
#include "services/tracing/perfetto/system_test_utils.h" // nogncheck #include "services/tracing/perfetto/system_test_utils.h" // nogncheck
...@@ -65,7 +65,7 @@ perfetto::TraceConfig StopTracingTriggerConfig( ...@@ -65,7 +65,7 @@ perfetto::TraceConfig StopTracingTriggerConfig(
} }
void SetSystemProducerSocketAndChecksAsync(const std::string& producer_socket) { void SetSystemProducerSocketAndChecksAsync(const std::string& producer_socket) {
// We need to let the AndroidSystemProducer know the MockSystemService socket // We need to let the PosixSystemProducer know the MockSystemService socket
// address and that if we're running on Android devices older then Pie to // address and that if we're running on Android devices older then Pie to
// still connect. // still connect.
tracing::PerfettoTracedProcess::GetTaskRunner() tracing::PerfettoTracedProcess::GetTaskRunner()
...@@ -75,12 +75,12 @@ void SetSystemProducerSocketAndChecksAsync(const std::string& producer_socket) { ...@@ -75,12 +75,12 @@ void SetSystemProducerSocketAndChecksAsync(const std::string& producer_socket) {
base::BindOnce( base::BindOnce(
[](const std::string& producer_socket) { [](const std::string& producer_socket) {
// The only other type of system producer is // The only other type of system producer is
// AndroidSystemProducer so this assert ensures that the // PosixSystemProducer so this assert ensures that the
// static_cast below is safe. // static_cast below is safe.
ASSERT_FALSE(tracing::PerfettoTracedProcess::Get() ASSERT_FALSE(tracing::PerfettoTracedProcess::Get()
->SystemProducerForTesting() ->SystemProducerForTesting()
->IsDummySystemProducerForTesting()); ->IsDummySystemProducerForTesting());
auto* producer = static_cast<tracing::AndroidSystemProducer*>( auto* producer = static_cast<tracing::PosixSystemProducer*>(
tracing::PerfettoTracedProcess::Get() tracing::PerfettoTracedProcess::Get()
->SystemProducerForTesting()); ->SystemProducerForTesting());
producer->SetNewSocketForTesting(producer_socket.c_str()); producer->SetNewSocketForTesting(producer_socket.c_str());
...@@ -104,7 +104,7 @@ std::unique_ptr<tracing::MockConsumer> CreateDefaultConsumer( ...@@ -104,7 +104,7 @@ std::unique_ptr<tracing::MockConsumer> CreateDefaultConsumer(
} }
} // namespace } // namespace
} // namespace content } // namespace content
#endif // defined(OS_ANDROID) #endif // defined(OS_POSIX)
using base::trace_event::TraceLog; using base::trace_event::TraceLog;
...@@ -1698,7 +1698,7 @@ IN_PROC_BROWSER_TEST_F(ProtoBackgroundTracingTest, ProtoTraceReceived) { ...@@ -1698,7 +1698,7 @@ IN_PROC_BROWSER_TEST_F(ProtoBackgroundTracingTest, ProtoTraceReceived) {
EXPECT_TRUE(checker.stats().has_interned_source_locations); EXPECT_TRUE(checker.stats().has_interned_source_locations);
} }
#if defined(OS_ANDROID) #if defined(OS_POSIX)
IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
PerfettoSystemBackgroundScenarioDefaultName) { PerfettoSystemBackgroundScenarioDefaultName) {
// This test will ensure that a BackgroundTracing scenario set to SYSTEM mode // This test will ensure that a BackgroundTracing scenario set to SYSTEM mode
...@@ -1832,6 +1832,6 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -1832,6 +1832,6 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
// received we won't see any packets and |received_packets()| will be 0. // received we won't see any packets and |received_packets()| will be 0.
EXPECT_LT(0u, system_consumer->received_packets()); EXPECT_LT(0u, system_consumer->received_packets());
} }
#endif // defined(OS_ANDROID) #endif // defined(OS_POSIX)
} // namespace content } // namespace content
...@@ -1313,7 +1313,6 @@ test("content_browsertests") { ...@@ -1313,7 +1313,6 @@ test("content_browsertests") {
"//content/shell:content_shell_lib", "//content/shell:content_shell_lib",
"//content/shell/android:content_shell_assets", "//content/shell/android:content_shell_assets",
"//content/shell/android:content_shell_jni_headers", "//content/shell/android:content_shell_jni_headers",
"//services/tracing:test_utils",
"//testing/android/native_test:native_test_support", "//testing/android/native_test:native_test_support",
"//ui/android:android", "//ui/android:android",
"//ui/touch_selection:touch_selection", "//ui/touch_selection:touch_selection",
...@@ -1337,6 +1336,9 @@ test("content_browsertests") { ...@@ -1337,6 +1336,9 @@ test("content_browsertests") {
] ]
deps += [ "//content/browser/speech/proto" ] deps += [ "//content/browser/speech/proto" ]
} }
if (is_posix) {
deps += [ "//services/tracing:test_utils" ]
}
# HID support is not available without udev. # HID support is not available without udev.
is_linux_without_udev = is_linux && !use_udev is_linux_without_udev = is_linux && !use_udev
...@@ -2060,6 +2062,7 @@ test("content_unittests") { ...@@ -2060,6 +2062,7 @@ test("content_unittests") {
if (is_posix) { if (is_posix) {
sources += [ "../browser/posix_file_descriptor_info_impl_unittest.cc" ] sources += [ "../browser/posix_file_descriptor_info_impl_unittest.cc" ]
deps += [ "//services/tracing:test_utils" ]
} }
if (enable_plugins) { if (enable_plugins) {
......
...@@ -66,13 +66,11 @@ source_set("test_utils") { ...@@ -66,13 +66,11 @@ source_set("test_utils") {
"//third_party/perfetto/protos/perfetto/trace:lite", "//third_party/perfetto/protos/perfetto/trace:lite",
] ]
if (is_android) { if (is_posix) {
sources += [ sources += [
"perfetto/system_test_utils.cc", "perfetto/system_test_utils.cc",
"perfetto/system_test_utils.h", "perfetto/system_test_utils.h",
] ]
deps += [ "//third_party/perfetto/src/tracing:ipc" ]
} }
} }
...@@ -110,9 +108,8 @@ source_set("tests") { ...@@ -110,9 +108,8 @@ source_set("tests") {
"//third_party/perfetto/protos/perfetto/trace/track_event:lite", "//third_party/perfetto/protos/perfetto/trace/track_event:lite",
] ]
if (is_android) { if (is_posix) {
sources += [ "perfetto/system_perfetto_unittest.cc" ] sources += [ "perfetto/system_perfetto_unittest.cc" ]
deps += [ "//third_party/perfetto/include/perfetto/ext/tracing/ipc" ]
if (can_unwind_with_cfi_table && is_official_build) { if (can_unwind_with_cfi_table && is_official_build) {
sources += sources +=
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include <thread> #include <thread>
#include <utility> #include <utility>
#include "base/android/build_info.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
...@@ -20,6 +19,7 @@ ...@@ -20,6 +19,7 @@
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h" #include "base/test/task_environment.h"
#include "build/build_config.h"
#include "services/tracing/perfetto/perfetto_service.h" #include "services/tracing/perfetto/perfetto_service.h"
#include "services/tracing/perfetto/producer_host.h" #include "services/tracing/perfetto/producer_host.h"
#include "services/tracing/perfetto/system_test_utils.h" #include "services/tracing/perfetto/system_test_utils.h"
...@@ -33,6 +33,10 @@ ...@@ -33,6 +33,10 @@
#include "third_party/perfetto/protos/perfetto/config/trace_config.pb.h" #include "third_party/perfetto/protos/perfetto/config/trace_config.pb.h"
#include "third_party/perfetto/protos/perfetto/trace/trace.pb.h" #include "third_party/perfetto/protos/perfetto/trace/trace.pb.h"
#if defined(OS_ANDROID)
#include "base/android/build_info.h" // nogncheck
#endif // defined(OS_ANDROID)
namespace tracing { namespace tracing {
namespace { namespace {
...@@ -114,22 +118,22 @@ class SystemPerfettoTest : public testing::Test { ...@@ -114,22 +118,22 @@ class SystemPerfettoTest : public testing::Test {
RunUntilIdle(); RunUntilIdle();
} }
std::unique_ptr<MockAndroidSystemProducer> CreateMockAndroidSystemProducer( std::unique_ptr<MockPosixSystemProducer> CreateMockPosixSystemProducer(
MockSystemService* service, MockSystemService* service,
int num_data_sources_expected = 0, int num_data_sources_expected = 0,
base::RunLoop* system_data_source_enabled_runloop = nullptr, base::RunLoop* system_data_source_enabled_runloop = nullptr,
base::RunLoop* system_data_source_disabled_runloop = nullptr, base::RunLoop* system_data_source_disabled_runloop = nullptr,
bool check_sdk_level = false) { bool check_sdk_level = false) {
std::unique_ptr<MockAndroidSystemProducer> result; std::unique_ptr<MockPosixSystemProducer> result;
base::RunLoop loop_finished; base::RunLoop loop_finished;
// When we construct a MockAndroidSystemProducer it needs to be on the // When we construct a MockPosixSystemProducer it needs to be on the
// correct sequence. Construct it on the task runner and wait on the // correct sequence. Construct it on the task runner and wait on the
// |loop_finished| to ensure it is completely set up. // |loop_finished| to ensure it is completely set up.
PerfettoTracedProcess::GetTaskRunner() PerfettoTracedProcess::GetTaskRunner()
->GetOrCreateTaskRunner() ->GetOrCreateTaskRunner()
->PostTaskAndReply( ->PostTaskAndReply(
FROM_HERE, base::BindLambdaForTesting([&]() { FROM_HERE, base::BindLambdaForTesting([&]() {
result.reset(new MockAndroidSystemProducer( result.reset(new MockPosixSystemProducer(
service->producer(), check_sdk_level, service->producer(), check_sdk_level,
num_data_sources_expected, num_data_sources_expected,
system_data_source_enabled_runloop system_data_source_enabled_runloop
...@@ -235,7 +239,7 @@ TEST_F(SystemPerfettoTest, SystemTraceEndToEnd) { ...@@ -235,7 +239,7 @@ TEST_F(SystemPerfettoTest, SystemTraceEndToEnd) {
// Set up the producer to talk to the system. // Set up the producer to talk to the system.
base::RunLoop system_data_source_enabled_runloop; base::RunLoop system_data_source_enabled_runloop;
base::RunLoop system_data_source_disabled_runloop; base::RunLoop system_data_source_disabled_runloop;
auto system_producer = CreateMockAndroidSystemProducer( auto system_producer = CreateMockPosixSystemProducer(
system_service.get(), system_service.get(),
/* num_data_sources = */ 1, &system_data_source_enabled_runloop, /* num_data_sources = */ 1, &system_data_source_enabled_runloop,
&system_data_source_disabled_runloop); &system_data_source_disabled_runloop);
...@@ -269,52 +273,6 @@ TEST_F(SystemPerfettoTest, SystemTraceEndToEnd) { ...@@ -269,52 +273,6 @@ TEST_F(SystemPerfettoTest, SystemTraceEndToEnd) {
PerfettoProducer::DeleteSoonForTesting(std::move(system_producer)); PerfettoProducer::DeleteSoonForTesting(std::move(system_producer));
} }
// TODO(crbug/964324): We need to run this test in permissive mode, but
// currently the bots don't do that. We should switch this to a telemetry
// test to ensure our integration works on P+ Android devices.
TEST_F(SystemPerfettoTest, DISABLED_SystemTraceEndToEndRealService) {
if (base::android::BuildInfo::GetInstance()->sdk_int() <
base::android::SDK_VERSION_P) {
LOG(INFO)
<< "Skipping SystemTraceEndToEndRealService test, this phone "
<< "is pre P SDK, which means there is no 'real' service running.";
return;
}
perfetto::protos::TraceConfig trace_config;
trace_config.add_buffers()->set_size_kb(1024);
trace_config.add_data_sources()->mutable_config()->set_name(
data_sources_[0]->name());
trace_config.add_data_sources()->mutable_config()->set_name(
data_sources_[2]->name());
trace_config.set_duration_ms(100);
std::string path = "/data/misc/perfetto-traces/trace";
path += RandomASCII(16);
EXPECT_TRUE(
ExecPerfetto({"-o", path, "-c"}, trace_config.SerializeAsString()))
<< "failed with stderr: \"" << stderr_ << "\"";
char* data = new char[1024 * 1024];
ASSERT_TRUE(data);
int num_bytes = base::ReadFile(base::FilePath(path), data, (1024 * 1024) - 1);
EXPECT_NE(num_bytes, -1);
std::string output(data, num_bytes);
delete[] data;
perfetto::protos::Trace trace;
EXPECT_TRUE(trace.ParseFromString(output));
int count = 0;
for (const auto& packet : trace.packet()) {
if (packet.has_for_testing()) {
++count;
}
}
EXPECT_EQ(1 + 7, count);
EXPECT_EQ(0, remove(path.c_str()));
}
TEST_F(SystemPerfettoTest, OneSystemSourceWithMultipleLocalSources) { TEST_F(SystemPerfettoTest, OneSystemSourceWithMultipleLocalSources) {
auto system_service = CreateMockSystemService(); auto system_service = CreateMockSystemService();
...@@ -330,7 +288,7 @@ TEST_F(SystemPerfettoTest, OneSystemSourceWithMultipleLocalSources) { ...@@ -330,7 +288,7 @@ TEST_F(SystemPerfettoTest, OneSystemSourceWithMultipleLocalSources) {
base::RunLoop system_data_source_enabled_runloop; base::RunLoop system_data_source_enabled_runloop;
base::RunLoop system_data_source_disabled_runloop; base::RunLoop system_data_source_disabled_runloop;
auto system_producer = CreateMockAndroidSystemProducer( auto system_producer = CreateMockPosixSystemProducer(
system_service.get(), system_service.get(),
/* num_data_sources = */ 1, &system_data_source_enabled_runloop, /* num_data_sources = */ 1, &system_data_source_enabled_runloop,
&system_data_source_disabled_runloop); &system_data_source_disabled_runloop);
...@@ -472,7 +430,7 @@ TEST_F(SystemPerfettoTest, MultipleSystemSourceWithOneLocalSourcesLocalFirst) { ...@@ -472,7 +430,7 @@ TEST_F(SystemPerfettoTest, MultipleSystemSourceWithOneLocalSourcesLocalFirst) {
base::RunLoop system_data_source_enabled_runloop; base::RunLoop system_data_source_enabled_runloop;
base::RunLoop system_data_source_disabled_runloop; base::RunLoop system_data_source_disabled_runloop;
auto system_producer = CreateMockAndroidSystemProducer( auto system_producer = CreateMockPosixSystemProducer(
system_service.get(), system_service.get(),
/* num_data_sources = */ 3, &system_data_source_enabled_runloop, /* num_data_sources = */ 3, &system_data_source_enabled_runloop,
&system_data_source_disabled_runloop); &system_data_source_disabled_runloop);
...@@ -544,7 +502,7 @@ TEST_F(SystemPerfettoTest, MultipleSystemAndLocalSources) { ...@@ -544,7 +502,7 @@ TEST_F(SystemPerfettoTest, MultipleSystemAndLocalSources) {
base::RunLoop system_data_source_enabled_runloop; base::RunLoop system_data_source_enabled_runloop;
base::RunLoop system_data_source_disabled_runloop; base::RunLoop system_data_source_disabled_runloop;
auto system_producer = CreateMockAndroidSystemProducer( auto system_producer = CreateMockPosixSystemProducer(
system_service.get(), system_service.get(),
/* num_data_sources = */ 3, &system_data_source_enabled_runloop, /* num_data_sources = */ 3, &system_data_source_enabled_runloop,
&system_data_source_disabled_runloop); &system_data_source_disabled_runloop);
...@@ -629,7 +587,7 @@ TEST_F(SystemPerfettoTest, MultipleSystemAndLocalSourcesLocalFirst) { ...@@ -629,7 +587,7 @@ TEST_F(SystemPerfettoTest, MultipleSystemAndLocalSourcesLocalFirst) {
// trace starts. // trace starts.
base::RunLoop system_data_source_enabled_runloop; base::RunLoop system_data_source_enabled_runloop;
base::RunLoop system_data_source_disabled_runloop; base::RunLoop system_data_source_disabled_runloop;
auto system_producer = CreateMockAndroidSystemProducer( auto system_producer = CreateMockPosixSystemProducer(
system_service.get(), system_service.get(),
/* num_data_sources = */ 3, &system_data_source_enabled_runloop, /* num_data_sources = */ 3, &system_data_source_enabled_runloop,
&system_data_source_disabled_runloop); &system_data_source_disabled_runloop);
...@@ -727,6 +685,7 @@ TEST_F(SystemPerfettoTest, MultipleSystemAndLocalSourcesLocalFirst) { ...@@ -727,6 +685,7 @@ TEST_F(SystemPerfettoTest, MultipleSystemAndLocalSourcesLocalFirst) {
PerfettoProducer::DeleteSoonForTesting(std::move(system_producer)); PerfettoProducer::DeleteSoonForTesting(std::move(system_producer));
} }
#if defined(OS_ANDROID)
TEST_F(SystemPerfettoTest, SystemToLowAPILevel) { TEST_F(SystemPerfettoTest, SystemToLowAPILevel) {
if (base::android::BuildInfo::GetInstance()->sdk_int() >= if (base::android::BuildInfo::GetInstance()->sdk_int() >=
base::android::SDK_VERSION_P) { base::android::SDK_VERSION_P) {
...@@ -763,7 +722,7 @@ TEST_F(SystemPerfettoTest, SystemToLowAPILevel) { ...@@ -763,7 +722,7 @@ TEST_F(SystemPerfettoTest, SystemToLowAPILevel) {
base::RunLoop system_data_source_enabled_runloop; base::RunLoop system_data_source_enabled_runloop;
base::RunLoop system_data_source_disabled_runloop; base::RunLoop system_data_source_disabled_runloop;
auto system_producer = CreateMockAndroidSystemProducer( auto system_producer = CreateMockPosixSystemProducer(
system_service.get(), system_service.get(),
/* num_data_sources = */ 1, &system_data_source_enabled_runloop, /* num_data_sources = */ 1, &system_data_source_enabled_runloop,
&system_data_source_disabled_runloop, check_sdk_level); &system_data_source_disabled_runloop, check_sdk_level);
...@@ -817,8 +776,10 @@ TEST_F(SystemPerfettoTest, EnabledOnDebugBuilds) { ...@@ -817,8 +776,10 @@ TEST_F(SystemPerfettoTest, EnabledOnDebugBuilds) {
->IsDummySystemProducerForTesting()); ->IsDummySystemProducerForTesting());
} }
} }
#endif // defined(OS_ANDROID)
TEST_F(SystemPerfettoTest, RespectsFeatureList) { TEST_F(SystemPerfettoTest, RespectsFeatureList) {
#if defined(OS_ANDROID)
if (base::android::BuildInfo::GetInstance()->is_debug_android()) { if (base::android::BuildInfo::GetInstance()->is_debug_android()) {
// The feature list is ignored on debug android builds so we should have a // The feature list is ignored on debug android builds so we should have a
// real system producer so just bail out of this test. // real system producer so just bail out of this test.
...@@ -827,6 +788,7 @@ TEST_F(SystemPerfettoTest, RespectsFeatureList) { ...@@ -827,6 +788,7 @@ TEST_F(SystemPerfettoTest, RespectsFeatureList) {
->IsDummySystemProducerForTesting()); ->IsDummySystemProducerForTesting());
return; return;
} }
#endif // defined(OS_ANDROID)
{ {
base::test::ScopedFeatureList feature_list; base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(features::kEnablePerfettoSystemTracing); feature_list.InitAndEnableFeature(features::kEnablePerfettoSystemTracing);
......
...@@ -82,14 +82,14 @@ perfetto::TracingService* MockSystemService::GetService() { ...@@ -82,14 +82,14 @@ perfetto::TracingService* MockSystemService::GetService() {
return service_->service(); return service_->service();
} }
MockAndroidSystemProducer::MockAndroidSystemProducer( MockPosixSystemProducer::MockPosixSystemProducer(
const std::string& socket, const std::string& socket,
bool check_sdk_level, bool check_sdk_level,
uint32_t num_data_sources, uint32_t num_data_sources,
base::OnceClosure data_source_enabled_callback, base::OnceClosure data_source_enabled_callback,
base::OnceClosure data_source_disabled_callback) base::OnceClosure data_source_disabled_callback)
: AndroidSystemProducer(socket.c_str(), : PosixSystemProducer(socket.c_str(),
PerfettoTracedProcess::Get()->GetTaskRunner()), PerfettoTracedProcess::Get()->GetTaskRunner()),
num_data_sources_expected_(num_data_sources), num_data_sources_expected_(num_data_sources),
data_source_enabled_callback_(std::move(data_source_enabled_callback)), data_source_enabled_callback_(std::move(data_source_enabled_callback)),
data_source_disabled_callback_(std::move(data_source_disabled_callback)) { data_source_disabled_callback_(std::move(data_source_disabled_callback)) {
...@@ -97,7 +97,7 @@ MockAndroidSystemProducer::MockAndroidSystemProducer( ...@@ -97,7 +97,7 @@ MockAndroidSystemProducer::MockAndroidSystemProducer(
// ownership of ourselves to PerfettoTracedProcess. Since someone else manages // ownership of ourselves to PerfettoTracedProcess. Since someone else manages
// our deletion we need to be careful in the deconstructor to not double free // our deletion we need to be careful in the deconstructor to not double free
// ourselves (so we must call release once we get back our pointer. // ourselves (so we must call release once we get back our pointer.
std::unique_ptr<MockAndroidSystemProducer> client; std::unique_ptr<MockPosixSystemProducer> client;
client.reset(this); client.reset(this);
old_producer_ = PerfettoTracedProcess::Get()->SetSystemProducerForTesting( old_producer_ = PerfettoTracedProcess::Get()->SetSystemProducerForTesting(
std::move(client)); std::move(client));
...@@ -105,7 +105,7 @@ MockAndroidSystemProducer::MockAndroidSystemProducer( ...@@ -105,7 +105,7 @@ MockAndroidSystemProducer::MockAndroidSystemProducer(
Connect(); Connect();
} }
MockAndroidSystemProducer::~MockAndroidSystemProducer() { MockPosixSystemProducer::~MockPosixSystemProducer() {
// See comment in the constructor. // See comment in the constructor.
auto client = PerfettoTracedProcess::Get()->SetSystemProducerForTesting( auto client = PerfettoTracedProcess::Get()->SetSystemProducerForTesting(
std::move(old_producer_)); std::move(old_producer_));
...@@ -113,10 +113,10 @@ MockAndroidSystemProducer::~MockAndroidSystemProducer() { ...@@ -113,10 +113,10 @@ MockAndroidSystemProducer::~MockAndroidSystemProducer() {
client.release(); client.release();
} }
void MockAndroidSystemProducer::StartDataSource( void MockPosixSystemProducer::StartDataSource(
perfetto::DataSourceInstanceID id, perfetto::DataSourceInstanceID id,
const perfetto::DataSourceConfig& data_source_config) { const perfetto::DataSourceConfig& data_source_config) {
AndroidSystemProducer::StartDataSource(id, data_source_config); PosixSystemProducer::StartDataSource(id, data_source_config);
CHECK_LT(num_data_sources_active_, num_data_sources_expected_); CHECK_LT(num_data_sources_active_, num_data_sources_expected_);
if (++num_data_sources_active_ == num_data_sources_expected_ && if (++num_data_sources_active_ == num_data_sources_expected_ &&
data_source_enabled_callback_) { data_source_enabled_callback_) {
...@@ -124,27 +124,27 @@ void MockAndroidSystemProducer::StartDataSource( ...@@ -124,27 +124,27 @@ void MockAndroidSystemProducer::StartDataSource(
} }
} }
void MockAndroidSystemProducer::StopDataSource( void MockPosixSystemProducer::StopDataSource(
perfetto::DataSourceInstanceID id) { perfetto::DataSourceInstanceID id) {
AndroidSystemProducer::StopDataSource(id); PosixSystemProducer::StopDataSource(id);
CHECK_GT(num_data_sources_active_, 0u); CHECK_GT(num_data_sources_active_, 0u);
if (--num_data_sources_active_ == 0 && data_source_disabled_callback_) { if (--num_data_sources_active_ == 0 && data_source_disabled_callback_) {
std::move(data_source_disabled_callback_).Run(); std::move(data_source_disabled_callback_).Run();
} }
} }
void MockAndroidSystemProducer::CommitData( void MockPosixSystemProducer::CommitData(
const perfetto::CommitDataRequest& commit, const perfetto::CommitDataRequest& commit,
CommitDataCallback callback) { CommitDataCallback callback) {
AndroidSystemProducer::CommitData(commit, callback); PosixSystemProducer::CommitData(commit, callback);
} }
void MockAndroidSystemProducer::SetDataSourceEnabledCallback( void MockPosixSystemProducer::SetDataSourceEnabledCallback(
base::OnceClosure data_source_enabled_callback) { base::OnceClosure data_source_enabled_callback) {
data_source_enabled_callback_ = std::move(data_source_enabled_callback); data_source_enabled_callback_ = std::move(data_source_enabled_callback);
} }
void MockAndroidSystemProducer::SetDataSourceDisabledCallback( void MockPosixSystemProducer::SetDataSourceDisabledCallback(
base::OnceClosure data_source_disabled_callback) { base::OnceClosure data_source_disabled_callback) {
data_source_disabled_callback_ = std::move(data_source_disabled_callback); data_source_disabled_callback_ = std::move(data_source_disabled_callback);
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include <string> #include <string>
#include "services/tracing/public/cpp/perfetto/android_system_producer.h" #include "services/tracing/public/cpp/perfetto/posix_system_producer.h"
namespace base { namespace base {
class ScopedTempDir; class ScopedTempDir;
...@@ -45,16 +45,16 @@ class MockSystemService { ...@@ -45,16 +45,16 @@ class MockSystemService {
std::unique_ptr<perfetto::base::TaskRunner> task_runner_; std::unique_ptr<perfetto::base::TaskRunner> task_runner_;
}; };
class MockAndroidSystemProducer : public AndroidSystemProducer { class MockPosixSystemProducer : public PosixSystemProducer {
public: public:
MockAndroidSystemProducer( MockPosixSystemProducer(
const std::string& socket, const std::string& socket,
bool check_sdk_level = false, bool check_sdk_level = false,
uint32_t num_data_sources = 0, uint32_t num_data_sources = 0,
base::OnceClosure data_source_enabled_callback = base::OnceClosure(), base::OnceClosure data_source_enabled_callback = base::OnceClosure(),
base::OnceClosure data_source_disabled_callback = base::OnceClosure()); base::OnceClosure data_source_disabled_callback = base::OnceClosure());
~MockAndroidSystemProducer() override; ~MockPosixSystemProducer() override;
void StartDataSource( void StartDataSource(
perfetto::DataSourceInstanceID id, perfetto::DataSourceInstanceID id,
......
...@@ -117,12 +117,11 @@ target(tracing_lib_type, "cpp") { ...@@ -117,12 +117,11 @@ target(tracing_lib_type, "cpp") {
] ]
# Add the SystemProducer implementations. # Add the SystemProducer implementations.
if (is_android) { if (is_posix) {
sources += [ sources += [
"perfetto/android_system_producer.cc", "perfetto/posix_system_producer.cc",
"perfetto/android_system_producer.h", "perfetto/posix_system_producer.h",
] ]
deps += [ "//third_party/perfetto/src/tracing:ipc" ]
} }
if (is_android && can_unwind_with_cfi_table && is_official_build) { if (is_android && can_unwind_with_cfi_table && is_official_build) {
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#include "services/tracing/public/cpp/trace_startup.h" #include "services/tracing/public/cpp/trace_startup.h"
#include "services/tracing/public/cpp/tracing_features.h" #include "services/tracing/public/cpp/tracing_features.h"
#if defined(OS_ANDROID) #if defined(OS_POSIX)
// As per 'gn help check': // As per 'gn help check':
/* /*
If you have conditional includes, make sure the build conditions and the If you have conditional includes, make sure the build conditions and the
...@@ -24,29 +24,29 @@ ...@@ -24,29 +24,29 @@
*/ */
// We add the nogncheck to ensure this doesn't trigger incorrect errors on // We add the nogncheck to ensure this doesn't trigger incorrect errors on
// non-android builds. // non-android builds.
#include "services/tracing/public/cpp/perfetto/android_system_producer.h" // nogncheck #include "services/tracing/public/cpp/perfetto/posix_system_producer.h" // nogncheck
#include "third_party/perfetto/include/perfetto/ext/tracing/ipc/default_socket.h" // nogncheck #include "third_party/perfetto/include/perfetto/ext/tracing/ipc/default_socket.h" // nogncheck
#endif // defined(OS_ANDROID) #endif // defined(OS_POSIX)
namespace tracing { namespace tracing {
namespace { namespace {
std::unique_ptr<SystemProducer> NewSystemProducer(PerfettoTaskRunner* runner, std::unique_ptr<SystemProducer> NewSystemProducer(PerfettoTaskRunner* runner,
const char* socket_name) { const char* socket_name) {
#if defined(OS_ANDROID) #if defined(OS_POSIX)
if (ShouldSetupSystemTracing()) { if (ShouldSetupSystemTracing()) {
DCHECK(socket_name); DCHECK(socket_name);
return std::make_unique<AndroidSystemProducer>(socket_name, runner); return std::make_unique<PosixSystemProducer>(socket_name, runner);
} }
#endif // defined(OS_ANDROID) #endif // defined(OS_POSIX)
return std::make_unique<DummyProducer>(runner); return std::make_unique<DummyProducer>(runner);
} }
const char* MaybeSocket() { const char* MaybeSocket() {
#if defined(OS_ANDROID) #if defined(OS_POSIX)
return perfetto::GetProducerSocket(); return perfetto::GetProducerSocket();
#else #else
return nullptr; return nullptr;
#endif // defined(OS_ANDROID) #endif // defined(OS_POSIX)
} }
} // namespace } // namespace
...@@ -217,7 +217,7 @@ bool PerfettoTracedProcess::CanStartTracing( ...@@ -217,7 +217,7 @@ bool PerfettoTracedProcess::CanStartTracing(
} }
} else { } else {
// In tests this is possible due to the periodic polling of CanStartTracing // In tests this is possible due to the periodic polling of CanStartTracing
// by the AndroidSystemProducer, when we swap it out for a // by the PosixSystemProducer, when we swap it out for a
// MockSystemProducer there can be three PerfettoProducers calling this // MockSystemProducer there can be three PerfettoProducers calling this
// function. In production nothing ever calls the // function. In production nothing ever calls the
// |Set.*ProducerForTesting()| functions so this should never be reached. // |Set.*ProducerForTesting()| functions so this should never be reached.
......
...@@ -2,15 +2,15 @@ ...@@ -2,15 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "services/tracing/public/cpp/perfetto/android_system_producer.h" #include "services/tracing/public/cpp/perfetto/posix_system_producer.h"
#include <utility> #include <utility>
#include "base/android/build_info.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/trace_event/trace_log.h" #include "base/trace_event/trace_log.h"
#include "build/build_config.h"
#include "services/tracing/public/cpp/perfetto/shared_memory.h" #include "services/tracing/public/cpp/perfetto/shared_memory.h"
#include "services/tracing/public/cpp/traced_process_impl.h" #include "services/tracing/public/cpp/traced_process_impl.h"
#include "third_party/perfetto/include/perfetto/ext/tracing/core/commit_data_request.h" #include "third_party/perfetto/include/perfetto/ext/tracing/core/commit_data_request.h"
...@@ -22,6 +22,10 @@ ...@@ -22,6 +22,10 @@
#include "third_party/perfetto/include/perfetto/protozero/scattered_stream_writer.h" #include "third_party/perfetto/include/perfetto/protozero/scattered_stream_writer.h"
#include "third_party/perfetto/protos/perfetto/common/track_event_descriptor.pbzero.h" #include "third_party/perfetto/protos/perfetto/common/track_event_descriptor.pbzero.h"
#if defined(OS_ANDROID)
#include "base/android/build_info.h"
#endif // defined(OS_ANDROID)
namespace tracing { namespace tracing {
namespace { namespace {
constexpr uint32_t kInitialConnectionBackoffMs = 100; constexpr uint32_t kInitialConnectionBackoffMs = 100;
...@@ -45,19 +49,19 @@ uint32_t IncreaseBackoff(uint32_t current, uint32_t max) { ...@@ -45,19 +49,19 @@ uint32_t IncreaseBackoff(uint32_t current, uint32_t max) {
} }
} // namespace } // namespace
AndroidSystemProducer::AndroidSystemProducer(const char* socket, PosixSystemProducer::PosixSystemProducer(const char* socket,
PerfettoTaskRunner* task_runner) PerfettoTaskRunner* task_runner)
: SystemProducer(task_runner), : SystemProducer(task_runner),
socket_name_(socket), socket_name_(socket),
connection_backoff_ms_(kInitialConnectionBackoffMs) { connection_backoff_ms_(kInitialConnectionBackoffMs) {
Connect(); Connect();
} }
AndroidSystemProducer::~AndroidSystemProducer() { PosixSystemProducer::~PosixSystemProducer() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
} }
void AndroidSystemProducer::SetDisallowPreAndroidPieForTesting(bool disallow) { void PosixSystemProducer::SetDisallowPreAndroidPieForTesting(bool disallow) {
disallow_pre_android_pie = disallow; disallow_pre_android_pie = disallow;
if (!disallow && state_ == State::kUninitialized) { if (!disallow && state_ == State::kUninitialized) {
// If previously we would not have connected, we now attempt to connect // If previously we would not have connected, we now attempt to connect
...@@ -66,12 +70,12 @@ void AndroidSystemProducer::SetDisallowPreAndroidPieForTesting(bool disallow) { ...@@ -66,12 +70,12 @@ void AndroidSystemProducer::SetDisallowPreAndroidPieForTesting(bool disallow) {
} }
} }
void AndroidSystemProducer::SetNewSocketForTesting(const char* socket) { void PosixSystemProducer::SetNewSocketForTesting(const char* socket) {
socket_name_ = socket; socket_name_ = socket;
if (state_ == State::kConnected) { if (state_ == State::kConnected) {
// If we are fully connected we need to reset the service before we // If we are fully connected we need to reset the service before we
// reconnect. // reconnect.
DisconnectWithReply(base::BindOnce(&AndroidSystemProducer::OnDisconnect, DisconnectWithReply(base::BindOnce(&PosixSystemProducer::OnDisconnect,
base::Unretained(this))); base::Unretained(this)));
} else { } else {
// In any other case we just need to do a normal disconnect and // In any other case we just need to do a normal disconnect and
...@@ -81,21 +85,21 @@ void AndroidSystemProducer::SetNewSocketForTesting(const char* socket) { ...@@ -81,21 +85,21 @@ void AndroidSystemProducer::SetNewSocketForTesting(const char* socket) {
} }
} }
void AndroidSystemProducer::ResetSequenceForTesting() { void PosixSystemProducer::ResetSequenceForTesting() {
// DETACH the sequence and then immediately attach it. This is needed in tests // DETACH the sequence and then immediately attach it. This is needed in tests
// because we might be executing in a TaskEnvironment, but the global // because we might be executing in a TaskEnvironment, but the global
// PerfettoTracedProcess (which contains a pointer to AndroidSystemProducer) // PerfettoTracedProcess (which contains a pointer to PosixSystemProducer)
// will leak between tests, but the sequence will no longer be valid. // will leak between tests, but the sequence will no longer be valid.
DETACH_FROM_SEQUENCE(sequence_checker_); DETACH_FROM_SEQUENCE(sequence_checker_);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
} }
bool AndroidSystemProducer::IsTracingActive() { bool PosixSystemProducer::IsTracingActive() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return data_sources_tracing_ > 0; return data_sources_tracing_ > 0;
} }
void AndroidSystemProducer::NewDataSourceAdded( void PosixSystemProducer::NewDataSourceAdded(
const PerfettoTracedProcess::DataSourceBase* const data_source) { const PerfettoTracedProcess::DataSourceBase* const data_source) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (state_ != State::kConnected) { if (state_ != State::kConnected) {
...@@ -127,7 +131,7 @@ void AndroidSystemProducer::NewDataSourceAdded( ...@@ -127,7 +131,7 @@ void AndroidSystemProducer::NewDataSourceAdded(
service_->RegisterDataSource(new_registration); service_->RegisterDataSource(new_registration);
} }
void AndroidSystemProducer::DisconnectWithReply( void PosixSystemProducer::DisconnectWithReply(
base::OnceClosure on_disconnect_complete) { base::OnceClosure on_disconnect_complete) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (state_ == State::kConnected) { if (state_ == State::kConnected) {
...@@ -160,7 +164,7 @@ void AndroidSystemProducer::DisconnectWithReply( ...@@ -160,7 +164,7 @@ void AndroidSystemProducer::DisconnectWithReply(
DelayedReconnect(); DelayedReconnect();
} }
void AndroidSystemProducer::OnConnect() { void PosixSystemProducer::OnConnect() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!PerfettoTracedProcess::Get()->CanStartTracing(this, if (!PerfettoTracedProcess::Get()->CanStartTracing(this,
base::OnceClosure())) { base::OnceClosure())) {
...@@ -175,7 +179,7 @@ void AndroidSystemProducer::OnConnect() { ...@@ -175,7 +179,7 @@ void AndroidSystemProducer::OnConnect() {
} }
} }
void AndroidSystemProducer::OnDisconnect() { void PosixSystemProducer::OnDisconnect() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(service_.get()); DCHECK(service_.get());
// Currently our data sources don't support the concept of the service // Currently our data sources don't support the concept of the service
...@@ -188,7 +192,7 @@ void AndroidSystemProducer::OnDisconnect() { ...@@ -188,7 +192,7 @@ void AndroidSystemProducer::OnDisconnect() {
// |ProducerEndpoint| has finished cleaning up. // |ProducerEndpoint| has finished cleaning up.
task_runner()->GetOrCreateTaskRunner()->PostTask( task_runner()->GetOrCreateTaskRunner()->PostTask(
FROM_HERE, base::BindOnce( FROM_HERE, base::BindOnce(
[](base::WeakPtr<AndroidSystemProducer> weak_ptr) { [](base::WeakPtr<PosixSystemProducer> weak_ptr) {
if (!weak_ptr) { if (!weak_ptr) {
return; return;
} }
...@@ -200,7 +204,7 @@ void AndroidSystemProducer::OnDisconnect() { ...@@ -200,7 +204,7 @@ void AndroidSystemProducer::OnDisconnect() {
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
void AndroidSystemProducer::OnTracingSetup() { void PosixSystemProducer::OnTracingSetup() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// TODO(oysteine): plumb this through the service. // TODO(oysteine): plumb this through the service.
const size_t kShmemBufferPageSize = 4096; const size_t kShmemBufferPageSize = 4096;
...@@ -213,13 +217,13 @@ void AndroidSystemProducer::OnTracingSetup() { ...@@ -213,13 +217,13 @@ void AndroidSystemProducer::OnTracingSetup() {
PerfettoTracedProcess::GetTaskRunner()); PerfettoTracedProcess::GetTaskRunner());
} }
void AndroidSystemProducer::SetupDataSource(perfetto::DataSourceInstanceID, void PosixSystemProducer::SetupDataSource(perfetto::DataSourceInstanceID,
const perfetto::DataSourceConfig&) { const perfetto::DataSourceConfig&) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Always called before StartDataSource but not used for any setup currently. // Always called before StartDataSource but not used for any setup currently.
} }
void AndroidSystemProducer::StartDataSource( void PosixSystemProducer::StartDataSource(
perfetto::DataSourceInstanceID id, perfetto::DataSourceInstanceID id,
const perfetto::DataSourceConfig& config) { const perfetto::DataSourceConfig& config) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
...@@ -229,7 +233,7 @@ void AndroidSystemProducer::StartDataSource( ...@@ -229,7 +233,7 @@ void AndroidSystemProducer::StartDataSource(
auto can_trace = PerfettoTracedProcess::Get()->CanStartTracing( auto can_trace = PerfettoTracedProcess::Get()->CanStartTracing(
this, this,
base::BindOnce( base::BindOnce(
[](base::WeakPtr<AndroidSystemProducer> weak_ptr, [](base::WeakPtr<PosixSystemProducer> weak_ptr,
PerfettoTracedProcess::DataSourceBase* data_source, PerfettoTracedProcess::DataSourceBase* data_source,
perfetto::DataSourceInstanceID id, perfetto::DataSourceInstanceID id,
const perfetto::DataSourceConfig& data_source_config) { const perfetto::DataSourceConfig& data_source_config) {
...@@ -252,13 +256,13 @@ void AndroidSystemProducer::StartDataSource( ...@@ -252,13 +256,13 @@ void AndroidSystemProducer::StartDataSource(
} }
} }
void AndroidSystemProducer::StopDataSource(perfetto::DataSourceInstanceID id) { void PosixSystemProducer::StopDataSource(perfetto::DataSourceInstanceID id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (auto* const data_source : PerfettoTracedProcess::Get()->data_sources()) { for (auto* const data_source : PerfettoTracedProcess::Get()->data_sources()) {
if (data_source->data_source_id() == id && if (data_source->data_source_id() == id &&
data_source->producer() == this) { data_source->producer() == this) {
data_source->StopTracing(base::BindOnce( data_source->StopTracing(base::BindOnce(
[](base::WeakPtr<AndroidSystemProducer> weak_ptr, [](base::WeakPtr<PosixSystemProducer> weak_ptr,
perfetto::DataSourceInstanceID id) { perfetto::DataSourceInstanceID id) {
if (!weak_ptr) { if (!weak_ptr) {
return; return;
...@@ -279,7 +283,7 @@ void AndroidSystemProducer::StopDataSource(perfetto::DataSourceInstanceID id) { ...@@ -279,7 +283,7 @@ void AndroidSystemProducer::StopDataSource(perfetto::DataSourceInstanceID id) {
} }
} }
void AndroidSystemProducer::Flush( void PosixSystemProducer::Flush(
perfetto::FlushRequestID id, perfetto::FlushRequestID id,
const perfetto::DataSourceInstanceID* data_source_ids, const perfetto::DataSourceInstanceID* data_source_ids,
size_t num_data_sources) { size_t num_data_sources) {
...@@ -290,7 +294,7 @@ void AndroidSystemProducer::Flush( ...@@ -290,7 +294,7 @@ void AndroidSystemProducer::Flush(
data_source->data_source_id()) != data_source->data_source_id()) !=
data_source_ids + num_data_sources) { data_source_ids + num_data_sources) {
data_source->Flush(base::BindRepeating( data_source->Flush(base::BindRepeating(
[](base::WeakPtr<AndroidSystemProducer> weak_ptr, [](base::WeakPtr<PosixSystemProducer> weak_ptr,
perfetto::FlushRequestID flush_id) { perfetto::FlushRequestID flush_id) {
if (weak_ptr) { if (weak_ptr) {
weak_ptr->NotifyFlushComplete(flush_id); weak_ptr->NotifyFlushComplete(flush_id);
...@@ -301,7 +305,7 @@ void AndroidSystemProducer::Flush( ...@@ -301,7 +305,7 @@ void AndroidSystemProducer::Flush(
} }
} }
void AndroidSystemProducer::ClearIncrementalState( void PosixSystemProducer::ClearIncrementalState(
const perfetto::DataSourceInstanceID* data_source_ids, const perfetto::DataSourceInstanceID* data_source_ids,
size_t num_data_sources) { size_t num_data_sources) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
...@@ -316,23 +320,22 @@ void AndroidSystemProducer::ClearIncrementalState( ...@@ -316,23 +320,22 @@ void AndroidSystemProducer::ClearIncrementalState(
} }
} }
void AndroidSystemProducer::CommitData( void PosixSystemProducer::CommitData(const perfetto::CommitDataRequest& commit,
const perfetto::CommitDataRequest& commit, CommitDataCallback callback) {
CommitDataCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(service_); DCHECK(service_);
service_->CommitData(commit, std::move(callback)); service_->CommitData(commit, std::move(callback));
} }
perfetto::SharedMemoryArbiter* AndroidSystemProducer::GetSharedMemoryArbiter() { perfetto::SharedMemoryArbiter* PosixSystemProducer::GetSharedMemoryArbiter() {
return shared_memory_arbiter_.get(); return shared_memory_arbiter_.get();
} }
perfetto::SharedMemory* AndroidSystemProducer::shared_memory() const { perfetto::SharedMemory* PosixSystemProducer::shared_memory() const {
return shared_memory_; return shared_memory_;
} }
void AndroidSystemProducer::NotifyFlushComplete(perfetto::FlushRequestID id) { void PosixSystemProducer::NotifyFlushComplete(perfetto::FlushRequestID id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (pending_replies_for_latest_flush_.first != id) { if (pending_replies_for_latest_flush_.first != id) {
// Ignore; completed flush was for an earlier request. // Ignore; completed flush was for an earlier request.
...@@ -345,56 +348,55 @@ void AndroidSystemProducer::NotifyFlushComplete(perfetto::FlushRequestID id) { ...@@ -345,56 +348,55 @@ void AndroidSystemProducer::NotifyFlushComplete(perfetto::FlushRequestID id) {
} }
} }
void AndroidSystemProducer::RegisterTraceWriter(uint32_t writer_id, void PosixSystemProducer::RegisterTraceWriter(uint32_t writer_id,
uint32_t target_buffer) { uint32_t target_buffer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(service_); DCHECK(service_);
service_->RegisterTraceWriter(writer_id, target_buffer); service_->RegisterTraceWriter(writer_id, target_buffer);
} }
void AndroidSystemProducer::UnregisterTraceWriter(uint32_t writer_id) { void PosixSystemProducer::UnregisterTraceWriter(uint32_t writer_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(service_); DCHECK(service_);
service_->UnregisterTraceWriter(writer_id); service_->UnregisterTraceWriter(writer_id);
} }
void AndroidSystemProducer::RegisterDataSource( void PosixSystemProducer::RegisterDataSource(
const perfetto::DataSourceDescriptor&) { const perfetto::DataSourceDescriptor&) {
// Never called by SharedMemoryArbiter/TraceWriter. // Never called by SharedMemoryArbiter/TraceWriter.
NOTREACHED(); NOTREACHED();
} }
void AndroidSystemProducer::UnregisterDataSource(const std::string& name) { void PosixSystemProducer::UnregisterDataSource(const std::string& name) {
// Never called by SharedMemoryArbiter/TraceWriter. // Never called by SharedMemoryArbiter/TraceWriter.
NOTREACHED(); NOTREACHED();
} }
void AndroidSystemProducer::NotifyDataSourceStopped( void PosixSystemProducer::NotifyDataSourceStopped(
perfetto::DataSourceInstanceID id) { perfetto::DataSourceInstanceID id) {
// Never called by SharedMemoryArbiter/TraceWriter. // Never called by SharedMemoryArbiter/TraceWriter.
NOTREACHED(); NOTREACHED();
} }
void AndroidSystemProducer::NotifyDataSourceStarted( void PosixSystemProducer::NotifyDataSourceStarted(
perfetto::DataSourceInstanceID id) { perfetto::DataSourceInstanceID id) {
// Never called by SharedMemoryArbiter/TraceWriter. // Never called by SharedMemoryArbiter/TraceWriter.
NOTREACHED(); NOTREACHED();
} }
size_t AndroidSystemProducer::shared_buffer_page_size_kb() const { size_t PosixSystemProducer::shared_buffer_page_size_kb() const {
// Never called by SharedMemoryArbiter/TraceWriter. // Never called by SharedMemoryArbiter/TraceWriter.
NOTREACHED(); NOTREACHED();
return 0; return 0;
} }
perfetto::SharedMemoryArbiter* perfetto::SharedMemoryArbiter* PosixSystemProducer::GetInProcessShmemArbiter() {
AndroidSystemProducer::GetInProcessShmemArbiter() {
// Never called by SharedMemoryArbiter/TraceWriter. // Never called by SharedMemoryArbiter/TraceWriter.
NOTREACHED(); NOTREACHED();
return GetSharedMemoryArbiter(); return GetSharedMemoryArbiter();
} }
void AndroidSystemProducer::ActivateTriggers( void PosixSystemProducer::ActivateTriggers(
const std::vector<std::string>& triggers) { const std::vector<std::string>& triggers) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (state_ == State::kConnected) { if (state_ == State::kConnected) {
...@@ -402,7 +404,7 @@ void AndroidSystemProducer::ActivateTriggers( ...@@ -402,7 +404,7 @@ void AndroidSystemProducer::ActivateTriggers(
} }
} }
void AndroidSystemProducer::ConnectSocket() { void PosixSystemProducer::ConnectSocket() {
state_ = State::kConnecting; state_ = State::kConnecting;
service_ = perfetto::ProducerIPCClient::Connect( service_ = perfetto::ProducerIPCClient::Connect(
socket_name_.c_str(), this, socket_name_.c_str(), this,
...@@ -414,13 +416,16 @@ void AndroidSystemProducer::ConnectSocket() { ...@@ -414,13 +416,16 @@ void AndroidSystemProducer::ConnectSocket() {
perfetto::TracingService::ProducerSMBScrapingMode::kEnabled); perfetto::TracingService::ProducerSMBScrapingMode::kEnabled);
} }
bool AndroidSystemProducer::SkipIfPreAndroidPie() const { bool PosixSystemProducer::SkipIfOnAndroidAndPreAndroidPie() const {
#if defined(OS_ANDROID)
return disallow_pre_android_pie && return disallow_pre_android_pie &&
base::android::BuildInfo::GetInstance()->sdk_int() < base::android::BuildInfo::GetInstance()->sdk_int() <
base::android::SDK_VERSION_P; base::android::SDK_VERSION_P;
#endif // defined(OS_ANDROID)
return false;
} }
void AndroidSystemProducer::InvokeStoredOnDisconnectCallbacks() { void PosixSystemProducer::InvokeStoredOnDisconnectCallbacks() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (auto& callback : on_disconnect_callbacks_) { for (auto& callback : on_disconnect_callbacks_) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
...@@ -429,9 +434,9 @@ void AndroidSystemProducer::InvokeStoredOnDisconnectCallbacks() { ...@@ -429,9 +434,9 @@ void AndroidSystemProducer::InvokeStoredOnDisconnectCallbacks() {
on_disconnect_callbacks_.clear(); on_disconnect_callbacks_.clear();
} }
void AndroidSystemProducer::Connect() { void PosixSystemProducer::Connect() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (SkipIfPreAndroidPie()) { if (SkipIfOnAndroidAndPreAndroidPie()) {
return; return;
} }
switch (state_) { switch (state_) {
...@@ -459,9 +464,9 @@ void AndroidSystemProducer::Connect() { ...@@ -459,9 +464,9 @@ void AndroidSystemProducer::Connect() {
} }
} }
void AndroidSystemProducer::DelayedReconnect() { void PosixSystemProducer::DelayedReconnect() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (SkipIfPreAndroidPie()) { if (SkipIfOnAndroidAndPreAndroidPie()) {
return; return;
} }
if (state_ == State::kDisconnected) { if (state_ == State::kDisconnected) {
...@@ -472,7 +477,7 @@ void AndroidSystemProducer::DelayedReconnect() { ...@@ -472,7 +477,7 @@ void AndroidSystemProducer::DelayedReconnect() {
task_runner()->GetOrCreateTaskRunner()->PostDelayedTask( task_runner()->GetOrCreateTaskRunner()->PostDelayedTask(
FROM_HERE, FROM_HERE,
base::BindOnce( base::BindOnce(
[](base::WeakPtr<AndroidSystemProducer> weak_ptr) { [](base::WeakPtr<PosixSystemProducer> weak_ptr) {
if (!weak_ptr) { if (!weak_ptr) {
return; return;
} }
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef SERVICES_TRACING_PUBLIC_CPP_PERFETTO_ANDROID_SYSTEM_PRODUCER_H_ #ifndef SERVICES_TRACING_PUBLIC_CPP_PERFETTO_POSIX_SYSTEM_PRODUCER_H_
#define SERVICES_TRACING_PUBLIC_CPP_PERFETTO_ANDROID_SYSTEM_PRODUCER_H_ #define SERVICES_TRACING_PUBLIC_CPP_PERFETTO_POSIX_SYSTEM_PRODUCER_H_
#include <memory> #include <memory>
#include <set> #include <set>
...@@ -26,7 +26,7 @@ class SharedMemory; ...@@ -26,7 +26,7 @@ class SharedMemory;
namespace tracing { namespace tracing {
class COMPONENT_EXPORT(TRACING_CPP) AndroidSystemProducer class COMPONENT_EXPORT(TRACING_CPP) PosixSystemProducer
: public SystemProducer { : public SystemProducer {
public: public:
enum class State { enum class State {
...@@ -35,10 +35,10 @@ class COMPONENT_EXPORT(TRACING_CPP) AndroidSystemProducer ...@@ -35,10 +35,10 @@ class COMPONENT_EXPORT(TRACING_CPP) AndroidSystemProducer
kConnected = 2, kConnected = 2,
kDisconnected = 3 kDisconnected = 3
}; };
AndroidSystemProducer(const char* socket, PerfettoTaskRunner* task_runner); PosixSystemProducer(const char* socket, PerfettoTaskRunner* task_runner);
~AndroidSystemProducer() override; ~PosixSystemProducer() override;
// Functions needed for AndroidSystemProducer only. // Functions needed for PosixSystemProducer only.
// //
// Lets tests ignore the SDK check (Perfetto only runs on post Android Pie // Lets tests ignore the SDK check (Perfetto only runs on post Android Pie
// devices by default, so for trybots on older OSs we need to ignore the check // devices by default, so for trybots on older OSs we need to ignore the check
...@@ -47,7 +47,7 @@ class COMPONENT_EXPORT(TRACING_CPP) AndroidSystemProducer ...@@ -47,7 +47,7 @@ class COMPONENT_EXPORT(TRACING_CPP) AndroidSystemProducer
// TODO(nuskos): We need to make this possible for telemetry as well, since // TODO(nuskos): We need to make this possible for telemetry as well, since
// they might have side loaded the app. // they might have side loaded the app.
void SetDisallowPreAndroidPieForTesting(bool disallow); void SetDisallowPreAndroidPieForTesting(bool disallow);
// |socket| must remain alive as long as AndroidSystemProducer is around // |socket| must remain alive as long as PosixSystemProducer is around
// trying to connect to it. // trying to connect to it.
void SetNewSocketForTesting(const char* socket); void SetNewSocketForTesting(const char* socket);
...@@ -128,7 +128,7 @@ class COMPONENT_EXPORT(TRACING_CPP) AndroidSystemProducer ...@@ -128,7 +128,7 @@ class COMPONENT_EXPORT(TRACING_CPP) AndroidSystemProducer
void ConnectSocket(); void ConnectSocket();
// Returns true if we should skip setup because this Android device is Android // Returns true if we should skip setup because this Android device is Android
// O or below. // O or below.
bool SkipIfPreAndroidPie() const; bool SkipIfOnAndroidAndPreAndroidPie() const;
// If any OnDisconnect callbacks are stored, this will invoke them and delete // If any OnDisconnect callbacks are stored, this will invoke them and delete
// references to them must be called on the proper sequence. // references to them must be called on the proper sequence.
void InvokeStoredOnDisconnectCallbacks(); void InvokeStoredOnDisconnectCallbacks();
...@@ -155,10 +155,10 @@ class COMPONENT_EXPORT(TRACING_CPP) AndroidSystemProducer ...@@ -155,10 +155,10 @@ class COMPONENT_EXPORT(TRACING_CPP) AndroidSystemProducer
SEQUENCE_CHECKER(sequence_checker_); SEQUENCE_CHECKER(sequence_checker_);
// NOTE: Weak pointers must be invalidated before all other member variables. // NOTE: Weak pointers must be invalidated before all other member variables.
// and thus must be the last member variable. // and thus must be the last member variable.
base::WeakPtrFactory<AndroidSystemProducer> weak_ptr_factory_{this}; base::WeakPtrFactory<PosixSystemProducer> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(AndroidSystemProducer); DISALLOW_COPY_AND_ASSIGN(PosixSystemProducer);
}; };
} // namespace tracing } // namespace tracing
#endif // SERVICES_TRACING_PUBLIC_CPP_PERFETTO_ANDROID_SYSTEM_PRODUCER_H_ #endif // SERVICES_TRACING_PUBLIC_CPP_PERFETTO_POSIX_SYSTEM_PRODUCER_H_
...@@ -27,9 +27,9 @@ PerfettoTaskRunner::PerfettoTaskRunner( ...@@ -27,9 +27,9 @@ PerfettoTaskRunner::PerfettoTaskRunner(
PerfettoTaskRunner::~PerfettoTaskRunner() { PerfettoTaskRunner::~PerfettoTaskRunner() {
DCHECK(GetOrCreateTaskRunner()->RunsTasksInCurrentSequence()); DCHECK(GetOrCreateTaskRunner()->RunsTasksInCurrentSequence());
#if defined(OS_ANDROID) #if defined(OS_POSIX)
fd_controllers_.clear(); fd_controllers_.clear();
#endif // defined(OS_ANDROID) #endif // defined(OS_POSIX)
} }
void PerfettoTaskRunner::PostTask(std::function<void()> task) { void PerfettoTaskRunner::PostTask(std::function<void()> task) {
...@@ -79,26 +79,43 @@ bool PerfettoTaskRunner::RunsTasksOnCurrentThread() const { ...@@ -79,26 +79,43 @@ bool PerfettoTaskRunner::RunsTasksOnCurrentThread() const {
void PerfettoTaskRunner::AddFileDescriptorWatch( void PerfettoTaskRunner::AddFileDescriptorWatch(
int fd, int fd,
std::function<void()> callback) { std::function<void()> callback) {
#if !defined(OS_ANDROID) #if !defined(OS_POSIX)
NOTREACHED(); NOTREACHED();
#else #else
DCHECK(GetOrCreateTaskRunner()->RunsTasksInCurrentSequence()); DCHECK(GetOrCreateTaskRunner()->RunsTasksInCurrentSequence());
DCHECK(!base::Contains(fd_controllers_, fd)); DCHECK(!base::Contains(fd_controllers_, fd));
fd_controllers_[fd] = base::FileDescriptorWatcher::WatchReadable( // Set it up as a nullptr to signal intent to add a watch.
fd, fd_controllers_[fd];
base::BindRepeating([](std::function<void()> callback) { callback(); }, task_runner_->PostTask(
std::move(callback))); FROM_HERE,
#endif // !defined(OS_ANDROID) base::BindOnce(
[](PerfettoTaskRunner* perfetto_runner, int fd,
std::function<void()> callback) {
DCHECK(perfetto_runner->GetOrCreateTaskRunner()
->RunsTasksInCurrentSequence());
auto it = perfetto_runner->fd_controllers_.find(fd);
// If we can't find this fd, then RemoveFileDescriptor has already
// been called so just early out.
if (it == perfetto_runner->fd_controllers_.end()) {
return;
}
it->second = base::FileDescriptorWatcher::WatchReadable(
fd, base::BindRepeating(
[](std::function<void()> callback) { callback(); },
std::move(callback)));
},
base::Unretained(this), fd, std::move(callback)));
#endif // !defined(OS_POSIX)
} }
void PerfettoTaskRunner::RemoveFileDescriptorWatch(int fd) { void PerfettoTaskRunner::RemoveFileDescriptorWatch(int fd) {
#if !defined(OS_ANDROID) #if !defined(OS_POSIX)
NOTREACHED(); NOTREACHED();
#else #else
DCHECK(GetOrCreateTaskRunner()->RunsTasksInCurrentSequence()); DCHECK(GetOrCreateTaskRunner()->RunsTasksInCurrentSequence());
DCHECK(base::Contains(fd_controllers_, fd)); DCHECK(base::Contains(fd_controllers_, fd));
fd_controllers_.erase(fd); fd_controllers_.erase(fd);
#endif // !defined(OS_ANDROID) #endif // !defined(OS_POSIX)
} }
void PerfettoTaskRunner::ResetTaskRunnerForTesting( void PerfettoTaskRunner::ResetTaskRunnerForTesting(
......
...@@ -16,12 +16,12 @@ ...@@ -16,12 +16,12 @@
#include "services/tracing/public/mojom/perfetto_service.mojom.h" #include "services/tracing/public/mojom/perfetto_service.mojom.h"
#include "third_party/perfetto/include/perfetto/base/task_runner.h" #include "third_party/perfetto/include/perfetto/base/task_runner.h"
#if defined(OS_ANDROID) #if defined(OS_POSIX)
#include <map>
// Needed for base::FileDescriptorWatcher::Controller and for implementing // Needed for base::FileDescriptorWatcher::Controller and for implementing
// AddFileDescriptorWatch & RemoveFileDescriptorWatch on Android. // AddFileDescriptorWatch & RemoveFileDescriptorWatch.
#include <map>
#include "base/files/file_descriptor_watcher_posix.h" #include "base/files/file_descriptor_watcher_posix.h"
#endif // defined(OS_ANDROID) #endif // defined(OS_POSIX)
namespace tracing { namespace tracing {
...@@ -62,10 +62,10 @@ class COMPONENT_EXPORT(TRACING_CPP) PerfettoTaskRunner ...@@ -62,10 +62,10 @@ class COMPONENT_EXPORT(TRACING_CPP) PerfettoTaskRunner
void OnDeferredTasksDrainTimer(); void OnDeferredTasksDrainTimer();
scoped_refptr<base::SequencedTaskRunner> task_runner_; scoped_refptr<base::SequencedTaskRunner> task_runner_;
#if defined(OS_ANDROID) #if defined(OS_POSIX)
std::map<int, std::unique_ptr<base::FileDescriptorWatcher::Controller>> std::map<int, std::unique_ptr<base::FileDescriptorWatcher::Controller>>
fd_controllers_; fd_controllers_;
#endif // defined(OS_ANDROID) #endif // defined(OS_POSIX)
DISALLOW_COPY_AND_ASSIGN(PerfettoTaskRunner); DISALLOW_COPY_AND_ASSIGN(PerfettoTaskRunner);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment