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

Unified Diff: base/trace_event/memory_peak_detector.cc

Issue 2786373002: memory-infra: Add peak-detector skeleton. (Closed)
Patch Set: . 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..7b49b55ab2599a71b66a38ea22adb3a0c262bad2
--- /dev/null
+++ b/base/trace_event/memory_peak_detector.cc
@@ -0,0 +1,157 @@
+// 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;
Primiano Tucci (use gerrit) 2017/03/31 16:51:07 will sort this out in the next CL when I move the
ssid 2017/03/31 18:03:35 Acknowledged.
+} // 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) {
+ // Bind the checker to the Initialize() call. Where this get constructed is
+ // irrelevant.
+ sequence_checker_.DetachFromSequence();
+}
+
+MemoryPeakDetector::~MemoryPeakDetector() {
+ // This should be hit only in tests. In that case the test should call
+ // TearDownForTesting() first.
+ DCHECK(sequence_checker_.CalledOnValidSequence());
+ 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(sequence_checker_.CalledOnValidSequence());
+ DCHECK(!get_dump_providers_function.is_null());
+ DCHECK(!on_peak_detected_callback.is_null());
+ DCHECK(state_ == NOT_INITIALIZED || state_ == DISABLED);
ssid 2017/03/31 18:03:35 Why initialize multiple times? shouldn't this be s
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 TearDown is only for tests. initialize multiple ti
ssid 2017/04/03 17:55:27 If we call it multiple times, maybe Setup()? also
Primiano Tucci (use gerrit) 2017/04/03 20:28:15 +1, done
+ DCHECK(dump_providers_.empty());
+ 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() {
+ DCHECK(sequence_checker_.CalledOnValidSequence());
+ task_runner_->PostTask(
+ FROM_HERE, Bind(&MemoryPeakDetector::StartInternal, Unretained(this)));
+}
+
+void MemoryPeakDetector::StartInternal() {
+ DCHECK_EQ(DISABLED, state_);
+ state_ = ENABLED;
+ polling_interval_ms_ = kInitialPollingIntervalMs;
+
ssid 2017/03/31 18:03:35 You'd also need to set poll_count = 0;
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 That is for testing only and I only tear it down i
ssid 2017/04/03 17:55:27 Does it mean this function will not support multip
Primiano Tucci (use gerrit) 2017/04/03 20:28:15 I see where the confusion comes from. poll_count_
ssid 2017/04/04 05:08:35 Acknowledged.
+ // 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. Deferring the polling, hence, this is not
+ // just a minor optimization.
ssid 2017/03/31 18:03:35 Do we need a comment that says "this is not just a
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 That was to prevent a future myself from removing
+ ReloadDumpProvidersAndStartPollingIfNeeded();
+}
+
+void MemoryPeakDetector::Stop() {
+ DCHECK(sequence_checker_.CalledOnValidSequence());
+ 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() {
+ DCHECK(sequence_checker_.CalledOnValidSequence());
+ task_runner_->PostTask(
ssid 2017/03/31 18:03:35 What if we registered the first dump provider befo
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 Ah you are right, I have to make this a no-op (ear
ssid 2017/04/03 17:55:27 Acknowledged.
+ FROM_HERE,
+ Bind(&MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded,
+ Unretained(this)));
+}
+
+void MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded() {
+ DCHECK_NE(NOT_INITIALIZED, state_);
+ if (state_ == DISABLED)
+ return; // Start will deal with this later.
ssid 2017/03/31 18:03:35 nit: Start()
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 Done.
+
+ DCHECK((state_ == RUNNING && !dump_providers_.empty()) ||
+ (state_ == ENABLED && dump_providers_.empty()));
+
+ // This is really MemoryDumpManager::GetDumpProvidersForPolling, % testing.
+ dump_providers_.clear();
+ 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 th next PollMemoryAndDetectPeak() task to early return.
ssid 2017/03/31 18:03:35 the
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 Done.
+ 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);
+ mdp_info->dump_provider->PollFastMemoryTotal(&memory_total);
ssid 2017/03/31 18:03:35 uint64_t value = 0; mdpinfo->dump_provider->Po
Primiano Tucci (use gerrit) 2017/03/31 20:01:54 Yup, I would have replaced allt this part next, bu
+ }
+
+ // 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