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

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

Issue 2582453002: [tracing] Implement polling in MemoryDumpManager (Closed)
Patch Set: Address comments. Created 3 years, 10 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
« no previous file with comments | « base/trace_event/memory_dump_scheduler.h ('k') | base/trace_event/memory_dump_session_state.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_dump_scheduler.h"
6
7 #include "base/single_thread_task_runner.h"
8 #include "base/threading/thread_task_runner_handle.h"
9 #include "base/trace_event/memory_dump_manager.h"
10 #include "build/build_config.h"
11
12 namespace base {
13 namespace trace_event {
14
15 namespace {
16 // Threshold on increase in memory from last dump beyond which a new dump must
Primiano Tucci (use gerrit) 2017/02/15 18:13:44 maybe you can add a goo.gl link to your doc or the
ssid 2017/02/17 16:53:02 I added it in the ShouldTriggerDump function.
17 // be triggered.
18 int64_t kMemoryIncreaseThreshold = 50 * 1024 * 1024; // 50MiB
19 const uint32_t kMemoryTotalsPollingInterval = 25;
20 uint32_t g_polling_interval_ms_for_testing = 0;
21 } // namespace
22
23 MemoryDumpScheduler::MemoryDumpScheduler(
24 MemoryDumpManager* mdm,
25 scoped_refptr<SingleThreadTaskRunner> polling_task_runner)
26 : mdm_(mdm), polling_state_(polling_task_runner) {}
27
28 MemoryDumpScheduler::~MemoryDumpScheduler() {}
29
30 void MemoryDumpScheduler::AddTrigger(MemoryDumpType trigger_type,
31 MemoryDumpLevelOfDetail level_of_detail,
32 uint32_t min_time_between_dumps_ms) {
33 if (trigger_type == MemoryDumpType::PEAK_MEMORY_USAGE) {
34 DCHECK(!periodic_state_.is_configured);
35 DCHECK(!polling_state_.is_configured);
36 DCHECK_NE(0u, min_time_between_dumps_ms);
37
38 polling_state_.level_of_detail = level_of_detail;
39 polling_state_.min_polls_between_dumps =
40 (min_time_between_dumps_ms + polling_state_.polling_interval_ms - 1) /
41 polling_state_.polling_interval_ms;
42 polling_state_.is_configured = true;
43 } else if (trigger_type == MemoryDumpType::PERIODIC_INTERVAL) {
44 DCHECK(!polling_state_.is_configured);
45 periodic_state_.is_configured = true;
46 DCHECK_NE(0u, min_time_between_dumps_ms);
47 switch (level_of_detail) {
48 case MemoryDumpLevelOfDetail::BACKGROUND:
49 break;
50 case MemoryDumpLevelOfDetail::LIGHT:
51 DCHECK_EQ(0u, periodic_state_.light_dump_period_ms);
52 periodic_state_.light_dump_period_ms = min_time_between_dumps_ms;
53 break;
54 case MemoryDumpLevelOfDetail::DETAILED:
55 DCHECK_EQ(0u, periodic_state_.heavy_dump_period_ms);
56 periodic_state_.heavy_dump_period_ms = min_time_between_dumps_ms;
57 break;
58 }
59
60 periodic_state_.min_timer_period_ms = std::min(
61 periodic_state_.min_timer_period_ms, min_time_between_dumps_ms);
62 DCHECK_EQ(0u, periodic_state_.light_dump_period_ms %
63 periodic_state_.min_timer_period_ms);
64 DCHECK_EQ(0u, periodic_state_.heavy_dump_period_ms %
65 periodic_state_.min_timer_period_ms);
66 }
67 }
68
69 void MemoryDumpScheduler::NotifyPeriodicTriggerSupported() {
70 if (!periodic_state_.is_configured || periodic_state_.timer.IsRunning())
71 return;
72 periodic_state_.light_dumps_rate = periodic_state_.light_dump_period_ms /
73 periodic_state_.min_timer_period_ms;
74 periodic_state_.heavy_dumps_rate = periodic_state_.heavy_dump_period_ms /
75 periodic_state_.min_timer_period_ms;
76
77 periodic_state_.dump_count = 0;
78 periodic_state_.timer.Start(
79 FROM_HERE,
80 TimeDelta::FromMilliseconds(periodic_state_.min_timer_period_ms),
81 Bind(&MemoryDumpScheduler::RequestPeriodicGlobalDump, Unretained(this)));
82 }
83
84 void MemoryDumpScheduler::NotifyPollingSupported() {
85 if (!polling_state_.is_configured || polling_state_.is_polling_enabled)
86 return;
87 polling_state_.is_polling_enabled = true;
88 polling_state_.num_polls_from_last_dump = 0;
89 polling_state_.last_dump_memory_total = 0;
90 polling_state_.polling_task_runner->PostTask(
91 FROM_HERE,
92 Bind(&MemoryDumpScheduler::PollMemoryOnPollingThread, Unretained(this)));
93 }
94
95 void MemoryDumpScheduler::DisableAllTriggers() {
96 if (periodic_state_.timer.IsRunning())
97 periodic_state_.timer.Stop();
98 DisablePolling();
99 }
100
101 void MemoryDumpScheduler::DisablePolling() {
102 if (ThreadTaskRunnerHandle::Get() != polling_state_.polling_task_runner) {
103 if (polling_state_.polling_task_runner->PostTask(
104 FROM_HERE,
105 Bind(&MemoryDumpScheduler::DisablePolling, Unretained(this))))
106 return;
107 }
108 polling_state_.is_polling_enabled = false;
109 polling_state_.is_configured = false;
110 polling_state_.polling_task_runner = nullptr;
111 }
112
113 // static
114 void MemoryDumpScheduler::SetPollingIntervalForTesting(uint32_t interval) {
115 g_polling_interval_ms_for_testing = interval;
116 }
117
118 bool MemoryDumpScheduler::IsPeriodicTimerRunningForTesting() {
119 return periodic_state_.timer.IsRunning();
120 }
121
122 void MemoryDumpScheduler::RequestPeriodicGlobalDump() {
123 MemoryDumpLevelOfDetail level_of_detail = MemoryDumpLevelOfDetail::BACKGROUND;
124 if (periodic_state_.light_dumps_rate > 0 &&
125 periodic_state_.dump_count % periodic_state_.light_dumps_rate == 0)
126 level_of_detail = MemoryDumpLevelOfDetail::LIGHT;
127 if (periodic_state_.heavy_dumps_rate > 0 &&
128 periodic_state_.dump_count % periodic_state_.heavy_dumps_rate == 0)
129 level_of_detail = MemoryDumpLevelOfDetail::DETAILED;
130 ++periodic_state_.dump_count;
131
132 mdm_->RequestGlobalDump(MemoryDumpType::PERIODIC_INTERVAL, level_of_detail);
133 }
134
135 void MemoryDumpScheduler::PollMemoryOnPollingThread() {
136 if (!polling_state_.is_configured)
137 return;
138
139 uint64_t polled_memory = 0;
140 bool res = mdm_->PollFastMemoryTotal(&polled_memory);
141 DCHECK(res);
142 if (polling_state_.level_of_detail == MemoryDumpLevelOfDetail::DETAILED) {
143 TRACE_COUNTER1(MemoryDumpManager::kTraceCategory, "PolledMemoryMB",
144 polled_memory / 1024 / 1024);
145 }
146
147 if (ShouldTriggerDump(polled_memory)) {
148 TRACE_EVENT_INSTANT1(MemoryDumpManager::kTraceCategory,
149 "Peak memory dump Triggered",
150 TRACE_EVENT_SCOPE_PROCESS, "total_usage_MB",
151 polled_memory / 1024 / 1024);
152
153 mdm_->RequestGlobalDump(MemoryDumpType::PEAK_MEMORY_USAGE,
154 polling_state_.level_of_detail);
155 }
156
157 // TODO(ssid): Use RequestSchedulerCallback, crbug.com/607533.
158 ThreadTaskRunnerHandle::Get()->PostDelayedTask(
159 FROM_HERE,
160 Bind(&MemoryDumpScheduler::PollMemoryOnPollingThread, Unretained(this)),
161 TimeDelta::FromMilliseconds(polling_state_.polling_interval_ms));
162 }
163
164 bool MemoryDumpScheduler::ShouldTriggerDump(uint64_t current_memory_total) {
165 if (current_memory_total == 0)
166 return false;
167
168 bool should_dump = false;
169 ++polling_state_.num_polls_from_last_dump;
170 if (polling_state_.last_dump_memory_total == 0) {
171 // If it's first sample then trigger memory dump.
172 should_dump = true;
173 } else if (polling_state_.min_polls_between_dumps >
174 polling_state_.num_polls_from_last_dump) {
175 return false;
176 }
177
178 int64_t increase_from_last_dump =
179 current_memory_total - polling_state_.last_dump_memory_total;
180 should_dump |= increase_from_last_dump > kMemoryIncreaseThreshold;
181 if (should_dump) {
182 polling_state_.last_dump_memory_total = current_memory_total;
183 polling_state_.num_polls_from_last_dump = 0;
184 }
185 return should_dump;
186 }
187
188 MemoryDumpScheduler::PeriodicTriggerState::PeriodicTriggerState()
189 : is_configured(false),
190 dump_count(0),
191 min_timer_period_ms(std::numeric_limits<uint32_t>::max()),
192 light_dumps_rate(0),
193 heavy_dumps_rate(0),
194 light_dump_period_ms(0),
195 heavy_dump_period_ms(0) {}
196
197 MemoryDumpScheduler::PeriodicTriggerState::~PeriodicTriggerState() {
198 DCHECK(!timer.IsRunning());
199 }
200
201 MemoryDumpScheduler::PollingTriggerState::PollingTriggerState(
202 scoped_refptr<SingleThreadTaskRunner> polling_task_runner)
203 : is_configured(false),
204 is_polling_enabled(false),
205 level_of_detail(MemoryDumpLevelOfDetail::FIRST),
206 polling_task_runner(polling_task_runner),
207 polling_interval_ms(g_polling_interval_ms_for_testing
208 ? g_polling_interval_ms_for_testing
209 : kMemoryTotalsPollingInterval),
210 min_polls_between_dumps(0),
211 num_polls_from_last_dump(0),
212 last_dump_memory_total(0) {}
213
214 MemoryDumpScheduler::PollingTriggerState::~PollingTriggerState() {
215 DCHECK(!polling_task_runner);
216 }
217
218 } // namespace trace_event
219 } // namespace base
OLDNEW
« no previous file with comments | « base/trace_event/memory_dump_scheduler.h ('k') | base/trace_event/memory_dump_session_state.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698