Chromium Code Reviews| 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 { | |
| 16 const uint32_t kInitialPollingIntervalMs = 1; | |
| 17 } // namespace | |
| 18 | |
| 19 namespace base { | |
| 20 namespace trace_event { | |
| 21 | |
| 22 // static | |
| 23 MemoryPeakDetector* MemoryPeakDetector::GetInstance() { | |
| 24 static MemoryPeakDetector* instance = new MemoryPeakDetector(); | |
| 25 return instance; | |
| 26 } | |
| 27 | |
| 28 MemoryPeakDetector::MemoryPeakDetector() | |
| 29 : state_(NOT_INITIALIZED), polling_interval_ms_(0), poll_tasks_count_(0) {} | |
| 30 | |
| 31 MemoryPeakDetector::~MemoryPeakDetector() { | |
| 32 // This should be hit only in tests. In that case the test should call | |
| 33 // TearDownForTesting() first. | |
| 34 DCHECK_EQ(NOT_INITIALIZED, state_); | |
| 35 } | |
| 36 | |
| 37 void MemoryPeakDetector::Initialize( | |
| 38 const GetDumpProvidersFunction& get_dump_providers_function, | |
| 39 const scoped_refptr<SequencedTaskRunner>& task_runner, | |
| 40 const OnPeakDetectedCallback& on_peak_detected_callback) { | |
| 41 DCHECK(!get_dump_providers_function.is_null()); | |
| 42 DCHECK(!on_peak_detected_callback.is_null()); | |
| 43 DCHECK(state_ == NOT_INITIALIZED || state_ == DISABLED); | |
| 44 DCHECK(dump_providers_.empty()); | |
|
ssid
2017/04/03 17:55:27
DCHECK(task_runner)?
Primiano Tucci (use gerrit)
2017/04/03 20:28:15
Done.
| |
| 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::TearDownForTesting() { | |
| 52 get_dump_providers_function_.Reset(); | |
| 53 task_runner_ = nullptr; | |
| 54 on_peak_detected_callback_.Reset(); | |
| 55 dump_providers_.clear(); | |
| 56 state_ = NOT_INITIALIZED; | |
| 57 poll_tasks_count_ = 0; | |
| 58 } | |
| 59 | |
| 60 void MemoryPeakDetector::Start() { | |
| 61 task_runner_->PostTask( | |
| 62 FROM_HERE, Bind(&MemoryPeakDetector::StartInternal, Unretained(this))); | |
| 63 } | |
| 64 | |
| 65 void MemoryPeakDetector::StartInternal() { | |
| 66 DCHECK_EQ(DISABLED, state_); | |
| 67 state_ = ENABLED; | |
| 68 polling_interval_ms_ = kInitialPollingIntervalMs; | |
| 69 | |
| 70 // If there are any dump providers available, NotifyMemoryDumpProvidersChanged | |
| 71 // will fetch them and start the polling. Otherwise this will remain in the | |
| 72 // ENABLED state and the actual polling will start on the next call to | |
| 73 // ReloadDumpProvidersAndStartPollingIfNeeded(). | |
| 74 // Depending on the sandbox model, it is possible that no polling-capable dump | |
| 75 // providers will be ever available. | |
| 76 ReloadDumpProvidersAndStartPollingIfNeeded(); | |
| 77 } | |
| 78 | |
| 79 void MemoryPeakDetector::Stop() { | |
| 80 task_runner_->PostTask( | |
| 81 FROM_HERE, Bind(&MemoryPeakDetector::StopInternal, Unretained(this))); | |
| 82 } | |
| 83 | |
| 84 void MemoryPeakDetector::StopInternal() { | |
| 85 DCHECK_NE(NOT_INITIALIZED, state_); | |
| 86 state_ = DISABLED; | |
| 87 dump_providers_.clear(); | |
| 88 } | |
| 89 | |
| 90 void MemoryPeakDetector::NotifyMemoryDumpProvidersChanged() { | |
| 91 // It is possible to call this before the first Initialize() call, in which | |
| 92 // case we want to just make this a noop. Start() will fetch the MDP list | |
| 93 // anyways later. | |
| 94 if (!task_runner_) | |
| 95 return; | |
| 96 task_runner_->PostTask( | |
| 97 FROM_HERE, | |
| 98 Bind(&MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded, | |
| 99 Unretained(this))); | |
| 100 } | |
| 101 | |
| 102 void MemoryPeakDetector::ReloadDumpProvidersAndStartPollingIfNeeded() { | |
|
hjd
2017/04/03 13:25:30
I think the following sequence or one like it can
ssid
2017/04/03 17:55:27
We will never have the case where we go from havin
Primiano Tucci (use gerrit)
2017/04/03 20:28:15
Even if we do, this code should be robust enough t
ssid
2017/04/04 05:08:35
hm no. What Hector said was right. If we have a ca
Primiano Tucci (use gerrit)
2017/04/04 09:14:31
Okay you were both right. There is a corner case I
| |
| 103 DCHECK_NE(NOT_INITIALIZED, state_); | |
| 104 if (state_ == DISABLED) | |
| 105 return; // Start() will re-fetch the MDP list later. | |
| 106 | |
| 107 DCHECK((state_ == RUNNING && !dump_providers_.empty()) || | |
| 108 (state_ == ENABLED && dump_providers_.empty())); | |
| 109 | |
| 110 dump_providers_.clear(); | |
| 111 | |
| 112 // This is really MemoryDumpManager::GetDumpProvidersForPolling, % testing. | |
| 113 get_dump_providers_function_.Run(&dump_providers_); | |
| 114 | |
| 115 if (state_ == ENABLED && !dump_providers_.empty()) { | |
| 116 // It's now time to start polling for realz. | |
| 117 state_ = RUNNING; | |
| 118 task_runner_->PostTask( | |
| 119 FROM_HERE, | |
| 120 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this))); | |
| 121 } else if (state_ == RUNNING && dump_providers_.empty()) { | |
| 122 // Will cause the next PollMemoryAndDetectPeak() task to early return. | |
| 123 state_ = ENABLED; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 void MemoryPeakDetector::PollMemoryAndDetectPeak() { | |
| 128 if (state_ != RUNNING) | |
| 129 return; | |
| 130 | |
| 131 // We should never end up in a situation where state_ == RUNNING but all dump | |
| 132 // providers are gone. | |
| 133 DCHECK(!dump_providers_.empty()); | |
| 134 | |
| 135 poll_tasks_count_++; | |
| 136 uint64_t memory_total = 0; | |
| 137 for (const scoped_refptr<MemoryDumpProviderInfo>& mdp_info : | |
| 138 dump_providers_) { | |
| 139 DCHECK(mdp_info->options.is_fast_polling_supported); | |
| 140 uint64_t value = 0; | |
| 141 mdp_info->dump_provider->PollFastMemoryTotal(&value); | |
| 142 memory_total += value; | |
| 143 } | |
| 144 ignore_result(memory_total); // TODO(primiano): temporary until next CL. | |
| 145 | |
| 146 // TODO(primiano): Move actual peak detection logic from the | |
| 147 // MemoryDumpScheduler in next CLs. | |
| 148 | |
| 149 SequencedTaskRunnerHandle::Get()->PostDelayedTask( | |
| 150 FROM_HERE, | |
| 151 Bind(&MemoryPeakDetector::PollMemoryAndDetectPeak, Unretained(this)), | |
| 152 TimeDelta::FromMilliseconds(polling_interval_ms_)); | |
| 153 } | |
| 154 | |
| 155 } // namespace trace_event | |
| 156 } // namespace base | |
| OLD | NEW |