Index: components/metrics/metrics_service.h |
diff --git a/components/metrics/metrics_service.h b/components/metrics/metrics_service.h |
index ce4102b8dfa36408fe037bd6772184d759a48a4e..33ecdda693eb55677238a43bbcf585381f839b29 100644 |
--- a/components/metrics/metrics_service.h |
+++ b/components/metrics/metrics_service.h |
@@ -22,6 +22,7 @@ |
#include "base/metrics/field_trial.h" |
#include "base/metrics/histogram_flattener.h" |
#include "base/metrics/histogram_snapshot_manager.h" |
+#include "base/metrics/persistent_memory_allocator.h" |
#include "base/metrics/user_metrics.h" |
#include "base/observer_list.h" |
#include "base/time/time.h" |
@@ -40,6 +41,7 @@ namespace base { |
class DictionaryValue; |
class HistogramSamples; |
class PrefService; |
+class SharedMemoryAllocator; |
} |
namespace variations { |
@@ -214,6 +216,16 @@ class MetricsService : public base::HistogramFlattener { |
// Pushes a log that has been generated by an external component. |
void PushExternalLog(const std::string& log); |
+ // Add a persistent memory segment that contains histograms. Ownership |
+ // of this object remains with the caller; it must be removed from here |
+ // before being destroyed. |
+ void AddPersistentMemorySegment(base::PersistentMemoryAllocator* alloc); |
+ |
+ // Remove a persistent memory segment. This should be done only after |
+ // a final reporting has been done to avoid data loss and incorrect |
+ // reporting. |
+ void RemovePersistentMemorySegment(base::PersistentMemoryAllocator* alloc); |
+ |
protected: |
// Exposed for testing. |
MetricsLogManager* log_manager() { return &log_manager_; } |
@@ -244,6 +256,7 @@ class MetricsService : public base::HistogramFlattener { |
UNSET |
}; |
+ typedef std::set<base::PersistentMemoryAllocator*> AllocatorSet; |
typedef std::vector<variations::SyntheticTrialGroup> SyntheticTrialGroups; |
// Registers a field trial name and group to be used to annotate a UMA report |
@@ -256,6 +269,58 @@ class MetricsService : public base::HistogramFlattener { |
void RegisterSyntheticFieldTrial( |
const variations::SyntheticTrialGroup& trial_group); |
+ |
+ // A class for iterating over the histograms in persistent memory segments. |
+ // This is implemented as an STL-compatible iterator so it can be used in |
+ // places that accept histograms both from here and from std containers. |
+ class PersistentHistogramIterator { |
+ // This class provides a reference-counted pointer to a histogram which |
+ // is necessary for histogram objects that are dynamically generated |
+ // (generally pointing to persistent memory) and to be owned by an STL |
+ // iterator which, by necessity, must be copyable. |
+ class HistogramPointer : public base::RefCounted<HistogramPointer> { |
+ public: |
+ HistogramPointer(base::HistogramBase* h) : histogram_(h) {} |
+ base::HistogramBase* get() { return histogram_.get(); } |
+ |
+ private: |
+ scoped_ptr<base::HistogramBase> histogram_; |
+ }; |
+ |
+ public: |
+ PersistentHistogramIterator(AllocatorSet& allocators, |
+ AllocatorSet::iterator pos); |
+ |
+ PersistentHistogramIterator& operator++(); |
+ PersistentHistogramIterator operator++(int) { |
+ PersistentHistogramIterator tmp(*this); |
+ operator++(); |
+ return tmp; |
+ } |
+ |
+ bool operator==(const PersistentHistogramIterator& rhs) const { |
+ return allocator_iter_ == rhs.allocator_iter_ && |
+ histogram_iter_ == rhs.histogram_iter_; |
+ } |
+ bool operator!=(const PersistentHistogramIterator& rhs) const { |
+ return allocator_iter_ != rhs.allocator_iter_ || |
+ histogram_iter_ != rhs.histogram_iter_; |
+ } |
+ base::HistogramBase* operator*() { |
+ return current_histogram_->get(); |
+ } |
+ |
+ private: |
+ AllocatorSet& allocators_; |
+ AllocatorSet::iterator allocator_iter_; |
+ base::PersistentMemoryAllocator::Iterator histogram_iter_; |
+ |
+ // STL iterators must be copyable so a regular scoped_ptr is not |
+ // sufficient as it doesn't allow such. A ref-counted one is needed. |
+ scoped_refptr<HistogramPointer> current_histogram_; |
+ }; |
+ |
+ |
// Calls into the client to initialize some system profile metrics. |
void StartInitTask(); |
@@ -394,6 +459,10 @@ class MetricsService : public base::HistogramFlattener { |
// upload or intentional sampling of logs. |
void SkipAndDiscardUpload(); |
+ // Begin and End iterators for going through all the metrics under management. |
+ PersistentHistogramIterator persistent_begin(); |
+ PersistentHistogramIterator persistent_end(); |
+ |
// Manager for the various in-flight logs. |
MetricsLogManager log_manager_; |
@@ -476,6 +545,7 @@ class MetricsService : public base::HistogramFlattener { |
FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, |
PermutedEntropyCacheClearedWhenLowEntropyReset); |
FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial); |
+ FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, MultiplePersistentAllocators); |
// Weak pointers factory used to post task on different threads. All weak |
// pointers managed by this factory have the same lifetime as MetricsService. |
@@ -485,6 +555,9 @@ class MetricsService : public base::HistogramFlattener { |
// this factory are invalidated in ScheduleNextStateSave. |
base::WeakPtrFactory<MetricsService> state_saver_factory_; |
+ // Persistent memory segments that contain histograms created elsewhere. |
+ AllocatorSet allocators_; |
+ |
DISALLOW_COPY_AND_ASSIGN(MetricsService); |
}; |