OLD | NEW |
---|---|
(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 if (!allocators_by_id_.Lookup(id)) | |
38 return; | |
39 | |
40 // Transfer allocator from "active" list to "release" list. | |
41 allocators_to_release_.push_back( | |
42 make_scoped_ptr(allocators_by_id_.Replace(id, nullptr))); | |
43 allocators_by_id_.Remove(id); | |
44 } | |
45 | |
46 void SubprocessMetricsProvider::RecordHistogramSnapshotsFromAllocator( | |
47 base::HistogramSnapshotManager* snapshot_manager, | |
48 int id, | |
49 base::PersistentHistogramAllocator* allocator) { | |
50 DCHECK(allocator); | |
51 | |
52 int histogram_count = 0; | |
53 base::PersistentHistogramAllocator::Iterator hist_iter; | |
54 allocator->CreateIterator(&hist_iter); | |
55 while (true) { | |
56 scoped_ptr<base::HistogramBase> histogram = | |
57 allocator->GetNextHistogram(&hist_iter); | |
58 if (!histogram) | |
59 break; | |
60 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram)); | |
61 ++histogram_count; | |
62 } | |
63 | |
64 DVLOG(1) << "Reported " << histogram_count << " histograms from subprocess #" | |
65 << id; | |
66 } | |
67 | |
68 void SubprocessMetricsProvider::RecordHistogramSnapshots( | |
69 base::HistogramSnapshotManager* snapshot_manager) { | |
70 DCHECK(thread_checker_.CalledOnValidThread()); | |
71 | |
72 for (AllocatorByIdMap::iterator iter(&allocators_by_id_); !iter.IsAtEnd(); | |
73 iter.Advance()) { | |
74 RecordHistogramSnapshotsFromAllocator( | |
75 snapshot_manager, iter.GetCurrentKey(), iter.GetCurrentValue()); | |
76 } | |
77 | |
78 for (auto& allocator : allocators_to_release_) | |
79 RecordHistogramSnapshotsFromAllocator(snapshot_manager, 0, allocator.get()); | |
80 | |
81 UMA_HISTOGRAM_COUNTS_100( | |
82 "UMA.SubprocessMetricsProvider.SubproccessCount", | |
83 allocators_by_id_.size() + allocators_to_release_.size()); | |
84 | |
85 // The snapshot-manager has taken ownership of the histograms but needs | |
86 // access to only the histogram objects, not "sample" data it uses. Thus, | |
87 // it is safe to release shared-memory segments without for the snapshot- | |
88 // manager to "finish". | |
89 allocators_to_release_.clear(); | |
90 } | |
91 | |
92 void SubprocessMetricsProvider::Observe( | |
93 int type, | |
94 const content::NotificationSource& source, | |
95 const content::NotificationDetails& details) { | |
96 DCHECK(thread_checker_.CalledOnValidThread()); | |
97 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type); | |
98 content::RenderProcessHost* host = | |
99 content::Source<content::RenderProcessHost>(source).ptr(); | |
100 | |
101 // Sometimes, the same host will cause multiple notifications in tests so | |
102 // could possibly do the same in a release build. | |
103 if (!scoped_observer_.IsObserving(host)) | |
104 scoped_observer_.Add(host); | |
105 } | |
106 | |
107 void SubprocessMetricsProvider::RenderProcessReady( | |
108 content::RenderProcessHost* host) { | |
109 DCHECK(thread_checker_.CalledOnValidThread()); | |
110 scoped_ptr<base::SharedPersistentMemoryAllocator> allocator = | |
111 host->ExtractMetricsAllocator(); | |
Alexei Svitkine (slow)
2016/03/29 17:51:24
Please add a comment discussing the lifetime of th
bcwhite
2016/03/30 21:25:55
Done. Good point about the UMA not being enabled,
Alexei Svitkine (slow)
2016/03/31 15:22:04
You can implement the OnRecordingEnabled() and OnR
bcwhite
2016/03/31 18:27:49
Done.
| |
112 if (allocator) { | |
113 RegisterSubprocessAllocator( | |
114 host->GetID(), | |
115 make_scoped_ptr(new base::PersistentHistogramAllocator( | |
116 std::move(allocator)))); | |
117 } | |
118 } | |
119 | |
120 void SubprocessMetricsProvider::RenderProcessHostDestroyed( | |
121 content::RenderProcessHost* host) { | |
122 DCHECK(thread_checker_.CalledOnValidThread()); | |
Alexei Svitkine (slow)
2016/03/29 17:51:24
Nit: Add an empty line after the DCHECKs at the st
bcwhite
2016/03/30 21:25:55
Done.
| |
123 DeregisterSubprocessAllocator(host->GetID()); | |
124 scoped_observer_.Remove(host); | |
125 } | |
OLD | NEW |