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 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <map> 12 #include <string> 13 14 #include "base/base_export.h" 15 #include "base/compiler_specific.h" 16 #include "base/logging.h" 17 #include "base/macros.h" 18 #include "base/memory/scoped_ptr.h" 19 #include "base/metrics/histogram_base.h" 20 #include "base/metrics/sample_map.h" 21 #include "base/synchronization/lock.h" 22 23 namespace base { 24 25 #define UMA_HISTOGRAM_SPARSE_SLOWLY(name, sample) \ 26 do { \ 27 base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( \ 28 name, base::HistogramBase::kUmaTargetedHistogramFlag); \ 29 histogram->Add(sample); \ 30 } while (0) 31 32 class HistogramSamples; 33 34 class BASE_EXPORT SparseHistogram : public HistogramBase { 35 public: 36 // If there's one with same name, return the existing one. If not, create a 37 // new one. 38 static HistogramBase* FactoryGet(const std::string& name, int32_t flags); 39 40 ~SparseHistogram() override; 41 42 // HistogramBase implementation: 43 uint64_t name_hash() const override; 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; 49 void AddCount(Sample value, int count) override; 50 void AddSamples(const HistogramSamples& samples) override; 51 bool AddSamplesFromPickle(base::PickleIterator* iter) override; 52 scoped_ptr<HistogramSamples> SnapshotSamples() const override; 53 void WriteHTMLGraph(std::string* output) const override; 54 void WriteAscii(std::string* output) const override; 55 56 protected: 57 // HistogramBase implementation: 58 bool SerializeInfoImpl(base::Pickle* pickle) const override; 59 60 private: 61 // Clients should always use FactoryGet to create SparseHistogram. 62 explicit SparseHistogram(const std::string& name); 63 64 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( 65 base::PickleIterator* iter); 66 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); 67 68 void GetParameters(DictionaryValue* params) const override; 69 void GetCountAndBucketData(Count* count, 70 int64_t* sum, 71 ListValue* buckets) const override; 72 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 82 // For constuctor calling. 83 friend class SparseHistogramTest; 84 85 // Protects access to |samples_|. 86 mutable base::Lock lock_; 87 88 SampleMap samples_; 89 90 DISALLOW_COPY_AND_ASSIGN(SparseHistogram); 91 }; 92 93 } // namespace base 94 95 #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_ 96