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

Side by Side Diff: net/disk_cache/entry_impl.cc

Issue 4067002: First pass at adding http/backend cache to NetLog (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Minor cleanup Created 10 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2010 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/entry_impl.h" 5 #include "net/disk_cache/entry_impl.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "base/values.h"
10 #include "net/base/io_buffer.h" 11 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h" 12 #include "net/base/net_errors.h"
12 #include "net/disk_cache/backend_impl.h" 13 #include "net/disk_cache/backend_impl.h"
13 #include "net/disk_cache/bitmap.h" 14 #include "net/disk_cache/bitmap.h"
14 #include "net/disk_cache/cache_util.h" 15 #include "net/disk_cache/cache_util.h"
15 #include "net/disk_cache/histogram_macros.h" 16 #include "net/disk_cache/histogram_macros.h"
16 #include "net/disk_cache/sparse_control.h" 17 #include "net/disk_cache/sparse_control.h"
17 18
18 using base::Time; 19 using base::Time;
19 using base::TimeDelta; 20 using base::TimeDelta;
20 using base::TimeTicks; 21 using base::TimeTicks;
21 22
22 namespace { 23 namespace {
23 24
24 // Index for the file used to store the key, if any (files_[kKeyFileIndex]). 25 // Index for the file used to store the key, if any (files_[kKeyFileIndex]).
25 const int kKeyFileIndex = 3; 26 const int kKeyFileIndex = 3;
26 27
28 // NetLog parameters for the creation of an EntryImpl. Contains an entry's name
29 // and whether it was created or opened.
30 class EntryCreationParameters : public net::NetLog::EventParameters {
31 public:
32 EntryCreationParameters(const std::string& key, bool created)
33 : key_(key), created_(created) {
34 }
35
36 Value* ToValue() const {
37 DictionaryValue *dict = new DictionaryValue();
eroman 2011/01/05 02:39:06 nit: DictionaryValue*
mmenke 2011/01/05 16:32:25 DOn 2011/01/05 02:39:06, eroman wrote:
38 dict->SetString("key", key_);
39 dict->SetBoolean("created", created_);
40 return dict;
41 }
42
43 private:
44 const std::string key_;
45 const bool created_;
46 };
47
48 // NetLog parameters for both reading and writing to an EntryImpl.
49 class ReadWriteDataParams : public net::NetLog::EventParameters {
50 public:
51 // For reads, |truncate| must be false.
52 ReadWriteDataParams(int index, int offset, int buf_len, bool truncate)
53 : index_(index), offset_(offset), buf_len_(buf_len), truncate_(truncate) {
54 }
55
56 Value* ToValue() const {
57 DictionaryValue* dict = new DictionaryValue();
58 dict->SetInteger("index", index_);
59 dict->SetInteger("offset", offset_);
60 dict->SetInteger("buf_len", buf_len_);
61 if (truncate_)
62 dict->SetBoolean("truncate", truncate_);
63 return dict;
64 }
65
66 private:
67 int index_;
eroman 2011/01/05 02:39:06 nit: can you make these all const ?
mmenke 2011/01/05 16:32:25 Done.
68 int offset_;
69 int buf_len_;
70 bool truncate_;
71 };
72
27 // This class implements FileIOCallback to buffer the callback from a file IO 73 // This class implements FileIOCallback to buffer the callback from a file IO
28 // operation from the actual net class. 74 // operation from the actual net class.
29 class SyncCallback: public disk_cache::FileIOCallback { 75 class SyncCallback: public disk_cache::FileIOCallback {
30 public: 76 public:
77 // |end_event_type| is the event type to log on completion. Logs nothing on
78 // discard, or when the NetLog is not set to log all events.
31 SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer, 79 SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer,
32 net::CompletionCallback* callback ) 80 net::CompletionCallback* callback,
81 net::NetLog::EventType end_event_type)
33 : entry_(entry), callback_(callback), buf_(buffer), 82 : entry_(entry), callback_(callback), buf_(buffer),
34 start_(TimeTicks::Now()) { 83 start_(TimeTicks::Now()), end_event_type_(end_event_type) {
35 entry->AddRef(); 84 entry->AddRef();
36 entry->IncrementIoCount(); 85 entry->IncrementIoCount();
37 } 86 }
38 ~SyncCallback() {} 87 ~SyncCallback() {}
39 88
40 virtual void OnFileIOComplete(int bytes_copied); 89 virtual void OnFileIOComplete(int bytes_copied);
41 void Discard(); 90 void Discard();
91
42 private: 92 private:
43 disk_cache::EntryImpl* entry_; 93 disk_cache::EntryImpl* entry_;
44 net::CompletionCallback* callback_; 94 net::CompletionCallback* callback_;
45 scoped_refptr<net::IOBuffer> buf_; 95 scoped_refptr<net::IOBuffer> buf_;
46 TimeTicks start_; 96 TimeTicks start_;
97 net::NetLog::EventType end_event_type_;
47 98
48 DISALLOW_COPY_AND_ASSIGN(SyncCallback); 99 DISALLOW_COPY_AND_ASSIGN(SyncCallback);
49 }; 100 };
50 101
51 void SyncCallback::OnFileIOComplete(int bytes_copied) { 102 void SyncCallback::OnFileIOComplete(int bytes_copied) {
52 entry_->DecrementIoCount(); 103 entry_->DecrementIoCount();
53 if (callback_) { 104 if (callback_) {
105 if (entry_->net_log().IsLoggingAllEvents())
106 entry_->net_log().EndEventWithErrorCode(end_event_type_, bytes_copied);
eroman 2011/01/05 02:39:06 Would we want to log the actual bytes copied on no
mmenke 2011/01/05 16:32:25 You do, indeed, understand the code. With writes,
54 entry_->ReportIOTime(disk_cache::EntryImpl::kAsyncIO, start_); 107 entry_->ReportIOTime(disk_cache::EntryImpl::kAsyncIO, start_);
55 callback_->Run(bytes_copied); 108 callback_->Run(bytes_copied);
56 } 109 }
57 entry_->Release(); 110 entry_->Release();
58 delete this; 111 delete this;
59 } 112 }
60 113
61 void SyncCallback::Discard() { 114 void SyncCallback::Discard() {
62 callback_ = NULL; 115 callback_ = NULL;
63 buf_ = NULL; 116 buf_ = NULL;
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 // maybe for a child entry, so it is important to do it before deleting this 354 // maybe for a child entry, so it is important to do it before deleting this
302 // entry. 355 // entry.
303 sparse_.reset(); 356 sparse_.reset();
304 357
305 // Remove this entry from the list of open entries. 358 // Remove this entry from the list of open entries.
306 backend_->OnEntryDestroyBegin(entry_.address()); 359 backend_->OnEntryDestroyBegin(entry_.address());
307 360
308 if (doomed_) { 361 if (doomed_) {
309 DeleteEntryData(true); 362 DeleteEntryData(true);
310 } else { 363 } else {
364 net_log_.AddEvent(net::NetLog::TYPE_DISK_CACHE_CLOSE, NULL);
311 bool ret = true; 365 bool ret = true;
312 for (int index = 0; index < kNumStreams; index++) { 366 for (int index = 0; index < kNumStreams; index++) {
313 if (user_buffers_[index].get()) { 367 if (user_buffers_[index].get()) {
314 if (!(ret = Flush(index, 0))) 368 if (!(ret = Flush(index, 0)))
315 LOG(ERROR) << "Failed to save user data"; 369 LOG(ERROR) << "Failed to save user data";
316 } 370 }
317 if (unreported_size_[index]) { 371 if (unreported_size_[index]) {
318 backend_->ModifyStorageSize( 372 backend_->ModifyStorageSize(
319 entry_.Data()->data_size[index] - unreported_size_[index], 373 entry_.Data()->data_size[index] - unreported_size_[index],
320 entry_.Data()->data_size[index]); 374 entry_.Data()->data_size[index]);
321 } 375 }
322 } 376 }
323 377
324 if (!ret) { 378 if (!ret) {
325 // There was a failure writing the actual data. Mark the entry as dirty. 379 // There was a failure writing the actual data. Mark the entry as dirty.
326 int current_id = backend_->GetCurrentEntryId(); 380 int current_id = backend_->GetCurrentEntryId();
327 node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1; 381 node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1;
328 node_.Store(); 382 node_.Store();
329 } else if (node_.HasData() && node_.Data()->dirty) { 383 } else if (node_.HasData() && node_.Data()->dirty) {
330 node_.Data()->dirty = 0; 384 node_.Data()->dirty = 0;
331 node_.Store(); 385 node_.Store();
332 } 386 }
333 } 387 }
334 388
335 Trace("~EntryImpl out 0x%p", reinterpret_cast<void*>(this)); 389 Trace("~EntryImpl out 0x%p", reinterpret_cast<void*>(this));
390 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_ENTRY, NULL);
336 backend_->OnEntryDestroyEnd(); 391 backend_->OnEntryDestroyEnd();
337 } 392 }
338 393
339 void EntryImpl::Doom() { 394 void EntryImpl::Doom() {
340 backend_->background_queue()->DoomEntryImpl(this); 395 backend_->background_queue()->DoomEntryImpl(this);
341 } 396 }
342 397
343 void EntryImpl::Close() { 398 void EntryImpl::Close() {
344 backend_->background_queue()->CloseEntryImpl(this); 399 backend_->background_queue()->CloseEntryImpl(this);
345 } 400 }
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 void EntryImpl::DoomImpl() { 535 void EntryImpl::DoomImpl() {
481 if (doomed_) 536 if (doomed_)
482 return; 537 return;
483 538
484 SetPointerForInvalidEntry(backend_->GetCurrentEntryId()); 539 SetPointerForInvalidEntry(backend_->GetCurrentEntryId());
485 backend_->InternalDoomEntry(this); 540 backend_->InternalDoomEntry(this);
486 } 541 }
487 542
488 int EntryImpl::ReadDataImpl(int index, int offset, net::IOBuffer* buf, 543 int EntryImpl::ReadDataImpl(int index, int offset, net::IOBuffer* buf,
489 int buf_len, CompletionCallback* callback) { 544 int buf_len, CompletionCallback* callback) {
545 if (net_log_.IsLoggingAllEvents()) {
546 net_log_.BeginEvent(
547 net::NetLog::TYPE_DISK_CACHE_READ_DATA,
548 make_scoped_refptr(
549 new ReadWriteDataParams(index, offset, buf_len, false)));
550 }
551
552 int result = InternalReadData(index, offset, buf, buf_len, callback);
553
554 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) {
555 net_log_.EndEventWithErrorCode(net::NetLog::TYPE_DISK_CACHE_READ_DATA,
556 result);
557 }
558 return result;
559 }
560
561 int EntryImpl::WriteDataImpl(int index, int offset, net::IOBuffer* buf,
562 int buf_len, CompletionCallback* callback,
563 bool truncate) {
564 if (net_log_.IsLoggingAllEvents()) {
565 net_log_.BeginEvent(
566 net::NetLog::TYPE_DISK_CACHE_WRITE_DATA,
567 make_scoped_refptr(
568 new ReadWriteDataParams(index, offset, buf_len, truncate)));
569 }
570
571 int result = InternalWriteData(index, offset, buf, buf_len, callback,
572 truncate);
573
574 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) {
575 net_log_.EndEventWithErrorCode(net::NetLog::TYPE_DISK_CACHE_WRITE_DATA,
576 result);
577 }
578 return result;
579 }
580
581 int EntryImpl::InternalReadData(int index, int offset, net::IOBuffer* buf,
582 int buf_len, CompletionCallback* callback) {
490 DCHECK(node_.Data()->dirty || read_only_); 583 DCHECK(node_.Data()->dirty || read_only_);
491 DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len; 584 DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len;
492 if (index < 0 || index >= kNumStreams) 585 if (index < 0 || index >= kNumStreams)
493 return net::ERR_INVALID_ARGUMENT; 586 return net::ERR_INVALID_ARGUMENT;
494 587
495 int entry_size = entry_.Data()->data_size[index]; 588 int entry_size = entry_.Data()->data_size[index];
496 if (offset >= entry_size || offset < 0 || !buf_len) 589 if (offset >= entry_size || offset < 0 || !buf_len)
497 return 0; 590 return 0;
498 591
499 if (buf_len < 0) 592 if (buf_len < 0)
(...skipping 29 matching lines...) Expand all
529 return net::ERR_FAILED; 622 return net::ERR_FAILED;
530 623
531 size_t file_offset = offset; 624 size_t file_offset = offset;
532 if (address.is_block_file()) { 625 if (address.is_block_file()) {
533 DCHECK_LE(offset + buf_len, kMaxBlockSize); 626 DCHECK_LE(offset + buf_len, kMaxBlockSize);
534 file_offset += address.start_block() * address.BlockSize() + 627 file_offset += address.start_block() * address.BlockSize() +
535 kBlockHeaderSize; 628 kBlockHeaderSize;
536 } 629 }
537 630
538 SyncCallback* io_callback = NULL; 631 SyncCallback* io_callback = NULL;
539 if (callback) 632 if (callback) {
540 io_callback = new SyncCallback(this, buf, callback); 633 io_callback = new SyncCallback(this, buf, callback,
634 net::NetLog::TYPE_DISK_CACHE_READ_DATA);
635 }
541 636
542 bool completed; 637 bool completed;
543 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) { 638 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) {
544 if (io_callback) 639 if (io_callback)
545 io_callback->Discard(); 640 io_callback->Discard();
546 return net::ERR_FAILED; 641 return net::ERR_FAILED;
547 } 642 }
548 643
549 if (io_callback && completed) 644 if (io_callback && completed)
550 io_callback->Discard(); 645 io_callback->Discard();
551 646
552 ReportIOTime(kRead, start); 647 ReportIOTime(kRead, start);
553 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING; 648 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING;
554 } 649 }
555 650
556 int EntryImpl::WriteDataImpl(int index, int offset, net::IOBuffer* buf, 651 int EntryImpl::InternalWriteData(int index, int offset, net::IOBuffer* buf,
557 int buf_len, CompletionCallback* callback, 652 int buf_len, CompletionCallback* callback,
558 bool truncate) { 653 bool truncate) {
559 DCHECK(node_.Data()->dirty || read_only_); 654 DCHECK(node_.Data()->dirty || read_only_);
560 DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len; 655 DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len;
561 if (index < 0 || index >= kNumStreams) 656 if (index < 0 || index >= kNumStreams)
562 return net::ERR_INVALID_ARGUMENT; 657 return net::ERR_INVALID_ARGUMENT;
563 658
564 if (offset < 0 || buf_len < 0) 659 if (offset < 0 || buf_len < 0)
565 return net::ERR_INVALID_ARGUMENT; 660 return net::ERR_INVALID_ARGUMENT;
566 661
567 int max_file_size = backend_->MaxFileSize(); 662 int max_file_size = backend_->MaxFileSize();
568 663
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 kBlockHeaderSize; 716 kBlockHeaderSize;
622 } else if (truncate || (extending && !buf_len)) { 717 } else if (truncate || (extending && !buf_len)) {
623 if (!file->SetLength(offset + buf_len)) 718 if (!file->SetLength(offset + buf_len))
624 return net::ERR_FAILED; 719 return net::ERR_FAILED;
625 } 720 }
626 721
627 if (!buf_len) 722 if (!buf_len)
628 return 0; 723 return 0;
629 724
630 SyncCallback* io_callback = NULL; 725 SyncCallback* io_callback = NULL;
631 if (callback) 726 if (callback) {
632 io_callback = new SyncCallback(this, buf, callback); 727 io_callback = new SyncCallback(this, buf, callback,
728 net::NetLog::TYPE_DISK_CACHE_WRITE_DATA);
729 }
633 730
634 bool completed; 731 bool completed;
635 if (!file->Write(buf->data(), buf_len, file_offset, io_callback, 732 if (!file->Write(buf->data(), buf_len, file_offset, io_callback,
636 &completed)) { 733 &completed)) {
637 if (io_callback) 734 if (io_callback)
638 io_callback->Discard(); 735 io_callback->Discard();
639 return net::ERR_FAILED; 736 return net::ERR_FAILED;
640 } 737 }
641 738
642 if (io_callback && completed) 739 if (io_callback && completed)
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 bool EntryImpl::IsSameEntry(const std::string& key, uint32 hash) { 847 bool EntryImpl::IsSameEntry(const std::string& key, uint32 hash) {
751 if (entry_.Data()->hash != hash || 848 if (entry_.Data()->hash != hash ||
752 static_cast<size_t>(entry_.Data()->key_len) != key.size()) 849 static_cast<size_t>(entry_.Data()->key_len) != key.size())
753 return false; 850 return false;
754 851
755 std::string my_key = GetKey(); 852 std::string my_key = GetKey();
756 return key.compare(my_key) ? false : true; 853 return key.compare(my_key) ? false : true;
757 } 854 }
758 855
759 void EntryImpl::InternalDoom() { 856 void EntryImpl::InternalDoom() {
857 net_log_.AddEvent(net::NetLog::TYPE_DISK_CACHE_DOOM, NULL);
760 DCHECK(node_.HasData()); 858 DCHECK(node_.HasData());
761 if (!node_.Data()->dirty) { 859 if (!node_.Data()->dirty) {
762 node_.Data()->dirty = backend_->GetCurrentEntryId(); 860 node_.Data()->dirty = backend_->GetCurrentEntryId();
763 node_.Store(); 861 node_.Store();
764 } 862 }
765 doomed_ = true; 863 doomed_ = true;
766 } 864 }
767 865
768 void EntryImpl::DeleteEntryData(bool everything) { 866 void EntryImpl::DeleteEntryData(bool everything) {
769 DCHECK(doomed_ || !everything); 867 DCHECK(doomed_ || !everything);
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 CACHE_UMA(AGE_MS, "SparseWriteTime", 0, start); 1005 CACHE_UMA(AGE_MS, "SparseWriteTime", 0, start);
908 break; 1006 break;
909 case kAsyncIO: 1007 case kAsyncIO:
910 CACHE_UMA(AGE_MS, "AsyncIOTime", group, start); 1008 CACHE_UMA(AGE_MS, "AsyncIOTime", group, start);
911 break; 1009 break;
912 default: 1010 default:
913 NOTREACHED(); 1011 NOTREACHED();
914 } 1012 }
915 } 1013 }
916 1014
1015 void EntryImpl::BeginLogging(net::NetLog* net_log, bool created) {
1016 DCHECK(!net_log_.net_log());
1017 net_log_ = net::BoundNetLog::Make(
1018 net_log, net::NetLog::SOURCE_DISK_CACHE_ENTRY);
1019 net_log_.BeginEvent(
1020 net::NetLog::TYPE_DISK_CACHE_ENTRY,
1021 make_scoped_refptr(new EntryCreationParameters(GetKey(), created)));
1022 }
1023
1024 const net::BoundNetLog& EntryImpl::net_log() const {
eroman 2011/01/05 02:39:06 Is it possible for this to be called before BeginL
mmenke 2011/01/05 16:32:25 No, I don't believe it's possible. The only two f
1025 return net_log_;
1026 }
1027
917 // ------------------------------------------------------------------------ 1028 // ------------------------------------------------------------------------
918 1029
919 bool EntryImpl::CreateDataBlock(int index, int size) { 1030 bool EntryImpl::CreateDataBlock(int index, int size) {
920 DCHECK(index >= 0 && index < kNumStreams); 1031 DCHECK(index >= 0 && index < kNumStreams);
921 1032
922 Addr address(entry_.Data()->data_addr[index]); 1033 Addr address(entry_.Data()->data_addr[index]);
923 if (!CreateBlock(size, &address)) 1034 if (!CreateBlock(size, &address))
924 return false; 1035 return false;
925 1036
926 entry_.Data()->data_addr[index] = address.value(); 1037 entry_.Data()->data_addr[index] = address.value();
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
1290 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this), 1401 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this),
1291 entry_.address().value(), node_.address().value()); 1402 entry_.address().value(), node_.address().value());
1292 1403
1293 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], 1404 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0],
1294 entry_.Data()->data_addr[1], entry_.Data()->long_key); 1405 entry_.Data()->data_addr[1], entry_.Data()->long_key);
1295 1406
1296 Trace(" doomed: %d 0x%x", doomed_, dirty); 1407 Trace(" doomed: %d 0x%x", doomed_, dirty);
1297 } 1408 }
1298 1409
1299 } // namespace disk_cache 1410 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698