OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/disk_cache/blockfile/in_flight_backend_io.h" | 5 #include "net/disk_cache/blockfile/in_flight_backend_io.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/profiler/scoped_tracker.h" | |
12 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
13 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
14 #include "net/disk_cache/blockfile/backend_impl.h" | 13 #include "net/disk_cache/blockfile/backend_impl.h" |
15 #include "net/disk_cache/blockfile/entry_impl.h" | 14 #include "net/disk_cache/blockfile/entry_impl.h" |
16 #include "net/disk_cache/blockfile/histogram_macros.h" | 15 #include "net/disk_cache/blockfile/histogram_macros.h" |
17 | 16 |
18 // Provide a BackendImpl object to macros from histogram_macros.h. | 17 // Provide a BackendImpl object to macros from histogram_macros.h. |
19 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_ | 18 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_ |
20 | 19 |
21 namespace disk_cache { | 20 namespace disk_cache { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 void BackendIO::OnDone(bool cancel) { | 57 void BackendIO::OnDone(bool cancel) { |
59 if (IsEntryOperation()) { | 58 if (IsEntryOperation()) { |
60 CACHE_UMA(TIMES, "TotalIOTime", 0, ElapsedTime()); | 59 CACHE_UMA(TIMES, "TotalIOTime", 0, ElapsedTime()); |
61 } | 60 } |
62 | 61 |
63 if (!ReturnsEntry()) | 62 if (!ReturnsEntry()) |
64 return; | 63 return; |
65 | 64 |
66 if (result() == net::OK) { | 65 if (result() == net::OK) { |
67 static_cast<EntryImpl*>(*entry_ptr_)->OnEntryCreated(backend_); | 66 static_cast<EntryImpl*>(*entry_ptr_)->OnEntryCreated(backend_); |
68 if (cancel) { | 67 if (cancel) |
69 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is | |
70 // fixed. | |
71 tracked_objects::ScopedTracker tracking_profile( | |
72 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 BackendIO::OnDone")); | |
73 | |
74 (*entry_ptr_)->Close(); | 68 (*entry_ptr_)->Close(); |
75 } | |
76 } | 69 } |
77 } | 70 } |
78 | 71 |
79 bool BackendIO::IsEntryOperation() { | 72 bool BackendIO::IsEntryOperation() { |
80 return operation_ > OP_MAX_BACKEND; | 73 return operation_ > OP_MAX_BACKEND; |
81 } | 74 } |
82 | 75 |
83 // Runs on the background thread. | 76 // Runs on the background thread. |
84 void BackendIO::ReferenceEntry() { | 77 void BackendIO::ReferenceEntry() { |
85 entry_->AddRef(); | 78 entry_->AddRef(); |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 const scoped_refptr<base::SingleThreadTaskRunner>& background_thread) | 327 const scoped_refptr<base::SingleThreadTaskRunner>& background_thread) |
335 : backend_(backend), | 328 : backend_(backend), |
336 background_thread_(background_thread), | 329 background_thread_(background_thread), |
337 ptr_factory_(this) { | 330 ptr_factory_(this) { |
338 } | 331 } |
339 | 332 |
340 InFlightBackendIO::~InFlightBackendIO() { | 333 InFlightBackendIO::~InFlightBackendIO() { |
341 } | 334 } |
342 | 335 |
343 void InFlightBackendIO::Init(const net::CompletionCallback& callback) { | 336 void InFlightBackendIO::Init(const net::CompletionCallback& callback) { |
344 // TODO(vadimt): Remove wrapping the callback with | 337 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
345 // ScopedTracker::TrackCallback() once crbug.com/422516 is fixed. | |
346 scoped_refptr<BackendIO> operation(new BackendIO( | |
347 this, backend_, | |
348 tracked_objects::ScopedTracker::TrackCallback( | |
349 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 InFlightBackendIO::Init"), | |
350 callback))); | |
351 operation->Init(); | 338 operation->Init(); |
352 PostOperation(operation.get()); | 339 PostOperation(operation.get()); |
353 } | 340 } |
354 | 341 |
355 void InFlightBackendIO::OpenEntry(const std::string& key, Entry** entry, | 342 void InFlightBackendIO::OpenEntry(const std::string& key, Entry** entry, |
356 const net::CompletionCallback& callback) { | 343 const net::CompletionCallback& callback) { |
357 scoped_refptr<BackendIO> operation(new BackendIO( | 344 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
358 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
359 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
360 "422516 InFlightBackendIO::OpenEntry"), | |
361 callback))); | |
362 operation->OpenEntry(key, entry); | 345 operation->OpenEntry(key, entry); |
363 PostOperation(operation.get()); | 346 PostOperation(operation.get()); |
364 } | 347 } |
365 | 348 |
366 void InFlightBackendIO::CreateEntry(const std::string& key, Entry** entry, | 349 void InFlightBackendIO::CreateEntry(const std::string& key, Entry** entry, |
367 const net::CompletionCallback& callback) { | 350 const net::CompletionCallback& callback) { |
368 scoped_refptr<BackendIO> operation(new BackendIO( | 351 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
369 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
370 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
371 "422516 InFlightBackendIO::CreateEntry"), | |
372 callback))); | |
373 operation->CreateEntry(key, entry); | 352 operation->CreateEntry(key, entry); |
374 PostOperation(operation.get()); | 353 PostOperation(operation.get()); |
375 } | 354 } |
376 | 355 |
377 void InFlightBackendIO::DoomEntry(const std::string& key, | 356 void InFlightBackendIO::DoomEntry(const std::string& key, |
378 const net::CompletionCallback& callback) { | 357 const net::CompletionCallback& callback) { |
379 scoped_refptr<BackendIO> operation(new BackendIO( | 358 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
380 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
381 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
382 "422516 InFlightBackendIO::DoomEntry"), | |
383 callback))); | |
384 operation->DoomEntry(key); | 359 operation->DoomEntry(key); |
385 PostOperation(operation.get()); | 360 PostOperation(operation.get()); |
386 } | 361 } |
387 | 362 |
388 void InFlightBackendIO::DoomAllEntries( | 363 void InFlightBackendIO::DoomAllEntries( |
389 const net::CompletionCallback& callback) { | 364 const net::CompletionCallback& callback) { |
390 scoped_refptr<BackendIO> operation(new BackendIO( | 365 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
391 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
392 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
393 "422516 InFlightBackendIO::DoomAllEntries"), | |
394 callback))); | |
395 operation->DoomAllEntries(); | 366 operation->DoomAllEntries(); |
396 PostOperation(operation.get()); | 367 PostOperation(operation.get()); |
397 } | 368 } |
398 | 369 |
399 void InFlightBackendIO::DoomEntriesBetween(const base::Time initial_time, | 370 void InFlightBackendIO::DoomEntriesBetween(const base::Time initial_time, |
400 const base::Time end_time, | 371 const base::Time end_time, |
401 const net::CompletionCallback& callback) { | 372 const net::CompletionCallback& callback) { |
402 scoped_refptr<BackendIO> operation(new BackendIO( | 373 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
403 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
404 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
405 "422516 InFlightBackendIO::DoomEntriesBetween"), | |
406 callback))); | |
407 operation->DoomEntriesBetween(initial_time, end_time); | 374 operation->DoomEntriesBetween(initial_time, end_time); |
408 PostOperation(operation.get()); | 375 PostOperation(operation.get()); |
409 } | 376 } |
410 | 377 |
411 void InFlightBackendIO::DoomEntriesSince( | 378 void InFlightBackendIO::DoomEntriesSince( |
412 const base::Time initial_time, const net::CompletionCallback& callback) { | 379 const base::Time initial_time, const net::CompletionCallback& callback) { |
413 scoped_refptr<BackendIO> operation(new BackendIO( | 380 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
414 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
415 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
416 "422516 InFlightBackendIO::DoomEntriesSince"), | |
417 callback))); | |
418 operation->DoomEntriesSince(initial_time); | 381 operation->DoomEntriesSince(initial_time); |
419 PostOperation(operation.get()); | 382 PostOperation(operation.get()); |
420 } | 383 } |
421 | 384 |
422 void InFlightBackendIO::OpenNextEntry(Rankings::Iterator* iterator, | 385 void InFlightBackendIO::OpenNextEntry(Rankings::Iterator* iterator, |
423 Entry** next_entry, | 386 Entry** next_entry, |
424 const net::CompletionCallback& callback) { | 387 const net::CompletionCallback& callback) { |
425 scoped_refptr<BackendIO> operation(new BackendIO( | 388 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
426 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
427 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
428 "422516 InFlightBackendIO::OpenNextEntry"), | |
429 callback))); | |
430 operation->OpenNextEntry(iterator, next_entry); | 389 operation->OpenNextEntry(iterator, next_entry); |
431 PostOperation(operation.get()); | 390 PostOperation(operation.get()); |
432 } | 391 } |
433 | 392 |
434 void InFlightBackendIO::EndEnumeration( | 393 void InFlightBackendIO::EndEnumeration( |
435 scoped_ptr<Rankings::Iterator> iterator) { | 394 scoped_ptr<Rankings::Iterator> iterator) { |
436 scoped_refptr<BackendIO> operation( | 395 scoped_refptr<BackendIO> operation( |
437 new BackendIO(this, backend_, net::CompletionCallback())); | 396 new BackendIO(this, backend_, net::CompletionCallback())); |
438 operation->EndEnumeration(iterator.Pass()); | 397 operation->EndEnumeration(iterator.Pass()); |
439 PostOperation(operation.get()); | 398 PostOperation(operation.get()); |
(...skipping 14 matching lines...) Expand all Loading... |
454 } | 413 } |
455 | 414 |
456 void InFlightBackendIO::DoomEntryImpl(EntryImpl* entry) { | 415 void InFlightBackendIO::DoomEntryImpl(EntryImpl* entry) { |
457 scoped_refptr<BackendIO> operation( | 416 scoped_refptr<BackendIO> operation( |
458 new BackendIO(this, backend_, net::CompletionCallback())); | 417 new BackendIO(this, backend_, net::CompletionCallback())); |
459 operation->DoomEntryImpl(entry); | 418 operation->DoomEntryImpl(entry); |
460 PostOperation(operation.get()); | 419 PostOperation(operation.get()); |
461 } | 420 } |
462 | 421 |
463 void InFlightBackendIO::FlushQueue(const net::CompletionCallback& callback) { | 422 void InFlightBackendIO::FlushQueue(const net::CompletionCallback& callback) { |
464 scoped_refptr<BackendIO> operation(new BackendIO( | 423 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
465 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
466 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
467 "422516 InFlightBackendIO::FlushQueue"), | |
468 callback))); | |
469 operation->FlushQueue(); | 424 operation->FlushQueue(); |
470 PostOperation(operation.get()); | 425 PostOperation(operation.get()); |
471 } | 426 } |
472 | 427 |
473 void InFlightBackendIO::RunTask( | 428 void InFlightBackendIO::RunTask( |
474 const base::Closure& task, const net::CompletionCallback& callback) { | 429 const base::Closure& task, const net::CompletionCallback& callback) { |
475 scoped_refptr<BackendIO> operation(new BackendIO( | 430 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
476 this, backend_, | |
477 tracked_objects::ScopedTracker::TrackCallback( | |
478 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 InFlightBackendIO::RunTask"), | |
479 callback))); | |
480 operation->RunTask(task); | 431 operation->RunTask(task); |
481 PostOperation(operation.get()); | 432 PostOperation(operation.get()); |
482 } | 433 } |
483 | 434 |
484 void InFlightBackendIO::ReadData(EntryImpl* entry, int index, int offset, | 435 void InFlightBackendIO::ReadData(EntryImpl* entry, int index, int offset, |
485 net::IOBuffer* buf, int buf_len, | 436 net::IOBuffer* buf, int buf_len, |
486 const net::CompletionCallback& callback) { | 437 const net::CompletionCallback& callback) { |
487 scoped_refptr<BackendIO> operation(new BackendIO( | 438 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
488 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
489 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
490 "422516 InFlightBackendIO::ReadData"), | |
491 callback))); | |
492 operation->ReadData(entry, index, offset, buf, buf_len); | 439 operation->ReadData(entry, index, offset, buf, buf_len); |
493 PostOperation(operation.get()); | 440 PostOperation(operation.get()); |
494 } | 441 } |
495 | 442 |
496 void InFlightBackendIO::WriteData(EntryImpl* entry, int index, int offset, | 443 void InFlightBackendIO::WriteData(EntryImpl* entry, int index, int offset, |
497 net::IOBuffer* buf, int buf_len, | 444 net::IOBuffer* buf, int buf_len, |
498 bool truncate, | 445 bool truncate, |
499 const net::CompletionCallback& callback) { | 446 const net::CompletionCallback& callback) { |
500 scoped_refptr<BackendIO> operation(new BackendIO( | 447 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
501 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
502 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
503 "422516 InFlightBackendIO::WriteData"), | |
504 callback))); | |
505 operation->WriteData(entry, index, offset, buf, buf_len, truncate); | 448 operation->WriteData(entry, index, offset, buf, buf_len, truncate); |
506 PostOperation(operation.get()); | 449 PostOperation(operation.get()); |
507 } | 450 } |
508 | 451 |
509 void InFlightBackendIO::ReadSparseData( | 452 void InFlightBackendIO::ReadSparseData( |
510 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len, | 453 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len, |
511 const net::CompletionCallback& callback) { | 454 const net::CompletionCallback& callback) { |
512 scoped_refptr<BackendIO> operation(new BackendIO( | 455 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
513 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
514 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
515 "422516 InFlightBackendIO::ReadSparseData"), | |
516 callback))); | |
517 operation->ReadSparseData(entry, offset, buf, buf_len); | 456 operation->ReadSparseData(entry, offset, buf, buf_len); |
518 PostOperation(operation.get()); | 457 PostOperation(operation.get()); |
519 } | 458 } |
520 | 459 |
521 void InFlightBackendIO::WriteSparseData( | 460 void InFlightBackendIO::WriteSparseData( |
522 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len, | 461 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len, |
523 const net::CompletionCallback& callback) { | 462 const net::CompletionCallback& callback) { |
524 scoped_refptr<BackendIO> operation(new BackendIO( | 463 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
525 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
526 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
527 "422516 InFlightBackendIO::WriteSparseData"), | |
528 callback))); | |
529 operation->WriteSparseData(entry, offset, buf, buf_len); | 464 operation->WriteSparseData(entry, offset, buf, buf_len); |
530 PostOperation(operation.get()); | 465 PostOperation(operation.get()); |
531 } | 466 } |
532 | 467 |
533 void InFlightBackendIO::GetAvailableRange( | 468 void InFlightBackendIO::GetAvailableRange( |
534 EntryImpl* entry, int64 offset, int len, int64* start, | 469 EntryImpl* entry, int64 offset, int len, int64* start, |
535 const net::CompletionCallback& callback) { | 470 const net::CompletionCallback& callback) { |
536 scoped_refptr<BackendIO> operation(new BackendIO( | 471 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
537 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
538 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
539 "422516 InFlightBackendIO::GetAvailableRange"), | |
540 callback))); | |
541 operation->GetAvailableRange(entry, offset, len, start); | 472 operation->GetAvailableRange(entry, offset, len, start); |
542 PostOperation(operation.get()); | 473 PostOperation(operation.get()); |
543 } | 474 } |
544 | 475 |
545 void InFlightBackendIO::CancelSparseIO(EntryImpl* entry) { | 476 void InFlightBackendIO::CancelSparseIO(EntryImpl* entry) { |
546 scoped_refptr<BackendIO> operation( | 477 scoped_refptr<BackendIO> operation( |
547 new BackendIO(this, backend_, net::CompletionCallback())); | 478 new BackendIO(this, backend_, net::CompletionCallback())); |
548 operation->CancelSparseIO(entry); | 479 operation->CancelSparseIO(entry); |
549 PostOperation(operation.get()); | 480 PostOperation(operation.get()); |
550 } | 481 } |
551 | 482 |
552 void InFlightBackendIO::ReadyForSparseIO( | 483 void InFlightBackendIO::ReadyForSparseIO( |
553 EntryImpl* entry, const net::CompletionCallback& callback) { | 484 EntryImpl* entry, const net::CompletionCallback& callback) { |
554 scoped_refptr<BackendIO> operation(new BackendIO( | 485 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
555 this, backend_, tracked_objects::ScopedTracker::TrackCallback( | |
556 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
557 "422516 InFlightBackendIO::CancelSparseIO"), | |
558 callback))); | |
559 operation->ReadyForSparseIO(entry); | 486 operation->ReadyForSparseIO(entry); |
560 PostOperation(operation.get()); | 487 PostOperation(operation.get()); |
561 } | 488 } |
562 | 489 |
563 void InFlightBackendIO::WaitForPendingIO() { | 490 void InFlightBackendIO::WaitForPendingIO() { |
564 InFlightIO::WaitForPendingIO(); | 491 InFlightIO::WaitForPendingIO(); |
565 } | 492 } |
566 | 493 |
567 void InFlightBackendIO::OnOperationComplete(BackgroundIO* operation, | 494 void InFlightBackendIO::OnOperationComplete(BackgroundIO* operation, |
568 bool cancel) { | 495 bool cancel) { |
569 BackendIO* op = static_cast<BackendIO*>(operation); | 496 BackendIO* op = static_cast<BackendIO*>(operation); |
570 op->OnDone(cancel); | 497 op->OnDone(cancel); |
571 | 498 |
572 if (!op->callback().is_null() && (!cancel || op->IsEntryOperation())) { | 499 if (!op->callback().is_null() && (!cancel || op->IsEntryOperation())) |
573 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed. | |
574 tracked_objects::ScopedTracker tracking_profile( | |
575 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
576 "422516 InFlightBackendIO::OnOperationComplete")); | |
577 | |
578 op->callback().Run(op->result()); | 500 op->callback().Run(op->result()); |
579 } | |
580 } | 501 } |
581 | 502 |
582 void InFlightBackendIO::PostOperation(BackendIO* operation) { | 503 void InFlightBackendIO::PostOperation(BackendIO* operation) { |
583 background_thread_->PostTask( | 504 background_thread_->PostTask( |
584 FROM_HERE, base::Bind(&BackendIO::ExecuteOperation, operation)); | 505 FROM_HERE, base::Bind(&BackendIO::ExecuteOperation, operation)); |
585 OnOperationPosted(operation); | 506 OnOperationPosted(operation); |
586 } | 507 } |
587 | 508 |
588 base::WeakPtr<InFlightBackendIO> InFlightBackendIO::GetWeakPtr() { | 509 base::WeakPtr<InFlightBackendIO> InFlightBackendIO::GetWeakPtr() { |
589 return ptr_factory_.GetWeakPtr(); | 510 return ptr_factory_.GetWeakPtr(); |
590 } | 511 } |
591 | 512 |
592 } // namespace | 513 } // namespace |
OLD | NEW |