| OLD | NEW |
| 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/backend_impl.h" | 5 #include "net/disk_cache/backend_impl.h" |
| 6 | 6 |
| 7 #include "base/field_trial.h" | 7 #include "base/field_trial.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/histogram.h" | 10 #include "base/histogram.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/stringprintf.h" |
| 14 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 15 #include "base/time.h" | 16 #include "base/time.h" |
| 16 #include "base/timer.h" | 17 #include "base/timer.h" |
| 17 #include "base/worker_pool.h" | 18 #include "base/worker_pool.h" |
| 18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 19 #include "net/disk_cache/cache_util.h" | 20 #include "net/disk_cache/cache_util.h" |
| 20 #include "net/disk_cache/entry_impl.h" | 21 #include "net/disk_cache/entry_impl.h" |
| 21 #include "net/disk_cache/errors.h" | 22 #include "net/disk_cache/errors.h" |
| 22 #include "net/disk_cache/experiments.h" | 23 #include "net/disk_cache/experiments.h" |
| 24 #include "net/disk_cache/file.h" |
| 23 #include "net/disk_cache/hash.h" | 25 #include "net/disk_cache/hash.h" |
| 24 #include "net/disk_cache/file.h" | |
| 25 #include "net/disk_cache/mem_backend_impl.h" | 26 #include "net/disk_cache/mem_backend_impl.h" |
| 26 | 27 |
| 27 // This has to be defined before including histogram_macros.h from this file. | 28 // This has to be defined before including histogram_macros.h from this file. |
| 28 #define NET_DISK_CACHE_BACKEND_IMPL_CC_ | 29 #define NET_DISK_CACHE_BACKEND_IMPL_CC_ |
| 29 #include "net/disk_cache/histogram_macros.h" | 30 #include "net/disk_cache/histogram_macros.h" |
| 30 | 31 |
| 31 using base::Time; | 32 using base::Time; |
| 32 using base::TimeDelta; | 33 using base::TimeDelta; |
| 33 using base::TimeTicks; | 34 using base::TimeTicks; |
| 34 | 35 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 return sizeof(disk_cache::IndexHeader) + table_size; | 68 return sizeof(disk_cache::IndexHeader) + table_size; |
| 68 } | 69 } |
| 69 | 70 |
| 70 // ------------------------------------------------------------------------ | 71 // ------------------------------------------------------------------------ |
| 71 | 72 |
| 72 // Returns a fully qualified name from path and name, using a given name prefix | 73 // Returns a fully qualified name from path and name, using a given name prefix |
| 73 // and index number. For instance, if the arguments are "/foo", "bar" and 5, it | 74 // and index number. For instance, if the arguments are "/foo", "bar" and 5, it |
| 74 // will return "/foo/old_bar_005". | 75 // will return "/foo/old_bar_005". |
| 75 FilePath GetPrefixedName(const FilePath& path, const std::string& name, | 76 FilePath GetPrefixedName(const FilePath& path, const std::string& name, |
| 76 int index) { | 77 int index) { |
| 77 std::string tmp = StringPrintf("%s%s_%03d", "old_", name.c_str(), index); | 78 std::string tmp = base::StringPrintf("%s%s_%03d", "old_", |
| 79 name.c_str(), index); |
| 78 return path.AppendASCII(tmp); | 80 return path.AppendASCII(tmp); |
| 79 } | 81 } |
| 80 | 82 |
| 81 // This is a simple Task to cleanup old caches. | 83 // This is a simple Task to cleanup old caches. |
| 82 class CleanupTask : public Task { | 84 class CleanupTask : public Task { |
| 83 public: | 85 public: |
| 84 CleanupTask(const FilePath& path, const std::string& name) | 86 CleanupTask(const FilePath& path, const std::string& name) |
| 85 : path_(path), name_(name) {} | 87 : path_(path), name_(name) {} |
| 86 | 88 |
| 87 virtual void Run(); | 89 virtual void Run(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 // for the current cache configuration. Returns true if we are active part of | 186 // for the current cache configuration. Returns true if we are active part of |
| 185 // the CacheThrottle field trial. | 187 // the CacheThrottle field trial. |
| 186 bool SetFieldTrialInfo(int size_group) { | 188 bool SetFieldTrialInfo(int size_group) { |
| 187 static bool first = true; | 189 static bool first = true; |
| 188 if (!first) | 190 if (!first) |
| 189 return false; | 191 return false; |
| 190 | 192 |
| 191 // Field trials involve static objects so we have to do this only once. | 193 // Field trials involve static objects so we have to do this only once. |
| 192 first = false; | 194 first = false; |
| 193 scoped_refptr<FieldTrial> trial1 = new FieldTrial("CacheSize", 10); | 195 scoped_refptr<FieldTrial> trial1 = new FieldTrial("CacheSize", 10); |
| 194 std::string group1 = StringPrintf("CacheSizeGroup_%d", size_group); | 196 std::string group1 = base::StringPrintf("CacheSizeGroup_%d", size_group); |
| 195 trial1->AppendGroup(group1, FieldTrial::kAllRemainingProbability); | 197 trial1->AppendGroup(group1, FieldTrial::kAllRemainingProbability); |
| 196 | 198 |
| 197 scoped_refptr<FieldTrial> trial2 = new FieldTrial("CacheThrottle", 100); | 199 scoped_refptr<FieldTrial> trial2 = new FieldTrial("CacheThrottle", 100); |
| 198 int group2a = trial2->AppendGroup("CacheThrottle_On", 10); // 10 % in. | 200 int group2a = trial2->AppendGroup("CacheThrottle_On", 10); // 10 % in. |
| 199 trial2->AppendGroup("CacheThrottle_Off", 10); // 10 % control. | 201 trial2->AppendGroup("CacheThrottle_Off", 10); // 10 % control. |
| 200 | 202 |
| 201 return trial2->group() == group2a; | 203 return trial2->group() == group2a; |
| 202 } | 204 } |
| 203 | 205 |
| 204 // ------------------------------------------------------------------------ | 206 // ------------------------------------------------------------------------ |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 *iter = NULL; | 512 *iter = NULL; |
| 511 } | 513 } |
| 512 | 514 |
| 513 void BackendImpl::GetStats(StatsItems* stats) { | 515 void BackendImpl::GetStats(StatsItems* stats) { |
| 514 if (disabled_) | 516 if (disabled_) |
| 515 return; | 517 return; |
| 516 | 518 |
| 517 std::pair<std::string, std::string> item; | 519 std::pair<std::string, std::string> item; |
| 518 | 520 |
| 519 item.first = "Entries"; | 521 item.first = "Entries"; |
| 520 item.second = StringPrintf("%d", data_->header.num_entries); | 522 item.second = base::StringPrintf("%d", data_->header.num_entries); |
| 521 stats->push_back(item); | 523 stats->push_back(item); |
| 522 | 524 |
| 523 item.first = "Pending IO"; | 525 item.first = "Pending IO"; |
| 524 item.second = StringPrintf("%d", num_pending_io_); | 526 item.second = base::StringPrintf("%d", num_pending_io_); |
| 525 stats->push_back(item); | 527 stats->push_back(item); |
| 526 | 528 |
| 527 item.first = "Max size"; | 529 item.first = "Max size"; |
| 528 item.second = StringPrintf("%d", max_size_); | 530 item.second = base::StringPrintf("%d", max_size_); |
| 529 stats->push_back(item); | 531 stats->push_back(item); |
| 530 | 532 |
| 531 item.first = "Current size"; | 533 item.first = "Current size"; |
| 532 item.second = StringPrintf("%d", data_->header.num_bytes); | 534 item.second = base::StringPrintf("%d", data_->header.num_bytes); |
| 533 stats->push_back(item); | 535 stats->push_back(item); |
| 534 | 536 |
| 535 stats_.GetItems(stats); | 537 stats_.GetItems(stats); |
| 536 } | 538 } |
| 537 | 539 |
| 538 // ------------------------------------------------------------------------ | 540 // ------------------------------------------------------------------------ |
| 539 | 541 |
| 540 int BackendImpl::SyncInit() { | 542 int BackendImpl::SyncInit() { |
| 541 DCHECK(!init_); | 543 DCHECK(!init_); |
| 542 if (init_) | 544 if (init_) |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 911 DCHECK(type != net::MEMORY_CACHE); | 913 DCHECK(type != net::MEMORY_CACHE); |
| 912 cache_type_ = type; | 914 cache_type_ = type; |
| 913 } | 915 } |
| 914 | 916 |
| 915 FilePath BackendImpl::GetFileName(Addr address) const { | 917 FilePath BackendImpl::GetFileName(Addr address) const { |
| 916 if (!address.is_separate_file() || !address.is_initialized()) { | 918 if (!address.is_separate_file() || !address.is_initialized()) { |
| 917 NOTREACHED(); | 919 NOTREACHED(); |
| 918 return FilePath(); | 920 return FilePath(); |
| 919 } | 921 } |
| 920 | 922 |
| 921 std::string tmp = StringPrintf("f_%06x", address.FileNumber()); | 923 std::string tmp = base::StringPrintf("f_%06x", address.FileNumber()); |
| 922 return path_.AppendASCII(tmp); | 924 return path_.AppendASCII(tmp); |
| 923 } | 925 } |
| 924 | 926 |
| 925 MappedFile* BackendImpl::File(Addr address) { | 927 MappedFile* BackendImpl::File(Addr address) { |
| 926 if (disabled_) | 928 if (disabled_) |
| 927 return NULL; | 929 return NULL; |
| 928 return block_files_.GetFile(address); | 930 return block_files_.GetFile(address); |
| 929 } | 931 } |
| 930 | 932 |
| 931 bool BackendImpl::CreateExternalFile(Addr* address) { | 933 bool BackendImpl::CreateExternalFile(Addr* address) { |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1101 bool BackendImpl::IsLoaded() const { | 1103 bool BackendImpl::IsLoaded() const { |
| 1102 CACHE_UMA(COUNTS, "PendingIO", GetSizeGroup(), num_pending_io_); | 1104 CACHE_UMA(COUNTS, "PendingIO", GetSizeGroup(), num_pending_io_); |
| 1103 if (user_flags_ & kNoLoadProtection) | 1105 if (user_flags_ & kNoLoadProtection) |
| 1104 return false; | 1106 return false; |
| 1105 | 1107 |
| 1106 return num_pending_io_ > 5; | 1108 return num_pending_io_ > 5; |
| 1107 } | 1109 } |
| 1108 | 1110 |
| 1109 std::string BackendImpl::HistogramName(const char* name, int experiment) const { | 1111 std::string BackendImpl::HistogramName(const char* name, int experiment) const { |
| 1110 if (!experiment) | 1112 if (!experiment) |
| 1111 return StringPrintf("DiskCache.%d.%s", cache_type_, name); | 1113 return base::StringPrintf("DiskCache.%d.%s", cache_type_, name); |
| 1112 return StringPrintf("DiskCache.%d.%s_%d", cache_type_, name, experiment); | 1114 return base::StringPrintf("DiskCache.%d.%s_%d", cache_type_, |
| 1115 name, experiment); |
| 1113 } | 1116 } |
| 1114 | 1117 |
| 1115 base::WeakPtr<BackendImpl> BackendImpl::GetWeakPtr() { | 1118 base::WeakPtr<BackendImpl> BackendImpl::GetWeakPtr() { |
| 1116 return ptr_factory_.GetWeakPtr(); | 1119 return ptr_factory_.GetWeakPtr(); |
| 1117 } | 1120 } |
| 1118 | 1121 |
| 1119 int BackendImpl::GetSizeGroup() const { | 1122 int BackendImpl::GetSizeGroup() const { |
| 1120 if (disabled_) | 1123 if (disabled_) |
| 1121 return 0; | 1124 return 0; |
| 1122 | 1125 |
| (...skipping 945 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2068 if (total_memory > kMaxBuffersSize || total_memory <= 0) | 2071 if (total_memory > kMaxBuffersSize || total_memory <= 0) |
| 2069 total_memory = kMaxBuffersSize; | 2072 total_memory = kMaxBuffersSize; |
| 2070 | 2073 |
| 2071 done = true; | 2074 done = true; |
| 2072 } | 2075 } |
| 2073 | 2076 |
| 2074 return static_cast<int>(total_memory); | 2077 return static_cast<int>(total_memory); |
| 2075 } | 2078 } |
| 2076 | 2079 |
| 2077 } // namespace disk_cache | 2080 } // namespace disk_cache |
| OLD | NEW |