Chromium Code Reviews| Index: base/metrics/persistent_sample_map.h |
| diff --git a/base/metrics/persistent_sample_map.h b/base/metrics/persistent_sample_map.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..acdd3b07e0e7ec9adadfa5bf7b849a5547592dc6 |
| --- /dev/null |
| +++ b/base/metrics/persistent_sample_map.h |
| @@ -0,0 +1,95 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// PersistentSampleMap implements HistogramSamples interface. It is used |
| +// by the SparseHistogram class to store samples in persistent memory which |
| +// allows it to be shared between processes or live across restarts. |
| + |
| +#ifndef BASE_METRICS_PERSISTENT_SAMPLE_MAP_H_ |
| +#define BASE_METRICS_PERSISTENT_SAMPLE_MAP_H_ |
| + |
| +#include <stdint.h> |
| + |
| +#include <map> |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/metrics/histogram_base.h" |
| +#include "base/metrics/histogram_samples.h" |
| +#include "base/metrics/persistent_memory_allocator.h" |
| + |
| +namespace base { |
| + |
| +class BASE_EXPORT PersistentSampleMap : public HistogramSamples { |
| + public: |
| + PersistentSampleMap(uint64_t id, |
| + PersistentMemoryAllocator* allocator, |
| + Metadata* meta); |
| + ~PersistentSampleMap() override; |
| + |
| + // HistogramSamples: |
| + void Accumulate(HistogramBase::Sample value, |
| + HistogramBase::Count count) override; |
| + HistogramBase::Count GetCount(HistogramBase::Sample value) const override; |
| + HistogramBase::Count TotalCount() const override; |
| + scoped_ptr<SampleCountIterator> Iterator() const override; |
| + |
| + protected: |
| + // Performs arithemetic. |op| is ADD or SUBTRACT. |
| + bool AddSubtractImpl(SampleCountIterator* iter, Operator op) override; |
| + |
| + // Gets a pointer to a "count" corresponding to a given |value|. Returns NULL |
| + // if sample does not exist. |
| + HistogramBase::Count* GetSampleCountStorage(HistogramBase::Sample value); |
| + |
| + // Gets a pointer to a "count" corresponding to a given |value|, creating |
| + // the sample (initialized to zero) if it does not already exists. |
| + HistogramBase::Count* GetOrCreateSampleCountStorage( |
| + HistogramBase::Sample value); |
| + |
| + private: |
| + enum : HistogramBase::Sample { kAllSamples = -1 }; |
| + |
| + // Imports samples from persistent memory, stopping after importing |
|
Alexei Svitkine (slow)
2016/03/08 19:46:25
I don't understand what Import means in this conte
bcwhite
2016/03/09 01:16:52
Done.
|
| + // |until_value| or when there is nothing left to import. Pass kAllSamples |
|
Alexei Svitkine (slow)
2016/03/08 19:46:25
What does "stopping after importing |until_value|"
bcwhite
2016/03/09 01:16:52
It's the latter.
I had -1 originally documented i
Alexei Svitkine (slow)
2016/03/09 20:35:19
Okay. I guess it doesn't matter too much either wa
|
| + // to import everything. Returns a pointer to the count for |until_value| |
| + // if it is found, NULL otherwise. |
|
Alexei Svitkine (slow)
2016/03/08 19:46:25
Nit: NULL -> null
bcwhite
2016/03/09 01:16:52
Done.
|
| + HistogramBase::Count* ImportSamples(HistogramBase::Sample until_value); |
| + |
| + // All created/loaded sample values and their associated counts. |
|
Alexei Svitkine (slow)
2016/03/08 19:46:25
Expand on ownership.
bcwhite
2016/03/09 01:16:52
Done.
|
| + std::map<HistogramBase::Sample, HistogramBase::Count*> sample_counts_; |
| + |
| + // The persistent memory allocator holding samples and an iterator through it. |
| + PersistentMemoryAllocator* allocator_; |
| + PersistentMemoryAllocator::Iterator sample_iter_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PersistentSampleMap); |
| +}; |
| + |
| +class BASE_EXPORT PersistentSampleMapIterator : public SampleCountIterator { |
|
Alexei Svitkine (slow)
2016/03/08 19:46:25
This class shouldn't be used directly by clients,
bcwhite
2016/03/09 01:16:52
That works. Would you like me to do the same to S
Alexei Svitkine (slow)
2016/03/09 20:35:19
Yes, please. :)
bcwhite
2016/03/09 22:57:33
Done.
|
| + public: |
| + typedef std::map<HistogramBase::Sample, HistogramBase::Count*> |
| + SampleToCountMap; |
| + |
| + explicit PersistentSampleMapIterator(const SampleToCountMap& sample_counts); |
| + ~PersistentSampleMapIterator() override; |
| + |
| + // SampleCountIterator: |
| + bool Done() const override; |
| + void Next() override; |
| + void Get(HistogramBase::Sample* min, |
| + HistogramBase::Sample* max, |
| + HistogramBase::Count* count) const override; |
| + |
| + private: |
| + void SkipEmptyBuckets(); |
| + |
| + SampleToCountMap::const_iterator iter_; |
| + const SampleToCountMap::const_iterator end_; |
| +}; |
| + |
| +} // namespace base |
| + |
| +#endif // BASE_METRICS_PERSISTENT_SAMPLE_MAP_H_ |