chromium/base/metrics/histogram.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Histogram is an object that aggregates statistics, and can summarize them in
// various forms, including ASCII graphical, HTML, and numerically (as a
// vector of numbers corresponding to each of the aggregating buckets).
// See header file for details and examples.

#include "base/metrics/histogram.h"

#include <inttypes.h>
#include <limits.h>
#include <math.h>

#include <algorithm>
#include <atomic>
#include <memory>
#include <string>
#include <string_view>
#include <utility>

#include "base/compiler_specific.h"
#include "base/debug/alias.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/metrics/dummy_histogram.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/metrics_hashes.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/metrics/persistent_memory_allocator.h"
#include "base/metrics/sample_vector.h"
#include "base/metrics/statistics_recorder.h"
#include "base/notreached.h"
#include "base/pickle.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/values.h"
#include "build/build_config.h"

namespace base {

namespace {

bool ReadHistogramArguments(PickleIterator* iter,
                            std::string* histogram_name,
                            int* flags,
                            int* declared_min,
                            int* declared_max,
                            size_t* bucket_count,
                            uint32_t* range_checksum) {}

bool ValidateRangeChecksum(const HistogramBase& histogram,
                           uint32_t range_checksum) {}

}  // namespace

Count;
Sample;

class Histogram::Factory {};

HistogramBase* Histogram::Factory::Build() {}

HistogramBase* Histogram::FactoryGet(std::string_view name,
                                     Sample minimum,
                                     Sample maximum,
                                     size_t bucket_count,
                                     int32_t flags) {}

HistogramBase* Histogram::FactoryTimeGet(std::string_view name,
                                         TimeDelta minimum,
                                         TimeDelta maximum,
                                         size_t bucket_count,
                                         int32_t flags) {}

HistogramBase* Histogram::FactoryMicrosecondsTimeGet(std::string_view name,
                                                     TimeDelta minimum,
                                                     TimeDelta maximum,
                                                     size_t bucket_count,
                                                     int32_t flags) {}

HistogramBase* Histogram::FactoryGet(const std::string& name,
                                     Sample minimum,
                                     Sample maximum,
                                     size_t bucket_count,
                                     int32_t flags) {}

HistogramBase* Histogram::FactoryTimeGet(const std::string& name,
                                         TimeDelta minimum,
                                         TimeDelta maximum,
                                         size_t bucket_count,
                                         int32_t flags) {}

HistogramBase* Histogram::FactoryMicrosecondsTimeGet(const std::string& name,
                                                     TimeDelta minimum,
                                                     TimeDelta maximum,
                                                     size_t bucket_count,
                                                     int32_t flags) {}

HistogramBase* Histogram::FactoryGet(const char* name,
                                     Sample minimum,
                                     Sample maximum,
                                     size_t bucket_count,
                                     int32_t flags) {}

HistogramBase* Histogram::FactoryTimeGet(const char* name,
                                         TimeDelta minimum,
                                         TimeDelta maximum,
                                         size_t bucket_count,
                                         int32_t flags) {}

HistogramBase* Histogram::FactoryMicrosecondsTimeGet(const char* name,
                                                     TimeDelta minimum,
                                                     TimeDelta maximum,
                                                     size_t bucket_count,
                                                     int32_t flags) {}

std::unique_ptr<HistogramBase> Histogram::PersistentCreate(
    const char* name,
    const BucketRanges* ranges,
    const DelayedPersistentAllocation& counts,
    const DelayedPersistentAllocation& logged_counts,
    HistogramSamples::Metadata* meta,
    HistogramSamples::Metadata* logged_meta) {}

// Calculate what range of values are held in each bucket.
// We have to be careful that we don't pick a ratio between starting points in
// consecutive buckets that is sooo small, that the integer bounds are the same
// (effectively making one bucket get no values).  We need to avoid:
//   ranges(i) == ranges(i + 1)
// To avoid that, we just do a fine-grained bucket width as far as we need to
// until we get a ratio that moves us along at least 2 units at a time.  From
// that bucket onward we do use the exponential growth of buckets.
//
// static
void Histogram::InitializeBucketRanges(Sample minimum,
                                       Sample maximum,
                                       BucketRanges* ranges) {}

// static
const int Histogram::kCommonRaceBasedCountMismatch =;

uint32_t Histogram::FindCorruption(const HistogramSamples& samples) const {}

const BucketRanges* Histogram::bucket_ranges() const {}

Sample Histogram::declared_min() const {}

Sample Histogram::declared_max() const {}

Sample Histogram::ranges(size_t i) const {}

size_t Histogram::bucket_count() const {}

// static
bool Histogram::InspectConstructionArguments(std::string_view name,
                                             Sample* minimum,
                                             Sample* maximum,
                                             size_t* bucket_count) {}

uint64_t Histogram::name_hash() const {}

HistogramType Histogram::GetHistogramType() const {}

bool Histogram::HasConstructionArguments(Sample expected_minimum,
                                         Sample expected_maximum,
                                         size_t expected_bucket_count) const {}

void Histogram::Add(int value) {}

void Histogram::AddCount(int value, int count) {}

std::unique_ptr<HistogramSamples> Histogram::SnapshotSamples() const {}

std::unique_ptr<HistogramSamples> Histogram::SnapshotUnloggedSamples() const {}

void Histogram::MarkSamplesAsLogged(const HistogramSamples& samples) {}

std::unique_ptr<HistogramSamples> Histogram::SnapshotDelta() {}

std::unique_ptr<HistogramSamples> Histogram::SnapshotFinalDelta() const {}

void Histogram::AddSamples(const HistogramSamples& samples) {}

bool Histogram::AddSamplesFromPickle(PickleIterator* iter) {}

base::Value::Dict Histogram::ToGraphDict() const {}

void Histogram::SerializeInfoImpl(Pickle* pickle) const {}

Histogram::Histogram(const char* name, const BucketRanges* ranges)
    :{}

Histogram::Histogram(const char* name,
                     const BucketRanges* ranges,
                     const DelayedPersistentAllocation& counts,
                     const DelayedPersistentAllocation& logged_counts,
                     HistogramSamples::Metadata* meta,
                     HistogramSamples::Metadata* logged_meta)
    :{}

Histogram::~Histogram() = default;

const std::string Histogram::GetAsciiBucketRange(size_t i) const {}

//------------------------------------------------------------------------------
// Private methods

// static
HistogramBase* Histogram::DeserializeInfoImpl(PickleIterator* iter) {}

// static
HistogramBase* Histogram::FactoryGetInternal(std::string_view name,
                                             Sample minimum,
                                             Sample maximum,
                                             size_t bucket_count,
                                             int32_t flags) {}

// static
HistogramBase* Histogram::FactoryTimeGetInternal(std::string_view name,
                                                 TimeDelta minimum,
                                                 TimeDelta maximum,
                                                 size_t bucket_count,
                                                 int32_t flags) {}

// static
HistogramBase* Histogram::FactoryMicrosecondsTimeGetInternal(
    std::string_view name,
    TimeDelta minimum,
    TimeDelta maximum,
    size_t bucket_count,
    int32_t flags) {}

std::unique_ptr<SampleVector> Histogram::SnapshotAllSamples() const {}

std::unique_ptr<SampleVector> Histogram::SnapshotUnloggedSamplesImpl() const {}

Value::Dict Histogram::GetParameters() const {}

//------------------------------------------------------------------------------
// LinearHistogram: This histogram uses a traditional set of evenly spaced
// buckets.
//------------------------------------------------------------------------------

class LinearHistogram::Factory : public Histogram::Factory {};

LinearHistogram::~LinearHistogram() = default;

HistogramBase* LinearHistogram::FactoryGet(std::string_view name,
                                           Sample minimum,
                                           Sample maximum,
                                           size_t bucket_count,
                                           int32_t flags) {}

HistogramBase* LinearHistogram::FactoryTimeGet(std::string_view name,
                                               TimeDelta minimum,
                                               TimeDelta maximum,
                                               size_t bucket_count,
                                               int32_t flags) {}

HistogramBase* LinearHistogram::FactoryGet(const std::string& name,
                                           Sample minimum,
                                           Sample maximum,
                                           size_t bucket_count,
                                           int32_t flags) {}

HistogramBase* LinearHistogram::FactoryTimeGet(const std::string& name,
                                               TimeDelta minimum,
                                               TimeDelta maximum,
                                               size_t bucket_count,
                                               int32_t flags) {}

HistogramBase* LinearHistogram::FactoryGet(const char* name,
                                           Sample minimum,
                                           Sample maximum,
                                           size_t bucket_count,
                                           int32_t flags) {}

HistogramBase* LinearHistogram::FactoryTimeGet(const char* name,
                                               TimeDelta minimum,
                                               TimeDelta maximum,
                                               size_t bucket_count,
                                               int32_t flags) {}

std::unique_ptr<HistogramBase> LinearHistogram::PersistentCreate(
    const char* name,
    const BucketRanges* ranges,
    const DelayedPersistentAllocation& counts,
    const DelayedPersistentAllocation& logged_counts,
    HistogramSamples::Metadata* meta,
    HistogramSamples::Metadata* logged_meta) {}

HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
    std::string_view name,
    Sample minimum,
    Sample maximum,
    size_t bucket_count,
    int32_t flags,
    const DescriptionPair descriptions[]) {}

