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_step(bool enabled) { | |
| 45 enable_collect_memory_usage_step_ = 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() override; | |
| 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 DISALLOW_COPY_AND_ASSIGN(TotalMemoryGrowthTracker); | |
|
Alexei Svitkine (slow)
2016/10/12 21:46:12
Nit: Indent is wrong.
You can run "git cl format"
mwlodar
2016/10/12 22:45:39
I did not know about this command, thanks.
| |
| 66 }; | |
| 67 | |
| 44 // Returns true to indicate that LeakDetector should be enabled on a renderer | 68 // Returns true to indicate that LeakDetector should be enabled on a renderer |
| 45 // process. This is determined as follows: | 69 // process. This is determined as follows: |
| 46 // 1. If the number of processes running LeakDetector is not at max, returns | 70 // 1. If the number of processes running LeakDetector is not at max, returns |
| 47 // true with a probability of |per_process_enable_probability_|. | 71 // true with a probability of |per_process_enable_probability_|. |
| 48 // 2. If the number of processes running LeakDetector has reached the max, | 72 // 2. If the number of processes running LeakDetector has reached the max, |
| 49 // returns false. | 73 // returns false. |
| 50 bool ShouldRandomlyEnableLeakDetectorOnRendererProcess() const; | 74 bool ShouldRandomlyEnableLeakDetectorOnRendererProcess() const; |
| 51 | 75 |
| 52 // Stores a given array of leak reports in |stored_reports_|. |process_type| | 76 // 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 | 77 // is the type of process that generated these reports. The reports must all |
| 54 // come from the same process type. | 78 // come from the same process type. |
| 55 void StoreLeakReports(const std::vector<MemoryLeakReportProto>& reports, | 79 void StoreLeakReports(const std::vector<MemoryLeakReportProto>& reports, |
| 56 MemoryLeakReportProto::ProcessType process_type); | 80 MemoryLeakReportProto::ProcessType process_type); |
| 57 | 81 |
| 82 // Method called from MetricsMemoryDetails when memory usage gets measured. | |
| 83 // Updates |memory_usage_info| in stored_reports_[i] for i >= |index|. | |
| 84 void OnMemoryDetailCollectionDone(size_t index); | |
| 85 | |
| 58 // All leak reports received through OnLeakFound() are stored in protobuf | 86 // All leak reports received through OnLeakFound() are stored in protobuf |
| 59 // format. | 87 // format. |
| 60 std::vector<MemoryLeakReportProto> stored_reports_; | 88 std::vector<MemoryLeakReportProto> stored_reports_; |
| 61 | 89 |
| 62 // Contains all the parameters passed to LeakDetector. Store them in a single | 90 // Contains all the parameters passed to LeakDetector. Store them in a single |
| 63 // protobuf message instead of in separate member variables. | 91 // protobuf message instead of in separate member variables. |
| 64 const MemoryLeakReportProto::Params params_; | 92 const MemoryLeakReportProto::Params params_; |
| 65 | 93 |
| 66 // The build ID of the current Chrome binary. | 94 // The build ID of the current Chrome binary. |
| 67 std::vector<uint8_t> build_id_; | 95 std::vector<uint8_t> build_id_; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 78 // simultaneously. | 106 // simultaneously. |
| 79 int max_renderer_processes_with_leak_detector_enabled_; | 107 int max_renderer_processes_with_leak_detector_enabled_; |
| 80 | 108 |
| 81 // The number of renderer processes on which the leak detector has been | 109 // The number of renderer processes on which the leak detector has been |
| 82 // enabled and is running. | 110 // enabled and is running. |
| 83 int num_renderer_processes_with_leak_detector_enabled_; | 111 int num_renderer_processes_with_leak_detector_enabled_; |
| 84 | 112 |
| 85 // For thread safety. | 113 // For thread safety. |
| 86 base::ThreadChecker thread_checker_; | 114 base::ThreadChecker thread_checker_; |
| 87 | 115 |
| 116 // An object passed to MetricsMemoryDetails for collecting memory usage. | |
| 117 TotalMemoryGrowthTracker total_memory_growth_tracker_; | |
| 118 | |
| 119 // A flag indicating if a task should be scheduled to populate memory usage | |
| 120 // information in the reports. | |
| 121 bool enable_collect_memory_usage_step_; | |
| 122 | |
| 123 // Indicates that collecting memory usage is running. | |
| 124 bool waiting_for_collect_memory_usage_step_; | |
| 125 | |
| 126 // For passing |this| into a callback. | |
| 127 base::WeakPtrFactory<LeakDetectorController> weak_ptr_factory_; | |
| 128 | |
| 88 DISALLOW_COPY_AND_ASSIGN(LeakDetectorController); | 129 DISALLOW_COPY_AND_ASSIGN(LeakDetectorController); |
| 89 }; | 130 }; |
| 90 | 131 |
| 91 } // namespace metrics | 132 } // namespace metrics |
| 92 | 133 |
| 93 #endif // CHROME_BROWSER_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_CONTROLLER_H_ | 134 #endif // CHROME_BROWSER_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_CONTROLLER_H_ |
| OLD | NEW |