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

Unified Diff: base/trace_event/memory_dump_scheduler.cc

Issue 2743993004: [memory-infra] Make MemoryDumpScheduler non-thread safe singleton (Closed)
Patch Set: rebase. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/trace_event/memory_dump_scheduler.h ('k') | base/trace_event/memory_dump_scheduler_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/trace_event/memory_dump_scheduler.cc
diff --git a/base/trace_event/memory_dump_scheduler.cc b/base/trace_event/memory_dump_scheduler.cc
index 68cc274a3abf9aad821a4bee3a2d5c78f6f0788b..66ea6c9f1aeb5dd98b8af1ac43d11a26b0d025be 100644
--- a/base/trace_event/memory_dump_scheduler.cc
+++ b/base/trace_event/memory_dump_scheduler.cc
@@ -21,108 +21,131 @@ const uint32_t kMemoryTotalsPollingInterval = 25;
uint32_t g_polling_interval_ms_for_testing = 0;
} // namespace
-MemoryDumpScheduler::MemoryDumpScheduler(
- MemoryDumpManager* mdm,
- scoped_refptr<SingleThreadTaskRunner> polling_task_runner)
- : mdm_(mdm), polling_state_(polling_task_runner) {}
+// static
+MemoryDumpScheduler* MemoryDumpScheduler::GetInstance() {
+ static MemoryDumpScheduler* instance = new MemoryDumpScheduler();
+ return instance;
+}
+MemoryDumpScheduler::MemoryDumpScheduler() : mdm_(nullptr), is_setup_(false) {}
MemoryDumpScheduler::~MemoryDumpScheduler() {}
+void MemoryDumpScheduler::Setup(
+ MemoryDumpManager* mdm,
+ scoped_refptr<SingleThreadTaskRunner> polling_task_runner) {
+ mdm_ = mdm;
+ polling_task_runner_ = polling_task_runner;
+ periodic_state_.reset(new PeriodicTriggerState);
+ polling_state_.reset(new PollingTriggerState);
+ is_setup_ = true;
+}
+
void MemoryDumpScheduler::AddTrigger(MemoryDumpType trigger_type,
MemoryDumpLevelOfDetail level_of_detail,
uint32_t min_time_between_dumps_ms) {
+ DCHECK(is_setup_);
if (trigger_type == MemoryDumpType::PEAK_MEMORY_USAGE) {
- DCHECK(!periodic_state_.is_configured);
- DCHECK_EQ(PollingTriggerState::DISABLED, polling_state_.current_state);
+ DCHECK(!periodic_state_->is_configured);
+ DCHECK_EQ(PollingTriggerState::DISABLED, polling_state_->current_state);
DCHECK_NE(0u, min_time_between_dumps_ms);
- polling_state_.level_of_detail = level_of_detail;
- polling_state_.min_polls_between_dumps =
- (min_time_between_dumps_ms + polling_state_.polling_interval_ms - 1) /
- polling_state_.polling_interval_ms;
- polling_state_.current_state = PollingTriggerState::CONFIGURED;
+ polling_state_->level_of_detail = level_of_detail;
+ polling_state_->min_polls_between_dumps =
+ (min_time_between_dumps_ms + polling_state_->polling_interval_ms - 1) /
+ polling_state_->polling_interval_ms;
+ polling_state_->current_state = PollingTriggerState::CONFIGURED;
} else if (trigger_type == MemoryDumpType::PERIODIC_INTERVAL) {
- DCHECK_EQ(PollingTriggerState::DISABLED, polling_state_.current_state);
- periodic_state_.is_configured = true;
+ DCHECK_EQ(PollingTriggerState::DISABLED, polling_state_->current_state);
+ periodic_state_->is_configured = true;
DCHECK_NE(0u, min_time_between_dumps_ms);
switch (level_of_detail) {
case MemoryDumpLevelOfDetail::BACKGROUND:
break;
case MemoryDumpLevelOfDetail::LIGHT:
- DCHECK_EQ(0u, periodic_state_.light_dump_period_ms);
- periodic_state_.light_dump_period_ms = min_time_between_dumps_ms;
+ DCHECK_EQ(0u, periodic_state_->light_dump_period_ms);
+ periodic_state_->light_dump_period_ms = min_time_between_dumps_ms;
break;
case MemoryDumpLevelOfDetail::DETAILED:
- DCHECK_EQ(0u, periodic_state_.heavy_dump_period_ms);
- periodic_state_.heavy_dump_period_ms = min_time_between_dumps_ms;
+ DCHECK_EQ(0u, periodic_state_->heavy_dump_period_ms);
+ periodic_state_->heavy_dump_period_ms = min_time_between_dumps_ms;
break;
}
- periodic_state_.min_timer_period_ms = std::min(
- periodic_state_.min_timer_period_ms, min_time_between_dumps_ms);
- DCHECK_EQ(0u, periodic_state_.light_dump_period_ms %
- periodic_state_.min_timer_period_ms);
- DCHECK_EQ(0u, periodic_state_.heavy_dump_period_ms %
- periodic_state_.min_timer_period_ms);
+ periodic_state_->min_timer_period_ms = std::min(
+ periodic_state_->min_timer_period_ms, min_time_between_dumps_ms);
+ DCHECK_EQ(0u, periodic_state_->light_dump_period_ms %
+ periodic_state_->min_timer_period_ms);
+ DCHECK_EQ(0u, periodic_state_->heavy_dump_period_ms %
+ periodic_state_->min_timer_period_ms);
}
}
-void MemoryDumpScheduler::NotifyPeriodicTriggerSupported() {
- if (!periodic_state_.is_configured || periodic_state_.timer.IsRunning())
+void MemoryDumpScheduler::EnablePeriodicTriggerIfNeeded() {
+ DCHECK(is_setup_);
+ if (!periodic_state_->is_configured || periodic_state_->timer.IsRunning())
return;
- periodic_state_.light_dumps_rate = periodic_state_.light_dump_period_ms /
- periodic_state_.min_timer_period_ms;
- periodic_state_.heavy_dumps_rate = periodic_state_.heavy_dump_period_ms /
- periodic_state_.min_timer_period_ms;
+ periodic_state_->light_dumps_rate = periodic_state_->light_dump_period_ms /
+ periodic_state_->min_timer_period_ms;
+ periodic_state_->heavy_dumps_rate = periodic_state_->heavy_dump_period_ms /
+ periodic_state_->min_timer_period_ms;
- periodic_state_.dump_count = 0;
- periodic_state_.timer.Start(
+ periodic_state_->dump_count = 0;
+ periodic_state_->timer.Start(
FROM_HERE,
- TimeDelta::FromMilliseconds(periodic_state_.min_timer_period_ms),
+ TimeDelta::FromMilliseconds(periodic_state_->min_timer_period_ms),
Bind(&MemoryDumpScheduler::RequestPeriodicGlobalDump, Unretained(this)));
}
-void MemoryDumpScheduler::NotifyPollingSupported() {
- if (polling_state_.current_state != PollingTriggerState::CONFIGURED)
+void MemoryDumpScheduler::EnablePollingIfNeeded() {
+ DCHECK(is_setup_);
+ if (polling_state_->current_state != PollingTriggerState::CONFIGURED)
return;
- polling_state_.current_state = PollingTriggerState::ENABLED;
- polling_state_.ResetTotals();
+ polling_state_->current_state = PollingTriggerState::ENABLED;
+ polling_state_->ResetTotals();
- polling_state_.polling_task_runner->PostTask(
+ polling_task_runner_->PostTask(
FROM_HERE,
Bind(&MemoryDumpScheduler::PollMemoryOnPollingThread, Unretained(this)));
}
void MemoryDumpScheduler::NotifyDumpTriggered() {
- if (polling_state_.polling_task_runner &&
- !polling_state_.polling_task_runner->RunsTasksOnCurrentThread()) {
- polling_state_.polling_task_runner->PostTask(
+ if (polling_task_runner_ &&
+ !polling_task_runner_->RunsTasksOnCurrentThread()) {
+ polling_task_runner_->PostTask(
FROM_HERE,
Bind(&MemoryDumpScheduler::NotifyDumpTriggered, Unretained(this)));
return;
}
- if (polling_state_.current_state != PollingTriggerState::ENABLED)
+
+ if (!polling_state_ ||
+ polling_state_->current_state != PollingTriggerState::ENABLED) {
return;
+ }
- polling_state_.ResetTotals();
+ polling_state_->ResetTotals();
}
void MemoryDumpScheduler::DisableAllTriggers() {
- if (periodic_state_.timer.IsRunning())
- periodic_state_.timer.Stop();
- DisablePolling();
-}
+ if (periodic_state_) {
+ if (periodic_state_->timer.IsRunning())
+ periodic_state_->timer.Stop();
+ periodic_state_.reset();
+ }
-void MemoryDumpScheduler::DisablePolling() {
- if (!polling_state_.polling_task_runner->RunsTasksOnCurrentThread()) {
- if (polling_state_.polling_task_runner->PostTask(
- FROM_HERE,
- Bind(&MemoryDumpScheduler::DisablePolling, Unretained(this))))
- return;
+ if (polling_task_runner_) {
+ DCHECK(polling_state_);
+ polling_task_runner_->PostTask(
+ FROM_HERE, Bind(&MemoryDumpScheduler::DisablePollingOnPollingThread,
+ Unretained(this)));
+ polling_task_runner_ = nullptr;
}
- polling_state_.current_state = PollingTriggerState::DISABLED;
- polling_state_.polling_task_runner = nullptr;
+ is_setup_ = false;
+}
+
+void MemoryDumpScheduler::DisablePollingOnPollingThread() {
+ polling_state_->current_state = PollingTriggerState::DISABLED;
+ polling_state_.reset();
}
// static
@@ -131,30 +154,30 @@ void MemoryDumpScheduler::SetPollingIntervalForTesting(uint32_t interval) {
}
bool MemoryDumpScheduler::IsPeriodicTimerRunningForTesting() {
- return periodic_state_.timer.IsRunning();
+ return periodic_state_->timer.IsRunning();
}
void MemoryDumpScheduler::RequestPeriodicGlobalDump() {
MemoryDumpLevelOfDetail level_of_detail = MemoryDumpLevelOfDetail::BACKGROUND;
- if (periodic_state_.light_dumps_rate > 0 &&
- periodic_state_.dump_count % periodic_state_.light_dumps_rate == 0)
+ if (periodic_state_->light_dumps_rate > 0 &&
+ periodic_state_->dump_count % periodic_state_->light_dumps_rate == 0)
level_of_detail = MemoryDumpLevelOfDetail::LIGHT;
- if (periodic_state_.heavy_dumps_rate > 0 &&
- periodic_state_.dump_count % periodic_state_.heavy_dumps_rate == 0)
+ if (periodic_state_->heavy_dumps_rate > 0 &&
+ periodic_state_->dump_count % periodic_state_->heavy_dumps_rate == 0)
level_of_detail = MemoryDumpLevelOfDetail::DETAILED;
- ++periodic_state_.dump_count;
+ ++periodic_state_->dump_count;
mdm_->RequestGlobalDump(MemoryDumpType::PERIODIC_INTERVAL, level_of_detail);
}
void MemoryDumpScheduler::PollMemoryOnPollingThread() {
- if (polling_state_.current_state != PollingTriggerState::ENABLED)
+ if (polling_state_->current_state != PollingTriggerState::ENABLED)
return;
uint64_t polled_memory = 0;
bool res = mdm_->PollFastMemoryTotal(&polled_memory);
DCHECK(res);
- if (polling_state_.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) {
+ if (polling_state_->level_of_detail == MemoryDumpLevelOfDetail::DETAILED) {
TRACE_COUNTER1(MemoryDumpManager::kTraceCategory, "PolledMemoryMB",
polled_memory / 1024 / 1024);
}
@@ -166,14 +189,14 @@ void MemoryDumpScheduler::PollMemoryOnPollingThread() {
polled_memory / 1024 / 1024);
mdm_->RequestGlobalDump(MemoryDumpType::PEAK_MEMORY_USAGE,
- polling_state_.level_of_detail);
+ polling_state_->level_of_detail);
}
// TODO(ssid): Use RequestSchedulerCallback, crbug.com/607533.
ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
Bind(&MemoryDumpScheduler::PollMemoryOnPollingThread, Unretained(this)),
- TimeDelta::FromMilliseconds(polling_state_.polling_interval_ms));
+ TimeDelta::FromMilliseconds(polling_state_->polling_interval_ms));
}
bool MemoryDumpScheduler::ShouldTriggerDump(uint64_t current_memory_total) {
@@ -184,52 +207,52 @@ bool MemoryDumpScheduler::ShouldTriggerDump(uint64_t current_memory_total) {
return false;
bool should_dump = false;
- ++polling_state_.num_polls_from_last_dump;
- if (polling_state_.last_dump_memory_total == 0) {
+ ++polling_state_->num_polls_from_last_dump;
+ if (polling_state_->last_dump_memory_total == 0) {
// If it's first sample then trigger memory dump.
should_dump = true;
- } else if (polling_state_.min_polls_between_dumps >
- polling_state_.num_polls_from_last_dump) {
+ } else if (polling_state_->min_polls_between_dumps >
+ polling_state_->num_polls_from_last_dump) {
return false;
}
int64_t increase_from_last_dump =
- current_memory_total - polling_state_.last_dump_memory_total;
+ current_memory_total - polling_state_->last_dump_memory_total;
should_dump |=
- increase_from_last_dump > polling_state_.memory_increase_threshold;
+ increase_from_last_dump > polling_state_->memory_increase_threshold;
should_dump |= IsCurrentSamplePeak(current_memory_total);
if (should_dump)
- polling_state_.ResetTotals();
+ polling_state_->ResetTotals();
return should_dump;
}
bool MemoryDumpScheduler::IsCurrentSamplePeak(
uint64_t current_memory_total_bytes) {
uint64_t current_memory_total_kb = current_memory_total_bytes / 1024;
- polling_state_.last_memory_totals_kb_index =
- (polling_state_.last_memory_totals_kb_index + 1) %
+ polling_state_->last_memory_totals_kb_index =
+ (polling_state_->last_memory_totals_kb_index + 1) %
PollingTriggerState::kMaxNumMemorySamples;
uint64_t mean = 0;
for (uint32_t i = 0; i < PollingTriggerState::kMaxNumMemorySamples; ++i) {
- if (polling_state_.last_memory_totals_kb[i] == 0) {
+ if (polling_state_->last_memory_totals_kb[i] == 0) {
// Not enough samples to detect peaks.
polling_state_
- .last_memory_totals_kb[polling_state_.last_memory_totals_kb_index] =
+ ->last_memory_totals_kb[polling_state_->last_memory_totals_kb_index] =
current_memory_total_kb;
return false;
}
- mean += polling_state_.last_memory_totals_kb[i];
+ mean += polling_state_->last_memory_totals_kb[i];
}
mean = mean / PollingTriggerState::kMaxNumMemorySamples;
uint64_t variance = 0;
for (uint32_t i = 0; i < PollingTriggerState::kMaxNumMemorySamples; ++i) {
- variance += (polling_state_.last_memory_totals_kb[i] - mean) *
- (polling_state_.last_memory_totals_kb[i] - mean);
+ variance += (polling_state_->last_memory_totals_kb[i] - mean) *
+ (polling_state_->last_memory_totals_kb[i] - mean);
}
variance = variance / PollingTriggerState::kMaxNumMemorySamples;
polling_state_
- .last_memory_totals_kb[polling_state_.last_memory_totals_kb_index] =
+ ->last_memory_totals_kb[polling_state_->last_memory_totals_kb_index] =
current_memory_total_kb;
// If stddev is less than 0.2% then we consider that the process is inactive.
@@ -256,11 +279,9 @@ MemoryDumpScheduler::PeriodicTriggerState::~PeriodicTriggerState() {
DCHECK(!timer.IsRunning());
}
-MemoryDumpScheduler::PollingTriggerState::PollingTriggerState(
- scoped_refptr<SingleThreadTaskRunner> polling_task_runner)
+MemoryDumpScheduler::PollingTriggerState::PollingTriggerState()
: current_state(DISABLED),
level_of_detail(MemoryDumpLevelOfDetail::FIRST),
- polling_task_runner(polling_task_runner),
polling_interval_ms(g_polling_interval_ms_for_testing
? g_polling_interval_ms_for_testing
: kMemoryTotalsPollingInterval),
@@ -270,9 +291,7 @@ MemoryDumpScheduler::PollingTriggerState::PollingTriggerState(
memory_increase_threshold(0),
last_memory_totals_kb_index(0) {}
-MemoryDumpScheduler::PollingTriggerState::~PollingTriggerState() {
- DCHECK(!polling_task_runner);
-}
+MemoryDumpScheduler::PollingTriggerState::~PollingTriggerState() {}
void MemoryDumpScheduler::PollingTriggerState::ResetTotals() {
if (!memory_increase_threshold) {
« no previous file with comments | « base/trace_event/memory_dump_scheduler.h ('k') | base/trace_event/memory_dump_scheduler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698