Commit 67f4c2f3 authored by Oystein Eftevaag's avatar Oystein Eftevaag Committed by Commit Bot

Background tracing: Use the new Perfetto Consumer mojo endpoint

rather than the TracingController.

This is in preparation for being able to handle proto traces
in addition to JSON.

R=ssid@chromium.org
BUG=925151

Change-Id: I91ed3c9648587fb19a5102b869a5d5ab688e0187
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1546146
Commit-Queue: oysteine <oysteine@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarssid <ssid@chromium.org>
Cr-Commit-Position: refs/heads/master@{#657413}
parent 4ece4b7d
...@@ -21,7 +21,7 @@ class BackgroundTracingTest : public testing::Test { ...@@ -21,7 +21,7 @@ class BackgroundTracingTest : public testing::Test {
BackgroundTracingTest() = default; BackgroundTracingTest() = default;
void TearDown() override { void TearDown() override {
content::BackgroundTracingManager::GetInstance()->AbortScenario(); content::BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
} }
private: private:
......
...@@ -34,7 +34,7 @@ TEST_F(BackgroundTracingMetricsProviderTest, UploadsTraceLog) { ...@@ -34,7 +34,7 @@ TEST_F(BackgroundTracingMetricsProviderTest, UploadsTraceLog) {
EXPECT_FALSE(provider.HasIndependentMetrics()); EXPECT_FALSE(provider.HasIndependentMetrics());
content::BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting( content::BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting(
kDummyTrace); std::make_unique<std::string>(kDummyTrace));
EXPECT_TRUE(provider.HasIndependentMetrics()); EXPECT_TRUE(provider.HasIndependentMetrics());
metrics::ChromeUserMetricsExtension uma_proto; metrics::ChromeUserMetricsExtension uma_proto;
...@@ -57,11 +57,11 @@ TEST_F(BackgroundTracingMetricsProviderTest, HandleMissingTrace) { ...@@ -57,11 +57,11 @@ TEST_F(BackgroundTracingMetricsProviderTest, HandleMissingTrace) {
EXPECT_FALSE(provider.HasIndependentMetrics()); EXPECT_FALSE(provider.HasIndependentMetrics());
content::BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting( content::BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting(
kDummyTrace); std::make_unique<std::string>(kDummyTrace));
EXPECT_TRUE(provider.HasIndependentMetrics()); EXPECT_TRUE(provider.HasIndependentMetrics());
content::BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting( content::BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting(
""); nullptr);
metrics::ChromeUserMetricsExtension uma_proto; metrics::ChromeUserMetricsExtension uma_proto;
uma_proto.set_client_id(100); uma_proto.set_client_id(100);
uma_proto.set_session_id(15); uma_proto.set_session_id(15);
......
...@@ -148,7 +148,7 @@ IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest, ...@@ -148,7 +148,7 @@ IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
local_state->GetInt64(prefs::kBackgroundTracingLastUpload)); local_state->GetInt64(prefs::kBackgroundTracingLastUpload));
EXPECT_FALSE(last_upload_time.is_null()); EXPECT_FALSE(last_upload_time.is_null());
content::BackgroundTracingManager::GetInstance()->AbortScenario(); content::BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
base::RunLoop wait_for_abort; base::RunLoop wait_for_abort;
content::BackgroundTracingManager::GetInstance()->WhenIdle( content::BackgroundTracingManager::GetInstance()->WhenIdle(
wait_for_abort.QuitClosure()); wait_for_abort.QuitClosure());
...@@ -188,7 +188,7 @@ IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest, ...@@ -188,7 +188,7 @@ IN_PROC_BROWSER_TEST_F(ChromeTracingDelegateBrowserTest,
local_state->GetInt64(prefs::kBackgroundTracingLastUpload)); local_state->GetInt64(prefs::kBackgroundTracingLastUpload));
EXPECT_FALSE(last_upload_time.is_null()); EXPECT_FALSE(last_upload_time.is_null());
content::BackgroundTracingManager::GetInstance()->AbortScenario(); content::BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
base::RunLoop wait_for_abort; base::RunLoop wait_for_abort;
content::BackgroundTracingManager::GetInstance()->WhenIdle( content::BackgroundTracingManager::GetInstance()->WhenIdle(
wait_for_abort.QuitClosure()); wait_for_abort.QuitClosure());
......
...@@ -16,8 +16,14 @@ ...@@ -16,8 +16,14 @@
#include "content/browser/tracing/background_tracing_manager_impl.h" #include "content/browser/tracing/background_tracing_manager_impl.h"
#include "content/browser/tracing/background_tracing_rule.h" #include "content/browser/tracing/background_tracing_rule.h"
#include "content/browser/tracing/tracing_controller_impl.h" #include "content/browser/tracing/tracing_controller_impl.h"
#include "content/public/common/service_manager_connection.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/system/data_pipe_drainer.h"
#include "services/service_manager/public/cpp/connector.h"
#include "services/tracing/public/cpp/perfetto/perfetto_config.h"
#include "services/tracing/public/cpp/perfetto/trace_event_data_source.h" #include "services/tracing/public/cpp/perfetto/trace_event_data_source.h"
#include "services/tracing/public/cpp/tracing_features.h" #include "services/tracing/public/cpp/tracing_features.h"
#include "services/tracing/public/mojom/constants.mojom.h"
using base::trace_event::TraceConfig; using base::trace_event::TraceConfig;
using Metrics = content::BackgroundTracingManagerImpl::Metrics; using Metrics = content::BackgroundTracingManagerImpl::Metrics;
...@@ -50,6 +56,182 @@ class BackgroundTracingActiveScenario::TracingTimer { ...@@ -50,6 +56,182 @@ class BackgroundTracingActiveScenario::TracingTimer {
BackgroundTracingManager::StartedFinalizingCallback callback_; BackgroundTracingManager::StartedFinalizingCallback callback_;
}; };
class BackgroundTracingActiveScenario::TracingSession {
public:
virtual ~TracingSession() = default;
virtual bool BeginFinalizing() = 0;
};
class PerfettoTracingSession
: public BackgroundTracingActiveScenario::TracingSession,
public tracing::mojom::TracingSessionClient,
public mojo::DataPipeDrainer::Client {
public:
PerfettoTracingSession(BackgroundTracingActiveScenario* parent_scenario,
const TraceConfig& chrome_config,
BackgroundTracingConfigImpl::CategoryPreset preset)
: parent_scenario_(parent_scenario),
category_preset_(preset),
raw_data_(std::make_unique<std::string>()) {
#if !defined(OS_ANDROID)
// TODO(crbug.com/941318): Re-enable startup tracing for Android once all
// Perfetto-related deadlocks are resolved.
if (!TracingControllerImpl::GetInstance()->IsTracing() &&
tracing::TracingUsesPerfettoBackend()) {
tracing::TraceEventDataSource::GetInstance()->SetupStartupTracing(
/*privacy_filtering_enabled=*/true);
}
#endif
ServiceManagerConnection::GetForProcess()->GetConnector()->BindInterface(
tracing::mojom::kServiceName, &consumer_host_);
perfetto::TraceConfig perfetto_config = tracing::GetDefaultPerfettoConfig(
chrome_config, /*privacy_filtering_enabled=*/true);
tracing::mojom::TracingSessionClientPtr tracing_session_client;
binding_.Bind(mojo::MakeRequest(&tracing_session_client));
binding_.set_connection_error_handler(
base::BindOnce(&PerfettoTracingSession::OnTracingSessionEnded,
base::Unretained(this)));
consumer_host_->EnableTracing(
mojo::MakeRequest(&tracing_session_host_),
std::move(tracing_session_client), std::move(perfetto_config),
tracing::mojom::TracingClientPriority::kBackground);
tracing_session_host_.set_connection_error_handler(
base::BindOnce(&PerfettoTracingSession::OnTracingSessionEnded,
base::Unretained(this)));
}
// BackgroundTracingActiveScenario::TracingSession implementation.
bool BeginFinalizing() override {
bool is_allowed_finalization =
BackgroundTracingManagerImpl::GetInstance()->IsAllowedFinalization();
if (!is_allowed_finalization) {
return false;
}
tracing_session_host_->DisableTracing();
return true;
}
// mojo::DataPipeDrainer::Client implementation:
void OnDataAvailable(const void* data, size_t num_bytes) override {
raw_data_->append(reinterpret_cast<const char*>(data), num_bytes);
}
void OnDataComplete() override {
has_finished_receiving_data_ = true;
MaybeFinishedReceivingTrace();
}
// tracing::mojom::TracingSession implementation:
void OnTracingEnabled() override {
BackgroundTracingManagerImpl::GetInstance()->OnStartTracingDone(
category_preset_);
}
void OnTracingDisabled() override {
mojo::ScopedDataPipeProducerHandle producer_handle;
mojo::ScopedDataPipeConsumerHandle consumer_handle;
MojoResult result =
mojo::CreateDataPipe(nullptr, &producer_handle, &consumer_handle);
DCHECK_EQ(MOJO_RESULT_OK, result);
drainer_ = std::make_unique<mojo::DataPipeDrainer>(
this, std::move(consumer_handle));
tracing_session_host_->ReadBuffers(
std::move(producer_handle),
base::BindOnce(&PerfettoTracingSession::OnReadBuffersComplete,
base::Unretained(this)));
}
void OnReadBuffersComplete() {
has_finished_read_buffers_ = true;
MaybeFinishedReceivingTrace();
}
void MaybeFinishedReceivingTrace() {
if (has_finished_read_buffers_ && has_finished_receiving_data_) {
DCHECK(raw_data_);
parent_scenario_->OnProtoDataComplete(std::move(raw_data_));
}
}
private:
void OnTracingSessionEnded() { parent_scenario_->AbortScenario(); }
BackgroundTracingActiveScenario* const parent_scenario_;
mojo::Binding<tracing::mojom::TracingSessionClient> binding_{this};
tracing::mojom::TracingSessionHostPtr tracing_session_host_;
std::unique_ptr<mojo::DataPipeDrainer> drainer_;
tracing::mojom::ConsumerHostPtr consumer_host_;
BackgroundTracingConfigImpl::CategoryPreset category_preset_;
std::unique_ptr<std::string> raw_data_;
bool has_finished_read_buffers_ = false;
bool has_finished_receiving_data_ = false;
};
class LegacyTracingSession
: public BackgroundTracingActiveScenario::TracingSession {
public:
LegacyTracingSession(BackgroundTracingActiveScenario* parent_scenario,
const TraceConfig& chrome_config,
BackgroundTracingConfigImpl::CategoryPreset preset)
: parent_scenario_(parent_scenario) {
#if !defined(OS_ANDROID)
// TODO(crbug.com/941318): Re-enable startup tracing for Android once all
// Perfetto-related deadlocks are resolved.
if (!TracingControllerImpl::GetInstance()->IsTracing() &&
tracing::TracingUsesPerfettoBackend()) {
tracing::TraceEventDataSource::GetInstance()->SetupStartupTracing(
/*privacy_filtering_enabled=*/false);
}
#endif
bool result = TracingControllerImpl::GetInstance()->StartTracing(
chrome_config,
base::BindOnce(
&BackgroundTracingManagerImpl::OnStartTracingDone,
base::Unretained(BackgroundTracingManagerImpl::GetInstance()),
preset));
// We check IsEnabled() before creating the LegacyTracingSession,
// so any failures to start tracing at this point would be due to invalid
// configs which we treat as a failure scenario.
DCHECK(result);
}
~LegacyTracingSession() override {
if (TracingControllerImpl::GetInstance()->IsTracing()) {
TracingControllerImpl::GetInstance()->StopTracing(nullptr);
}
}
// BackgroundTracingActiveScenario::TracingSession implementation.
bool BeginFinalizing() override {
if (!BackgroundTracingManagerImpl::GetInstance()->IsAllowedFinalization()) {
return false;
}
auto trace_data_endpoint =
TracingControllerImpl::CreateCompressedStringEndpoint(
TracingControllerImpl::CreateCallbackEndpoint(base::BindRepeating(
&BackgroundTracingActiveScenario::OnJSONDataComplete,
parent_scenario_->GetWeakPtr())),
true /* compress_with_background_priority */);
TracingControllerImpl::GetInstance()->StopTracing(trace_data_endpoint);
return true;
}
private:
BackgroundTracingActiveScenario* const parent_scenario_;
};
BackgroundTracingActiveScenario::BackgroundTracingActiveScenario( BackgroundTracingActiveScenario::BackgroundTracingActiveScenario(
std::unique_ptr<const BackgroundTracingConfigImpl> config, std::unique_ptr<const BackgroundTracingConfigImpl> config,
bool requires_anonymized_data, bool requires_anonymized_data,
...@@ -104,6 +286,7 @@ void BackgroundTracingActiveScenario::SetState(State new_state) { ...@@ -104,6 +286,7 @@ void BackgroundTracingActiveScenario::SetState(State new_state) {
} }
if (scenario_state_ == State::kAborted) { if (scenario_state_ == State::kAborted) {
tracing_session_.reset();
std::move(on_aborted_callback_).Run(); std::move(on_aborted_callback_).Run();
} }
} }
...@@ -118,6 +301,11 @@ void BackgroundTracingActiveScenario::SetRuleTriggeredCallbackForTesting( ...@@ -118,6 +301,11 @@ void BackgroundTracingActiveScenario::SetRuleTriggeredCallbackForTesting(
rule_triggered_callback_for_testing_ = callback; rule_triggered_callback_for_testing_ = callback;
} }
base::WeakPtr<BackgroundTracingActiveScenario>
BackgroundTracingActiveScenario::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void BackgroundTracingActiveScenario::StartTracingIfConfigNeedsIt() { void BackgroundTracingActiveScenario::StartTracingIfConfigNeedsIt() {
DCHECK(config_); DCHECK(config_);
if (config_->tracing_mode() == BackgroundTracingConfigImpl::PREEMPTIVE) { if (config_->tracing_mode() == BackgroundTracingConfigImpl::PREEMPTIVE) {
...@@ -132,50 +320,45 @@ void BackgroundTracingActiveScenario::StartTracingIfConfigNeedsIt() { ...@@ -132,50 +320,45 @@ void BackgroundTracingActiveScenario::StartTracingIfConfigNeedsIt() {
void BackgroundTracingActiveScenario::StartTracing( void BackgroundTracingActiveScenario::StartTracing(
BackgroundTracingConfigImpl::CategoryPreset preset, BackgroundTracingConfigImpl::CategoryPreset preset,
base::trace_event::TraceRecordMode record_mode) { base::trace_event::TraceRecordMode record_mode) {
TraceConfig config = BackgroundTracingConfigImpl::GetConfigForCategoryPreset( TraceConfig chrome_config =
preset, record_mode); BackgroundTracingConfigImpl::GetConfigForCategoryPreset(preset,
record_mode);
if (requires_anonymized_data_) if (requires_anonymized_data_)
config.EnableArgumentFilter(); chrome_config.EnableArgumentFilter();
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
// Set low trace buffer size on Android in order to upload small trace files. // Set low trace buffer size on Android in order to upload small trace files.
if (config_->tracing_mode() == BackgroundTracingConfigImpl::PREEMPTIVE) { if (config_->tracing_mode() == BackgroundTracingConfigImpl::PREEMPTIVE) {
config.SetTraceBufferSizeInEvents(20000); chrome_config.SetTraceBufferSizeInEvents(20000);
config.SetTraceBufferSizeInKb(500); chrome_config.SetTraceBufferSizeInKb(500);
}
#else
// TODO(crbug.com/941318): Re-enable startup tracing for Android once all
// Perfetto-related deadlocks are resolved.
if (!TracingControllerImpl::GetInstance()->IsTracing() &&
tracing::TracingUsesPerfettoBackend()) {
// TODO(oysteine): This should pass in |requires_anonymized_data_| instead
// of false only when using consumer API with proto output. But, for JSON
// output we still need this to be false since filtering happens in the JSON
// exporter.
tracing::TraceEventDataSource::GetInstance()->SetupStartupTracing(
/*privacy_filtering_enabled=*/false);
} }
#endif #endif
if (!TracingControllerImpl::GetInstance()->StartTracing( // If the tracing controller is tracing, i.e. DevTools or about://tracing,
config, // we don't start background tracing to not interfere with the user activity.
base::BindOnce( if (TracingControllerImpl::GetInstance()->IsTracing()) {
&BackgroundTracingManagerImpl::OnStartTracingDone,
base::Unretained(BackgroundTracingManagerImpl::GetInstance()),
preset))) {
AbortScenario(); AbortScenario();
return; return;
} }
SetState(State::kTracing);
// Activate the categories immediately. StartTracing eventually does this // Activate the categories immediately. StartTracing eventually does this
// itself, but asynchronously via PostTask, and in the meantime events will be // itself, but asynchronously via Mojo, and in the meantime events will be
// dropped. This ensures that we start recording events for those categories // dropped. This ensures that we start recording events for those categories
// immediately. // immediately.
uint8_t modes = base::trace_event::TraceLog::RECORDING_MODE; uint8_t modes = base::trace_event::TraceLog::RECORDING_MODE;
if (!config.event_filters().empty()) if (!chrome_config.event_filters().empty())
modes |= base::trace_event::TraceLog::FILTERING_MODE; modes |= base::trace_event::TraceLog::FILTERING_MODE;
base::trace_event::TraceLog::GetInstance()->SetEnabled(config, modes); base::trace_event::TraceLog::GetInstance()->SetEnabled(chrome_config, modes);
DCHECK(!tracing_session_);
if (base::FeatureList::IsEnabled(features::kBackgroundTracingProtoOutput)) {
tracing_session_ =
std::make_unique<PerfettoTracingSession>(this, chrome_config, preset);
} else {
tracing_session_ =
std::make_unique<LegacyTracingSession>(this, chrome_config, preset);
}
SetState(State::kTracing);
BackgroundTracingManagerImpl::RecordMetric(Metrics::RECORDING_ENABLED); BackgroundTracingManagerImpl::RecordMetric(Metrics::RECORDING_ENABLED);
} }
...@@ -185,56 +368,37 @@ void BackgroundTracingActiveScenario::BeginFinalizing( ...@@ -185,56 +368,37 @@ void BackgroundTracingActiveScenario::BeginFinalizing(
triggered_named_event_handle_ = -1; triggered_named_event_handle_ = -1;
tracing_timer_.reset(); tracing_timer_.reset();
scoped_refptr<TracingControllerImpl::TraceDataEndpoint> trace_data_endpoint; bool is_allowed_finalization = tracing_session_->BeginFinalizing();
bool is_allowed_finalization =
BackgroundTracingManagerImpl::GetInstance()->IsAllowedFinalization(); if (!is_allowed_finalization) {
base::RepeatingClosure started_finalizing_closure;
if (!callback.is_null()) {
started_finalizing_closure =
base::BindRepeating(callback, is_allowed_finalization);
}
if (is_allowed_finalization) {
trace_data_endpoint = TracingControllerImpl::CreateCompressedStringEndpoint(
TracingControllerImpl::CreateCallbackEndpoint(base::BindRepeating(
&BackgroundTracingActiveScenario::OnTracingStopped,
weak_ptr_factory_.GetWeakPtr(),
std::move(started_finalizing_closure))),
true /* compress_with_background_priority */);
BackgroundTracingManagerImpl::RecordMetric(Metrics::FINALIZATION_ALLOWED);
} else {
trace_data_endpoint =
TracingControllerImpl::CreateCallbackEndpoint(base::BindRepeating(
[](base::RepeatingClosure closure,
base::WeakPtr<BackgroundTracingActiveScenario> active_scenario,
std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString* file_contents) {
if (active_scenario) {
active_scenario->SetState(State::kAborted);
}
if (closure) {
std::move(closure).Run();
}
},
std::move(started_finalizing_closure),
weak_ptr_factory_.GetWeakPtr()));
BackgroundTracingManagerImpl::RecordMetric( BackgroundTracingManagerImpl::RecordMetric(
Metrics::FINALIZATION_DISALLOWED); Metrics::FINALIZATION_DISALLOWED);
SetState(State::kAborted);
if (!callback.is_null()) {
callback.Run(false);
}
return;
} }
TracingControllerImpl::GetInstance()->StopTracing(trace_data_endpoint); SetState(State::kFinalizing);
BackgroundTracingManagerImpl::RecordMetric(Metrics::FINALIZATION_ALLOWED);
DCHECK(!started_finalizing_closure_);
if (!callback.is_null()) {
started_finalizing_closure_ =
base::BindOnce(callback, /*is_allowed_finalization=*/true);
}
} }
void BackgroundTracingActiveScenario::OnTracingStopped( void BackgroundTracingActiveScenario::OnJSONDataComplete(
base::RepeatingClosure started_finalizing_closure,
std::unique_ptr<const base::DictionaryValue> metadata, std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString* file_contents) { base::RefCountedString* file_contents) {
SetState(State::kFinalizing);
BackgroundTracingManagerImpl::RecordMetric(Metrics::FINALIZATION_STARTED); BackgroundTracingManagerImpl::RecordMetric(Metrics::FINALIZATION_STARTED);
UMA_HISTOGRAM_MEMORY_KB("Tracing.Background.FinalizingTraceSizeInKB", UMA_HISTOGRAM_MEMORY_KB("Tracing.Background.FinalizingTraceSizeInKB",
file_contents->size() / 1024); file_contents->size() / 1024);
// Send the finalized and compressed tracing data to the destination
// callback.
if (!receive_callback_.is_null()) { if (!receive_callback_.is_null()) {
receive_callback_.Run( receive_callback_.Run(
file_contents, std::move(metadata), file_contents, std::move(metadata),
...@@ -242,8 +406,22 @@ void BackgroundTracingActiveScenario::OnTracingStopped( ...@@ -242,8 +406,22 @@ void BackgroundTracingActiveScenario::OnTracingStopped(
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
if (!started_finalizing_closure.is_null()) { if (started_finalizing_closure_) {
std::move(started_finalizing_closure).Run(); std::move(started_finalizing_closure_).Run();
}
}
void BackgroundTracingActiveScenario::OnProtoDataComplete(
std::unique_ptr<std::string> proto_trace) {
BackgroundTracingManagerImpl::RecordMetric(Metrics::FINALIZATION_STARTED);
UMA_HISTOGRAM_MEMORY_KB("Tracing.Background.FinalizingTraceSizeInKB",
proto_trace->size() / 1024);
BackgroundTracingManager::GetInstance()->SetTraceToUploadForTesting(
std::move(proto_trace));
if (started_finalizing_closure_) {
std::move(started_finalizing_closure_).Run();
} }
} }
...@@ -254,6 +432,7 @@ void BackgroundTracingActiveScenario::OnFinalizeComplete(bool success) { ...@@ -254,6 +432,7 @@ void BackgroundTracingActiveScenario::OnFinalizeComplete(bool success) {
BackgroundTracingManagerImpl::RecordMetric(Metrics::UPLOAD_FAILED); BackgroundTracingManagerImpl::RecordMetric(Metrics::UPLOAD_FAILED);
} }
tracing_session_.reset();
SetState(State::kIdle); SetState(State::kIdle);
// Now that a trace has completed, we may need to enable recording again. // Now that a trace has completed, we may need to enable recording again.
...@@ -261,24 +440,8 @@ void BackgroundTracingActiveScenario::OnFinalizeComplete(bool success) { ...@@ -261,24 +440,8 @@ void BackgroundTracingActiveScenario::OnFinalizeComplete(bool success) {
} }
void BackgroundTracingActiveScenario::AbortScenario() { void BackgroundTracingActiveScenario::AbortScenario() {
if ((state() != State::kTracing)) { // Setting the kAborted state will cause |this| to be destroyed.
// Setting the kAborted state will cause |this| to be destroyed. SetState(State::kAborted);
SetState(State::kAborted);
return;
}
auto trace_data_endpoint =
TracingControllerImpl::CreateCallbackEndpoint(base::BindRepeating(
[](base::WeakPtr<BackgroundTracingActiveScenario> active_scenario,
std::unique_ptr<const base::DictionaryValue>,
base::RefCountedString*) {
if (active_scenario) {
active_scenario->SetState(State::kAborted);
}
},
weak_ptr_factory_.GetWeakPtr()));
TracingControllerImpl::GetInstance()->StopTracing(trace_data_endpoint);
} }
void BackgroundTracingActiveScenario::TriggerNamedEvent( void BackgroundTracingActiveScenario::TriggerNamedEvent(
...@@ -371,6 +534,10 @@ void BackgroundTracingActiveScenario::OnRuleTriggered( ...@@ -371,6 +534,10 @@ void BackgroundTracingActiveScenario::OnRuleTriggered(
BackgroundTracingManagerImpl::RecordMetric(Metrics::PREEMPTIVE_TRIGGERED); BackgroundTracingManagerImpl::RecordMetric(Metrics::PREEMPTIVE_TRIGGERED);
} }
// Make a copy of the callback in case BeginFinalizing() ends up aborting the
// scenario and |this| gets deleted.
auto rule_triggered_callback_for_testing =
rule_triggered_callback_for_testing_;
if (trace_delay < 0) { if (trace_delay < 0) {
BeginFinalizing(std::move(callback)); BeginFinalizing(std::move(callback));
} else { } else {
...@@ -378,8 +545,8 @@ void BackgroundTracingActiveScenario::OnRuleTriggered( ...@@ -378,8 +545,8 @@ void BackgroundTracingActiveScenario::OnRuleTriggered(
tracing_timer_->StartTimer(trace_delay); tracing_timer_->StartTimer(trace_delay);
} }
if (!rule_triggered_callback_for_testing_.is_null()) { if (!rule_triggered_callback_for_testing.is_null()) {
rule_triggered_callback_for_testing_.Run(); rule_triggered_callback_for_testing.Run();
} }
} }
......
...@@ -11,7 +11,9 @@ ...@@ -11,7 +11,9 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "content/browser/tracing/background_tracing_config_impl.h" #include "content/browser/tracing/background_tracing_config_impl.h"
#include "content/browser/tracing/tracing_controller_impl.h"
#include "content/public/browser/background_tracing_manager.h" #include "content/public/browser/background_tracing_manager.h"
#include "services/tracing/public/mojom/perfetto_service.mojom.h"
namespace base { namespace base {
class RefCountedString; class RefCountedString;
...@@ -24,13 +26,14 @@ class BackgroundTracingConfigImpl; ...@@ -24,13 +26,14 @@ class BackgroundTracingConfigImpl;
class BackgroundTracingActiveScenario { class BackgroundTracingActiveScenario {
public: public:
enum class State { kIdle, kTracing, kFinalizing, kUploading, kAborted }; enum class State { kIdle, kTracing, kFinalizing, kUploading, kAborted };
class TracingSession;
BackgroundTracingActiveScenario( BackgroundTracingActiveScenario(
std::unique_ptr<const BackgroundTracingConfigImpl> config, std::unique_ptr<const BackgroundTracingConfigImpl> config,
bool requires_anonymized_data, bool requires_anonymized_data,
BackgroundTracingManager::ReceiveCallback receive_callback, BackgroundTracingManager::ReceiveCallback receive_callback,
base::OnceClosure on_aborted_callback); base::OnceClosure on_aborted_callback);
~BackgroundTracingActiveScenario(); virtual ~BackgroundTracingActiveScenario();
void StartTracingIfConfigNeedsIt(); void StartTracingIfConfigNeedsIt();
void AbortScenario(); void AbortScenario();
...@@ -39,6 +42,7 @@ class BackgroundTracingActiveScenario { ...@@ -39,6 +42,7 @@ class BackgroundTracingActiveScenario {
void GenerateMetadataDict(base::DictionaryValue* metadata_dict); void GenerateMetadataDict(base::DictionaryValue* metadata_dict);
State state() const { return scenario_state_; } State state() const { return scenario_state_; }
bool requires_anonymized_data() const { return requires_anonymized_data_; } bool requires_anonymized_data() const { return requires_anonymized_data_; }
base::WeakPtr<BackgroundTracingActiveScenario> GetWeakPtr();
void TriggerNamedEvent( void TriggerNamedEvent(
BackgroundTracingManager::TriggerHandle handle, BackgroundTracingManager::TriggerHandle handle,
...@@ -48,6 +52,16 @@ class BackgroundTracingActiveScenario { ...@@ -48,6 +52,16 @@ class BackgroundTracingActiveScenario {
const BackgroundTracingRule* triggered_rule, const BackgroundTracingRule* triggered_rule,
BackgroundTracingManager::StartedFinalizingCallback callback); BackgroundTracingManager::StartedFinalizingCallback callback);
// Called by LegacyTracingSession when the final trace data is ready.
void OnJSONDataComplete(std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString*);
// Called by the PerfettoTracingSession when the proto trace is ready.
void OnProtoDataComplete(std::unique_ptr<std::string> proto_trace);
// Called when the finalized trace data has been uploaded/transferred away
// from the background tracing system.
void OnFinalizeComplete(bool success);
// For testing // For testing
CONTENT_EXPORT void FireTimerForTesting(); CONTENT_EXPORT void FireTimerForTesting();
CONTENT_EXPORT void SetRuleTriggeredCallbackForTesting( CONTENT_EXPORT void SetRuleTriggeredCallbackForTesting(
...@@ -56,20 +70,15 @@ class BackgroundTracingActiveScenario { ...@@ -56,20 +70,15 @@ class BackgroundTracingActiveScenario {
private: private:
void StartTracing(BackgroundTracingConfigImpl::CategoryPreset, void StartTracing(BackgroundTracingConfigImpl::CategoryPreset,
base::trace_event::TraceRecordMode); base::trace_event::TraceRecordMode);
void BeginFinalizing( void BeginFinalizing(
BackgroundTracingManager::StartedFinalizingCallback callback); BackgroundTracingManager::StartedFinalizingCallback callback);
void OnTracingStopped(base::RepeatingClosure started_finalizing_closure,
std::unique_ptr<const base::DictionaryValue> metadata,
base::RefCountedString*);
void OnFinalizeComplete(bool success);
BackgroundTracingRule* GetRuleAbleToTriggerTracing( BackgroundTracingRule* GetRuleAbleToTriggerTracing(
const std::string& trigger_name); const std::string& trigger_name);
void SetState(State new_state); void SetState(State new_state);
std::unique_ptr<TracingSession> tracing_session_;
std::unique_ptr<const BackgroundTracingConfigImpl> config_; std::unique_ptr<const BackgroundTracingConfigImpl> config_;
bool requires_anonymized_data_; bool requires_anonymized_data_;
State scenario_state_; State scenario_state_;
...@@ -78,6 +87,7 @@ class BackgroundTracingActiveScenario { ...@@ -78,6 +87,7 @@ class BackgroundTracingActiveScenario {
BackgroundTracingManager::ReceiveCallback receive_callback_; BackgroundTracingManager::ReceiveCallback receive_callback_;
BackgroundTracingManager::TriggerHandle triggered_named_event_handle_; BackgroundTracingManager::TriggerHandle triggered_named_event_handle_;
base::OnceClosure on_aborted_callback_; base::OnceClosure on_aborted_callback_;
base::OnceClosure started_finalizing_closure_;
class TracingTimer; class TracingTimer;
std::unique_ptr<TracingTimer> tracing_timer_; std::unique_ptr<TracingTimer> tracing_timer_;
......
...@@ -16,8 +16,11 @@ ...@@ -16,8 +16,11 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/pattern.h" #include "base/strings/pattern.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "content/browser/devtools/protocol/devtools_protocol_test_support.h"
#include "content/browser/tracing/background_startup_tracing_observer.h" #include "content/browser/tracing/background_startup_tracing_observer.h"
#include "content/browser/tracing/background_tracing_active_scenario.h" #include "content/browser/tracing/background_tracing_active_scenario.h"
#include "content/browser/tracing/background_tracing_manager_impl.h" #include "content/browser/tracing/background_tracing_manager_impl.h"
...@@ -28,6 +31,7 @@ ...@@ -28,6 +31,7 @@
#include "content/public/test/content_browser_test.h" #include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h" #include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h" #include "content/public/test/test_utils.h"
#include "services/tracing/public/cpp/tracing_features.h"
#include "third_party/zlib/zlib.h" #include "third_party/zlib/zlib.h"
using base::trace_event::TraceLog; using base::trace_event::TraceLog;
...@@ -48,6 +52,21 @@ class TestStartupPreferenceManagerImpl ...@@ -48,6 +52,21 @@ class TestStartupPreferenceManagerImpl
bool enabled_ = false; bool enabled_ = false;
}; };
// Wait until |condition| returns true.
void WaitForCondition(base::RepeatingCallback<bool()> condition,
const std::string& description) {
const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(30);
const base::TimeTicks start_time = base::TimeTicks::Now();
while (!condition.Run() && (base::TimeTicks::Now() - start_time < kTimeout)) {
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), TestTimeouts::tiny_timeout());
run_loop.Run();
}
ASSERT_TRUE(condition.Run())
<< "Timeout waiting for condition: " << description;
}
// An helper class that observes tracing states transition and allows // An helper class that observes tracing states transition and allows
// synchronisation with tests. The class adds itself as a tracelog // synchronisation with tests. The class adds itself as a tracelog
// enable state observer and provides methods to wait for a given state. // enable state observer and provides methods to wait for a given state.
...@@ -357,7 +376,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -357,7 +376,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle, trigger_helper.receive_closure(true)); handle, trigger_helper.receive_closure(true));
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -388,7 +407,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -388,7 +407,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle, trigger_helper.receive_closure(false)); handle, trigger_helper.receive_closure(false));
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -422,7 +441,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -422,7 +441,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
trigger_helper.WaitForTriggerReceived(); trigger_helper.WaitForTriggerReceived();
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -469,7 +488,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -469,7 +488,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
trigger_helper.WaitForTriggerReceived(); trigger_helper.WaitForTriggerReceived();
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -500,7 +519,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -500,7 +519,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle, trigger_helper.receive_closure(true)); handle, trigger_helper.receive_closure(true));
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -539,7 +558,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -539,7 +558,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle, trigger_helper.receive_closure(true)); handle, trigger_helper.receive_closure(true));
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -598,7 +617,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -598,7 +617,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle2, trigger_helper.receive_closure(false)); handle2, trigger_helper.receive_closure(false));
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -662,7 +681,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -662,7 +681,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -705,7 +724,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -705,7 +724,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle, trigger_helper.receive_closure(false)); handle, trigger_helper.receive_closure(false));
// Abort the scenario. // Abort the scenario.
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_FALSE(trace_receiver_helper.trace_received()); EXPECT_FALSE(trace_receiver_helper.trace_received());
...@@ -737,7 +756,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -737,7 +756,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle, trigger_helper.receive_closure(false)); handle, trigger_helper.receive_closure(false));
// Abort the scenario. // Abort the scenario.
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_FALSE(trace_receiver_helper.trace_received()); EXPECT_FALSE(trace_receiver_helper.trace_received());
...@@ -783,7 +802,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -783,7 +802,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle, trigger_helper.receive_closure(false)); handle, trigger_helper.receive_closure(false));
// Abort the scenario. // Abort the scenario.
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_FALSE(trace_receiver_helper.trace_received()); EXPECT_FALSE(trace_receiver_helper.trace_received());
...@@ -829,7 +848,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -829,7 +848,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
trigger_helper.WaitForTriggerReceived(); trigger_helper.WaitForTriggerReceived();
// Abort the scenario. // Abort the scenario.
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_FALSE(trace_receiver_helper.trace_received()); EXPECT_FALSE(trace_receiver_helper.trace_received());
...@@ -872,7 +891,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -872,7 +891,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
LOCAL_HISTOGRAM_COUNTS("fake", 2); LOCAL_HISTOGRAM_COUNTS("fake", 2);
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -917,7 +936,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -917,7 +936,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
// Abort the scenario. // Abort the scenario.
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -961,7 +980,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -961,7 +980,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
LOCAL_HISTOGRAM_COUNTS("fake", 0); LOCAL_HISTOGRAM_COUNTS("fake", 0);
// Abort the scenario. // Abort the scenario.
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_FALSE(trace_receiver_helper.trace_received()); EXPECT_FALSE(trace_receiver_helper.trace_received());
...@@ -1006,7 +1025,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -1006,7 +1025,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
LOCAL_HISTOGRAM_COUNTS("fake", 0); LOCAL_HISTOGRAM_COUNTS("fake", 0);
// Abort the scenario. // Abort the scenario.
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_FALSE(trace_receiver_helper.trace_received()); EXPECT_FALSE(trace_receiver_helper.trace_received());
...@@ -1060,7 +1079,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -1060,7 +1079,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
->FireTimerForTesting(); ->FireTimerForTesting();
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -1090,7 +1109,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -1090,7 +1109,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle, trigger_helper.receive_closure(true)); handle, trigger_helper.receive_closure(true));
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -1135,7 +1154,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -1135,7 +1154,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
EXPECT_TRUE(trace_receiver_helper.trace_received(1)); EXPECT_TRUE(trace_receiver_helper.trace_received(1));
// Abort the scenario. // Abort the scenario.
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
} }
...@@ -1200,7 +1219,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -1200,7 +1219,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle1, trigger_helper.receive_closure(true)); handle1, trigger_helper.receive_closure(true));
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -1235,7 +1254,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -1235,7 +1254,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
handle, trigger_helper.receive_closure(false)); handle, trigger_helper.receive_closure(false));
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -1295,7 +1314,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -1295,7 +1314,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
->FireTimerForTesting(); ->FireTimerForTesting();
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
...@@ -1343,7 +1362,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -1343,7 +1362,7 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
EXPECT_TRUE(preferences->GetBackgroundStartupTracingEnabled()); EXPECT_TRUE(preferences->GetBackgroundStartupTracingEnabled());
// Abort the scenario. // Abort the scenario.
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_FALSE(trace_receiver_helper.trace_received()); EXPECT_FALSE(trace_receiver_helper.trace_received());
...@@ -1402,11 +1421,85 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, RunStartupTracing) { ...@@ -1402,11 +1421,85 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, RunStartupTracing) {
->FireTimerForTesting(); ->FireTimerForTesting();
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenario(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
EXPECT_FALSE(preferences->GetBackgroundStartupTracingEnabled()); EXPECT_FALSE(preferences->GetBackgroundStartupTracingEnabled());
} }
namespace {
class ProtoBackgroundTracingTest : public DevToolsProtocolTest {
public:
ProtoBackgroundTracingTest() {
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{features::kTracingPerfettoBackend,
features::kBackgroundTracingProtoOutput},
/*disabled_features=*/{});
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
} // namespace
IN_PROC_BROWSER_TEST_F(ProtoBackgroundTracingTest,
DevtoolsInterruptsBackgroundTracing) {
TestBackgroundTracingHelper background_tracing_helper;
TestTraceReceiverHelper trace_receiver_helper;
std::unique_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
EXPECT_TRUE(BackgroundTracingManager::GetInstance()->SetActiveScenario(
std::move(config), trace_receiver_helper.get_receive_callback(),
BackgroundTracingManager::NO_DATA_FILTERING));
background_tracing_helper.WaitForTracingEnabled();
NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1);
Attach();
base::Value* start_tracing_result =
SendCommand("Tracing.start", nullptr, true);
ASSERT_TRUE(start_tracing_result);
BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted();
}
IN_PROC_BROWSER_TEST_F(ProtoBackgroundTracingTest, ProtoTraceReceived) {
TestBackgroundTracingHelper background_tracing_helper;
std::unique_ptr<BackgroundTracingConfig> config = CreatePreemptiveConfig();
BackgroundTracingManager::TriggerHandle handle =
BackgroundTracingManager::GetInstance()->RegisterTriggerType(
"preemptive_test");
EXPECT_TRUE(BackgroundTracingManager::GetInstance()->SetActiveScenario(
std::move(config), base::DoNothing(),
BackgroundTracingManager::NO_DATA_FILTERING));
background_tracing_helper.WaitForTracingEnabled();
TestTriggerHelper trigger_helper;
BackgroundTracingManager::GetInstance()->TriggerNamedEvent(
handle, trigger_helper.receive_closure(true));
WaitForCondition(
base::BindRepeating([]() {
return BackgroundTracingManager::GetInstance()->HasTraceToUpload();
}),
"trace received");
std::string trace_data =
BackgroundTracingManager::GetInstance()->GetLatestTraceToUpload();
BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(!trace_data.empty());
}
} // namespace content } // namespace content
...@@ -162,18 +162,19 @@ bool BackgroundTracingManagerImpl::HasActiveScenario() { ...@@ -162,18 +162,19 @@ bool BackgroundTracingManagerImpl::HasActiveScenario() {
} }
bool BackgroundTracingManagerImpl::HasTraceToUpload() { bool BackgroundTracingManagerImpl::HasTraceToUpload() {
// TODO(oysteine): This should return the collected trace once we have the new DCHECK_CURRENTLY_ON(BrowserThread::UI);
// coordinator API to collect protos. https://crbug.com/925142. return !trace_to_upload_.empty();
// Note: This can be called on any thread and needs to be thread safe.
return !trace_to_upload_for_testing_.empty();
} }
std::string BackgroundTracingManagerImpl::GetLatestTraceToUpload() { std::string BackgroundTracingManagerImpl::GetLatestTraceToUpload() {
// TODO(oysteine): This should return the collected trace once we have the new DCHECK_CURRENTLY_ON(BrowserThread::UI);
// coordinator API to collect protos. https://crbug.com/925142.
// Note: This can be called on any thread and needs to be thread safe.
std::string ret; std::string ret;
ret.swap(trace_to_upload_for_testing_); ret.swap(trace_to_upload_);
if (active_scenario_) {
active_scenario_->OnFinalizeComplete(true);
}
return ret; return ret;
} }
...@@ -241,8 +242,18 @@ bool BackgroundTracingManagerImpl::IsTracingForTesting() { ...@@ -241,8 +242,18 @@ bool BackgroundTracingManagerImpl::IsTracingForTesting() {
} }
void BackgroundTracingManagerImpl::SetTraceToUploadForTesting( void BackgroundTracingManagerImpl::SetTraceToUploadForTesting(
base::StringPiece data) { std::unique_ptr<std::string> trace_data) {
trace_to_upload_for_testing_ = data.data(); SetTraceToUpload(std::move(trace_data));
}
void BackgroundTracingManagerImpl::SetTraceToUpload(
std::unique_ptr<std::string> trace_data) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (trace_data) {
trace_to_upload_.swap(*trace_data);
} else {
trace_to_upload_.clear();
}
} }
void BackgroundTracingManagerImpl::ValidateStartupScenario() { void BackgroundTracingManagerImpl::ValidateStartupScenario() {
...@@ -315,7 +326,6 @@ BackgroundTracingManagerImpl::RegisterTriggerType(const char* trigger_name) { ...@@ -315,7 +326,6 @@ BackgroundTracingManagerImpl::RegisterTriggerType(const char* trigger_name) {
return static_cast<TriggerHandle>(trigger_handle_ids_); return static_cast<TriggerHandle>(trigger_handle_ids_);
} }
bool BackgroundTracingManagerImpl::IsTriggerHandleValid( bool BackgroundTracingManagerImpl::IsTriggerHandleValid(
BackgroundTracingManager::TriggerHandle handle) const { BackgroundTracingManager::TriggerHandle handle) const {
return trigger_handles_.find(handle) != trigger_handles_.end(); return trigger_handles_.find(handle) != trigger_handles_.end();
...@@ -368,6 +378,10 @@ BackgroundTracingManagerImpl::GenerateMetadataDict() { ...@@ -368,6 +378,10 @@ BackgroundTracingManagerImpl::GenerateMetadataDict() {
return metadata_dict; return metadata_dict;
} }
void BackgroundTracingManagerImpl::AbortScenarioForTesting() {
AbortScenario();
}
void BackgroundTracingManagerImpl::AbortScenario() { void BackgroundTracingManagerImpl::AbortScenario() {
if (active_scenario_) { if (active_scenario_) {
active_scenario_->AbortScenario(); active_scenario_->AbortScenario();
......
...@@ -78,7 +78,7 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager { ...@@ -78,7 +78,7 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager {
bool SetActiveScenario(std::unique_ptr<BackgroundTracingConfig>, bool SetActiveScenario(std::unique_ptr<BackgroundTracingConfig>,
ReceiveCallback, ReceiveCallback,
DataFiltering data_filtering) override; DataFiltering data_filtering) override;
CONTENT_EXPORT void AbortScenario() override; void AbortScenario();
bool HasActiveScenario() override; bool HasActiveScenario() override;
// Named triggers // Named triggers
...@@ -92,6 +92,7 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager { ...@@ -92,6 +92,7 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager {
StartedFinalizingCallback callback); StartedFinalizingCallback callback);
bool HasTraceToUpload() override; bool HasTraceToUpload() override;
std::string GetLatestTraceToUpload() override; std::string GetLatestTraceToUpload() override;
void SetTraceToUpload(std::unique_ptr<std::string> trace_data);
// Add/remove EnabledStateObserver. // Add/remove EnabledStateObserver.
CONTENT_EXPORT void AddEnabledStateObserver(EnabledStateObserver* observer); CONTENT_EXPORT void AddEnabledStateObserver(EnabledStateObserver* observer);
...@@ -116,9 +117,9 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager { ...@@ -116,9 +117,9 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager {
CONTENT_EXPORT void InvalidateTriggerHandlesForTesting(); CONTENT_EXPORT void InvalidateTriggerHandlesForTesting();
CONTENT_EXPORT bool IsTracingForTesting(); CONTENT_EXPORT bool IsTracingForTesting();
void WhenIdle(IdleCallback idle_callback) override; void WhenIdle(IdleCallback idle_callback) override;
CONTENT_EXPORT void AbortScenarioForTesting() override;
CONTENT_EXPORT void SetTraceToUploadForTesting( CONTENT_EXPORT void SetTraceToUploadForTesting(
base::StringPiece data) override; std::unique_ptr<std::string> trace_data) override;
private: private:
friend class base::NoDestructor<BackgroundTracingManagerImpl>; friend class base::NoDestructor<BackgroundTracingManagerImpl>;
...@@ -147,8 +148,9 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager { ...@@ -147,8 +148,9 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager {
IdleCallback idle_callback_; IdleCallback idle_callback_;
base::RepeatingClosure tracing_enabled_callback_for_testing_; base::RepeatingClosure tracing_enabled_callback_for_testing_;
// This field contains serialized trace log proto. // This field contains serialized trace log proto.
std::string trace_to_upload_for_testing_; std::string trace_to_upload_;
DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerImpl); DISALLOW_COPY_AND_ASSIGN(BackgroundTracingManagerImpl);
}; };
......
...@@ -102,8 +102,9 @@ class BackgroundTracingManager { ...@@ -102,8 +102,9 @@ class BackgroundTracingManager {
virtual std::string GetLatestTraceToUpload() = 0; virtual std::string GetLatestTraceToUpload() = 0;
// For tests // For tests
virtual void AbortScenario() = 0; virtual void AbortScenarioForTesting() = 0;
virtual void SetTraceToUploadForTesting(base::StringPiece data) = 0; virtual void SetTraceToUploadForTesting(
std::unique_ptr<std::string> trace_data) = 0;
protected: protected:
virtual ~BackgroundTracingManager() {} virtual ~BackgroundTracingManager() {}
......
...@@ -15,7 +15,8 @@ ...@@ -15,7 +15,8 @@
namespace tracing { namespace tracing {
perfetto::TraceConfig GetDefaultPerfettoConfig( perfetto::TraceConfig GetDefaultPerfettoConfig(
const base::trace_event::TraceConfig& chrome_config) { const base::trace_event::TraceConfig& chrome_config,
bool privacy_filtering_enabled) {
perfetto::TraceConfig perfetto_config; perfetto::TraceConfig perfetto_config;
size_t size_limit = chrome_config.GetTraceBufferSizeInKb(); size_t size_limit = chrome_config.GetTraceBufferSizeInKb();
...@@ -52,6 +53,7 @@ perfetto::TraceConfig GetDefaultPerfettoConfig( ...@@ -52,6 +53,7 @@ perfetto::TraceConfig GetDefaultPerfettoConfig(
trace_event_config->set_target_buffer(0); trace_event_config->set_target_buffer(0);
auto* chrome_proto_config = trace_event_config->mutable_chrome_config(); auto* chrome_proto_config = trace_event_config->mutable_chrome_config();
chrome_proto_config->set_trace_config(chrome_config_string); chrome_proto_config->set_trace_config(chrome_config_string);
chrome_proto_config->set_privacy_filtering_enabled(privacy_filtering_enabled);
// Capture system trace events if supported and enabled. The datasources will // Capture system trace events if supported and enabled. The datasources will
// only emit events if system tracing is enabled in |chrome_config|. // only emit events if system tracing is enabled in |chrome_config|.
...@@ -62,6 +64,8 @@ perfetto::TraceConfig GetDefaultPerfettoConfig( ...@@ -62,6 +64,8 @@ perfetto::TraceConfig GetDefaultPerfettoConfig(
system_trace_config->set_target_buffer(0); system_trace_config->set_target_buffer(0);
auto* system_chrome_config = system_trace_config->mutable_chrome_config(); auto* system_chrome_config = system_trace_config->mutable_chrome_config();
system_chrome_config->set_trace_config(chrome_config_string); system_chrome_config->set_trace_config(chrome_config_string);
system_chrome_config->set_privacy_filtering_enabled(
privacy_filtering_enabled);
#endif #endif
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -70,6 +74,7 @@ perfetto::TraceConfig GetDefaultPerfettoConfig( ...@@ -70,6 +74,7 @@ perfetto::TraceConfig GetDefaultPerfettoConfig(
arc_trace_config->set_target_buffer(0); arc_trace_config->set_target_buffer(0);
auto* arc_chrome_config = arc_trace_config->mutable_chrome_config(); auto* arc_chrome_config = arc_trace_config->mutable_chrome_config();
arc_chrome_config->set_trace_config(chrome_config_string); arc_chrome_config->set_trace_config(chrome_config_string);
arc_chrome_config->set_privacy_filtering_enabled(privacy_filtering_enabled);
#endif #endif
// Also capture global metadata. // Also capture global metadata.
...@@ -79,7 +84,8 @@ perfetto::TraceConfig GetDefaultPerfettoConfig( ...@@ -79,7 +84,8 @@ perfetto::TraceConfig GetDefaultPerfettoConfig(
trace_metadata_config->set_target_buffer(0); trace_metadata_config->set_target_buffer(0);
auto* metadata_chrome_config = trace_metadata_config->mutable_chrome_config(); auto* metadata_chrome_config = trace_metadata_config->mutable_chrome_config();
metadata_chrome_config->set_trace_config(chrome_config_string); metadata_chrome_config->set_trace_config(chrome_config_string);
// TODO(ssid): Also set privacy_filtering_enabled here. metadata_chrome_config->set_privacy_filtering_enabled(
privacy_filtering_enabled);
return perfetto_config; return perfetto_config;
} }
......
...@@ -17,7 +17,8 @@ class TraceConfig; ...@@ -17,7 +17,8 @@ class TraceConfig;
namespace tracing { namespace tracing {
perfetto::TraceConfig COMPONENT_EXPORT(TRACING_CPP) GetDefaultPerfettoConfig( perfetto::TraceConfig COMPONENT_EXPORT(TRACING_CPP) GetDefaultPerfettoConfig(
const base::trace_event::TraceConfig& chrome_config); const base::trace_event::TraceConfig& chrome_config,
bool privacy_filtering_enabled = false);
} // namespace tracing } // namespace tracing
......
...@@ -19,6 +19,11 @@ namespace features { ...@@ -19,6 +19,11 @@ namespace features {
const base::Feature kTracingPerfettoBackend{"TracingPerfettoBackend", const base::Feature kTracingPerfettoBackend{"TracingPerfettoBackend",
base::FEATURE_ENABLED_BY_DEFAULT}; base::FEATURE_ENABLED_BY_DEFAULT};
// Causes the BackgroundTracingManager to upload proto messages via UMA,
// rather than JSON via the crash frontend.
const base::Feature kBackgroundTracingProtoOutput{
"BackgroundTracingProtoOutput", base::FEATURE_DISABLED_BY_DEFAULT};
// Runs the tracing service as an in-process browser service. // Runs the tracing service as an in-process browser service.
const base::Feature kTracingServiceInProcess { const base::Feature kTracingServiceInProcess {
"TracingServiceInProcess", "TracingServiceInProcess",
......
...@@ -21,6 +21,9 @@ extern const COMPONENT_EXPORT(TRACING_CPP) base::Feature ...@@ -21,6 +21,9 @@ extern const COMPONENT_EXPORT(TRACING_CPP) base::Feature
extern const COMPONENT_EXPORT(TRACING_CPP) base::Feature extern const COMPONENT_EXPORT(TRACING_CPP) base::Feature
kTracingServiceInProcess; kTracingServiceInProcess;
extern const COMPONENT_EXPORT(TRACING_CPP) base::Feature
kBackgroundTracingProtoOutput;
} // namespace features } // namespace features
namespace tracing { namespace tracing {
......
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