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/http/infinite_cache.h" | 5 #include "net/http/infinite_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 uint32 vary_hash; | 75 uint32 vary_hash; |
76 int32 headers_size; | 76 int32 headers_size; |
77 int32 response_size; | 77 int32 response_size; |
78 uint32 headers_hash; | 78 uint32 headers_hash; |
79 uint32 response_hash; | 79 uint32 response_hash; |
80 }; | 80 }; |
81 const size_t kRecordSize = sizeof(Key) + sizeof(Details); | 81 const size_t kRecordSize = sizeof(Key) + sizeof(Details); |
82 | 82 |
83 // Some constants related to the database file. | 83 // Some constants related to the database file. |
84 uint32 kMagicSignature = 0x1f00cace; | 84 uint32 kMagicSignature = 0x1f00cace; |
85 uint32 kCurrentVersion = 0x10001; | 85 uint32 kCurrentVersion = 0x10002; |
86 | 86 |
87 // Basic limits for the experiment. | 87 // Basic limits for the experiment. |
88 int kMaxNumEntries = 500 * 1000; | 88 int kMaxNumEntries = 200 * 1000; |
tburkard
2012/11/30 19:00:54
Why reduce the size?
rvargas (doing something else)
2012/11/30 19:12:23
When I increased the size I was under the assumpti
| |
89 int kMaxTrackingSize = 40 * 1024 * 1024; | 89 int kMaxTrackingSize = 40 * 1024 * 1024; |
90 | 90 |
91 // Settings that control how we generate histograms. | 91 // Settings that control how we generate histograms. |
92 int kTimerMinutes = 5; | 92 int kTimerMinutes = 5; |
93 int kReportSizeStep = 100 * 1024 * 1024; | 93 int kReportSizeStep = 100 * 1024 * 1024; |
94 | 94 |
95 // Buffer to read and write the file. | 95 // Buffer to read and write the file. |
96 const size_t kBufferSize = 1024 * 1024; | 96 const size_t kBufferSize = 1024 * 1024; |
97 const size_t kMaxRecordsToRead = kBufferSize / kRecordSize; | 97 const size_t kMaxRecordsToRead = kBufferSize / kRecordSize; |
98 COMPILE_ASSERT(kRecordSize * kMaxRecordsToRead < kBufferSize, wrong_buffer); | 98 COMPILE_ASSERT(kRecordSize * kMaxRecordsToRead < kBufferSize, wrong_buffer); |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
597 | 597 |
598 PlatformFile file = base::CreatePlatformFile( | 598 PlatformFile file = base::CreatePlatformFile( |
599 path_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, NULL, NULL); | 599 path_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, NULL, NULL); |
600 if (file == base::kInvalidPlatformFileValue) | 600 if (file == base::kInvalidPlatformFileValue) |
601 return InitializeData(); | 601 return InitializeData(); |
602 if (!ReadData(file)) | 602 if (!ReadData(file)) |
603 InitializeData(); | 603 InitializeData(); |
604 base::ClosePlatformFile(file); | 604 base::ClosePlatformFile(file); |
605 if (header_->disabled) { | 605 if (header_->disabled) { |
606 UMA_HISTOGRAM_BOOLEAN("InfiniteCache.Full", true); | 606 UMA_HISTOGRAM_BOOLEAN("InfiniteCache.Full", true); |
607 map_.clear(); | 607 InitializeData(); |
608 } | 608 } |
609 } | 609 } |
610 | 610 |
611 void InfiniteCache::Worker::StoreData() { | 611 void InfiniteCache::Worker::StoreData() { |
612 if (!init_ || flushed_ || path_.empty()) | 612 if (!init_ || flushed_ || path_.empty()) |
613 return; | 613 return; |
614 | 614 |
615 header_->update_time = Time::Now().ToInternalValue(); | 615 header_->update_time = Time::Now().ToInternalValue(); |
616 header_->generation++; | 616 header_->generation++; |
617 header_->header_hash = base::Hash( | 617 header_->header_hash = base::Hash( |
(...skipping 14 matching lines...) Expand all Loading... | |
632 LOG(ERROR) << "Failed to write experiment data"; | 632 LOG(ERROR) << "Failed to write experiment data"; |
633 } | 633 } |
634 } | 634 } |
635 | 635 |
636 void InfiniteCache::Worker::InitializeData() { | 636 void InfiniteCache::Worker::InitializeData() { |
637 header_.reset(new Header); | 637 header_.reset(new Header); |
638 memset(header_.get(), 0, sizeof(Header)); | 638 memset(header_.get(), 0, sizeof(Header)); |
639 header_->magic = kMagicSignature; | 639 header_->magic = kMagicSignature; |
640 header_->version = kCurrentVersion; | 640 header_->version = kCurrentVersion; |
641 header_->creation_time = Time::Now().ToInternalValue(); | 641 header_->creation_time = Time::Now().ToInternalValue(); |
642 map_.clear(); | |
642 | 643 |
643 UMA_HISTOGRAM_BOOLEAN("InfiniteCache.Initialize", true); | 644 UMA_HISTOGRAM_BOOLEAN("InfiniteCache.Initialize", true); |
644 init_ = true; | 645 init_ = true; |
645 } | 646 } |
646 | 647 |
647 bool InfiniteCache::Worker::ReadData(PlatformFile file) { | 648 bool InfiniteCache::Worker::ReadData(PlatformFile file) { |
648 if (!ReadAndVerifyHeader(file)) | 649 if (!ReadAndVerifyHeader(file)) |
649 return false; | 650 return false; |
650 | 651 |
651 scoped_array<char> buffer(new char[kBufferSize]); | 652 scoped_array<char> buffer(new char[kBufferSize]); |
652 size_t offset = sizeof(Header); | 653 size_t offset = sizeof(Header); |
653 uint32 hash = adler32(0, Z_NULL, 0); | 654 uint32 hash = adler32(0, Z_NULL, 0); |
654 | 655 |
655 for (int remaining_records = header_->num_entries; remaining_records;) { | 656 for (int remaining_records = header_->num_entries; remaining_records;) { |
656 int num_records = std::min(header_->num_entries, | 657 int num_records = std::min(remaining_records, |
657 static_cast<int>(kMaxRecordsToRead)); | 658 static_cast<int>(kMaxRecordsToRead)); |
658 size_t num_bytes = num_records * kRecordSize; | 659 size_t num_bytes = num_records * kRecordSize; |
659 remaining_records -= num_records; | 660 remaining_records -= num_records; |
660 if (!remaining_records) | 661 if (!remaining_records) |
661 num_bytes += sizeof(uint32); // Trailing hash. | 662 num_bytes += sizeof(uint32); // Trailing hash. |
662 DCHECK_LE(num_bytes, kBufferSize); | 663 DCHECK_LE(num_bytes, kBufferSize); |
663 | 664 |
664 if (!ReadPlatformFile(file, offset, buffer.get(), num_bytes)) | 665 if (!ReadPlatformFile(file, offset, buffer.get(), num_bytes)) |
665 return false; | 666 return false; |
666 | 667 |
(...skipping 29 matching lines...) Expand all Loading... | |
696 if (!WritePlatformFile(file, 0, header_.get(), sizeof(Header))) | 697 if (!WritePlatformFile(file, 0, header_.get(), sizeof(Header))) |
697 return false; | 698 return false; |
698 | 699 |
699 scoped_array<char> buffer(new char[kBufferSize]); | 700 scoped_array<char> buffer(new char[kBufferSize]); |
700 size_t offset = sizeof(Header); | 701 size_t offset = sizeof(Header); |
701 uint32 hash = adler32(0, Z_NULL, 0); | 702 uint32 hash = adler32(0, Z_NULL, 0); |
702 | 703 |
703 DCHECK_EQ(header_->num_entries, static_cast<int32>(map_.size())); | 704 DCHECK_EQ(header_->num_entries, static_cast<int32>(map_.size())); |
704 KeyMap::iterator iterator = map_.begin(); | 705 KeyMap::iterator iterator = map_.begin(); |
705 for (int remaining_records = header_->num_entries; remaining_records;) { | 706 for (int remaining_records = header_->num_entries; remaining_records;) { |
706 int num_records = std::min(header_->num_entries, | 707 int num_records = std::min(remaining_records, |
707 static_cast<int>(kMaxRecordsToRead)); | 708 static_cast<int>(kMaxRecordsToRead)); |
708 size_t num_bytes = num_records * kRecordSize; | 709 size_t num_bytes = num_records * kRecordSize; |
709 remaining_records -= num_records; | 710 remaining_records -= num_records; |
710 | 711 |
711 for (int i = 0; i < num_records; i++) { | 712 for (int i = 0; i < num_records; i++) { |
712 if (iterator == map_.end()) { | 713 if (iterator == map_.end()) { |
713 NOTREACHED(); | 714 NOTREACHED(); |
714 return false; | 715 return false; |
715 } | 716 } |
716 char* record = buffer.get() + i * kRecordSize; | 717 char* record = buffer.get() + i * kRecordSize; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
797 if (header_->num_entries == kMaxNumEntries) { | 798 if (header_->num_entries == kMaxNumEntries) { |
798 int use_hours = header_->use_minutes / 60; | 799 int use_hours = header_->use_minutes / 60; |
799 int age_hours = (Time::Now() - | 800 int age_hours = (Time::Now() - |
800 Time::FromInternalValue(header_->creation_time)).InHours(); | 801 Time::FromInternalValue(header_->creation_time)).InHours(); |
801 UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.MaxUseTime", use_hours); | 802 UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.MaxUseTime", use_hours); |
802 UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.MaxAge", age_hours); | 803 UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.MaxAge", age_hours); |
803 | 804 |
804 int entry_size = static_cast<int>(header_->total_size / kMaxNumEntries); | 805 int entry_size = static_cast<int>(header_->total_size / kMaxNumEntries); |
805 UMA_HISTOGRAM_COUNTS("InfiniteCache.FinalAvgEntrySize", entry_size); | 806 UMA_HISTOGRAM_COUNTS("InfiniteCache.FinalAvgEntrySize", entry_size); |
806 header_->disabled = 1; | 807 header_->disabled = 1; |
808 header_->num_entries = 0; | |
807 map_.clear(); | 809 map_.clear(); |
808 } | 810 } |
809 } | 811 } |
810 | 812 |
811 void InfiniteCache::Worker::Remove(const Details& details) { | 813 void InfiniteCache::Worker::Remove(const Details& details) { |
812 UpdateSize(details.headers_size, 0); | 814 UpdateSize(details.headers_size, 0); |
813 UpdateSize(details.response_size, 0); | 815 UpdateSize(details.response_size, 0); |
814 header_->num_entries--; | 816 header_->num_entries--; |
815 } | 817 } |
816 | 818 |
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1149 int InfiniteCache::FlushDataForTest(const CompletionCallback& callback) { | 1151 int InfiniteCache::FlushDataForTest(const CompletionCallback& callback) { |
1150 DCHECK(worker_); | 1152 DCHECK(worker_); |
1151 int* result = new int; | 1153 int* result = new int; |
1152 task_runner_->PostTaskAndReply( | 1154 task_runner_->PostTaskAndReply( |
1153 FROM_HERE, base::Bind(&InfiniteCache::Worker::Flush, worker_, result), | 1155 FROM_HERE, base::Bind(&InfiniteCache::Worker::Flush, worker_, result), |
1154 base::Bind(&OnComplete, callback, base::Owned(result))); | 1156 base::Bind(&OnComplete, callback, base::Owned(result))); |
1155 return net::ERR_IO_PENDING; | 1157 return net::ERR_IO_PENDING; |
1156 } | 1158 } |
1157 | 1159 |
1158 } // namespace net | 1160 } // namespace net |
OLD | NEW |