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_LEAK_DETECTOR_LEAK_DETECTOR_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_CONTROLLER_H_ | 6 #define CHROME_BROWSER_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 14 #include "chrome/browser/metrics/leak_detector/leak_detector_remote_controller.h " | 14 #include "chrome/browser/metrics/leak_detector/leak_detector_remote_controller.h " |
| 15 #include "chrome/browser/metrics/metrics_memory_details.h" | |
| 15 #include "components/metrics/leak_detector/leak_detector.h" | 16 #include "components/metrics/leak_detector/leak_detector.h" |
| 16 #include "components/metrics/proto/memory_leak_report.pb.h" | 17 #include "components/metrics/proto/memory_leak_report.pb.h" |
| 17 | 18 |
| 18 namespace metrics { | 19 namespace metrics { |
| 19 | 20 |
| 20 // This class initializes the LeakDetector on the browser process and registers | 21 // This class initializes the LeakDetector on the browser process and registers |
| 21 // itself to be notified of leak reports. | 22 // itself to be notified of leak reports. |
| 22 class LeakDetectorController | 23 class LeakDetectorController |
| 23 : public LeakDetector::Observer, | 24 : public LeakDetector::Observer, |
| 24 public LeakDetectorRemoteController::LocalController { | 25 public LeakDetectorRemoteController::LocalController { |
| 25 public: | 26 public: |
| 26 LeakDetectorController(); | 27 LeakDetectorController(); |
| 27 ~LeakDetectorController() override; | 28 ~LeakDetectorController() override; |
| 28 | 29 |
| 29 // Retrieves all reports in |stored_reports_|, moving them to |*reports|. | 30 // Retrieves all reports in |stored_reports_|, moving them to |*reports|. |
| 30 // |stored_reports_| is empty afterwards. | 31 // |stored_reports_| is empty afterwards. |
| 31 void GetLeakReports(std::vector<MemoryLeakReportProto>* reports); | 32 void GetLeakReports(std::vector<MemoryLeakReportProto>* reports); |
| 32 | 33 |
| 33 protected: | 34 protected: |
| 34 // LeakDetector::Observer: | 35 // LeakDetector::Observer: |
| 35 void OnLeaksFound(const std::vector<MemoryLeakReportProto>& reports) override; | 36 void OnLeaksFound(const std::vector<MemoryLeakReportProto>& reports) override; |
| 36 | 37 |
| 37 // LeakDetectorRemoteController::LocalController: | 38 // LeakDetectorRemoteController::LocalController: |
| 38 MemoryLeakReportProto::Params GetParamsAndRecordRequest() override; | 39 MemoryLeakReportProto::Params GetParamsAndRecordRequest() override; |
| 39 void SendLeakReports( | 40 void SendLeakReports( |
| 40 const std::vector<MemoryLeakReportProto>& reports) override; | 41 const std::vector<MemoryLeakReportProto>& reports) override; |
| 41 void OnRemoteProcessShutdown() override; | 42 void OnRemoteProcessShutdown() override; |
| 42 | 43 |
| 44 void set_enable_collect_memory_usage_(bool enabled) { | |
|
Simon Que
2016/10/07 20:24:25
Nit: No trailing underscore in mutator name.
mwlodar
2016/10/07 21:23:42
Done.
| |
| 45 enable_collect_memory_usage_ = enabled; | |
| 46 } | |
| 47 | |
| 43 private: | 48 private: |
| 49 // Accumulates total memory usage info inside MetricsMemoryDetails. | |
| 50 class TotalMemoryGrowthTracker : public MemoryGrowthTracker { | |
| 51 public: | |
| 52 TotalMemoryGrowthTracker(); | |
| 53 ~TotalMemoryGrowthTracker(); | |
| 54 | |
| 55 // Updates the inner counter by sample (in kb). Returns always false | |
| 56 // to suppress generating growth/shrink histograms in MetricsMemoryDetails. | |
| 57 bool UpdateSample(base::ProcessId pid, int sample, int* diff) override; | |
| 58 | |
| 59 size_t total_usage_kb() const { return total_usage_kb_; } | |
| 60 void reset() { total_usage_kb_ = 0; } | |
| 61 | |
| 62 private: | |
| 63 size_t total_usage_kb_; | |
| 64 }; | |
| 65 | |
| 44 // Returns true to indicate that LeakDetector should be enabled on a renderer | 66 // Returns true to indicate that LeakDetector should be enabled on a renderer |
| 45 // process. This is determined as follows: | 67 // process. This is determined as follows: |
| 46 // 1. If the number of processes running LeakDetector is not at max, returns | 68 // 1. If the number of processes running LeakDetector is not at max, returns |
| 47 // true with a probability of |per_process_enable_probability_|. | 69 // true with a probability of |per_process_enable_probability_|. |
| 48 // 2. If the number of processes running LeakDetector has reached the max, | 70 // 2. If the number of processes running LeakDetector has reached the max, |
| 49 // returns false. | 71 // returns false. |
| 50 bool ShouldRandomlyEnableLeakDetectorOnRendererProcess() const; | 72 bool ShouldRandomlyEnableLeakDetectorOnRendererProcess() const; |
| 51 | 73 |
| 52 // Stores a given array of leak reports in |stored_reports_|. |process_type| | 74 // Stores a given array of leak reports in |stored_reports_|. |process_type| |
| 53 // is the type of process that generated these reports. The reports must all | 75 // is the type of process that generated these reports. The reports must all |
| 54 // come from the same process type. | 76 // come from the same process type. |
| 55 void StoreLeakReports(const std::vector<MemoryLeakReportProto>& reports, | 77 void StoreLeakReports(const std::vector<MemoryLeakReportProto>& reports, |
| 56 MemoryLeakReportProto::ProcessType process_type); | 78 MemoryLeakReportProto::ProcessType process_type); |
| 57 | 79 |
| 80 // Method called from MetricsMemoryDetails when memory usage gets measured. | |
| 81 // Updates |memory_usage_info| in stored_reports_[i] for i >= |index|. | |
| 82 void OnMemoryDetailCollectionDone(size_t index); | |
| 83 | |
| 58 // All leak reports received through OnLeakFound() are stored in protobuf | 84 // All leak reports received through OnLeakFound() are stored in protobuf |
| 59 // format. | 85 // format. |
| 60 std::vector<MemoryLeakReportProto> stored_reports_; | 86 std::vector<MemoryLeakReportProto> stored_reports_; |
| 61 | 87 |
| 62 // Contains all the parameters passed to LeakDetector. Store them in a single | 88 // Contains all the parameters passed to LeakDetector. Store them in a single |
| 63 // protobuf message instead of in separate member variables. | 89 // protobuf message instead of in separate member variables. |
| 64 const MemoryLeakReportProto::Params params_; | 90 const MemoryLeakReportProto::Params params_; |
| 65 | 91 |
| 66 // The build ID of the current Chrome binary. | 92 // The build ID of the current Chrome binary. |
| 67 std::vector<uint8_t> build_id_; | 93 std::vector<uint8_t> build_id_; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 78 // simultaneously. | 104 // simultaneously. |
| 79 int max_renderer_processes_with_leak_detector_enabled_; | 105 int max_renderer_processes_with_leak_detector_enabled_; |
| 80 | 106 |
| 81 // The number of renderer processes on which the leak detector has been | 107 // The number of renderer processes on which the leak detector has been |
| 82 // enabled and is running. | 108 // enabled and is running. |
| 83 int num_renderer_processes_with_leak_detector_enabled_; | 109 int num_renderer_processes_with_leak_detector_enabled_; |
| 84 | 110 |
| 85 // For thread safety. | 111 // For thread safety. |
| 86 base::ThreadChecker thread_checker_; | 112 base::ThreadChecker thread_checker_; |
| 87 | 113 |
| 114 // An object passed to MetricsMemoryDetails for collecting memory usage. | |
| 115 TotalMemoryGrowthTracker total_memory_growth_tracker_; | |
| 116 | |
| 117 // A flag indicating if a task should be scheduled to populate memory usage | |
| 118 // information in the reports. | |
| 119 bool enable_collect_memory_usage_; | |
|
Simon Que
2016/10/07 20:24:25
Nit: incorrect grammar, try: enable_memory_usage_c
mwlodar
2016/10/07 21:23:42
Done.
| |
| 120 | |
| 121 // Indicates that collecting memory usage is running. | |
| 122 bool waiting_for_collect_memory_usage_; | |
| 123 | |
| 124 // For passing |this| into a callback. | |
| 125 base::WeakPtrFactory<LeakDetectorController> weak_ptr_factory_; | |
| 126 | |
| 88 DISALLOW_COPY_AND_ASSIGN(LeakDetectorController); | 127 DISALLOW_COPY_AND_ASSIGN(LeakDetectorController); |
| 89 }; | 128 }; |
| 90 | 129 |
| 91 } // namespace metrics | 130 } // namespace metrics |
| 92 | 131 |
| 93 #endif // CHROME_BROWSER_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_CONTROLLER_H_ | 132 #endif // CHROME_BROWSER_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_CONTROLLER_H_ |
| OLD | NEW |