OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // StatisticsRecorder handles all histograms in the system. It provides a | 5 // StatisticsRecorder handles all histograms in the system. It provides a |
6 // general place for histograms to register, and supports a global API for | 6 // general place for histograms to register, and supports a global API for |
7 // accessing (i.e., dumping, or graphing) the data in all the histograms. | 7 // accessing (i.e., dumping, or graphing) the data in all the histograms. |
8 | 8 |
9 #ifndef BASE_METRICS_STATISTICS_RECORDER_H_ | 9 #ifndef BASE_METRICS_STATISTICS_RECORDER_H_ |
10 #define BASE_METRICS_STATISTICS_RECORDER_H_ | 10 #define BASE_METRICS_STATISTICS_RECORDER_H_ |
11 | 11 |
12 #include <list> | 12 #include <list> |
13 #include <map> | 13 #include <map> |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/base_export.h" | 17 #include "base/base_export.h" |
18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
| 19 #include "base/gtest_prod_util.h" |
| 20 #include "base/lazy_instance.h" |
19 | 21 |
20 namespace base { | 22 namespace base { |
21 | 23 |
22 class CachedRanges; | 24 class CachedRanges; |
23 class Histogram; | 25 class Histogram; |
24 class Lock; | 26 class Lock; |
25 | 27 |
26 class BASE_EXPORT StatisticsRecorder { | 28 class BASE_EXPORT StatisticsRecorder { |
27 public: | 29 public: |
28 typedef std::vector<Histogram*> Histograms; | 30 typedef std::vector<Histogram*> Histograms; |
29 | 31 |
30 StatisticsRecorder(); | 32 // Initializes the StatisticsRecorder system. |
31 | 33 static void Initialize(); |
32 ~StatisticsRecorder(); | |
33 | 34 |
34 // Find out if histograms can now be registered into our list. | 35 // Find out if histograms can now be registered into our list. |
35 static bool IsActive(); | 36 static bool IsActive(); |
36 | 37 |
37 // Register, or add a new histogram to the collection of statistics. If an | 38 // Register, or add a new histogram to the collection of statistics. If an |
38 // identically named histogram is already registered, then the argument | 39 // identically named histogram is already registered, then the argument |
39 // |histogram| will deleted. The returned value is always the registered | 40 // |histogram| will deleted. The returned value is always the registered |
40 // histogram (either the argument, or the pre-existing registered histogram). | 41 // histogram (either the argument, or the pre-existing registered histogram). |
41 static Histogram* RegisterOrDeleteDuplicate(Histogram* histogram); | 42 static Histogram* RegisterOrDeleteDuplicate(Histogram* histogram); |
42 | 43 |
(...skipping 27 matching lines...) Expand all Loading... |
70 static bool dump_on_exit() { return dump_on_exit_; } | 71 static bool dump_on_exit() { return dump_on_exit_; } |
71 | 72 |
72 static void set_dump_on_exit(bool enable) { dump_on_exit_ = enable; } | 73 static void set_dump_on_exit(bool enable) { dump_on_exit_ = enable; } |
73 | 74 |
74 // GetSnapshot copies some of the pointers to registered histograms into the | 75 // GetSnapshot copies some of the pointers to registered histograms into the |
75 // caller supplied vector (Histograms). Only histograms with names matching | 76 // caller supplied vector (Histograms). Only histograms with names matching |
76 // query are returned. The query must be a substring of histogram name for its | 77 // query are returned. The query must be a substring of histogram name for its |
77 // pointer to be copied. | 78 // pointer to be copied. |
78 static void GetSnapshot(const std::string& query, Histograms* snapshot); | 79 static void GetSnapshot(const std::string& query, Histograms* snapshot); |
79 | 80 |
80 | |
81 private: | 81 private: |
82 // We keep all registered histograms in a map, from name to histogram. | 82 // We keep all registered histograms in a map, from name to histogram. |
83 typedef std::map<std::string, Histogram*> HistogramMap; | 83 typedef std::map<std::string, Histogram*> HistogramMap; |
84 | 84 |
85 // We keep all |cached_ranges_| in a map, from checksum to a list of | 85 // We keep all |cached_ranges_| in a map, from checksum to a list of |
86 // |cached_ranges_|. Checksum is calculated from the |ranges_| in | 86 // |cached_ranges_|. Checksum is calculated from the |ranges_| in |
87 // |cached_ranges_|. | 87 // |cached_ranges_|. |
88 typedef std::map<uint32, std::list<CachedRanges*>*> RangesMap; | 88 typedef std::map<uint32, std::list<CachedRanges*>*> RangesMap; |
89 | 89 |
| 90 friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; |
| 91 |
| 92 // Allow tests to access our innards for testing purposes. |
| 93 FRIEND_TEST_ALL_PREFIXES(HistogramTest, StartupShutdownTest); |
| 94 FRIEND_TEST_ALL_PREFIXES(HistogramTest, RecordedStartupTest); |
| 95 FRIEND_TEST_ALL_PREFIXES(HistogramTest, RangeTest); |
| 96 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CustomRangeTest); |
| 97 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CachedRangesTest); |
| 98 |
| 99 StatisticsRecorder(); |
| 100 |
| 101 ~StatisticsRecorder(); |
| 102 |
90 static HistogramMap* histograms_; | 103 static HistogramMap* histograms_; |
91 | 104 |
92 static RangesMap* ranges_; | 105 static RangesMap* ranges_; |
93 | 106 |
94 // lock protects access to the above map. | 107 // lock protects access to the above map. |
95 static base::Lock* lock_; | 108 static base::Lock* lock_; |
96 | 109 |
97 // Dump all known histograms to log. | 110 // Dump all known histograms to log. |
98 static bool dump_on_exit_; | 111 static bool dump_on_exit_; |
99 | 112 |
100 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); | 113 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); |
101 }; | 114 }; |
102 | 115 |
103 } // namespace base | 116 } // namespace base |
104 | 117 |
105 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ | 118 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ |
OLD | NEW |