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 Initialize() | |
| 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 // Initializes the peak detector, binding the polling tasks on the given | |
| 45 // thread. Initialize() can be call serveral times, provided that: (1) Stop() | |
|
hjd
2017/04/03 13:25:30
nit: call -> called
hjd
2017/04/03 13:25:30
nit: typo: serveral -> several
Primiano Tucci (use gerrit)
2017/04/03 20:28:15
Done.
Primiano Tucci (use gerrit)
2017/04/03 20:28:16
Done.
| |
| 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 // |task_runner| when a memory peak is detected. | |
|
ssid
2017/04/03 17:55:28
nit: |task_runner| is not defined in the declarati
Primiano Tucci (use gerrit)
2017/04/03 20:28:15
Done.
| |
| 56 void Initialize(const GetDumpProvidersFunction&, | |
| 57 const scoped_refptr<SequencedTaskRunner>&, | |
| 58 const OnPeakDetectedCallback&); | |
| 59 void TearDownForTesting(); | |
| 60 | |
| 61 // This posts a task onto the passed task runner which refreshes the list of | |
| 62 // dump providers via the GetDumpProvidersFunction. If at least one dump | |
| 63 // provider is available, this starts immediately polling on the task runner. | |
| 64 // If not, the detector remains in the ENABLED state and will start polling | |
| 65 // automatically (i.e. without requiring another call to Start()) on the | |
| 66 // next call to NotifyMemoryDumpProvidersChanged(). | |
| 67 void Start(); | |
| 68 | |
| 69 // Stops the polling on the task runner (if was active at all). This doesn't | |
| 70 // wait for the task runner to drain pending tasks, so it is possible that | |
| 71 // a polling will happen concurrently (or in the immediate future) with the | |
| 72 // Stop() call. It is responsibility of the caller to drain or synchronize | |
| 73 // with the task runner. | |
| 74 void Stop(); | |
| 75 | |
| 76 // Used by MemoryDumpManager to notify that the list of polling-capable dump | |
| 77 // providers has changed. The peak detector will reload the list on the next | |
| 78 // polling task. This function can be called before Initialize(), in which | |
| 79 // case will be just a no-op. | |
| 80 void NotifyMemoryDumpProvidersChanged(); | |
| 81 | |
| 82 private: | |
| 83 friend class MemoryPeakDetectorTest; | |
| 84 | |
| 85 MemoryPeakDetector(); | |
| 86 ~MemoryPeakDetector(); | |
| 87 | |
| 88 // All these methods are always called on the |task_runner_|. | |
| 89 void StartInternal(); | |
| 90 void StopInternal(); | |
| 91 void ReloadDumpProvidersAndStartPollingIfNeeded(); | |
| 92 void PollMemoryAndDetectPeak(); | |
| 93 | |
| 94 // It is safe to call these testing method only on the |task_runner_|. | |
| 95 State state_for_testing() const { return state_; } | |
| 96 uint32_t poll_tasks_count_for_testing() const { return poll_tasks_count_; } | |
| 97 | |
| 98 // The task runner where all the internal calls are posted onto. This field | |
| 99 // must be accessed only by the thread that owns the class instance and NOT by | |
|
ssid
2017/04/03 17:55:27
"thread that owns the class" is not true right?
Primiano Tucci (use gerrit)
2017/04/03 20:28:15
Right. reworded.
| |
| 100 // by the posted tasks that run on the |task_runner_| itself. | |
| 101 scoped_refptr<SequencedTaskRunner> task_runner_; | |
| 102 | |
| 103 // After the Initialize() call, the fields below, must be accessed only from | |
| 104 // the |task_runner_|. | |
| 105 | |
| 106 // Bound function to get an updated list of polling-capable dump providers. | |
| 107 GetDumpProvidersFunction get_dump_providers_function_; | |
| 108 | |
| 109 // The callback to invoke when peaks are detected. | |
| 110 OnPeakDetectedCallback on_peak_detected_callback_; | |
| 111 | |
| 112 // List of polling-aware dump providers to invoke upon each poll. | |
| 113 DumpProvidersList dump_providers_; | |
| 114 | |
| 115 State state_; | |
| 116 uint32_t polling_interval_ms_; | |
| 117 uint32_t poll_tasks_count_; // for testing, | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(MemoryPeakDetector); | |
| 120 }; | |
| 121 | |
| 122 } // namespace trace_event | |
| 123 } // namespace base | |
| 124 | |
| 125 #endif // BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_ | |
| OLD | NEW |