OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/tracing/arc_tracing_agent.h" | 5 #include "content/browser/tracing/arc_tracing_agent.h" |
6 | 6 |
7 #include <memory> | |
7 #include <string> | 8 #include <string> |
9 #include <utility> | |
10 #include <vector> | |
8 | 11 |
9 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | |
14 #include "base/files/file.h" | |
15 #include "base/files/file_descriptor_watcher_posix.h" | |
10 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/ptr_util.h" | |
11 #include "base/memory/singleton.h" | 18 #include "base/memory/singleton.h" |
12 #include "base/threading/thread_checker.h" | 19 #include "base/memory/weak_ptr.h" |
20 #include "base/posix/unix_domain_socket_linux.h" | |
21 #include "base/threading/sequenced_task_runner_handle.h" | |
13 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "base/time/time.h" | |
24 #include "cc/base/ring_buffer.h" | |
25 #include "content/public/browser/browser_thread.h" | |
14 | 26 |
15 namespace content { | 27 namespace content { |
16 | 28 |
17 namespace { | 29 namespace { |
18 | 30 |
31 // The maximum size used to store one trace event. The ad hoc trace format for | |
32 // atrace is 1024 bytes. Here we add additional size as we're using JSON and | |
33 // have additional data field. | |
oystein (OOO til 10th of July)
2017/04/17 21:43:39
nit: s/field/fields/
Earl Ou
2017/04/17 22:03:56
Done.
| |
34 constexpr size_t kArcTraceMessageLength = 1024 + 512; | |
35 | |
19 constexpr char kArcTracingAgentName[] = "arc"; | 36 constexpr char kArcTracingAgentName[] = "arc"; |
20 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; | 37 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; |
21 | 38 |
22 void OnStopTracing(bool success) { | 39 // Number of events for the ring buffer. |
23 DLOG_IF(WARNING, !success) << "Failed to stop ARC tracing."; | 40 constexpr size_t kTraceEventBufferSize = 64000; |
24 } | 41 |
42 // A helper class for reading trace data from the client side. We separate this | |
43 // from |ArcTracingAgentImpl| to isolate the logic that runs on browser's IO | |
44 // thread. All the functions in this class except for constructor, destructor, | |
45 // and |GetWeakPtr| are expected to be run on browser's IO thread. | |
46 class ArcTracingReader { | |
47 public: | |
48 using StopTracingCallback = | |
49 base::Callback<void(const scoped_refptr<base::RefCountedString>&)>; | |
50 | |
51 ArcTracingReader() : weak_ptr_factory_(this) {} | |
52 | |
53 void StartTracing(base::ScopedFD read_fd) { | |
54 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
55 read_fd_ = std::move(read_fd); | |
56 // We don't use the weak pointer returned by |GetWeakPtr| to avoid using it | |
57 // on different task runner. Instead, we use |base::Unretained| here as | |
58 // |fd_watcher_| is always destroyed before |this| is destroyed. | |
59 fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( | |
60 read_fd_.get(), base::Bind(&ArcTracingReader::OnTraceDataAvailable, | |
61 base::Unretained(this))); | |
62 } | |
63 | |
64 void OnTraceDataAvailable() { | |
65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
66 | |
67 char buf[kArcTraceMessageLength]; | |
68 std::vector<base::ScopedFD> unused_fds; | |
69 ssize_t n = base::UnixDomainSocket::RecvMsg( | |
70 read_fd_.get(), buf, kArcTraceMessageLength, &unused_fds); | |
71 // When EOF, return and do nothing. The clean up is done in StopTracing. | |
72 if (n == 0) | |
73 return; | |
74 | |
75 if (n < 0) { | |
76 DPLOG(WARNING) << "Unexpected error while reading trace from client."; | |
77 // Do nothing here as StopTracing will do the clean up and the existing | |
78 // trace logs will be returned. | |
79 return; | |
80 } | |
81 | |
82 if (n > static_cast<ssize_t>(kArcTraceMessageLength)) { | |
83 DLOG(WARNING) << "Unexpected data size when reading trace from client."; | |
84 return; | |
85 } | |
86 ring_buffer_.SaveToBuffer(std::string(buf, n)); | |
87 } | |
88 | |
89 void StopTracing(const StopTracingCallback& callback) { | |
90 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
91 fd_watcher_.reset(); | |
92 read_fd_.reset(); | |
93 | |
94 bool append_comma = false; | |
95 std::string data; | |
96 for (auto it = ring_buffer_.Begin(); it; ++it) { | |
97 if (append_comma) | |
98 data.append(","); | |
99 else | |
100 append_comma = true; | |
101 data.append(**it); | |
102 } | |
103 ring_buffer_.Clear(); | |
104 | |
105 BrowserThread::PostTask( | |
106 BrowserThread::IO, FROM_HERE, | |
107 base::Bind(callback, base::RefCountedString::TakeString(&data))); | |
108 } | |
109 | |
110 base::WeakPtr<ArcTracingReader> GetWeakPtr() { | |
111 return weak_ptr_factory_.GetWeakPtr(); | |
112 } | |
113 | |
114 private: | |
115 base::ScopedFD read_fd_; | |
116 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_; | |
117 cc::RingBuffer<std::string, kTraceEventBufferSize> ring_buffer_; | |
118 // NOTE: Weak pointers must be invalidated before all other member variables | |
119 // so it must be the last member. | |
120 base::WeakPtrFactory<ArcTracingReader> weak_ptr_factory_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(ArcTracingReader); | |
123 }; | |
25 | 124 |
26 class ArcTracingAgentImpl : public ArcTracingAgent { | 125 class ArcTracingAgentImpl : public ArcTracingAgent { |
27 public: | 126 public: |
28 // base::trace_event::TracingAgent overrides: | 127 // base::trace_event::TracingAgent overrides: |
29 std::string GetTracingAgentName() override { return kArcTracingAgentName; } | 128 std::string GetTracingAgentName() override { return kArcTracingAgentName; } |
30 | 129 |
31 std::string GetTraceEventLabel() override { return kArcTraceLabel; } | 130 std::string GetTraceEventLabel() override { return kArcTraceLabel; } |
32 | 131 |
33 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, | 132 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, |
34 const StartAgentTracingCallback& callback) override { | 133 const StartAgentTracingCallback& callback) override { |
35 DCHECK(thread_checker_.CalledOnValidThread()); | 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
135 | |
36 // delegate_ may be nullptr if ARC is not enabled on the system. In such | 136 // delegate_ may be nullptr if ARC is not enabled on the system. In such |
37 // case, simply do nothing. | 137 // case, simply do nothing. |
38 if (!delegate_) { | 138 bool success = (delegate_ != nullptr); |
139 | |
140 base::ScopedFD write_fd, read_fd; | |
141 success = success && CreateSocketPair(&read_fd, &write_fd); | |
142 | |
143 if (!success) { | |
39 // Use PostTask as the convention of TracingAgent. The caller expects | 144 // Use PostTask as the convention of TracingAgent. The caller expects |
40 // callback to be called after this function returns. | 145 // callback to be called after this function returns. |
41 base::ThreadTaskRunnerHandle::Get()->PostTask( | 146 base::ThreadTaskRunnerHandle::Get()->PostTask( |
42 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); | 147 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); |
43 return; | 148 return; |
44 } | 149 } |
45 | 150 |
46 delegate_->StartTracing(trace_config, | 151 BrowserThread::PostTask( |
152 BrowserThread::IO, FROM_HERE, | |
153 base::Bind(&ArcTracingReader::StartTracing, reader_.GetWeakPtr(), | |
154 base::Passed(&read_fd))); | |
155 | |
156 delegate_->StartTracing(trace_config, std::move(write_fd), | |
47 base::Bind(callback, GetTracingAgentName())); | 157 base::Bind(callback, GetTracingAgentName())); |
48 } | 158 } |
49 | 159 |
50 void StopAgentTracing(const StopAgentTracingCallback& callback) override { | 160 void StopAgentTracing(const StopAgentTracingCallback& callback) override { |
51 DCHECK(thread_checker_.CalledOnValidThread()); | 161 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
52 if (delegate_) | |
53 delegate_->StopTracing(base::Bind(OnStopTracing)); | |
54 | 162 |
55 // Trace data is collect via systrace (debugd) in dev-mode. Simply | 163 if (is_stopping_) { |
56 // return empty data here. | 164 DLOG(WARNING) << "Already working on stopping ArcTracingAgent."; |
57 std::string no_data; | 165 return; |
58 base::ThreadTaskRunnerHandle::Get()->PostTask( | 166 } |
59 FROM_HERE, | 167 is_stopping_ = true; |
60 base::Bind(callback, GetTracingAgentName(), GetTraceEventLabel(), | 168 if (delegate_) { |
61 base::RefCountedString::TakeString(&no_data))); | 169 delegate_->StopTracing( |
170 base::Bind(&ArcTracingAgentImpl::OnArcTracingStopped, | |
171 weak_ptr_factory_.GetWeakPtr(), callback)); | |
172 } | |
62 } | 173 } |
63 | 174 |
64 // ArcTracingAgent overrides: | 175 // ArcTracingAgent overrides: |
65 void SetDelegate(Delegate* delegate) override { | 176 void SetDelegate(Delegate* delegate) override { |
66 DCHECK(thread_checker_.CalledOnValidThread()); | 177 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
67 delegate_ = delegate; | 178 delegate_ = delegate; |
68 } | 179 } |
69 | 180 |
70 static ArcTracingAgentImpl* GetInstance() { | 181 static ArcTracingAgentImpl* GetInstance() { |
71 return base::Singleton<ArcTracingAgentImpl>::get(); | 182 return base::Singleton<ArcTracingAgentImpl>::get(); |
72 } | 183 } |
73 | 184 |
74 private: | 185 private: |
75 // This allows constructor and destructor to be private and usable only | 186 // This allows constructor and destructor to be private and usable only |
76 // by the Singleton class. | 187 // by the Singleton class. |
77 friend struct base::DefaultSingletonTraits<ArcTracingAgentImpl>; | 188 friend struct base::DefaultSingletonTraits<ArcTracingAgentImpl>; |
78 | 189 |
79 ArcTracingAgentImpl() = default; | 190 ArcTracingAgentImpl() : weak_ptr_factory_(this) {} |
191 | |
80 ~ArcTracingAgentImpl() override = default; | 192 ~ArcTracingAgentImpl() override = default; |
81 | 193 |
194 void OnArcTracingStopped(const StopAgentTracingCallback& callback, | |
195 bool success) { | |
196 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
197 if (!success) { | |
198 DLOG(WARNING) << "Failed to stop ARC tracing."; | |
199 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), | |
200 new base::RefCountedString()); | |
201 is_stopping_ = false; | |
202 return; | |
203 } | |
204 BrowserThread::PostTask( | |
205 BrowserThread::IO, FROM_HERE, | |
206 base::Bind(&ArcTracingReader::StopTracing, reader_.GetWeakPtr(), | |
207 base::Bind(&ArcTracingAgentImpl::OnTracingReaderStopped, | |
208 weak_ptr_factory_.GetWeakPtr(), callback))); | |
209 } | |
210 | |
211 void OnTracingReaderStopped( | |
212 const StopAgentTracingCallback& callback, | |
213 const scoped_refptr<base::RefCountedString>& data) { | |
214 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
215 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), data); | |
216 is_stopping_ = false; | |
217 } | |
218 | |
82 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. | 219 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. |
83 base::ThreadChecker thread_checker_; | 220 // We use |reader_.GetWeakPtr()| when binding callbacks with its functions. |
221 // Notes that the weak pointer returned by it can only be deferenced or | |
222 // invalided in the same task runner to avoid racing condition. The | |
223 // destruction of |reader_| is also a source of invalidation. However, we're | |
224 // lucky as we're using |ArcTracingAgentImpl| as a singleton, the | |
225 // destruction is always performed after all MessageLoops are destroyed, and | |
226 // thus there are no race conditions in such situation. | |
227 ArcTracingReader reader_; | |
228 bool is_stopping_ = false; | |
229 // NOTE: Weak pointers must be invalidated before all other member variables | |
230 // so it must be the last member. | |
231 base::WeakPtrFactory<ArcTracingAgentImpl> weak_ptr_factory_; | |
84 | 232 |
85 DISALLOW_COPY_AND_ASSIGN(ArcTracingAgentImpl); | 233 DISALLOW_COPY_AND_ASSIGN(ArcTracingAgentImpl); |
86 }; | 234 }; |
87 | 235 |
88 } // namespace | 236 } // namespace |
89 | 237 |
90 // static | 238 // static |
91 ArcTracingAgent* ArcTracingAgent::GetInstance() { | 239 ArcTracingAgent* ArcTracingAgent::GetInstance() { |
92 return ArcTracingAgentImpl::GetInstance(); | 240 return ArcTracingAgentImpl::GetInstance(); |
93 } | 241 } |
94 | 242 |
95 ArcTracingAgent::ArcTracingAgent() = default; | 243 ArcTracingAgent::ArcTracingAgent() = default; |
96 ArcTracingAgent::~ArcTracingAgent() = default; | 244 ArcTracingAgent::~ArcTracingAgent() = default; |
97 | 245 |
98 ArcTracingAgent::Delegate::~Delegate() = default; | 246 ArcTracingAgent::Delegate::~Delegate() = default; |
99 | 247 |
100 } // namespace content | 248 } // namespace content |
OLD | NEW |