| 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_dump_scheduler.h" | 5 #include "base/trace_event/memory_dump_scheduler.h" |
| 6 | 6 |
| 7 #include "base/process/process_metrics.h" |
| 7 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "base/trace_event/memory_dump_manager.h" | 10 #include "base/trace_event/memory_dump_manager.h" |
| 10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 namespace trace_event { | 14 namespace trace_event { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 // Threshold on increase in memory from last dump beyond which a new dump must | 17 // Threshold on increase in memory from last dump beyond which a new dump must |
| 17 // be triggered. | 18 // be triggered. |
| 18 int64_t kMemoryIncreaseThreshold = 50 * 1024 * 1024; // 50MiB | 19 int64_t kDefaultMemoryIncreaseThreshold = 50 * 1024 * 1024; // 50MiB |
| 19 const uint32_t kMemoryTotalsPollingInterval = 25; | 20 const uint32_t kMemoryTotalsPollingInterval = 25; |
| 20 uint32_t g_polling_interval_ms_for_testing = 0; | 21 uint32_t g_polling_interval_ms_for_testing = 0; |
| 21 } // namespace | 22 } // namespace |
| 22 | 23 |
| 23 MemoryDumpScheduler::MemoryDumpScheduler( | 24 MemoryDumpScheduler::MemoryDumpScheduler( |
| 24 MemoryDumpManager* mdm, | 25 MemoryDumpManager* mdm, |
| 25 scoped_refptr<SingleThreadTaskRunner> polling_task_runner) | 26 scoped_refptr<SingleThreadTaskRunner> polling_task_runner) |
| 26 : mdm_(mdm), polling_state_(polling_task_runner) {} | 27 : mdm_(mdm), polling_state_(polling_task_runner) {} |
| 27 | 28 |
| 28 MemoryDumpScheduler::~MemoryDumpScheduler() {} | 29 MemoryDumpScheduler::~MemoryDumpScheduler() {} |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 periodic_state_.timer.Start( | 79 periodic_state_.timer.Start( |
| 79 FROM_HERE, | 80 FROM_HERE, |
| 80 TimeDelta::FromMilliseconds(periodic_state_.min_timer_period_ms), | 81 TimeDelta::FromMilliseconds(periodic_state_.min_timer_period_ms), |
| 81 Bind(&MemoryDumpScheduler::RequestPeriodicGlobalDump, Unretained(this))); | 82 Bind(&MemoryDumpScheduler::RequestPeriodicGlobalDump, Unretained(this))); |
| 82 } | 83 } |
| 83 | 84 |
| 84 void MemoryDumpScheduler::NotifyPollingSupported() { | 85 void MemoryDumpScheduler::NotifyPollingSupported() { |
| 85 if (!polling_state_.is_configured || polling_state_.is_polling_enabled) | 86 if (!polling_state_.is_configured || polling_state_.is_polling_enabled) |
| 86 return; | 87 return; |
| 87 polling_state_.is_polling_enabled = true; | 88 polling_state_.is_polling_enabled = true; |
| 89 for (uint32_t i = 0; i < PollingTriggerState::kMaxNumMemorySamples; ++i) |
| 90 polling_state_.last_memory_totals_kb[i] = 0; |
| 91 polling_state_.last_memory_totals_kb_index = 0; |
| 88 polling_state_.num_polls_from_last_dump = 0; | 92 polling_state_.num_polls_from_last_dump = 0; |
| 89 polling_state_.last_dump_memory_total = 0; | 93 polling_state_.last_dump_memory_total = 0; |
| 94 |
| 95 if (!polling_state_.memory_increase_threshold) { |
| 96 polling_state_.memory_increase_threshold = kDefaultMemoryIncreaseThreshold; |
| 97 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \ |
| 98 defined(OS_ANDROID) |
| 99 // Set threshold to 1% of total system memory. |
| 100 SystemMemoryInfoKB meminfo; |
| 101 bool res = GetSystemMemoryInfo(&meminfo); |
| 102 if (res) |
| 103 polling_state_.memory_increase_threshold = (meminfo.total / 100) * 1024; |
| 104 #endif |
| 105 } |
| 106 |
| 90 polling_state_.polling_task_runner->PostTask( | 107 polling_state_.polling_task_runner->PostTask( |
| 91 FROM_HERE, | 108 FROM_HERE, |
| 92 Bind(&MemoryDumpScheduler::PollMemoryOnPollingThread, Unretained(this))); | 109 Bind(&MemoryDumpScheduler::PollMemoryOnPollingThread, Unretained(this))); |
| 93 } | 110 } |
| 94 | 111 |
| 95 void MemoryDumpScheduler::DisableAllTriggers() { | 112 void MemoryDumpScheduler::DisableAllTriggers() { |
| 96 if (periodic_state_.timer.IsRunning()) | 113 if (periodic_state_.timer.IsRunning()) |
| 97 periodic_state_.timer.Stop(); | 114 periodic_state_.timer.Stop(); |
| 98 DisablePolling(); | 115 DisablePolling(); |
| 99 } | 116 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 if (polling_state_.last_dump_memory_total == 0) { | 190 if (polling_state_.last_dump_memory_total == 0) { |
| 174 // If it's first sample then trigger memory dump. | 191 // If it's first sample then trigger memory dump. |
| 175 should_dump = true; | 192 should_dump = true; |
| 176 } else if (polling_state_.min_polls_between_dumps > | 193 } else if (polling_state_.min_polls_between_dumps > |
| 177 polling_state_.num_polls_from_last_dump) { | 194 polling_state_.num_polls_from_last_dump) { |
| 178 return false; | 195 return false; |
| 179 } | 196 } |
| 180 | 197 |
| 181 int64_t increase_from_last_dump = | 198 int64_t increase_from_last_dump = |
| 182 current_memory_total - polling_state_.last_dump_memory_total; | 199 current_memory_total - polling_state_.last_dump_memory_total; |
| 183 should_dump |= increase_from_last_dump > kMemoryIncreaseThreshold; | 200 should_dump |= |
| 201 increase_from_last_dump > polling_state_.memory_increase_threshold; |
| 202 should_dump |= IsCurrentSamplePeak(current_memory_total); |
| 184 if (should_dump) { | 203 if (should_dump) { |
| 185 polling_state_.last_dump_memory_total = current_memory_total; | 204 polling_state_.last_dump_memory_total = current_memory_total; |
| 186 polling_state_.num_polls_from_last_dump = 0; | 205 polling_state_.num_polls_from_last_dump = 0; |
| 206 for (uint32_t i = 0; i < PollingTriggerState::kMaxNumMemorySamples; ++i) |
| 207 polling_state_.last_memory_totals_kb[i] = 0; |
| 208 polling_state_.last_memory_totals_kb_index = 0; |
| 187 } | 209 } |
| 188 return should_dump; | 210 return should_dump; |
| 189 } | 211 } |
| 190 | 212 |
| 213 bool MemoryDumpScheduler::IsCurrentSamplePeak( |
| 214 uint64_t current_memory_total_bytes) { |
| 215 uint64_t current_memory_total_kb = current_memory_total_bytes / 1024; |
| 216 polling_state_.last_memory_totals_kb_index = |
| 217 (polling_state_.last_memory_totals_kb_index + 1) % |
| 218 PollingTriggerState::kMaxNumMemorySamples; |
| 219 uint64_t mean = 0; |
| 220 for (uint32_t i = 0; i < PollingTriggerState::kMaxNumMemorySamples; ++i) { |
| 221 if (polling_state_.last_memory_totals_kb[i] == 0) { |
| 222 // Not enough samples to detect peaks. |
| 223 polling_state_ |
| 224 .last_memory_totals_kb[polling_state_.last_memory_totals_kb_index] = |
| 225 current_memory_total_kb; |
| 226 return false; |
| 227 } |
| 228 mean += polling_state_.last_memory_totals_kb[i]; |
| 229 } |
| 230 mean = mean / PollingTriggerState::kMaxNumMemorySamples; |
| 231 uint64_t variance = 0; |
| 232 for (uint32_t i = 0; i < PollingTriggerState::kMaxNumMemorySamples; ++i) { |
| 233 variance += (polling_state_.last_memory_totals_kb[i] - mean) * |
| 234 (polling_state_.last_memory_totals_kb[i] - mean); |
| 235 } |
| 236 variance = variance / PollingTriggerState::kMaxNumMemorySamples; |
| 237 |
| 238 polling_state_ |
| 239 .last_memory_totals_kb[polling_state_.last_memory_totals_kb_index] = |
| 240 current_memory_total_kb; |
| 241 |
| 242 // If stddev is less than 0.2% then we consider that the process is inactive. |
| 243 bool is_stddev_low = variance < mean / 500 * mean / 500; |
| 244 if (is_stddev_low) |
| 245 return false; |
| 246 |
| 247 // (mean + 3.69 * stddev) corresponds to a value that is higher than current |
| 248 // sample with 99.99% probability. |
| 249 return (current_memory_total_kb - mean) * (current_memory_total_kb - mean) > |
| 250 (3.69 * 3.69 * variance); |
| 251 } |
| 252 |
| 191 MemoryDumpScheduler::PeriodicTriggerState::PeriodicTriggerState() | 253 MemoryDumpScheduler::PeriodicTriggerState::PeriodicTriggerState() |
| 192 : is_configured(false), | 254 : is_configured(false), |
| 193 dump_count(0), | 255 dump_count(0), |
| 194 min_timer_period_ms(std::numeric_limits<uint32_t>::max()), | 256 min_timer_period_ms(std::numeric_limits<uint32_t>::max()), |
| 195 light_dumps_rate(0), | 257 light_dumps_rate(0), |
| 196 heavy_dumps_rate(0), | 258 heavy_dumps_rate(0), |
| 197 light_dump_period_ms(0), | 259 light_dump_period_ms(0), |
| 198 heavy_dump_period_ms(0) {} | 260 heavy_dump_period_ms(0) {} |
| 199 | 261 |
| 200 MemoryDumpScheduler::PeriodicTriggerState::~PeriodicTriggerState() { | 262 MemoryDumpScheduler::PeriodicTriggerState::~PeriodicTriggerState() { |
| 201 DCHECK(!timer.IsRunning()); | 263 DCHECK(!timer.IsRunning()); |
| 202 } | 264 } |
| 203 | 265 |
| 204 MemoryDumpScheduler::PollingTriggerState::PollingTriggerState( | 266 MemoryDumpScheduler::PollingTriggerState::PollingTriggerState( |
| 205 scoped_refptr<SingleThreadTaskRunner> polling_task_runner) | 267 scoped_refptr<SingleThreadTaskRunner> polling_task_runner) |
| 206 : is_configured(false), | 268 : is_configured(false), |
| 207 is_polling_enabled(false), | 269 is_polling_enabled(false), |
| 208 level_of_detail(MemoryDumpLevelOfDetail::FIRST), | 270 level_of_detail(MemoryDumpLevelOfDetail::FIRST), |
| 209 polling_task_runner(polling_task_runner), | 271 polling_task_runner(polling_task_runner), |
| 210 polling_interval_ms(g_polling_interval_ms_for_testing | 272 polling_interval_ms(g_polling_interval_ms_for_testing |
| 211 ? g_polling_interval_ms_for_testing | 273 ? g_polling_interval_ms_for_testing |
| 212 : kMemoryTotalsPollingInterval), | 274 : kMemoryTotalsPollingInterval), |
| 213 min_polls_between_dumps(0), | 275 min_polls_between_dumps(0), |
| 214 num_polls_from_last_dump(0), | 276 num_polls_from_last_dump(0), |
| 215 last_dump_memory_total(0) {} | 277 last_dump_memory_total(0), |
| 278 memory_increase_threshold(0), |
| 279 last_memory_totals_kb_index(0) {} |
| 216 | 280 |
| 217 MemoryDumpScheduler::PollingTriggerState::~PollingTriggerState() { | 281 MemoryDumpScheduler::PollingTriggerState::~PollingTriggerState() { |
| 218 DCHECK(!polling_task_runner); | 282 DCHECK(!polling_task_runner); |
| 219 } | 283 } |
| 220 | 284 |
| 221 } // namespace trace_event | 285 } // namespace trace_event |
| 222 } // namespace base | 286 } // namespace base |
| OLD | NEW |