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

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: Forgot to update comment for StopNetLog() 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 FileWriter* file_writer =
eroman 2017/02/22 02:03:29 see suggestion in header file -- we try to keep po
wangyix1 2017/02/23 02:14:57 Done.
239 new BoundedFileWriter(directory, max_total_size / total_num_files,
240 total_num_files, file_task_runner);
241 return std::unique_ptr<FileNetLogObserver>(new FileNetLogObserver(
242 file_task_runner, file_writer, max_total_size * 2, std::move(constants)));
243 }
244
245 std::unique_ptr<FileNetLogObserver> FileNetLogObserver::CreateUnbounded(
246 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
247 const base::FilePath& log_path,
248 std::unique_ptr<base::Value> constants) {
249 FileWriter* file_writer = new UnboundedFileWriter(log_path, file_task_runner);
250 return std::unique_ptr<FileNetLogObserver>(new FileNetLogObserver(
251 file_task_runner, file_writer, std::numeric_limits<size_t>::max(),
252 std::move(constants)));
253 }
222 254
223 FileNetLogObserver::~FileNetLogObserver() { 255 FileNetLogObserver::~FileNetLogObserver() {
224 if (net_log()) { 256 if (net_log()) {
225 // StopObserving was not called. 257 // StopObserving was not called.
226 file_task_runner_->PostTask( 258 file_task_runner_->PostTask(
227 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles, 259 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::DeleteAllFiles,
228 base::Unretained(file_writer_))); 260 base::Unretained(file_writer_)));
229 net_log()->DeprecatedRemoveObserver(this); 261 net_log()->DeprecatedRemoveObserver(this);
230 } 262 }
231
232 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_); 263 file_task_runner_->DeleteSoon(FROM_HERE, file_writer_);
233 } 264 }
234 265
235 void FileNetLogObserver::StartObservingBounded( 266 void FileNetLogObserver::StartObserving(NetLog* net_log,
236 NetLog* net_log, 267 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); 268 net_log->DeprecatedAddObserver(this, capture_mode);
301 } 269 }
302 270
303 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data, 271 void FileNetLogObserver::StopObserving(std::unique_ptr<base::Value> polled_data,
304 const base::Closure& callback) { 272 const base::Closure& callback) {
305 file_task_runner_->PostTaskAndReply( 273 file_task_runner_->PostTaskAndReply(
306 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop, 274 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::FlushThenStop,
307 base::Unretained(file_writer_), write_queue_, 275 base::Unretained(file_writer_), write_queue_,
308 base::Passed(&polled_data)), 276 base::Passed(&polled_data)),
309 callback); 277 callback);
(...skipping 14 matching lines...) Expand all
324 // the queue. Because only 1 item is added to the queue at a time, if 292 // 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 293 // queue_size > kNumWriteQueueEvents a task has already been posted, or will
326 // be posted. 294 // be posted.
327 if (queue_size == kNumWriteQueueEvents) { 295 if (queue_size == kNumWriteQueueEvents) {
328 file_task_runner_->PostTask( 296 file_task_runner_->PostTask(
329 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush, 297 FROM_HERE, base::Bind(&FileNetLogObserver::FileWriter::Flush,
330 base::Unretained(file_writer_), write_queue_)); 298 base::Unretained(file_writer_), write_queue_));
331 } 299 }
332 } 300 }
333 301
302 FileNetLogObserver::FileNetLogObserver(
303 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
304 FileWriter* file_writer,
305 size_t write_queue_memory_max,
306 std::unique_ptr<base::Value> constants)
307 : file_task_runner_(file_task_runner),
308 write_queue_(new WriteQueue(write_queue_memory_max)),
309 file_writer_(file_writer) {
310 if (!constants)
311 constants = GetNetConstants();
312 file_task_runner_->PostTask(
313 FROM_HERE,
314 base::Bind(&FileNetLogObserver::FileWriter::Initialize,
315 base::Unretained(file_writer_), base::Passed(&constants)));
316 }
317
334 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max) 318 FileNetLogObserver::WriteQueue::WriteQueue(size_t memory_max)
335 : memory_(0), memory_max_(memory_max) {} 319 : memory_(0), memory_max_(memory_max) {}
336 320
337 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue( 321 size_t FileNetLogObserver::WriteQueue::AddEntryToQueue(
338 std::unique_ptr<std::string> event) { 322 std::unique_ptr<std::string> event) {
339 base::AutoLock lock(lock_); 323 base::AutoLock lock(lock_);
340 324
341 memory_ += event->size(); 325 memory_ += event->size();
342 queue_.push(std::move(event)); 326 queue_.push(std::move(event));
343 327
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() { 520 void FileNetLogObserver::UnboundedFileWriter::DeleteAllFiles() {
537 DCHECK(task_runner_->RunsTasksOnCurrentThread()); 521 DCHECK(task_runner_->RunsTasksOnCurrentThread());
538 522
539 // Reset |file_| to release the file handle so base::DeleteFile can 523 // Reset |file_| to release the file handle so base::DeleteFile can
540 // safely access it. 524 // safely access it.
541 file_.reset(); 525 file_.reset();
542 base::DeleteFile(file_path_, false); 526 base::DeleteFile(file_path_, false);
543 } 527 }
544 528
545 } // namespace net 529 } // namespace net
OLDNEW
« net/log/file_net_log_observer.h ('K') | « net/log/file_net_log_observer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698