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

Side by Side Diff: services/tracing/public/cpp/provider.cc

Issue 2208783002: Make Tracing Service not use outgoing InterfaceProvider, update conventions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 4 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 2015 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/tracing/public/cpp/provider.h"
6
7 #include <utility>
8
9 #include "base/callback.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/platform_thread.h"
16 #include "base/time/time.h"
17 #include "base/trace_event/trace_config.h"
18 #include "base/trace_event/trace_event.h"
19 #include "services/shell/public/cpp/connection.h"
20 #include "services/shell/public/cpp/connector.h"
21 #include "services/tracing/public/cpp/switches.h"
22
23 namespace tracing {
24 namespace {
25
26 // Controls access to |g_tracing_singleton_created|, which can be accessed from
27 // different threads.
28 base::LazyInstance<base::Lock>::Leaky g_singleton_lock =
29 LAZY_INSTANCE_INITIALIZER;
30
31 // Whether we are the first TracingImpl to be created in this mojo
32 // application. The first TracingImpl in a physical mojo application connects
33 // to the mojo:tracing service.
34 //
35 // If this is a ContentHandler, it will outlive all its served Applications. If
36 // this is a raw mojo application, it is the only Application served.
37 bool g_tracing_singleton_created = false;
38
39 }
40
41 Provider::Provider()
42 : binding_(this), tracing_forced_(false), weak_factory_(this) {}
43
44 Provider::~Provider() {
45 StopTracing();
46 }
47
48 void Provider::Initialize(shell::Connector* connector, const std::string& url) {
49 {
50 base::AutoLock lock(g_singleton_lock.Get());
51 if (g_tracing_singleton_created)
52 return;
53 g_tracing_singleton_created = true;
54 }
55
56 // This will only set the name for the first app in a loaded mojo file. It's
57 // up to something like CoreServices to name its own child threads.
58 base::PlatformThread::SetName(url);
59
60 mojom::FactoryPtr factory;
61 connector->ConnectToInterface("mojo:tracing", &factory);
62 mojom::ProviderPtr provider;
63 Bind(GetProxy(&provider));
64 factory->CreateRecorder(std::move(provider));
65 #ifdef NDEBUG
66 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
67 tracing::kEarlyTracing)) {
68 ForceEnableTracing();
69 }
70 #else
71 ForceEnableTracing();
72 #endif
73 }
74
75 void Provider::Bind(mojom::ProviderRequest request) {
76 if (!binding_.is_bound()) {
77 binding_.Bind(std::move(request));
78 } else {
79 LOG(ERROR) << "Cannot accept two connections to TraceProvider.";
80 }
81 }
82
83 void Provider::StartTracing(const std::string& categories,
84 mojom::RecorderPtr recorder) {
85 DCHECK(!recorder_);
86 recorder_ = std::move(recorder);
87 tracing_forced_ = false;
88 if (!base::trace_event::TraceLog::GetInstance()->IsEnabled()) {
89 base::trace_event::TraceLog::GetInstance()->SetEnabled(
90 base::trace_event::TraceConfig(categories,
91 base::trace_event::RECORD_UNTIL_FULL),
92 base::trace_event::TraceLog::RECORDING_MODE);
93 }
94 }
95
96 void Provider::StopTracing() {
97 if (recorder_) {
98 base::trace_event::TraceLog::GetInstance()->SetDisabled();
99
100 base::trace_event::TraceLog::GetInstance()->Flush(
101 base::Bind(&Provider::SendChunk, base::Unretained(this)));
102 }
103 }
104
105 void Provider::ForceEnableTracing() {
106 base::trace_event::TraceLog::GetInstance()->SetEnabled(
107 base::trace_event::TraceConfig("*", base::trace_event::RECORD_UNTIL_FULL),
108 base::trace_event::TraceLog::RECORDING_MODE);
109 tracing_forced_ = true;
110 base::MessageLoop::current()->task_runner()->PostTask(
111 FROM_HERE,
112 base::Bind(&Provider::DelayedStop, weak_factory_.GetWeakPtr()));
113 }
114
115 void Provider::DelayedStop() {
116 // We use this indirection to account for cases where the Initialize method
117 // takes more than one second to finish; thus we start the countdown only when
118 // the current thread is unblocked.
119 base::MessageLoop::current()->task_runner()->PostDelayedTask(
120 FROM_HERE,
121 base::Bind(&Provider::StopIfForced, weak_factory_.GetWeakPtr()),
122 base::TimeDelta::FromSeconds(1));
123 }
124
125 void Provider::StopIfForced() {
126 if (!tracing_forced_) {
127 return;
128 }
129 base::trace_event::TraceLog::GetInstance()->SetDisabled();
130 base::trace_event::TraceLog::GetInstance()->Flush(
131 base::Callback<void(const scoped_refptr<base::RefCountedString>&,
132 bool)>());
133 }
134
135 void Provider::SendChunk(
136 const scoped_refptr<base::RefCountedString>& events_str,
137 bool has_more_events) {
138 DCHECK(recorder_);
139 // The string will be empty if an error eccured or there were no trace
140 // events. Empty string is not a valid chunk to record so skip in this case.
141 if (!events_str->data().empty()) {
142 recorder_->Record(mojo::String(events_str->data()));
143 }
144 if (!has_more_events) {
145 recorder_.reset();
146 }
147 }
148
149 } // namespace tracing
OLDNEW
« no previous file with comments | « services/tracing/public/cpp/provider.h ('k') | services/tracing/public/cpp/trace_provider_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698