HistogramType LinearHistogram::GetHistogramType() const {}

LinearHistogram::LinearHistogram(const char* name, const BucketRanges* ranges)
    :{}

LinearHistogram::LinearHistogram(
    const char* name,
    const BucketRanges* ranges,
    const DelayedPersistentAllocation& counts,
    const DelayedPersistentAllocation& logged_counts,
    HistogramSamples::Metadata* meta,
    HistogramSamples::Metadata* logged_meta)
    :{}

const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const {}

// static
void LinearHistogram::InitializeBucketRanges(Sample minimum,
                                             Sample maximum,
                                             BucketRanges* ranges) {}

// static
HistogramBase* LinearHistogram::FactoryGetInternal(std::string_view name,
                                                   Sample minimum,
                                                   Sample maximum,
                                                   size_t bucket_count,
                                                   int32_t flags) {}

// static
HistogramBase* LinearHistogram::FactoryTimeGetInternal(std::string_view name,
                                                       TimeDelta minimum,
                                                       TimeDelta maximum,
                                                       size_t bucket_count,
                                                       int32_t flags) {}

// static
HistogramBase* LinearHistogram::DeserializeInfoImpl(PickleIterator* iter) {}

//------------------------------------------------------------------------------
// ScaledLinearHistogram: This is a wrapper around a LinearHistogram that
// scales input counts.
//------------------------------------------------------------------------------

