Chromium Code Reviews| 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 <map> | |
| 8 #include <memory> | 9 #include <memory> |
| 9 #include <set> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 12 #include "base/id_map.h" | 13 #include "base/metrics/statistics_recorder.h" |
| 13 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
| 14 #include "base/threading/thread_checker.h" | 15 #include "base/synchronization/lock.h" |
| 15 #include "components/metrics/metrics_provider.h" | 16 #include "components/metrics/metrics_provider.h" |
| 16 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 18 #include "content/public/browser/render_process_host_observer.h" | 19 #include "content/public/browser/render_process_host_observer.h" |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 class PersistentHistogramAllocator; | 22 class PersistentHistogramAllocator; |
| 22 class SharedPersistentMemoryAllocator; | 23 class SharedPersistentMemoryAllocator; |
| 23 } | 24 } |
| 24 | 25 |
| 25 // SubprocessMetricsProvider gathers and logs histograms stored in shared | 26 // SubprocessMetricsProvider gathers and logs histograms stored in shared |
| 26 // memory segments between processes. | 27 // memory segments between processes. |
| 27 class SubprocessMetricsProvider : public metrics::MetricsProvider, | 28 class SubprocessMetricsProvider |
| 28 public content::NotificationObserver, | 29 : public metrics::MetricsProvider, |
| 29 public content::RenderProcessHostObserver { | 30 public base::StatisticsRecorder::MetricsDisplay, |
| 31 public content::NotificationObserver, | |
| 32 public content::RenderProcessHostObserver { | |
| 30 public: | 33 public: |
| 31 SubprocessMetricsProvider(); | 34 SubprocessMetricsProvider(); |
| 32 ~SubprocessMetricsProvider() override; | 35 ~SubprocessMetricsProvider() override; |
| 33 | 36 |
| 34 private: | 37 private: |
| 35 friend class SubprocessMetricsProviderTest; | 38 friend class SubprocessMetricsProviderTest; |
| 36 | 39 |
| 37 // Indicates subprocess to be monitored with unique id for later reference. | 40 // Indicates subprocess to be monitored with unique id for later reference. |
| 38 // Metrics reporting will read histograms from it and upload them to UMA. | 41 // Metrics reporting will read histograms from it and upload them to UMA. |
| 39 void RegisterSubprocessAllocator( | 42 void RegisterSubprocessAllocator( |
| 40 int id, | 43 int id, |
| 41 std::unique_ptr<base::PersistentHistogramAllocator> allocator); | 44 std::unique_ptr<base::PersistentHistogramAllocator> allocator); |
| 42 | 45 |
| 43 // Indicates that a subprocess has exited and is thus finished with the | 46 // Indicates that a subprocess has exited and is thus finished with the |
| 44 // allocator it was using. | 47 // allocator it was using. |
| 45 void DeregisterSubprocessAllocator(int id); | 48 void DeregisterSubprocessAllocator(int id); |
| 46 | 49 |
| 47 // Report all histograms of a given allocator to the snapshot-manager. | 50 // Report histograms of a given allocator to the snapshot-manager. If |
| 51 // |query| is not empty, only histograms that contain that string will | |
| 52 // be recorded. If |absolute| is true, no delta from the previous snapshot | |
| 53 // will be done. Only one caller, the caller that uploads to UMA, is | |
| 54 // allowed to calcuate deltas because they are not independent. | |
| 48 void RecordHistogramSnapshotsFromAllocator( | 55 void RecordHistogramSnapshotsFromAllocator( |
| 49 base::HistogramSnapshotManager* snapshot_manager, | 56 base::HistogramSnapshotManager* snapshot_manager, |
| 57 const std::string& query, | |
| 58 bool absolute, | |
| 50 int id, | 59 int id, |
| 51 base::PersistentHistogramAllocator* allocator); | 60 base::PersistentHistogramAllocator* allocator); |
| 52 | 61 |
| 53 // metrics::MetricsProvider: | 62 // metrics::MetricsProvider: |
| 54 void OnRecordingEnabled() override; | 63 void OnRecordingEnabled() override; |
| 55 void OnRecordingDisabled() override; | 64 void OnRecordingDisabled() override; |
| 56 void RecordHistogramSnapshots( | 65 void RecordHistogramSnapshots( |
| 57 base::HistogramSnapshotManager* snapshot_manager) override; | 66 base::HistogramSnapshotManager* snapshot_manager) override; |
| 58 | 67 |
| 68 // base::StatisticsRecorder::MetricsDisplay: | |
| 69 void WriteTitleString(std::string* output) override; | |
| 70 void WriteGraphs(const std::string& query, | |
| 71 bool html, | |
| 72 std::string* output) override; | |
| 73 | |
| 59 // content::NotificationObserver: | 74 // content::NotificationObserver: |
| 60 void Observe(int type, | 75 void Observe(int type, |
| 61 const content::NotificationSource& source, | 76 const content::NotificationSource& source, |
| 62 const content::NotificationDetails& details) override; | 77 const content::NotificationDetails& details) override; |
| 63 | 78 |
| 64 // content::RenderProcessHostObserver: | 79 // content::RenderProcessHostObserver: |
| 65 void RenderProcessReady(content::RenderProcessHost* host) override; | 80 void RenderProcessReady(content::RenderProcessHost* host) override; |
| 66 void RenderProcessExited(content::RenderProcessHost* host, | 81 void RenderProcessExited(content::RenderProcessHost* host, |
| 67 base::TerminationStatus status, | 82 base::TerminationStatus status, |
| 68 int exit_code) override; | 83 int exit_code) override; |
| 69 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; | 84 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; |
| 70 | 85 |
| 71 base::ThreadChecker thread_checker_; | 86 // Lock for accessing objects. This is required because MetricsDisplay is |
| 87 // typically called by a background thread. | |
|
Ilya Sherman
2016/04/13 00:29:32
Hmm, why so? I would have expected it to be calle
bcwhite
2016/04/13 11:58:18
The previous code broke because it was called on a
| |
| 88 base::Lock lock_; | |
| 72 | 89 |
| 73 // Object for registing notification requests. | 90 // Object for registing notification requests. |
| 74 content::NotificationRegistrar registrar_; | 91 content::NotificationRegistrar registrar_; |
| 75 | 92 |
| 76 // All of the shared-persistent-allocators for known sub-processes. | 93 // All of the shared-persistent-allocators for known sub-processes. |
| 77 using AllocatorByIdMap = | 94 using AllocatorByIdMap = |
| 78 IDMap<base::PersistentHistogramAllocator, IDMapOwnPointer, int>; | 95 std::map<int, std::unique_ptr<base::PersistentHistogramAllocator>>; |
| 79 AllocatorByIdMap allocators_by_id_; | 96 AllocatorByIdMap allocators_by_id_; |
| 80 | 97 |
| 81 // Allocators that are no longer attached to a subprocess, to be released | 98 // Allocators that are no longer attached to a subprocess, to be released |
| 82 // once the last data contained therein has been reported. | 99 // once the last data contained therein has been reported. |
| 83 std::vector<std::unique_ptr<base::PersistentHistogramAllocator>> | 100 std::vector<std::unique_ptr<base::PersistentHistogramAllocator>> |
| 84 allocators_to_release_; | 101 allocators_to_release_; |
| 85 | 102 |
| 86 // Track all observed render processes to un-observe them on exit. | 103 // Track all observed render processes to un-observe them on exit. |
| 87 ScopedObserver<content::RenderProcessHost, SubprocessMetricsProvider> | 104 ScopedObserver<content::RenderProcessHost, SubprocessMetricsProvider> |
| 88 scoped_observer_; | 105 scoped_observer_; |
| 89 | 106 |
| 90 // Flag indicating if metrics recording is enabled. Allocators will not | 107 // Flag indicating if metrics recording is enabled. Allocators will not |
| 91 // live past the death of the subprocess if it is not. | 108 // live past the death of the subprocess if it is not. |
| 92 bool metrics_recording_enabled_ = false; | 109 bool metrics_recording_enabled_ = false; |
| 93 | 110 |
| 94 DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProvider); | 111 DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProvider); |
| 95 }; | 112 }; |
| 96 | 113 |
| 97 #endif // CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ | 114 #endif // CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ |
| OLD | NEW |