OLD | NEW |
---|---|
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> | |
8 | |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/sys_info.h" | |
11 #include "base/threading/sequenced_task_runner_handle.h" | 10 #include "base/threading/sequenced_task_runner_handle.h" |
12 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "base/trace_event/memory_dump_manager.h" | |
13 #include "base/trace_event/memory_dump_provider_info.h" | 13 #include "base/trace_event/memory_dump_provider_info.h" |
14 #include "base/trace_event/trace_event.h" | |
15 #include "build/build_config.h" | |
14 | 16 |
15 namespace base { | 17 namespace base { |
16 namespace trace_event { | 18 namespace trace_event { |
17 | 19 |
18 // static | 20 // static |
19 MemoryPeakDetector* MemoryPeakDetector::GetInstance() { | 21 MemoryPeakDetector* MemoryPeakDetector::GetInstance() { |
20 static MemoryPeakDetector* instance = new MemoryPeakDetector(); | 22 static MemoryPeakDetector* instance = new MemoryPeakDetector(); |
21 return instance; | 23 return instance; |
22 } | 24 } |
23 | 25 |
24 MemoryPeakDetector::MemoryPeakDetector() | 26 MemoryPeakDetector::MemoryPeakDetector() |
25 : generation_(0), | 27 : generation_(0), |
26 state_(NOT_INITIALIZED), | 28 state_(NOT_INITIALIZED), |
27 polling_interval_ms_(0), | |
28 poll_tasks_count_for_testing_(0) {} | 29 poll_tasks_count_for_testing_(0) {} |
29 | 30 |
30 MemoryPeakDetector::~MemoryPeakDetector() { | 31 MemoryPeakDetector::~MemoryPeakDetector() { |
31 // This is hit only in tests, in which case the test is expected to TearDown() | 32 // This is hit only in tests, in which case the test is expected to TearDown() |
32 // cleanly and not leave the peak detector running. | 33 // cleanly and not leave the peak detector running. |
33 DCHECK_EQ(NOT_INITIALIZED, state_); | 34 DCHECK_EQ(NOT_INITIALIZED, state_); |
34 } | 35 } |
35 | 36 |
36 void MemoryPeakDetector::Setup( | 37 void MemoryPeakDetector::Setup( |
37 const GetDumpProvidersFunction& get_dump_providers_function, | 38 const GetDumpProvidersFunction& get_dump_providers_function, |
38 const scoped_refptr<SequencedTaskRunner>& task_runner, | 39 const scoped_refptr<SequencedTaskRunner>& task_runner, |
39 const OnPeakDetectedCallback& on_peak_detected_callback) { | 40 const OnPeakDetectedCallback& on_peak_detected_callback) { |
40 DCHECK(!get_dump_providers_function.is_null()); | 41 DCHECK(!get_dump_providers_function.is_null()); |
41 DCHECK(task_runner); | 42 DCHECK(task_runner); |
42 DCHECK(!on_peak_detected_callback.is_null()); | 43 DCHECK(!on_peak_detected_callback.is_null()); |
43 DCHECK(state_ == NOT_INITIALIZED || state_ == DISABLED); | 44 DCHECK(state_ == NOT_INITIALIZED || state_ == DISABLED); |
44 DCHECK(dump_providers_.empty()); | 45 DCHECK(dump_providers_.empty()); |
45 get_dump_providers_function_ = get_dump_providers_function; | 46 get_dump_providers_function_ = get_dump_providers_function; |
46 task_runner_ = task_runner; | 47 task_runner_ = task_runner; |
47 on_peak_detected_callback_ = on_peak_detected_callback; | 48 on_peak_detected_callback_ = on_peak_detected_callback; |
48 state_ = DISABLED; | 49 state_ = DISABLED; |
50 config_ = {}; | |
51 ResetPollHistory(); | |
52 | |
53 #if !defined(OS_NACL) | |
54 // Set threshold to 1% of total system memory. | |
55 static_threshold_bytes_ = | |
56 static_cast<uint64_t>(SysInfo::AmountOfPhysicalMemory()) / 100; | |
57 #else | |
58 static_threshold_bytes_ = 0; | |
ssid
2017/04/10 20:55:06
Not a big deal, but why not have a default value h
Primiano Tucci (use gerrit)
2017/04/11 11:12:09
Done
| |
59 #endif | |
49 } | 60 } |
50 | 61 |
51 void MemoryPeakDetector::TearDown() { | 62 void MemoryPeakDetector::TearDown() { |
52 if (task_runner_) { | 63 if (task_runner_) { |
53 task_runner_->PostTask( | 64 task_runner_->PostTask( |
54 FROM_HERE, | 65 FROM_HERE, |
55 Bind(&MemoryPeakDetector::TearDownInternal, Unretained(this))); | 66 Bind(&MemoryPeakDetector::TearDownInternal, Unretained(this))); |
56 } | 67 } |
57 task_runner_ = nullptr; | 68 task_runner_ = nullptr; |
58 } | 69 } |
59 | 70 |
60 void MemoryPeakDetector::Start() { | 71 void MemoryPeakDetector::Start(MemoryPeakDetector::Config config) { |
61 task_runner_->PostTask( | 72 if (!config.polling_interval_ms) { |
62 FROM_HERE, Bind(&MemoryPeakDetector::StartInternal, Unretained(this))); | 73 NOTREACHED(); |
74 return; | |
75 } | |
76 task_runner_->PostTask(FROM_HERE, Bind(&MemoryPeakDetector::StartInternal, | |
77 Unretained(this), config)); | |
63 } | 78 } |
64 | 79 |
65 void MemoryPeakDetector::Stop() { | 80 void MemoryPeakDetector::Stop() { |
66 task_runner_->PostTask( | 81 task_runner_->PostTask( |
67 FROM_HERE, Bind(&MemoryPeakDetector::StopInternal, Unretained(this))); | 82 FROM_HERE, Bind(&MemoryPeakDetector::StopInternal, Unretained(this))); |
68 } | 83 } |
69 | 84 |
85 void MemoryPeakDetector::Clear() { | |
86 if (!task_runner_) | |
87 return; // Can be called before Setup(). | |
88 task_runner_->PostTask( | |
89 FROM_HERE, Bind(&MemoryPeakDetector::ResetPollHistory, Unretained(this))); | |
90 } | |
91 | |
70 void MemoryPeakDetector::NotifyMemoryDumpProvidersChanged() { | 92 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_) | 93 if (!task_runner_) |
74 return; | 94 return; // Can be called before Setup(). |
75 task_runner_->PostTask( | 95 task_runner_->PostTask( |
76 FROM_HERE, | 96 FROM_HERE, |
77 Bind(&MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded, | 97 Bind(&MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded, |
78 Unretained(this))); | 98 Unretained(this))); |
79 } | 99 } |
80 | 100 |
81 void MemoryPeakDetector::StartInternal() { | 101 void MemoryPeakDetector::StartInternal(MemoryPeakDetector::Config config) { |
82 DCHECK_EQ(DISABLED, state_); | 102 DCHECK_EQ(DISABLED, state_); |
83 state_ = ENABLED; | 103 state_ = ENABLED; |
84 polling_interval_ms_ = 1; // TODO(primiano): temporary until next CL. | 104 config_ = config; |
105 ResetPollHistory(); | |
85 | 106 |
86 // If there are any dump providers available, NotifyMemoryDumpProvidersChanged | 107 // If there are any dump providers available, |
87 // will fetch them and start the polling. Otherwise this will remain in the | 108 // NotifyMemoryDumpProvidersChanged will fetch them and start the polling. |
88 // ENABLED state and the actual polling will start on the next call to | 109 // Otherwise this will remain in the ENABLED state and the actual polling |
110 // will start on the next call to | |
89 // ReloadDumpProvidersAndStartPollingIfNeeded(). | 111 // ReloadDumpProvidersAndStartPollingIfNeeded(). |
90 // Depending on the sandbox model, it is possible that no polling-capable dump | 112 // Depending on the sandbox model, it is possible that no polling-capable |
91 // providers will be ever available. | 113 // dump providers will be ever available. |
92 ReloadDumpProvidersAndStartPollingIfNeeded(); | 114 ReloadDumpProvidersAndStartPollingIfNeeded(); |
93 } | 115 } |
94 | 116 |
95 void MemoryPeakDetector::StopInternal() { | 117 void MemoryPeakDetector::StopInternal() { |
96 DCHECK_NE(NOT_INITIALIZED, state_); | 118 DCHECK_NE(NOT_INITIALIZED, state_); |
97 state_ = DISABLED; | 119 state_ = DISABLED; |
98 ++generation_; | 120 ++generation_; |
99 dump_providers_.clear(); | 121 dump_providers_.clear(); |
100 } | 122 } |
101 | 123 |
(...skipping 23 matching lines...) Expand all Loading... | |
125 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, | 147 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, |
126 Unretained(this), ++generation_)); | 148 Unretained(this), ++generation_)); |
127 } else if (state_ == RUNNING && dump_providers_.empty()) { | 149 } else if (state_ == RUNNING && dump_providers_.empty()) { |
128 // Will cause the next PollMemoryAndDetectPeak() task to early return. | 150 // Will cause the next PollMemoryAndDetectPeak() task to early return. |
129 state_ = ENABLED; | 151 state_ = ENABLED; |
130 ++generation_; | 152 ++generation_; |
131 } | 153 } |
132 } | 154 } |
133 | 155 |
134 void MemoryPeakDetector::PollMemoryAndDetectPeak(uint32_t expected_generation) { | 156 void MemoryPeakDetector::PollMemoryAndDetectPeak(uint32_t expected_generation) { |
135 if (state_ != RUNNING || expected_generation != generation_) | 157 if (state_ != RUNNING || generation_ != expected_generation) |
136 return; | 158 return; |
137 | 159 |
138 // We should never end up in a situation where state_ == RUNNING but all dump | 160 // We should never end up in a situation where state_ == RUNNING but all dump |
139 // providers are gone. | 161 // providers are gone. |
140 DCHECK(!dump_providers_.empty()); | 162 DCHECK(!dump_providers_.empty()); |
141 | 163 |
142 poll_tasks_count_for_testing_++; | 164 poll_tasks_count_for_testing_++; |
143 uint64_t memory_total = 0; | 165 uint64_t polled_mem_bytes = 0; |
144 for (const scoped_refptr<MemoryDumpProviderInfo>& mdp_info : | 166 for (const scoped_refptr<MemoryDumpProviderInfo>& mdp_info : |
145 dump_providers_) { | 167 dump_providers_) { |
146 DCHECK(mdp_info->options.is_fast_polling_supported); | 168 DCHECK(mdp_info->options.is_fast_polling_supported); |
147 uint64_t value = 0; | 169 uint64_t value = 0; |
148 mdp_info->dump_provider->PollFastMemoryTotal(&value); | 170 mdp_info->dump_provider->PollFastMemoryTotal(&value); |
149 memory_total += value; | 171 polled_mem_bytes += value; |
150 } | 172 } |
151 ignore_result(memory_total); // TODO(primiano): temporary until next CL. | 173 if (config_.enable_verbose_poll_tracing) { |
174 TRACE_COUNTER1(MemoryDumpManager::kTraceCategory, "PolledMemoryMB", | |
175 polled_mem_bytes / 1024 / 1024); | |
176 } | |
152 | 177 |
153 // TODO(primiano): Move actual peak detection logic from the | 178 // Peak detection logic. Design doc: https://goo.gl/0kOU4A . |
154 // MemoryDumpScheduler in next CLs. | 179 bool is_peak = false; |
180 if (skip_polls_ > 0) { | |
181 skip_polls_--; | |
182 } else if (last_dump_memory_total_ == 0) { | |
183 last_dump_memory_total_ = polled_mem_bytes; | |
184 } else if (polled_mem_bytes > 0) { | |
185 int64_t diff_from_last_dump = polled_mem_bytes - last_dump_memory_total_; | |
155 | 186 |
187 DCHECK_GT(static_threshold_bytes_, 0u); | |
188 is_peak = static_threshold_bytes_ && | |
189 (diff_from_last_dump > static_threshold_bytes_); | |
190 | |
191 if (!is_peak) | |
192 is_peak = DetectPeakUsingSlidingWindowStddev(polled_mem_bytes); | |
193 } | |
194 | |
195 if (is_peak) { | |
196 TRACE_EVENT_INSTANT1(MemoryDumpManager::kTraceCategory, | |
197 "Peak memory detected", TRACE_EVENT_SCOPE_PROCESS, | |
198 "PolledMemoryMB", polled_mem_bytes / 1024 / 1024); | |
199 on_peak_detected_callback_.Run(); | |
200 ResetPollHistory(); | |
201 last_dump_memory_total_ = polled_mem_bytes; | |
202 } | |
203 | |
204 DCHECK_GT(config_.polling_interval_ms, 0u); | |
156 SequencedTaskRunnerHandle::Get()->PostDelayedTask( | 205 SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
157 FROM_HERE, | 206 FROM_HERE, |
158 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this), | 207 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this), |
159 expected_generation), | 208 expected_generation), |
160 TimeDelta::FromMilliseconds(polling_interval_ms_)); | 209 TimeDelta::FromMilliseconds(config_.polling_interval_ms)); |
210 } | |
211 | |
212 bool MemoryPeakDetector::DetectPeakUsingSlidingWindowStddev( | |
213 uint64_t polled_mem_bytes) { | |
214 DCHECK(polled_mem_bytes); | |
215 samples_bytes_[samples_index_] = polled_mem_bytes; | |
216 samples_index_ = (samples_index_ + 1) % kSlidingWindowNumSamples; | |
217 float mean = 0; | |
218 for (uint32_t i = 0; i < kSlidingWindowNumSamples; ++i) { | |
219 if (samples_bytes_[i] == 0) | |
220 return false; // Not enough samples to detect peaks. | |
221 mean += samples_bytes_[i]; | |
222 } | |
223 mean /= kSlidingWindowNumSamples; | |
224 float variance = 0; | |
225 for (uint32_t i = 0; i < kSlidingWindowNumSamples; ++i) { | |
226 const float deviation = samples_bytes_[i] - mean; | |
227 variance += deviation * deviation; | |
228 } | |
229 variance /= kSlidingWindowNumSamples; | |
230 | |
231 // If stddev is less than 0.2% then we consider that the process is inactive. | |
232 if (variance < (mean / 500) * (mean / 500)) | |
233 return false; | |
234 | |
235 // (mean + 3.69 * stddev) corresponds to a value that is higher than current | |
236 // sample with 99.99% probability. | |
237 const float cur_sample_deviation = polled_mem_bytes - mean; | |
238 return cur_sample_deviation * cur_sample_deviation > (3.69 * 3.69 * variance); | |
239 } | |
240 | |
241 void MemoryPeakDetector::ResetPollHistory() { | |
242 skip_polls_ = 0; | |
243 last_dump_memory_total_ = 0; | |
244 memset(samples_bytes_, 0, sizeof(samples_bytes_)); | |
245 samples_index_ = 0; | |
246 if (config_.polling_interval_ms > 0) { | |
247 skip_polls_ = | |
248 (config_.min_time_between_peaks_ms + config_.polling_interval_ms - 1) / | |
249 config_.polling_interval_ms; | |
250 } | |
251 } | |
252 | |
253 void MemoryPeakDetector::SetStaticThresholdForTesting( | |
254 uint64_t static_threshold_bytes) { | |
255 DCHECK_EQ(DISABLED, state_); | |
256 static_threshold_bytes_ = static_threshold_bytes; | |
161 } | 257 } |
162 | 258 |
163 } // namespace trace_event | 259 } // namespace trace_event |
164 } // namespace base | 260 } // namespace base |
OLD | NEW |