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

Side by Side Diff: chrome/browser/metrics/subprocess_metrics_provider.cc

Issue 1671933002: Create and pass shared-histogram-allocator from browser to renderer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@hsm-merge
Patch Set: addressed review comments by Alexei; fixed test on some builds Created 4 years, 8 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
OLDNEW
(Empty)
1 // Copyright 2016 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
5 #include "chrome/browser/metrics/subprocess_metrics_provider.h"
6
7 #include "base/logging.h"
8 #include "base/metrics/histogram_base.h"
9 #include "base/metrics/histogram_macros.h"
10 #include "base/metrics/persistent_histogram_allocator.h"
11 #include "base/metrics/persistent_memory_allocator.h"
12 #include "components/metrics/metrics_service.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/browser/notification_types.h"
15 #include "content/public/browser/render_process_host.h"
16
17 SubprocessMetricsProvider::SubprocessMetricsProvider()
18 : scoped_observer_(this) {
19 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
20 content::NotificationService::AllBrowserContextsAndSources());
21 }
22
23 SubprocessMetricsProvider::~SubprocessMetricsProvider() {}
24
25 void SubprocessMetricsProvider::RegisterSubprocessAllocator(
26 int id,
27 scoped_ptr<base::PersistentHistogramAllocator> allocator) {
28 DCHECK(thread_checker_.CalledOnValidThread());
29 DCHECK(!allocators_by_id_.Lookup(id));
30
31 // Map is "MapOwnPointer" so transfer ownership to it.
32 allocators_by_id_.AddWithID(allocator.release(), id);
33 }
34
35 void SubprocessMetricsProvider::DeregisterSubprocessAllocator(int id) {
36 DCHECK(thread_checker_.CalledOnValidThread());
37
38 if (!allocators_by_id_.Lookup(id))
39 return;
40
41 // Extract the matching allocator from the list of active ones.
42 scoped_ptr<base::PersistentHistogramAllocator> allocator(
43 allocators_by_id_.Replace(id, nullptr));
44 allocators_by_id_.Remove(id);
45 DCHECK(allocator);
46
47 // If metrics recording is enabled, transfer the allocator to the "release"
48 // list. The allocator will continue to live (and keep the associated shared
49 // memory alive) until the next upload after which it will be released.
50 // Otherwise, the allocator and its memory will be released when the
51 // scoped_ptr goes out of scope at the end of this method.
52 if (metrics_recording_enabled_)
53 allocators_to_release_.push_back(std::move(allocator));
54 }
55
56 void SubprocessMetricsProvider::OnRecordingEnabled() {
57 DCHECK(thread_checker_.CalledOnValidThread());
58
59 metrics_recording_enabled_ = true;
60 }
61
62 void SubprocessMetricsProvider::OnRecordingDisabled() {
63 DCHECK(thread_checker_.CalledOnValidThread());
64
65 metrics_recording_enabled_ = false;
66 allocators_to_release_.clear();
67 }
68
69 void SubprocessMetricsProvider::RecordHistogramSnapshotsFromAllocator(
70 base::HistogramSnapshotManager* snapshot_manager,
71 int id,
72 base::PersistentHistogramAllocator* allocator) {
73 DCHECK(allocator);
74
75 int histogram_count = 0;
76 base::PersistentHistogramAllocator::Iterator hist_iter;
77 allocator->CreateIterator(&hist_iter);
78 while (true) {
79 scoped_ptr<base::HistogramBase> histogram =
80 allocator->GetNextHistogram(&hist_iter);
81 if (!histogram)
82 break;
83 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram));
84 ++histogram_count;
85 }
86
87 DVLOG(1) << "Reported " << histogram_count << " histograms from subprocess #"
88 << id;
89 }
90
91 void SubprocessMetricsProvider::RecordHistogramSnapshots(
92 base::HistogramSnapshotManager* snapshot_manager) {
93 DCHECK(thread_checker_.CalledOnValidThread());
94
95 for (AllocatorByIdMap::iterator iter(&allocators_by_id_); !iter.IsAtEnd();
96 iter.Advance()) {
97 RecordHistogramSnapshotsFromAllocator(
98 snapshot_manager, iter.GetCurrentKey(), iter.GetCurrentValue());
99 }
100
101 for (auto& allocator : allocators_to_release_)
102 RecordHistogramSnapshotsFromAllocator(snapshot_manager, 0, allocator.get());
103
104 UMA_HISTOGRAM_COUNTS_100(
105 "UMA.SubprocessMetricsProvider.SubproccessCount",
106 allocators_by_id_.size() + allocators_to_release_.size());
107
108 // The snapshot-manager has taken ownership of the histograms but needs
109 // access to only the histogram objects, not "sample" data it uses. Thus,
110 // it is safe to release shared-memory segments without waiting for the
111 // snapshot-manager to "finish".
112 allocators_to_release_.clear();
113 }
114
115 void SubprocessMetricsProvider::Observe(
116 int type,
117 const content::NotificationSource& source,
118 const content::NotificationDetails& details) {
119 DCHECK(thread_checker_.CalledOnValidThread());
120 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type);
121
122 content::RenderProcessHost* host =
123 content::Source<content::RenderProcessHost>(source).ptr();
124
125 // Sometimes, the same host will cause multiple notifications in tests so
126 // could possibly do the same in a release build.
127 if (!scoped_observer_.IsObserving(host))
128 scoped_observer_.Add(host);
129 }
130
131 void SubprocessMetricsProvider::RenderProcessReady(
132 content::RenderProcessHost* host) {
133 DCHECK(thread_checker_.CalledOnValidThread());
134
135 // If the render-process-host passed a persistent-memory-allocator to the
136 // renderer process, extract it and register it here.
137 scoped_ptr<base::SharedPersistentMemoryAllocator> allocator =
138 host->ExtractMetricsAllocator();
139 if (allocator) {
140 RegisterSubprocessAllocator(
141 host->GetID(),
142 make_scoped_ptr(new base::PersistentHistogramAllocator(
143 std::move(allocator))));
144 }
145 }
146
147 void SubprocessMetricsProvider::RenderProcessHostDestroyed(
148 content::RenderProcessHost* host) {
149 DCHECK(thread_checker_.CalledOnValidThread());
150
151 DeregisterSubprocessAllocator(host->GetID());
grt (UTC plus 2) 2016/04/01 14:22:48 since RegisterSubprocessAllocator happens once the
bcwhite 2016/04/01 16:50:26 I had that originally but there are cases where a
152 scoped_observer_.Remove(host);
153 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698