#ifdef UNSAFE_BUFFERS_BUILD
#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 {
enum : uint32_t { … };
subtle::AtomicWord g_histogram_allocator = …;
std::unique_ptr<BucketRanges> CreateRangesFromData(
HistogramBase::Sample* ranges_data,
uint32_t ranges_checksum,
size_t count) { … }
size_t CalculateRequiredCountsBytes(size_t bucket_count) { … }
void MergeSamplesToExistingHistogram(
HistogramBase* existing,
const HistogramBase* histogram,
std::unique_ptr<HistogramSamples> samples) { … }
}
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) { … }
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() { … }
void GlobalHistogramAllocator::CreateWithPersistentMemory(
void* base,
size_t size,
size_t page_size,
uint64_t id,
std::string_view name) { … }
void GlobalHistogramAllocator::CreateWithLocalMemory(size_t size,
uint64_t id,
std::string_view name) { … }
#if !BUILDFLAG(IS_NACL)
bool GlobalHistogramAllocator::CreateWithFile(const FilePath& file_path,
size_t size,
uint64_t id,
std::string_view name,
bool exclusive_write) { … }
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) { … }
bool GlobalHistogramAllocator::CreateWithActiveFileInDir(
const FilePath& dir,
size_t size,
uint64_t id,
std::string_view name) { … }
FilePath GlobalHistogramAllocator::ConstructFilePath(const FilePath& dir,
std::string_view name) { … }
FilePath GlobalHistogramAllocator::ConstructFilePathForActiveFile(
const FilePath& dir,
std::string_view name) { … }
FilePath GlobalHistogramAllocator::ConstructFilePathForUploadDir(
const FilePath& dir,
std::string_view name,
base::Time stamp,
ProcessId pid) { … }
FilePath GlobalHistogramAllocator::ConstructFilePathForUploadDir(
const FilePath& dir,
std::string_view name) { … }
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
void GlobalHistogramAllocator::CreateWithSharedMemoryRegion(
const UnsafeSharedMemoryRegion& region) { … }
void GlobalHistogramAllocator::Set(GlobalHistogramAllocator* allocator) { … }
GlobalHistogramAllocator* GlobalHistogramAllocator::Get() { … }
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() { … }
}