Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(737)

Unified Diff: base/trace_event/memory_peak_detector.h

Issue 2786373002: memory-infra: Add peak-detector skeleton. (Closed)
Patch Set: minor typos Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/trace_event/memory_dump_provider_info.h ('k') | base/trace_event/memory_peak_detector.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..b91429583322549619e7f91da7eb45af953696aa
--- /dev/null
+++ b/base/trace_event/memory_peak_detector.h
@@ -0,0 +1,139 @@
+// 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 Setup()
+ 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();
+
+ // Configures the peak detector, binding the polling tasks on the given
+ // thread. Setup() can be called several times, provided that: (1) Stop()
+ // 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
+ // given task runner when a memory peak is detected.
+ void Setup(const GetDumpProvidersFunction&,
+ const scoped_refptr<SequencedTaskRunner>&,
+ const OnPeakDetectedCallback&);
+
+ // Releases the |task_runner_| and the bound callbacks.
+ void TearDown();
+
+ // 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 Setup(), 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 TearDownInternal();
+ void ReloadDumpProvidersAndStartPollingIfNeeded();
+ void PollMemoryAndDetectPeak(uint32_t expected_generation);
+
+ // It is safe to call these testing methods only on the |task_runner_|.
+ State state_for_testing() const { return state_; }
+ uint32_t poll_tasks_count_for_testing() const {
+ return poll_tasks_count_for_testing_;
+ }
+
+ // The task runner where all the internal calls are posted onto. This field
+ // must be NOT be accessed by the tasks posted on the |task_runner_| because
+ // there might still be outstanding tasks on the |task_runner_| while this
+ // refptr is reset. This can only be safely accessed by the public methods
+ // above, which the client of this class is supposed to call sequentially.
+ scoped_refptr<SequencedTaskRunner> task_runner_;
+
+ // After the Setup() 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_;
+
+ // The generation is incremented every time the |state_| is changed and causes
+ // PollMemoryAndDetectPeak() to early out if the posted task doesn't match the
+ // most recent |generation_|. This allows to drop on the floor outstanding
+ // PostDelayedTask that refer to an old sequence that was later Stop()-ed or
+ // disabled because of NotifyMemoryDumpProvidersChanged().
+ uint32_t generation_;
+
+ 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_
« no previous file with comments | « base/trace_event/memory_dump_provider_info.h ('k') | base/trace_event/memory_peak_detector.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698