persistent_sample_map.h 4.6 KB

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