chromium/base/metrics/persistent_histogram_allocator.cc

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "base/metrics/persistent_histogram_allocator.h"

#include <atomic>
#include <limits>
#include <string_view>
#include <utility>

#include "base/debug/crash_logging.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/files/memory_mapped_file.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/metrics_hashes.h"
#include "base/metrics/persistent_sample_map.h"
#include "base/metrics/sparse_histogram.h"
#include "base/metrics/statistics_recorder.h"
#include "base/notreached.h"
#include "base/pickle.h"
#include "base/process/process_handle.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/lock.h"
#include "build/build_config.h"

namespace base {

namespace {

// Type identifiers used when storing in persistent memory so they can be
// identified during extraction; the first 4 bytes of the SHA1 of the name
// is used as a unique integer. A "version number" is added to the base
// so that, if the structure of that object changes, stored older versions
// will be safely ignored.
enum : uint32_t {};

// The current globally-active persistent allocator for all new histograms.
// The object held here will obviously not be destructed at process exit
// but that's best since PersistentMemoryAllocator objects (that underlie
// GlobalHistogramAllocator objects) are explicitly forbidden from doing
// anything essential at exit anyway due to the fact that they depend on data
// managed elsewhere and which could be destructed first. An AtomicWord is
// used instead of std::atomic because the latter can create global ctors
// and dtors.
subtle::AtomicWord g_histogram_allocator =;

// Take an array of range boundaries and create a proper BucketRanges object
// which is returned to the caller. A return of nullptr indicates that the
// passed boundaries are invalid.
std::unique_ptr<BucketRanges> CreateRangesFromData(
    HistogramBase::Sample* ranges_data,
    uint32_t ranges_checksum,
    size_t count) {}

// Calculate the number of bytes required to store all of a histogram's
// "counts". This will return zero (0) if |bucket_count| is not valid.
size_t CalculateRequiredCountsBytes(size_t bucket_count) {}

void MergeSamplesToExistingHistogram(
    HistogramBase* existing,
    const HistogramBase* histogram,
    std::unique_ptr<HistogramSamples> samples) {}

}  // namespace

PersistentSparseHistogramDataManager::PersistentSparseHistogramDataManager(
    PersistentMemoryAllocator* allocator)
    :{}

PersistentSparseHistogramDataManager::~PersistentSparseHistogramDataManager() =
    default;

std::unique_ptr<PersistentSampleMapRecords>
PersistentSparseHistogramDataManager::CreateSampleMapRecords(uint64_t id) {}

std::vector<PersistentSparseHistogramDataManager::ReferenceAndSample>*
PersistentSparseHistogramDataManager::GetSampleMapRecordsWhileLocked(
    uint64_t id) {}

std::vector<PersistentMemoryAllocator::Reference>
PersistentSparseHistogramDataManager::LoadRecords(
    PersistentSampleMapRecords* sample_map_records,
    std::optional<HistogramBase::Sample> until_value) {}

PersistentSampleMapRecords::PersistentSampleMapRecords(
    PersistentSparseHistogramDataManager* data_manager,
    uint64_t sample_map_id,
    std::vector<PersistentSparseHistogramDataManager::ReferenceAndSample>*
        records)
    :{}

PersistentSampleMapRecords::~PersistentSampleMapRecords() = default;

std::vector<PersistentMemoryAllocator::Reference>
PersistentSampleMapRecords::GetNextRecords(
    std::optional<HistogramBase::Sample> until_value) {}

PersistentMemoryAllocator::Reference PersistentSampleMapRecords::CreateNew(
    HistogramBase::Sample value) {}


// This data will be held in persistent memory in order for processes to
// locate and use histograms created elsewhere.
struct PersistentHistogramAllocator::PersistentHistogramData {};

PersistentHistogramAllocator::Iterator::Iterator(
    PersistentHistogramAllocator* allocator)
    :{}

std::unique_ptr<HistogramBase>
PersistentHistogramAllocator::Iterator::GetNextWithIgnore(Reference ignore) {}

PersistentHistogramAllocator::PersistentHistogramAllocator(
    std::unique_ptr<PersistentMemoryAllocator> memory)
    :{}

PersistentHistogramAllocator::~PersistentHistogramAllocator() = default;

std::unique_ptr<HistogramBase> PersistentHistogramAllocator::GetHistogram(
    Reference ref) {}

std::unique_ptr<HistogramBase> PersistentHistogramAllocator::AllocateHistogram(
    HistogramType histogram_type,
    std::string_view name,
    int minimum,
    int maximum,
    const BucketRanges* bucket_ranges,
    int32_t flags,
    Reference* ref_ptr) {}

void PersistentHistogramAllocator::FinalizeHistogram(Reference ref,
                                                     bool registered) {}

void PersistentHistogramAllocator::MergeHistogramDeltaToStatisticsRecorder(
    HistogramBase* histogram) {}

void PersistentHistogramAllocator::MergeHistogramFinalDeltaToStatisticsRecorder(
    const HistogramBase* histogram) {}

std::unique_ptr<PersistentSampleMapRecords>
PersistentHistogramAllocator::CreateSampleMapRecords(uint64_t id) {}

void PersistentHistogramAllocator::CreateTrackingHistograms(
    std::string_view name) {}

void PersistentHistogramAllocator::UpdateTrackingHistograms() {}

void PersistentHistogramAllocator::SetRangesManager(
    RangesManager* ranges_manager) {}

void PersistentHistogramAllocator::ClearLastCreatedReferenceForTesting() {}

std::unique_ptr<HistogramBase> PersistentHistogramAllocator::CreateHistogram(
    PersistentHistogramData* histogram_data_ptr) {}

HistogramBase*
PersistentHistogramAllocator::GetOrCreateStatisticsRecorderHistogram(
    const HistogramBase* histogram) {}

GlobalHistogramAllocator::~GlobalHistogramAllocator() {}

// static
void GlobalHistogramAllocator::CreateWithPersistentMemory(
    void* base,
    size_t size,
    size_t page_size,
    uint64_t id,
    std::string_view name) {}

// static
void GlobalHistogramAllocator::CreateWithLocalMemory(size_t size,
                                                     uint64_t id,
                                                     std::string_view name) {}

#if !BUILDFLAG(IS_NACL)
// static
bool GlobalHistogramAllocator::CreateWithFile(const FilePath& file_path,
                                              size_t size,
                                              uint64_t id,
                                              std::string_view name,
                                              bool exclusive_write) {}

// static
bool GlobalHistogramAllocator::CreateWithActiveFile(const FilePath& base_path,
                                                    const FilePath& active_path,
                                                    const FilePath& spare_path,
                                                    size_t size,
                                                    uint64_t id,
                                                    std::string_view name) {}

// static
bool GlobalHistogramAllocator::CreateWithActiveFileInDir(
    const FilePath& dir,
    size_t size,
    uint64_t id,
    std::string_view name) {}

// static
FilePath GlobalHistogramAllocator::ConstructFilePath(const FilePath& dir,
                                                     std::string_view name) {}

// static
FilePath GlobalHistogramAllocator::ConstructFilePathForActiveFile(
    const FilePath& dir,
    std::string_view name) {}

// static
FilePath GlobalHistogramAllocator::ConstructFilePathForUploadDir(
    const FilePath& dir,
    std::string_view name,
    base::Time stamp,
    ProcessId pid) {}

// static
FilePath GlobalHistogramAllocator::ConstructFilePathForUploadDir(
    const FilePath& dir,
    std::string_view name) {}

// static
bool GlobalHistogramAllocator::ParseFilePath(const FilePath& path,
                                             std::string* out_name,
                                             Time* out_stamp,
                                             ProcessId* out_pid) {}

bool GlobalHistogramAllocator::CreateSpareFile(const FilePath& spare_path,
                                               size_t size) {}
#endif  // !BUILDFLAG(IS_NACL)

// static
void GlobalHistogramAllocator::CreateWithSharedMemoryRegion(
    const UnsafeSharedMemoryRegion& region) {}

// static
void GlobalHistogramAllocator::Set(GlobalHistogramAllocator* allocator) {}

// static
GlobalHistogramAllocator* GlobalHistogramAllocator::Get() {}

// static
GlobalHistogramAllocator* GlobalHistogramAllocator::ReleaseForTesting() {}

void GlobalHistogramAllocator::SetPersistentLocation(const FilePath& location) {}

const FilePath& GlobalHistogramAllocator::GetPersistentLocation() const {}

bool GlobalHistogramAllocator::HasPersistentLocation() const {}

bool GlobalHistogramAllocator::MovePersistentFile(const FilePath& dir) {}

bool GlobalHistogramAllocator::WriteToPersistentLocation() {}

void GlobalHistogramAllocator::DeletePersistentLocation() {}

GlobalHistogramAllocator::GlobalHistogramAllocator(
    std::unique_ptr<PersistentMemoryAllocator> memory)
    :{}

void GlobalHistogramAllocator::ImportHistogramsToStatisticsRecorder() {}

}  // namespace base