Index: services/resource_coordinator/public/cpp/tracing/trace_event_agent.cc |
diff --git a/services/resource_coordinator/public/cpp/tracing/trace_event_agent.cc b/services/resource_coordinator/public/cpp/tracing/trace_event_agent.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..44683fbfccdac467e1f1f0563993f44d78bef1e3 |
--- /dev/null |
+++ b/services/resource_coordinator/public/cpp/tracing/trace_event_agent.cc |
@@ -0,0 +1,110 @@ |
+// 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/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 resource_coordinator { |
+namespace tracing { |
+ |
+// static |
+TraceEventAgent* TraceEventAgent::GetOrCreateInstance( |
Primiano Tucci (use gerrit)
2017/05/11 15:31:07
Not sure what the final intended use of this, but
chiniforooshan
2017/05/15 20:44:11
I need a CreateInstance(agent_registry) which will
Primiano Tucci (use gerrit)
2017/05/16 04:45:11
If this is the case can you just make this TraceEv
chiniforooshan
2017/05/16 15:14:44
But, this is not guaranteed to be called once. Thi
Primiano Tucci (use gerrit)
2017/05/17 00:28:17
Okay I think I am still missing this point. Realis
chiniforooshan
2017/05/17 06:33:46
Agree, let's differ this discussion to the CL that
|
+ mojom::AgentSetPtr agent_set) { |
+ static TraceEventAgent* instance = nullptr; |
+ if (!instance) { |
+ DCHECK(agent_set); |
+ instance = new TraceEventAgent(std::move(agent_set)); |
+ } |
+ return instance; |
+} |
+ |
+TraceEventAgent::TraceEventAgent(mojom::AgentSetPtr agent_set) |
+ : binding_(this) { |
+ // agent_set 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_set. |
+ if (!agent_set) |
+ return; |
+ agent_set->RegisterAgent(binding_.CreateInterfacePtrAndBind(), "traceEvents", |
+ mojom::TraceDataType::ARRAY, |
+ false /* supports_explicit_clock_sync */); |
+} |
+ |
+TraceEventAgent::~TraceEventAgent() { |
+ if (base::trace_event::TraceLog::GetInstance()->IsEnabled()) |
+ StopAndFlush(); |
+} |
+ |
+void TraceEventAgent::AddMetadataGenerator(MetadataGenerator generator) { |
+ metadata_generators_.push_back(generator); |
+} |
+ |
+void TraceEventAgent::StartTracing(const std::string& config, |
+ mojom::RecorderPtr recorder, |
+ bool report_categories_only, |
Primiano Tucci (use gerrit)
2017/05/11 15:31:07
Hmm honestly this report_categories logic seems a
chiniforooshan
2017/05/15 20:44:11
Done.
|
+ const StartTracingCallback& callback) { |
+ DCHECK(!recorder_); |
+ recorder_ = std::move(recorder); |
+ report_categories_only_ = report_categories_only; |
+ 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 TraceEventAgent::StopAndFlush() { |
+ base::trace_event::TraceLog::GetInstance()->SetDisabled(); |
+ if (!recorder_) |
+ return; |
+ if (report_categories_only_) { |
+ std::vector<std::string> category_vector; |
+ base::trace_event::TraceLog::GetInstance()->GetKnownCategoryGroups( |
+ &category_vector); |
+ recorder_->AddCategories(base::JoinString(category_vector, ",")); |
+ } |
+ base::trace_event::TraceLog::GetInstance()->Flush( |
+ base::Bind(&TraceEventAgent::SendChunk, base::Unretained(this))); |
+} |
+ |
+void TraceEventAgent::RequestClockSyncMarker( |
+ const std::string& sync_id, |
+ const RequestClockSyncMarkerCallback& callback) { |
+ NOTREACHED() << "This agent does not support explicit clock sync"; |
+} |
+ |
+void TraceEventAgent::RequestBufferStatus( |
+ const RequestBufferStatusCallback& callback) { |
+ auto status = base::trace_event::TraceLog::GetInstance()->GetStatus(); |
+ callback.Run(status.event_capacity, status.event_count); |
+} |
+ |
+void TraceEventAgent::SendChunk( |
+ const scoped_refptr<base::RefCountedString>& events_str, |
+ bool has_more_events) { |
+ DCHECK(recorder_); |
+ if (!events_str->data().empty() && !report_categories_only_) |
+ recorder_->AddChunk(events_str->data()); |
+ if (!has_more_events) { |
+ if (!report_categories_only_) { |
+ for (const auto& generator : metadata_generators_) { |
+ auto metadata = generator.Run(); |
+ if (metadata) |
+ recorder_->AddMetadata(std::move(metadata)); |
+ } |
+ } |
+ recorder_.reset(); |
+ } |
+} |
+ |
+} // namespace tracing |
+} |