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

Unified Diff: base/trace_event/memory_peak_detector.cc

Issue 2786373002: memory-infra: Add peak-detector skeleton. (Closed)
Patch Set: remove seqchecker Created 3 years, 9 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
Index: base/trace_event/memory_peak_detector.cc
diff --git a/base/trace_event/memory_peak_detector.cc b/base/trace_event/memory_peak_detector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f9fb49a4faae93a00ea42dc005d4677b6cbc57d8
--- /dev/null
+++ b/base/trace_event/memory_peak_detector.cc
@@ -0,0 +1,156 @@
+// 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.
+
+#include "base/trace_event/memory_peak_detector.h"
+
+#include <stdint.h>
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "base/threading/sequenced_task_runner_handle.h"
+#include "base/time/time.h"
+#include "base/trace_event/memory_dump_provider_info.h"
+
+namespace {
+const uint32_t kInitialPollingIntervalMs = 1;
+} // namespace
+
+namespace base {
+namespace trace_event {
+
+// static
+MemoryPeakDetector* MemoryPeakDetector::GetInstance() {
+ static MemoryPeakDetector* instance = new MemoryPeakDetector();
+ return instance;
+}
+
+MemoryPeakDetector::MemoryPeakDetector()
+ : state_(NOT_INITIALIZED), polling_interval_ms_(0), poll_tasks_count_(0) {}
+
+MemoryPeakDetector::~MemoryPeakDetector() {
+ // This should be hit only in tests. In that case the test should call
+ // TearDownForTesting() first.
+ DCHECK_EQ(NOT_INITIALIZED, state_);
+}
+
+void MemoryPeakDetector::Initialize(
+ const GetDumpProvidersFunction& get_dump_providers_function,
+ const scoped_refptr<SequencedTaskRunner>& task_runner,
+ const OnPeakDetectedCallback& on_peak_detected_callback) {
+ DCHECK(!get_dump_providers_function.is_null());
+ DCHECK(!on_peak_detected_callback.is_null());
+ DCHECK(state_ == NOT_INITIALIZED || state_ == DISABLED);
+ DCHECK(dump_providers_.empty());
ssid 2017/04/03 17:55:27 DCHECK(task_runner)?
Primiano Tucci (use gerrit) 2017/04/03 20:28:15 Done.
+ get_dump_providers_function_ = get_dump_providers_function;
+ task_runner_ = task_runner;
+ on_peak_detected_callback_ = on_peak_detected_callback;
+ state_ = DISABLED;
+}
+
+void MemoryPeakDetector::TearDownForTesting() {
+ get_dump_providers_function_.Reset();
+ task_runner_ = nullptr;
+ on_peak_detected_callback_.Reset();
+ dump_providers_.clear();
+ state_ = NOT_INITIALIZED;
+ poll_tasks_count_ = 0;
+}
+
+void MemoryPeakDetector::Start() {
+ task_runner_->PostTask(
+ FROM_HERE, Bind(&MemoryPeakDetector::StartInternal, Unretained(this)));
+}
+
+void MemoryPeakDetector::StartInternal() {
+ DCHECK_EQ(DISABLED, state_);
+ state_ = ENABLED;
+ polling_interval_ms_ = kInitialPollingIntervalMs;
+
+ // If there are any dump providers available, NotifyMemoryDumpProvidersChanged
+ // will fetch them and start the polling. Otherwise this will remain in the
+ // ENABLED state and the actual polling will start on the next call to
+ // ReloadDumpProvidersAndStartPollingIfNeeded().
+ // Depending on the sandbox model, it is possible that no polling-capable dump
+ // providers will be ever available.
+ ReloadDumpProvidersAndStartPollingIfNeeded();
+}
+
+void MemoryPeakDetector::Stop() {
+ task_runner_->PostTask(
+ FROM_HERE, Bind(&MemoryPeakDetector::StopInternal, Unretained(this)));
+}
+
+void MemoryPeakDetector::StopInternal() {
+ DCHECK_NE(NOT_INITIALIZED, state_);
+ state_ = DISABLED;
+ dump_providers_.clear();
+}
+
+void MemoryPeakDetector::NotifyMemoryDumpProvidersChanged() {
+ // It is possible to call this before the first Initialize() call, in which
+ // case we want to just make this a noop. Start() will fetch the MDP list
+ // anyways later.
+ if (!task_runner_)
+ return;
+ task_runner_->PostTask(
+ FROM_HERE,
+ Bind(&MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded,
+ Unretained(this)));
+}
+
+void MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded() {
hjd 2017/04/03 13:25:30 I think the following sequence or one like it can
ssid 2017/04/03 17:55:27 We will never have the case where we go from havin
Primiano Tucci (use gerrit) 2017/04/03 20:28:15 Even if we do, this code should be robust enough t
ssid 2017/04/04 05:08:35 hm no. What Hector said was right. If we have a ca
Primiano Tucci (use gerrit) 2017/04/04 09:14:31 Okay you were both right. There is a corner case I
+ DCHECK_NE(NOT_INITIALIZED, state_);
+ if (state_ == DISABLED)
+ return; // Start() will re-fetch the MDP list later.
+
+ DCHECK((state_ == RUNNING && !dump_providers_.empty()) ||
+ (state_ == ENABLED && dump_providers_.empty()));
+
+ dump_providers_.clear();
+
+ // This is really MemoryDumpManager::GetDumpProvidersForPolling, % testing.
+ get_dump_providers_function_.Run(&dump_providers_);
+
+ if (state_ == ENABLED && !dump_providers_.empty()) {
+ // It's now time to start polling for realz.
+ state_ = RUNNING;
+ task_runner_->PostTask(
+ FROM_HERE,
+ Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this)));
+ } else if (state_ == RUNNING && dump_providers_.empty()) {
+ // Will cause the next PollMemoryAndDetectPeak() task to early return.
+ state_ = ENABLED;
+ }
+}
+
+void MemoryPeakDetector::PollMemoryAndDetectPeak() {
+ if (state_ != RUNNING)
+ return;
+
+ // We should never end up in a situation where state_ == RUNNING but all dump
+ // providers are gone.
+ DCHECK(!dump_providers_.empty());
+
+ poll_tasks_count_++;
+ uint64_t memory_total = 0;
+ for (const scoped_refptr<MemoryDumpProviderInfo>& mdp_info :
+ dump_providers_) {
+ DCHECK(mdp_info->options.is_fast_polling_supported);
+ uint64_t value = 0;
+ mdp_info->dump_provider->PollFastMemoryTotal(&value);
+ memory_total += value;
+ }
+ ignore_result(memory_total); // TODO(primiano): temporary until next CL.
+
+ // TODO(primiano): Move actual peak detection logic from the
+ // MemoryDumpScheduler in next CLs.
+
+ SequencedTaskRunnerHandle::Get()->PostDelayedTask(
+ FROM_HERE,
+ Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this)),
+ TimeDelta::FromMilliseconds(polling_interval_ms_));
+}
+
+} // namespace trace_event
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698