This source file includes following definitions.
- PrepareDeltas
- PrepareDelta
- InspectLoggedSamplesInconsistency
#include "base/metrics/histogram_snapshot_manager.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram_flattener.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/stl_util.h"
using std::map;
using std::string;
namespace base {
HistogramSnapshotManager::HistogramSnapshotManager(
HistogramFlattener* histogram_flattener)
: histogram_flattener_(histogram_flattener) {
DCHECK(histogram_flattener_);
}
HistogramSnapshotManager::~HistogramSnapshotManager() {
STLDeleteValues(&logged_samples_);
}
void HistogramSnapshotManager::PrepareDeltas(
HistogramBase::Flags flag_to_set,
HistogramBase::Flags required_flags) {
StatisticsRecorder::Histograms histograms;
StatisticsRecorder::GetHistograms(&histograms);
for (StatisticsRecorder::Histograms::const_iterator it = histograms.begin();
histograms.end() != it;
++it) {
(*it)->SetFlags(flag_to_set);
if (((*it)->flags() & required_flags) == required_flags)
PrepareDelta(**it);
}
}
void HistogramSnapshotManager::PrepareDelta(const HistogramBase& histogram) {
DCHECK(histogram_flattener_);
scoped_ptr<HistogramSamples> snapshot(histogram.SnapshotSamples());
const std::string& histogram_name = histogram.histogram_name();
int corruption = histogram.FindCorruption(*snapshot);
if (HistogramBase::BUCKET_ORDER_ERROR & corruption) {
CHECK_NE(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption);
CHECK(false);
}
CHECK_EQ(0, HistogramBase::RANGE_CHECKSUM_ERROR & corruption);
if (corruption) {
DLOG(ERROR) << "Histogram: " << histogram_name
<< " has data corruption: " << corruption;
histogram_flattener_->InconsistencyDetected(
static_cast<HistogramBase::Inconsistency>(corruption));
int old_corruption = inconsistencies_[histogram_name];
if (old_corruption == (corruption | old_corruption))
return;
inconsistencies_[histogram_name] |= corruption;
histogram_flattener_->UniqueInconsistencyDetected(
static_cast<HistogramBase::Inconsistency>(corruption));
return;
}
HistogramSamples* to_log;
map<string, HistogramSamples*>::iterator it =
logged_samples_.find(histogram_name);
if (it == logged_samples_.end()) {
to_log = snapshot.release();
logged_samples_[histogram_name] = to_log;
} else {
HistogramSamples* already_logged = it->second;
InspectLoggedSamplesInconsistency(*snapshot, already_logged);
snapshot->Subtract(*already_logged);
already_logged->Add(*snapshot);
to_log = snapshot.get();
}
if (to_log->TotalCount() > 0)
histogram_flattener_->RecordDelta(histogram, *to_log);
}
void HistogramSnapshotManager::InspectLoggedSamplesInconsistency(
const HistogramSamples& new_snapshot,
HistogramSamples* logged_samples) {
HistogramBase::Count discrepancy =
logged_samples->TotalCount() - logged_samples->redundant_count();
if (!discrepancy)
return;
histogram_flattener_->InconsistencyDetectedInLoggedCount(discrepancy);
if (discrepancy > Histogram::kCommonRaceBasedCountMismatch) {
logged_samples->Subtract(*logged_samples);
logged_samples->Add(new_snapshot);
}
}
}