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

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

Issue 15772003: Disk Cache: Make Stats independent of the backend implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 7 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
« net/disk_cache/stats.h ('K') | « net/disk_cache/stats.h ('k') | no next file » | 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) 2011 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/metrics/histogram_samples.h" 9 #include "base/metrics/histogram_samples.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "net/disk_cache/backend_impl.h"
13 12
14 namespace { 13 namespace {
15 14
16 const int32 kDiskSignature = 0xF01427E0; 15 const int32 kDiskSignature = 0xF01427E0;
17 16
18 struct OnDiskStats { 17 struct OnDiskStats {
19 int32 signature; 18 int32 signature;
20 int size; 19 int size;
21 int data_sizes[disk_cache::Stats::kDataSizesLength]; 20 int data_sizes[disk_cache::Stats::kDataSizesLength];
22 int64 counters[disk_cache::Stats::MAX_COUNTER]; 21 int64 counters[disk_cache::Stats::MAX_COUNTER];
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 "Doom recent entries", 63 "Doom recent entries",
65 "ga.js evicted" 64 "ga.js evicted"
66 }; 65 };
67 COMPILE_ASSERT(arraysize(kCounterNames) == disk_cache::Stats::MAX_COUNTER, 66 COMPILE_ASSERT(arraysize(kCounterNames) == disk_cache::Stats::MAX_COUNTER,
68 update_the_names); 67 update_the_names);
69 68
70 } // namespace 69 } // namespace
71 70
72 namespace disk_cache { 71 namespace disk_cache {
73 72
74 bool LoadStats(BackendImpl* backend, Addr address, OnDiskStats* stats) { 73 bool VerifyStats(OnDiskStats* stats) {
75 MappedFile* file = backend->File(address);
76 if (!file)
77 return false;
78
79 size_t offset = address.start_block() * address.BlockSize() +
80 kBlockHeaderSize;
81 memset(stats, 0, sizeof(*stats));
82 if (!file->Read(stats, sizeof(*stats), offset))
83 return false;
84
85 if (stats->signature != kDiskSignature) 74 if (stats->signature != kDiskSignature)
86 return false; 75 return false;
87 76
88 // We don't want to discard the whole cache every time we have one extra 77 // We don't want to discard the whole cache every time we have one extra
89 // counter; we keep old data if we can. 78 // counter; we keep old data if we can.
90 if (static_cast<unsigned int>(stats->size) > sizeof(*stats)) { 79 if (static_cast<unsigned int>(stats->size) > sizeof(*stats)) {
91 memset(stats, 0, sizeof(*stats)); 80 memset(stats, 0, sizeof(*stats));
92 stats->signature = kDiskSignature; 81 stats->signature = kDiskSignature;
93 } else if (static_cast<unsigned int>(stats->size) != sizeof(*stats)) { 82 } else if (static_cast<unsigned int>(stats->size) != sizeof(*stats)) {
94 size_t delta = sizeof(*stats) - static_cast<unsigned int>(stats->size); 83 size_t delta = sizeof(*stats) - static_cast<unsigned int>(stats->size);
95 memset(reinterpret_cast<char*>(stats) + stats->size, 0, delta); 84 memset(reinterpret_cast<char*>(stats) + stats->size, 0, delta);
96 stats->size = sizeof(*stats); 85 stats->size = sizeof(*stats);
97 } 86 }
98 87
99 return true; 88 return true;
100 } 89 }
101 90
102 bool StoreStats(BackendImpl* backend, Addr address, OnDiskStats* stats) { 91 Stats::Stats() : size_histogram_(NULL) {
103 MappedFile* file = backend->File(address);
104 if (!file)
105 return false;
106
107 size_t offset = address.start_block() * address.BlockSize() +
108 kBlockHeaderSize;
109 return file->Write(stats, sizeof(*stats), offset);
110 }
111
112 bool CreateStats(BackendImpl* backend, Addr* address, OnDiskStats* stats) {
113 if (!backend->CreateBlock(BLOCK_256, 2, address))
114 return false;
115
116 // If we have more than 512 bytes of counters, change kDiskSignature so we
117 // don't overwrite something else (LoadStats must fail).
118 COMPILE_ASSERT(sizeof(*stats) <= 256 * 2, use_more_blocks);
119 memset(stats, 0, sizeof(*stats));
120 stats->signature = kDiskSignature;
121 stats->size = sizeof(*stats);
122
123 return StoreStats(backend, *address, stats);
124 }
125
126 Stats::Stats() : backend_(NULL), size_histogram_(NULL) {
127 } 92 }
128 93
129 Stats::~Stats() { 94 Stats::~Stats() {
130 } 95 }
131 96
132 bool Stats::Init(BackendImpl* backend, uint32* storage_addr) { 97 bool Stats::Init(void* data, int num_bytes, Addr address) {
133 OnDiskStats stats; 98 OnDiskStats local_stats;
134 Addr address(*storage_addr); 99 OnDiskStats* stats = &local_stats;
135 if (address.is_initialized()) { 100 if (!num_bytes) {
136 if (!LoadStats(backend, address, &stats)) 101 memset(stats, 0, sizeof(local_stats));
102 local_stats.signature = kDiskSignature;
103 local_stats.size = sizeof(local_stats);
104 } else if (num_bytes >= static_cast<int>(sizeof(*stats))) {
105 stats = reinterpret_cast<OnDiskStats*>(data);
106 if (!VerifyStats(stats))
137 return false; 107 return false;
138 } else { 108 } else {
139 if (!CreateStats(backend, &address, &stats)) 109 return false;
140 return false;
141 *storage_addr = address.value();
142 } 110 }
143 111
144 storage_addr_ = address.value(); 112 storage_addr_ = address;
145 backend_ = backend;
146 113
147 memcpy(data_sizes_, stats.data_sizes, sizeof(data_sizes_)); 114 memcpy(data_sizes_, stats->data_sizes, sizeof(data_sizes_));
148 memcpy(counters_, stats.counters, sizeof(counters_)); 115 memcpy(counters_, stats->counters, sizeof(counters_));
116 return true;
117 }
149 118
119 void Stats::InitSizeHistogram() {
150 // It seems impossible to support this histogram for more than one 120 // It seems impossible to support this histogram for more than one
151 // simultaneous objects with the current infrastructure. 121 // simultaneous objects with the current infrastructure.
152 static bool first_time = true; 122 static bool first_time = true;
153 if (first_time) { 123 if (first_time) {
154 first_time = false; 124 first_time = false;
155 // ShouldReportAgain() will re-enter this object. 125 if (!size_histogram_) {
156 if (!size_histogram_ && backend->cache_type() == net::DISK_CACHE &&
157 backend->ShouldReportAgain()) {
158 // Stats may be reused when the cache is re-created, but we want only one 126 // Stats may be reused when the cache is re-created, but we want only one
159 // histogram at any given time. 127 // histogram at any given time.
160 size_histogram_ = StatsHistogram::FactoryGet("DiskCache.SizeStats", this); 128 size_histogram_ = StatsHistogram::FactoryGet("DiskCache.SizeStats", this);
161 } 129 }
162 } 130 }
131 }
163 132
164 return true; 133 int Stats::StorageSize() {
134 // If we have more than 512 bytes of counters, change kDiskSignature so we
135 // don't overwrite something else (LoadStats must fail).
136 COMPILE_ASSERT(sizeof(OnDiskStats) <= 256 * 2, use_more_blocks);
137 return 256 * 2;
165 } 138 }
166 139
167 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { 140 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) {
168 // We keep a counter of the data block size on an array where each entry is 141 // We keep a counter of the data block size on an array where each entry is
169 // the adjusted log base 2 of the size. The first entry counts blocks of 256 142 // the adjusted log base 2 of the size. The first entry counts blocks of 256
170 // bytes, the second blocks up to 512 bytes, etc. With 20 entries, the last 143 // bytes, the second blocks up to 512 bytes, etc. With 20 entries, the last
171 // one stores entries of more than 64 MB 144 // one stores entries of more than 64 MB
172 int new_index = GetStatsBucket(new_size); 145 int new_index = GetStatsBucket(new_size);
173 int old_index = GetStatsBucket(old_size); 146 int old_index = GetStatsBucket(old_size);
174 147
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 int Stats::GetLargeEntriesSize() { 200 int Stats::GetLargeEntriesSize() {
228 int total = 0; 201 int total = 0;
229 // data_sizes_[20] stores values between 512 KB and 1 MB (see comment before 202 // data_sizes_[20] stores values between 512 KB and 1 MB (see comment before
230 // GetStatsBucket()). 203 // GetStatsBucket()).
231 for (int bucket = 20; bucket < kDataSizesLength; bucket++) 204 for (int bucket = 20; bucket < kDataSizesLength; bucket++)
232 total += data_sizes_[bucket] * GetBucketRange(bucket); 205 total += data_sizes_[bucket] * GetBucketRange(bucket);
233 206
234 return total; 207 return total;
235 } 208 }
236 209
237 void Stats::Store() { 210 bool Stats::SerializeStats(void* data, int num_bytes, Addr* address) {
238 if (!backend_) 211 OnDiskStats* stats = reinterpret_cast<OnDiskStats*>(data);
239 return; 212 if (num_bytes < static_cast<int>(sizeof(*stats)))
213 return false;
gavinp 2013/05/23 15:32:33 Hmm. Odd that this is an error return; the only ca
rvargas (doing something else) 2013/05/23 19:39:43 The caller doesn't know what sizeof(OnDiskStats) i
240 214
241 OnDiskStats stats; 215 stats->signature = kDiskSignature;
242 stats.signature = kDiskSignature; 216 stats->size = sizeof(stats);
243 stats.size = sizeof(stats); 217 memcpy(stats->data_sizes, data_sizes_, sizeof(data_sizes_));
244 memcpy(stats.data_sizes, data_sizes_, sizeof(data_sizes_)); 218 memcpy(stats->counters, counters_, sizeof(counters_));
245 memcpy(stats.counters, counters_, sizeof(counters_));
246 219
247 Addr address(storage_addr_); 220 *address = storage_addr_;
248 StoreStats(backend_, address, &stats); 221 return true;
249 } 222 }
250 223
251 int Stats::GetBucketRange(size_t i) const { 224 int Stats::GetBucketRange(size_t i) const {
252 if (i < 2) 225 if (i < 2)
253 return static_cast<int>(1024 * i); 226 return static_cast<int>(1024 * i);
254 227
255 if (i < 12) 228 if (i < 12)
256 return static_cast<int>(2048 * (i - 1)); 229 return static_cast<int>(2048 * (i - 1));
257 230
258 if (i < 17) 231 if (i < 17)
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 int Stats::GetRatio(Counters hit, Counters miss) const { 297 int Stats::GetRatio(Counters hit, Counters miss) const {
325 int64 ratio = GetCounter(hit) * 100; 298 int64 ratio = GetCounter(hit) * 100;
326 if (!ratio) 299 if (!ratio)
327 return 0; 300 return 0;
328 301
329 ratio /= (GetCounter(hit) + GetCounter(miss)); 302 ratio /= (GetCounter(hit) + GetCounter(miss));
330 return static_cast<int>(ratio); 303 return static_cast<int>(ratio);
331 } 304 }
332 305
333 } // namespace disk_cache 306 } // namespace disk_cache
OLDNEW
« net/disk_cache/stats.h ('K') | « net/disk_cache/stats.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698