Chromium Code Reviews| 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/files/file.h" | |
| 17 #include "base/files/file_descriptor_watcher_posix.h" | |
| 18 #include "base/json/json_reader.h" | |
| 10 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/memory/ptr_util.h" | |
| 11 #include "base/memory/singleton.h" | 21 #include "base/memory/singleton.h" |
| 12 #include "base/threading/thread_checker.h" | 22 #include "base/memory/weak_ptr.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() : weak_ptr_factory_(this) {} | |
| 52 | |
| 53 void StartTracing(int read_fd) { | |
|
Luis Héctor Chávez
2017/04/04 17:28:35
As mentioned below, make this a ScopedFD.
shunhsingou
2017/04/05 03:41:50
Done.
| |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 55 read_fd_.reset(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 weak_ptr_factory_.GetWeakPtr())); | |
| 63 } | |
| 64 | |
| 65 void OnTraceDataAvailable() { | |
| 66 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 67 | |
| 68 char buf[kArcTraceMessageLength + 1]; | |
|
Luis Héctor Chávez
2017/04/04 17:28:35
nit: You don't need the +1 here.
shunhsingou
2017/04/05 03:42:45
Done.
| |
| 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 LOG(WARNING) << "Unexpected error while reading trace from client: " | |
|
Luis Héctor Chávez
2017/04/04 17:28:35
nit: PLOG(WARNING) << "Unexpected error while read
shunhsingou
2017/04/05 03:41:51
Done.
| |
| 78 << strerror(errno); | |
| 79 // Do nothing here as StopTracing will do the clean up and the existing | |
| 80 // trace logs will be returned. | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 if (n > (ssize_t)kArcTraceMessageLength) { | |
|
Luis Héctor Chávez
2017/04/04 17:28:35
Avoid C-style casts. static_cast<ssize_t>(kArcTrac
shunhsingou
2017/04/05 03:41:51
Done.
| |
| 85 LOG(WARNING) << "Unexpected data size when reading trace from client."; | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 char* data = new char[n + 1]; | |
|
Luis Héctor Chávez
2017/04/04 17:28:34
avoid doing this as much as possible. Instead do:
shunhsingou
2017/04/05 03:41:51
Done.
| |
| 90 memcpy(data, buf, n); | |
| 91 data[n] = 0; | |
| 92 | |
| 93 // Here we only validate the data, which should be a JSON string. But we | |
| 94 // don't really parse them into the fields of |TraceEvent|. It's because | |
| 95 // |TraceEvent| is designed for Chrome trace. There are many restricion in | |
| 96 // its implementation, e.g., no pid and tid together, and no creation of | |
| 97 // TimeTicks with existing long value. The flags used for |TraceEvent| is | |
| 98 // also not suitable for our use case here. | |
| 99 // | |
| 100 // To avoid the issue, we put entire JSON string in the |name| field | |
| 101 // instead, so that we can still use |TraceBuffer| and |TraceBufferChunk| | |
| 102 // for data storage. | |
| 103 std::unique_ptr<base::Value> value = base::JSONReader::Read(data); | |
| 104 if (value.get() == nullptr) { | |
| 105 LOG(WARNING) << "Get incorrect trace data from container: " << data; | |
|
Luis Héctor Chávez
2017/04/04 17:28:34
I'd probably not log the data because it might be
shunhsingou
2017/04/05 03:41:51
Done.
| |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 // Save data into trace buffer. | |
| 110 if (!chunk_) | |
| 111 chunk_ = trace_buffer_->GetChunk(&chunk_index_); | |
| 112 size_t event_index; | |
| 113 TraceEvent* event = chunk_->AddTraceEvent(&event_index); | |
| 114 | |
| 115 event->Initialize(0, base::TimeTicks(), base::ThreadTicks(), 0, nullptr, | |
| 116 data, nullptr, 0, 0, 0, nullptr, nullptr, nullptr, | |
| 117 nullptr, 0); | |
| 118 | |
| 119 if (chunk_->IsFull()) { | |
| 120 trace_buffer_->ReturnChunk(chunk_index_, std::move(chunk_)); | |
| 121 chunk_ = trace_buffer_->GetChunk(&chunk_index_); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 void StopTracing(const StopTracingCallback& callback) { | |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 127 // Stop fd_watcher_. | |
| 128 fd_watcher_.reset(); | |
| 129 read_fd_.reset(); | |
| 130 | |
| 131 if (chunk_) | |
| 132 trace_buffer_->ReturnChunk(chunk_index_, std::move(chunk_)); | |
| 133 | |
| 134 bool append_comma = false; | |
| 135 std::string data; | |
| 136 while (const TraceBufferChunk* chunk = trace_buffer_->NextChunk()) { | |
| 137 for (size_t i = 0; i < chunk->size(); ++i) { | |
| 138 const TraceEvent* event = chunk->GetEventAt(i); | |
| 139 if (append_comma) | |
| 140 data.append(","); | |
| 141 // See comment in |OnTraceDataAvailable|. We put the whole valid JSON | |
| 142 // object in |name|. | |
| 143 data.append(event->name()); | |
| 144 append_comma = true; | |
| 145 delete[] event->name(); | |
|
Luis Héctor Chávez
2017/04/04 17:28:35
Eek. What happens if the chunk is evicted because
shunhsingou
2017/04/05 03:41:51
Right. It leaks here. I added a checking code when
| |
| 146 } | |
| 147 } | |
| 148 callback.Run(base::RefCountedString::TakeString(&data)); | |
|
Luis Héctor Chávez
2017/04/04 17:28:34
Keep in mind that this will make OnTracingReaderSt
shunhsingou
2017/04/05 03:41:50
Done.
| |
| 149 } | |
| 150 | |
| 151 base::WeakPtr<ArcTracingReader> GetWeakPtr() { | |
| 152 return weak_ptr_factory_.GetWeakPtr(); | |
| 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 // NOTE: Weak pointers must be invalidated before all other member variables | |
| 163 // so it must be the last member. | |
| 164 base::WeakPtrFactory<ArcTracingReader> weak_ptr_factory_; | |
| 165 }; | |
| 25 | 166 |
| 26 class ArcTracingAgentImpl : public ArcTracingAgent { | 167 class ArcTracingAgentImpl : public ArcTracingAgent { |
| 27 public: | 168 public: |
| 28 // base::trace_event::TracingAgent overrides: | 169 // base::trace_event::TracingAgent overrides: |
| 29 std::string GetTracingAgentName() override { return kArcTracingAgentName; } | 170 std::string GetTracingAgentName() override { return kArcTracingAgentName; } |
| 30 | 171 |
| 31 std::string GetTraceEventLabel() override { return kArcTraceLabel; } | 172 std::string GetTraceEventLabel() override { return kArcTraceLabel; } |
| 32 | 173 |
| 33 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, | 174 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, |
| 34 const StartAgentTracingCallback& callback) override { | 175 const StartAgentTracingCallback& callback) override { |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); | 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 177 | |
| 36 // delegate_ may be nullptr if ARC is not enabled on the system. In such | 178 // delegate_ may be nullptr if ARC is not enabled on the system. In such |
| 37 // case, simply do nothing. | 179 // case, simply do nothing. |
| 38 if (!delegate_) { | 180 bool success = delegate_ != nullptr; |
|
Luis Héctor Chávez
2017/04/04 17:28:34
Creating a socket pair when ARC is not enabled see
shunhsingou
2017/04/05 03:41:50
It should be short-circuited. Fixed the statement
| |
| 181 | |
| 182 base::ScopedFD write_fd, read_fd; | |
| 183 success &= CreateSocketPair(&read_fd, &write_fd); | |
| 184 | |
| 185 if (!success) { | |
| 39 // Use PostTask as the convention of TracingAgent. The caller expects | 186 // Use PostTask as the convention of TracingAgent. The caller expects |
| 40 // callback to be called after this function returns. | 187 // callback to be called after this function returns. |
| 41 base::ThreadTaskRunnerHandle::Get()->PostTask( | 188 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 42 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); | 189 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); |
| 43 return; | 190 return; |
| 44 } | 191 } |
| 45 | 192 |
| 46 delegate_->StartTracing(trace_config, | 193 BrowserThread::PostTask( |
| 194 BrowserThread::IO, FROM_HERE, | |
| 195 base::Bind(&ArcTracingReader::StartTracing, reader_->GetWeakPtr(), | |
| 196 read_fd.release())); | |
|
Luis Héctor Chávez
2017/04/04 17:28:34
you can do base::Passed(std::move(read_fd)) instea
shunhsingou
2017/04/05 03:41:51
Done.
| |
| 197 | |
| 198 delegate_->StartTracing(trace_config, std::move(write_fd), | |
| 47 base::Bind(callback, GetTracingAgentName())); | 199 base::Bind(callback, GetTracingAgentName())); |
| 48 } | 200 } |
| 49 | 201 |
| 50 void StopAgentTracing(const StopAgentTracingCallback& callback) override { | 202 void StopAgentTracing(const StopAgentTracingCallback& callback) override { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | 203 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 52 if (delegate_) | |
| 53 delegate_->StopTracing(base::Bind(OnStopTracing)); | |
| 54 | 204 |
| 55 // Trace data is collect via systrace (debugd) in dev-mode. Simply | 205 if (is_stopping) { |
| 56 // return empty data here. | 206 DLOG(WARNING) << "Already working on stopping ArcTracingAgent."; |
| 57 std::string no_data; | 207 return; |
| 58 base::ThreadTaskRunnerHandle::Get()->PostTask( | 208 } |
| 59 FROM_HERE, | 209 is_stopping = true; |
| 60 base::Bind(callback, GetTracingAgentName(), GetTraceEventLabel(), | 210 if (delegate_) { |
| 61 base::RefCountedString::TakeString(&no_data))); | 211 delegate_->StopTracing( |
| 212 base::Bind(&ArcTracingAgentImpl::OnArcTracingStopped, | |
| 213 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 214 } | |
| 62 } | 215 } |
| 63 | 216 |
| 64 // ArcTracingAgent overrides: | 217 // ArcTracingAgent overrides: |
| 65 void SetDelegate(Delegate* delegate) override { | 218 void SetDelegate(Delegate* delegate) override { |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); | 219 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 67 delegate_ = delegate; | 220 delegate_ = delegate; |
| 68 } | 221 } |
| 69 | 222 |
| 70 static ArcTracingAgentImpl* GetInstance() { | 223 static ArcTracingAgentImpl* GetInstance() { |
| 71 return base::Singleton<ArcTracingAgentImpl>::get(); | 224 return base::Singleton<ArcTracingAgentImpl>::get(); |
| 72 } | 225 } |
| 73 | 226 |
| 74 private: | 227 private: |
| 75 // This allows constructor and destructor to be private and usable only | 228 // This allows constructor and destructor to be private and usable only |
| 76 // by the Singleton class. | 229 // by the Singleton class. |
| 77 friend struct base::DefaultSingletonTraits<ArcTracingAgentImpl>; | 230 friend struct base::DefaultSingletonTraits<ArcTracingAgentImpl>; |
| 78 | 231 |
| 79 ArcTracingAgentImpl() = default; | 232 ArcTracingAgentImpl() : weak_ptr_factory_(this) {} |
| 233 | |
| 80 ~ArcTracingAgentImpl() override = default; | 234 ~ArcTracingAgentImpl() override = default; |
| 81 | 235 |
| 236 void OnArcTracingStopped(const StopAgentTracingCallback& callback, | |
| 237 bool success) { | |
|
Luis Héctor Chávez
2017/04/04 17:28:35
DCHECK_CURRENTLY_ON(BrowserThread::UI);?
shunhsingou
2017/04/05 03:41:50
Done.
| |
| 238 if (!success) { | |
| 239 DLOG(WARNING) << "Failed to stop ARC tracing."; | |
| 240 std::string no_data; | |
| 241 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), | |
| 242 base::RefCountedString::TakeString(&no_data)); | |
| 243 is_stopping = false; | |
| 244 return; | |
| 245 } | |
| 246 BrowserThread::PostTask( | |
| 247 BrowserThread::IO, FROM_HERE, | |
| 248 base::Bind(&ArcTracingReader::StopTracing, reader_->GetWeakPtr(), | |
| 249 base::Bind(&ArcTracingAgentImpl::OnTracingReaderStopped, | |
| 250 weak_ptr_factory_.GetWeakPtr(), callback))); | |
| 251 } | |
| 252 | |
| 253 void OnTracingReaderStopped( | |
| 254 const StopAgentTracingCallback& callback, | |
| 255 const scoped_refptr<base::RefCountedString>& data) { | |
|
Luis Héctor Chávez
2017/04/04 17:28:34
DCHECK_CURRENTLY_ON(BrowserThread::UI);?
shunhsingou
2017/04/05 03:41:50
Done.
| |
| 256 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), data); | |
| 257 is_stopping = false; | |
| 258 } | |
| 259 | |
| 82 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. | 260 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. |
| 83 base::ThreadChecker thread_checker_; | 261 std::unique_ptr<ArcTracingReader> reader_ = |
|
Luis Héctor Chávez
2017/04/04 17:28:34
nit: move the initialization to the constructor.
shunhsingou
2017/04/05 03:41:50
Done.
| |
| 262 base::MakeUnique<ArcTracingReader>(); | |
| 263 bool is_stopping = false; | |
| 264 // NOTE: Weak pointers must be invalidated before all other member variables | |
| 265 // so it must be the last member. | |
| 266 base::WeakPtrFactory<ArcTracingAgentImpl> weak_ptr_factory_; | |
| 84 | 267 |
| 85 DISALLOW_COPY_AND_ASSIGN(ArcTracingAgentImpl); | 268 DISALLOW_COPY_AND_ASSIGN(ArcTracingAgentImpl); |
| 86 }; | 269 }; |
| 87 | 270 |
| 88 } // namespace | 271 } // namespace |
| 89 | 272 |
| 90 // static | 273 // static |
| 91 ArcTracingAgent* ArcTracingAgent::GetInstance() { | 274 ArcTracingAgent* ArcTracingAgent::GetInstance() { |
| 92 return ArcTracingAgentImpl::GetInstance(); | 275 return ArcTracingAgentImpl::GetInstance(); |
| 93 } | 276 } |
| 94 | 277 |
| 95 ArcTracingAgent::ArcTracingAgent() = default; | 278 ArcTracingAgent::ArcTracingAgent() = default; |
| 96 ArcTracingAgent::~ArcTracingAgent() = default; | 279 ArcTracingAgent::~ArcTracingAgent() = default; |
| 97 | 280 |
| 98 ArcTracingAgent::Delegate::~Delegate() = default; | 281 ArcTracingAgent::Delegate::~Delegate() = default; |
| 99 | 282 |
| 100 } // namespace content | 283 } // namespace content |
| OLD | NEW |