Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "services/resource_coordinator/public/cpp/tracing/trace_event_agent.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/memory/ref_counted_memory.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "base/trace_event/trace_log.h" | |
| 12 #include "base/values.h" | |
| 13 #include "services/service_manager/public/cpp/connector.h" | |
| 14 | |
| 15 namespace resource_coordinator { | |
| 16 namespace tracing { | |
| 17 | |
| 18 // static | |
| 19 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
| |
| 20 mojom::AgentSetPtr agent_set) { | |
| 21 static TraceEventAgent* instance = nullptr; | |
| 22 if (!instance) { | |
| 23 DCHECK(agent_set); | |
| 24 instance = new TraceEventAgent(std::move(agent_set)); | |
| 25 } | |
| 26 return instance; | |
| 27 } | |
| 28 | |
| 29 TraceEventAgent::TraceEventAgent(mojom::AgentSetPtr agent_set) | |
| 30 : binding_(this) { | |
| 31 // agent_set can be null in tests. The constructor is private and cannot be | |
| 32 // directly used in non-test scenarios. GetOrCreateInstance makes sure that | |
| 33 // the constructor is called with a non-null agent_set. | |
| 34 if (!agent_set) | |
| 35 return; | |
| 36 agent_set->RegisterAgent(binding_.CreateInterfacePtrAndBind(), "traceEvents", | |
| 37 mojom::TraceDataType::ARRAY, | |
| 38 false /* supports_explicit_clock_sync */); | |
| 39 } | |
| 40 | |
| 41 TraceEventAgent::~TraceEventAgent() { | |
| 42 if (base::trace_event::TraceLog::GetInstance()->IsEnabled()) | |
| 43 StopAndFlush(); | |
| 44 } | |
| 45 | |
| 46 void TraceEventAgent::AddMetadataGenerator(MetadataGenerator generator) { | |
| 47 metadata_generators_.push_back(generator); | |
| 48 } | |
| 49 | |
| 50 void TraceEventAgent::StartTracing(const std::string& config, | |
| 51 mojom::RecorderPtr recorder, | |
| 52 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.
| |
| 53 const StartTracingCallback& callback) { | |
| 54 DCHECK(!recorder_); | |
| 55 recorder_ = std::move(recorder); | |
| 56 report_categories_only_ = report_categories_only; | |
| 57 if (!base::trace_event::TraceLog::GetInstance()->IsEnabled()) { | |
| 58 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
| 59 base::trace_event::TraceConfig(config), | |
| 60 base::trace_event::TraceLog::RECORDING_MODE); | |
| 61 } | |
| 62 callback.Run(); | |
| 63 } | |
| 64 | |
| 65 void TraceEventAgent::StopAndFlush() { | |
| 66 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
| 67 if (!recorder_) | |
| 68 return; | |
| 69 if (report_categories_only_) { | |
| 70 std::vector<std::string> category_vector; | |
| 71 base::trace_event::TraceLog::GetInstance()->GetKnownCategoryGroups( | |
| 72 &category_vector); | |
| 73 recorder_->AddCategories(base::JoinString(category_vector, ",")); | |
| 74 } | |
| 75 base::trace_event::TraceLog::GetInstance()->Flush( | |
| 76 base::Bind(&TraceEventAgent::SendChunk, base::Unretained(this))); | |
| 77 } | |
| 78 | |
| 79 void TraceEventAgent::RequestClockSyncMarker( | |
| 80 const std::string& sync_id, | |
| 81 const RequestClockSyncMarkerCallback& callback) { | |
| 82 NOTREACHED() << "This agent does not support explicit clock sync"; | |
| 83 } | |
| 84 | |
| 85 void TraceEventAgent::RequestBufferStatus( | |
| 86 const RequestBufferStatusCallback& callback) { | |
| 87 auto status = base::trace_event::TraceLog::GetInstance()->GetStatus(); | |
| 88 callback.Run(status.event_capacity, status.event_count); | |
| 89 } | |
| 90 | |
| 91 void TraceEventAgent::SendChunk( | |
| 92 const scoped_refptr<base::RefCountedString>& events_str, | |
| 93 bool has_more_events) { | |
| 94 DCHECK(recorder_); | |
| 95 if (!events_str->data().empty() && !report_categories_only_) | |
| 96 recorder_->AddChunk(events_str->data()); | |
| 97 if (!has_more_events) { | |
| 98 if (!report_categories_only_) { | |
| 99 for (const auto& generator : metadata_generators_) { | |
| 100 auto metadata = generator.Run(); | |
| 101 if (metadata) | |
| 102 recorder_->AddMetadata(std::move(metadata)); | |
| 103 } | |
| 104 } | |
| 105 recorder_.reset(); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 } // namespace tracing | |
| 110 } | |
| OLD | NEW |