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/histogram_persistence.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 namespace metrics { | |
Alexei Svitkine (slow)
2016/03/24 17:29:53
The metrics namespace is for code in components/me
bcwhite
2016/03/29 11:53:27
Done.
| |
18 | |
19 SubprocessMetricsProvider::SubprocessMetricsProvider() | |
20 : scoped_observer_(this) { | |
21 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | |
Alexei Svitkine (slow)
2016/03/24 17:29:53
I think notifications are deprecated. Is there a w
bcwhite
2016/03/29 11:53:27
Some notifications are deprecated but I couldn't f
| |
22 content::NotificationService::AllBrowserContextsAndSources()); | |
23 } | |
24 | |
25 SubprocessMetricsProvider::~SubprocessMetricsProvider() { | |
26 } | |
27 | |
28 void SubprocessMetricsProvider::RegisterSubprocessAllocator( | |
29 int id, | |
30 scoped_ptr<base::SharedPersistentMemoryAllocator> allocator) { | |
31 DCHECK(thread_checker_.CalledOnValidThread()); | |
32 DCHECK(!allocators_by_id_.Lookup(id)); | |
33 | |
34 // Map is "MapOwnPointer" so transfer ownership to it. | |
35 allocators_by_id_.AddWithID(allocator.release(), id); | |
36 } | |
37 | |
38 void SubprocessMetricsProvider::DeregisterSubprocessAllocator(int id) { | |
39 DCHECK(thread_checker_.CalledOnValidThread()); | |
40 if (!allocators_by_id_.Lookup(id)) | |
41 return; | |
42 | |
43 // Transfer allocator from "active" list to "release" list. | |
44 allocators_to_release_.push_back( | |
45 make_scoped_ptr(allocators_by_id_.Replace(id, nullptr))); | |
46 allocators_by_id_.Remove(id); | |
47 } | |
48 | |
49 void SubprocessMetricsProvider::RecordHistogramSnapshotsFromAllocator( | |
50 base::HistogramSnapshotManager* snapshot_manager, | |
51 int id, | |
52 base::SharedPersistentMemoryAllocator* allocator) { | |
53 DCHECK(allocator); | |
54 | |
55 int histogram_count = 0; | |
56 base::PersistentMemoryAllocator::Iterator hist_iter; | |
57 allocator->CreateIterator(&hist_iter); | |
58 while (true) { | |
59 scoped_ptr<base::HistogramBase> histogram( | |
60 base::GetNextPersistentHistogram(allocator, &hist_iter)); | |
61 if (!histogram) | |
62 break; | |
63 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram)); | |
64 ++histogram_count; | |
65 } | |
66 | |
67 DVLOG(1) << "Reported " << histogram_count << " histograms from subprocess #" | |
68 << id; | |
69 } | |
70 | |
71 void SubprocessMetricsProvider::RecordHistogramSnapshots( | |
72 base::HistogramSnapshotManager* snapshot_manager) { | |
73 DCHECK(thread_checker_.CalledOnValidThread()); | |
74 | |
75 for (AllocatorByIdMap::iterator iter(&allocators_by_id_); !iter.IsAtEnd(); | |
76 iter.Advance()) { | |
77 RecordHistogramSnapshotsFromAllocator( | |
78 snapshot_manager, iter.GetCurrentKey(), iter.GetCurrentValue()); | |
79 } | |
80 | |
81 for (auto& allocator : allocators_to_release_) | |
82 RecordHistogramSnapshotsFromAllocator(snapshot_manager, 0, allocator.get()); | |
83 | |
84 UMA_HISTOGRAM_COUNTS_100( | |
85 "UMA.SubprocessMetricsProvider.SubproccessCount", | |
86 allocators_by_id_.size() + allocators_to_release_.size()); | |
87 | |
88 // The snapshot-manager has taken ownership of the histograms but needs | |
89 // access to only the histogram objects, not "sample" data it uses. Thus, | |
90 // it is safe to release shared-memory segments without for the snapshot- | |
91 // manager to "finish". | |
92 allocators_to_release_.clear(); | |
93 } | |
94 | |
95 void SubprocessMetricsProvider::Observe( | |
96 int type, | |
97 const content::NotificationSource& source, | |
98 const content::NotificationDetails& details) { | |
99 DCHECK(thread_checker_.CalledOnValidThread()); | |
100 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type); | |
101 content::RenderProcessHost* host = | |
102 content::Source<content::RenderProcessHost>(source).ptr(); | |
103 | |
104 // Sometimes, the same host will cause multiple notifications in tests so | |
105 // could possibly do the same in a release build. | |
106 if (!scoped_observer_.IsObserving(host)) | |
107 scoped_observer_.Add(host); | |
108 } | |
109 | |
110 void SubprocessMetricsProvider::RenderProcessReady( | |
111 content::RenderProcessHost* host) { | |
112 DCHECK(thread_checker_.CalledOnValidThread()); | |
113 scoped_ptr<base::SharedPersistentMemoryAllocator> allocator = | |
114 host->GetMetricsAllocator(); | |
115 if (allocator) | |
116 RegisterSubprocessAllocator(host->GetID(), std::move(allocator)); | |
117 } | |
118 | |
119 void SubprocessMetricsProvider::RenderProcessHostDestroyed( | |
120 content::RenderProcessHost* host) { | |
121 DCHECK(thread_checker_.CalledOnValidThread()); | |
122 DeregisterSubprocessAllocator(host->GetID()); | |
123 scoped_observer_.Remove(host); | |
124 } | |
125 | |
126 } // namespace metrics | |
OLD | NEW |