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

Side by Side Diff: services/resource_coordinator/public/cpp/tracing/chrome_agent.cc

Issue 2833873003: WIP: The tracing service prototype
Patch Set: sync 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 unified diff | Download patch
OLDNEW
(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/chrome_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/trace_event/trace_log.h"
11 #include "services/service_manager/public/cpp/connector.h"
12
13 namespace resource_coordinator {
14 namespace tracing {
15
16 // static
17 void ChromeAgent::InitializeIfNeeded(mojom::AgentSetPtr agent_set) {
18 static ChromeAgent* instance = nullptr;
19 if (!instance)
20 instance = new ChromeAgent(std::move(agent_set));
21 }
22
23 ChromeAgent::ChromeAgent(mojom::AgentSetPtr agent_set)
24 : agent_set_(std::move(agent_set)), binding_(this) {
25 agent_set_->RegisterAgent(binding_.CreateInterfacePtrAndBind(), "traceEvents",
26 mojom::TraceDataType::ARRAY,
27 false /* supports_explicit_clock_sync */);
28 }
29
30 ChromeAgent::~ChromeAgent() {}
31
32 void ChromeAgent::StartTracing(const std::string& config,
33 mojom::RecorderPtr recorder,
34 bool report_categories_only,
35 const StartTracingCallback& callback) {
36 DCHECK(!recorder_);
37 recorder_ = std::move(recorder);
38 report_categories_only_ = report_categories_only;
39 if (!base::trace_event::TraceLog::GetInstance()->IsEnabled()) {
40 base::trace_event::TraceLog::GetInstance()->SetEnabled(
41 base::trace_event::TraceConfig(config),
42 base::trace_event::TraceLog::RECORDING_MODE);
43 }
44 callback.Run();
45 }
46
47 void ChromeAgent::StopAndFlush() {
48 if (!recorder_)
49 return;
50 base::trace_event::TraceLog::GetInstance()->SetDisabled();
51 if (!report_categories_only_) {
52 base::trace_event::TraceLog::GetInstance()->Flush(
53 base::Bind(&ChromeAgent::SendChunk, base::Unretained(this)));
54 } else {
55 base::trace_event::TraceLog::GetInstance()->Flush(
56 base::trace_event::TraceLog::OutputCallback());
57 std::vector<std::string> category_vector;
58 base::trace_event::TraceLog::GetInstance()->GetKnownCategoryGroups(
59 &category_vector);
60 std::string categories_str;
61 bool first = true;
62 for (const auto& category : category_vector) {
63 if (!first)
64 categories_str += ",";
65 first = false;
66 categories_str += category;
67 }
68 recorder_->AddCategories(categories_str);
69 recorder_.reset();
70 }
71 }
72
73 void ChromeAgent::RequestClockSyncMarker(
74 const std::string& sync_id,
75 const RequestClockSyncMarkerCallback& callback) {
76 NOTREACHED() << "This agent does not support explicit clock sync";
77 }
78
79 void ChromeAgent::RequestBufferStatus(
80 const RequestBufferStatusCallback& callback) {
81 auto status = base::trace_event::TraceLog::GetInstance()->GetStatus();
82 callback.Run(status.event_capacity, status.event_count);
83 }
84
85 void ChromeAgent::SendChunk(
86 const scoped_refptr<base::RefCountedString>& events_str,
87 bool has_more_events) {
88 DCHECK(recorder_);
89 if (!events_str->data().empty())
90 recorder_->AddChunk(events_str->data());
91 if (!has_more_events)
92 recorder_.reset();
93 }
94
95 } // namespace tracing
96 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698