1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/trace_event/memory_dump_manager.h"
#include <algorithm>
#include "base/atomic_sequence_num.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/thread_task_runner_handle.h"
#include "base/trace_event/memory_dump_provider.h"
#include "base/trace_event/memory_dump_session_state.h"
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event_argument.h"
#include "build/build_config.h"
#if !defined(OS_NACL)
#include "base/trace_event/process_memory_totals_dump_provider.h"
#endif
#if defined(OS_LINUX) || defined(OS_ANDROID)
#include "base/trace_event/malloc_dump_provider.h"
#include "base/trace_event/process_memory_maps_dump_provider.h"
#endif
#if defined(OS_ANDROID)
#include "base/trace_event/java_heap_dump_provider_android.h"
#endif
#if defined(OS_WIN)
#include "base/trace_event/winheap_dump_provider_win.h"
#endif
namespace base {
namespace trace_event {
namespace {
// TODO(primiano): this should be smarter and should do something similar to
// trace event synthetic delays.
const char kTraceCategory[] = TRACE_DISABLED_BY_DEFAULT("memory-infra");
// Throttle mmaps at a rate of once every kHeavyMmapsDumpsRate standard dumps.
const int kHeavyMmapsDumpsRate = 8; // 250 ms * 8 = 2000 ms.
const int kDumpIntervalMs = 250;
const int kTraceEventNumArgs = 1;
const char* kTraceEventArgNames[] = {"dumps"};
const unsigned char kTraceEventArgTypes[] = {TRACE_VALUE_TYPE_CONVERTABLE};
StaticAtomicSequenceNumber g_next_guid;
uint32 g_periodic_dumps_count = 0;
MemoryDumpManager* g_instance_for_testing = nullptr;
MemoryDumpProvider* g_mmaps_dump_provider = nullptr;
void RequestPeriodicGlobalDump() {
MemoryDumpType dump_type = g_periodic_dumps_count == 0
? MemoryDumpType::PERIODIC_INTERVAL_WITH_MMAPS
: MemoryDumpType::PERIODIC_INTERVAL;
if (++g_periodic_dumps_count == kHeavyMmapsDumpsRate)
g_periodic_dumps_count = 0;
MemoryDumpManager::GetInstance()->RequestGlobalDump(dump_type);
}
} // namespace
// static
const char* const MemoryDumpManager::kTraceCategoryForTesting = kTraceCategory;
// static
const int MemoryDumpManager::kMaxConsecutiveFailuresCount = 3;
// static
const uint64 MemoryDumpManager::kInvalidTracingProcessId = 0;
// static
MemoryDumpManager* MemoryDumpManager::GetInstance() {
if (g_instance_for_testing)
return g_instance_for_testing;
return Singleton<MemoryDumpManager,
LeakySingletonTraits<MemoryDumpManager>>::get();
}
// static
void MemoryDumpManager::SetInstanceForTesting(MemoryDumpManager* instance) {
if (instance)
instance->skip_core_dumpers_auto_registration_for_testing_ = true;
g_instance_for_testing = instance;
}
MemoryDumpManager::MemoryDumpManager()
: did_unregister_dump_provider_(false),
delegate_(nullptr),
memory_tracing_enabled_(0),
tracing_process_id_(kInvalidTracingProcessId),
system_allocator_pool_name_(nullptr),
skip_core_dumpers_auto_registration_for_testing_(false) {
g_next_guid.GetNext(); // Make sure that first guid is not zero.
}
MemoryDumpManager::~MemoryDumpManager() {
base::trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this);
}
void MemoryDumpManager::Initialize() {
TRACE_EVENT0(kTraceCategory, "init"); // Add to trace-viewer category list.
trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this);
if (skip_core_dumpers_auto_registration_for_testing_)
return;
// Enable the core dump providers.
#if !defined(OS_NACL)
RegisterDumpProvider(ProcessMemoryTotalsDumpProvider::GetInstance());
#endif
#if defined(OS_LINUX) || defined(OS_ANDROID)
g_mmaps_dump_provider = ProcessMemoryMapsDumpProvider::GetInstance();
RegisterDumpProvider(g_mmaps_dump_provider);
RegisterDumpProvider(MallocDumpProvider::GetInstance());
system_allocator_pool_name_ = MallocDumpProvider::kAllocatedObjects;
#endif
#if defined(OS_ANDROID)
RegisterDumpProvider(JavaHeapDumpProvider::GetInstance());
#endif
#if defined(OS_WIN)
RegisterDumpProvider(WinHeapDumpProvider::GetInstance());
system_allocator_pool_name_ = WinHeapDumpProvider::kAllocatedObjects;
#endif
}
void MemoryDumpManager::SetDelegate(MemoryDumpManagerDelegate* delegate) {
AutoLock lock(lock_);
DCHECK_EQ(static_cast<MemoryDumpManagerDelegate*>(nullptr), delegate_);
delegate_ = delegate;
}
void MemoryDumpManager::RegisterDumpProvider(
MemoryDumpProvider* mdp,
const scoped_refptr<SingleThreadTaskRunner>& task_runner) {
MemoryDumpProviderInfo mdp_info(mdp, task_runner);
AutoLock lock(lock_);
dump_providers_.insert(mdp_info);
}
void MemoryDumpManager::RegisterDumpProvider(MemoryDumpProvider* mdp) {
RegisterDumpProvider(mdp, nullptr);
}
void MemoryDumpManager::UnregisterDumpProvider(MemoryDumpProvider* mdp) {
AutoLock lock(lock_);
auto mdp_iter = dump_providers_.begin();
for (; mdp_iter != dump_providers_.end(); ++mdp_iter) {
if (mdp_iter->dump_provider == mdp)
break;
}
if (mdp_iter == dump_providers_.end())
return;
// Unregistration of a MemoryDumpProvider while tracing is ongoing is safe
// only if the MDP has specified a thread affinity (via task_runner()) AND
// the unregistration happens on the same thread (so the MDP cannot unregister
// and OnMemoryDump() at the same time).
// Otherwise, it is not possible to guarantee that its unregistration is
// race-free. If you hit this DCHECK, your MDP has a bug.
DCHECK_IMPLIES(
subtle::NoBarrier_Load(&memory_tracing_enabled_),
mdp_iter->task_runner && mdp_iter->task_runner->BelongsToCurrentThread())
<< "The MemoryDumpProvider attempted to unregister itself in a racy way. "
<< "Please file a crbug.";
dump_providers_.erase(mdp_iter);
did_unregister_dump_provider_ = true;
}
void MemoryDumpManager::RequestGlobalDump(
MemoryDumpType dump_type,
const MemoryDumpCallback& callback) {
// Bail out immediately if tracing is not enabled at all.
if (!UNLIKELY(subtle::NoBarrier_Load(&memory_tracing_enabled_))) {
if (!callback.is_null())
callback.Run(0u /* guid */, false /* success */);
return;
}
const uint64 guid =
TraceLog::GetInstance()->MangleEventId(g_next_guid.GetNext());
// The delegate_ is supposed to be thread safe, immutable and long lived.
// No need to keep the lock after we ensure that a delegate has been set.
MemoryDumpManagerDelegate* delegate;
{
AutoLock lock(lock_);
delegate = delegate_;
}
if (delegate) {
// The delegate is in charge to coordinate the request among all the
// processes and call the CreateLocalDumpPoint on the local process.
MemoryDumpRequestArgs args = {guid, dump_type};
delegate->RequestGlobalMemoryDump(args, callback);
} else if (!callback.is_null()) {
callback.Run(guid, false /* success */);
}
}
void MemoryDumpManager::RequestGlobalDump(MemoryDumpType dump_type) {
RequestGlobalDump(dump_type, MemoryDumpCallback());
}
void MemoryDumpManager::CreateProcessDump(const MemoryDumpRequestArgs& args,
const MemoryDumpCallback& callback) {
scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state;
{
AutoLock lock(lock_);
did_unregister_dump_provider_ = false;
pmd_async_state.reset(new ProcessMemoryDumpAsyncState(
args, dump_providers_.begin(), session_state_, callback));
}
// Start the thread hop. |dump_providers_| are kept sorted by thread, so
// ContinueAsyncProcessDump will hop at most once per thread (w.r.t. thread
// affinity specified by the MemoryDumpProvider(s) in RegisterDumpProvider()).
ContinueAsyncProcessDump(pmd_async_state.Pass());
}
// At most one ContinueAsyncProcessDump() can be active at any time for a given
// PMD, regardless of status of the |lock_|. |lock_| is used here purely to
// ensure consistency w.r.t. (un)registrations of |dump_providers_|.
// The linearization of dump providers' OnMemoryDump invocations is achieved by
// means of subsequent PostTask(s).
//
// 1) Prologue:
// - Check if the dump provider is disabled, if so skip the dump.
// - Check if we are on the right thread. If not hop and continue there.
// 2) Invoke the dump provider's OnMemoryDump() (unless skipped).
// 3) Epilogue:
// - Unregister the dump provider if it failed too many times consecutively.
// - Advance the |next_dump_provider| iterator to the next dump provider.
// - If this was the last hop, create a trace event, add it to the trace
// and finalize (invoke callback).
void MemoryDumpManager::ContinueAsyncProcessDump(
scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state) {
// Initalizes the ThreadLocalEventBuffer to guarantee that the TRACE_EVENTs
// in the PostTask below don't end up registering their own dump providers
// (for discounting trace memory overhead) while holding the |lock_|.
TraceLog::GetInstance()->InitializeThreadLocalEventBufferIfSupported();
// DO NOT put any LOG() statement in the locked sections, as in some contexts
// (GPU process) LOG() ends up performing PostTask/IPCs.
MemoryDumpProvider* mdp;
bool skip_dump = false;
{
AutoLock lock(lock_);
// In the unlikely event that a dump provider was unregistered while
// dumping, abort the dump, as that would make |next_dump_provider| invalid.
// Registration, on the other hand, is safe as per std::set<> contract.
if (did_unregister_dump_provider_) {
return AbortDumpLocked(pmd_async_state->callback,
pmd_async_state->task_runner,
pmd_async_state->req_args.dump_guid);
}
auto* mdp_info = &*pmd_async_state->next_dump_provider;
mdp = mdp_info->dump_provider;
if (mdp_info->disabled) {
skip_dump = true;
} else if (mdp == g_mmaps_dump_provider &&
pmd_async_state->req_args.dump_type !=
MemoryDumpType::PERIODIC_INTERVAL_WITH_MMAPS &&
pmd_async_state->req_args.dump_type !=
MemoryDumpType::EXPLICITLY_TRIGGERED) {
// Mmaps dumping is very heavyweight and cannot be performed at the same
// rate of other dumps. TODO(primiano): this is a hack and should be
// cleaned up as part of crbug.com/499731.
skip_dump = true;
} else if (mdp_info->task_runner &&
!mdp_info->task_runner->BelongsToCurrentThread()) {
// It's time to hop onto another thread.
// Copy the callback + arguments just for the unlikley case in which
// PostTask fails. In such case the Bind helper will destroy the
// pmd_async_state and we must keep a copy of the fields to notify the
// abort.
MemoryDumpCallback callback = pmd_async_state->callback;
scoped_refptr<SingleThreadTaskRunner> callback_task_runner =
pmd_async_state->task_runner;
const uint64 dump_guid = pmd_async_state->req_args.dump_guid;
const bool did_post_task = mdp_info->task_runner->PostTask(
FROM_HERE, Bind(&MemoryDumpManager::ContinueAsyncProcessDump,
Unretained(this), Passed(pmd_async_state.Pass())));
if (did_post_task)
return;
// The thread is gone. At this point the best thing we can do is to
// disable the dump provider and abort this dump.
mdp_info->disabled = true;
return AbortDumpLocked(callback, callback_task_runner, dump_guid);
}
} // AutoLock(lock_)
// Invoke the dump provider without holding the |lock_|.
bool finalize = false;
bool dump_successful = false;
if (!skip_dump)
dump_successful = mdp->OnMemoryDump(&pmd_async_state->process_memory_dump);
{
AutoLock lock(lock_);
if (did_unregister_dump_provider_) {
return AbortDumpLocked(pmd_async_state->callback,
pmd_async_state->task_runner,
pmd_async_state->req_args.dump_guid);
}
auto* mdp_info = &*pmd_async_state->next_dump_provider;
if (dump_successful) {
mdp_info->consecutive_failures = 0;
} else if (!skip_dump) {
++mdp_info->consecutive_failures;
if (mdp_info->consecutive_failures >= kMaxConsecutiveFailuresCount) {
mdp_info->disabled = true;
}
}
++pmd_async_state->next_dump_provider;
finalize = pmd_async_state->next_dump_provider == dump_providers_.end();
}
if (!skip_dump && !dump_successful) {
LOG(ERROR) << "A memory dumper failed, possibly due to sandboxing "
"(crbug.com/461788). Disabling dumper for current process. "
"Try restarting chrome with the --no-sandbox switch.";
}
if (finalize)
return FinalizeDumpAndAddToTrace(pmd_async_state.Pass());
ContinueAsyncProcessDump(pmd_async_state.Pass());
}
// static
void MemoryDumpManager::FinalizeDumpAndAddToTrace(
scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state) {
if (!pmd_async_state->task_runner->BelongsToCurrentThread()) {
scoped_refptr<SingleThreadTaskRunner> task_runner =
pmd_async_state->task_runner;
task_runner->PostTask(FROM_HERE,
Bind(&MemoryDumpManager::FinalizeDumpAndAddToTrace,
Passed(pmd_async_state.Pass())));
return;
}
scoped_refptr<ConvertableToTraceFormat> event_value(new TracedValue());
pmd_async_state->process_memory_dump.AsValueInto(
static_cast<TracedValue*>(event_value.get()));
const char* const event_name =
MemoryDumpTypeToString(pmd_async_state->req_args.dump_type);
TRACE_EVENT_API_ADD_TRACE_EVENT(
TRACE_EVENT_PHASE_MEMORY_DUMP,
TraceLog::GetCategoryGroupEnabled(kTraceCategory), event_name,
pmd_async_state->req_args.dump_guid, kTraceEventNumArgs,
kTraceEventArgNames, kTraceEventArgTypes, nullptr /* arg_values */,
&event_value, TRACE_EVENT_FLAG_HAS_ID);
if (!pmd_async_state->callback.is_null()) {
pmd_async_state->callback.Run(pmd_async_state->req_args.dump_guid,
true /* success */);
pmd_async_state->callback.Reset();
}
}
// static
void MemoryDumpManager::AbortDumpLocked(
MemoryDumpCallback callback,
scoped_refptr<SingleThreadTaskRunner> task_runner,
uint64 dump_guid) {
if (callback.is_null())
return; // There is nothing to NACK.
// Post the callback even if we are already on the right thread to avoid
// invoking the callback while holding the lock_.
task_runner->PostTask(FROM_HERE,
Bind(callback, dump_guid, false /* success */));
}
void MemoryDumpManager::OnTraceLogEnabled() {
// TODO(primiano): at this point we query TraceLog::GetCurrentCategoryFilter
// to figure out (and cache) which dumpers should be enabled or not.
// For the moment piggy back everything on the generic "memory" category.
bool enabled;
TRACE_EVENT_CATEGORY_GROUP_ENABLED(kTraceCategory, &enabled);
// Initialize the TraceLog for the current thread. This is to avoid that the
// TraceLog memory dump provider is registered lazily in the PostTask() below
// while the |lock_| is taken;
TraceLog::GetInstance()->InitializeThreadLocalEventBufferIfSupported();
AutoLock lock(lock_);
// There is no point starting the tracing without a delegate.
if (!enabled || !delegate_) {
// Disable all the providers.
for (auto it = dump_providers_.begin(); it != dump_providers_.end(); ++it)
it->disabled = true;
return;
}
session_state_ = new MemoryDumpSessionState();
for (auto it = dump_providers_.begin(); it != dump_providers_.end(); ++it) {
it->disabled = false;
it->consecutive_failures = 0;
}
subtle::NoBarrier_Store(&memory_tracing_enabled_, 1);
// TODO(primiano): This is a temporary hack to disable periodic memory dumps
// when running memory benchmarks until they can be enabled/disabled in
// base::trace_event::TraceConfig. See https://goo.gl/5Hj3o0.
if (delegate_->IsCoordinatorProcess() &&
!CommandLine::ForCurrentProcess()->HasSwitch(
"enable-memory-benchmarking")) {
g_periodic_dumps_count = 0;
periodic_dump_timer_.Start(FROM_HERE,
TimeDelta::FromMilliseconds(kDumpIntervalMs),
base::Bind(&RequestPeriodicGlobalDump));
}
}
void MemoryDumpManager::OnTraceLogDisabled() {
AutoLock lock(lock_);
periodic_dump_timer_.Stop();
subtle::NoBarrier_Store(&memory_tracing_enabled_, 0);
session_state_ = nullptr;
}
uint64 MemoryDumpManager::GetTracingProcessId() const {
return delegate_->GetTracingProcessId();
}
MemoryDumpManager::MemoryDumpProviderInfo::MemoryDumpProviderInfo(
MemoryDumpProvider* dump_provider,
const scoped_refptr<SingleThreadTaskRunner>& task_runner)
: dump_provider(dump_provider),
task_runner(task_runner),
consecutive_failures(0),
disabled(false) {
}
MemoryDumpManager::MemoryDumpProviderInfo::~MemoryDumpProviderInfo() {
}
bool MemoryDumpManager::MemoryDumpProviderInfo::operator<(
const MemoryDumpProviderInfo& other) const {
if (task_runner == other.task_runner)
return dump_provider < other.dump_provider;
return task_runner < other.task_runner;
}
MemoryDumpManager::ProcessMemoryDumpAsyncState::ProcessMemoryDumpAsyncState(
MemoryDumpRequestArgs req_args,
MemoryDumpProviderInfoSet::iterator next_dump_provider,
const scoped_refptr<MemoryDumpSessionState>& session_state,
MemoryDumpCallback callback)
: process_memory_dump(session_state),
req_args(req_args),
next_dump_provider(next_dump_provider),
callback(callback),
task_runner(MessageLoop::current()->task_runner()) {
}
MemoryDumpManager::ProcessMemoryDumpAsyncState::~ProcessMemoryDumpAsyncState() {
}
} // namespace trace_event
} // namespace base
|