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

Side by Side Diff: net/log/file_net_log_observer.cc

Issue 2698143004: Add ongoing events to net-export log when logging starts (Closed)
Patch Set: Updated ios NetExportMessageHandler Created 3 years, 10 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
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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 209
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 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateBounded(
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 DCHECK_GT(total_num_files, 0u);
226 // The BoundedFileWriter uses a soft limit to write events to file that allows
227 // the size of the file to exceed the limit, but the WriteQueue uses a hard
228 // limit which the size of |WriteQueue::queue_| cannot exceed. Thus, the
229 // BoundedFileWriter may write more events to file than can be contained by
230 // the WriteQueue if they have the same size limit. The maximum size of the
231 // WriteQueue is doubled to allow |WriteQueue::queue_| to hold enough events
232 // for the BoundedFileWriter to fill all files. As long as all events have
233 // sizes <= the size of an individual event file, the discrepancy between the
234 // hard limit and the soft limit will not cause an issue.
235 // TODO(dconnol): Handle the case when the WriteQueue still doesn't
236 // contain enough events to fill all files, because of very large events
237 // relative to file size.
238 std::unique_ptr<FileWriter> file_writer(
239 new BoundedFileWriter(directory, max_total_size / total_num_files,
240 total_num_files, file_task_runner));
241
242 scoped_refptr<WriteQueue> write_queue(new WriteQueue(max_total_size * 2));
243
244 return std::unique_ptr<FileNetLogObserver>(
eroman 2017/02/24 00:17:39 [optional] base::MakeUnique. This has the advanta
wangyix1 2017/02/24 00:58:08 Using MakeUnique failed to compile since the FileN
eroman 2017/02/24 01:05:46 Oh right, good point!
245 new FileNetLogObserver(file_task_runner, std::move(file_writer),
246 write_queue, std::move(constants)));
eroman 2017/02/24 00:17:39 std::move(write_queue) --- saves a refcount incre
wangyix1 2017/02/24 00:58:08 Done.
247 }
248
249 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateUnbounded(
250 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
251 const base::FilePath& log_path,
252 std::unique_ptr<base::Value> constants) {
253 std::unique_ptr<FileWriter> file_writer(
254 new UnboundedFileWriter(log_path, file_task_runner));
255
256 scoped_refptr<WriteQueue> write_queue(
257 new WriteQueue(std::numeric_limits<size_t>::max()));
258
259 return std::unique_ptr<FileNetLogObserver>(
eroman 2017/02/24 00:17:39 [optional] same thing with base::MakeUnique
wangyix1 2017/02/24 00:58:08 Using MakeUnique failed to compile since the FileN
260 new FileNetLogObserver(file_task_runner, std::move(file_writer),
261 write_queue, std::move(constants)));
eroman 2017/02/24 00:17:38 std::move(write_queue)
wangyix1 2017/02/24 00:58:08 Done.
262 }
222 263
223 FileNetLogObserver::~FileNetLogObserver() { 264 FileNetLogObserver::~FileNetLogObserver() {
224 if (net_log()) { 265 if (net_log()) {
225 // StopObserving was not called. 266 // StopObserving was not called.
226 file_task_runner_->PostTask( 267 file_task_runner_->PostTask(
227 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, 268 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles,
228 base::Unretained(file_writer_))); 269 base::Unretained(file_writer_)));
229 net_log()->DeprecatedRemoveObserver(this); 270 net_log()->DeprecatedRemoveObserver(this);
230 } 271 }
231
232 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_); 272 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_);
233 } 273 }
234 274
235 void FileNetLogObserver::StartObservingBounded( 275 void FileNetLogObserver::StartObserving(NetLog* net_log,
236 NetLog* net_log, 276 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); 277 net_log->DeprecatedAddObserver(this, capture_mode);
301 } 278 }
302 279
303 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, 280 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data,
304 const base::Closure& callback) { 281 const base::Closure& callback) {
305 file_task_runner_->PostTaskAndReply( 282 file_task_runner_->PostTaskAndReply(
306 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, 283 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop,
307 base::Unretained(file_writer_), write_queue_, 284 base::Unretained(file_writer_), write_queue_,
308 base::Passed(&polled_data)), 285 base::Passed(&polled_data)),
309 callback); 286 callback);
(...skipping 14 matching lines...) Expand all
324 // the queue. Because only 1 item is added to the queue at a time, if 301 // 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 302 // queue_size > kNumWriteQueueEvents a task has already been posted, or will
326 // be posted. 303 // be posted.
327 if (queue_size == kNumWriteQueueEvents) { 304 if (queue_size == kNumWriteQueueEvents) {
328 file_task_runner_->PostTask( 305 file_task_runner_->PostTask(
329 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush, 306 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush,
330 base::Unretained(file_writer_), write_queue_)); 307 base::Unretained(file_writer_), write_queue_));
331 } 308 }
332 } 309 }
333 310
311 FileNetLogObserver::FileNetLogObserver(
312 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
313 std::unique_ptr<FileWriter> file_writer,
314 scoped_refptr<WriteQueue> write_queue,
315 std::unique_ptr<base::Value> constants)
316 : file_task_runner_(file_task_runner),
eroman 2017/02/24 00:17:39 std::move()
wangyix1 2017/02/24 00:58:08 Done.
317 write_queue_(write_queue),
eroman 2017/02/24 00:17:39 std::move()
wangyix1 2017/02/24 00:58:08 Done.
318 file_writer_(file_writer.release()) {
319 if (!constants)
320 constants = GetNetConstants();
321 file_task_runner_->PostTask(
322 FROM_HERE,
323 base::Bind(&FileNetLogObserver::FileWriter::Initialize,
324 base::Unretained(file_writer_), base::Passed(&constants)));
325 }
326
334 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max) 327 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max)
335 : memory_(0), memory_max_(memory_max) {} 328 : memory_(0), memory_max_(memory_max) {}
336 329
337 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue( 330 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue(
338 std::unique_ptr<std::string> event) { 331 std::unique_ptr<std::string> event) {
339 base::AutoLock lock(lock_); 332 base::AutoLock lock(lock_);
340 333
341 memory_ += event->size(); 334 memory_ += event->size();
342 queue_.push(std::move(event)); 335 queue_.push(std::move(event));
343 336
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { 529 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() {
537 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 530 DCHECK(task_runner_->RunsTasksOnCurrentThread());
538 531
539 // Reset |file_| to release the file handle so base::DeleteFile can 532 // Reset |file_| to release the file handle so base::DeleteFile can
540 // safely access it. 533 // safely access it.
541 file_.reset(); 534 file_.reset();
542 base::DeleteFile(file_path_, false); 535 base::DeleteFile(file_path_, false);
543 } 536 }
544 537
545 } // namespace net 538 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698