| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ | 5 #ifndef CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ |
| 6 #define CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ | 6 #define CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/id_map.h" | 12 #include "base/id_map.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 13 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
| 14 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 15 #include "components/metrics/metrics_provider.h" | 16 #include "components/metrics/metrics_provider.h" |
| 17 #include "content/public/browser/browser_child_process_observer.h" |
| 16 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 18 #include "content/public/browser/render_process_host_observer.h" | 20 #include "content/public/browser/render_process_host_observer.h" |
| 19 | 21 |
| 20 namespace base { | 22 namespace base { |
| 21 class PersistentHistogramAllocator; | 23 class PersistentHistogramAllocator; |
| 22 class SharedPersistentMemoryAllocator; | 24 class SharedPersistentMemoryAllocator; |
| 23 } | 25 } |
| 24 | 26 |
| 25 // SubprocessMetricsProvider gathers and merges histograms stored in shared | 27 // SubprocessMetricsProvider gathers and merges histograms stored in shared |
| 26 // memory segments between processes. Merging occurs when a process exits, | 28 // memory segments between processes. Merging occurs when a process exits, |
| 27 // when metrics are being collected for upload, or when something else needs | 29 // when metrics are being collected for upload, or when something else needs |
| 28 // combined metrics (such as the chrome://histograms page). | 30 // combined metrics (such as the chrome://histograms page). |
| 29 class SubprocessMetricsProvider : public metrics::MetricsProvider, | 31 class SubprocessMetricsProvider : public metrics::MetricsProvider, |
| 32 public content::BrowserChildProcessObserver, |
| 30 public content::NotificationObserver, | 33 public content::NotificationObserver, |
| 31 public content::RenderProcessHostObserver { | 34 public content::RenderProcessHostObserver { |
| 32 public: | 35 public: |
| 33 SubprocessMetricsProvider(); | 36 SubprocessMetricsProvider(); |
| 34 ~SubprocessMetricsProvider() override; | 37 ~SubprocessMetricsProvider() override; |
| 35 | 38 |
| 39 // Start tracking sub-processes. This is called separately rather than being |
| 40 // set in the constructor because library code that checks for calling on |
| 41 // the correct thread (i.e. UI thread) can break tests because test threads |
| 42 // don't have that ID. |
| 43 void EnableSubprocessTracking(); |
| 44 |
| 36 private: | 45 private: |
| 37 friend class SubprocessMetricsProviderTest; | 46 friend class SubprocessMetricsProviderTest; |
| 38 | 47 |
| 39 // Indicates subprocess to be monitored with unique id for later reference. | 48 // Indicates subprocess to be monitored with unique id for later reference. |
| 40 // Metrics reporting will read histograms from it and upload them to UMA. | 49 // Metrics reporting will read histograms from it and upload them to UMA. |
| 41 void RegisterSubprocessAllocator( | 50 void RegisterSubprocessAllocator( |
| 42 int id, | 51 int id, |
| 43 std::unique_ptr<base::PersistentHistogramAllocator> allocator); | 52 std::unique_ptr<base::PersistentHistogramAllocator> allocator); |
| 44 | 53 |
| 45 // Indicates that a subprocess has exited and is thus finished with the | 54 // Indicates that a subprocess has exited and is thus finished with the |
| 46 // allocator it was using. | 55 // allocator it was using. |
| 47 void DeregisterSubprocessAllocator(int id); | 56 void DeregisterSubprocessAllocator(int id); |
| 48 | 57 |
| 49 // Merge all histograms of a given allocator to the global StatisticsRecorder. | 58 // Merge all histograms of a given allocator to the global StatisticsRecorder. |
| 50 // This is called periodically during UMA metrics collection (if enabled) and | 59 // This is called periodically during UMA metrics collection (if enabled) and |
| 51 // possibly on-demand for other purposes. | 60 // possibly on-demand for other purposes. |
| 52 void MergeHistogramDeltasFromAllocator( | 61 void MergeHistogramDeltasFromAllocator( |
| 53 int id, | 62 int id, |
| 54 base::PersistentHistogramAllocator* allocator); | 63 base::PersistentHistogramAllocator* allocator); |
| 55 | 64 |
| 56 // metrics::MetricsProvider: | 65 // metrics::MetricsProvider: |
| 57 void MergeHistogramDeltas() override; | 66 void MergeHistogramDeltas() override; |
| 58 | 67 |
| 68 // content::BrowserChildProcessObserver: |
| 69 void BrowserChildProcessHostConnected( |
| 70 const content::ChildProcessData& data) override; |
| 71 void BrowserChildProcessHostDisconnected( |
| 72 const content::ChildProcessData& data) override; |
| 73 void BrowserChildProcessCrashed( |
| 74 const content::ChildProcessData& data, |
| 75 int exit_code) override; |
| 76 void BrowserChildProcessKilled( |
| 77 const content::ChildProcessData& data, |
| 78 int exit_code) override; |
| 79 |
| 59 // content::NotificationObserver: | 80 // content::NotificationObserver: |
| 60 void Observe(int type, | 81 void Observe(int type, |
| 61 const content::NotificationSource& source, | 82 const content::NotificationSource& source, |
| 62 const content::NotificationDetails& details) override; | 83 const content::NotificationDetails& details) override; |
| 63 | 84 |
| 64 // content::RenderProcessHostObserver: | 85 // content::RenderProcessHostObserver: |
| 65 void RenderProcessReady(content::RenderProcessHost* host) override; | 86 void RenderProcessReady(content::RenderProcessHost* host) override; |
| 66 void RenderProcessExited(content::RenderProcessHost* host, | 87 void RenderProcessExited(content::RenderProcessHost* host, |
| 67 base::TerminationStatus status, | 88 base::TerminationStatus status, |
| 68 int exit_code) override; | 89 int exit_code) override; |
| 69 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; | 90 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; |
| 70 | 91 |
| 92 // Gets a histogram allocator from a subprocess. This must be called on |
| 93 // the IO thread. |
| 94 static std::unique_ptr<base::PersistentHistogramAllocator> |
| 95 GetSubprocessHistogramAllocatorOnIOThread(int id); |
| 96 |
| 71 base::ThreadChecker thread_checker_; | 97 base::ThreadChecker thread_checker_; |
| 72 | 98 |
| 73 // Object for registing notification requests. | 99 // Object for registing notification requests. |
| 74 content::NotificationRegistrar registrar_; | 100 content::NotificationRegistrar registrar_; |
| 75 | 101 |
| 76 // All of the shared-persistent-allocators for known sub-processes. | 102 // All of the shared-persistent-allocators for known sub-processes. |
| 77 using AllocatorByIdMap = | 103 using AllocatorByIdMap = |
| 78 IDMap<base::PersistentHistogramAllocator, IDMapOwnPointer, int>; | 104 IDMap<base::PersistentHistogramAllocator, IDMapOwnPointer, int>; |
| 79 AllocatorByIdMap allocators_by_id_; | 105 AllocatorByIdMap allocators_by_id_; |
| 80 | 106 |
| 81 // Track all observed render processes to un-observe them on exit. | 107 // Track all observed render processes to un-observe them on exit. |
| 82 ScopedObserver<content::RenderProcessHost, SubprocessMetricsProvider> | 108 ScopedObserver<content::RenderProcessHost, SubprocessMetricsProvider> |
| 83 scoped_observer_; | 109 scoped_observer_; |
| 84 | 110 |
| 111 base::WeakPtrFactory<SubprocessMetricsProvider> weak_ptr_factory_; |
| 112 |
| 85 DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProvider); | 113 DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProvider); |
| 86 }; | 114 }; |
| 87 | 115 |
| 88 #endif // CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ | 116 #endif // CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ |
| OLD | NEW |