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

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

Issue 2737153002: [memory-infra] Implement peak detection logic (Closed)
Patch Set: nit. Created 3 years, 9 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_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) {
28 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) || \
Primiano Tucci (use gerrit) 2017/03/10 10:19:33 if it's not a problem can you move this into Notif
ssid 2017/03/10 19:49:01 Agreed.
29 defined(OS_ANDROID)
30 // Set threshold to 1% of total system memory.
31 SystemMemoryInfoKB meminfo;
32 bool res = GetSystemMemoryInfo(&meminfo);
33 if (res)
34 polling_state_.memory_increase_threshold = (meminfo.total / 100) * 1024;
35 #endif
36 }
27 37
28 MemoryDumpScheduler::~MemoryDumpScheduler() {} 38 MemoryDumpScheduler::~MemoryDumpScheduler() {}
29 39
30 void MemoryDumpScheduler::AddTrigger(MemoryDumpType trigger_type, 40 void MemoryDumpScheduler::AddTrigger(MemoryDumpType trigger_type,
31 MemoryDumpLevelOfDetail level_of_detail, 41 MemoryDumpLevelOfDetail level_of_detail,
32 uint32_t min_time_between_dumps_ms) { 42 uint32_t min_time_between_dumps_ms) {
33 if (trigger_type == MemoryDumpType::PEAK_MEMORY_USAGE) { 43 if (trigger_type == MemoryDumpType::PEAK_MEMORY_USAGE) {
34 DCHECK(!periodic_state_.is_configured); 44 DCHECK(!periodic_state_.is_configured);
35 DCHECK(!polling_state_.is_configured); 45 DCHECK(!polling_state_.is_configured);
36 DCHECK_NE(0u, min_time_between_dumps_ms); 46 DCHECK_NE(0u, min_time_between_dumps_ms);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 periodic_state_.timer.Start( 88 periodic_state_.timer.Start(
79 FROM_HERE, 89 FROM_HERE,
80 TimeDelta::FromMilliseconds(periodic_state_.min_timer_period_ms), 90 TimeDelta::FromMilliseconds(periodic_state_.min_timer_period_ms),
81 Bind(&MemoryDumpScheduler::RequestPeriodicGlobalDump, Unretained(this))); 91 Bind(&MemoryDumpScheduler::RequestPeriodicGlobalDump, Unretained(this)));
82 } 92 }
83 93
84 void MemoryDumpScheduler::NotifyPollingSupported() { 94 void MemoryDumpScheduler::NotifyPollingSupported() {
85 if (!polling_state_.is_configured || polling_state_.is_polling_enabled) 95 if (!polling_state_.is_configured || polling_state_.is_polling_enabled)
86 return; 96 return;
87 polling_state_.is_polling_enabled = true; 97 polling_state_.is_polling_enabled = true;
98 for (uint32_t i = 0;
99 i < PollingTriggerState::kNumTotalsTrackedForPeakDetection; ++i) {
100 polling_state_.last_memory_totals_kb[i] = 0;
101 }
102 polling_state_.last_memory_totals_kb_index = 0;
88 polling_state_.num_polls_from_last_dump = 0; 103 polling_state_.num_polls_from_last_dump = 0;
89 polling_state_.last_dump_memory_total = 0; 104 polling_state_.last_dump_memory_total = 0;
90 polling_state_.polling_task_runner->PostTask( 105 polling_state_.polling_task_runner->PostTask(
91 FROM_HERE, 106 FROM_HERE,
92 Bind(&MemoryDumpScheduler::PollMemoryOnPollingThread, Unretained(this))); 107 Bind(&MemoryDumpScheduler::PollMemoryOnPollingThread, Unretained(this)));
93 } 108 }
94 109
95 void MemoryDumpScheduler::DisableAllTriggers() { 110 void MemoryDumpScheduler::DisableAllTriggers() {
96 if (periodic_state_.timer.IsRunning()) 111 if (periodic_state_.timer.IsRunning())
97 periodic_state_.timer.Stop(); 112 periodic_state_.timer.Stop();
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 if (polling_state_.last_dump_memory_total == 0) { 188 if (polling_state_.last_dump_memory_total == 0) {
174 // If it's first sample then trigger memory dump. 189 // If it's first sample then trigger memory dump.
175 should_dump = true; 190 should_dump = true;
176 } else if (polling_state_.min_polls_between_dumps > 191 } else if (polling_state_.min_polls_between_dumps >
177 polling_state_.num_polls_from_last_dump) { 192 polling_state_.num_polls_from_last_dump) {
178 return false; 193 return false;
179 } 194 }
180 195
181 int64_t increase_from_last_dump = 196 int64_t increase_from_last_dump =
182 current_memory_total - polling_state_.last_dump_memory_total; 197 current_memory_total - polling_state_.last_dump_memory_total;
183 should_dump |= increase_from_last_dump > kMemoryIncreaseThreshold; 198 should_dump |=
199 increase_from_last_dump > polling_state_.memory_increase_threshold;
200 should_dump |= IsCurrentSamplePeak(current_memory_total);
184 if (should_dump) { 201 if (should_dump) {
185 polling_state_.last_dump_memory_total = current_memory_total; 202 polling_state_.last_dump_memory_total = current_memory_total;
186 polling_state_.num_polls_from_last_dump = 0; 203 polling_state_.num_polls_from_last_dump = 0;
204 for (uint32_t i = 0;
205 i < PollingTriggerState::kNumTotalsTrackedForPeakDetection; ++i) {
Primiano Tucci (use gerrit) 2017/03/10 10:19:34 maybe if you call this kNumPeakSamples those if-s
ssid 2017/03/10 19:49:01 Done.
206 polling_state_.last_memory_totals_kb[i] = 0;
207 }
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::kNumTotalsTrackedForPeakDetection;
219 uint64_t mean = 0;
220 for (uint32_t i = 0;
221 i < PollingTriggerState::kNumTotalsTrackedForPeakDetection; ++i) {
222 if (polling_state_.last_memory_totals_kb[i] == 0) {
223 // Not enough samples to detect peaks.
224 polling_state_
225 .last_memory_totals_kb[polling_state_.last_memory_totals_kb_index] =
226 current_memory_total_kb;
Primiano Tucci (use gerrit) 2017/03/10 10:19:33 you are storing this anyways, in this if and below
ssid 2017/03/10 19:49:01 I am trying to compute the mean and stddev without
227 return false;
228 }
229 mean += polling_state_.last_memory_totals_kb[i];
230 }
231 mean = mean / PollingTriggerState::kNumTotalsTrackedForPeakDetection;
232 uint64_t variance = 0;
233 for (uint32_t i = 0;
234 i < PollingTriggerState::kNumTotalsTrackedForPeakDetection; ++i) {
235 variance += (polling_state_.last_memory_totals_kb[i] - mean) *
236 (polling_state_.last_memory_totals_kb[i] - mean);
237 }
238 variance = variance / PollingTriggerState::kNumTotalsTrackedForPeakDetection;
239
240 polling_state_
241 .last_memory_totals_kb[polling_state_.last_memory_totals_kb_index] =
242 current_memory_total_kb;
243
244 // If stddev is less than 0.2% then we consider that the process is inactive.
245 bool is_stddev_low = variance < mean / 500 * mean / 500;
246 if (is_stddev_low)
247 return false;
248
249 // (mean + 3.69 * stddev) corresponds to a value that is higher than current
250 // sample with 99.99% probability.
251 return (current_memory_total_kb - mean) * (current_memory_total_kb - mean) >
252 (3.69 * 3.69 * variance);
253 }
254
191 MemoryDumpScheduler::PeriodicTriggerState::PeriodicTriggerState() 255 MemoryDumpScheduler::PeriodicTriggerState::PeriodicTriggerState()
192 : is_configured(false), 256 : is_configured(false),
193 dump_count(0), 257 dump_count(0),
194 min_timer_period_ms(std::numeric_limits<uint32_t>::max()), 258 min_timer_period_ms(std::numeric_limits<uint32_t>::max()),
195 light_dumps_rate(0), 259 light_dumps_rate(0),
196 heavy_dumps_rate(0), 260 heavy_dumps_rate(0),
197 light_dump_period_ms(0), 261 light_dump_period_ms(0),
198 heavy_dump_period_ms(0) {} 262 heavy_dump_period_ms(0) {}
199 263
200 MemoryDumpScheduler::PeriodicTriggerState::~PeriodicTriggerState() { 264 MemoryDumpScheduler::PeriodicTriggerState::~PeriodicTriggerState() {
201 DCHECK(!timer.IsRunning()); 265 DCHECK(!timer.IsRunning());
202 } 266 }
203 267
204 MemoryDumpScheduler::PollingTriggerState::PollingTriggerState( 268 MemoryDumpScheduler::PollingTriggerState::PollingTriggerState(
205 scoped_refptr<SingleThreadTaskRunner> polling_task_runner) 269 scoped_refptr<SingleThreadTaskRunner> polling_task_runner)
206 : is_configured(false), 270 : is_configured(false),
207 is_polling_enabled(false), 271 is_polling_enabled(false),
208 level_of_detail(MemoryDumpLevelOfDetail::FIRST), 272 level_of_detail(MemoryDumpLevelOfDetail::FIRST),
209 polling_task_runner(polling_task_runner), 273 polling_task_runner(polling_task_runner),
210 polling_interval_ms(g_polling_interval_ms_for_testing 274 polling_interval_ms(g_polling_interval_ms_for_testing
211 ? g_polling_interval_ms_for_testing 275 ? g_polling_interval_ms_for_testing
212 : kMemoryTotalsPollingInterval), 276 : kMemoryTotalsPollingInterval),
213 min_polls_between_dumps(0), 277 min_polls_between_dumps(0),
214 num_polls_from_last_dump(0), 278 num_polls_from_last_dump(0),
215 last_dump_memory_total(0) {} 279 last_dump_memory_total(0),
280 memory_increase_threshold(kDefaultMemoryIncreaseThreshold),
281 last_memory_totals_kb_index(0) {}
216 282
217 MemoryDumpScheduler::PollingTriggerState::~PollingTriggerState() { 283 MemoryDumpScheduler::PollingTriggerState::~PollingTriggerState() {
218 DCHECK(!polling_task_runner); 284 DCHECK(!polling_task_runner);
219 } 285 }
220 286
221 } // namespace trace_event 287 } // namespace trace_event
222 } // namespace base 288 } // namespace base
OLDNEW
« base/trace_event/memory_dump_manager_unittest.cc ('K') | « base/trace_event/memory_dump_scheduler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698