ScaledLinearHistogram::ScaledLinearHistogram(std::string_view name,
                                             Sample minimum,
                                             Sample maximum,
                                             size_t bucket_count,
                                             int32_t scale,
                                             int32_t flags)
    :{}

ScaledLinearHistogram::ScaledLinearHistogram(const std::string& name,
                                             Sample minimum,
                                             Sample maximum,
                                             size_t bucket_count,
                                             int32_t scale,
                                             int32_t flags)
    :{}

ScaledLinearHistogram::ScaledLinearHistogram(const char* name,
                                             Sample minimum,
                                             Sample maximum,
                                             size_t bucket_count,
                                             int32_t scale,
                                             int32_t flags)
    :{}

ScaledLinearHistogram::~ScaledLinearHistogram() = default;

void ScaledLinearHistogram::AddScaledCount(Sample value, int64_t count) {}

//------------------------------------------------------------------------------
// This section provides implementation for BooleanHistogram.
//------------------------------------------------------------------------------

class BooleanHistogram::Factory : public Histogram::Factory {};

HistogramBase* BooleanHistogram::FactoryGet(std::string_view name,
                                            int32_t flags) {}

HistogramBase* BooleanHistogram::FactoryGet(const std::string& name,
                                            int32_t flags) {}

HistogramBase* BooleanHistogram::FactoryGet(const char* name, int32_t flags) {}

