Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: base/metrics/sparse_histogram.cc

Issue 1734033003: Add support for persistent sparse histograms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/sparse_histogram.h ('k') | base/metrics/sparse_histogram_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "base/metrics/sparse_histogram.h" 5 #include "base/metrics/sparse_histogram.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/metrics/metrics_hashes.h" 9 #include "base/metrics/metrics_hashes.h"
10 #include "base/metrics/persistent_histogram_allocator.h"
11 #include "base/metrics/persistent_sample_map.h"
10 #include "base/metrics/sample_map.h" 12 #include "base/metrics/sample_map.h"
11 #include "base/metrics/statistics_recorder.h" 13 #include "base/metrics/statistics_recorder.h"
12 #include "base/pickle.h" 14 #include "base/pickle.h"
13 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
14 #include "base/synchronization/lock.h" 16 #include "base/synchronization/lock.h"
15 17
16 namespace base { 18 namespace base {
17 19
18 typedef HistogramBase::Count Count; 20 typedef HistogramBase::Count Count;
19 typedef HistogramBase::Sample Sample; 21 typedef HistogramBase::Sample Sample;
20 22
21 // static 23 // static
22 HistogramBase* SparseHistogram::FactoryGet(const std::string& name, 24 HistogramBase* SparseHistogram::FactoryGet(const std::string& name,
23 int32_t flags) { 25 int32_t flags) {
26 // Import histograms from known persistent storage. Histograms could have
27 // been added by other processes and they must be fetched and recognized
28 // locally in order to be found by FindHistograms() below. If the persistent
29 // memory segment is not shared between processes, this call does nothing.
30 PersistentHistogramAllocator::ImportGlobalHistograms();
31
24 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name); 32 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
33 if (!histogram) {
34 // Try to create the histogram using a "persistent" allocator. As of
35 // 2016-02-25, the availability of such is controlled by a base::Feature
36 // that is off by default. If the allocator doesn't exist or if
37 // allocating from it fails, code below will allocate the histogram from
38 // the process heap.
39 PersistentMemoryAllocator::Reference histogram_ref = 0;
40 scoped_ptr<HistogramBase> tentative_histogram;
41 PersistentHistogramAllocator* allocator =
42 PersistentHistogramAllocator::GetGlobalAllocator();
43 if (allocator) {
44 tentative_histogram = allocator->AllocateHistogram(
45 SPARSE_HISTOGRAM, name, 0, 0, nullptr, flags, &histogram_ref);
46 }
25 47
26 if (!histogram) { 48 // Handle the case where no persistent allocator is present or the
27 // To avoid racy destruction at shutdown, the following will be leaked. 49 // persistent allocation fails (perhaps because it is full).
28 HistogramBase* tentative_histogram = new SparseHistogram(name); 50 if (!tentative_histogram) {
29 tentative_histogram->SetFlags(flags); 51 DCHECK(!histogram_ref); // Should never have been set.
30 histogram = 52 DCHECK(!allocator); // Shouldn't have failed.
31 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); 53 flags &= ~HistogramBase::kIsPersistent;
54 tentative_histogram.reset(new SparseHistogram(name));
55 tentative_histogram->SetFlags(flags);
56 }
57
58 // Register this histogram with the StatisticsRecorder. Keep a copy of
59 // the pointer value to tell later whether the locally created histogram
60 // was registered or deleted. The type is "void" because it could point
61 // to released memory after the following line.
62 const void* tentative_histogram_ptr = tentative_histogram.get();
63 histogram = StatisticsRecorder::RegisterOrDeleteDuplicate(
64 tentative_histogram.release());
65
66 // Persistent histograms need some follow-up processing.
67 if (histogram_ref) {
68 allocator->FinalizeHistogram(histogram_ref,
69 histogram == tentative_histogram_ptr);
70 }
71
32 ReportHistogramActivity(*histogram, HISTOGRAM_CREATED); 72 ReportHistogramActivity(*histogram, HISTOGRAM_CREATED);
33 } else { 73 } else {
34 ReportHistogramActivity(*histogram, HISTOGRAM_LOOKUP); 74 ReportHistogramActivity(*histogram, HISTOGRAM_LOOKUP);
35 } 75 }
76
36 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType()); 77 DCHECK_EQ(SPARSE_HISTOGRAM, histogram->GetHistogramType());
37 return histogram; 78 return histogram;
38 } 79 }
39 80
81 // static
82 scoped_ptr<HistogramBase> SparseHistogram::PersistentCreate(
83 PersistentMemoryAllocator* allocator,
84 const std::string& name,
85 HistogramSamples::Metadata* meta,
86 HistogramSamples::Metadata* logged_meta) {
87 return make_scoped_ptr(
88 new SparseHistogram(allocator, name, meta, logged_meta));
89 }
90
40 SparseHistogram::~SparseHistogram() {} 91 SparseHistogram::~SparseHistogram() {}
41 92
42 uint64_t SparseHistogram::name_hash() const { 93 uint64_t SparseHistogram::name_hash() const {
43 return samples_.id(); 94 return samples_->id();
44 } 95 }
45 96
46 HistogramType SparseHistogram::GetHistogramType() const { 97 HistogramType SparseHistogram::GetHistogramType() const {
47 return SPARSE_HISTOGRAM; 98 return SPARSE_HISTOGRAM;
48 } 99 }
49 100
50 bool SparseHistogram::HasConstructionArguments( 101 bool SparseHistogram::HasConstructionArguments(
51 Sample expected_minimum, 102 Sample expected_minimum,
52 Sample expected_maximum, 103 Sample expected_maximum,
53 uint32_t expected_bucket_count) const { 104 uint32_t expected_bucket_count) const {
54 // SparseHistogram never has min/max/bucket_count limit. 105 // SparseHistogram never has min/max/bucket_count limit.
55 return false; 106 return false;
56 } 107 }
57 108
58 void SparseHistogram::Add(Sample value) { 109 void SparseHistogram::Add(Sample value) {
59 AddCount(value, 1); 110 AddCount(value, 1);
60 } 111 }
61 112
62 void SparseHistogram::AddCount(Sample value, int count) { 113 void SparseHistogram::AddCount(Sample value, int count) {
63 if (count <= 0) { 114 if (count <= 0) {
64 NOTREACHED(); 115 NOTREACHED();
65 return; 116 return;
66 } 117 }
67 { 118 {
68 base::AutoLock auto_lock(lock_); 119 base::AutoLock auto_lock(lock_);
69 samples_.Accumulate(value, count); 120 samples_->Accumulate(value, count);
70 } 121 }
71 122
72 FindAndRunCallback(value); 123 FindAndRunCallback(value);
73 } 124 }
74 125
75 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const { 126 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotSamples() const {
76 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); 127 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash()));
77 128
78 base::AutoLock auto_lock(lock_); 129 base::AutoLock auto_lock(lock_);
79 snapshot->Add(samples_); 130 snapshot->Add(*samples_);
80 return std::move(snapshot); 131 return std::move(snapshot);
81 } 132 }
82 133
83 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotDelta() { 134 scoped_ptr<HistogramSamples> SparseHistogram::SnapshotDelta() {
84 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash())); 135 scoped_ptr<SampleMap> snapshot(new SampleMap(name_hash()));
85 base::AutoLock auto_lock(lock_); 136 base::AutoLock auto_lock(lock_);
86 snapshot->Add(samples_); 137 snapshot->Add(*samples_);
87 138
88 // Subtract what was previously logged and update that information. 139 // Subtract what was previously logged and update that information.
89 snapshot->Subtract(logged_samples_); 140 snapshot->Subtract(*logged_samples_);
90 logged_samples_.Add(*snapshot); 141 logged_samples_->Add(*snapshot);
91 return std::move(snapshot); 142 return std::move(snapshot);
92 } 143 }
93 144
94 void SparseHistogram::AddSamples(const HistogramSamples& samples) { 145 void SparseHistogram::AddSamples(const HistogramSamples& samples) {
95 base::AutoLock auto_lock(lock_); 146 base::AutoLock auto_lock(lock_);
96 samples_.Add(samples); 147 samples_->Add(samples);
97 } 148 }
98 149
99 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) { 150 bool SparseHistogram::AddSamplesFromPickle(PickleIterator* iter) {
100 base::AutoLock auto_lock(lock_); 151 base::AutoLock auto_lock(lock_);
101 return samples_.AddFromPickle(iter); 152 return samples_->AddFromPickle(iter);
102 } 153 }
103 154
104 void SparseHistogram::WriteHTMLGraph(std::string* output) const { 155 void SparseHistogram::WriteHTMLGraph(std::string* output) const {
105 output->append("<PRE>"); 156 output->append("<PRE>");
106 WriteAsciiImpl(true, "<br>", output); 157 WriteAsciiImpl(true, "<br>", output);
107 output->append("</PRE>"); 158 output->append("</PRE>");
108 } 159 }
109 160
110 void SparseHistogram::WriteAscii(std::string* output) const { 161 void SparseHistogram::WriteAscii(std::string* output) const {
111 WriteAsciiImpl(true, "\n", output); 162 WriteAsciiImpl(true, "\n", output);
112 } 163 }
113 164
114 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const { 165 bool SparseHistogram::SerializeInfoImpl(Pickle* pickle) const {
115 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags()); 166 return pickle->WriteString(histogram_name()) && pickle->WriteInt(flags());
116 } 167 }
117 168
118 SparseHistogram::SparseHistogram(const std::string& name) 169 SparseHistogram::SparseHistogram(const std::string& name)
119 : HistogramBase(name), 170 : HistogramBase(name),
120 samples_(HashMetricName(name)) {} 171 samples_(new SampleMap(HashMetricName(name))),
172 logged_samples_(new SampleMap(samples_->id())) {}
173
174 SparseHistogram::SparseHistogram(PersistentMemoryAllocator* allocator,
175 const std::string& name,
176 HistogramSamples::Metadata* meta,
177 HistogramSamples::Metadata* logged_meta)
178 : HistogramBase(name),
179 // While other histogram types maintain a static vector of values with
180 // sufficient space for both "active" and "logged" samples, with each
181 // SampleVector being given the appropriate half, sparse histograms
182 // have no such initial allocation. Each sample has its own record
183 // attached to a single PersistentSampleMap by a common 64-bit identifier.
184 // Since a sparse histogram has two sample maps (active and logged),
185 // there must be two sets of sample records with diffent IDs. The
186 // "active" samples use, for convenience purposes, an ID matching
187 // that of the histogram while the "logged" samples use that number
188 // plus 1.
189 samples_(new PersistentSampleMap(HashMetricName(name), allocator, meta)),
190 logged_samples_(
191 new PersistentSampleMap(samples_->id() + 1, allocator, logged_meta)) {
192 }
121 193
122 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) { 194 HistogramBase* SparseHistogram::DeserializeInfoImpl(PickleIterator* iter) {
123 std::string histogram_name; 195 std::string histogram_name;
124 int flags; 196 int flags;
125 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) { 197 if (!iter->ReadString(&histogram_name) || !iter->ReadInt(&flags)) {
126 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; 198 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name;
127 return NULL; 199 return NULL;
128 } 200 }
129 201
130 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag); 202 DCHECK(flags & HistogramBase::kIPCSerializationSourceFlag);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 std::string* output) const { 272 std::string* output) const {
201 StringAppendF(output, 273 StringAppendF(output,
202 "Histogram: %s recorded %d samples", 274 "Histogram: %s recorded %d samples",
203 histogram_name().c_str(), 275 histogram_name().c_str(),
204 total_count); 276 total_count);
205 if (flags() & ~kHexRangePrintingFlag) 277 if (flags() & ~kHexRangePrintingFlag)
206 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag); 278 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
207 } 279 }
208 280
209 } // namespace base 281 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/sparse_histogram.h ('k') | base/metrics/sparse_histogram_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698