#include "base/metrics/persistent_sample_map.h"
#include <stddef.h>
#include <stdint.h>
#include <atomic>
#include <type_traits>
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/debug/crash_logging.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/metrics/sample_map_iterator.h"
#include "base/notreached.h"
#include "build/buildflag.h"
namespace base {
using Count32 = HistogramBase::Count32;
using Sample32 = HistogramBase::Sample32;
namespace {
struct SampleRecord {
static constexpr uint32_t kPersistentTypeId = 0x8FE6A69F + 1;
static constexpr size_t kExpectedInstanceSize = 16;
uint64_t id;
Sample32 value;
std::atomic<Count32> count;
static_assert(std::atomic<Count32>::is_always_lock_free);
static_assert(std::is_standard_layout_v<std::atomic<Count32>>);
static_assert(sizeof(std::atomic<Count32>) == sizeof(Count32));
static_assert(alignof(std::atomic<Count32>) == alignof(Count32));
};
}
PersistentSampleMap::PersistentSampleMap(
uint64_t id,
PersistentHistogramAllocator* allocator,
Metadata* meta)
: HistogramSamples(id, meta), allocator_(allocator) {}
PersistentSampleMap::~PersistentSampleMap() = default;
void PersistentSampleMap::Accumulate(Sample32 value, Count32 count) {
GetOrCreateSampleCountStorage(value)->fetch_add(count,
std::memory_order_relaxed);
IncreaseSumAndCount(int64_t{count} * value, count);
}
Count32 PersistentSampleMap::GetCount(Sample32 value) const {
const std::atomic<Count32>* const count_pointer =
GetSampleCountStorage(value);
return count_pointer ? count_pointer->load(std::memory_order_relaxed) : 0;
}
Count32 PersistentSampleMap::TotalCount() const {
ImportSamples();
Count32 count = 0;
for (const auto& entry : sample_counts_) {
count += entry.second->load(std::memory_order_relaxed);
}
return count;
}
std::unique_ptr<SampleCountIterator> PersistentSampleMap::Iterator() const {
ImportSamples();
return std::make_unique<SampleMapIterator<SampleToCountMap, false>>(
sample_counts_);
}
std::unique_ptr<SampleCountIterator> PersistentSampleMap::ExtractingIterator() {
ImportSamples();
return std::make_unique<SampleMapIterator<SampleToCountMap, true>>(
sample_counts_);
}
bool PersistentSampleMap::IsDefinitelyEmpty() const {
NOTREACHED();
}
PersistentMemoryAllocator::Reference
PersistentSampleMap::GetNextPersistentRecord(
PersistentMemoryAllocator::Iterator& iterator,
uint64_t* sample_map_id,
Sample32* value) {
const SampleRecord* record = iterator.GetNextOfObject<SampleRecord>();
if (!record) {
return 0;
}
*sample_map_id = record->id;
*value = record->value;
return iterator.GetAsReference(record);
}
PersistentMemoryAllocator::Reference
PersistentSampleMap::CreatePersistentRecord(
PersistentMemoryAllocator* allocator,
uint64_t sample_map_id,
Sample32 value) {
SampleRecord* record = allocator->New<SampleRecord>();
if (record) {
record->id = sample_map_id;
record->value = value;
record->count = 0;
PersistentMemoryAllocator::Reference ref =
allocator->GetAsReference(record);
allocator->MakeIterable(ref);
return ref;
}
if (!allocator->IsFull()) {
const bool corrupt = allocator->IsCorrupt();
SCOPED_CRASH_KEY_BOOL("PersistentSampleMap", "corrupted", corrupt);
DUMP_WILL_BE_NOTREACHED() << "corrupt=" << corrupt;
}
return 0;
}
bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter,
Operator op) {
Sample32 min;
int64_t max;
Count32 count;
for (; !iter->Done(); iter->Next()) {
iter->Get(&min, &max, &count);
if (count == 0) {
continue;
}
if (int64_t{min} + 1 != max) {
return false;
}
GetOrCreateSampleCountStorage(min)->fetch_add(
(op == HistogramSamples::ADD) ? count : -count,
std::memory_order_seq_cst);
}
return true;
}
std::atomic<Count32>* PersistentSampleMap::GetSampleCountStorage(
Sample32 value) const {
const auto it = sample_counts_.find(value);
return (it == sample_counts_.end()) ? ImportSamples(value) : it->second.get();
}
std::atomic<Count32>* PersistentSampleMap::GetOrCreateSampleCountStorage(
Sample32 value) {
std::atomic<Count32>* count_pointer = GetSampleCountStorage(value);
if (count_pointer) {
return count_pointer;
}
CHECK(records_);
PersistentMemoryAllocator::Reference ref = records_->CreateNew(value);
if (!ref) {
count_pointer = new std::atomic<Count32>(0);
sample_counts_[value] = count_pointer;
return count_pointer;
}
count_pointer = ImportSamples(value);
DCHECK(count_pointer);
return count_pointer;
}
PersistentSampleMapRecords* PersistentSampleMap::GetRecords() const {
if (!records_) {
records_ = allocator_->CreateSampleMapRecords(id());
}
return records_.get();
}
std::atomic<Count32>* PersistentSampleMap::ImportSamples(
std::optional<Sample32> until_value) const {
std::vector<PersistentMemoryAllocator::Reference> refs;
PersistentSampleMapRecords* records = GetRecords();
while (!(refs = records->GetNextRecords(until_value)).empty()) {
for (auto ref : refs) {
SampleRecord* const record = records->GetAsObject<SampleRecord>(ref);
if (!record) {
continue;
}
DCHECK_EQ(id(), record->id);
const auto ret = sample_counts_.insert({record->value, &record->count});
if (!ret.second) {
DCHECK_EQ(0, record->count);
}
if (until_value.has_value() && record->value == until_value.value()) {
CHECK_EQ(refs.back(), ref);
return &record->count;
}
}
}
return nullptr;
}
}