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

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

Issue 2793023002: memory-infra: port peak detection logic to MemoryPeakDetector (Closed)
Patch Set: fix lambdas for MSVC 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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/memory_peak_detector.h" 5 #include "base/trace_event/memory_peak_detector.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/sys_info.h"
11 #include "base/threading/sequenced_task_runner_handle.h" 12 #include "base/threading/sequenced_task_runner_handle.h"
12 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "base/trace_event/memory_dump_manager.h"
13 #include "base/trace_event/memory_dump_provider_info.h" 15 #include "base/trace_event/memory_dump_provider_info.h"
16 #include "base/trace_event/trace_event.h"
14 17
15 namespace base { 18 namespace base {
16 namespace trace_event { 19 namespace trace_event {
17 20
18 // static 21 // static
19 MemoryPeakDetector* MemoryPeakDetector::GetInstance() { 22 MemoryPeakDetector* MemoryPeakDetector::GetInstance() {
20 static MemoryPeakDetector* instance = new MemoryPeakDetector(); 23 static MemoryPeakDetector* instance = new MemoryPeakDetector();
21 return instance; 24 return instance;
22 } 25 }
23 26
24 MemoryPeakDetector::MemoryPeakDetector() 27 MemoryPeakDetector::MemoryPeakDetector()
25 : generation_(0), 28 : generation_(0),
26 state_(NOT_INITIALIZED), 29 state_(NOT_INITIALIZED),
27 polling_interval_ms_(0),
28 poll_tasks_count_for_testing_(0) {} 30 poll_tasks_count_for_testing_(0) {}
29 31
30 MemoryPeakDetector::~MemoryPeakDetector() { 32 MemoryPeakDetector::~MemoryPeakDetector() {
31 // This is hit only in tests, in which case the test is expected to TearDown() 33 // This is hit only in tests, in which case the test is expected to TearDown()
32 // cleanly and not leave the peak detector running. 34 // cleanly and not leave the peak detector running.
33 DCHECK_EQ(NOT_INITIALIZED, state_); 35 DCHECK_EQ(NOT_INITIALIZED, state_);
34 } 36 }
35 37
36 void MemoryPeakDetector::Setup( 38 void MemoryPeakDetector::Setup(
37 const GetDumpProvidersFunction& get_dump_providers_function, 39 const GetDumpProvidersFunction& get_dump_providers_function,
38 const scoped_refptr<SequencedTaskRunner>& task_runner, 40 const scoped_refptr<SequencedTaskRunner>& task_runner,
39 const OnPeakDetectedCallback& on_peak_detected_callback) { 41 const OnPeakDetectedCallback& on_peak_detected_callback) {
40 DCHECK(!get_dump_providers_function.is_null()); 42 DCHECK(!get_dump_providers_function.is_null());
41 DCHECK(task_runner); 43 DCHECK(task_runner);
42 DCHECK(!on_peak_detected_callback.is_null()); 44 DCHECK(!on_peak_detected_callback.is_null());
43 DCHECK(state_ == NOT_INITIALIZED || state_ == DISABLED); 45 DCHECK(state_ == NOT_INITIALIZED || state_ == DISABLED);
44 DCHECK(dump_providers_.empty()); 46 DCHECK(dump_providers_.empty());
45 get_dump_providers_function_ = get_dump_providers_function; 47 get_dump_providers_function_ = get_dump_providers_function;
46 task_runner_ = task_runner; 48 task_runner_ = task_runner;
47 on_peak_detected_callback_ = on_peak_detected_callback; 49 on_peak_detected_callback_ = on_peak_detected_callback;
48 state_ = DISABLED; 50 state_ = DISABLED;
51 config_ = {};
52 ResetPollHistory();
53
54 // Set threshold to 1% of total system memory.
55 static_threshold_bytes_ =
56 static_cast<uint64_t>(SysInfo::AmountOfPhysicalMemory()) / 100;
49 } 57 }
50 58
51 void MemoryPeakDetector::TearDown() { 59 void MemoryPeakDetector::TearDown() {
52 if (task_runner_) { 60 if (task_runner_) {
53 task_runner_->PostTask( 61 task_runner_->PostTask(
54 FROM_HERE, 62 FROM_HERE,
55 Bind(&MemoryPeakDetector::TearDownInternal, Unretained(this))); 63 Bind(&MemoryPeakDetector::TearDownInternal, Unretained(this)));
56 } 64 }
57 task_runner_ = nullptr; 65 task_runner_ = nullptr;
58 } 66 }
59 67
60 void MemoryPeakDetector::Start() { 68 void MemoryPeakDetector::Start(MemoryPeakDetector::Config config) {
61 task_runner_->PostTask( 69 if (!config.polling_interval_ms) {
62 FROM_HERE, Bind(&MemoryPeakDetector::StartInternal, Unretained(this))); 70 NOTREACHED();
71 return;
72 }
73 task_runner_->PostTask(FROM_HERE, Bind(&MemoryPeakDetector::StartInternal,
74 Unretained(this), config));
63 } 75 }
64 76
65 void MemoryPeakDetector::Stop() { 77 void MemoryPeakDetector::Stop() {
66 task_runner_->PostTask( 78 task_runner_->PostTask(
67 FROM_HERE, Bind(&MemoryPeakDetector::StopInternal, Unretained(this))); 79 FROM_HERE, Bind(&MemoryPeakDetector::StopInternal, Unretained(this)));
68 } 80 }
69 81
82 void MemoryPeakDetector::Clear() {
83 if (!task_runner_)
84 return; // Can be called before Setup().
85 task_runner_->PostTask(
86 FROM_HERE, Bind(&MemoryPeakDetector::ResetPollHistory, Unretained(this)));
87 }
88
70 void MemoryPeakDetector::NotifyMemoryDumpProvidersChanged() { 89 void MemoryPeakDetector::NotifyMemoryDumpProvidersChanged() {
71 // It is possible to call this before the first Setup() call, in which case
72 // we want to just make this a noop. The next Start() will fetch the MDP list.
73 if (!task_runner_) 90 if (!task_runner_)
74 return; 91 return; // Can be called before Setup().
75 task_runner_->PostTask( 92 task_runner_->PostTask(
76 FROM_HERE, 93 FROM_HERE,
77 Bind(&MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded, 94 Bind(&MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded,
78 Unretained(this))); 95 Unretained(this)));
79 } 96 }
80 97
81 void MemoryPeakDetector::StartInternal() { 98 void MemoryPeakDetector::StartInternal(MemoryPeakDetector::Config config) {
82 DCHECK_EQ(DISABLED, state_); 99 DCHECK_EQ(DISABLED, state_);
83 state_ = ENABLED; 100 state_ = ENABLED;
ssid 2017/04/05 21:23:46 DCHECK_GT(polling_interval_ms, 0) DCHECK_GT(min_ti
Primiano Tucci (use gerrit) 2017/04/06 20:07:10 This one is already done in ::Start() I added an e
ssid 2017/04/10 20:55:06 Acknowledged.
84 polling_interval_ms_ = 1; // TODO(primiano): temporary until next CL. 101 config_ = config;
102 ResetPollHistory();
ssid 2017/04/05 21:26:46 Should we test if Start() Stop() Start() happened,
Primiano Tucci (use gerrit) 2017/04/06 20:07:10 Done. See RestartClearsState test
85 103
86 // If there are any dump providers available, NotifyMemoryDumpProvidersChanged 104 // If there are any dump providers available,
87 // will fetch them and start the polling. Otherwise this will remain in the 105 // NotifyMemoryDumpProvidersChanged will fetch them and start the polling.
88 // ENABLED state and the actual polling will start on the next call to 106 // Otherwise this will remain in the ENABLED state and the actual polling
107 // will start on the next call to
89 // ReloadDumpProvidersAndStartPollingIfNeeded(). 108 // ReloadDumpProvidersAndStartPollingIfNeeded().
90 // Depending on the sandbox model, it is possible that no polling-capable dump 109 // Depending on the sandbox model, it is possible that no polling-capable
91 // providers will be ever available. 110 // dump providers will be ever available.
92 ReloadDumpProvidersAndStartPollingIfNeeded(); 111 ReloadDumpProvidersAndStartPollingIfNeeded();
93 } 112 }
94 113
95 void MemoryPeakDetector::StopInternal() { 114 void MemoryPeakDetector::StopInternal() {
96 DCHECK_NE(NOT_INITIALIZED, state_); 115 DCHECK_NE(NOT_INITIALIZED, state_);
97 state_ = DISABLED; 116 state_ = DISABLED;
98 ++generation_; 117 ++generation_;
99 dump_providers_.clear(); 118 dump_providers_.clear();
100 } 119 }
101 120
(...skipping 23 matching lines...) Expand all
125 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, 144 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak,
126 Unretained(this), ++generation_)); 145 Unretained(this), ++generation_));
127 } else if (state_ == RUNNING && dump_providers_.empty()) { 146 } else if (state_ == RUNNING && dump_providers_.empty()) {
128 // Will cause the next PollMemoryAndDetectPeak() task to early return. 147 // Will cause the next PollMemoryAndDetectPeak() task to early return.
129 state_ = ENABLED; 148 state_ = ENABLED;
130 ++generation_; 149 ++generation_;
131 } 150 }
132 } 151 }
133 152
134 void MemoryPeakDetector::PollMemoryAndDetectPeak(uint32_t expected_generation) { 153 void MemoryPeakDetector::PollMemoryAndDetectPeak(uint32_t expected_generation) {
135 if (state_ != RUNNING || expected_generation != generation_) 154 if (state_ != RUNNING || generation_ != expected_generation)
136 return; 155 return;
137 156
138 // We should never end up in a situation where state_ == RUNNING but all dump 157 // We should never end up in a situation where state_ == RUNNING but all dump
139 // providers are gone. 158 // providers are gone.
140 DCHECK(!dump_providers_.empty()); 159 DCHECK(!dump_providers_.empty());
141 160
142 poll_tasks_count_for_testing_++; 161 poll_tasks_count_for_testing_++;
143 uint64_t memory_total = 0; 162 uint64_t polled_mem_bytes = 0;
144 for (const scoped_refptr<MemoryDumpProviderInfo>& mdp_info : 163 for (const scoped_refptr<MemoryDumpProviderInfo>& mdp_info :
145 dump_providers_) { 164 dump_providers_) {
146 DCHECK(mdp_info->options.is_fast_polling_supported); 165 DCHECK(mdp_info->options.is_fast_polling_supported);
147 uint64_t value = 0; 166 uint64_t value = 0;
148 mdp_info->dump_provider->PollFastMemoryTotal(&value); 167 mdp_info->dump_provider->PollFastMemoryTotal(&value);
149 memory_total += value; 168 polled_mem_bytes += value;
150 } 169 }
151 ignore_result(memory_total); // TODO(primiano): temporary until next CL. 170 if (config_.enable_verbose_poll_tracing) {
171 TRACE_COUNTER1(MemoryDumpManager::kTraceCategory, "PolledMemoryKB",
172 polled_mem_bytes / 1024);
ssid 2017/04/05 21:23:46 MB is easier to read in TV.
Primiano Tucci (use gerrit) 2017/04/06 20:07:10 Ahh I see, good point. done
173 }
152 174
153 // TODO(primiano): Move actual peak detection logic from the 175 // Peak detection logic. Design doc: https://goo.gl/0kOU4A .
154 // MemoryDumpScheduler in next CLs. 176 bool is_peak = false;
177 if (skip_polls_ > 0) {
178 skip_polls_--;
179 } else if (last_dump_memory_total_ == 0) {
180 last_dump_memory_total_ = polled_mem_bytes;
181 } else if (polled_mem_bytes > 0) {
182 int64_t diff_from_last_dump = polled_mem_bytes - last_dump_memory_total_;
183
184 DCHECK_GT(static_threshold_bytes_, 0u);
185 is_peak = static_threshold_bytes_ &&
ssid 2017/04/05 21:23:46 why this condition after dcheck above? should we n
Primiano Tucci (use gerrit) 2017/04/06 20:07:10 I expect AmountOfPhysicalMemory can be 0 in tests
186 (diff_from_last_dump > static_threshold_bytes_);
187
188 if (!is_peak)
189 is_peak = DetectPeakUsingSlidingWindowStddev(polled_mem_bytes);
190 }
191
192 if (is_peak) {
193 TRACE_EVENT_INSTANT1(MemoryDumpManager::kTraceCategory,
194 "Peak memory dump detected", TRACE_EVENT_SCOPE_PROCESS,
ssid 2017/04/05 21:23:46 "Peak memory detected"? Also can we keep this in M
Primiano Tucci (use gerrit) 2017/04/06 20:07:10 Done.
195 "PolledMemoryKB", polled_mem_bytes / 1024);
196 on_peak_detected_callback_.Run();
197 ResetPollHistory();
198 last_dump_memory_total_ = polled_mem_bytes;
199 }
155 200
156 SequencedTaskRunnerHandle::Get()->PostDelayedTask( 201 SequencedTaskRunnerHandle::Get()->PostDelayedTask(
157 FROM_HERE, 202 FROM_HERE,
158 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this), 203 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this),
159 expected_generation), 204 expected_generation),
160 TimeDelta::FromMilliseconds(polling_interval_ms_)); 205 TimeDelta::FromMilliseconds(config_.polling_interval_ms));
206 }
207
208 bool MemoryPeakDetector::DetectPeakUsingSlidingWindowStddev(
209 uint64_t polled_mem_bytes) {
210 DCHECK(polled_mem_bytes);
211 samples_bytes_[samples_index_] = polled_mem_bytes;
212 samples_index_ = (samples_index_ + 1) % kSlidingWindowNumSamples;
213 uint64_t mean = 0;
214 for (uint32_t i = 0; i < kSlidingWindowNumSamples; ++i) {
215 if (samples_bytes_[i] == 0)
216 return false; // Not enough samples to detect peaks.
217 mean += samples_bytes_[i];
218 }
219 mean /= kSlidingWindowNumSamples;
220 uint64_t variance = 0;
ssid 2017/04/05 21:23:46 Either make this double or make the sample kb. A s
Primiano Tucci (use gerrit) 2017/04/06 20:07:10 Oh excellent point.
221 for (uint32_t i = 0; i < kSlidingWindowNumSamples; ++i) {
222 const uint64_t deviation = samples_bytes_[i] - mean;
223 variance += deviation * deviation;
224 }
225 variance /= kSlidingWindowNumSamples;
226
227 // If stddev is less than 0.2% then we consider that the process is inactive.
228 if (variance < (mean / 500) * (mean / 500))
229 return false;
230
231 // (mean + 3.69 * stddev) corresponds to a value that is higher than current
232 // sample with 99.99% probability.
233 const uint64_t cur_sample_deviation = polled_mem_bytes - mean;
234 return cur_sample_deviation * cur_sample_deviation > (3.69 * 3.69 * variance);
235 }
236
237 void MemoryPeakDetector::ResetPollHistory() {
238 skip_polls_ = 0;
239 last_dump_memory_total_ = 0;
ssid 2017/04/05 21:23:46 hm shouldn't this be set to the current value inst
Primiano Tucci (use gerrit) 2017/04/06 20:07:10 Does it make a difference in practice? If I set th
ssid 2017/04/10 20:55:06 The issue is: If we detect peak in browser and ren
Primiano Tucci (use gerrit) 2017/04/11 11:12:09 Okay I changes this, but still not sure this makes
240 memset(samples_bytes_, 0, sizeof(samples_bytes_));
241 samples_index_ = 0;
242 if (config_.polling_interval_ms > 0) {
ssid 2017/04/05 21:23:46 Maybe we should check for state_ == running or ena
Primiano Tucci (use gerrit) 2017/04/06 20:07:10 No I use this also to clearup during Setup().
ssid 2017/04/10 20:55:06 hm that is true. Maybe we could just do it in next
Primiano Tucci (use gerrit) 2017/04/11 11:12:09 I just added a bool bool keep_last_sample here, so
243 skip_polls_ =
244 (config_.min_time_between_peaks_ms + config_.polling_interval_ms - 1) /
245 config_.polling_interval_ms;
246 }
247 }
248
249 void MemoryPeakDetector::SetStaticThresholdForTesting(
250 uint64_t static_threshold_bytes) {
251 DCHECK_EQ(DISABLED, state_);
252 static_threshold_bytes_ = static_threshold_bytes;
161 } 253 }
162 254
163 } // namespace trace_event 255 } // namespace trace_event
164 } // namespace base 256 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698