Chromium Code Reviews| 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) | |
|
hjd
2017/04/06 11:33:48
The previous check also excluded iOS I don't know
Primiano Tucci (use gerrit)
2017/04/06 20:07:10
This one seems defined for ios (base/sys_info_ios.
| |
| 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; | |
| 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, "PolledMemoryKB", | |
| 175 polled_mem_bytes / 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_; | |
| 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 dump detected", TRACE_EVENT_SCOPE_PROCESS, | |
| 198 "PolledMemoryKB", polled_mem_bytes / 1024); | |
| 199 on_peak_detected_callback_.Run(); | |
| 200 ResetPollHistory(); | |
| 201 last_dump_memory_total_ = polled_mem_bytes; | |
| 202 } | |
| 155 | 203 |
| 156 SequencedTaskRunnerHandle::Get()->PostDelayedTask( | 204 SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
| 157 FROM_HERE, | 205 FROM_HERE, |
| 158 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this), | 206 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this), |
| 159 expected_generation), | 207 expected_generation), |
| 160 TimeDelta::FromMilliseconds(polling_interval_ms_)); | 208 TimeDelta::FromMilliseconds(config_.polling_interval_ms)); |
| 209 } | |
| 210 | |
| 211 bool MemoryPeakDetector::DetectPeakUsingSlidingWindowStddev( | |
| 212 uint64_t polled_mem_bytes) { | |
| 213 DCHECK(polled_mem_bytes); | |
| 214 samples_bytes_[samples_index_] = polled_mem_bytes; | |
| 215 samples_index_ = (samples_index_ + 1) % kSlidingWindowNumSamples; | |
| 216 uint64_t mean = 0; | |
| 217 for (uint32_t i = 0; i < kSlidingWindowNumSamples; ++i) { | |
| 218 if (samples_bytes_[i] == 0) | |
|
hjd
2017/04/06 11:33:48
We won't be able to detect a spike from 0 with thi
Primiano Tucci (use gerrit)
2017/04/06 20:07:10
Yeah. 0 memory usage is quite rare :)
| |
| 219 return false; // Not enough samples to detect peaks. | |
| 220 mean += samples_bytes_[i]; | |
| 221 } | |
| 222 mean /= kSlidingWindowNumSamples; | |
| 223 uint64_t variance = 0; | |
| 224 for (uint32_t i = 0; i < kSlidingWindowNumSamples; ++i) { | |
| 225 const uint64_t deviation = samples_bytes_[i] - mean; | |
| 226 variance += deviation * deviation; | |
| 227 } | |
| 228 variance /= kSlidingWindowNumSamples; | |
| 229 | |
| 230 // If stddev is less than 0.2% then we consider that the process is inactive. | |
| 231 if (variance < (mean / 500) * (mean / 500)) | |
| 232 return false; | |
| 233 | |
| 234 // (mean + 3.69 * stddev) corresponds to a value that is higher than current | |
| 235 // sample with 99.99% probability. | |
| 236 const uint64_t cur_sample_deviation = polled_mem_bytes - mean; | |
| 237 return cur_sample_deviation * cur_sample_deviation > (3.69 * 3.69 * variance); | |
| 238 } | |
| 239 | |
| 240 void MemoryPeakDetector::ResetPollHistory() { | |
| 241 skip_polls_ = 0; | |
| 242 last_dump_memory_total_ = 0; | |
| 243 memset(samples_bytes_, 0, sizeof(samples_bytes_)); | |
| 244 samples_index_ = 0; | |
| 245 if (config_.polling_interval_ms > 0) { | |
| 246 skip_polls_ = | |
| 247 (config_.min_time_between_peaks_ms + config_.polling_interval_ms - 1) / | |
| 248 config_.polling_interval_ms; | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 void MemoryPeakDetector::SetStaticThresholdForTesting( | |
| 253 uint64_t static_threshold_bytes) { | |
| 254 DCHECK_EQ(DISABLED, state_); | |
| 255 static_threshold_bytes_ = static_threshold_bytes; | |
| 161 } | 256 } |
| 162 | 257 |
| 163 } // namespace trace_event | 258 } // namespace trace_event |
| 164 } // namespace base | 259 } // namespace base |
| OLD | NEW |