Chromium Code Reviews| Index: base/trace_event/memory_peak_detector.h |
| diff --git a/base/trace_event/memory_peak_detector.h b/base/trace_event/memory_peak_detector.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..931d0c15429abb4920cb0f1db8302dbb9685c0f0 |
| --- /dev/null |
| +++ b/base/trace_event/memory_peak_detector.h |
| @@ -0,0 +1,125 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_ |
| +#define BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_ |
| + |
| +#include <stdint.h> |
| + |
| +#include <memory> |
| +#include <vector> |
| + |
| +#include "base/base_export.h" |
| +#include "base/callback.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| + |
| +namespace base { |
| + |
| +class SequencedTaskRunner; |
| + |
| +namespace trace_event { |
| + |
| +struct MemoryDumpProviderInfo; |
| + |
| +// This class is NOT thread-safe, the caller has to ensure linearization of |
| +// the calls to the public methods. In any case, the public methods do NOT have |
| +// to be called from the |task_runner| on which the polling tasks run. |
| +class BASE_EXPORT MemoryPeakDetector { |
| + public: |
| + using OnPeakDetectedCallback = RepeatingClosure; |
| + using DumpProvidersList = std::vector<scoped_refptr<MemoryDumpProviderInfo>>; |
| + using GetDumpProvidersFunction = RepeatingCallback<void(DumpProvidersList*)>; |
| + |
| + enum State { |
| + NOT_INITIALIZED = 0, // Before Initialize() |
| + DISABLED, // Before Start() or after Stop(). |
| + ENABLED, // After Start() but no dump_providers_ are available. |
| + RUNNING // After Start(). The PollMemoryAndDetectPeak() task is scheduled. |
| + }; |
| + |
| + static MemoryPeakDetector* GetInstance(); |
| + |
| + // Initializes the peak detector, binding the polling tasks on the given |
| + // 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.
|
| + // is called; (2a) the previous task_runner is flushed or (2b) the task_runner |
| + // remains the same. |
| + // GetDumpProvidersFunction: is the function that will be invoked to get |
| + // an updated list of polling-capable dump providers. This is really just |
| + // MemoryDumpManager::GetDumpProvidersForPolling, but this extra level of |
| + // indirection allows easier testing. |
| + // SequencedTaskRunner: the task runner where PollMemoryAndDetectPeak() will |
| + // be periodically called. |
| + // OnPeakDetectedCallback: a callback that will be invoked on the |
| + // |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.
|
| + void Initialize(const GetDumpProvidersFunction&, |
| + const scoped_refptr<SequencedTaskRunner>&, |
| + const OnPeakDetectedCallback&); |
| + void TearDownForTesting(); |
| + |
| + // This posts a task onto the passed task runner which refreshes the list of |
| + // dump providers via the GetDumpProvidersFunction. If at least one dump |
| + // provider is available, this starts immediately polling on the task runner. |
| + // If not, the detector remains in the ENABLED state and will start polling |
| + // automatically (i.e. without requiring another call to Start()) on the |
| + // next call to NotifyMemoryDumpProvidersChanged(). |
| + void Start(); |
| + |
| + // Stops the polling on the task runner (if was active at all). This doesn't |
| + // wait for the task runner to drain pending tasks, so it is possible that |
| + // a polling will happen concurrently (or in the immediate future) with the |
| + // Stop() call. It is responsibility of the caller to drain or synchronize |
| + // with the task runner. |
| + void Stop(); |
| + |
| + // Used by MemoryDumpManager to notify that the list of polling-capable dump |
| + // providers has changed. The peak detector will reload the list on the next |
| + // polling task. This function can be called before Initialize(), in which |
| + // case will be just a no-op. |
| + void NotifyMemoryDumpProvidersChanged(); |
| + |
| + private: |
| + friend class MemoryPeakDetectorTest; |
| + |
| + MemoryPeakDetector(); |
| + ~MemoryPeakDetector(); |
| + |
| + // All these methods are always called on the |task_runner_|. |
| + void StartInternal(); |
| + void StopInternal(); |
| + void ReloadDumpProvidersAndStartPollingIfNeeded(); |
| + void PollMemoryAndDetectPeak(); |
| + |
| + // It is safe to call these testing method only on the |task_runner_|. |
| + State state_for_testing() const { return state_; } |
| + uint32_t poll_tasks_count_for_testing() const { return poll_tasks_count_; } |
| + |
| + // The task runner where all the internal calls are posted onto. This field |
| + // 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.
|
| + // by the posted tasks that run on the |task_runner_| itself. |
| + scoped_refptr<SequencedTaskRunner> task_runner_; |
| + |
| + // After the Initialize() call, the fields below, must be accessed only from |
| + // the |task_runner_|. |
| + |
| + // Bound function to get an updated list of polling-capable dump providers. |
| + GetDumpProvidersFunction get_dump_providers_function_; |
| + |
| + // The callback to invoke when peaks are detected. |
| + OnPeakDetectedCallback on_peak_detected_callback_; |
| + |
| + // List of polling-aware dump providers to invoke upon each poll. |
| + DumpProvidersList dump_providers_; |
| + |
| + State state_; |
| + uint32_t polling_interval_ms_; |
| + uint32_t poll_tasks_count_; // for testing, |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MemoryPeakDetector); |
| +}; |
| + |
| +} // namespace trace_event |
| +} // namespace base |
| + |
| +#endif // BASE_TRACE_EVENT_MEMORY_PEAK_DETECTOR_H_ |