OLD | NEW |
---|---|
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/blockfile/stats.h" | 5 #include "net/disk_cache/blockfile/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/bucket_ranges.h" | |
10 #include "base/metrics/histogram.h" | |
9 #include "base/metrics/histogram_samples.h" | 11 #include "base/metrics/histogram_samples.h" |
12 #include "base/metrics/sample_vector.h" | |
13 #include "base/metrics/statistics_recorder.h" | |
10 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
12 | 16 |
13 namespace { | 17 namespace { |
14 | 18 |
15 const int32 kDiskSignature = 0xF01427E0; | 19 const int32 kDiskSignature = 0xF01427E0; |
16 | 20 |
17 struct OnDiskStats { | 21 struct OnDiskStats { |
18 int32 signature; | 22 int32 signature; |
19 int size; | 23 int size; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 stats->signature = kDiskSignature; | 85 stats->signature = kDiskSignature; |
82 } else if (static_cast<unsigned int>(stats->size) != sizeof(*stats)) { | 86 } else if (static_cast<unsigned int>(stats->size) != sizeof(*stats)) { |
83 size_t delta = sizeof(*stats) - static_cast<unsigned int>(stats->size); | 87 size_t delta = sizeof(*stats) - static_cast<unsigned int>(stats->size); |
84 memset(reinterpret_cast<char*>(stats) + stats->size, 0, delta); | 88 memset(reinterpret_cast<char*>(stats) + stats->size, 0, delta); |
85 stats->size = sizeof(*stats); | 89 stats->size = sizeof(*stats); |
86 } | 90 } |
87 | 91 |
88 return true; | 92 return true; |
89 } | 93 } |
90 | 94 |
91 Stats::Stats() : size_histogram_(NULL) { | 95 Stats::Stats() { |
92 } | 96 } |
93 | 97 |
94 Stats::~Stats() { | 98 Stats::~Stats() { |
95 if (size_histogram_) { | |
96 size_histogram_->Disable(); | |
97 } | |
98 } | 99 } |
99 | 100 |
100 bool Stats::Init(void* data, int num_bytes, Addr address) { | 101 bool Stats::Init(void* data, int num_bytes, Addr address) { |
101 OnDiskStats local_stats; | 102 OnDiskStats local_stats; |
102 OnDiskStats* stats = &local_stats; | 103 OnDiskStats* stats = &local_stats; |
103 if (!num_bytes) { | 104 if (!num_bytes) { |
104 memset(stats, 0, sizeof(local_stats)); | 105 memset(stats, 0, sizeof(local_stats)); |
105 local_stats.signature = kDiskSignature; | 106 local_stats.signature = kDiskSignature; |
106 local_stats.size = sizeof(local_stats); | 107 local_stats.size = sizeof(local_stats); |
107 } else if (num_bytes >= static_cast<int>(sizeof(*stats))) { | 108 } else if (num_bytes >= static_cast<int>(sizeof(*stats))) { |
108 stats = reinterpret_cast<OnDiskStats*>(data); | 109 stats = reinterpret_cast<OnDiskStats*>(data); |
109 if (!VerifyStats(stats)) | 110 if (!VerifyStats(stats)) |
110 return false; | 111 return false; |
111 } else { | 112 } else { |
112 return false; | 113 return false; |
113 } | 114 } |
114 | 115 |
115 storage_addr_ = address; | 116 storage_addr_ = address; |
116 | 117 |
117 memcpy(data_sizes_, stats->data_sizes, sizeof(data_sizes_)); | 118 memcpy(data_sizes_, stats->data_sizes, sizeof(data_sizes_)); |
118 memcpy(counters_, stats->counters, sizeof(counters_)); | 119 memcpy(counters_, stats->counters, sizeof(counters_)); |
119 | 120 |
120 // Clean up old value. | 121 // Clean up old value. |
121 SetCounter(UNUSED, 0); | 122 SetCounter(UNUSED, 0); |
122 return true; | 123 return true; |
123 } | 124 } |
124 | 125 |
125 void Stats::InitSizeHistogram() { | 126 void Stats::InitSizeHistogram() { |
126 // It seems impossible to support this histogram for more than one | 127 // Only generate this histogram for the main cache. |
127 // simultaneous objects with the current infrastructure. | |
128 static bool first_time = true; | 128 static bool first_time = true; |
129 if (first_time) { | 129 if (!first_time) |
Alexei Svitkine (slow)
2014/05/27 18:59:47
It seems that in the new version of the code, you'
rvargas (doing something else)
2014/05/27 19:36:38
The logic is pretty much the same: The histogram i
| |
130 first_time = false; | 130 return; |
131 if (!size_histogram_) { | 131 |
132 // Stats may be reused when the cache is re-created, but we want only one | 132 first_time = false; |
133 // histogram at any given time. | 133 int min = 1; |
134 size_histogram_ = StatsHistogram::FactoryGet("DiskCache.SizeStats", this); | 134 int max = 64 * 1024; |
135 } | 135 int num_buckets = 75; |
136 base::BucketRanges ranges(num_buckets + 1); | |
137 base::Histogram::InitializeBucketRanges(min, max, &ranges); | |
138 | |
139 base::HistogramBase* stats_histogram = base::Histogram::FactoryGet( | |
140 "DiskCache.SizeStats2", min, max, num_buckets, | |
Alexei Svitkine (slow)
2014/05/27 18:59:47
Please add a histograms.xml entry for this histogr
rvargas (doing something else)
2014/05/27 19:36:38
Done.
| |
141 base::HistogramBase::kUmaTargetedHistogramFlag); | |
142 | |
143 base::SampleVector samples(&ranges); | |
144 for (int i = 0; i < kDataSizesLength; i++) { | |
145 if (data_sizes_[i] < 0) | |
Alexei Svitkine (slow)
2014/05/27 18:59:47
This could use a comment.
rvargas (doing something else)
2014/05/27 19:36:38
Done.
| |
146 data_sizes_[i] = 0; | |
Alexei Svitkine (slow)
2014/05/27 18:59:47
Does it make sense for this to actually change dat
rvargas (doing something else)
2014/05/27 19:36:38
That's something that I saw when testing with my d
| |
147 | |
148 samples.Accumulate(GetBucketRange(i) / 1024, data_sizes_[i]); | |
136 } | 149 } |
150 stats_histogram->AddSamples(samples); | |
137 } | 151 } |
138 | 152 |
139 int Stats::StorageSize() { | 153 int Stats::StorageSize() { |
140 // If we have more than 512 bytes of counters, change kDiskSignature so we | 154 // If we have more than 512 bytes of counters, change kDiskSignature so we |
141 // don't overwrite something else (LoadStats must fail). | 155 // don't overwrite something else (LoadStats must fail). |
142 COMPILE_ASSERT(sizeof(OnDiskStats) <= 256 * 2, use_more_blocks); | 156 COMPILE_ASSERT(sizeof(OnDiskStats) <= 256 * 2, use_more_blocks); |
143 return 256 * 2; | 157 return 256 * 2; |
144 } | 158 } |
145 | 159 |
146 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { | 160 void Stats::ModifyStorageStats(int32 old_size, int32 new_size) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 if (i > static_cast<size_t>(kDataSizesLength)) { | 255 if (i > static_cast<size_t>(kDataSizesLength)) { |
242 NOTREACHED(); | 256 NOTREACHED(); |
243 i = kDataSizesLength; | 257 i = kDataSizesLength; |
244 } | 258 } |
245 | 259 |
246 i -= 17; | 260 i -= 17; |
247 n <<= i; | 261 n <<= i; |
248 return n; | 262 return n; |
249 } | 263 } |
250 | 264 |
251 void Stats::Snapshot(base::HistogramSamples* samples) const { | |
252 for (int i = 0; i < kDataSizesLength; i++) { | |
253 int count = data_sizes_[i]; | |
254 if (count < 0) | |
255 count = 0; | |
256 samples->Accumulate(GetBucketRange(i), count); | |
257 } | |
258 } | |
259 | |
260 // The array will be filled this way: | 265 // The array will be filled this way: |
261 // index size | 266 // index size |
262 // 0 [0, 1024) | 267 // 0 [0, 1024) |
263 // 1 [1024, 2048) | 268 // 1 [1024, 2048) |
264 // 2 [2048, 4096) | 269 // 2 [2048, 4096) |
265 // 3 [4K, 6K) | 270 // 3 [4K, 6K) |
266 // ... | 271 // ... |
267 // 10 [18K, 20K) | 272 // 10 [18K, 20K) |
268 // 11 [20K, 24K) | 273 // 11 [20K, 24K) |
269 // 12 [24k, 28K) | 274 // 12 [24k, 28K) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 int Stats::GetRatio(Counters hit, Counters miss) const { | 308 int Stats::GetRatio(Counters hit, Counters miss) const { |
304 int64 ratio = GetCounter(hit) * 100; | 309 int64 ratio = GetCounter(hit) * 100; |
305 if (!ratio) | 310 if (!ratio) |
306 return 0; | 311 return 0; |
307 | 312 |
308 ratio /= (GetCounter(hit) + GetCounter(miss)); | 313 ratio /= (GetCounter(hit) + GetCounter(miss)); |
309 return static_cast<int>(ratio); | 314 return static_cast<int>(ratio); |
310 } | 315 } |
311 | 316 |
312 } // namespace disk_cache | 317 } // namespace disk_cache |
OLD | NEW |