[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | #ifndef BASE_METRICS_SPARSE_HISTOGRAM_H_ |
| 6 | #define BASE_METRICS_SPARSE_HISTOGRAM_H_ |
| 7 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 11 | #include <map> |
| 12 | #include <string> |
| 13 | |
| 14 | #include "base/base_export.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 15 | #include "base/compiler_specific.h" |
[email protected] | 5418aeb6 | 2013-03-16 09:15:54 | [diff] [blame] | 16 | #include "base/logging.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 17 | #include "base/macros.h" |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 18 | #include "base/memory/scoped_ptr.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 19 | #include "base/metrics/histogram_base.h" |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 20 | #include "base/metrics/sample_map.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 21 | #include "base/synchronization/lock.h" |
| 22 | |
| 23 | namespace base { |
| 24 | |
asvitkine | c0fb802 | 2014-08-26 04:39:35 | [diff] [blame] | 25 | #define UMA_HISTOGRAM_SPARSE_SLOWLY(name, sample) \ |
[email protected] | 5418aeb6 | 2013-03-16 09:15:54 | [diff] [blame] | 26 | do { \ |
asvitkine | c0fb802 | 2014-08-26 04:39:35 | [diff] [blame] | 27 | base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( \ |
| 28 | name, base::HistogramBase::kUmaTargetedHistogramFlag); \ |
[email protected] | 5418aeb6 | 2013-03-16 09:15:54 | [diff] [blame] | 29 | histogram->Add(sample); \ |
| 30 | } while (0) |
| 31 | |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 32 | class HistogramSamples; |
| 33 | |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 34 | class BASE_EXPORT SparseHistogram : public HistogramBase { |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 35 | public: |
| 36 | // If there's one with same name, return the existing one. If not, create a |
| 37 | // new one. |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 38 | static HistogramBase* FactoryGet(const std::string& name, int32_t flags); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 39 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 40 | ~SparseHistogram() override; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 41 | |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 42 | // HistogramBase implementation: |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 43 | uint64_t name_hash() const override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 44 | HistogramType GetHistogramType() const override; |
| 45 | bool HasConstructionArguments(Sample expected_minimum, |
| 46 | Sample expected_maximum, |
| 47 | size_t expected_bucket_count) const override; |
| 48 | void Add(Sample value) override; |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 49 | void AddCount(Sample value, int count) override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 50 | void AddSamples(const HistogramSamples& samples) override; |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 51 | bool AddSamplesFromPickle(base::PickleIterator* iter) override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 52 | scoped_ptr<HistogramSamples> SnapshotSamples() const override; |
| 53 | void WriteHTMLGraph(std::string* output) const override; |
| 54 | void WriteAscii(std::string* output) const override; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 55 | |
| 56 | protected: |
| 57 | // HistogramBase implementation: |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 58 | bool SerializeInfoImpl(base::Pickle* pickle) const override; |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 59 | |
| 60 | private: |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 61 | // Clients should always use FactoryGet to create SparseHistogram. |
[email protected] | f3c697c5 | 2013-01-15 10:52:11 | [diff] [blame] | 62 | explicit SparseHistogram(const std::string& name); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 63 | |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 64 | friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 65 | base::PickleIterator* iter); |
| 66 | static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 67 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 68 | void GetParameters(DictionaryValue* params) const override; |
| 69 | void GetCountAndBucketData(Count* count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 70 | int64_t* sum, |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 71 | ListValue* buckets) const override; |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 72 | |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 73 | // Helpers for emitting Ascii graphic. Each method appends data to output. |
| 74 | void WriteAsciiImpl(bool graph_it, |
| 75 | const std::string& newline, |
| 76 | std::string* output) const; |
| 77 | |
| 78 | // Write a common header message describing this histogram. |
| 79 | void WriteAsciiHeader(const Count total_count, |
| 80 | std::string* output) const; |
| 81 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 82 | // For constuctor calling. |
| 83 | friend class SparseHistogramTest; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 84 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 85 | // Protects access to |samples_|. |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 86 | mutable base::Lock lock_; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 87 | |
| 88 | SampleMap samples_; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(SparseHistogram); |
| 91 | }; |
| 92 | |
| 93 | } // namespace base |
| 94 | |
| 95 | #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_ |