OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/trace_event/memory_peak_detector.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/threading/sequenced_task_runner_handle.h" |
| 12 #include "base/time/time.h" |
| 13 #include "base/trace_event/memory_dump_provider_info.h" |
| 14 |
| 15 namespace base { |
| 16 namespace trace_event { |
| 17 |
| 18 // static |
| 19 MemoryPeakDetector* MemoryPeakDetector::GetInstance() { |
| 20 static MemoryPeakDetector* instance = new MemoryPeakDetector(); |
| 21 return instance; |
| 22 } |
| 23 |
| 24 MemoryPeakDetector::MemoryPeakDetector() |
| 25 : generation_(0), |
| 26 state_(NOT_INITIALIZED), |
| 27 polling_interval_ms_(0), |
| 28 poll_tasks_count_for_testing_(0) {} |
| 29 |
| 30 MemoryPeakDetector::~MemoryPeakDetector() { |
| 31 // 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 DCHECK_EQ(NOT_INITIALIZED, state_); |
| 34 } |
| 35 |
| 36 void MemoryPeakDetector::Setup( |
| 37 const GetDumpProvidersFunction& get_dump_providers_function, |
| 38 const scoped_refptr<SequencedTaskRunner>& task_runner, |
| 39 const OnPeakDetectedCallback& on_peak_detected_callback) { |
| 40 DCHECK(!get_dump_providers_function.is_null()); |
| 41 DCHECK(task_runner); |
| 42 DCHECK(!on_peak_detected_callback.is_null()); |
| 43 DCHECK(state_ == NOT_INITIALIZED || state_ == DISABLED); |
| 44 DCHECK(dump_providers_.empty()); |
| 45 get_dump_providers_function_ = get_dump_providers_function; |
| 46 task_runner_ = task_runner; |
| 47 on_peak_detected_callback_ = on_peak_detected_callback; |
| 48 state_ = DISABLED; |
| 49 } |
| 50 |
| 51 void MemoryPeakDetector::TearDown() { |
| 52 if (task_runner_) { |
| 53 task_runner_->PostTask( |
| 54 FROM_HERE, |
| 55 Bind(&MemoryPeakDetector::TearDownInternal, Unretained(this))); |
| 56 } |
| 57 task_runner_ = nullptr; |
| 58 } |
| 59 |
| 60 void MemoryPeakDetector::Start() { |
| 61 task_runner_->PostTask( |
| 62 FROM_HERE, Bind(&MemoryPeakDetector::StartInternal, Unretained(this))); |
| 63 } |
| 64 |
| 65 void MemoryPeakDetector::Stop() { |
| 66 task_runner_->PostTask( |
| 67 FROM_HERE, Bind(&MemoryPeakDetector::StopInternal, Unretained(this))); |
| 68 } |
| 69 |
| 70 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_) |
| 74 return; |
| 75 task_runner_->PostTask( |
| 76 FROM_HERE, |
| 77 Bind(&MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded, |
| 78 Unretained(this))); |
| 79 } |
| 80 |
| 81 void MemoryPeakDetector::StartInternal() { |
| 82 DCHECK_EQ(DISABLED, state_); |
| 83 state_ = ENABLED; |
| 84 polling_interval_ms_ = 1; // TODO(primiano): temporary until next CL. |
| 85 |
| 86 // If there are any dump providers available, NotifyMemoryDumpProvidersChanged |
| 87 // will fetch them and start the polling. Otherwise this will remain in the |
| 88 // ENABLED state and the actual polling will start on the next call to |
| 89 // ReloadDumpProvidersAndStartPollingIfNeeded(). |
| 90 // Depending on the sandbox model, it is possible that no polling-capable dump |
| 91 // providers will be ever available. |
| 92 ReloadDumpProvidersAndStartPollingIfNeeded(); |
| 93 } |
| 94 |
| 95 void MemoryPeakDetector::StopInternal() { |
| 96 DCHECK_NE(NOT_INITIALIZED, state_); |
| 97 state_ = DISABLED; |
| 98 ++generation_; |
| 99 dump_providers_.clear(); |
| 100 } |
| 101 |
| 102 void MemoryPeakDetector::TearDownInternal() { |
| 103 StopInternal(); |
| 104 get_dump_providers_function_.Reset(); |
| 105 on_peak_detected_callback_.Reset(); |
| 106 state_ = NOT_INITIALIZED; |
| 107 } |
| 108 |
| 109 void MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded() { |
| 110 if (state_ == DISABLED || state_ == NOT_INITIALIZED) |
| 111 return; // Start() will re-fetch the MDP list later. |
| 112 |
| 113 DCHECK((state_ == RUNNING && !dump_providers_.empty()) || |
| 114 (state_ == ENABLED && dump_providers_.empty())); |
| 115 |
| 116 dump_providers_.clear(); |
| 117 |
| 118 // This is really MemoryDumpManager::GetDumpProvidersForPolling, % testing. |
| 119 get_dump_providers_function_.Run(&dump_providers_); |
| 120 |
| 121 if (state_ == ENABLED && !dump_providers_.empty()) { |
| 122 // It's now time to start polling for realz. |
| 123 state_ = RUNNING; |
| 124 task_runner_->PostTask(FROM_HERE, |
| 125 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, |
| 126 Unretained(this), ++generation_)); |
| 127 } else if (state_ == RUNNING && dump_providers_.empty()) { |
| 128 // Will cause the next PollMemoryAndDetectPeak() task to early return. |
| 129 state_ = ENABLED; |
| 130 ++generation_; |
| 131 } |
| 132 } |
| 133 |
| 134 void MemoryPeakDetector::PollMemoryAndDetectPeak(uint32_t expected_generation) { |
| 135 if (state_ != RUNNING || expected_generation != generation_) |
| 136 return; |
| 137 |
| 138 // We should never end up in a situation where state_ == RUNNING but all dump |
| 139 // providers are gone. |
| 140 DCHECK(!dump_providers_.empty()); |
| 141 |
| 142 poll_tasks_count_for_testing_++; |
| 143 uint64_t memory_total = 0; |
| 144 for (const scoped_refptr<MemoryDumpProviderInfo>& mdp_info : |
| 145 dump_providers_) { |
| 146 DCHECK(mdp_info->options.is_fast_polling_supported); |
| 147 uint64_t value = 0; |
| 148 mdp_info->dump_provider->PollFastMemoryTotal(&value); |
| 149 memory_total += value; |
| 150 } |
| 151 ignore_result(memory_total); // TODO(primiano): temporary until next CL. |
| 152 |
| 153 // TODO(primiano): Move actual peak detection logic from the |
| 154 // MemoryDumpScheduler in next CLs. |
| 155 |
| 156 SequencedTaskRunnerHandle::Get()->PostDelayedTask( |
| 157 FROM_HERE, |
| 158 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this), |
| 159 expected_generation), |
| 160 TimeDelta::FromMilliseconds(polling_interval_ms_)); |
| 161 } |
| 162 |
| 163 } // namespace trace_event |
| 164 } // namespace base |
OLD | NEW |