#include "base/metrics/persistent_sample_map.h"
#include "base/atomicops.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/debug/crash_logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
namespace base {
typedef HistogramBase::Count Count;
typedef HistogramBase::Sample Sample;
namespace {
template <typename T, typename I>
class IteratorTemplate : public SampleCountIterator {
public:
explicit IteratorTemplate(T& sample_counts)
: iter_(sample_counts.begin()), end_(sample_counts.end()) {
SkipEmptyBuckets();
}
~IteratorTemplate() override;
bool Done() const override { return iter_ == end_; }
void Next() override {
DCHECK(!Done());
++iter_;
SkipEmptyBuckets();
}
void Get(HistogramBase::Sample* min,
int64_t* max,
HistogramBase::Count* count) override;
private:
void SkipEmptyBuckets() {
while (!Done() && subtle::NoBarrier_Load(iter_->second) == 0) {
++iter_;
}
}
I iter_;
const I end_;
};
typedef std::map<HistogramBase::Sample, HistogramBase::Count*> SampleToCountMap;
typedef IteratorTemplate<const SampleToCountMap,
SampleToCountMap::const_iterator>
PersistentSampleMapIterator;
template <>
PersistentSampleMapIterator::~IteratorTemplate() = default;
template <>
void PersistentSampleMapIterator::Get(Sample* min, int64_t* max, Count* count) {
DCHECK(!Done());
*min = iter_->first;
*max = strict_cast<int64_t>(iter_->first) + 1;
*count = subtle::NoBarrier_Load(iter_->second);
}
typedef IteratorTemplate<SampleToCountMap, SampleToCountMap::iterator>
ExtractingPersistentSampleMapIterator;
template <>
ExtractingPersistentSampleMapIterator::~IteratorTemplate() {
DCHECK(Done());
}
template <>
void ExtractingPersistentSampleMapIterator::Get(Sample* min,
int64_t* max,
Count* count) {
DCHECK(!Done());
*min = iter_->first;
*max = strict_cast<int64_t>(iter_->first) + 1;
*count = subtle::NoBarrier_AtomicExchange(iter_->second, 0);
}
struct SampleRecord {
static constexpr uint32_t kPersistentTypeId = 0x8FE6A69F + 1;
static constexpr size_t kExpectedInstanceSize = 16;
uint64_t id;
Sample value;
Count count;
};
}
PersistentSampleMap::PersistentSampleMap(
uint64_t id,
PersistentHistogramAllocator* allocator,
Metadata* meta)
: HistogramSamples(id, meta), allocator_(allocator) {}
PersistentSampleMap::~PersistentSampleMap() {
if (records_)
records_->Release(this);
}
void PersistentSampleMap::Accumulate(Sample value, Count count) {
subtle::NoBarrier_AtomicIncrement(GetOrCreateSampleCountStorage(value),
count);
IncreaseSumAndCount(strict_cast<int64_t>(count) * value, count);
}
Count PersistentSampleMap::GetCount(Sample value) const {
Count* count_pointer =
const_cast<PersistentSampleMap*>(this)->GetSampleCountStorage(value);
return count_pointer ? subtle::NoBarrier_Load(count_pointer) : 0;
}
Count PersistentSampleMap::TotalCount() const {
const_cast<PersistentSampleMap*>(this)->ImportSamples(-1, true);
Count count = 0;
for (const auto& entry : sample_counts_) {
count += subtle::NoBarrier_Load(entry.second);
}
return count;
}
std::unique_ptr<SampleCountIterator> PersistentSampleMap::Iterator() const {
const_cast<PersistentSampleMap*>(this)->ImportSamples(-1, true);
return std::make_unique<PersistentSampleMapIterator>(sample_counts_);
}
std::unique_ptr<SampleCountIterator> PersistentSampleMap::ExtractingIterator() {
ImportSamples(-1, true);
return std::make_unique<ExtractingPersistentSampleMapIterator>(
sample_counts_);
}
PersistentMemoryAllocator::Reference
PersistentSampleMap::GetNextPersistentRecord(
PersistentMemoryAllocator::Iterator& iterator,
uint64_t* sample_map_id) {
const SampleRecord* record = iterator.GetNextOfObject<SampleRecord>();
if (!record)
return 0;
*sample_map_id = record->id;
return iterator.GetAsReference(record);
}
PersistentMemoryAllocator::Reference
PersistentSampleMap::CreatePersistentRecord(
PersistentMemoryAllocator* allocator,
uint64_t sample_map_id,
Sample value) {
SampleRecord* record = allocator->New<SampleRecord>();
if (!record) {
#if !BUILDFLAG(IS_NACL)
SCOPED_CRASH_KEY_BOOL("PersistentSampleMap", "full", allocator->IsFull());
SCOPED_CRASH_KEY_BOOL("PersistentSampleMap", "corrupted",
allocator->IsCorrupt());
#endif
NOTREACHED() << "full=" << allocator->IsFull()
<< ", corrupt=" << allocator->IsCorrupt();
return 0;
}
record->id = sample_map_id;
record->value = value;
record->count = 0;
PersistentMemoryAllocator::Reference ref = allocator->GetAsReference(record);
allocator->MakeIterable(ref);
return ref;
}
bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter,
Operator op) {
Sample min;
int64_t max;
Count count;
for (; !iter->Done(); iter->Next()) {
iter->Get(&min, &max, &count);
if (count == 0)
continue;
if (strict_cast<int64_t>(min) + 1 != max)
return false;
subtle::Barrier_AtomicIncrement(
GetOrCreateSampleCountStorage(min),
(op == HistogramSamples::ADD) ? count : -count);
}
return true;
}
Count* PersistentSampleMap::GetSampleCountStorage(Sample value) {
auto it = sample_counts_.find(value);
if (it != sample_counts_.end())
return it->second;
return ImportSamples(value, false);
}
Count* PersistentSampleMap::GetOrCreateSampleCountStorage(Sample value) {
Count* count_pointer = GetSampleCountStorage(value);
if (count_pointer)
return count_pointer;
DCHECK(records_);
PersistentMemoryAllocator::Reference ref = records_->CreateNew(value);
if (!ref) {
count_pointer = new Count(0);
sample_counts_[value] = count_pointer;
return count_pointer;
}
count_pointer = ImportSamples(value, false);
DCHECK(count_pointer);
return count_pointer;
}
PersistentSampleMapRecords* PersistentSampleMap::GetRecords() {
if (!records_)
records_ = allocator_->UseSampleMapRecords(id(), this);
return records_;
}
Count* PersistentSampleMap::ImportSamples(Sample until_value,
bool import_everything) {
Count* found_count = nullptr;
PersistentMemoryAllocator::Reference ref;
PersistentSampleMapRecords* records = GetRecords();
while ((ref = records->GetNext()) != 0) {
SampleRecord* record = records->GetAsObject<SampleRecord>(ref);
if (!record)
continue;
DCHECK_EQ(id(), record->id);
if (!Contains(sample_counts_, record->value)) {
sample_counts_[record->value] = &record->count;
} else {
DCHECK_EQ(0, record->count);
}
if (record->value == until_value) {
if (!found_count)
found_count = &record->count;
if (!import_everything)
break;
}
}
return found_count;
}
}