Index: services/resource_coordinator/public/cpp/tracing/chrome_agent.cc |
diff --git a/services/resource_coordinator/public/cpp/tracing/chrome_agent.cc b/services/resource_coordinator/public/cpp/tracing/chrome_agent.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e9aa0a6ea31a5bd2db8969a30cf2a9310d758b6e |
--- /dev/null |
+++ b/services/resource_coordinator/public/cpp/tracing/chrome_agent.cc |
@@ -0,0 +1,108 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "services/resource_coordinator/public/cpp/tracing/chrome_agent.h" |
+ |
+#include "base/bind.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/ref_counted_memory.h" |
+#include "base/strings/string_util.h" |
+#include "base/trace_event/trace_log.h" |
+#include "base/values.h" |
+#include "services/service_manager/public/cpp/connector.h" |
+ |
+namespace tracing { |
+ |
+// static |
+ChromeAgent* ChromeAgent::GetOrCreateInstance( |
+ mojom::AgentRegistryPtr agent_registry) { |
+ static ChromeAgent* instance = nullptr; |
+ if (!instance) { |
+ DCHECK(agent_registry); |
+ instance = new ChromeAgent(std::move(agent_registry)); |
+ } |
+ return instance; |
+} |
+ |
+ChromeAgent::ChromeAgent(mojom::AgentRegistryPtr agent_registry) |
+ : binding_(this) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ // agent_registry can be null in tests. The constructor is private and cannot |
+ // be directly used in non-test scenarios. GetOrCreateInstance makes sure that |
+ // the constructor is called with a non-null agent_registry. |
+ if (!agent_registry) |
+ return; |
+ agent_registry->RegisterAgent(binding_.CreateInterfacePtrAndBind(), |
+ "traceEvents", mojom::TraceDataType::ARRAY, |
+ false /* supports_explicit_clock_sync */); |
+} |
+ |
+ChromeAgent::~ChromeAgent() { |
+ if (base::trace_event::TraceLog::GetInstance()->IsEnabled()) |
+ StopAndFlush(); |
+} |
+ |
+void ChromeAgent::AddMetadataGeneratorFunction( |
+ MetadataGeneratorFunction generator) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ metadata_generator_functions_.push_back(generator); |
+} |
+ |
+void ChromeAgent::StartTracing(const std::string& config, |
+ mojom::RecorderPtr recorder, |
+ const StartTracingCallback& callback) { |
+ DCHECK(!recorder_); |
+ recorder_ = std::move(recorder); |
+ if (!base::trace_event::TraceLog::GetInstance()->IsEnabled()) { |
+ base::trace_event::TraceLog::GetInstance()->SetEnabled( |
+ base::trace_event::TraceConfig(config), |
+ base::trace_event::TraceLog::RECORDING_MODE); |
+ } |
+ callback.Run(); |
+} |
+ |
+void ChromeAgent::StopAndFlush() { |
+ base::trace_event::TraceLog::GetInstance()->SetDisabled(); |
+ if (!recorder_) |
+ return; |
+ base::trace_event::TraceLog::GetInstance()->Flush( |
+ base::Bind(&ChromeAgent::SendChunk, base::Unretained(this))); |
+} |
+ |
+void ChromeAgent::RequestClockSyncMarker( |
+ const std::string& sync_id, |
+ const RequestClockSyncMarkerCallback& callback) { |
+ NOTREACHED() << "This agent does not support explicit clock sync"; |
+} |
+ |
+void ChromeAgent::RequestBufferStatus( |
+ const RequestBufferStatusCallback& callback) { |
+ auto status = base::trace_event::TraceLog::GetInstance()->GetStatus(); |
+ callback.Run(status.event_capacity, status.event_count); |
+} |
+ |
+void ChromeAgent::GetCategories(const GetCategoriesCallback& callback) { |
+ std::vector<std::string> category_vector; |
+ base::trace_event::TraceLog::GetInstance()->GetKnownCategoryGroups( |
+ &category_vector); |
+ callback.Run(base::JoinString(category_vector, ",")); |
+} |
+ |
+void ChromeAgent::SendChunk( |
Primiano Tucci (use gerrit)
2017/05/16 04:45:11
minor comment: I'd probably call this OnTraceLogFl
chiniforooshan
2017/05/16 15:14:44
Done.
|
+ const scoped_refptr<base::RefCountedString>& events_str, |
+ bool has_more_events) { |
+ DCHECK(recorder_); |
Primiano Tucci (use gerrit)
2017/05/16 04:45:11
I think the pattern is to not add dhcecks if we ar
chiniforooshan
2017/05/16 15:14:45
Done.
|
+ if (!events_str->data().empty()) |
+ recorder_->AddChunk(events_str->data()); |
+ if (!has_more_events) { |
+ for (const auto& generator : metadata_generator_functions_) { |
+ auto metadata = generator.Run(); |
+ if (metadata) |
+ recorder_->AddMetadata(std::move(metadata)); |
+ } |
+ recorder_.reset(); |
+ } |
+} |
+ |
+} // namespace tracing |