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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
base / metrics / statistics_recorder.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/metrics/statistics_recorder.h"
#include <string_view>
#include "base/at_exit.h"
#include "base/barrier_closure.h"
#include "base/containers/contains.h"
#include "base/debug/leak_annotations.h"
#include "base/json/string_escape.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_snapshot_manager.h"
#include "base/metrics/metrics_hashes.h"
#include "base/metrics/persistent_histogram_allocator.h"
#include "base/metrics/record_histogram_checker.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "build/build_config.h"
namespace base {
namespace {
bool HistogramNameLesser(const base::HistogramBase* a,
const base::HistogramBase* b) {
return strcmp(a->histogram_name(), b->histogram_name()) < 0;
}
} // namespace
// static
LazyInstance<Lock>::Leaky StatisticsRecorder::lock_ = LAZY_INSTANCE_INITIALIZER;
// static
StatisticsRecorder* StatisticsRecorder::top_ = nullptr;
// static
bool StatisticsRecorder::is_vlog_initialized_ = false;
// static
std::atomic<bool> StatisticsRecorder::have_active_callbacks_{false};
// static
std::atomic<StatisticsRecorder::GlobalSampleCallback>
StatisticsRecorder::global_sample_callback_{nullptr};
StatisticsRecorder::ScopedHistogramSampleObserver::
ScopedHistogramSampleObserver(const std::string& name,
OnSampleCallback callback)
: histogram_name_(name), callback_(callback) {
StatisticsRecorder::AddHistogramSampleObserver(histogram_name_, this);
}
StatisticsRecorder::ScopedHistogramSampleObserver::
~ScopedHistogramSampleObserver() {
StatisticsRecorder::RemoveHistogramSampleObserver(histogram_name_, this);
}
void StatisticsRecorder::ScopedHistogramSampleObserver::RunCallback(
const char* histogram_name,
uint64_t name_hash,
HistogramBase::Sample sample) {
callback_.Run(histogram_name, name_hash, sample);
}
StatisticsRecorder::~StatisticsRecorder() {
const AutoLock auto_lock(GetLock());
DCHECK_EQ(this, top_);
top_ = previous_;
}
// static
void StatisticsRecorder::EnsureGlobalRecorderWhileLocked() {
AssertLockHeld();
if (top_) {
return;
}
const StatisticsRecorder* const p = new StatisticsRecorder;
// The global recorder is never deleted.
ANNOTATE_LEAKING_OBJECT_PTR(p);
DCHECK_EQ(p, top_);
}
// static
void StatisticsRecorder::RegisterHistogramProvider(
const WeakPtr<HistogramProvider>& provider) {
const AutoLock auto_lock(GetLock());
EnsureGlobalRecorderWhileLocked();
top_->providers_.push_back(provider);
}
// static
HistogramBase* StatisticsRecorder::RegisterOrDeleteDuplicate(
HistogramBase* histogram) {
CHECK(histogram);
uint64_t hash = histogram->name_hash();
// Ensure that histograms use HashMetricName() to compute their hash, since
// that function is used to look up histograms. Intentionally a DCHECK since
// this is expensive.
DCHECK_EQ(hash, HashMetricName(histogram->histogram_name()));
// Declared before |auto_lock| so that the histogram is deleted after the lock
// is released (no point in holding the lock longer than needed).
std::unique_ptr<HistogramBase> histogram_deleter;
const AutoLock auto_lock(GetLock());
EnsureGlobalRecorderWhileLocked();
HistogramBase*& registered = top_->histograms_[hash];
if (!registered) {
registered = histogram;
ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322
// If there are callbacks for this histogram, we set the kCallbackExists
// flag.
if (base::Contains(top_->observers_, hash)) {
// Note: SetFlags() does not write to persistent memory, it only writes to
// an in-memory version of the flags.
histogram->SetFlags(HistogramBase::kCallbackExists);
}
return histogram;
}
// Assert that there was no collision. Note that this is intentionally a
// DCHECK because 1) this is expensive to call repeatedly, and 2) this
// comparison may cause a read in persistent memory, which can cause I/O (this
// is bad because |lock_| is currently being held).
//
// If you are a developer adding a new histogram and this DCHECK is being hit,
// you are unluckily a victim of a hash collision. For now, the best solution
// is to rename the histogram. Reach out to chrome-metrics-team@google.com if
// you are unsure!
DCHECK_EQ(strcmp(histogram->histogram_name(), registered->histogram_name()),
0)
<< "Histogram name hash collision between " << histogram->histogram_name()
<< " and " << registered->histogram_name() << " (hash = " << hash << ")";
if (histogram == registered) {
// The histogram was registered before.
return histogram;
}
// We already have a histogram with this name.
histogram_deleter.reset(histogram);
return registered;
}
// static
const BucketRanges* StatisticsRecorder::RegisterOrDeleteDuplicateRanges(
const BucketRanges* ranges) {
const BucketRanges* registered;
{
const AutoLock auto_lock(GetLock());
EnsureGlobalRecorderWhileLocked();
registered = top_->ranges_manager_.GetOrRegisterCanonicalRanges(ranges);
}
// Delete the duplicate ranges outside the lock to reduce contention.
if (registered != ranges) {
delete ranges;
} else {
ANNOTATE_LEAKING_OBJECT_PTR(ranges);
}
return registered;
}
// static
void StatisticsRecorder::WriteGraph(const std::string& query,
std::string* output) {
if (query.length())
StringAppendF(output, "Collections of histograms for %s\n", query.c_str());
else
output->append("Collections of all histograms\n");
for (const HistogramBase* const histogram :
Sort(WithName(GetHistograms(), query))) {
histogram->WriteAscii(output);
output->append("\n");
}
}
// static
std::string StatisticsRecorder::ToJSON(JSONVerbosityLevel verbosity_level) {
std::string output = "{\"histograms\":[";
const char* sep = "";
for (const HistogramBase* const histogram : Sort(GetHistograms())) {
output += sep;
sep = ",";
std::string json;
histogram->WriteJSON(&json, verbosity_level);
output += json;
}
output += "]}";
return output;
}
// static
std::vector<const BucketRanges*> StatisticsRecorder::GetBucketRanges() {
const AutoLock auto_lock(GetLock());
// Manipulate |top_| through a const variable to ensure it is not mutated.
const auto* const_top = top_;
if (!const_top) {
return std::vector<const BucketRanges*>();
}
return const_top->ranges_manager_.GetBucketRanges();
}
// static
HistogramBase* StatisticsRecorder::FindHistogram(std::string_view name) {
uint64_t hash = HashMetricName(name);
// This must be called *before* the lock is acquired below because it may call
// back into StatisticsRecorder to register histograms. Those called methods
// will acquire the lock at that time.
ImportGlobalPersistentHistograms();
const AutoLock auto_lock(GetLock());
// Manipulate |top_| through a const variable to ensure it is not mutated.
const auto* const_top = top_;
if (!const_top) {
return nullptr;
}
return const_top->FindHistogramByHashInternal(hash, name);
}
// static
StatisticsRecorder::HistogramProviders
StatisticsRecorder::GetHistogramProviders() {
const AutoLock auto_lock(GetLock());
// Manipulate |top_| through a const variable to ensure it is not mutated.
const auto* const_top = top_;
if (!const_top) {
return StatisticsRecorder::HistogramProviders();
}
return const_top->providers_;
}
// static
void StatisticsRecorder::ImportProvidedHistograms(bool async,
OnceClosure done_callback) {
// Merge histogram data from each provider in turn.
HistogramProviders providers = GetHistogramProviders();
auto barrier_callback =
BarrierClosure(providers.size(), std::move(done_callback));
for (const WeakPtr<HistogramProvider>& provider : providers) {
// Weak-pointer may be invalid if the provider was destructed, though they
// generally never are.
if (!provider) {
barrier_callback.Run();
continue;
}
provider->MergeHistogramDeltas(async, barrier_callback);
}
}
// static
void StatisticsRecorder::ImportProvidedHistogramsSync() {
ImportProvidedHistograms(/*async=*/false, /*done_callback=*/DoNothing());
}
// static
void StatisticsRecorder::PrepareDeltas(
bool include_persistent,
HistogramBase::Flags flags_to_set,
HistogramBase::Flags required_flags,
HistogramSnapshotManager* snapshot_manager) {
Histograms histograms = Sort(GetHistograms(include_persistent));
snapshot_manager->PrepareDeltas(std::move(histograms), flags_to_set,
required_flags);
}
// static
void StatisticsRecorder::InitLogOnShutdown() {
const AutoLock auto_lock(GetLock());
InitLogOnShutdownWhileLocked();
}
HistogramBase* StatisticsRecorder::FindHistogramByHashInternal(
uint64_t hash,
std::string_view name) const {
AssertLockHeld();
const HistogramMap::const_iterator it = histograms_.find(hash);
if (it == histograms_.end()) {
return nullptr;
}
// Assert that there was no collision. Note that this is intentionally a
// DCHECK because 1) this is expensive to call repeatedly, and 2) this
// comparison may cause a read in persistent memory, which can cause I/O (this
// is bad because |lock_| is currently being held).
//
// If you are a developer adding a new histogram and this DCHECK is being hit,
// you are unluckily a victim of a hash collision. For now, the best solution
// is to rename the histogram. Reach out to chrome-metrics-team@google.com if
// you are unsure!
DCHECK_EQ(name, it->second->histogram_name())
<< "Histogram name hash collision between " << name << " and "
<< it->second->histogram_name() << " (hash = " << hash << ")";
return it->second;
}
// static
void StatisticsRecorder::AddHistogramSampleObserver(
const std::string& name,
StatisticsRecorder::ScopedHistogramSampleObserver* observer) {
DCHECK(observer);
uint64_t hash = HashMetricName(name);
const AutoLock auto_lock(GetLock());
EnsureGlobalRecorderWhileLocked();
auto iter = top_->observers_.find(hash);
if (iter == top_->observers_.end()) {
top_->observers_.insert(
{hash, base::MakeRefCounted<HistogramSampleObserverList>(
ObserverListPolicy::EXISTING_ONLY)});
}
top_->observers_[hash]->AddObserver(observer);
HistogramBase* histogram = top_->FindHistogramByHashInternal(hash, name);
if (histogram) {
// Note: SetFlags() does not write to persistent memory, it only writes to
// an in-memory version of the flags.
histogram->SetFlags(HistogramBase::kCallbackExists);
}
have_active_callbacks_.store(
global_sample_callback() || !top_->observers_.empty(),
std::memory_order_relaxed);
}
// static
void StatisticsRecorder::RemoveHistogramSampleObserver(
const std::string& name,
StatisticsRecorder::ScopedHistogramSampleObserver* observer) {
uint64_t hash = HashMetricName(name);
const AutoLock auto_lock(GetLock());
EnsureGlobalRecorderWhileLocked();
auto iter = top_->observers_.find(hash);
CHECK(iter != top_->observers_.end(), base::NotFatalUntil::M125);
auto result = iter->second->RemoveObserver(observer);
if (result ==
HistogramSampleObserverList::RemoveObserverResult::kWasOrBecameEmpty) {
top_->observers_.erase(hash);
// We also clear the flag from the histogram (if it exists).
HistogramBase* histogram = top_->FindHistogramByHashInternal(hash, name);
if (histogram) {
// Note: ClearFlags() does not write to persistent memory, it only writes
// to an in-memory version of the flags.
histogram->ClearFlags(HistogramBase::kCallbackExists);
}
}
have_active_callbacks_.store(
global_sample_callback() || !top_->observers_.empty(),
std::memory_order_relaxed);
}
// static
void StatisticsRecorder::FindAndRunHistogramCallbacks(
base::PassKey<HistogramBase>,
const char* histogram_name,
uint64_t name_hash,
HistogramBase::Sample sample) {
DCHECK_EQ(name_hash, HashMetricName(histogram_name));
const AutoLock auto_lock(GetLock());
// Manipulate |top_| through a const variable to ensure it is not mutated.
const auto* const_top = top_;
if (!const_top) {
return;
}
auto it = const_top->observers_.find(name_hash);
// Ensure that this observer is still registered, as it might have been
// unregistered before we acquired the lock.
if (it == const_top->observers_.end()) {
return;
}
it->second->Notify(FROM_HERE, &ScopedHistogramSampleObserver::RunCallback,
histogram_name, name_hash, sample);
}
// static
void StatisticsRecorder::SetGlobalSampleCallback(
const GlobalSampleCallback& new_global_sample_callback) {
const AutoLock auto_lock(GetLock());
EnsureGlobalRecorderWhileLocked();
DCHECK(!global_sample_callback() || !new_global_sample_callback);
global_sample_callback_.store(new_global_sample_callback);
have_active_callbacks_.store(
new_global_sample_callback || !top_->observers_.empty(),
std::memory_order_relaxed);
}
// static
size_t StatisticsRecorder::GetHistogramCount() {
const AutoLock auto_lock(GetLock());
// Manipulate |top_| through a const variable to ensure it is not mutated.
const auto* const_top = top_;
if (!const_top) {
return 0;
}
return const_top->histograms_.size();
}
// static
void StatisticsRecorder::ForgetHistogramForTesting(std::string_view name) {
const AutoLock auto_lock(GetLock());
EnsureGlobalRecorderWhileLocked();
uint64_t hash = HashMetricName(name);
HistogramBase* base = top_->FindHistogramByHashInternal(hash, name);
if (!base) {
return;
}
if (base->GetHistogramType() != SPARSE_HISTOGRAM) {
// When forgetting a histogram, it's likely that other information is also
// becoming invalid. Clear the persistent reference that may no longer be
// valid. There's no danger in this as, at worst, duplicates will be created
// in persistent memory.
static_cast<Histogram*>(base)->bucket_ranges()->set_persistent_reference(0);
}
// This performs another lookup in the map, but this is fine since this is
// only used in tests.
top_->histograms_.erase(hash);
}
// static
std::unique_ptr<StatisticsRecorder>
StatisticsRecorder::CreateTemporaryForTesting() {
const AutoLock auto_lock(GetLock());
std::unique_ptr<StatisticsRecorder> temporary_recorder =
WrapUnique(new StatisticsRecorder());
temporary_recorder->ranges_manager_
.DoNotReleaseRangesOnDestroyForTesting(); // IN-TEST
return temporary_recorder;
}
// static
void StatisticsRecorder::SetRecordChecker(
std::unique_ptr<RecordHistogramChecker> record_checker) {
const AutoLock auto_lock(GetLock());
EnsureGlobalRecorderWhileLocked();
top_->record_checker_ = std::move(record_checker);
}
// static
bool StatisticsRecorder::ShouldRecordHistogram(uint32_t histogram_hash) {
const AutoLock auto_lock(GetLock());
// Manipulate |top_| through a const variable to ensure it is not mutated.
const auto* const_top = top_;
return !const_top || !const_top->record_checker_ ||
const_top->record_checker_->ShouldRecord(histogram_hash);
}
// static
StatisticsRecorder::Histograms StatisticsRecorder::GetHistograms(
bool include_persistent) {
// This must be called *before* the lock is acquired below because it will
// call back into this object to register histograms. Those called methods
// will acquire the lock at that time.
ImportGlobalPersistentHistograms();
Histograms out;
const AutoLock auto_lock(GetLock());
// Manipulate |top_| through a const variable to ensure it is not mutated.
const auto* const_top = top_;
if (!const_top) {
return out;
}
out.reserve(const_top->histograms_.size());
for (const auto& entry : const_top->histograms_) {
// Note: HasFlags() does not read to persistent memory, it only reads an
// in-memory version of the flags.
bool is_persistent = entry.second->HasFlags(HistogramBase::kIsPersistent);
if (!include_persistent && is_persistent) {
continue;
}
out.push_back(entry.second);
}
return out;
}
// static
StatisticsRecorder::Histograms StatisticsRecorder::Sort(Histograms histograms) {
ranges::sort(histograms, &HistogramNameLesser);
return histograms;
}
// static
StatisticsRecorder::Histograms StatisticsRecorder::WithName(
Histograms histograms,
const std::string& query,
bool case_sensitive) {
// Need a C-string query for comparisons against C-string histogram name.
std::string lowercase_query;
const char* query_string;
if (case_sensitive) {
query_string = query.c_str();
} else {
lowercase_query = base::ToLowerASCII(query);
query_string = lowercase_query.c_str();
}
histograms.erase(
ranges::remove_if(
histograms,
[query_string, case_sensitive](const HistogramBase* const h) {
return !strstr(
case_sensitive
? h->histogram_name()
: base::ToLowerASCII(h->histogram_name()).c_str(),
query_string);
}),
histograms.end());
return histograms;
}
// static
void StatisticsRecorder::ImportGlobalPersistentHistograms() {
// Import histograms from known persistent storage. Histograms could have been
// added by other processes and they must be fetched and recognized locally.
// If the persistent memory segment is not shared between processes, this call
// does nothing.
if (GlobalHistogramAllocator* allocator = GlobalHistogramAllocator::Get())
allocator->ImportHistogramsToStatisticsRecorder();
}
StatisticsRecorder::StatisticsRecorder() {
AssertLockHeld();
previous_ = top_;
top_ = this;
InitLogOnShutdownWhileLocked();
}
// static
void StatisticsRecorder::InitLogOnShutdownWhileLocked() {
AssertLockHeld();
if (!is_vlog_initialized_ && VLOG_IS_ON(1)) {
is_vlog_initialized_ = true;
const auto dump_to_vlog = [](void*) {
std::string output;
WriteGraph("", &output);
VLOG(1) << output;
};
AtExitManager::RegisterCallback(dump_to_vlog, nullptr);
}
}
} // namespace base