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