This source file includes following definitions.
- LogBase2
- VerifyStats
- Init
- InitSizeHistogram
- StorageSize
- ModifyStorageStats
- OnEvent
- SetCounter
- GetCounter
- GetItems
- GetHitRatio
- GetResurrectRatio
- ResetRatios
- GetLargeEntriesSize
- SerializeStats
- GetBucketRange
- Snapshot
- GetStatsBucket
- GetRatio
#include "net/disk_cache/blockfile/stats.h"
#include "base/format_macros.h"
#include "base/logging.h"
#include "base/metrics/histogram_samples.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
namespace {
const int32 kDiskSignature = 0xF01427E0;
struct OnDiskStats {
int32 signature;
int size;
int data_sizes[disk_cache::Stats::kDataSizesLength];
int64 counters[disk_cache::Stats::MAX_COUNTER];
};
COMPILE_ASSERT(sizeof(OnDiskStats) < 512, needs_more_than_2_blocks);
int LogBase2(int32 number) {
unsigned int value = static_cast<unsigned int>(number);
const unsigned int mask[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
const unsigned int s[] = {1, 2, 4, 8, 16};
unsigned int result = 0;
for (int i = 4; i >= 0; i--) {
if (value & mask[i]) {
value >>= s[i];
result |= s[i];
}
}
return static_cast<int>(result);
}
static const char* kCounterNames[] = {
"Open miss",
"Open hit",
"Create miss",
"Create hit",
"Resurrect hit",
"Create error",
"Trim entry",
"Doom entry",
"Doom cache",
"Invalid entry",
"Open entries",
"Max entries",
"Timer",
"Read data",
"Write data",
"Open rankings",
"Get rankings",
"Fatal error",
"Last report",
"Last report timer",
"Doom recent entries",
"unused"
};
COMPILE_ASSERT(arraysize(kCounterNames) == disk_cache::Stats::MAX_COUNTER,
update_the_names);
}
namespace disk_cache {
bool VerifyStats(OnDiskStats* stats) {
if (stats->signature != kDiskSignature)
return false;
if (static_cast<unsigned int>(stats->size) > sizeof(*stats)) {
memset(stats, 0, sizeof(*stats));
stats->signature = kDiskSignature;
} else if (static_cast<unsigned int>(stats->size) != sizeof(*stats)) {
size_t delta = sizeof(*stats) - static_cast<unsigned int>(stats->size);
memset(reinterpret_cast<char*>(stats) + stats->size, 0, delta);
stats->size = sizeof(*stats);
}
return true;
}
Stats::Stats() : size_histogram_(NULL) {
}
Stats::~Stats() {
if (size_histogram_) {
size_histogram_->Disable();
}
}
bool Stats::Init(void* data, int num_bytes, Addr address) {
OnDiskStats local_stats;
OnDiskStats* stats = &local_stats;
if (!num_bytes) {
memset(stats, 0, sizeof(local_stats));
local_stats.signature = kDiskSignature;
local_stats.size = sizeof(local_stats);
} else if (num_bytes >= static_cast<int>(sizeof(*stats))) {
stats = reinterpret_cast<OnDiskStats*>(data);
if (!VerifyStats(stats))
return false;
} else {
return false;
}
storage_addr_ = address;
memcpy(data_sizes_, stats->data_sizes, sizeof(data_sizes_));
memcpy(counters_, stats->counters, sizeof(counters_));
SetCounter(UNUSED, 0);
return true;
}
void Stats::InitSizeHistogram() {
static bool first_time = true;
if (first_time) {
first_time = false;
if (!size_histogram_) {
size_histogram_ = StatsHistogram::FactoryGet("DiskCache.SizeStats", this);
}
}
}
int Stats::StorageSize() {
COMPILE_ASSERT(sizeof(OnDiskStats) <= 256 * 2, use_more_blocks);
return 256 * 2;
}
void Stats::ModifyStorageStats(int32 old_size, int32 new_size) {
int new_index = GetStatsBucket(new_size);
int old_index = GetStatsBucket(old_size);
if (new_size)
data_sizes_[new_index]++;
if (old_size)
data_sizes_[old_index]--;
}
void Stats::OnEvent(Counters an_event) {
DCHECK(an_event >= MIN_COUNTER && an_event < MAX_COUNTER);
counters_[an_event]++;
}
void Stats::SetCounter(Counters counter, int64 value) {
DCHECK(counter >= MIN_COUNTER && counter < MAX_COUNTER);
counters_[counter] = value;
}
int64 Stats::GetCounter(Counters counter) const {
DCHECK(counter >= MIN_COUNTER && counter < MAX_COUNTER);
return counters_[counter];
}
void Stats::GetItems(StatsItems* items) {
std::pair<std::string, std::string> item;
for (int i = 0; i < kDataSizesLength; i++) {
item.first = base::StringPrintf("Size%02d", i);
item.second = base::StringPrintf("0x%08x", data_sizes_[i]);
items->push_back(item);
}
for (int i = MIN_COUNTER; i < MAX_COUNTER; i++) {
item.first = kCounterNames[i];
item.second = base::StringPrintf("0x%" PRIx64, counters_[i]);
items->push_back(item);
}
}
int Stats::GetHitRatio() const {
return GetRatio(OPEN_HIT, OPEN_MISS);
}
int Stats::GetResurrectRatio() const {
return GetRatio(RESURRECT_HIT, CREATE_HIT);
}
void Stats::ResetRatios() {
SetCounter(OPEN_HIT, 0);
SetCounter(OPEN_MISS, 0);
SetCounter(RESURRECT_HIT, 0);
SetCounter(CREATE_HIT, 0);
}
int Stats::GetLargeEntriesSize() {
int total = 0;
for (int bucket = 20; bucket < kDataSizesLength; bucket++)
total += data_sizes_[bucket] * GetBucketRange(bucket);
return total;
}
int Stats::SerializeStats(void* data, int num_bytes, Addr* address) {
OnDiskStats* stats = reinterpret_cast<OnDiskStats*>(data);
if (num_bytes < static_cast<int>(sizeof(*stats)))
return 0;
stats->signature = kDiskSignature;
stats->size = sizeof(*stats);
memcpy(stats->data_sizes, data_sizes_, sizeof(data_sizes_));
memcpy(stats->counters, counters_, sizeof(counters_));
*address = storage_addr_;
return sizeof(*stats);
}
int Stats::GetBucketRange(size_t i) const {
if (i < 2)
return static_cast<int>(1024 * i);
if (i < 12)
return static_cast<int>(2048 * (i - 1));
if (i < 17)
return static_cast<int>(4096 * (i - 11)) + 20 * 1024;
int n = 64 * 1024;
if (i > static_cast<size_t>(kDataSizesLength)) {
NOTREACHED();
i = kDataSizesLength;
}
i -= 17;
n <<= i;
return n;
}
void Stats::Snapshot(base::HistogramSamples* samples) const {
for (int i = 0; i < kDataSizesLength; i++) {
int count = data_sizes_[i];
if (count < 0)
count = 0;
samples->Accumulate(GetBucketRange(i), count);
}
}
int Stats::GetStatsBucket(int32 size) {
if (size < 1024)
return 0;
if (size < 20 * 1024)
return size / 2048 + 1;
if (size < 40 * 1024)
return (size - 20 * 1024) / 4096 + 11;
int result = LogBase2(size) + 1;
COMPILE_ASSERT(kDataSizesLength > 16, update_the_scale);
if (result >= kDataSizesLength)
result = kDataSizesLength - 1;
return result;
}
int Stats::GetRatio(Counters hit, Counters miss) const {
int64 ratio = GetCounter(hit) * 100;
if (!ratio)
return 0;
ratio /= (GetCounter(hit) + GetCounter(miss));
return static_cast<int>(ratio);
}
}