1 // Copyright 2016 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_METRICS_CONTENT_SUBPROCESS_METRICS_PROVIDER_H_ 6 #define COMPONENTS_METRICS_CONTENT_SUBPROCESS_METRICS_PROVIDER_H_ 7 8 #include <memory> 9 #include <set> 10 11 #include "base/containers/id_map.h" 12 #include "base/gtest_prod_util.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/metrics/statistics_recorder.h" 15 #include "base/scoped_multi_source_observation.h" 16 #include "base/threading/thread_checker.h" 17 #include "components/metrics/metrics_provider.h" 18 #include "content/public/browser/browser_child_process_observer.h" 19 #include "content/public/browser/render_process_host.h" 20 #include "content/public/browser/render_process_host_creation_observer.h" 21 #include "content/public/browser/render_process_host_observer.h" 22 23 namespace base { 24 class PersistentHistogramAllocator; 25 } 26 27 namespace metrics { 28 29 // SubprocessMetricsProvider gathers and merges histograms stored in shared 30 // memory segments between processes. Merging occurs when a process exits, 31 // when metrics are being collected for upload, or when something else needs 32 // combined metrics (such as the chrome://histograms page). 33 class SubprocessMetricsProvider 34 : public MetricsProvider, 35 public base::StatisticsRecorder::HistogramProvider, 36 public content::BrowserChildProcessObserver, 37 public content::RenderProcessHostCreationObserver, 38 public content::RenderProcessHostObserver { 39 public: 40 SubprocessMetricsProvider(); 41 42 SubprocessMetricsProvider(const SubprocessMetricsProvider&) = delete; 43 SubprocessMetricsProvider& operator=(const SubprocessMetricsProvider&) = 44 delete; 45 46 ~SubprocessMetricsProvider() override; 47 48 // Merge histograms for all subprocesses. This is used by tests that don't 49 // have access to the internal instance of this class. 50 static void MergeHistogramDeltasForTesting(); 51 52 private: 53 friend class SubprocessMetricsProviderTest; 54 friend class SubprocessMetricsProviderBrowserTest; 55 56 // Registers the existing render processes by calling 57 // OnRenderProcessHostCreated() and RenderProcessReady() according to its 58 // state. 59 void RegisterExistingRenderProcesses(); 60 61 // Indicates subprocess to be monitored with unique id for later reference. 62 // Metrics reporting will read histograms from it and upload them to UMA. 63 void RegisterSubprocessAllocator( 64 int id, 65 std::unique_ptr<base::PersistentHistogramAllocator> allocator); 66 67 // Indicates that a subprocess has exited and is thus finished with the 68 // allocator it was using. 69 void DeregisterSubprocessAllocator(int id); 70 71 // Merge all histograms of a given allocator to the global StatisticsRecorder. 72 // This is called periodically during UMA metrics collection (if enabled) and 73 // possibly on-demand for other purposes. 74 void MergeHistogramDeltasFromAllocator( 75 int id, 76 base::PersistentHistogramAllocator* allocator); 77 78 // MetricsProvider: 79 void MergeHistogramDeltas() override; 80 81 // content::BrowserChildProcessObserver: 82 void BrowserChildProcessLaunchedAndConnected( 83 const content::ChildProcessData& data) override; 84 void BrowserChildProcessHostDisconnected( 85 const content::ChildProcessData& data) override; 86 void BrowserChildProcessCrashed( 87 const content::ChildProcessData& data, 88 const content::ChildProcessTerminationInfo& info) override; 89 void BrowserChildProcessKilled( 90 const content::ChildProcessData& data, 91 const content::ChildProcessTerminationInfo& info) override; 92 93 // content::RenderProcessHostCreationObserver: 94 void OnRenderProcessHostCreated( 95 content::RenderProcessHost* process_host) override; 96 97 // content::RenderProcessHostObserver: 98 void RenderProcessReady(content::RenderProcessHost* host) override; 99 void RenderProcessExited( 100 content::RenderProcessHost* host, 101 const content::ChildProcessTerminationInfo& info) override; 102 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; 103 104 THREAD_CHECKER(thread_checker_); 105 106 // All of the shared-persistent-allocators for known sub-processes. 107 using AllocatorByIdMap = 108 base::IDMap<std::unique_ptr<base::PersistentHistogramAllocator>, int>; 109 AllocatorByIdMap allocators_by_id_; 110 111 // Track all observed render processes to un-observe them on exit. 112 base::ScopedMultiSourceObservation<content::RenderProcessHost, 113 content::RenderProcessHostObserver> 114 scoped_observations_{this}; 115 116 base::WeakPtrFactory<SubprocessMetricsProvider> weak_ptr_factory_{this}; 117 }; 118 119 } // namespace metrics 120 121 #endif // COMPONENTS_METRICS_CONTENT_SUBPROCESS_METRICS_PROVIDER_H_ 122