Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_ | |
| 6 #define BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/base_export.h" | |
| 14 #include "base/callback.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 | |
| 18 namespace base { | |
| 19 | |
| 20 class SequencedTaskRunner; | |
| 21 | |
| 22 namespace trace_event { | |
| 23 | |
| 24 struct MemoryDumpProviderInfo; | |
| 25 | |
| 26 // This class is NOT thread-safe, the caller has to ensure linearization of | |
| 27 // the calls to the public methods. In any case, the public methods do NOT have | |
| 28 // to be called from the |task_runner| on which the polling tasks run. | |
| 29 class BASE_EXPORT MemoryPeakDetector { | |
| 30 public: | |
| 31 using OnPeakDetectedCallback = RepeatingClosure; | |
| 32 using DumpProvidersList = std::vector<scoped_refptr<MemoryDumpProviderInfo>>; | |
| 33 using GetDumpProvidersFunction = RepeatingCallback<void(DumpProvidersList*)>; | |
| 34 | |
| 35 enum State { | |
| 36 NOT_INITIALIZED = 0, // Before Setup() | |
|
ssid
2017/04/04 05:08:35
It'd be good to change this to NOT_SETUP. but not
| |
| 37 DISABLED, // Before Start() or after Stop(). | |
| 38 ENABLED, // After Start() but no dump_providers_ are available. | |
| 39 RUNNING // After Start(). The PollMemoryAndDetectPeak() task is scheduled. | |
| 40 }; | |
| 41 | |
| 42 static MemoryPeakDetector* GetInstance(); | |
| 43 | |
| 44 // Configures the peak detector, binding the polling tasks on the given | |
| 45 // thread. Setup() can be called several times, provided that: (1) Stop() | |
| 46 // is called; (2a) the previous task_runner is flushed or (2b) the task_runner | |
| 47 // remains the same. | |
| 48 // GetDumpProvidersFunction: is the function that will be invoked to get | |
| 49 // an updated list of polling-capable dump providers. This is really just | |
| 50 // MemoryDumpManager::GetDumpProvidersForPolling, but this extra level of | |
| 51 // indirection allows easier testing. | |
| 52 // SequencedTaskRunner: the task runner where PollMemoryAndDetectPeak() will | |
| 53 // be periodically called. | |
| 54 // OnPeakDetectedCallback: a callback that will be invoked on the | |
| 55 // given task runner when a memory peak is detected. | |
| 56 void Setup(const GetDumpProvidersFunction&, | |
| 57 const scoped_refptr<SequencedTaskRunner>&, | |
| 58 const OnPeakDetectedCallback&); | |
| 59 | |
| 60 // Releases the |task_runner_| and the bound callbacks. | |
| 61 void TearDown(); | |
| 62 | |
| 63 // This posts a task onto the passed task runner which refreshes the list of | |
| 64 // dump providers via the GetDumpProvidersFunction. If at least one dump | |
| 65 // provider is available, this starts immediately polling on the task runner. | |
| 66 // If not, the detector remains in the ENABLED state and will start polling | |
| 67 // automatically (i.e. without requiring another call to Start()) on the | |
| 68 // next call to NotifyMemoryDumpProvidersChanged(). | |
| 69 void Start(); | |
| 70 | |
| 71 // Stops the polling on the task runner (if was active at all). This doesn't | |
| 72 // wait for the task runner to drain pending tasks, so it is possible that | |
| 73 // a polling will happen concurrently (or in the immediate future) with the | |
| 74 // Stop() call. It is responsibility of the caller to drain or synchronize | |
| 75 // with the task runner. | |
| 76 void Stop(); | |
| 77 | |
| 78 // Used by MemoryDumpManager to notify that the list of polling-capable dump | |
| 79 // providers has changed. The peak detector will reload the list on the next | |
| 80 // polling task. This function can be called before Setup(), in which | |
| 81 // case will be just a no-op. | |
| 82 void NotifyMemoryDumpProvidersChanged(); | |
| 83 | |
| 84 private: | |
| 85 friend class MemoryPeakDetectorTest; | |
| 86 | |
| 87 MemoryPeakDetector(); | |
| 88 ~MemoryPeakDetector(); | |
| 89 | |
| 90 // All these methods are always called on the |task_runner_|. | |
| 91 void StartInternal(); | |
| 92 void StopInternal(); | |
| 93 void TearDownInternal(); | |
| 94 void ReloadDumpProvidersAndStartPollingIfNeeded(); | |
| 95 void PollMemoryAndDetectPeak(); | |
| 96 | |
| 97 // It is safe to call these testing method only on the |task_runner_|. | |
| 98 State state_for_testing() const { return state_; } | |
| 99 uint32_t poll_tasks_count_for_testing() const { | |
| 100 return poll_tasks_count_for_testing_; | |
| 101 } | |
| 102 | |
| 103 // The task runner where all the internal calls are posted onto. This field | |
| 104 // must be NOT be accessed by the posted tasks on the |task_runner_| because | |
| 105 // there might still be outstanding tasks on the actual |task_runner_| while | |
| 106 // this refptr is reset. This can only be safely accessed by the public | |
| 107 // methods above, which the client of this class is supposed to call | |
| 108 // sequentially (i.e. from the same thread or while holding a lock). | |
| 109 scoped_refptr<SequencedTaskRunner> task_runner_; | |
| 110 | |
| 111 // After the Setup() call, the fields below, must be accessed only from | |
| 112 // the |task_runner_|. | |
| 113 | |
| 114 // Bound function to get an updated list of polling-capable dump providers. | |
| 115 GetDumpProvidersFunction get_dump_providers_function_; | |
| 116 | |
| 117 // The callback to invoke when peaks are detected. | |
| 118 OnPeakDetectedCallback on_peak_detected_callback_; | |
| 119 | |
| 120 // List of polling-aware dump providers to invoke upon each poll. | |
| 121 DumpProvidersList dump_providers_; | |
| 122 | |
| 123 State state_; | |
| 124 uint32_t polling_interval_ms_; | |
| 125 uint32_t poll_tasks_count_for_testing_; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(MemoryPeakDetector); | |
| 128 }; | |
| 129 | |
| 130 } // namespace trace_event | |
| 131 } // namespace base | |
| 132 | |
| 133 #endif // BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_ | |
| OLD | NEW |