kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [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 | |
kaiwang@chromium.org | a97a5ae | 2012-08-02 06:34:08 +0900 | [diff] [blame] | 5 | // StatisticsRecorder holds all Histograms and BucketRanges that are used by |
| 6 | // Histograms in the system. It provides a general place for |
| 7 | // Histograms/BucketRanges to register, and supports a global API for accessing |
| 8 | // (i.e., dumping, or graphing) the data. |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 9 | |
| 10 | #ifndef BASE_METRICS_STATISTICS_RECORDER_H_ |
| 11 | #define BASE_METRICS_STATISTICS_RECORDER_H_ |
| 12 | |
| 13 | #include <list> |
| 14 | #include <map> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "base/base_export.h" |
| 19 | #include "base/basictypes.h" |
rtenneti@chromium.org | 00491d4 | 2012-07-20 04:17:32 +0900 | [diff] [blame] | 20 | #include "base/gtest_prod_util.h" |
| 21 | #include "base/lazy_instance.h" |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 22 | |
| 23 | namespace base { |
| 24 | |
kaiwang@chromium.org | 7c4acad | 2012-07-26 05:02:48 +0900 | [diff] [blame] | 25 | class BucketRanges; |
kaiwang@chromium.org | 7881e42 | 2013-03-01 12:53:25 +0900 | [diff] [blame] | 26 | class HistogramBase; |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 27 | class Lock; |
| 28 | |
| 29 | class BASE_EXPORT StatisticsRecorder { |
| 30 | public: |
kaiwang@chromium.org | 7881e42 | 2013-03-01 12:53:25 +0900 | [diff] [blame] | 31 | typedef std::vector<HistogramBase*> Histograms; |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 32 | |
rtenneti@chromium.org | 00491d4 | 2012-07-20 04:17:32 +0900 | [diff] [blame] | 33 | // Initializes the StatisticsRecorder system. |
| 34 | static void Initialize(); |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 35 | |
| 36 | // Find out if histograms can now be registered into our list. |
| 37 | static bool IsActive(); |
| 38 | |
| 39 | // Register, or add a new histogram to the collection of statistics. If an |
| 40 | // identically named histogram is already registered, then the argument |
| 41 | // |histogram| will deleted. The returned value is always the registered |
| 42 | // histogram (either the argument, or the pre-existing registered histogram). |
kaiwang@chromium.org | 7881e42 | 2013-03-01 12:53:25 +0900 | [diff] [blame] | 43 | static HistogramBase* RegisterOrDeleteDuplicate(HistogramBase* histogram); |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 44 | |
kaiwang@chromium.org | a97a5ae | 2012-08-02 06:34:08 +0900 | [diff] [blame] | 45 | // Register, or add a new BucketRanges. If an identically BucketRanges is |
| 46 | // already registered, then the argument |ranges| will deleted. The returned |
| 47 | // value is always the registered BucketRanges (either the argument, or the |
| 48 | // pre-existing one). |
| 49 | static const BucketRanges* RegisterOrDeleteDuplicateRanges( |
| 50 | const BucketRanges* ranges); |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 51 | |
grt@chromium.org | e29d3e5 | 2013-11-15 03:33:53 +0900 | [diff] [blame^] | 52 | // Methods for appending histogram data to a string. Only histograms which |
| 53 | // have |query| as a substring are written to |output| (an empty string will |
| 54 | // process all registered histograms). |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 55 | static void WriteHTMLGraph(const std::string& query, std::string* output); |
| 56 | static void WriteGraph(const std::string& query, std::string* output); |
| 57 | |
grt@chromium.org | e29d3e5 | 2013-11-15 03:33:53 +0900 | [diff] [blame^] | 58 | // Returns the histograms with |query| as a substring as JSON text (an empty |
| 59 | // |query| will process all registered histograms). |
| 60 | static std::string ToJSON(const std::string& query); |
| 61 | |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 62 | // Method for extracting histograms which were marked for use by UMA. |
| 63 | static void GetHistograms(Histograms* output); |
| 64 | |
kaiwang@chromium.org | a97a5ae | 2012-08-02 06:34:08 +0900 | [diff] [blame] | 65 | // Method for extracting BucketRanges used by all histograms registered. |
| 66 | static void GetBucketRanges(std::vector<const BucketRanges*>* output); |
| 67 | |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 68 | // Find a histogram by name. It matches the exact name. This method is thread |
kaiwang@chromium.org | 6fa0889 | 2012-07-19 03:06:30 +0900 | [diff] [blame] | 69 | // safe. It returns NULL if a matching histogram is not found. |
kaiwang@chromium.org | 7881e42 | 2013-03-01 12:53:25 +0900 | [diff] [blame] | 70 | static HistogramBase* FindHistogram(const std::string& name); |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 71 | |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 72 | // GetSnapshot copies some of the pointers to registered histograms into the |
rnk@chromium.org | 1706cb4 | 2013-07-19 05:42:40 +0900 | [diff] [blame] | 73 | // caller supplied vector (Histograms). Only histograms with names matching |
| 74 | // query are returned. The query must be a substring of histogram name for its |
| 75 | // pointer to be copied. |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 76 | static void GetSnapshot(const std::string& query, Histograms* snapshot); |
| 77 | |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 78 | private: |
| 79 | // We keep all registered histograms in a map, from name to histogram. |
kaiwang@chromium.org | 7881e42 | 2013-03-01 12:53:25 +0900 | [diff] [blame] | 80 | typedef std::map<std::string, HistogramBase*> HistogramMap; |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 81 | |
kaiwang@chromium.org | 7c4acad | 2012-07-26 05:02:48 +0900 | [diff] [blame] | 82 | // We keep all |bucket_ranges_| in a map, from checksum to a list of |
| 83 | // |bucket_ranges_|. Checksum is calculated from the |ranges_| in |
| 84 | // |bucket_ranges_|. |
kaiwang@chromium.org | a97a5ae | 2012-08-02 06:34:08 +0900 | [diff] [blame] | 85 | typedef std::map<uint32, std::list<const BucketRanges*>*> RangesMap; |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 86 | |
rtenneti@chromium.org | 00491d4 | 2012-07-20 04:17:32 +0900 | [diff] [blame] | 87 | friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; |
kaiwang@chromium.org | e4e800c | 2013-01-12 06:52:44 +0900 | [diff] [blame] | 88 | friend class HistogramBaseTest; |
gavinp@chromium.org | 3831bf4 | 2012-10-27 21:44:07 +0900 | [diff] [blame] | 89 | friend class HistogramTest; |
kaiwang@chromium.org | 8ef1a41 | 2013-03-16 18:15:54 +0900 | [diff] [blame] | 90 | friend class SparseHistogramTest; |
kaiwang@chromium.org | a97a5ae | 2012-08-02 06:34:08 +0900 | [diff] [blame] | 91 | friend class StatisticsRecorderTest; |
vitalybuka@chromium.org | 47ca9d8 | 2013-10-23 10:16:04 +0900 | [diff] [blame] | 92 | FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest, |
| 93 | DeserializeHistogramAndAddSamples); |
rtenneti@chromium.org | 00491d4 | 2012-07-20 04:17:32 +0900 | [diff] [blame] | 94 | |
kaiwang@chromium.org | a97a5ae | 2012-08-02 06:34:08 +0900 | [diff] [blame] | 95 | // The constructor just initializes static members. Usually client code should |
| 96 | // use Initialize to do this. But in test code, you can friend this class and |
| 97 | // call destructor/constructor to get a clean StatisticsRecorder. |
rtenneti@chromium.org | 00491d4 | 2012-07-20 04:17:32 +0900 | [diff] [blame] | 98 | StatisticsRecorder(); |
rtenneti@chromium.org | 00491d4 | 2012-07-20 04:17:32 +0900 | [diff] [blame] | 99 | ~StatisticsRecorder(); |
| 100 | |
tonyg@chromium.org | 4af9296 | 2013-05-02 12:09:25 +0900 | [diff] [blame] | 101 | static void DumpHistogramsToVlog(void* instance); |
| 102 | |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 103 | static HistogramMap* histograms_; |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 104 | static RangesMap* ranges_; |
| 105 | |
kaiwang@chromium.org | a97a5ae | 2012-08-02 06:34:08 +0900 | [diff] [blame] | 106 | // Lock protects access to above maps. |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 107 | static base::Lock* lock_; |
| 108 | |
kaiwang@chromium.org | 3c57dc6 | 2012-07-14 06:48:29 +0900 | [diff] [blame] | 109 | DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); |
| 110 | }; |
| 111 | |
| 112 | } // namespace base |
| 113 | |
| 114 | #endif // BASE_METRICS_STATISTICS_RECORDER_H_ |