Skip to content

Commit

Permalink
memory-infra: Stop using RequestGlobalDump in MemoryDumpManagerTest
Browse files Browse the repository at this point in the history
This CL changes the memory-infra unittests to not depend on
the RequestGlobalDump() API. From a logical viewpoint,
the RequestGlobalDump() is not needed for an in-process-only
test like MemoryDumpManagerTest.
From a more practical viewpoint, this refactoring is required
in preparation of the next CLs that will remove
MemoryDumpManager::RequestGlobalDump() method and move all the
clients to directly use the service.

This CL doesn't cause any behavior change in memory-infra.

BUG=720352
TBR=thakis

Change-Id: I82ee1bf94dab55798be727f0ae70a1fede6634ca
Reviewed-on: https://chromium-review.googlesource.com/525534
Reviewed-by: Primiano Tucci <primiano@chromium.org>
Reviewed-by: siddhartha sivakumar <ssid@chromium.org>
Reviewed-by: Helen Li <xunjieli@chromium.org>
Reviewed-by: Hector Dearman <hjd@chromium.org>
Commit-Queue: siddhartha sivakumar <ssid@chromium.org>
Cr-Commit-Position: refs/heads/master@{#477851}
  • Loading branch information
primiano authored and Commit Bot committed Jun 8, 2017
1 parent 99d5d72 commit 16e1788
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 415 deletions.
1 change: 1 addition & 0 deletions base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ component("base") {
"trace_event/memory_allocator_dump_guid.h",
"trace_event/memory_dump_manager.cc",
"trace_event/memory_dump_manager.h",
"trace_event/memory_dump_manager_test_utils.h",
"trace_event/memory_dump_provider.h",
"trace_event/memory_dump_provider_info.cc",
"trace_event/memory_dump_provider_info.h",
Expand Down
26 changes: 16 additions & 10 deletions base/trace_event/memory_dump_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ void OnPeriodicSchedulerTick(MemoryDumpLevelOfDetail level_of_detail) {
const char* const MemoryDumpManager::kTraceCategory =
TRACE_DISABLED_BY_DEFAULT("memory-infra");

// static
const char* const MemoryDumpManager::kLogPrefix = "Memory-infra dump";

// static
const int MemoryDumpManager::kMaxConsecutiveFailuresCount = 3;

Expand All @@ -159,6 +156,7 @@ MemoryDumpManager* MemoryDumpManager::GetInstance() {
// static
std::unique_ptr<MemoryDumpManager>
MemoryDumpManager::CreateInstanceForTesting() {
DCHECK(!g_instance_for_testing);
std::unique_ptr<MemoryDumpManager> instance(new MemoryDumpManager());
g_instance_for_testing = instance.get();
return instance;
Expand Down Expand Up @@ -428,10 +426,8 @@ void MemoryDumpManager::RequestGlobalDump(
MemoryDumpType dump_type,
MemoryDumpLevelOfDetail level_of_detail,
const GlobalMemoryDumpCallback& callback) {
// If |request_dump_function_| is null MDM hasn't been initialized yet.
if (request_dump_function_.is_null()) {
VLOG(1) << kLogPrefix << " failed because"
<< " memory dump manager is not enabled.";
if (!is_initialized()) {
VLOG(1) << "RequestGlobalDump() FAIL: MemoryDumpManager is not initialized";
if (!callback.is_null())
callback.Run(0u /* guid */, false /* success */);
return;
Expand Down Expand Up @@ -468,7 +464,8 @@ void MemoryDumpManager::GetDumpProvidersForPolling(
void MemoryDumpManager::RequestGlobalDump(
MemoryDumpType dump_type,
MemoryDumpLevelOfDetail level_of_detail) {
RequestGlobalDump(dump_type, level_of_detail, GlobalMemoryDumpCallback());
auto noop_callback = [](uint64_t dump_guid, bool success) {};
RequestGlobalDump(dump_type, level_of_detail, Bind(noop_callback));
}

bool MemoryDumpManager::IsDumpProviderRegisteredForTesting(
Expand Down Expand Up @@ -499,6 +496,15 @@ MemoryDumpManager::GetOrCreateBgTaskRunnerLocked() {
void MemoryDumpManager::CreateProcessDump(
const MemoryDumpRequestArgs& args,
const ProcessMemoryDumpCallback& callback) {
if (!is_initialized()) {
VLOG(1) << "CreateProcessDump() FAIL: MemoryDumpManager is not initialized";
if (!callback.is_null()) {
callback.Run(args.dump_guid, false /* success */,
Optional<MemoryDumpCallbackResult>());
}
return;
}

char guid_str[20];
sprintf(guid_str, "0x%" PRIx64, args.dump_guid);
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(kTraceCategory, "ProcessMemoryDump",
Expand Down Expand Up @@ -732,7 +738,7 @@ void MemoryDumpManager::FinalizeDumpAndAddToTrace(

// The results struct to fill.
// TODO(hjd): Transitional until we send the full PMD. See crbug.com/704203
base::Optional<MemoryDumpCallbackResult> result;
Optional<MemoryDumpCallbackResult> result;

bool dump_successful = pmd_async_state->dump_successful;

Expand Down Expand Up @@ -862,7 +868,7 @@ void MemoryDumpManager::SetupForTracing(
}
}

// Only coordinator process triggers periodic global memory dumps.
// Only coordinator process triggers periodic memory dumps.
if (is_coordinator_ && !periodic_config.triggers.empty()) {
MemoryDumpScheduler::GetInstance()->Start(periodic_config,
GetOrCreateBgTaskRunnerLocked());
Expand Down
13 changes: 7 additions & 6 deletions base/trace_event/memory_dump_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class BASE_EXPORT MemoryDumpManager {
const GlobalMemoryDumpCallback& callback)>;

static const char* const kTraceCategory;
static const char* const kLogPrefix;

// This value is returned as the tracing id of the child processes by
// GetTracingProcessId() when tracing is not enabled.
Expand Down Expand Up @@ -113,13 +112,12 @@ class BASE_EXPORT MemoryDumpManager {
// to notify about the completion of the global dump (i.e. after all the
// processes have dumped) and its success (true iff all the dumps were
// successful).
void RequestGlobalDump(MemoryDumpType dump_type,
MemoryDumpLevelOfDetail level_of_detail,
const GlobalMemoryDumpCallback& callback);
void RequestGlobalDump(MemoryDumpType,
MemoryDumpLevelOfDetail,
const GlobalMemoryDumpCallback&);

// Same as above (still asynchronous), but without callback.
void RequestGlobalDump(MemoryDumpType dump_type,
MemoryDumpLevelOfDetail level_of_detail);
void RequestGlobalDump(MemoryDumpType, MemoryDumpLevelOfDetail);

// Prepare MemoryDumpManager for RequestGlobalMemoryDump calls for tracing
// related modes (non-SUMMARY_ONLY).
Expand Down Expand Up @@ -280,6 +278,9 @@ class BASE_EXPORT MemoryDumpManager {
void GetDumpProvidersForPolling(
std::vector<scoped_refptr<MemoryDumpProviderInfo>>*);

// Returns true if Initialize() has been called, false otherwise.
bool is_initialized() const { return !request_dump_function_.is_null(); }

// An ordererd set of registered MemoryDumpProviderInfo(s), sorted by task
// runner affinity (MDPs belonging to the same task runners are adjacent).
MemoryDumpProviderInfo::OrderedSet dump_providers_;
Expand Down
46 changes: 46 additions & 0 deletions base/trace_event/memory_dump_manager_test_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2017 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.

#ifndef BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_TEST_UTILS_H_
#define BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_TEST_UTILS_H_

#include "base/bind.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/memory_dump_request_args.h"

namespace base {
namespace trace_event {

// Short-circuit the RequestGlobalDump() calls to CreateProcessDump().
// Rationale: only the in-process logic is required for unittests.
void RequestGlobalDumpForInProcessTesting(
const MemoryDumpRequestArgs& args,
const GlobalMemoryDumpCallback& global_callback) {
// Turns a ProcessMemoryDumpCallback into a GlobalMemoryDumpCallback.
auto callback_adapter = [](const GlobalMemoryDumpCallback& global_callback,
uint64_t dump_guid, bool success,
const Optional<MemoryDumpCallbackResult>& result) {
if (!global_callback.is_null())
global_callback.Run(dump_guid, success);
};
MemoryDumpManager::GetInstance()->CreateProcessDump(
args, Bind(callback_adapter, global_callback));
};

// Short circuits the RequestGlobalDumpFunction() to CreateProcessDump(),
// effectively allowing to use both in unittests with the same behavior.
// Unittests are in-process only and don't require all the multi-process
// dump handshaking (which would require bits outside of base).
void InitializeMemoryDumpManagerForInProcessTesting(bool is_coordinator) {
MemoryDumpManager* instance = MemoryDumpManager::GetInstance();
instance->set_dumper_registrations_ignored_for_testing(true);
instance->Initialize(BindRepeating(&RequestGlobalDumpForInProcessTesting),
is_coordinator);
instance->set_dumper_registrations_ignored_for_testing(false);
}

} // namespace trace_event
} // namespace base

#endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_TEST_UTILS_H_
Loading

0 comments on commit 16e1788

Please sign in to comment.