#include "base/metrics/sample_vector.h"
#include <ostream>
#include <string_view>
#include <type_traits>
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/containers/heap_array.h"
#include "base/debug/crash_logging.h"
#include "base/debug/dump_without_crashing.h"
#include "base/debug/leak_annotations.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_span.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/persistent_memory_allocator.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "base/threading/platform_thread.h"
namespace base {
typedef HistogramBase::Count32 Count32;
typedef HistogramBase::Sample32 Sample32;
namespace {
template <bool support_extraction>
class SampleVectorIterator : public SampleCountIterator {
private:
using T = std::conditional_t<support_extraction,
HistogramBase::AtomicCount,
const HistogramBase::AtomicCount>;
public:
SampleVectorIterator(base::span<T> counts, const BucketRanges* bucket_ranges)
: counts_(counts), bucket_ranges_(bucket_ranges) {
SkipEmptyBuckets();
}
~SampleVectorIterator() override {
if constexpr (support_extraction) {
DCHECK(Done());
}
}
bool Done() const override { return index_ >= counts_.size(); }
void Next() override {
DCHECK(!Done());
++index_;
SkipEmptyBuckets();
}
void Get(Sample32* min,
int64_t* max,
HistogramBase::Count32* count) override {
DCHECK(!Done());
*min = bucket_ranges_->range(index_);
*max = strict_cast<int64_t>(bucket_ranges_->range(index_ + 1));
if constexpr (support_extraction) {
*count = subtle::NoBarrier_AtomicExchange(&counts_[index_], 0);
} else {
*count = subtle::NoBarrier_Load(&counts_[index_]);
}
}
bool GetBucketIndex(size_t* index) const override {
DCHECK(!Done());
if (index != nullptr) {
*index = index_;
}
return true;
}
private:
void SkipEmptyBuckets() {
if (Done()) {
return;
}
while (index_ < counts_.size()) {
if (subtle::NoBarrier_Load(&counts_[index_]) != 0) {
return;
}
++index_;
}
}
raw_span<T> counts_;
raw_ptr<const BucketRanges> bucket_ranges_;
size_t index_ = 0;
};
}
SampleVectorBase::SampleVectorBase(uint64_t id,
Metadata* meta,
const BucketRanges* bucket_ranges)
: HistogramSamples(id, meta),
bucket_ranges_(bucket_ranges),
counts_size_(bucket_ranges_->bucket_count()) {
CHECK_GE(counts_size_, 1u);
}
SampleVectorBase::SampleVectorBase(uint64_t id,
std::unique_ptr<Metadata> meta,
const BucketRanges* bucket_ranges)
: HistogramSamples(id, std::move(meta)),
bucket_ranges_(bucket_ranges),
counts_size_(bucket_ranges_->bucket_count()) {
CHECK_GE(counts_size_, 1u);
}
SampleVectorBase::~SampleVectorBase() = default;
void SampleVectorBase::Accumulate(Sample32 value, Count32 count) {
const size_t bucket_index = GetBucketIndex(value);
if (!counts().has_value()) {
if (AccumulateSingleSample(value, count, bucket_index)) {
if (counts().has_value()) {
MoveSingleSampleToCounts();
}
return;
}
MountCountsStorageAndMoveSingleSample();
}
Count32 new_bucket_count =
subtle::NoBarrier_AtomicIncrement(&counts_at(bucket_index), count);
IncreaseSumAndCount(strict_cast<int64_t>(count) * value, count);
Count32 old_bucket_count = new_bucket_count - count;
bool record_negative_sample =
(new_bucket_count >= 0) != (old_bucket_count >= 0) && count > 0;
if (record_negative_sample) [[unlikely]] {
RecordNegativeSample(SAMPLES_ACCUMULATE_OVERFLOW, count);
}
}
Count32 SampleVectorBase::GetCount(Sample32 value) const {
return GetCountAtIndex(GetBucketIndex(value));
}
Count32 SampleVectorBase::TotalCount() const {
SingleSample sample = single_sample().Load();
if (sample.count != 0) {
return sample.count;
}
if (counts().has_value() || MountExistingCountsStorage()) {
Count32 count = 0;
span<const HistogramBase::AtomicCount> counts_span = counts().value();
for (const HistogramBase::AtomicCount& c : counts_span) {
count += subtle::NoBarrier_Load(&c);
}
return count;
}
return 0;
}
Count32 SampleVectorBase::GetCountAtIndex(size_t bucket_index) const {
DCHECK(bucket_index < counts_size());
SingleSample sample = single_sample().Load();
if (sample.count != 0) {
return sample.bucket == bucket_index ? sample.count : 0;
}
if (counts().has_value() || MountExistingCountsStorage()) {
return subtle::NoBarrier_Load(&counts_at(bucket_index));
}
return 0;
}
std::unique_ptr<SampleCountIterator> SampleVectorBase::Iterator() const {
SingleSample sample = single_sample().Load();
if (sample.count != 0) {
static_assert(std::is_unsigned<decltype(SingleSample::bucket)>::value);
if (sample.bucket >= bucket_ranges_->bucket_count()) {
return std::make_unique<SampleVectorIterator<false>>(
base::span<const HistogramBase::AtomicCount>(), bucket_ranges_);
}
return std::make_unique<SingleSampleIterator>(
bucket_ranges_->range(sample.bucket),
bucket_ranges_->range(sample.bucket + 1), sample.count, sample.bucket,
false);
}
if (counts().has_value() || MountExistingCountsStorage()) {
return std::make_unique<SampleVectorIterator<false>>(*counts(),
bucket_ranges_);
}
return std::make_unique<SampleVectorIterator<false>>(
base::span<const HistogramBase::AtomicCount>(), bucket_ranges_);
}
std::unique_ptr<SampleCountIterator> SampleVectorBase::ExtractingIterator() {
SingleSample sample = single_sample().Extract();
if (sample.count != 0) {
static_assert(std::is_unsigned<decltype(SingleSample::bucket)>::value);
if (sample.bucket >= bucket_ranges_->bucket_count()) {
return std::make_unique<SampleVectorIterator<true>>(
base::span<HistogramBase::AtomicCount>(), bucket_ranges_);
}
return std::make_unique<SingleSampleIterator>(
bucket_ranges_->range(sample.bucket),
bucket_ranges_->range(sample.bucket + 1), sample.count, sample.bucket,
true);
}
if (counts().has_value() || MountExistingCountsStorage()) {
return std::make_unique<SampleVectorIterator<true>>(*counts(),
bucket_ranges_);
}
return std::make_unique<SampleVectorIterator<true>>(
base::span<HistogramBase::AtomicCount>(), bucket_ranges_);
}
bool SampleVectorBase::AddSubtractImpl(SampleCountIterator* iter,
HistogramSamples::Operator op) {
if (iter->Done()) {
return true;
}
HistogramBase::Count32 count;
size_t dest_index = GetDestinationBucketIndexAndCount(*iter, &count);
if (dest_index == SIZE_MAX) {
return false;
}
iter->Next();
if (!counts().has_value()) {
if (iter->Done()) {
if (single_sample().Accumulate(
dest_index, op == HistogramSamples::ADD ? count : -count)) {
if (counts().has_value()) {
MoveSingleSampleToCounts();
}
return true;
}
}
MountCountsStorageAndMoveSingleSample();
}
while (true) {
subtle::NoBarrier_AtomicIncrement(
&counts_at(dest_index), op == HistogramSamples::ADD ? count : -count);
if (iter->Done()) {
return true;
}
dest_index = GetDestinationBucketIndexAndCount(*iter, &count);
if (dest_index == SIZE_MAX) {
return false;
}
iter->Next();
}
}
size_t SampleVectorBase::GetDestinationBucketIndexAndCount(
SampleCountIterator& iter,
HistogramBase::Count32* count) {
Sample32 min;
int64_t max;
iter.Get(&min, &max, count);
size_t bucket_index;
if (!iter.GetBucketIndex(&bucket_index)) {
bucket_index = GetBucketIndex(min);
}
if (bucket_index >= counts_size() ||
min != bucket_ranges_->range(bucket_index) ||
max != bucket_ranges_->range(bucket_index + 1)) {
return SIZE_MAX;
}
return bucket_index;
}
size_t SampleVectorBase::GetBucketIndex(Sample32 value) const {
size_t bucket_count = bucket_ranges_->bucket_count();
CHECK_GE(value, bucket_ranges_->range(0));
CHECK_LT(value, bucket_ranges_->range(bucket_count));
Sample32 maximum = bucket_ranges_->range(bucket_count - 1);
if (maximum == static_cast<Sample32>(bucket_count - 1)) {
if (value < 1) {
return 0;
}
if (value > maximum) {
return bucket_count - 1;
}
return static_cast<size_t>(value);
}
size_t under = 0;
size_t over = bucket_count;
size_t mid;
do {
DCHECK_GE(over, under);
mid = under + (over - under) / 2;
if (mid == under) {
break;
}
if (bucket_ranges_->range(mid) <= value) {
under = mid;
} else {
over = mid;
}
} while (true);
DCHECK_LE(bucket_ranges_->range(mid), value);
CHECK_GT(bucket_ranges_->range(mid + 1), value);
return mid;
}
void SampleVectorBase::MoveSingleSampleToCounts() {
DCHECK(counts().has_value());
SingleSample sample = single_sample().ExtractAndDisable();
if (sample.count == 0) {
return;
}
if (sample.bucket >= counts_size()) {
return;
}
subtle::NoBarrier_AtomicIncrement(&counts_at(sample.bucket), sample.count);
}
void SampleVectorBase::MountCountsStorageAndMoveSingleSample() {
if (counts_data_.load(std::memory_order_relaxed) == nullptr) {
static base::NoDestructor<Lock> counts_lock;
AutoLock lock(*counts_lock);
if (counts_data_.load(std::memory_order_relaxed) == nullptr) {
span<HistogramBase::Count32> counts = CreateCountsStorageWhileLocked();
set_counts(counts);
}
}
MoveSingleSampleToCounts();
}
SampleVector::SampleVector(const BucketRanges* bucket_ranges)
: SampleVector(0, bucket_ranges) {}
SampleVector::SampleVector(uint64_t id, const BucketRanges* bucket_ranges)
: SampleVectorBase(id, std::make_unique<LocalMetadata>(), bucket_ranges) {}
SampleVector::~SampleVector() = default;
bool SampleVector::IsDefinitelyEmpty() const {
AtomicSingleSample sample = single_sample();
return HistogramSamples::IsDefinitelyEmpty() && !sample.IsDisabled() &&
sample.Load().count == 0;
}
bool SampleVector::MountExistingCountsStorage() const {
return counts().has_value();
}
std::string SampleVector::GetAsciiHeader(std::string_view histogram_name,
int32_t flags) const {
Count32 sample_count = TotalCount();
std::string output;
StrAppend(&output, {"Histogram: ", histogram_name, " recorded ",
NumberToString(sample_count), " samples"});
if (sample_count == 0) {
DCHECK_EQ(sum(), 0);
} else {
double mean = static_cast<float>(sum()) / sample_count;
StringAppendF(&output, ", mean = %.1f", mean);
}
if (flags) {
StringAppendF(&output, " (flags = 0x%x)", flags);
}
return output;
}
std::string SampleVector::GetAsciiBody() const {
Count32 sample_count = TotalCount();
double max_size = 0;
double scaling_factor = 1;
max_size = GetPeakBucketSize();
const double kLineLength = 72;
if (max_size > kLineLength) {
scaling_factor = kLineLength / max_size;
}
size_t print_width = 1;
for (uint32_t i = 0; i < bucket_count(); ++i) {
if (GetCountAtIndex(i)) {
size_t width =
GetSimpleAsciiBucketRange(bucket_ranges()->range(i)).size() + 1;
if (width > print_width) {
print_width = width;
}
}
}
int64_t remaining = sample_count;
int64_t past = 0;
std::string output;
for (uint32_t i = 0; i < bucket_count(); ++i) {
Count32 current = GetCountAtIndex(i);
remaining -= current;
std::string range = GetSimpleAsciiBucketRange(bucket_ranges()->range(i));
output.append(range);
for (size_t j = 0; range.size() + j < print_width + 1; ++j) {
output.push_back(' ');
}
if (0 == current && i < bucket_count() - 1 && 0 == GetCountAtIndex(i + 1)) {
while (i < bucket_count() - 1 && 0 == GetCountAtIndex(i + 1)) {
++i;
}
output.append("... \n");
continue;
}
Count32 current_size = round(current * scaling_factor);
WriteAsciiBucketGraph(current_size, kLineLength, &output);
WriteAsciiBucketContext(past, current, remaining, i, &output);
output.append("\n");
past += current;
}
DCHECK_EQ(sample_count, past);
return output;
}
double SampleVector::GetPeakBucketSize() const {
Count32 max = 0;
for (uint32_t i = 0; i < bucket_count(); ++i) {
Count32 current = GetCountAtIndex(i);
if (current > max) {
max = current;
}
}
return max;
}
void SampleVector::WriteAsciiBucketContext(int64_t past,
Count32 current,
int64_t remaining,
uint32_t current_bucket_index,
std::string* output) const {
double scaled_sum = (past + current + remaining) / 100.0;
WriteAsciiBucketValue(current, scaled_sum, output);
if (0 < current_bucket_index) {
double percentage = past / scaled_sum;
StringAppendF(output, " {%3.1f%%}", percentage);
}
}
span<HistogramBase::AtomicCount>
SampleVector::CreateCountsStorageWhileLocked() {
local_counts_.resize(counts_size());
return local_counts_;
}
PersistentSampleVector::PersistentSampleVector(
std::string_view name,
uint64_t id,
const BucketRanges* bucket_ranges,
Metadata* meta,
const DelayedPersistentAllocation& counts)
: SampleVectorBase(id, meta, bucket_ranges), persistent_counts_(counts) {
if (single_sample().IsDisabled()) {
const auto result = MountExistingCountsStorageImpl();
RecordMountExistingCountsStorageResult(result);
if (result != MountExistingCountsStorageResult::kSucceeded &&
result != MountExistingCountsStorageResult::kNothingToRead) {
SCOPED_CRASH_KEY_STRING64("PSV", "name", name);
SCOPED_CRASH_KEY_NUMBER("PSV", "counts_ref",
persistent_counts_.reference());
debug::DumpWithoutCrashing();
}
}
}
PersistentSampleVector::~PersistentSampleVector() = default;
bool PersistentSampleVector::IsDefinitelyEmpty() const {
NOTREACHED();
}
constinit std::atomic_uintptr_t
PersistentSampleVector::atomic_histogram_pointer{0};
void PersistentSampleVector::ResetMountExistingCountsStorageResultForTesting() {
atomic_histogram_pointer.store(0, std::memory_order_release);
}
void PersistentSampleVector::RecordMountExistingCountsStorageResult(
MountExistingCountsStorageResult result) {
static constexpr auto boundary = static_cast<base::HistogramBase::Sample32>(
MountExistingCountsStorageResult::kMaxValue);
HISTOGRAM_POINTER_USE(
std::addressof(atomic_histogram_pointer),
kMountExistingCountsStorageResult,
Add(static_cast<base::HistogramBase::Sample32>(result)),
base::LinearHistogram::FactoryGet(
kMountExistingCountsStorageResult, 1, boundary, boundary + 1,
base::HistogramBase::kUmaTargetedHistogramFlag));
}
PersistentSampleVector::MountExistingCountsStorageResult
PersistentSampleVector::MountExistingCountsStorageImpl() const {
if (!persistent_counts_.reference()) {
return MountExistingCountsStorageResult::kNothingToRead;
}
span<HistogramBase::AtomicCount> mem =
persistent_counts_.Get<HistogramBase::AtomicCount>();
if (mem.empty()) {
return MountExistingCountsStorageResult::kCorrupt;
}
set_counts(mem.first(counts_size()));
return MountExistingCountsStorageResult::kSucceeded;
}
bool PersistentSampleVector::MountExistingCountsStorage() const {
return MountExistingCountsStorageImpl() ==
MountExistingCountsStorageResult::kSucceeded;
}
span<HistogramBase::AtomicCount>
PersistentSampleVector::CreateCountsStorageWhileLocked() {
span<HistogramBase::AtomicCount> mem =
persistent_counts_.Get<HistogramBase::AtomicCount>();
if (mem.empty()) {
auto array = HeapArray<HistogramBase::AtomicCount>::WithSize(counts_size());
ANNOTATE_LEAKING_OBJECT_PTR(array.data());
return std::move(array).leak();
}
return mem.first(counts_size());
}
}