Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Unified Diff: services/resource_coordinator/public/cpp/tracing/chrome_trace_event_agent.cc

Issue 2878533003: tracing: the client lib of the tracing service (Closed)
Patch Set: review Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: services/resource_coordinator/public/cpp/tracing/chrome_trace_event_agent.cc
diff --git a/services/resource_coordinator/public/cpp/tracing/chrome_trace_event_agent.cc b/services/resource_coordinator/public/cpp/tracing/chrome_trace_event_agent.cc
new file mode 100644
index 0000000000000000000000000000000000000000..310d7a1be0c4ce1e793329193efcd2c2249287fe
--- /dev/null
+++ b/services/resource_coordinator/public/cpp/tracing/chrome_trace_event_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_trace_event_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::ChromeTraceEventAgent* g_chrome_trace_event_agent;
+} // namespace
+
+namespace tracing {
+
+// static
+ChromeTraceEventAgent* ChromeTraceEventAgent::GetInstance() {
+ return g_chrome_trace_event_agent;
+}
+
+ChromeTraceEventAgent::ChromeTraceEventAgent(
+ mojom::AgentRegistryPtr agent_registry)
+ : binding_(this) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(!g_chrome_trace_event_agent);
+ g_chrome_trace_event_agent = this;
+ // 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 */);
+}
+
+ChromeTraceEventAgent::~ChromeTraceEventAgent() {
+ g_chrome_trace_event_agent = nullptr;
+}
+
+void ChromeTraceEventAgent::AddMetadataGeneratorFunction(
+ MetadataGeneratorFunction generator) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ metadata_generator_functions_.push_back(generator);
+}
+
+void ChromeTraceEventAgent::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 ChromeTraceEventAgent::StopAndFlush() {
+ base::trace_event::TraceLog::GetInstance()->SetDisabled();
+ if (!recorder_)
+ return;
+ for (const auto& generator : metadata_generator_functions_) {
+ auto metadata = generator.Run();
+ if (metadata)
+ recorder_->AddMetadata(std::move(metadata));
+ }
+ base::trace_event::TraceLog::GetInstance()->Flush(base::Bind(
+ &ChromeTraceEventAgent::OnTraceLogFlush, base::Unretained(this)));
+}
+
+void ChromeTraceEventAgent::RequestClockSyncMarker(
+ const std::string& sync_id,
+ const RequestClockSyncMarkerCallback& callback) {
+ NOTREACHED() << "This agent does not support explicit clock sync";
+}
+
+void ChromeTraceEventAgent::RequestBufferStatus(
+ const RequestBufferStatusCallback& callback) {
+ base::trace_event::TraceLogStatus status =
+ base::trace_event::TraceLog::GetInstance()->GetStatus();
+ callback.Run(status.event_capacity, status.event_count);
+}
+
+void ChromeTraceEventAgent::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 ChromeTraceEventAgent::OnTraceLogFlush(
+ const scoped_refptr<base::RefCountedString>& events_str,
+ bool has_more_events) {
+ if (!events_str->data().empty())
+ recorder_->AddChunk(events_str->data());
+ if (!has_more_events)
+ recorder_.reset();
+}
+
+} // namespace tracing

Powered by Google App Engine
This is Rietveld 408576698