This source file includes following definitions.
- FactoryGet
- GetHistogramType
- HasConstructionArguments
- Add
- SnapshotSamples
- AddSamples
- AddSamplesFromPickle
- WriteHTMLGraph
- WriteAscii
- SerializeInfoImpl
- DeserializeInfoImpl
- GetParameters
- GetCountAndBucketData
- WriteAsciiImpl
- WriteAsciiHeader
#include "base/metrics/sparse_histogram.h"
#include "base/metrics/sample_map.h"
#include "base/metrics/statistics_recorder.h"
#include "base/pickle.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
using std::map;
using std::string;
namespace base {
typedef HistogramBase::Count Count;
typedef HistogramBase::Sample Sample;
HistogramBase* SparseHistogram::FactoryGet(const string& name, int32 flags) {
HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
if (!histogram) {
HistogramBase* tentative_histogram = new SparseHistogram(name);
tentative_histogram->SetFlags(flags);
histogram =
StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
}
DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType());
return histogram;
}
SparseHistogram::~SparseHistogram() {}
HistogramType SparseHistogram::GetHistogramType() const {
return SPARSE_HISTOGRAM;
}
bool SparseHistogram::HasConstructionArguments(
Sample expected_minimum,
Sample expected_maximum,
size_t expected_bucket_count) const {
return false;
}
void SparseHistogram::Add(Sample value) {
base::AutoLock auto_lock(lock_);
samples_.Accumulate(value, 1);
}
scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const {
scoped_ptr<SampleMap> snapshot(new SampleMap());
base::AutoLock auto_lock(lock_);
snapshot->Add(samples_);
return snapshot.PassAs<HistogramSamples>();
}
void SparseHistogram::AddSamples(const HistogramSamples& samples) {
base::AutoLock auto_lock(lock_);
samples_.Add(samples);
}
bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) {
base::AutoLock auto_lock(lock_);
return samples_.AddFromPickle(iter);
}
void SparseHistogram::WriteHTMLGraph(string* output) const {
output->append("<PRE>");
WriteAsciiImpl(true, "<br>", output);
output->append("</PRE>");
}
void SparseHistogram::WriteAscii(string* output) const {
WriteAsciiImpl(true, "\n", output);
}
bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const {
return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags());
}
SparseHistogram::SparseHistogram(const string& name)
: HistogramBase(name) {}
HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) {
string histogram_name;
int flags;
if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) {
DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name;
return NULL;
}
DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag);
flags &= ~HistogramBase::kIPCSerializationSourceFlag;
return SparseHistogram::FactoryGet(histogram_name, flags);
}
void SparseHistogram::GetParameters(DictionaryValue* params) const {
}
void SparseHistogram::GetCountAndBucketData(Count* count,
int64* sum,
ListValue* buckets) const {
}
void SparseHistogram::WriteAsciiImpl(bool graph_it,
const std::string& newline,
std::string* output) const {
scoped_ptr<HistogramSamples> snapshot = SnapshotSamples();
Count total_count = snapshot->TotalCount();
double scaled_total_count = total_count / 100.0;
WriteAsciiHeader(total_count, output);
output->append(newline);
Count largest_count = 0;
Sample largest_sample = 0;
scoped_ptr<SampleCountIterator> it = snapshot->Iterator();
while (!it->Done())
{
Sample min;
Sample max;
Count count;
it->Get(&min, &max, &count);
if (min > largest_sample)
largest_sample = min;
if (count > largest_count)
largest_count = count;
it->Next();
}
size_t print_width = GetSimpleAsciiBucketRange(largest_sample).size() + 1;
it = snapshot->Iterator();
while (!it->Done())
{
Sample min;
Sample max;
Count count;
it->Get(&min, &max, &count);
string range = GetSimpleAsciiBucketRange(min);
output->append(range);
for (size_t j = 0; range.size() + j < print_width + 1; ++j)
output->push_back(' ');
if (graph_it)
WriteAsciiBucketGraph(count, largest_count, output);
WriteAsciiBucketValue(count, scaled_total_count, output);
output->append(newline);
it->Next();
}
}
void SparseHistogram::WriteAsciiHeader(const Count total_count,
std::string* output) const {
StringAppendF(output,
"Histogram: %s recorded %d samples",
histogram_name().c_str(),
total_count);
if (flags() & ~kHexRangePrintingFlag)
StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
}
}