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

Side by Side Diff: base/trace_event/memory_peak_detector.cc

Issue 2786373002: memory-infra: Add peak-detector skeleton. (Closed)
Patch Set: . 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/trace_event/memory_peak_detector.h"
6
7 #include <stdint.h>
8
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/threading/sequenced_task_runner_handle.h"
12 #include "base/time/time.h"
13 #include "base/trace_event/memory_dump_provider_info.h"
14
15 namespace {
16 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.
17 } // namespace
18
19 namespace base {
20 namespace trace_event {
21
22 // static
23 MemoryPeakDetector* MemoryPeakDetector::GetInstance() {
24 static MemoryPeakDetector* instance = new MemoryPeakDetector();
25 return instance;
26 }
27
28 MemoryPeakDetector::MemoryPeakDetector()
29 : state_(NOT_INITIALIZED), polling_interval_ms_(0), poll_tasks_count_(0) {
30 // Bind the checker to the Initialize() call. Where this get constructed is
31 // irrelevant.
32 sequence_checker_.DetachFromSequence();
33 }
34
35 MemoryPeakDetector::~MemoryPeakDetector() {
36 // This should be hit only in tests. In that case the test should call
37 // TearDownForTesting() first.
38 DCHECK(sequence_checker_.CalledOnValidSequence());
39 DCHECK_EQ(NOT_INITIALIZED, state_);
40 }
41
42 void MemoryPeakDetector::Initialize(
43 const GetDumpProvidersFunction& get_dump_providers_function,
44 const scoped_refptr<SequencedTaskRunner>& task_runner,
45 const OnPeakDetectedCallback& on_peak_detected_callback) {
46 DCHECK(sequence_checker_.CalledOnValidSequence());
47 DCHECK(!get_dump_providers_function.is_null());
48 DCHECK(!on_peak_detected_callback.is_null());
49 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
50 DCHECK(dump_providers_.empty());
51 get_dump_providers_function_ = get_dump_providers_function;
52 task_runner_ = task_runner;
53 on_peak_detected_callback_ = on_peak_detected_callback;
54 state_ = DISABLED;
55 }
56
57 void MemoryPeakDetector::TearDownForTesting() {
58 get_dump_providers_function_.Reset();
59 task_runner_ = nullptr;
60 on_peak_detected_callback_.Reset();
61 dump_providers_.clear();
62 state_ = NOT_INITIALIZED;
63 poll_tasks_count_ = 0;
64 }
65
66 void MemoryPeakDetector::Start() {
67 DCHECK(sequence_checker_.CalledOnValidSequence());
68 task_runner_->PostTask(
69 FROM_HERE, Bind(&MemoryPeakDetector::StartInternal, Unretained(this)));
70 }
71
72 void MemoryPeakDetector::StartInternal() {
73 DCHECK_EQ(DISABLED, state_);
74 state_ = ENABLED;
75 polling_interval_ms_ = kInitialPollingIntervalMs;
76
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.
77 // If there are any dump providers available, NotifyMemoryDumpProvidersChanged
78 // will fetch them and start the polling. Otherwise this will remain in the
79 // ENABLED state and the actual polling will start on the next call to
80 // ReloadDumpProvidersAndStartPollingIfNeeded().
81 // Depending on the sandbox model, it is possible that no polling-capable dump
82 // providers will be ever available. Deferring the polling, hence, this is not
83 // 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
84 ReloadDumpProvidersAndStartPollingIfNeeded();
85 }
86
87 void MemoryPeakDetector::Stop() {
88 DCHECK(sequence_checker_.CalledOnValidSequence());
89 task_runner_->PostTask(
90 FROM_HERE, Bind(&MemoryPeakDetector::StopInternal, Unretained(this)));
91 }
92
93 void MemoryPeakDetector::StopInternal() {
94 DCHECK_NE(NOT_INITIALIZED, state_);
95 state_ = DISABLED;
96 dump_providers_.clear();
97 }
98
99 void MemoryPeakDetector::NotifyMemoryDumpProvidersChanged() {
100 DCHECK(sequence_checker_.CalledOnValidSequence());
101 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.
102 FROM_HERE,
103 Bind(&MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded,
104 Unretained(this)));
105 }
106
107 void MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded() {
108 DCHECK_NE(NOT_INITIALIZED, state_);
109 if (state_ == DISABLED)
110 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.
111
112 DCHECK((state_ == RUNNING && !dump_providers_.empty()) ||
113 (state_ == ENABLED && dump_providers_.empty()));
114
115 // This is really MemoryDumpManager::GetDumpProvidersForPolling, % testing.
116 dump_providers_.clear();
117 get_dump_providers_function_.Run(&dump_providers_);
118
119 if (state_ == ENABLED && !dump_providers_.empty()) {
120 // It's now time to start polling for realz.
121 state_ = RUNNING;
122 task_runner_->PostTask(
123 FROM_HERE,
124 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this)));
125 } else if (state_ == RUNNING && dump_providers_.empty()) {
126 // 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.
127 state_ = ENABLED;
128 }
129 }
130
131 void MemoryPeakDetector::PollMemoryAndDetectPeak() {
132 if (state_ != RUNNING)
133 return;
134
135 // We should never end up in a situation where state_ == RUNNING but all dump
136 // providers are gone.
137 DCHECK(!dump_providers_.empty());
138
139 poll_tasks_count_++;
140 uint64_t memory_total = 0;
141 for (const scoped_refptr<MemoryDumpProviderInfo>& mdp_info :
142 dump_providers_) {
143 DCHECK(mdp_info->options.is_fast_polling_supported);
144 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
145 }
146
147 // TODO(primiano): Move actual peak detection logic from the
148 // MemoryDumpScheduler in next CLs.
149
150 SequencedTaskRunnerHandle::Get()->PostDelayedTask(
151 FROM_HERE,
152 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this)),
153 TimeDelta::FromMilliseconds(polling_interval_ms_));
154 }
155
156 } // namespace trace_event
157 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698