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..fe041205471d0a4d56becc0b1bb2138d73bac9e2 |
| --- /dev/null |
| +++ b/base/trace_event/memory_peak_detector.h |
| @@ -0,0 +1,112 @@ |
| +// 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" |
| +#include "base/sequence_checker.h" |
| + |
| +namespace base { |
| + |
| +class SequencedTaskRunner; |
| + |
| +namespace trace_event { |
| + |
| +struct MemoryDumpProviderInfo; |
| + |
| +// This class is NOT thread-safe, the public methods must be called on the same |
| +// thread, which does not have to be |task_runner| on which the polling tasks |
|
ssid
2017/03/31 18:03:35
How are you planning to ensure this?
Which thread
Primiano Tucci (use gerrit)
2017/03/31 20:01:54
Oh you are right. Nah there is no need to post, be
|
| +// run (see Start()). |
|
ssid
2017/03/31 18:03:35
Start() does not have any documentation.
Primiano Tucci (use gerrit)
2017/03/31 20:01:55
now it does :)
|
| +class BASE_EXPORT MemoryPeakDetector { |
| + public: |
| + using OnPeakDetectedCallback = RepeatingClosure; |
| + using DumpProvidersList = std::vector<scoped_refptr<MemoryDumpProviderInfo>>; |
| + using GetDumpProvidersFunction = RepeatingCallback<void(DumpProvidersList*)>; |
| + |
| + enum State { |
|
ssid
2017/03/31 18:03:35
I think "State" is a confusing name here. Especial
Primiano Tucci (use gerrit)
2017/03/31 20:01:54
It's going to be better once the polling part of t
|
| + 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(); |
| + |
| + // 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 same |
| + // task runner) when a memory peak is detected. |
| + void Initialize(const GetDumpProvidersFunction&, |
| + const scoped_refptr<SequencedTaskRunner>&, |
|
ssid
2017/03/31 18:03:35
Is there any advantage of using sequenced task run
Primiano Tucci (use gerrit)
2017/03/31 20:01:54
nah just being future proof in case, in future, we
|
| + const OnPeakDetectedCallback&); |
|
ssid
2017/03/31 18:03:35
Maybe this callback shouldn't be part of Initializ
Primiano Tucci (use gerrit)
2017/03/31 20:01:55
Isn't the callback to be always the same though (e
ssid
2017/04/03 17:55:27
hm callback can be different. for instance for slo
Primiano Tucci (use gerrit)
2017/04/03 20:28:15
I think we should deal with that within the callba
|
| + void TearDownForTesting(); |
| + |
| + void Start(); |
| + 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. |
| + 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 |
| + // by the posted tasks that run on the |task_runner_| itself. |
| + scoped_refptr<SequencedTaskRunner> task_runner_; |
| + SequenceChecker sequence_checker_; |
| + |
| + // 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_ |