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

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: Lint fixes Created 9 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/entry_impl.h ('k') | net/disk_cache/sparse_control.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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();
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 DISALLOW_COPY_AND_ASSIGN(EntryCreationParameters);
48 };
49
50 // NetLog parameters for non-sparse reading and writing to an EntryImpl.
51 class ReadWriteDataParams : public net::NetLog::EventParameters {
52 public:
53 // For reads, |truncate| must be false.
54 ReadWriteDataParams(int index, int offset, int buf_len, bool truncate)
55 : index_(index), offset_(offset), buf_len_(buf_len), truncate_(truncate) {
56 }
57
58 Value* ToValue() const {
59 DictionaryValue* dict = new DictionaryValue();
60 dict->SetInteger("index", index_);
61 dict->SetInteger("offset", offset_);
62 dict->SetInteger("buf_len", buf_len_);
63 if (truncate_)
64 dict->SetBoolean("truncate", truncate_);
65 return dict;
66 }
67
68 private:
69 const int index_;
70 const int offset_;
71 const int buf_len_;
72 const bool truncate_;
73
74 DISALLOW_COPY_AND_ASSIGN(ReadWriteDataParams);
75 };
76
77 // NetLog parameters logged when non-sparse reads and writes complete.
78 class FileIOCompleteParameters : public net::NetLog::EventParameters {
79 public:
80 // |bytes_copied| is either the number of bytes copied or a network error
81 // code. |bytes_copied| must not be ERR_IO_PENDING, as it's not a valid
82 // result for an operation.
83 explicit FileIOCompleteParameters(int bytes_copied)
84 : bytes_copied_(bytes_copied) {
85 }
86
87 Value* ToValue() const {
88 DCHECK_NE(bytes_copied_, net::ERR_IO_PENDING);
89 DictionaryValue* dict = new DictionaryValue();
90 if (bytes_copied_ < 0) {
91 dict->SetInteger("net_error", bytes_copied_);
92 } else {
93 dict->SetInteger("bytes_copied", bytes_copied_);
94 }
95 return dict;
96 }
97
98 private:
99 const int bytes_copied_;
100
101 DISALLOW_COPY_AND_ASSIGN(FileIOCompleteParameters);
102 };
103
27 // This class implements FileIOCallback to buffer the callback from a file IO 104 // This class implements FileIOCallback to buffer the callback from a file IO
28 // operation from the actual net class. 105 // operation from the actual net class.
29 class SyncCallback: public disk_cache::FileIOCallback { 106 class SyncCallback: public disk_cache::FileIOCallback {
30 public: 107 public:
108 // |end_event_type| is the event type to log on completion. Logs nothing on
109 // discard, or when the NetLog is not set to log all events.
31 SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer, 110 SyncCallback(disk_cache::EntryImpl* entry, net::IOBuffer* buffer,
32 net::CompletionCallback* callback ) 111 net::CompletionCallback* callback,
112 net::NetLog::EventType end_event_type)
33 : entry_(entry), callback_(callback), buf_(buffer), 113 : entry_(entry), callback_(callback), buf_(buffer),
34 start_(TimeTicks::Now()) { 114 start_(TimeTicks::Now()), end_event_type_(end_event_type) {
35 entry->AddRef(); 115 entry->AddRef();
36 entry->IncrementIoCount(); 116 entry->IncrementIoCount();
37 } 117 }
38 ~SyncCallback() {} 118 ~SyncCallback() {}
39 119
40 virtual void OnFileIOComplete(int bytes_copied); 120 virtual void OnFileIOComplete(int bytes_copied);
41 void Discard(); 121 void Discard();
122
42 private: 123 private:
43 disk_cache::EntryImpl* entry_; 124 disk_cache::EntryImpl* entry_;
44 net::CompletionCallback* callback_; 125 net::CompletionCallback* callback_;
45 scoped_refptr<net::IOBuffer> buf_; 126 scoped_refptr<net::IOBuffer> buf_;
46 TimeTicks start_; 127 TimeTicks start_;
128 net::NetLog::EventType end_event_type_;
47 129
48 DISALLOW_COPY_AND_ASSIGN(SyncCallback); 130 DISALLOW_COPY_AND_ASSIGN(SyncCallback);
49 }; 131 };
50 132
51 void SyncCallback::OnFileIOComplete(int bytes_copied) { 133 void SyncCallback::OnFileIOComplete(int bytes_copied) {
52 entry_->DecrementIoCount(); 134 entry_->DecrementIoCount();
53 if (callback_) { 135 if (callback_) {
136 if (entry_->net_log().IsLoggingAllEvents()) {
137 entry_->net_log().EndEvent(
138 end_event_type_,
139 make_scoped_refptr(new FileIOCompleteParameters(bytes_copied)));
140 }
54 entry_->ReportIOTime(disk_cache::EntryImpl::kAsyncIO, start_); 141 entry_->ReportIOTime(disk_cache::EntryImpl::kAsyncIO, start_);
55 callback_->Run(bytes_copied); 142 callback_->Run(bytes_copied);
56 } 143 }
57 entry_->Release(); 144 entry_->Release();
58 delete this; 145 delete this;
59 } 146 }
60 147
61 void SyncCallback::Discard() { 148 void SyncCallback::Discard() {
62 callback_ = NULL; 149 callback_ = NULL;
63 buf_ = NULL; 150 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 388 // maybe for a child entry, so it is important to do it before deleting this
302 // entry. 389 // entry.
303 sparse_.reset(); 390 sparse_.reset();
304 391
305 // Remove this entry from the list of open entries. 392 // Remove this entry from the list of open entries.
306 backend_->OnEntryDestroyBegin(entry_.address()); 393 backend_->OnEntryDestroyBegin(entry_.address());
307 394
308 if (doomed_) { 395 if (doomed_) {
309 DeleteEntryData(true); 396 DeleteEntryData(true);
310 } else { 397 } else {
398 net_log_.AddEvent(net::NetLog::TYPE_DISK_CACHE_CLOSE, NULL);
311 bool ret = true; 399 bool ret = true;
312 for (int index = 0; index < kNumStreams; index++) { 400 for (int index = 0; index < kNumStreams; index++) {
313 if (user_buffers_[index].get()) { 401 if (user_buffers_[index].get()) {
314 if (!(ret = Flush(index, 0))) 402 if (!(ret = Flush(index, 0)))
315 LOG(ERROR) << "Failed to save user data"; 403 LOG(ERROR) << "Failed to save user data";
316 } 404 }
317 if (unreported_size_[index]) { 405 if (unreported_size_[index]) {
318 backend_->ModifyStorageSize( 406 backend_->ModifyStorageSize(
319 entry_.Data()->data_size[index] - unreported_size_[index], 407 entry_.Data()->data_size[index] - unreported_size_[index],
320 entry_.Data()->data_size[index]); 408 entry_.Data()->data_size[index]);
321 } 409 }
322 } 410 }
323 411
324 if (!ret) { 412 if (!ret) {
325 // There was a failure writing the actual data. Mark the entry as dirty. 413 // There was a failure writing the actual data. Mark the entry as dirty.
326 int current_id = backend_->GetCurrentEntryId(); 414 int current_id = backend_->GetCurrentEntryId();
327 node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1; 415 node_.Data()->dirty = current_id == 1 ? -1 : current_id - 1;
328 node_.Store(); 416 node_.Store();
329 } else if (node_.HasData() && node_.Data()->dirty) { 417 } else if (node_.HasData() && node_.Data()->dirty) {
330 node_.Data()->dirty = 0; 418 node_.Data()->dirty = 0;
331 node_.Store(); 419 node_.Store();
332 } 420 }
333 } 421 }
334 422
335 Trace("~EntryImpl out 0x%p", reinterpret_cast<void*>(this)); 423 Trace("~EntryImpl out 0x%p", reinterpret_cast<void*>(this));
424 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_ENTRY, NULL);
336 backend_->OnEntryDestroyEnd(); 425 backend_->OnEntryDestroyEnd();
337 } 426 }
338 427
339 void EntryImpl::Doom() { 428 void EntryImpl::Doom() {
340 backend_->background_queue()->DoomEntryImpl(this); 429 backend_->background_queue()->DoomEntryImpl(this);
341 } 430 }
342 431
343 void EntryImpl::Close() { 432 void EntryImpl::Close() {
344 backend_->background_queue()->CloseEntryImpl(this); 433 backend_->background_queue()->CloseEntryImpl(this);
345 } 434 }
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 void EntryImpl::DoomImpl() { 569 void EntryImpl::DoomImpl() {
481 if (doomed_) 570 if (doomed_)
482 return; 571 return;
483 572
484 SetPointerForInvalidEntry(backend_->GetCurrentEntryId()); 573 SetPointerForInvalidEntry(backend_->GetCurrentEntryId());
485 backend_->InternalDoomEntry(this); 574 backend_->InternalDoomEntry(this);
486 } 575 }
487 576
488 int EntryImpl::ReadDataImpl(int index, int offset, net::IOBuffer* buf, 577 int EntryImpl::ReadDataImpl(int index, int offset, net::IOBuffer* buf,
489 int buf_len, CompletionCallback* callback) { 578 int buf_len, CompletionCallback* callback) {
579 if (net_log_.IsLoggingAllEvents()) {
580 net_log_.BeginEvent(
581 net::NetLog::TYPE_DISK_CACHE_READ_DATA,
582 make_scoped_refptr(
583 new ReadWriteDataParams(index, offset, buf_len, false)));
584 }
585
586 int result = InternalReadData(index, offset, buf, buf_len, callback);
587
588 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) {
589 net_log_.EndEvent(
590 net::NetLog::TYPE_DISK_CACHE_READ_DATA,
591 make_scoped_refptr(new FileIOCompleteParameters(result)));
592 }
593 return result;
594 }
595
596 int EntryImpl::WriteDataImpl(int index, int offset, net::IOBuffer* buf,
597 int buf_len, CompletionCallback* callback,
598 bool truncate) {
599 if (net_log_.IsLoggingAllEvents()) {
600 net_log_.BeginEvent(
601 net::NetLog::TYPE_DISK_CACHE_WRITE_DATA,
602 make_scoped_refptr(
603 new ReadWriteDataParams(index, offset, buf_len, truncate)));
604 }
605
606 int result = InternalWriteData(index, offset, buf, buf_len, callback,
607 truncate);
608
609 if (result != net::ERR_IO_PENDING && net_log_.IsLoggingAllEvents()) {
610 net_log_.EndEvent(
611 net::NetLog::TYPE_DISK_CACHE_WRITE_DATA,
612 make_scoped_refptr(new FileIOCompleteParameters(result)));
613 }
614 return result;
615 }
616
617 int EntryImpl::InternalReadData(int index, int offset, net::IOBuffer* buf,
618 int buf_len, CompletionCallback* callback) {
490 DCHECK(node_.Data()->dirty || read_only_); 619 DCHECK(node_.Data()->dirty || read_only_);
491 DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len; 620 DVLOG(2) << "Read from " << index << " at " << offset << " : " << buf_len;
492 if (index < 0 || index >= kNumStreams) 621 if (index < 0 || index >= kNumStreams)
493 return net::ERR_INVALID_ARGUMENT; 622 return net::ERR_INVALID_ARGUMENT;
494 623
495 int entry_size = entry_.Data()->data_size[index]; 624 int entry_size = entry_.Data()->data_size[index];
496 if (offset >= entry_size || offset < 0 || !buf_len) 625 if (offset >= entry_size || offset < 0 || !buf_len)
497 return 0; 626 return 0;
498 627
499 if (buf_len < 0) 628 if (buf_len < 0)
(...skipping 29 matching lines...) Expand all
529 return net::ERR_FAILED; 658 return net::ERR_FAILED;
530 659
531 size_t file_offset = offset; 660 size_t file_offset = offset;
532 if (address.is_block_file()) { 661 if (address.is_block_file()) {
533 DCHECK_LE(offset + buf_len, kMaxBlockSize); 662 DCHECK_LE(offset + buf_len, kMaxBlockSize);
534 file_offset += address.start_block() * address.BlockSize() + 663 file_offset += address.start_block() * address.BlockSize() +
535 kBlockHeaderSize; 664 kBlockHeaderSize;
536 } 665 }
537 666
538 SyncCallback* io_callback = NULL; 667 SyncCallback* io_callback = NULL;
539 if (callback) 668 if (callback) {
540 io_callback = new SyncCallback(this, buf, callback); 669 io_callback = new SyncCallback(this, buf, callback,
670 net::NetLog::TYPE_DISK_CACHE_READ_DATA);
671 }
541 672
542 bool completed; 673 bool completed;
543 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) { 674 if (!file->Read(buf->data(), buf_len, file_offset, io_callback, &completed)) {
544 if (io_callback) 675 if (io_callback)
545 io_callback->Discard(); 676 io_callback->Discard();
546 return net::ERR_FAILED; 677 return net::ERR_FAILED;
547 } 678 }
548 679
549 if (io_callback && completed) 680 if (io_callback && completed)
550 io_callback->Discard(); 681 io_callback->Discard();
551 682
552 ReportIOTime(kRead, start); 683 ReportIOTime(kRead, start);
553 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING; 684 return (completed || !callback) ? buf_len : net::ERR_IO_PENDING;
554 } 685 }
555 686
556 int EntryImpl::WriteDataImpl(int index, int offset, net::IOBuffer* buf, 687 int EntryImpl::InternalWriteData(int index, int offset, net::IOBuffer* buf,
557 int buf_len, CompletionCallback* callback, 688 int buf_len, CompletionCallback* callback,
558 bool truncate) { 689 bool truncate) {
559 DCHECK(node_.Data()->dirty || read_only_); 690 DCHECK(node_.Data()->dirty || read_only_);
560 DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len; 691 DVLOG(2) << "Write to " << index << " at " << offset << " : " << buf_len;
561 if (index < 0 || index >= kNumStreams) 692 if (index < 0 || index >= kNumStreams)
562 return net::ERR_INVALID_ARGUMENT; 693 return net::ERR_INVALID_ARGUMENT;
563 694
564 if (offset < 0 || buf_len < 0) 695 if (offset < 0 || buf_len < 0)
565 return net::ERR_INVALID_ARGUMENT; 696 return net::ERR_INVALID_ARGUMENT;
566 697
567 int max_file_size = backend_->MaxFileSize(); 698 int max_file_size = backend_->MaxFileSize();
568 699
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 kBlockHeaderSize; 752 kBlockHeaderSize;
622 } else if (truncate || (extending && !buf_len)) { 753 } else if (truncate || (extending && !buf_len)) {
623 if (!file->SetLength(offset + buf_len)) 754 if (!file->SetLength(offset + buf_len))
624 return net::ERR_FAILED; 755 return net::ERR_FAILED;
625 } 756 }
626 757
627 if (!buf_len) 758 if (!buf_len)
628 return 0; 759 return 0;
629 760
630 SyncCallback* io_callback = NULL; 761 SyncCallback* io_callback = NULL;
631 if (callback) 762 if (callback) {
632 io_callback = new SyncCallback(this, buf, callback); 763 io_callback = new SyncCallback(this, buf, callback,
764 net::NetLog::TYPE_DISK_CACHE_WRITE_DATA);
765 }
633 766
634 bool completed; 767 bool completed;
635 if (!file->Write(buf->data(), buf_len, file_offset, io_callback, 768 if (!file->Write(buf->data(), buf_len, file_offset, io_callback,
636 &completed)) { 769 &completed)) {
637 if (io_callback) 770 if (io_callback)
638 io_callback->Discard(); 771 io_callback->Discard();
639 return net::ERR_FAILED; 772 return net::ERR_FAILED;
640 } 773 }
641 774
642 if (io_callback && completed) 775 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) { 883 bool EntryImpl::IsSameEntry(const std::string& key, uint32 hash) {
751 if (entry_.Data()->hash != hash || 884 if (entry_.Data()->hash != hash ||
752 static_cast<size_t>(entry_.Data()->key_len) != key.size()) 885 static_cast<size_t>(entry_.Data()->key_len) != key.size())
753 return false; 886 return false;
754 887
755 std::string my_key = GetKey(); 888 std::string my_key = GetKey();
756 return key.compare(my_key) ? false : true; 889 return key.compare(my_key) ? false : true;
757 } 890 }
758 891
759 void EntryImpl::InternalDoom() { 892 void EntryImpl::InternalDoom() {
893 net_log_.AddEvent(net::NetLog::TYPE_DISK_CACHE_DOOM, NULL);
760 DCHECK(node_.HasData()); 894 DCHECK(node_.HasData());
761 if (!node_.Data()->dirty) { 895 if (!node_.Data()->dirty) {
762 node_.Data()->dirty = backend_->GetCurrentEntryId(); 896 node_.Data()->dirty = backend_->GetCurrentEntryId();
763 node_.Store(); 897 node_.Store();
764 } 898 }
765 doomed_ = true; 899 doomed_ = true;
766 } 900 }
767 901
768 void EntryImpl::DeleteEntryData(bool everything) { 902 void EntryImpl::DeleteEntryData(bool everything) {
769 DCHECK(doomed_ || !everything); 903 DCHECK(doomed_ || !everything);
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 CACHE_UMA(AGE_MS, "SparseWriteTime", 0, start); 1041 CACHE_UMA(AGE_MS, "SparseWriteTime", 0, start);
908 break; 1042 break;
909 case kAsyncIO: 1043 case kAsyncIO:
910 CACHE_UMA(AGE_MS, "AsyncIOTime", group, start); 1044 CACHE_UMA(AGE_MS, "AsyncIOTime", group, start);
911 break; 1045 break;
912 default: 1046 default:
913 NOTREACHED(); 1047 NOTREACHED();
914 } 1048 }
915 } 1049 }
916 1050
1051 void EntryImpl::BeginLogging(net::NetLog* net_log, bool created) {
1052 DCHECK(!net_log_.net_log());
1053 net_log_ = net::BoundNetLog::Make(
1054 net_log, net::NetLog::SOURCE_DISK_CACHE_ENTRY);
1055 net_log_.BeginEvent(
1056 net::NetLog::TYPE_DISK_CACHE_ENTRY,
1057 make_scoped_refptr(new EntryCreationParameters(GetKey(), created)));
1058 }
1059
1060 const net::BoundNetLog& EntryImpl::net_log() const {
1061 return net_log_;
1062 }
1063
917 // ------------------------------------------------------------------------ 1064 // ------------------------------------------------------------------------
918 1065
919 bool EntryImpl::CreateDataBlock(int index, int size) { 1066 bool EntryImpl::CreateDataBlock(int index, int size) {
920 DCHECK(index >= 0 && index < kNumStreams); 1067 DCHECK(index >= 0 && index < kNumStreams);
921 1068
922 Addr address(entry_.Data()->data_addr[index]); 1069 Addr address(entry_.Data()->data_addr[index]);
923 if (!CreateBlock(size, &address)) 1070 if (!CreateBlock(size, &address))
924 return false; 1071 return false;
925 1072
926 entry_.Data()->data_addr[index] = address.value(); 1073 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), 1437 Trace("%s 0x%p 0x%x 0x%x", msg, reinterpret_cast<void*>(this),
1291 entry_.address().value(), node_.address().value()); 1438 entry_.address().value(), node_.address().value());
1292 1439
1293 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0], 1440 Trace(" data: 0x%x 0x%x 0x%x", entry_.Data()->data_addr[0],
1294 entry_.Data()->data_addr[1], entry_.Data()->long_key); 1441 entry_.Data()->data_addr[1], entry_.Data()->long_key);
1295 1442
1296 Trace(" doomed: %d 0x%x", doomed_, dirty); 1443 Trace(" doomed: %d 0x%x", doomed_, dirty);
1297 } 1444 }
1298 1445
1299 } // namespace disk_cache 1446 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/entry_impl.h ('k') | net/disk_cache/sparse_control.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698