1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
// Copyright (c) 2012 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.
// StatisticsRecorder holds all Histograms and BucketRanges that are used by
// Histograms in the system. It provides a general place for
// Histograms/BucketRanges to register, and supports a global API for accessing
// (i.e., dumping, or graphing) the data.
#ifndef BASE_METRICS_STATISTICS_RECORDER_H_
#define BASE_METRICS_STATISTICS_RECORDER_H_
#include <list>
#include <map>
#include <string>
#include <vector>
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/gtest_prod_util.h"
#include "base/lazy_instance.h"
namespace base {
class BucketRanges;
class HistogramBase;
class Lock;
class BASE_EXPORT StatisticsRecorder {
public:
typedef std::vector<HistogramBase*> Histograms;
// Initializes the StatisticsRecorder system. Safe to call multiple times.
static void Initialize();
// Find out if histograms can now be registered into our list.
static bool IsActive();
// Register, or add a new histogram to the collection of statistics. If an
// identically named histogram is already registered, then the argument
// |histogram| will deleted. The returned value is always the registered
// histogram (either the argument, or the pre-existing registered histogram).
static HistogramBase* RegisterOrDeleteDuplicate(HistogramBase* histogram);
// Register, or add a new BucketRanges. If an identically BucketRanges is
// already registered, then the argument |ranges| will deleted. The returned
// value is always the registered BucketRanges (either the argument, or the
// pre-existing one).
static const BucketRanges* RegisterOrDeleteDuplicateRanges(
const BucketRanges* ranges);
// Methods for appending histogram data to a string. Only histograms which
// have |query| as a substring are written to |output| (an empty string will
// process all registered histograms).
static void WriteHTMLGraph(const std::string& query, std::string* output);
static void WriteGraph(const std::string& query, std::string* output);
// Returns the histograms with |query| as a substring as JSON text (an empty
// |query| will process all registered histograms).
static std::string ToJSON(const std::string& query);
// Method for extracting histograms which were marked for use by UMA.
static void GetHistograms(Histograms* output);
// Method for extracting BucketRanges used by all histograms registered.
static void GetBucketRanges(std::vector<const BucketRanges*>* output);
// Find a histogram by name. It matches the exact name. This method is thread
// safe. It returns NULL if a matching histogram is not found.
static HistogramBase* FindHistogram(const std::string& name);
// GetSnapshot copies some of the pointers to registered histograms into the
// caller supplied vector (Histograms). Only histograms which have |query| as
// a substring are copied (an empty string will process all registered
// histograms).
static void GetSnapshot(const std::string& query, Histograms* snapshot);
private:
// HistogramNameRef holds a weak const ref to the name field of the associated
// Histogram object, allowing re-use of the underlying string storage for the
// map keys. The wrapper is required as using "const std::string&" as the key
// results in compile errors.
struct HistogramNameRef {
explicit HistogramNameRef(const std::string& name) : name_(name) {};
// Operator < is necessary to use this type as a std::map key.
bool operator<(const HistogramNameRef& other) const {
return name_ < other.name_;
}
// Weak, owned by the associated Histogram object.
const std::string& name_;
};
// We keep all registered histograms in a map, from name to histogram.
typedef std::map<HistogramNameRef, HistogramBase*> HistogramMap;
// We keep all |bucket_ranges_| in a map, from checksum to a list of
// |bucket_ranges_|. Checksum is calculated from the |ranges_| in
// |bucket_ranges_|.
typedef std::map<uint32, std::list<const BucketRanges*>*> RangesMap;
friend struct DefaultLazyInstanceTraits<StatisticsRecorder>;
friend class HistogramBaseTest;
friend class HistogramSnapshotManagerTest;
friend class HistogramTest;
friend class JsonPrefStoreTest;
friend class SparseHistogramTest;
friend class StatisticsRecorderTest;
FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest,
DeserializeHistogramAndAddSamples);
// The constructor just initializes static members. Usually client code should
// use Initialize to do this. But in test code, you can friend this class and
// call destructor/constructor to get a clean StatisticsRecorder.
StatisticsRecorder();
~StatisticsRecorder();
static void DumpHistogramsToVlog(void* instance);
static HistogramMap* histograms_;
static RangesMap* ranges_;
// Lock protects access to above maps.
static base::Lock* lock_;
DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder);
};
} // namespace base
#endif // BASE_METRICS_STATISTICS_RECORDER_H_
|