| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/stats.h" | 5 #include "net/disk_cache/stats.h" |
| 6 | 6 |
| 7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "net/disk_cache/backend_impl.h" | 11 #include "net/disk_cache/backend_impl.h" |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // If we have more than 512 bytes of counters, change kDiskSignature so we | 109 // If we have more than 512 bytes of counters, change kDiskSignature so we |
| 110 // don't overwrite something else (LoadStats must fail). | 110 // don't overwrite something else (LoadStats must fail). |
| 111 COMPILE_ASSERT(sizeof(*stats) <= 256 * 2, use_more_blocks); | 111 COMPILE_ASSERT(sizeof(*stats) <= 256 * 2, use_more_blocks); |
| 112 memset(stats, 0, sizeof(*stats)); | 112 memset(stats, 0, sizeof(*stats)); |
| 113 stats->signature = kDiskSignature; | 113 stats->signature = kDiskSignature; |
| 114 stats->size = sizeof(*stats); | 114 stats->size = sizeof(*stats); |
| 115 | 115 |
| 116 return StoreStats(backend, *address, stats); | 116 return StoreStats(backend, *address, stats); |
| 117 } | 117 } |
| 118 | 118 |
| 119 Stats::Stats() : backend_(NULL) { | 119 Stats::Stats() : backend_(NULL), size_histogram_(NULL) { |
| 120 } | 120 } |
| 121 | 121 |
| 122 Stats::~Stats() { | 122 Stats::~Stats() { |
| 123 } | 123 } |
| 124 | 124 |
| 125 bool Stats::Init(BackendImpl* backend, uint32* storage_addr) { | 125 bool Stats::Init(BackendImpl* backend, uint32* storage_addr) { |
| 126 OnDiskStats stats; | 126 OnDiskStats stats; |
| 127 Addr address(*storage_addr); | 127 Addr address(*storage_addr); |
| 128 if (address.is_initialized()) { | 128 if (address.is_initialized()) { |
| 129 if (!LoadStats(backend, address, &stats)) | 129 if (!LoadStats(backend, address, &stats)) |
| 130 return false; | 130 return false; |
| 131 } else { | 131 } else { |
| 132 if (!CreateStats(backend, &address, &stats)) | 132 if (!CreateStats(backend, &address, &stats)) |
| 133 return false; | 133 return false; |
| 134 *storage_addr = address.value(); | 134 *storage_addr = address.value(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 storage_addr_ = address.value(); | 137 storage_addr_ = address.value(); |
| 138 backend_ = backend; | 138 backend_ = backend; |
| 139 | 139 |
| 140 memcpy(data_sizes_, stats.data_sizes, sizeof(data_sizes_)); | 140 memcpy(data_sizes_, stats.data_sizes, sizeof(data_sizes_)); |
| 141 memcpy(counters_, stats.counters, sizeof(counters_)); | 141 memcpy(counters_, stats.counters, sizeof(counters_)); |
| 142 | 142 |
| 143 // It seems impossible to support this histogram for more than one | 143 // It seems impossible to support this histogram for more than one |
| 144 // simultaneous objects with the current infrastructure. | 144 // simultaneous objects with the current infrastructure. |
| 145 static bool first_time = true; | 145 static bool first_time = true; |
| 146 if (first_time) { | 146 if (first_time) { |
| 147 first_time = false; | 147 first_time = false; |
| 148 // ShouldReportAgain() will re-enter this object. | 148 // ShouldReportAgain() will re-enter this object. |
| 149 if (!size_histogram_.get() && backend->cache_type() == net::DISK_CACHE && | 149 if (!size_histogram_ && backend->cache_type() == net::DISK_CACHE && |
| 150 backend->ShouldReportAgain()) { | 150 backend->ShouldReportAgain()) { |
| 151 // Stats may be reused when the cache is re-created, but we want only one | 151 // Stats may be reused when the cache is re-created, but we want only one |
| 152 // histogram at any given time. | 152 // histogram at any given time. |
| 153 size_histogram_ = | 153 size_histogram_ = |
| 154 StatsHistogram::StatsHistogramFactoryGet("DiskCache.SizeStats"); | 154 StatsHistogram::StatsHistogramFactoryGet("DiskCache.SizeStats"); |
| 155 size_histogram_->Init(this); | 155 size_histogram_->Init(this); |
| 156 } | 156 } |
| 157 } | 157 } |
| 158 | 158 |
| 159 return true; | 159 return true; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 int Stats::GetRatio(Counters hit, Counters miss) const { | 320 int Stats::GetRatio(Counters hit, Counters miss) const { |
| 321 int64 ratio = GetCounter(hit) * 100; | 321 int64 ratio = GetCounter(hit) * 100; |
| 322 if (!ratio) | 322 if (!ratio) |
| 323 return 0; | 323 return 0; |
| 324 | 324 |
| 325 ratio /= (GetCounter(hit) + GetCounter(miss)); | 325 ratio /= (GetCounter(hit) + GetCounter(miss)); |
| 326 return static_cast<int>(ratio); | 326 return static_cast<int>(ratio); |
| 327 } | 327 } |
| 328 | 328 |
| 329 } // namespace disk_cache | 329 } // namespace disk_cache |
| OLD | NEW |