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

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: rebased 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(allocator);
77 while (true) {
78 scoped_ptr<base::HistogramBase> histogram = hist_iter.GetNext();
79 if (!histogram)
80 break;
81 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram));
82 ++histogram_count;
83 }
84
85 DVLOG(1) << "Reported " << histogram_count << " histograms from subprocess #"
86 << id;
87 }
88
89 void SubprocessMetricsProvider::RecordHistogramSnapshots(
90 base::HistogramSnapshotManager* snapshot_manager) {
91 DCHECK(thread_checker_.CalledOnValidThread());
92
93 for (AllocatorByIdMap::iterator iter(&allocators_by_id_); !iter.IsAtEnd();
94 iter.Advance()) {
95 RecordHistogramSnapshotsFromAllocator(
96 snapshot_manager, iter.GetCurrentKey(), iter.GetCurrentValue());
97 }
98
99 for (auto& allocator : allocators_to_release_)
100 RecordHistogramSnapshotsFromAllocator(snapshot_manager, 0, allocator.get());
101
102 UMA_HISTOGRAM_COUNTS_100(
103 "UMA.SubprocessMetricsProvider.SubprocessCount",
104 allocators_by_id_.size() + allocators_to_release_.size());
105
106 // The snapshot-manager has taken ownership of the histograms but needs
107 // access to only the histogram objects, not "sample" data it uses. Thus,
108 // it is safe to release shared-memory segments without waiting for the
109 // snapshot-manager to "finish".
110 allocators_to_release_.clear();
111 }
112
113 void SubprocessMetricsProvider::Observe(
114 int type,
115 const content::NotificationSource& source,
116 const content::NotificationDetails& details) {
117 DCHECK(thread_checker_.CalledOnValidThread());
118 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type);
119
120 content::RenderProcessHost* host =
121 content::Source<content::RenderProcessHost>(source).ptr();
122
123 // Sometimes, the same host will cause multiple notifications in tests so
124 // could possibly do the same in a release build.
125 if (!scoped_observer_.IsObserving(host))
126 scoped_observer_.Add(host);
127 }
128
129 void SubprocessMetricsProvider::RenderProcessReady(
130 content::RenderProcessHost* host) {
131 DCHECK(thread_checker_.CalledOnValidThread());
132
133 // If the render-process-host passed a persistent-memory-allocator to the
134 // renderer process, extract it and register it here.
135 scoped_ptr<base::SharedPersistentMemoryAllocator> allocator =
136 host->TakeMetricsAllocator();
137 if (allocator) {
138 RegisterSubprocessAllocator(
139 host->GetID(),
140 make_scoped_ptr(new base::PersistentHistogramAllocator(
141 std::move(allocator))));
142 }
143 }
144
145 void SubprocessMetricsProvider::RenderProcessExited(
146 content::RenderProcessHost* host,
147 base::TerminationStatus status,
148 int exit_code) {
149 DCHECK(thread_checker_.CalledOnValidThread());
150
151 DeregisterSubprocessAllocator(host->GetID());
152 }
153
154 void SubprocessMetricsProvider::RenderProcessHostDestroyed(
155 content::RenderProcessHost* host) {
156 DCHECK(thread_checker_.CalledOnValidThread());
157
158 // It's possible for a Renderer to terminate without RenderProcessExited
159 // (above) being called so it's necessary to de-register also upon the
160 // destruction of the host. If both get called, no harm is done.
161
162 DeregisterSubprocessAllocator(host->GetID());
163 scoped_observer_.Remove(host);
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698