OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "net/log/file_net_log_observer.h" | 5 #include "net/log/file_net_log_observer.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <memory> | 8 #include <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <set> | 10 #include <set> |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 // Is set to true after the first event is written to the log file. | 210 // Is set to true after the first event is written to the log file. |
211 bool first_event_written_; | 211 bool first_event_written_; |
212 | 212 |
213 // Task runner from the file thread. | 213 // Task runner from the file thread. |
214 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 214 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
215 | 215 |
216 DISALLOW_COPY_AND_ASSIGN(UnboundedFileWriter); | 216 DISALLOW_COPY_AND_ASSIGN(UnboundedFileWriter); |
217 }; | 217 }; |
218 | 218 |
219 FileNetLogObserver::FileNetLogObserver( | 219 FileNetLogObserver::FileNetLogObserver( |
220 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) | 220 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
221 : file_task_runner_(file_task_runner) {} | 221 const base::FilePath& directory, |
| 222 size_t max_total_size, |
| 223 size_t total_num_files, |
| 224 std::unique_ptr<base::Value> constants) |
| 225 : file_task_runner_(file_task_runner) { |
| 226 DCHECK_GT(total_num_files, 0u); |
| 227 // The BoundedFileWriter uses a soft limit to write events to file that allows |
| 228 // the size of the file to exceed the limit, but the WriteQueue uses a hard |
| 229 // limit which the size of |WriteQueue::queue_| cannot exceed. Thus, the |
| 230 // BoundedFileWriter may write more events to file than can be contained by |
| 231 // the WriteQueue if they have the same size limit. The maximum size of the |
| 232 // WriteQueue is doubled to allow |WriteQueue::queue_| to hold enough events |
| 233 // for the BoundedFileWriter to fill all files. As long as all events have |
| 234 // sizes <= the size of an individual event file, the discrepancy between the |
| 235 // hard limit and the soft limit will not cause an issue. |
| 236 // TODO(dconnol): Handle the case when the WriteQueue still doesn't |
| 237 // contain enough events to fill all files, because of very large events |
| 238 // relative to file size. |
| 239 file_writer_ = |
| 240 new BoundedFileWriter(directory, max_total_size / total_num_files, |
| 241 total_num_files, file_task_runner_); |
| 242 write_queue_ = new WriteQueue(max_total_size * 2); |
| 243 |
| 244 InitializeFileWriter(std::move(constants)); |
| 245 } |
| 246 |
| 247 FileNetLogObserver::FileNetLogObserver( |
| 248 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 249 const base::FilePath& log_path, |
| 250 std::unique_ptr<base::Value> constants) |
| 251 : file_task_runner_(file_task_runner) { |
| 252 file_writer_ = new UnboundedFileWriter(log_path, file_task_runner_); |
| 253 write_queue_ = new WriteQueue(std::numeric_limits<size_t>::max()); |
| 254 |
| 255 InitializeFileWriter(std::move(constants)); |
| 256 } |
222 | 257 |
223 FileNetLogObserver::~FileNetLogObserver() { | 258 FileNetLogObserver::~FileNetLogObserver() { |
224 if (net_log()) { | 259 if (net_log()) { |
225 // StopObserving was not called. | 260 // StopObserving was not called. |
226 file_task_runner_->PostTask( | 261 file_task_runner_->PostTask( |
227 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, | 262 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, |
228 base::Unretained(file_writer_))); | 263 base::Unretained(file_writer_))); |
229 net_log()->DeprecatedRemoveObserver(this); | 264 net_log()->DeprecatedRemoveObserver(this); |
230 } | 265 } |
231 | |
232 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_); | 266 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_); |
233 } | 267 } |
234 | 268 |
235 void FileNetLogObserver::StartObservingBounded( | 269 void FileNetLogObserver::StartObserving(NetLog* net_log, |
236 NetLog* net_log, | 270 NetLogCaptureMode capture_mode) { |
237 NetLogCaptureMode capture_mode, | |
238 const base::FilePath& directory, | |
239 std::unique_ptr<base::Value> constants, | |
240 URLRequestContext* url_request_context, | |
241 size_t max_total_size, | |
242 size_t total_num_files) { | |
243 DCHECK_GT(total_num_files, 0u); | |
244 | |
245 file_writer_ = | |
246 new BoundedFileWriter(directory, max_total_size / total_num_files, | |
247 total_num_files, file_task_runner_); | |
248 | |
249 // The |file_writer_| uses a soft limit to write events to file that allows | |
250 // the size of the file to exceed the limit, but the |write_queue_| uses a | |
251 // hard limit which the size of the |queue_| cannot exceed. Thus, the | |
252 // |file_writer_| may write more events to file than can be contained by the | |
253 // |write_queue_| if they have the same size limit. The maximum size of the | |
254 // |write_queue_| is doubled to allow the |queue_| to hold enough events for | |
255 // the |file_writer_| to fill all files. As long as all events have sizes <= | |
256 // the size of an individual event file, the discrepancy between the hard | |
257 // limit and the soft limit will not cause an issue. | |
258 // TODO(dconnol): Handle the case when the |write_queue_| still doesn't | |
259 // contain enough events to fill all files, because of very large events | |
260 // relative to file size. | |
261 write_queue_ = new WriteQueue(max_total_size * 2); | |
262 | |
263 StartObservingHelper(net_log, capture_mode, std::move(constants), | |
264 url_request_context); | |
265 } | |
266 | |
267 void FileNetLogObserver::StartObservingUnbounded( | |
268 NetLog* net_log, | |
269 NetLogCaptureMode capture_mode, | |
270 const base::FilePath& filepath, | |
271 std::unique_ptr<base::Value> constants, | |
272 URLRequestContext* url_request_context) { | |
273 file_writer_ = new UnboundedFileWriter(filepath, file_task_runner_); | |
274 | |
275 write_queue_ = new WriteQueue(std::numeric_limits<size_t>::max()); | |
276 | |
277 StartObservingHelper(net_log, capture_mode, std::move(constants), | |
278 url_request_context); | |
279 } | |
280 | |
281 void FileNetLogObserver::StartObservingHelper( | |
282 NetLog* net_log, | |
283 NetLogCaptureMode capture_mode, | |
284 std::unique_ptr<base::Value> constants, | |
285 URLRequestContext* url_request_context) { | |
286 if (!constants) | |
287 constants = GetNetConstants(); | |
288 file_task_runner_->PostTask( | |
289 FROM_HERE, | |
290 base::Bind(&FileNetLogObserver::FileWriter::Initialize, | |
291 base::Unretained(file_writer_), base::Passed(&constants))); | |
292 | |
293 if (url_request_context) { | |
294 DCHECK(url_request_context->CalledOnValidThread()); | |
295 std::set<URLRequestContext*> contexts; | |
296 contexts.insert(url_request_context); | |
297 CreateNetLogEntriesForActiveObjects(contexts, this); | |
298 } | |
299 | |
300 net_log->DeprecatedAddObserver(this, capture_mode); | 271 net_log->DeprecatedAddObserver(this, capture_mode); |
301 } | 272 } |
302 | 273 |
303 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, | 274 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, |
304 const base::Closure& callback) { | 275 const base::Closure& callback) { |
305 file_task_runner_->PostTaskAndReply( | 276 file_task_runner_->PostTaskAndReply( |
306 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, | 277 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, |
307 base::Unretained(file_writer_), write_queue_, | 278 base::Unretained(file_writer_), write_queue_, |
308 base::Passed(&polled_data)), | 279 base::Passed(&polled_data)), |
309 callback); | 280 callback); |
(...skipping 14 matching lines...) Expand all Loading... |
324 // the queue. Because only 1 item is added to the queue at a time, if | 295 // the queue. Because only 1 item is added to the queue at a time, if |
325 // queue_size > kNumWriteQueueEvents a task has already been posted, or will | 296 // queue_size > kNumWriteQueueEvents a task has already been posted, or will |
326 // be posted. | 297 // be posted. |
327 if (queue_size == kNumWriteQueueEvents) { | 298 if (queue_size == kNumWriteQueueEvents) { |
328 file_task_runner_->PostTask( | 299 file_task_runner_->PostTask( |
329 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush, | 300 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush, |
330 base::Unretained(file_writer_), write_queue_)); | 301 base::Unretained(file_writer_), write_queue_)); |
331 } | 302 } |
332 } | 303 } |
333 | 304 |
| 305 void FileNetLogObserver::InitializeFileWriter( |
| 306 std::unique_ptr<base::Value> constants) { |
| 307 if (!constants) |
| 308 constants = GetNetConstants(); |
| 309 file_task_runner_->PostTask( |
| 310 FROM_HERE, |
| 311 base::Bind(&FileNetLogObserver::FileWriter::Initialize, |
| 312 base::Unretained(file_writer_), base::Passed(&constants))); |
| 313 } |
| 314 |
334 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max) | 315 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max) |
335 : memory_(0), memory_max_(memory_max) {} | 316 : memory_(0), memory_max_(memory_max) {} |
336 | 317 |
337 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue( | 318 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue( |
338 std::unique_ptr<std::string> event) { | 319 std::unique_ptr<std::string> event) { |
339 base::AutoLock lock(lock_); | 320 base::AutoLock lock(lock_); |
340 | 321 |
341 memory_ += event->size(); | 322 memory_ += event->size(); |
342 queue_.push(std::move(event)); | 323 queue_.push(std::move(event)); |
343 | 324 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { | 517 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { |
537 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 518 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
538 | 519 |
539 // Reset |file_| to release the file handle so base::DeleteFile can | 520 // Reset |file_| to release the file handle so base::DeleteFile can |
540 // safely access it. | 521 // safely access it. |
541 file_.reset(); | 522 file_.reset(); |
542 base::DeleteFile(file_path_, false); | 523 base::DeleteFile(file_path_, false); |
543 } | 524 } |
544 | 525 |
545 } // namespace net | 526 } // namespace net |
OLD | NEW |