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::MetricsDisplayer, |
| 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 |
| 40 enum SnapshotType { |
| 41 SNAPSHOT_DELTA, |
| 42 SNAPSHOT_ABSOLUTE, |
| 43 }; |
| 44 |
37 // Indicates subprocess to be monitored with unique id for later reference. | 45 // Indicates subprocess to be monitored with unique id for later reference. |
38 // Metrics reporting will read histograms from it and upload them to UMA. | 46 // Metrics reporting will read histograms from it and upload them to UMA. |
39 void RegisterSubprocessAllocator( | 47 void RegisterSubprocessAllocator( |
40 int id, | 48 int id, |
41 std::unique_ptr<base::PersistentHistogramAllocator> allocator); | 49 std::unique_ptr<base::PersistentHistogramAllocator> allocator); |
42 | 50 |
43 // Indicates that a subprocess has exited and is thus finished with the | 51 // Indicates that a subprocess has exited and is thus finished with the |
44 // allocator it was using. | 52 // allocator it was using. |
45 void DeregisterSubprocessAllocator(int id); | 53 void DeregisterSubprocessAllocator(int id); |
46 | 54 |
47 // Report all histograms of a given allocator to the snapshot-manager. | 55 // Report histograms of a given allocator to the snapshot-manager. If |
| 56 // |query| is not empty, only histograms that contain that string will |
| 57 // be recorded. If |snapshot_type| is ABSOLUTE, no delta from the previous |
| 58 // snapshot will be done. Only one caller, the caller that uploads to UMA, |
| 59 // is allowed to calcuate deltas because they are not independent. |
48 void RecordHistogramSnapshotsFromAllocator( | 60 void RecordHistogramSnapshotsFromAllocator( |
49 base::HistogramSnapshotManager* snapshot_manager, | 61 base::HistogramSnapshotManager* snapshot_manager, |
| 62 const std::string& query, |
| 63 SnapshotType snapshot_type, |
50 int id, | 64 int id, |
51 base::PersistentHistogramAllocator* allocator); | 65 base::PersistentHistogramAllocator* allocator); |
52 | 66 |
53 // metrics::MetricsProvider: | 67 // metrics::MetricsProvider: |
54 void OnDidCreateMetricsLog() override; | 68 void OnDidCreateMetricsLog() override; |
55 void OnRecordingEnabled() override; | 69 void OnRecordingEnabled() override; |
56 void OnRecordingDisabled() override; | 70 void OnRecordingDisabled() override; |
57 void RecordHistogramSnapshots( | 71 void RecordHistogramSnapshots( |
58 base::HistogramSnapshotManager* snapshot_manager) override; | 72 base::HistogramSnapshotManager* snapshot_manager) override; |
59 | 73 |
| 74 // base::StatisticsRecorder::MetricsDisplayer: |
| 75 void WriteTitleString(std::string* output) override; |
| 76 void WriteGraphs(const std::string& query, |
| 77 DisplayType format, |
| 78 std::string* output) override; |
| 79 |
60 // content::NotificationObserver: | 80 // content::NotificationObserver: |
61 void Observe(int type, | 81 void Observe(int type, |
62 const content::NotificationSource& source, | 82 const content::NotificationSource& source, |
63 const content::NotificationDetails& details) override; | 83 const content::NotificationDetails& details) override; |
64 | 84 |
65 // content::RenderProcessHostObserver: | 85 // content::RenderProcessHostObserver: |
66 void RenderProcessReady(content::RenderProcessHost* host) override; | 86 void RenderProcessReady(content::RenderProcessHost* host) override; |
67 void RenderProcessExited(content::RenderProcessHost* host, | 87 void RenderProcessExited(content::RenderProcessHost* host, |
68 base::TerminationStatus status, | 88 base::TerminationStatus status, |
69 int exit_code) override; | 89 int exit_code) override; |
70 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; | 90 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; |
71 | 91 |
72 base::ThreadChecker thread_checker_; | 92 // Lock for accessing objects. This is required because MetricsDisplayer is |
| 93 // typically called by a background thread. |
| 94 base::Lock lock_; |
73 | 95 |
74 // Object for registing notification requests. | 96 // Object for registing notification requests. |
75 content::NotificationRegistrar registrar_; | 97 content::NotificationRegistrar registrar_; |
76 | 98 |
77 // All of the shared-persistent-allocators for known sub-processes. | 99 // All of the shared-persistent-allocators for known sub-processes. |
78 using AllocatorByIdMap = | 100 using AllocatorByIdMap = |
79 IDMap<base::PersistentHistogramAllocator, IDMapOwnPointer, int>; | 101 std::map<int, std::unique_ptr<base::PersistentHistogramAllocator>>; |
80 AllocatorByIdMap allocators_by_id_; | 102 AllocatorByIdMap allocators_by_id_; |
81 | 103 |
82 // Allocators that are no longer attached to a subprocess, to be released | 104 // Allocators that are no longer attached to a subprocess, to be released |
83 // once the last data contained therein has been reported. | 105 // once the last data contained therein has been reported. |
84 std::vector<std::unique_ptr<base::PersistentHistogramAllocator>> | 106 std::vector<std::unique_ptr<base::PersistentHistogramAllocator>> |
85 allocators_for_exited_processes_; | 107 allocators_for_exited_processes_; |
86 std::vector<std::unique_ptr<base::PersistentHistogramAllocator>> | 108 std::vector<std::unique_ptr<base::PersistentHistogramAllocator>> |
87 allocators_to_release_; | 109 allocators_to_release_; |
88 | 110 |
89 // Track all observed render processes to un-observe them on exit. | 111 // Track all observed render processes to un-observe them on exit. |
90 ScopedObserver<content::RenderProcessHost, SubprocessMetricsProvider> | 112 ScopedObserver<content::RenderProcessHost, SubprocessMetricsProvider> |
91 scoped_observer_; | 113 scoped_observer_; |
92 | 114 |
93 // Flag indicating if metrics recording is enabled. Allocators will not | 115 // Flag indicating if metrics recording is enabled. Allocators will not |
94 // live past the death of the subprocess if it is not. | 116 // live past the death of the subprocess if it is not. |
95 bool metrics_recording_enabled_ = false; | 117 bool metrics_recording_enabled_ = false; |
96 | 118 |
97 DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProvider); | 119 DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProvider); |
98 }; | 120 }; |
99 | 121 |
100 #endif // CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ | 122 #endif // CHROME_BROWSER_METRICS_SUBPROCESS_METRICS_PROVIDER_H_ |
OLD | NEW |