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 <string.h> | |
8 #include <sys/socket.h> | |
9 | |
10 #include <memory> | |
7 #include <string> | 11 #include <string> |
12 #include <utility> | |
13 #include <vector> | |
8 | 14 |
9 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/bind_helpers.h" | |
17 #include "base/files/file.h" | |
18 #include "base/files/file_descriptor_watcher_posix.h" | |
19 #include "base/json/json_reader.h" | |
10 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/memory/ptr_util.h" | |
11 #include "base/memory/singleton.h" | 22 #include "base/memory/singleton.h" |
12 #include "base/threading/thread_checker.h" | 23 #include "base/posix/unix_domain_socket_linux.h" |
24 #include "base/threading/sequenced_task_runner_handle.h" | |
13 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
26 #include "base/time/time.h" | |
27 #include "base/trace_event/trace_buffer.h" | |
28 #include "content/public/browser/browser_thread.h" | |
29 | |
30 using base::trace_event::TraceEvent; | |
31 using base::trace_event::TraceBuffer; | |
32 using base::trace_event::TraceBufferChunk; | |
14 | 33 |
15 namespace content { | 34 namespace content { |
16 | 35 |
17 namespace { | 36 namespace { |
18 | 37 |
38 constexpr size_t kArcTraceMessageLength = 1024 + 512; | |
19 constexpr char kArcTracingAgentName[] = "arc"; | 39 constexpr char kArcTracingAgentName[] = "arc"; |
20 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; | 40 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; |
21 | 41 |
22 void OnStopTracing(bool success) { | 42 // Number of chunks for the ring buffer. |
23 DLOG_IF(WARNING, !success) << "Failed to stop ARC tracing."; | 43 constexpr size_t kTraceEventChunks = |
24 } | 44 64000 / TraceBufferChunk::kTraceBufferChunkSize; |
45 | |
46 class ArcTracingReader { | |
47 public: | |
48 using StopTracingCallback = | |
49 base::Callback<void(const scoped_refptr<base::RefCountedString>&)>; | |
50 | |
51 ArcTracingReader() = default; | |
52 | |
53 void StartTracing(base::ScopedFD read_fd) { | |
54 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
55 read_fd_ = std::move(read_fd); | |
56 trace_buffer_.reset( | |
57 TraceBuffer::CreateTraceBufferRingBuffer(kTraceEventChunks)); | |
58 chunk_.reset(); | |
59 chunk_index_ = 0; | |
60 fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( | |
61 read_fd_.get(), base::Bind(&ArcTracingReader::OnTraceDataAvailable, | |
62 base::Unretained(this))); | |
63 } | |
64 | |
65 void OnTraceDataAvailable() { | |
66 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
67 | |
68 char buf[kArcTraceMessageLength]; | |
69 std::vector<base::ScopedFD> unused_fds; | |
70 ssize_t n = base::UnixDomainSocket::RecvMsg( | |
71 read_fd_.get(), buf, kArcTraceMessageLength, &unused_fds); | |
72 // When EOF, return and do nothing. The clean up is done in StopTracing. | |
73 if (n == 0) | |
74 return; | |
75 | |
76 if (n < 0) { | |
77 PLOG(WARNING) << "Unexpected error while reading trace from client."; | |
78 // Do nothing here as StopTracing will do the clean up and the existing | |
79 // trace logs will be returned. | |
80 return; | |
81 } | |
82 | |
83 if (n > static_cast<ssize_t>(kArcTraceMessageLength)) { | |
84 LOG(WARNING) << "Unexpected data size when reading trace from client."; | |
85 return; | |
86 } | |
87 | |
88 std::unique_ptr<char[]> data = base::MakeUnique<char[]>(n + 1); | |
89 memcpy(data.get(), buf, n); | |
90 data[n] = 0; | |
91 | |
92 // Here we only validate the data, which should be a JSON string. But we | |
93 // don't really parse them into the fields of |TraceEvent|. It's because | |
94 // |TraceEvent| is designed for Chrome trace. There are many restricion in | |
Luis Héctor Chávez
2017/04/06 00:59:08
nit: s/restricion/restrictions/
Earl Ou
2017/04/06 04:49:20
Done.
| |
95 // its implementation, e.g., no pid and tid together, and no creation of | |
96 // TimeTicks with existing long value. The flags used for |TraceEvent| is | |
97 // also not suitable for our use case here. | |
98 // | |
99 // To avoid the issue, we put entire JSON string in the |name| field | |
100 // instead, so that we can still use |TraceBuffer| and |TraceBufferChunk| | |
101 // for data storage. | |
102 std::unique_ptr<base::Value> value = base::JSONReader::Read(data.get()); | |
103 if (value.get() == nullptr) { | |
104 LOG(WARNING) << "Get incorrect trace data from container."; | |
105 return; | |
106 } | |
107 | |
108 // Save data into trace buffer. | |
109 if (!chunk_) | |
110 chunk_ = trace_buffer_->GetChunk(&chunk_index_); | |
111 size_t event_index; | |
112 TraceEvent* event = chunk_->AddTraceEvent(&event_index); | |
113 | |
114 // If this is a reused event, free the data we saved previously before | |
115 // calling Initialize() again. | |
116 if (event->name() != nullptr) | |
117 delete[] event->name(); | |
118 | |
119 event->Initialize(0, base::TimeTicks(), base::ThreadTicks(), 0, nullptr, | |
120 data.release(), nullptr, 0, 0, 0, nullptr, nullptr, | |
121 nullptr, nullptr, 0); | |
122 | |
123 if (chunk_->IsFull()) { | |
124 trace_buffer_->ReturnChunk(chunk_index_, std::move(chunk_)); | |
125 chunk_ = trace_buffer_->GetChunk(&chunk_index_); | |
126 } | |
127 } | |
128 | |
129 scoped_refptr<base::RefCountedString> StopTracing() { | |
130 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
131 // Stop fd_watcher_. | |
132 fd_watcher_.reset(); | |
133 read_fd_.reset(); | |
134 | |
135 if (chunk_) | |
136 trace_buffer_->ReturnChunk(chunk_index_, std::move(chunk_)); | |
137 | |
138 bool append_comma = false; | |
139 std::string data; | |
140 while (const TraceBufferChunk* chunk = trace_buffer_->NextChunk()) { | |
141 for (size_t i = 0; i < chunk->size(); ++i) { | |
142 const TraceEvent* event = chunk->GetEventAt(i); | |
143 if (append_comma) | |
144 data.append(","); | |
145 // See comment in |OnTraceDataAvailable|. We put the whole valid JSON | |
146 // object in |name|. | |
147 data.append(event->name()); | |
148 append_comma = true; | |
149 delete[] event->name(); | |
150 } | |
151 } | |
152 return base::RefCountedString::TakeString(&data); | |
153 } | |
154 | |
155 private: | |
156 base::ScopedFD read_fd_; | |
157 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_; | |
158 std::unique_ptr<TraceBuffer> trace_buffer_; | |
159 std::unique_ptr<TraceBufferChunk> chunk_; | |
160 size_t chunk_index_ = 0; | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(ArcTracingReader); | |
163 }; | |
25 | 164 |
26 class ArcTracingAgentImpl : public ArcTracingAgent { | 165 class ArcTracingAgentImpl : public ArcTracingAgent { |
27 public: | 166 public: |
28 // base::trace_event::TracingAgent overrides: | 167 // base::trace_event::TracingAgent overrides: |
29 std::string GetTracingAgentName() override { return kArcTracingAgentName; } | 168 std::string GetTracingAgentName() override { return kArcTracingAgentName; } |
30 | 169 |
31 std::string GetTraceEventLabel() override { return kArcTraceLabel; } | 170 std::string GetTraceEventLabel() override { return kArcTraceLabel; } |
32 | 171 |
33 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, | 172 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, |
34 const StartAgentTracingCallback& callback) override { | 173 const StartAgentTracingCallback& callback) override { |
35 DCHECK(thread_checker_.CalledOnValidThread()); | 174 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
175 | |
36 // delegate_ may be nullptr if ARC is not enabled on the system. In such | 176 // delegate_ may be nullptr if ARC is not enabled on the system. In such |
37 // case, simply do nothing. | 177 // case, simply do nothing. |
38 if (!delegate_) { | 178 bool success = delegate_ != nullptr; |
179 | |
180 base::ScopedFD write_fd, read_fd; | |
181 success = success && CreateSocketPair(&read_fd, &write_fd); | |
182 | |
183 if (!success) { | |
39 // Use PostTask as the convention of TracingAgent. The caller expects | 184 // Use PostTask as the convention of TracingAgent. The caller expects |
40 // callback to be called after this function returns. | 185 // callback to be called after this function returns. |
41 base::ThreadTaskRunnerHandle::Get()->PostTask( | 186 base::ThreadTaskRunnerHandle::Get()->PostTask( |
42 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); | 187 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); |
43 return; | 188 return; |
44 } | 189 } |
45 | 190 |
46 delegate_->StartTracing(trace_config, | 191 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
192 base::Bind(&ArcTracingReader::StartTracing, | |
193 base::Unretained(reader_.get()), | |
194 base::Passed(std::move(read_fd)))); | |
195 | |
196 delegate_->StartTracing(trace_config, std::move(write_fd), | |
47 base::Bind(callback, GetTracingAgentName())); | 197 base::Bind(callback, GetTracingAgentName())); |
48 } | 198 } |
49 | 199 |
50 void StopAgentTracing(const StopAgentTracingCallback& callback) override { | 200 void StopAgentTracing(const StopAgentTracingCallback& callback) override { |
51 DCHECK(thread_checker_.CalledOnValidThread()); | 201 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
52 if (delegate_) | |
53 delegate_->StopTracing(base::Bind(OnStopTracing)); | |
54 | 202 |
55 // Trace data is collect via systrace (debugd) in dev-mode. Simply | 203 if (is_stopping) { |
56 // return empty data here. | 204 DLOG(WARNING) << "Already working on stopping ArcTracingAgent."; |
57 std::string no_data; | 205 return; |
58 base::ThreadTaskRunnerHandle::Get()->PostTask( | 206 } |
59 FROM_HERE, | 207 is_stopping = true; |
60 base::Bind(callback, GetTracingAgentName(), GetTraceEventLabel(), | 208 if (delegate_) { |
61 base::RefCountedString::TakeString(&no_data))); | 209 delegate_->StopTracing( |
210 base::Bind(&ArcTracingAgentImpl::OnArcTracingStopped, | |
211 base::Unretained(this), callback)); | |
212 } | |
62 } | 213 } |
63 | 214 |
64 // ArcTracingAgent overrides: | 215 // ArcTracingAgent overrides: |
65 void SetDelegate(Delegate* delegate) override { | 216 void SetDelegate(Delegate* delegate) override { |
66 DCHECK(thread_checker_.CalledOnValidThread()); | 217 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
67 delegate_ = delegate; | 218 delegate_ = delegate; |
68 } | 219 } |
69 | 220 |
70 static ArcTracingAgentImpl* GetInstance() { | 221 static ArcTracingAgentImpl* GetInstance() { |
71 return base::Singleton<ArcTracingAgentImpl>::get(); | 222 return base::Singleton<ArcTracingAgentImpl>::get(); |
72 } | 223 } |
73 | 224 |
74 private: | 225 private: |
75 // This allows constructor and destructor to be private and usable only | 226 // This allows constructor and destructor to be private and usable only |
76 // by the Singleton class. | 227 // by the Singleton class. |
77 friend struct base::DefaultSingletonTraits<ArcTracingAgentImpl>; | 228 friend struct base::DefaultSingletonTraits<ArcTracingAgentImpl>; |
78 | 229 |
79 ArcTracingAgentImpl() = default; | 230 ArcTracingAgentImpl() : reader_(base::MakeUnique<ArcTracingReader>()) {} |
231 | |
80 ~ArcTracingAgentImpl() override = default; | 232 ~ArcTracingAgentImpl() override = default; |
81 | 233 |
234 void OnArcTracingStopped(const StopAgentTracingCallback& callback, | |
235 bool success) { | |
236 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
237 if (!success) { | |
238 DLOG(WARNING) << "Failed to stop ARC tracing."; | |
239 std::string no_data; | |
240 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), | |
241 base::RefCountedString::TakeString(&no_data)); | |
242 is_stopping = false; | |
243 return; | |
244 } | |
245 BrowserThread::PostTaskAndReplyWithResult( | |
246 BrowserThread::IO, FROM_HERE, | |
247 base::Bind(&ArcTracingReader::StopTracing, | |
248 base::Unretained(reader_.get())), | |
249 base::Bind(&ArcTracingAgentImpl::OnTracingReaderStopped, | |
250 base::Unretained(this), callback)); | |
Luis Héctor Chávez
2017/04/06 00:59:08
IIUC there's nothing that guarantees that |this| w
Earl Ou
2017/04/06 04:49:20
Hmm. True. weak_ptr_factory_ is required as |this|
Luis Héctor Chávez
2017/04/07 16:17:46
The destructor of |reader_| is another source of w
Earl Ou
2017/04/08 14:48:17
Done adding the comments above the declaration of
| |
251 } | |
252 | |
253 void OnTracingReaderStopped( | |
254 const StopAgentTracingCallback& callback, | |
255 const scoped_refptr<base::RefCountedString>& data) { | |
256 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
257 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), data); | |
258 is_stopping = false; | |
259 } | |
260 | |
82 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. | 261 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. |
83 base::ThreadChecker thread_checker_; | 262 std::unique_ptr<ArcTracingReader> reader_; |
263 bool is_stopping = false; | |
Luis Héctor Chávez
2017/04/06 00:59:08
nit: |is_stopping_|
Earl Ou
2017/04/06 04:49:20
Done.
| |
84 | 264 |
85 DISALLOW_COPY_AND_ASSIGN(ArcTracingAgentImpl); | 265 DISALLOW_COPY_AND_ASSIGN(ArcTracingAgentImpl); |
86 }; | 266 }; |
87 | 267 |
88 } // namespace | 268 } // namespace |
89 | 269 |
90 // static | 270 // static |
91 ArcTracingAgent* ArcTracingAgent::GetInstance() { | 271 ArcTracingAgent* ArcTracingAgent::GetInstance() { |
92 return ArcTracingAgentImpl::GetInstance(); | 272 return ArcTracingAgentImpl::GetInstance(); |
93 } | 273 } |
94 | 274 |
95 ArcTracingAgent::ArcTracingAgent() = default; | 275 ArcTracingAgent::ArcTracingAgent() = default; |
96 ArcTracingAgent::~ArcTracingAgent() = default; | 276 ArcTracingAgent::~ArcTracingAgent() = default; |
97 | 277 |
98 ArcTracingAgent::Delegate::~Delegate() = default; | 278 ArcTracingAgent::Delegate::~Delegate() = default; |
99 | 279 |
100 } // namespace content | 280 } // namespace content |
OLD | NEW |