1*635a8641SAndroid Build Coastguard Worker // Copyright 2016 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker // PersistentSampleMap implements HistogramSamples interface. It is used 6*635a8641SAndroid Build Coastguard Worker // by the SparseHistogram class to store samples in persistent memory which 7*635a8641SAndroid Build Coastguard Worker // allows it to be shared between processes or live across restarts. 8*635a8641SAndroid Build Coastguard Worker 9*635a8641SAndroid Build Coastguard Worker #ifndef BASE_METRICS_PERSISTENT_SAMPLE_MAP_H_ 10*635a8641SAndroid Build Coastguard Worker #define BASE_METRICS_PERSISTENT_SAMPLE_MAP_H_ 11*635a8641SAndroid Build Coastguard Worker 12*635a8641SAndroid Build Coastguard Worker #include <stdint.h> 13*635a8641SAndroid Build Coastguard Worker 14*635a8641SAndroid Build Coastguard Worker #include <map> 15*635a8641SAndroid Build Coastguard Worker #include <memory> 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h" 18*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 19*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_base.h" 20*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_samples.h" 21*635a8641SAndroid Build Coastguard Worker #include "base/metrics/persistent_memory_allocator.h" 22*635a8641SAndroid Build Coastguard Worker 23*635a8641SAndroid Build Coastguard Worker namespace base { 24*635a8641SAndroid Build Coastguard Worker 25*635a8641SAndroid Build Coastguard Worker class PersistentHistogramAllocator; 26*635a8641SAndroid Build Coastguard Worker class PersistentSampleMapRecords; 27*635a8641SAndroid Build Coastguard Worker 28*635a8641SAndroid Build Coastguard Worker // The logic here is similar to that of SampleMap but with different data 29*635a8641SAndroid Build Coastguard Worker // structures. Changes here likely need to be duplicated there. 30*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT PersistentSampleMap : public HistogramSamples { 31*635a8641SAndroid Build Coastguard Worker public: 32*635a8641SAndroid Build Coastguard Worker // Constructs a persistent sample map using a PersistentHistogramAllocator 33*635a8641SAndroid Build Coastguard Worker // as the data source for persistent records. 34*635a8641SAndroid Build Coastguard Worker PersistentSampleMap(uint64_t id, 35*635a8641SAndroid Build Coastguard Worker PersistentHistogramAllocator* allocator, 36*635a8641SAndroid Build Coastguard Worker Metadata* meta); 37*635a8641SAndroid Build Coastguard Worker 38*635a8641SAndroid Build Coastguard Worker ~PersistentSampleMap() override; 39*635a8641SAndroid Build Coastguard Worker 40*635a8641SAndroid Build Coastguard Worker // HistogramSamples: 41*635a8641SAndroid Build Coastguard Worker void Accumulate(HistogramBase::Sample value, 42*635a8641SAndroid Build Coastguard Worker HistogramBase::Count count) override; 43*635a8641SAndroid Build Coastguard Worker HistogramBase::Count GetCount(HistogramBase::Sample value) const override; 44*635a8641SAndroid Build Coastguard Worker HistogramBase::Count TotalCount() const override; 45*635a8641SAndroid Build Coastguard Worker std::unique_ptr<SampleCountIterator> Iterator() const override; 46*635a8641SAndroid Build Coastguard Worker 47*635a8641SAndroid Build Coastguard Worker // Uses a persistent-memory |iterator| to locate and return information about 48*635a8641SAndroid Build Coastguard Worker // the next record holding information for a PersistentSampleMap. The record 49*635a8641SAndroid Build Coastguard Worker // could be for any Map so return the |sample_map_id| as well. 50*635a8641SAndroid Build Coastguard Worker static PersistentMemoryAllocator::Reference GetNextPersistentRecord( 51*635a8641SAndroid Build Coastguard Worker PersistentMemoryAllocator::Iterator& iterator, 52*635a8641SAndroid Build Coastguard Worker uint64_t* sample_map_id); 53*635a8641SAndroid Build Coastguard Worker 54*635a8641SAndroid Build Coastguard Worker // Creates a new record in an |allocator| storing count information for a 55*635a8641SAndroid Build Coastguard Worker // specific sample |value| of a histogram with the given |sample_map_id|. 56*635a8641SAndroid Build Coastguard Worker static PersistentMemoryAllocator::Reference CreatePersistentRecord( 57*635a8641SAndroid Build Coastguard Worker PersistentMemoryAllocator* allocator, 58*635a8641SAndroid Build Coastguard Worker uint64_t sample_map_id, 59*635a8641SAndroid Build Coastguard Worker HistogramBase::Sample value); 60*635a8641SAndroid Build Coastguard Worker 61*635a8641SAndroid Build Coastguard Worker protected: 62*635a8641SAndroid Build Coastguard Worker // Performs arithemetic. |op| is ADD or SUBTRACT. 63*635a8641SAndroid Build Coastguard Worker bool AddSubtractImpl(SampleCountIterator* iter, Operator op) override; 64*635a8641SAndroid Build Coastguard Worker 65*635a8641SAndroid Build Coastguard Worker // Gets a pointer to a "count" corresponding to a given |value|. Returns NULL 66*635a8641SAndroid Build Coastguard Worker // if sample does not exist. 67*635a8641SAndroid Build Coastguard Worker HistogramBase::Count* GetSampleCountStorage(HistogramBase::Sample value); 68*635a8641SAndroid Build Coastguard Worker 69*635a8641SAndroid Build Coastguard Worker // Gets a pointer to a "count" corresponding to a given |value|, creating 70*635a8641SAndroid Build Coastguard Worker // the sample (initialized to zero) if it does not already exists. 71*635a8641SAndroid Build Coastguard Worker HistogramBase::Count* GetOrCreateSampleCountStorage( 72*635a8641SAndroid Build Coastguard Worker HistogramBase::Sample value); 73*635a8641SAndroid Build Coastguard Worker 74*635a8641SAndroid Build Coastguard Worker private: 75*635a8641SAndroid Build Coastguard Worker // Gets the object that manages persistent records. This returns the 76*635a8641SAndroid Build Coastguard Worker // |records_| member after first initializing it if necessary. 77*635a8641SAndroid Build Coastguard Worker PersistentSampleMapRecords* GetRecords(); 78*635a8641SAndroid Build Coastguard Worker 79*635a8641SAndroid Build Coastguard Worker // Imports samples from persistent memory by iterating over all sample 80*635a8641SAndroid Build Coastguard Worker // records found therein, adding them to the sample_counts_ map. If a 81*635a8641SAndroid Build Coastguard Worker // count for the sample |until_value| is found, stop the import and return 82*635a8641SAndroid Build Coastguard Worker // a pointer to that counter. If that value is not found, null will be 83*635a8641SAndroid Build Coastguard Worker // returned after all currently available samples have been loaded. Pass 84*635a8641SAndroid Build Coastguard Worker // true for |import_everything| to force the importing of all available 85*635a8641SAndroid Build Coastguard Worker // samples even if a match is found. 86*635a8641SAndroid Build Coastguard Worker HistogramBase::Count* ImportSamples(HistogramBase::Sample until_value, 87*635a8641SAndroid Build Coastguard Worker bool import_everything); 88*635a8641SAndroid Build Coastguard Worker 89*635a8641SAndroid Build Coastguard Worker // All created/loaded sample values and their associated counts. The storage 90*635a8641SAndroid Build Coastguard Worker // for the actual Count numbers is owned by the |records_| object and its 91*635a8641SAndroid Build Coastguard Worker // underlying allocator. 92*635a8641SAndroid Build Coastguard Worker std::map<HistogramBase::Sample, HistogramBase::Count*> sample_counts_; 93*635a8641SAndroid Build Coastguard Worker 94*635a8641SAndroid Build Coastguard Worker // The allocator that manages histograms inside persistent memory. This is 95*635a8641SAndroid Build Coastguard Worker // owned externally and is expected to live beyond the life of this object. 96*635a8641SAndroid Build Coastguard Worker PersistentHistogramAllocator* allocator_; 97*635a8641SAndroid Build Coastguard Worker 98*635a8641SAndroid Build Coastguard Worker // The object that manages sample records inside persistent memory. This is 99*635a8641SAndroid Build Coastguard Worker // owned by the |allocator_| object (above) and so, like it, is expected to 100*635a8641SAndroid Build Coastguard Worker // live beyond the life of this object. This value is lazily-initialized on 101*635a8641SAndroid Build Coastguard Worker // first use via the GetRecords() accessor method. 102*635a8641SAndroid Build Coastguard Worker PersistentSampleMapRecords* records_ = nullptr; 103*635a8641SAndroid Build Coastguard Worker 104*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(PersistentSampleMap); 105*635a8641SAndroid Build Coastguard Worker }; 106*635a8641SAndroid Build Coastguard Worker 107*635a8641SAndroid Build Coastguard Worker } // namespace base 108*635a8641SAndroid Build Coastguard Worker 109*635a8641SAndroid Build Coastguard Worker #endif // BASE_METRICS_PERSISTENT_SAMPLE_MAP_H_ 110