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 |
| 38 if (!allocators_by_id_.Lookup(id)) |
| 39 return; |
| 40 |
| 41 // Transfer allocator from "active" list to "release" list. The allocator |
| 42 // will continue to live (and keep the associated shared memory alive) |
| 43 // until the next upload after which it will be released. |
| 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::PersistentHistogramAllocator* allocator) { |
| 53 DCHECK(allocator); |
| 54 |
| 55 int histogram_count = 0; |
| 56 base::PersistentHistogramAllocator::Iterator hist_iter; |
| 57 allocator->CreateIterator(&hist_iter); |
| 58 while (true) { |
| 59 scoped_ptr<base::HistogramBase> histogram = |
| 60 allocator->GetNextHistogram(&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 waiting for the |
| 91 // snapshot-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 |
| 102 content::RenderProcessHost* host = |
| 103 content::Source<content::RenderProcessHost>(source).ptr(); |
| 104 |
| 105 // Sometimes, the same host will cause multiple notifications in tests so |
| 106 // could possibly do the same in a release build. |
| 107 if (!scoped_observer_.IsObserving(host)) |
| 108 scoped_observer_.Add(host); |
| 109 } |
| 110 |
| 111 void SubprocessMetricsProvider::RenderProcessReady( |
| 112 content::RenderProcessHost* host) { |
| 113 DCHECK(thread_checker_.CalledOnValidThread()); |
| 114 |
| 115 // If the render-process-host passed a persistent-memory-allocator to the |
| 116 // renderer process, extract it and register it here. |
| 117 scoped_ptr<base::SharedPersistentMemoryAllocator> allocator = |
| 118 host->ExtractMetricsAllocator(); |
| 119 if (allocator) { |
| 120 RegisterSubprocessAllocator( |
| 121 host->GetID(), |
| 122 make_scoped_ptr(new base::PersistentHistogramAllocator( |
| 123 std::move(allocator)))); |
| 124 } |
| 125 } |
| 126 |
| 127 void SubprocessMetricsProvider::RenderProcessHostDestroyed( |
| 128 content::RenderProcessHost* host) { |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 |
| 131 DeregisterSubprocessAllocator(host->GetID()); |
| 132 scoped_observer_.Remove(host); |
| 133 } |
OLD | NEW |