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