std::unique_ptr<HistogramBase> BooleanHistogram::PersistentCreate(
    const char* name,
    const BucketRanges* ranges,
    const DelayedPersistentAllocation& counts,
    const DelayedPersistentAllocation& logged_counts,
    HistogramSamples::Metadata* meta,
    HistogramSamples::Metadata* logged_meta) {}

HistogramType BooleanHistogram::GetHistogramType() const {}

// static
HistogramBase* BooleanHistogram::FactoryGetInternal(std::string_view name,
                                                    int32_t flags) {}

BooleanHistogram::BooleanHistogram(const char* name, const BucketRanges* ranges)
    :{}

BooleanHistogram::BooleanHistogram(
    const char* name,
    const BucketRanges* ranges,
    const DelayedPersistentAllocation& counts,
    const DelayedPersistentAllocation& logged_counts,
    HistogramSamples::Metadata* meta,
    HistogramSamples::Metadata* logged_meta)
    :{}

HistogramBase* BooleanHistogram::DeserializeInfoImpl(PickleIterator* iter) {}

//------------------------------------------------------------------------------
// CustomHistogram:
//------------------------------------------------------------------------------

class CustomHistogram::Factory : public Histogram::Factory {};

HistogramBase* CustomHistogram::FactoryGet(
    std::string_view name,
    const std::vector<Sample>& custom_ranges,
    int32_t flags) {}

HistogramBase* CustomHistogram::FactoryGet(
    const std::string& name,
    const std::vector<Sample>& custom_ranges,
    int32_t flags) {}

HistogramBase* CustomHistogram::FactoryGet(
    const char* name,
    const std::vector<Sample>& custom_ranges,
    int32_t flags) {}

std::unique_ptr<HistogramBase> CustomHistogram::PersistentCreate(
    const char* name,
    const BucketRanges* ranges,
    const DelayedPersistentAllocation& counts,
    const DelayedPersistentAllocation& logged_counts,
    HistogramSamples::Metadata* meta,
    HistogramSamples::Metadata* logged_meta) {}

HistogramType CustomHistogram::GetHistogramType() const {}

// static
std::vector<Sample> CustomHistogram::ArrayToCustomEnumRanges(
    base::span<const Sample> values) {}

CustomHistogram::CustomHistogram(const char* name, const BucketRanges* ranges)
    :{}

CustomHistogram::CustomHistogram(
    const char* name,
    const BucketRanges* ranges,
    const DelayedPersistentAllocation& counts,
    const DelayedPersistentAllocation& logged_counts,
    HistogramSamples::Metadata* meta,
    HistogramSamples::Metadata* logged_meta)
    :{}

void CustomHistogram::SerializeInfoImpl(Pickle* pickle) const {}

// static
HistogramBase* CustomHistogram::DeserializeInfoImpl(PickleIterator* iter) {}

// static
HistogramBase* CustomHistogram::FactoryGetInternal(
    std::string_view name,
    const std::vector<Sample>& custom_ranges,
    int32_t flags) {}

// static
bool CustomHistogram::ValidateCustomRanges(
    const std::vector<Sample>& custom_ranges) {}

namespace internal {

namespace {
// The pointer to the atomic const-pointer also needs to be atomic as some
// threads might already be alive when it's set. It requires acquire-release
// semantics to ensure the memory it points to is seen in its initialized state.
constinit std::atomic<const std::atomic<TimeTicks>*> g_last_foreground_time_ref;
}  // namespace

void SetSharedLastForegroundTimeForMetrics(
    const std::atomic<TimeTicks>* last_foreground_time_ref) {}

bool OverlapsBestEffortRange(TimeTicks sample_time, TimeDelta sample_interval) {}

}  // namespace internal

}  // namespace base