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() |
| 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(uint32_t expected_generation); |
| 96 |
| 97 // It is safe to call these testing methods 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 tasks posted on the |task_runner_| because |
| 105 // there might still be outstanding tasks on the |task_runner_| while this |
| 106 // refptr is reset. This can only be safely accessed by the public methods |
| 107 // above, which the client of this class is supposed to call sequentially. |
| 108 scoped_refptr<SequencedTaskRunner> task_runner_; |
| 109 |
| 110 // After the Setup() call, the fields below, must be accessed only from |
| 111 // the |task_runner_|. |
| 112 |
| 113 // Bound function to get an updated list of polling-capable dump providers. |
| 114 GetDumpProvidersFunction get_dump_providers_function_; |
| 115 |
| 116 // The callback to invoke when peaks are detected. |
| 117 OnPeakDetectedCallback on_peak_detected_callback_; |
| 118 |
| 119 // List of polling-aware dump providers to invoke upon each poll. |
| 120 DumpProvidersList dump_providers_; |
| 121 |
| 122 // The generation is incremented every time the |state_| is changed and causes |
| 123 // PollMemoryAndDetectPeak() to early out if the posted task doesn't match the |
| 124 // most recent |generation_|. This allows to drop on the floor outstanding |
| 125 // PostDelayedTask that refer to an old sequence that was later Stop()-ed or |
| 126 // disabled because of NotifyMemoryDumpProvidersChanged(). |
| 127 uint32_t generation_; |
| 128 |
| 129 State state_; |
| 130 uint32_t polling_interval_ms_; |
| 131 uint32_t poll_tasks_count_for_testing_; |
| 132 |
| 133 DISALLOW_COPY_AND_ASSIGN(MemoryPeakDetector); |
| 134 }; |
| 135 |
| 136 } // namespace trace_event |
| 137 } // namespace base |
| 138 |
| 139 #endif // BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_ |
OLD | NEW |