Commit 4a32f123 authored by kaiwang@chromium.org's avatar kaiwang@chromium.org

This is a copy of http://codereview.chromium.org/10796047/

It was reverted in https://chromiumcodereview.appspot.com/10807083/ because it unveiled a problem of build_nexe.py on Windows (http://crbug.com/138687).
Now the bug is fixed so submit this CL again.

Review URL: https://chromiumcodereview.appspot.com/10809076

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148391 0039d316-1c4b-4281-b951-d872f2087c98
parent 6d91451a
...@@ -436,6 +436,7 @@ ...@@ -436,6 +436,7 @@
'message_loop_unittest.cc', 'message_loop_unittest.cc',
'message_pump_glib_unittest.cc', 'message_pump_glib_unittest.cc',
'message_pump_libevent_unittest.cc', 'message_pump_libevent_unittest.cc',
'metrics/bucket_ranges_unittest.cc',
'metrics/field_trial_unittest.cc', 'metrics/field_trial_unittest.cc',
'metrics/histogram_unittest.cc', 'metrics/histogram_unittest.cc',
'metrics/stats_table_unittest.cc', 'metrics/stats_table_unittest.cc',
......
...@@ -236,6 +236,8 @@ ...@@ -236,6 +236,8 @@
'message_pump_default.h', 'message_pump_default.h',
'message_pump_win.cc', 'message_pump_win.cc',
'message_pump_win.h', 'message_pump_win.h',
'metrics/bucket_ranges.cc',
'metrics/bucket_ranges.h',
'metrics/histogram.cc', 'metrics/histogram.cc',
'metrics/histogram.h', 'metrics/histogram.h',
'metrics/histogram_base.cc', 'metrics/histogram_base.cc',
......
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/metrics/bucket_ranges.h"
#include <cmath>
#include "base/logging.h"
namespace base {
// Static table of checksums for all possible 8 bit bytes.
static const uint32 kCrcTable[256] = { 0x0, 0x77073096L, 0xee0e612cL,
0x990951baL, 0x76dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0xedb8832L,
0x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L, 0x9b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, 0x1adad47dL,
0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, 0x646ba8c0L, 0xfd62f97aL,
0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L,
0x4c69105eL, 0xd56041e4L, 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL,
0xa50ab56bL, 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, 0xc8d75180L,
0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, 0xb8bda50fL, 0x2802b89eL,
0x5f058808L, 0xc60cd9b2L, 0xb10be924L, 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL,
0xb6662d3dL, 0x76dc4190L, 0x1db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L,
0x6b6b51fL, 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0xf00f934L, 0x9609a88eL,
0xe10e9818L, 0x7f6a0dbbL, 0x86d3d2dL, 0x91646c97L, 0xe6635c01L, 0x6b6b51f4L,
0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, 0x1b01a57bL, 0x8208f4c1L,
0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL,
0x15da2d49L, 0x8cd37cf3L, 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L,
0xd4bb30e2L, 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, 0xaa0a4c5fL,
0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, 0xc90c2086L, 0x5768b525L,
0x206f85b3L, 0xb966d409L, 0xce61e49fL, 0x5edef90eL, 0x29d9c998L, 0xb0d09822L,
0xc7d7a8b4L, 0x59b33d17L, 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L,
0x9abfb3b6L, 0x3b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x4db2615L,
0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0xd6d6a3eL, 0x7a6a5aa8L, 0xe40ecf0bL,
0x9309ff9dL, 0xa00ae27L, 0x7d079eb1L, 0xf00f9344L, 0x8708a3d2L, 0x1e01f268L,
0x6906c2feL, 0xf762575dL, 0x806567cbL, 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L,
0x89d32be0L, 0x10da7a5aL, 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L,
0x60b08ed5L, 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, 0x36034af6L,
0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, 0x4669be79L, 0xcb61b38cL,
0xbc66831aL, 0x256fd2a0L, 0x5268e236L, 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L,
0x5505262fL, 0xc5ba3bbeL, 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L,
0xb5d0cf31L, 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
0x26d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x5005713L, 0x95bf4a82L,
0xe2b87a14L, 0x7bb12baeL, 0xcb61b38L, 0x92d28e9bL, 0xe5d5be0dL, 0x7cdcefb7L,
0xbdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL,
0xf6b9265bL, 0x6fb077e1L, 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL,
0x11010b5cL, 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, 0x4969474dL,
0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, 0x37d83bf0L, 0xa9bcae53L,
0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L,
0x24b4a3a6L, 0xbad03605L, 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL,
0xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
0x2d02ef8dL,
};
// We generate the CRC-32 using the low order bits to select whether to XOR in
// the reversed polynomial 0xedb88320L. This is nice and simple, and allows us
// to keep the quotient in a uint32. Since we're not concerned about the nature
// of corruptions (i.e., we don't care about bit sequencing, since we are
// handling memory changes, which are more grotesque) so we don't bother to
// get the CRC correct for big-endian vs little-ending calculations. All we
// need is a nice hash, that tends to depend on all the bits of the sample, with
// very little chance of changes in one place impacting changes in another
// place.
static uint32 Crc32(uint32 sum, HistogramBase::Sample value) {
// TODO(jar): Switch to false and watch stats.
const bool kUseRealCrc = true;
if (kUseRealCrc) {
union {
HistogramBase::Sample range;
unsigned char bytes[sizeof(HistogramBase::Sample)];
} converter;
converter.range = value;
for (size_t i = 0; i < sizeof(converter); ++i)
sum = kCrcTable[(sum & 0xff) ^ converter.bytes[i]] ^ (sum >> 8);
} else {
// Use hash techniques provided in ReallyFastHash, except we don't care
// about "avalanching" (which would worsten the hash, and add collisions),
// and we don't care about edge cases since we have an even number of bytes.
union {
HistogramBase::Sample range;
uint16 ints[sizeof(HistogramBase::Sample) / 2];
} converter;
DCHECK_EQ(sizeof(HistogramBase::Sample), sizeof(converter));
converter.range = value;
sum += converter.ints[0];
sum = (sum << 16) ^ sum ^ (static_cast<uint32>(converter.ints[1]) << 11);
sum += sum >> 11;
}
return sum;
}
BucketRanges::BucketRanges(size_t num_ranges)
: ranges_(num_ranges, 0),
checksum_(0) {}
BucketRanges::~BucketRanges() {}
void BucketRanges::set_range(size_t i, HistogramBase::Sample value) {
DCHECK_LT(i, ranges_.size());
DCHECK_GE(value, 0);
ranges_[i] = value;
}
uint32 BucketRanges::CalculateChecksum() {
// Seed checksum.
uint32 checksum = static_cast<uint32>(ranges_.size());
for (size_t index = 0; index < ranges_.size(); ++index)
checksum = Crc32(checksum, ranges_[index]);
return checksum;
}
bool BucketRanges::HasValidChecksum() {
return CalculateChecksum() == checksum_;
}
void BucketRanges::ResetChecksum() {
checksum_ = CalculateChecksum();
}
bool BucketRanges::Equals(const BucketRanges* other) const {
if (checksum_ != other->checksum_)
return false;
if (ranges_.size() != other->ranges_.size())
return false;
for (size_t index = 0; index < ranges_.size(); ++index) {
if (ranges_[index] != other->ranges_[index])
return false;
}
return true;
}
} // namespace base
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// BucketRanges stores the vector of ranges that delimit what samples are
// tallied in the corresponding buckets of a histogram. Histograms that have
// same ranges for all their corresponding buckets should share the same
// BucketRanges object.
//
// E.g. A 5 buckets LinearHistogram with 1 as minimal value and 4 as maximal
// value will need a BucketRanges with 6 ranges:
// 0, 1, 2, 3, 4, INT_MAX
//
// TODO(kaiwang): Currently we keep all negative values in 0~1 bucket. Consider
// changing 0 to INT_MIN.
#ifndef BASE_METRICS_BUCKET_RANGES_H_
#define BASE_METRICS_BUCKET_RANGES_H_
#include <vector>
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/metrics/histogram_base.h"
namespace base {
class BASE_EXPORT_PRIVATE BucketRanges {
public:
typedef std::vector<HistogramBase::Sample> Ranges;
BucketRanges(size_t num_ranges);
~BucketRanges();
size_t size() const { return ranges_.size(); }
HistogramBase::Sample range(size_t i) const { return ranges_[i]; }
void set_range(size_t i, HistogramBase::Sample value);
uint32 checksum() const { return checksum_; }
void set_checksum(uint32 checksum) { checksum_ = checksum; }
// Checksum methods to verify whether the ranges are corrupted (e.g. bad
// memory access).
uint32 CalculateChecksum();
bool HasValidChecksum();
void ResetChecksum();
// Return true iff |other| object has same ranges_ as |this| object's ranges_.
bool Equals(const BucketRanges* other) const;
private:
// A monotonically increasing list of values which determine which bucket to
// put a sample into. For each index, show the smallest sample that can be
// added to the corresponding bucket.
Ranges ranges_;
// Checksum for the conntents of ranges_. Used to detect random over-writes
// of our data, and to quickly see if some other BucketRanges instance is
// possibly Equal() to this instance.
// TODO(kaiwang): Consider change this to uint64. Because we see a lot of
// noise on UMA dashboard.
uint32 checksum_;
DISALLOW_COPY_AND_ASSIGN(BucketRanges);
};
} // namespace base
#endif // BASE_METRICS_BUCKET_RANGES_H_
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/metrics/bucket_ranges.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
TEST(BucketRangesTest, NormalSetup) {
BucketRanges ranges(5);
ASSERT_EQ(5u, ranges.size());
for (int i = 0; i < 5; i++) {
EXPECT_EQ(0, ranges.range(i));
}
EXPECT_EQ(0u, ranges.checksum());
ranges.set_range(3, 100);
EXPECT_EQ(100, ranges.range(3));
}
TEST(BucketRangesTest, Equals) {
// Compare empty ranges.
BucketRanges ranges1(3);
BucketRanges ranges2(3);
BucketRanges ranges3(5);
EXPECT_TRUE(ranges1.Equals(&ranges2));
EXPECT_FALSE(ranges1.Equals(&ranges3));
EXPECT_FALSE(ranges2.Equals(&ranges3));
// Compare full filled ranges.
ranges1.set_range(0, 0);
ranges1.set_range(1, 1);
ranges1.set_range(2, 2);
ranges1.set_checksum(100);
ranges2.set_range(0, 0);
ranges2.set_range(1, 1);
ranges2.set_range(2, 2);
ranges2.set_checksum(100);
EXPECT_TRUE(ranges1.Equals(&ranges2));
// Checksum does not match.
ranges1.set_checksum(99);
EXPECT_FALSE(ranges1.Equals(&ranges2));
ranges1.set_checksum(100);
// Range does not match.
ranges1.set_range(1, 3);
EXPECT_FALSE(ranges1.Equals(&ranges2));
}
TEST(BucketRangesTest, Checksum) {
BucketRanges ranges(3);
ranges.set_range(0, 0);
ranges.set_range(1, 1);
ranges.set_range(2, 2);
ranges.ResetChecksum();
EXPECT_EQ(289217253u, ranges.checksum());
ranges.set_range(2, 3);
EXPECT_FALSE(ranges.HasValidChecksum());
ranges.ResetChecksum();
EXPECT_EQ(2843835776u, ranges.checksum());
EXPECT_TRUE(ranges.HasValidChecksum());
}
} // namespace
} // namespace base
...@@ -383,7 +383,7 @@ Histogram::ClassType Histogram::histogram_type() const { ...@@ -383,7 +383,7 @@ Histogram::ClassType Histogram::histogram_type() const {
} }
Histogram::Sample Histogram::ranges(size_t i) const { Histogram::Sample Histogram::ranges(size_t i) const {
return cached_ranges_->ranges(i); return bucket_ranges_->range(i);
} }
size_t Histogram::bucket_count() const { size_t Histogram::bucket_count() const {
...@@ -423,7 +423,7 @@ Histogram::Histogram(const std::string& name, Sample minimum, ...@@ -423,7 +423,7 @@ Histogram::Histogram(const std::string& name, Sample minimum,
declared_max_(maximum), declared_max_(maximum),
bucket_count_(bucket_count), bucket_count_(bucket_count),
flags_(kNoFlags), flags_(kNoFlags),
cached_ranges_(new CachedRanges(bucket_count + 1, 0)), bucket_ranges_(new BucketRanges(bucket_count + 1)),
range_checksum_(0), range_checksum_(0),
sample_() { sample_() {
Initialize(); Initialize();
...@@ -436,7 +436,7 @@ Histogram::Histogram(const std::string& name, TimeDelta minimum, ...@@ -436,7 +436,7 @@ Histogram::Histogram(const std::string& name, TimeDelta minimum,
declared_max_(static_cast<int> (maximum.InMilliseconds())), declared_max_(static_cast<int> (maximum.InMilliseconds())),
bucket_count_(bucket_count), bucket_count_(bucket_count),
flags_(kNoFlags), flags_(kNoFlags),
cached_ranges_(new CachedRanges(bucket_count + 1, 0)), bucket_ranges_(new BucketRanges(bucket_count + 1)),
range_checksum_(0), range_checksum_(0),
sample_() { sample_() {
Initialize(); Initialize();
...@@ -557,12 +557,12 @@ void Histogram::Accumulate(Sample value, Count count, size_t index) { ...@@ -557,12 +557,12 @@ void Histogram::Accumulate(Sample value, Count count, size_t index) {
void Histogram::SetBucketRange(size_t i, Sample value) { void Histogram::SetBucketRange(size_t i, Sample value) {
DCHECK_GT(bucket_count_, i); DCHECK_GT(bucket_count_, i);
DCHECK_GE(value, 0); DCHECK_GE(value, 0);
cached_ranges_->SetBucketRange(i, value); bucket_ranges_->set_range(i, value);
} }
bool Histogram::ValidateBucketRanges() const { bool Histogram::ValidateBucketRanges() const {
// Standard assertions that all bucket ranges should satisfy. // Standard assertions that all bucket ranges should satisfy.
DCHECK_EQ(bucket_count_ + 1, cached_ranges_->size()); DCHECK_EQ(bucket_count_ + 1, bucket_ranges_->size());
DCHECK_EQ(0, ranges(0)); DCHECK_EQ(0, ranges(0));
DCHECK_EQ(declared_min(), ranges(1)); DCHECK_EQ(declared_min(), ranges(1));
DCHECK_EQ(declared_max(), ranges(bucket_count_ - 1)); DCHECK_EQ(declared_max(), ranges(bucket_count_ - 1));
...@@ -571,9 +571,9 @@ bool Histogram::ValidateBucketRanges() const { ...@@ -571,9 +571,9 @@ bool Histogram::ValidateBucketRanges() const {
} }
uint32 Histogram::CalculateRangeChecksum() const { uint32 Histogram::CalculateRangeChecksum() const {
DCHECK_EQ(cached_ranges_->size(), bucket_count() + 1); DCHECK_EQ(bucket_ranges_->size(), bucket_count() + 1);
// Seed checksum. // Seed checksum.
uint32 checksum = static_cast<uint32>(cached_ranges_->size()); uint32 checksum = static_cast<uint32>(bucket_ranges_->size());
for (size_t index = 0; index < bucket_count(); ++index) for (size_t index = 0; index < bucket_count(); ++index)
checksum = Crc32(checksum, ranges(index)); checksum = Crc32(checksum, ranges(index));
return checksum; return checksum;
...@@ -591,7 +591,7 @@ void Histogram::Initialize() { ...@@ -591,7 +591,7 @@ void Histogram::Initialize() {
size_t maximal_bucket_count = declared_max_ - declared_min_ + 2; size_t maximal_bucket_count = declared_max_ - declared_min_ + 2;
DCHECK_LE(bucket_count_, maximal_bucket_count); DCHECK_LE(bucket_count_, maximal_bucket_count);
DCHECK_EQ(0, ranges(0)); DCHECK_EQ(0, ranges(0));
cached_ranges_->SetBucketRange(bucket_count_, kSampleType_MAX); bucket_ranges_->set_range(bucket_count_, kSampleType_MAX);
} }
// We generate the CRC-32 using the low order bits to select whether to XOR in // We generate the CRC-32 using the low order bits to select whether to XOR in
...@@ -1000,8 +1000,8 @@ CustomHistogram::CustomHistogram(const std::string& name, ...@@ -1000,8 +1000,8 @@ CustomHistogram::CustomHistogram(const std::string& name,
} }
bool CustomHistogram::SerializeRanges(Pickle* pickle) const { bool CustomHistogram::SerializeRanges(Pickle* pickle) const {
for (size_t i = 0; i < cached_ranges()->size(); ++i) { for (size_t i = 0; i < bucket_ranges()->size(); ++i) {
if (!pickle->WriteInt(cached_ranges()->ranges(i))) if (!pickle->WriteInt(bucket_ranges()->range(i)))
return false; return false;
} }
return true; return true;
...@@ -1031,30 +1031,4 @@ double CustomHistogram::GetBucketSize(Count current, size_t i) const { ...@@ -1031,30 +1031,4 @@ double CustomHistogram::GetBucketSize(Count current, size_t i) const {
return 1; return 1;
} }
CachedRanges::CachedRanges(size_t bucket_count, int initial_value)
: ranges_(bucket_count, initial_value),
range_checksum_(0) {
}
CachedRanges::~CachedRanges() {
}
void CachedRanges::SetBucketRange(size_t i, Histogram::Sample value) {
DCHECK_LT(i, ranges_.size());
DCHECK_GE(value, 0);
ranges_[i] = value;
}
bool CachedRanges::Equals(CachedRanges* other) const {
if (range_checksum_ != other->range_checksum_)
return false;
if (ranges_.size() != other->ranges_.size())
return false;
for (size_t index = 0; index < ranges_.size(); ++index) {
if (ranges_[index] != other->ranges_[index])
return false;
}
return true;
}
} // namespace base } // namespace base
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/metrics/bucket_ranges.h"
#include "base/metrics/histogram_base.h" #include "base/metrics/histogram_base.h"
#include "base/time.h" #include "base/time.h"
...@@ -318,7 +319,7 @@ class Lock; ...@@ -318,7 +319,7 @@ class Lock;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
class BooleanHistogram; class BooleanHistogram;
class CachedRanges; class BucketRanges;
class CustomHistogram; class CustomHistogram;
class Histogram; class Histogram;
class LinearHistogram; class LinearHistogram;
...@@ -468,9 +469,6 @@ class BASE_EXPORT Histogram : public HistogramBase { ...@@ -468,9 +469,6 @@ class BASE_EXPORT Histogram : public HistogramBase {
// The following methods provide graphical histogram displays. // The following methods provide graphical histogram displays.
virtual void WriteHTMLGraph(std::string* output) const OVERRIDE; virtual void WriteHTMLGraph(std::string* output) const OVERRIDE;
virtual void WriteAscii(std::string* output) const OVERRIDE; virtual void WriteAscii(std::string* output) const OVERRIDE;
void WriteAsciiImpl(bool graph_it,
const std::string& newline,
std::string* output) const;
// Support generic flagging of Histograms. // Support generic flagging of Histograms.
// 0x1 Currently used to mark this histogram to be recorded by UMA.. // 0x1 Currently used to mark this histogram to be recorded by UMA..
...@@ -511,9 +509,9 @@ class BASE_EXPORT Histogram : public HistogramBase { ...@@ -511,9 +509,9 @@ class BASE_EXPORT Histogram : public HistogramBase {
virtual Sample ranges(size_t i) const; virtual Sample ranges(size_t i) const;
uint32 range_checksum() const { return range_checksum_; } uint32 range_checksum() const { return range_checksum_; }
virtual size_t bucket_count() const; virtual size_t bucket_count() const;
CachedRanges* cached_ranges() const { return cached_ranges_; } BucketRanges* bucket_ranges() const { return bucket_ranges_; }
void set_cached_ranges(CachedRanges* cached_ranges) { void set_bucket_ranges(BucketRanges* bucket_ranges) {
cached_ranges_ = cached_ranges; bucket_ranges_ = bucket_ranges;
} }
// Snapshot the current complete set of sample data. // Snapshot the current complete set of sample data.
// Override with atomic/locked snapshot if needed. // Override with atomic/locked snapshot if needed.
...@@ -526,7 +524,7 @@ class BASE_EXPORT Histogram : public HistogramBase { ...@@ -526,7 +524,7 @@ class BASE_EXPORT Histogram : public HistogramBase {
TimeDelta maximum, TimeDelta maximum,
size_t bucket_count); size_t bucket_count);
// Return true iff the range_checksum_ matches current |ranges_| vector in // Return true iff the range_checksum_ matches current |ranges_| vector in
// |cached_ranges_|. // |bucket_ranges_|.
bool HasValidRangeChecksum() const; bool HasValidRangeChecksum() const;
protected: protected:
...@@ -543,7 +541,7 @@ class BASE_EXPORT Histogram : public HistogramBase { ...@@ -543,7 +541,7 @@ class BASE_EXPORT Histogram : public HistogramBase {
// serialized parameters. // serialized parameters.
virtual bool SerializeRanges(Pickle* pickle) const; virtual bool SerializeRanges(Pickle* pickle) const;
// Initialize ranges_ mapping in cached_ranges_. // Initialize ranges_ mapping in bucket_ranges_.
void InitializeBucketRange(); void InitializeBucketRange();
// Method to override to skip the display of the i'th bucket if it's empty. // Method to override to skip the display of the i'th bucket if it's empty.
...@@ -576,7 +574,7 @@ class BASE_EXPORT Histogram : public HistogramBase { ...@@ -576,7 +574,7 @@ class BASE_EXPORT Histogram : public HistogramBase {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void SetBucketRange(size_t i, Sample value); void SetBucketRange(size_t i, Sample value);
// Validate that ranges_ in cached_ranges_ was created sensibly (top and // Validate that ranges_ in bucket_ranges_ was created sensibly (top and
// bottom range values relate properly to the declared_min_ and // bottom range values relate properly to the declared_min_ and
// declared_max_). // declared_max_).
bool ValidateBucketRanges() const; bool ValidateBucketRanges() const;
...@@ -601,6 +599,10 @@ class BASE_EXPORT Histogram : public HistogramBase { ...@@ -601,6 +599,10 @@ class BASE_EXPORT Histogram : public HistogramBase {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
// Helpers for emitting Ascii graphic. Each method appends data to output. // Helpers for emitting Ascii graphic. Each method appends data to output.
void WriteAsciiImpl(bool graph_it,
const std::string& newline,
std::string* output) const;
// Find out how large the (graphically) the largest bucket will appear to be. // Find out how large the (graphically) the largest bucket will appear to be.
double GetPeakBucketSize(const SampleSet& snapshot) const; double GetPeakBucketSize(const SampleSet& snapshot) const;
...@@ -639,8 +641,8 @@ class BASE_EXPORT Histogram : public HistogramBase { ...@@ -639,8 +641,8 @@ class BASE_EXPORT Histogram : public HistogramBase {
// For each index, show the least value that can be stored in the // For each index, show the least value that can be stored in the
// corresponding bucket. We also append one extra element in this array, // corresponding bucket. We also append one extra element in this array,
// containing kSampleType_MAX, to make calculations easy. // containing kSampleType_MAX, to make calculations easy.
// The dimension of ranges_ in cached_ranges_ is bucket_count + 1. // The dimension of ranges_ in bucket_ranges_ is bucket_count + 1.
CachedRanges* cached_ranges_; BucketRanges* bucket_ranges_;
// For redundancy, we store a checksum of all the sample ranges when ranges // For redundancy, we store a checksum of all the sample ranges when ranges
// are generated. If ever there is ever a difference, then the histogram must // are generated. If ever there is ever a difference, then the histogram must
...@@ -690,7 +692,7 @@ class BASE_EXPORT LinearHistogram : public Histogram { ...@@ -690,7 +692,7 @@ class BASE_EXPORT LinearHistogram : public Histogram {
LinearHistogram(const std::string& name, TimeDelta minimum, LinearHistogram(const std::string& name, TimeDelta minimum,
TimeDelta maximum, size_t bucket_count); TimeDelta maximum, size_t bucket_count);
// Initialize ranges_ mapping in cached_ranges_. // Initialize ranges_ mapping in bucket_ranges_.
void InitializeBucketRange(); void InitializeBucketRange();
virtual double GetBucketSize(Count current, size_t i) const OVERRIDE; virtual double GetBucketSize(Count current, size_t i) const OVERRIDE;
...@@ -762,51 +764,13 @@ class BASE_EXPORT CustomHistogram : public Histogram { ...@@ -762,51 +764,13 @@ class BASE_EXPORT CustomHistogram : public Histogram {
virtual bool SerializeRanges(Pickle* pickle) const OVERRIDE; virtual bool SerializeRanges(Pickle* pickle) const OVERRIDE;
// Initialize ranges_ mapping in cached_ranges_. // Initialize ranges_ mapping in bucket_ranges_.
void InitializedCustomBucketRange(const std::vector<Sample>& custom_ranges); void InitializedCustomBucketRange(const std::vector<Sample>& custom_ranges);
virtual double GetBucketSize(Count current, size_t i) const OVERRIDE; virtual double GetBucketSize(Count current, size_t i) const OVERRIDE;
DISALLOW_COPY_AND_ASSIGN(CustomHistogram); DISALLOW_COPY_AND_ASSIGN(CustomHistogram);
}; };
// CachedRanges stores the Ranges vector. Histograms that have same Ranges
// vector will use the same CachedRanges object.
class BASE_EXPORT CachedRanges {
public:
typedef std::vector<Histogram::Sample> Ranges;
CachedRanges(size_t bucket_count, int initial_value);
~CachedRanges();
//----------------------------------------------------------------------------
// Accessors methods for ranges_ and range_checksum_.
//----------------------------------------------------------------------------
size_t size() const { return ranges_.size(); }
Histogram::Sample ranges(size_t i) const { return ranges_[i]; }
void SetBucketRange(size_t i, Histogram::Sample value);
uint32 range_checksum(uint32 checksum) const { return range_checksum_; }
void SetRangeChecksum(uint32 checksum) { range_checksum_ = checksum; }
// Return true iff |other| object has same ranges_ as |this| object's ranges_.
bool Equals(CachedRanges* other) const;
private:
// Allow tests to corrupt our innards for testing purposes.
FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptBucketBounds);
// A monotonically increasing list of values which determine which bucket to
// put a sample into. For each index, show the smallest sample that can be
// added to the corresponding bucket.
Ranges ranges_;
// Checksum for the conntents of ranges_. Used to detect random over-writes
// of our data, and to quickly see if some other CachedRanges instance is
// possibly Equal() to this instance.
uint32 range_checksum_;
DISALLOW_COPY_AND_ASSIGN(CachedRanges);
};
} // namespace base } // namespace base
#endif // BASE_METRICS_HISTOGRAM_H_ #endif // BASE_METRICS_HISTOGRAM_H_
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/metrics/bucket_ranges.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/metrics/statistics_recorder.h" #include "base/metrics/statistics_recorder.h"
#include "base/time.h" #include "base/time.h"
...@@ -382,26 +383,29 @@ TEST(HistogramTest, CorruptBucketBounds) { ...@@ -382,26 +383,29 @@ TEST(HistogramTest, CorruptBucketBounds) {
EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0);
EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption.
CachedRanges* cached_ranges = histogram->cached_ranges(); BucketRanges* bucket_ranges = histogram->bucket_ranges();
std::swap(cached_ranges->ranges_[1], cached_ranges->ranges_[2]); HistogramBase::Sample tmp = bucket_ranges->range(1);
bucket_ranges->set_range(1, bucket_ranges->range(2));
bucket_ranges->set_range(2, tmp);
EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR, EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR,
histogram->FindCorruption(snapshot)); histogram->FindCorruption(snapshot));
std::swap(cached_ranges->ranges_[1], cached_ranges->ranges_[2]); bucket_ranges->set_range(2, bucket_ranges->range(1));
bucket_ranges->set_range(1, tmp);
EXPECT_EQ(0, histogram->FindCorruption(snapshot)); EXPECT_EQ(0, histogram->FindCorruption(snapshot));
++cached_ranges->ranges_[3]; bucket_ranges->set_range(3, bucket_ranges->range(3) + 1);
EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR,
histogram->FindCorruption(snapshot)); histogram->FindCorruption(snapshot));
// Show that two simple changes don't offset each other // Show that two simple changes don't offset each other
--cached_ranges->ranges_[4]; bucket_ranges->set_range(4, bucket_ranges->range(4) - 1);
EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR, EXPECT_EQ(Histogram::RANGE_CHECKSUM_ERROR,
histogram->FindCorruption(snapshot)); histogram->FindCorruption(snapshot));
// Repair histogram so that destructor won't DCHECK(). // Repair histogram so that destructor won't DCHECK().
--cached_ranges->ranges_[3]; bucket_ranges->set_range(3, bucket_ranges->range(3) - 1);
++cached_ranges->ranges_[4]; bucket_ranges->set_range(4, bucket_ranges->range(4) + 1);
} }
// Table was generated similarly to sample code for CRC-32 given on: // Table was generated similarly to sample code for CRC-32 given on:
...@@ -420,9 +424,9 @@ TEST(HistogramTest, Crc32TableTest) { ...@@ -420,9 +424,9 @@ TEST(HistogramTest, Crc32TableTest) {
} }
} }
// RangeTest, CustomRangeTest and CorruptBucketBounds test CachedRanges class. // RangeTest, CustomRangeTest and CorruptBucketBounds test BucketRanges class.
// The following tests sharing of CachedRanges object. // The following tests sharing of BucketRanges object.
TEST(HistogramTest, CachedRangesTest) { TEST(HistogramTest, BucketRangesTest) {
StatisticsRecorder recorder; StatisticsRecorder recorder;
StatisticsRecorder::Histograms histograms; StatisticsRecorder::Histograms histograms;
...@@ -438,11 +442,11 @@ TEST(HistogramTest, CachedRangesTest) { ...@@ -438,11 +442,11 @@ TEST(HistogramTest, CachedRangesTest) {
Histogram* histogram3(Histogram::FactoryGet( Histogram* histogram3(Histogram::FactoryGet(
"Histogram3", 1, 64, 16, Histogram::kNoFlags)); "Histogram3", 1, 64, 16, Histogram::kNoFlags));
CachedRanges* cached_ranges1 = histogram1->cached_ranges(); BucketRanges* bucket_ranges1 = histogram1->bucket_ranges();
CachedRanges* cached_ranges2 = histogram2->cached_ranges(); BucketRanges* bucket_ranges2 = histogram2->bucket_ranges();
CachedRanges* cached_ranges3 = histogram3->cached_ranges(); BucketRanges* bucket_ranges3 = histogram3->bucket_ranges();
EXPECT_TRUE(cached_ranges1->Equals(cached_ranges2)); EXPECT_TRUE(bucket_ranges1->Equals(bucket_ranges2));
EXPECT_FALSE(cached_ranges1->Equals(cached_ranges3)); EXPECT_FALSE(bucket_ranges1->Equals(bucket_ranges3));
} }
} // namespace base } // namespace base
...@@ -121,32 +121,32 @@ Histogram* StatisticsRecorder::RegisterOrDeleteDuplicate(Histogram* histogram) { ...@@ -121,32 +121,32 @@ Histogram* StatisticsRecorder::RegisterOrDeleteDuplicate(Histogram* histogram) {
// static // static
void StatisticsRecorder::RegisterOrDeleteDuplicateRanges(Histogram* histogram) { void StatisticsRecorder::RegisterOrDeleteDuplicateRanges(Histogram* histogram) {
DCHECK(histogram); DCHECK(histogram);
CachedRanges* histogram_ranges = histogram->cached_ranges(); BucketRanges* histogram_ranges = histogram->bucket_ranges();
DCHECK(histogram_ranges); DCHECK(histogram_ranges);
uint32 checksum = histogram->range_checksum(); uint32 checksum = histogram->range_checksum();
histogram_ranges->SetRangeChecksum(checksum); histogram_ranges->set_checksum(checksum);
RangesMap::iterator ranges_it = ranges_->find(checksum); RangesMap::iterator ranges_it = ranges_->find(checksum);
if (ranges_->end() == ranges_it) { if (ranges_->end() == ranges_it) {
// Register the new CachedRanges. // Register the new BucketRanges.
std::list<CachedRanges*>* checksum_matching_list( std::list<BucketRanges*>* checksum_matching_list(
new std::list<CachedRanges*>()); new std::list<BucketRanges*>());
checksum_matching_list->push_front(histogram_ranges); checksum_matching_list->push_front(histogram_ranges);
(*ranges_)[checksum] = checksum_matching_list; (*ranges_)[checksum] = checksum_matching_list;
return; return;
} }
// Use the registered CachedRanges if the registered CachedRanges has same // Use the registered BucketRanges if the registered BucketRanges has same
// ranges_ as |histogram|'s CachedRanges. // ranges_ as |histogram|'s BucketRanges.
std::list<CachedRanges*>* checksum_matching_list = ranges_it->second; std::list<BucketRanges*>* checksum_matching_list = ranges_it->second;
std::list<CachedRanges*>::iterator checksum_matching_list_it; std::list<BucketRanges*>::iterator checksum_matching_list_it;
for (checksum_matching_list_it = checksum_matching_list->begin(); for (checksum_matching_list_it = checksum_matching_list->begin();
checksum_matching_list_it != checksum_matching_list->end(); checksum_matching_list_it != checksum_matching_list->end();
++checksum_matching_list_it) { ++checksum_matching_list_it) {
CachedRanges* existing_histogram_ranges = *checksum_matching_list_it; BucketRanges* existing_histogram_ranges = *checksum_matching_list_it;
DCHECK(existing_histogram_ranges); DCHECK(existing_histogram_ranges);
if (existing_histogram_ranges->Equals(histogram_ranges)) { if (existing_histogram_ranges->Equals(histogram_ranges)) {
histogram->set_cached_ranges(existing_histogram_ranges); histogram->set_bucket_ranges(existing_histogram_ranges);
++number_of_vectors_saved_; ++number_of_vectors_saved_;
saved_ranges_size_ += histogram_ranges->size(); saved_ranges_size_ += histogram_ranges->size();
delete histogram_ranges; delete histogram_ranges;
...@@ -154,8 +154,8 @@ void StatisticsRecorder::RegisterOrDeleteDuplicateRanges(Histogram* histogram) { ...@@ -154,8 +154,8 @@ void StatisticsRecorder::RegisterOrDeleteDuplicateRanges(Histogram* histogram) {
} }
} }
// We haven't found a CachedRanges which has the same ranges. Register the // We haven't found a BucketRanges which has the same ranges. Register the
// new CachedRanges. // new BucketRanges.
DCHECK(checksum_matching_list_it == checksum_matching_list->end()); DCHECK(checksum_matching_list_it == checksum_matching_list->end());
checksum_matching_list->push_front(histogram_ranges); checksum_matching_list->push_front(histogram_ranges);
} }
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
namespace base { namespace base {
class CachedRanges; class BucketRanges;
class Histogram; class Histogram;
class Lock; class Lock;
...@@ -41,12 +41,12 @@ class BASE_EXPORT StatisticsRecorder { ...@@ -41,12 +41,12 @@ class BASE_EXPORT StatisticsRecorder {
// histogram (either the argument, or the pre-existing registered histogram). // histogram (either the argument, or the pre-existing registered histogram).
static Histogram* RegisterOrDeleteDuplicate(Histogram* histogram); static Histogram* RegisterOrDeleteDuplicate(Histogram* histogram);
// Register, or add a new cached_ranges_ of |histogram|. If an identical // Register, or add a new bucket_ranges_ of |histogram|. If an identical
// cached_ranges_ is already registered, then the cached_ranges_ of // bucket_ranges_ is already registered, then the bucket_ranges_ of
// |histogram| is deleted and the |histogram|'s cached_ranges_ is reset to the // |histogram| is deleted and the |histogram|'s bucket_ranges_ is reset to the
// registered cached_ranges_. The cached_ranges_ of |histogram| is always the // registered bucket_ranges_. The bucket_ranges_ of |histogram| is always the
// registered CachedRanges (either the argument's cached_ranges_, or the // registered BucketRanges (either the argument's bucket_ranges_, or the
// pre-existing registered cached_ranges_). // pre-existing registered bucket_ranges_).
static void RegisterOrDeleteDuplicateRanges(Histogram* histogram); static void RegisterOrDeleteDuplicateRanges(Histogram* histogram);
// Method for collecting stats about histograms created in browser and // Method for collecting stats about histograms created in browser and
...@@ -81,10 +81,10 @@ class BASE_EXPORT StatisticsRecorder { ...@@ -81,10 +81,10 @@ class BASE_EXPORT StatisticsRecorder {
// We keep all registered histograms in a map, from name to histogram. // We keep all registered histograms in a map, from name to histogram.
typedef std::map<std::string, Histogram*> HistogramMap; typedef std::map<std::string, Histogram*> HistogramMap;
// We keep all |cached_ranges_| in a map, from checksum to a list of // We keep all |bucket_ranges_| in a map, from checksum to a list of
// |cached_ranges_|. Checksum is calculated from the |ranges_| in // |bucket_ranges_|. Checksum is calculated from the |ranges_| in
// |cached_ranges_|. // |bucket_ranges_|.
typedef std::map<uint32, std::list<CachedRanges*>*> RangesMap; typedef std::map<uint32, std::list<BucketRanges*>*> RangesMap;
friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; friend struct DefaultLazyInstanceTraits<StatisticsRecorder>;
...@@ -93,7 +93,7 @@ class BASE_EXPORT StatisticsRecorder { ...@@ -93,7 +93,7 @@ class BASE_EXPORT StatisticsRecorder {
FRIEND_TEST_ALL_PREFIXES(HistogramTest, RecordedStartupTest); FRIEND_TEST_ALL_PREFIXES(HistogramTest, RecordedStartupTest);
FRIEND_TEST_ALL_PREFIXES(HistogramTest, RangeTest); FRIEND_TEST_ALL_PREFIXES(HistogramTest, RangeTest);
FRIEND_TEST_ALL_PREFIXES(HistogramTest, CustomRangeTest); FRIEND_TEST_ALL_PREFIXES(HistogramTest, CustomRangeTest);
FRIEND_TEST_ALL_PREFIXES(HistogramTest, CachedRangesTest); FRIEND_TEST_ALL_PREFIXES(HistogramTest, BucketRangesTest);
StatisticsRecorder(); StatisticsRecorder();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment