Commit b80b085b authored by Nico Weber's avatar Nico Weber

Replace global NO_TCMALLOC and USE_TCMALLOC defines with BUILDFLAG(USE_TCMALLOC).

We had a global define NO_TCMALLOC, and then build/build_config.h
set a define USE_TCMALLOC if that wasn't set.

Instead, use the buildflag system.

Also remove most allocator gn args from build/config/allocator.gni
to base/allocator/allocator.gni (the rest to move in a follow-up).

Also use a single, consistent mechanism for disabling allocator args
for nacl.

No behavior change.

Bug: 961767
Change-Id: Id2776cee1c77c00f401b73c414bb5e8d924ce4d1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1731613
Auto-Submit: Nico Weber <thakis@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Commit-Queue: Nico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683347}
parent 2d438beb
......@@ -17,6 +17,7 @@
# a bit easier to see which files apply in which cases rather than having a
# huge sequence of random-looking conditionals.
import("//base/allocator/allocator.gni")
import("//build/buildflag_header.gni")
import("//build/config/allocator.gni")
import("//build/config/arm.gni")
......
......@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//base/allocator/allocator.gni")
import("//build/buildflag_header.gni")
import("//build/config/allocator.gni")
import("//build/config/compiler/compiler.gni")
......@@ -294,6 +295,11 @@ buildflag_header("buildflags") {
"USE_ALLOCATOR_SHIM=$use_allocator_shim",
"USE_NEW_TCMALLOC=$use_new_tcmalloc",
]
if (use_allocator == "tcmalloc") {
flags += [ "USE_TCMALLOC=1" ]
} else {
flags += [ "USE_TCMALLOC=0" ]
}
}
# Used to shim malloc symbols on Android. see //base/allocator/README.md.
......
# Copyright 2019 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.
import("//build/config/sanitizers/sanitizers.gni")
# Temporarily disable tcmalloc on arm64 linux to get rid of compilation errors.
if (is_android || is_mac || is_ios || is_asan || is_lsan || is_tsan ||
is_msan || is_win || is_fuchsia || (is_linux && target_cpu == "arm64")) {
_default_allocator = "none"
} else {
_default_allocator = "tcmalloc"
}
# The debug CRT on Windows has some debug features that are incompatible with
# the shim. NaCl in particular does seem to link some binaries statically
# against the debug CRT with "is_nacl=false".
if ((is_linux || is_android || is_mac ||
(is_win && !is_component_build && !is_debug)) && !is_asan && !is_hwasan &&
!is_lsan && !is_tsan && !is_msan) {
_default_use_allocator_shim = true
} else {
_default_use_allocator_shim = false
}
declare_args() {
# Memory allocator to use. Set to "none" to use default allocator.
use_allocator = _default_allocator
# Causes all the allocations to be routed via allocator_shim.cc.
use_allocator_shim = _default_use_allocator_shim
}
assert(use_allocator == "none" || use_allocator == "tcmalloc")
assert(!is_win || use_allocator == "none", "Tcmalloc doesn't work on Windows.")
assert(!is_mac || use_allocator == "none", "Tcmalloc doesn't work on macOS.")
assert(!use_allocator_shim || is_linux || is_android || is_win || is_mac,
"use_allocator_shim works only on Android, Linux, macOS, and Windows.")
if (is_win && use_allocator_shim) {
assert(!is_component_build,
"The allocator shim doesn't work for the component build on Windows.")
}
......@@ -27,7 +27,7 @@ bool IsAllocatorInitialized() {
// Set by allocator_shim_override_ucrt_symbols_win.h when the
// shimmed _set_new_mode() is called.
return g_is_win_shim_layer_initialized;
#elif defined(OS_LINUX) && defined(USE_TCMALLOC) && \
#elif defined(OS_LINUX) && BUILDFLAG(USE_TCMALLOC) && \
!defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
// From third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h.
// TODO(primiano): replace with an include once base can depend on allocator.
......
......@@ -6,7 +6,7 @@
#include "base/allocator/buildflags.h"
#include "base/logging.h"
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
#if BUILDFLAG(USE_NEW_TCMALLOC)
#include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
#include "third_party/tcmalloc/chromium/src/gperftools/malloc_extension.h"
......@@ -22,33 +22,33 @@ namespace base {
namespace allocator {
void ReleaseFreeMemory() {
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
::MallocExtension::instance()->ReleaseFreeMemory();
#endif
}
bool GetNumericProperty(const char* name, size_t* value) {
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
return ::MallocExtension::instance()->GetNumericProperty(name, value);
#endif
return false;
}
bool SetNumericProperty(const char* name, size_t value) {
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
return ::MallocExtension::instance()->SetNumericProperty(name, value);
#endif
return false;
}
void GetHeapSample(std::string* writer) {
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
::MallocExtension::instance()->GetHeapSample(writer);
#endif
}
bool IsHeapProfilerRunning() {
#if defined(USE_TCMALLOC) && defined(ENABLE_PROFILING)
#if BUILDFLAG(USE_TCMALLOC) && defined(ENABLE_PROFILING)
return ::IsHeapProfilerRunning();
#endif
return false;
......@@ -56,7 +56,7 @@ bool IsHeapProfilerRunning() {
void SetHooks(AllocHookFunc alloc_hook, FreeHookFunc free_hook) {
// TODO(sque): Use allocator shim layer instead.
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
// Make sure no hooks get overwritten.
auto prev_alloc_hook = MallocHook::SetNewHook(alloc_hook);
if (alloc_hook)
......@@ -69,7 +69,7 @@ void SetHooks(AllocHookFunc alloc_hook, FreeHookFunc free_hook) {
}
int GetCallStack(void** stack, int max_stack_size) {
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
return MallocHook::GetCallerStackTrace(stack, max_stack_size, 0);
#endif
return 0;
......
......@@ -9,6 +9,7 @@
#include <atomic>
#include <new>
#include "base/allocator/buildflags.h"
#include "base/atomicops.h"
#include "base/bits.h"
#include "base/logging.h"
......@@ -351,7 +352,7 @@ ALWAYS_INLINE void ShimAlignedFree(void* address, void* context) {
// In the case of tcmalloc we also want to plumb into the glibc hooks
// to avoid that allocations made in glibc itself (e.g., strdup()) get
// accidentally performed on the glibc heap instead of the tcmalloc one.
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
#include "base/allocator/allocator_shim_override_glibc_weak_symbols.h"
#endif
......
......@@ -5,6 +5,7 @@
#include <stddef.h>
#include <stdio.h>
#include "base/allocator/buildflags.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/process/process_metrics.h"
......@@ -12,7 +13,7 @@
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
namespace {
using std::min;
......
......@@ -19,7 +19,7 @@
#endif // defined(OS_WIN)
// TODO(peria): Enable profiling on Windows.
#if BUILDFLAG(ENABLE_PROFILING) && !defined(NO_TCMALLOC) && !defined(OS_WIN)
#if BUILDFLAG(ENABLE_PROFILING) && BUILDFLAG(USE_TCMALLOC) && !defined(OS_WIN)
#if BUILDFLAG(USE_NEW_TCMALLOC)
#include "third_party/tcmalloc/chromium/src/gperftools/profiler.h"
......@@ -33,7 +33,7 @@ namespace base {
namespace debug {
// TODO(peria): Enable profiling on Windows.
#if BUILDFLAG(ENABLE_PROFILING) && !defined(NO_TCMALLOC) && !defined(OS_WIN)
#if BUILDFLAG(ENABLE_PROFILING) && BUILDFLAG(USE_TCMALLOC) && !defined(OS_WIN)
static int profile_count = 0;
......
......@@ -18,7 +18,7 @@
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
#if BUILDFLAG(USE_NEW_TCMALLOC)
#include "third_party/tcmalloc/chromium/src/config.h"
#include "third_party/tcmalloc/chromium/src/gperftools/tcmalloc.h"
......
......@@ -10,6 +10,7 @@
#include <sys/time.h>
#include <unistd.h>
#include "base/allocator/buildflags.h"
#include "base/logging.h"
#include "build/build_config.h"
......@@ -111,7 +112,7 @@ size_t ProcessMetrics::GetMallocUsage() {
return stats.size_in_use;
#elif defined(OS_LINUX) || defined(OS_ANDROID)
struct mallinfo minfo = mallinfo();
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
return minfo.uordblks;
#else
return minfo.hblkhd + minfo.arena;
......
......@@ -47,9 +47,9 @@ NOINLINE Type HideValueFromCompiler(volatile Type value) {
}
// TCmalloc, currently supported only by Linux/CrOS, supports malloc limits.
// - NO_TCMALLOC (should be defined if compiled with use_allocator!="tcmalloc")
// - USE_TCMALLOC (should be set if compiled with use_allocator=="tcmalloc")
// - ADDRESS_SANITIZER it has its own memory allocator
#if defined(OS_LINUX) && !defined(NO_TCMALLOC) && !defined(ADDRESS_SANITIZER)
#if defined(OS_LINUX) && BUILDFLAG(USE_TCMALLOC) && !defined(ADDRESS_SANITIZER)
#define MALLOC_OVERFLOW_TEST(function) function
#else
#define MALLOC_OVERFLOW_TEST(function) DISABLED_##function
......
......@@ -87,7 +87,7 @@ bool MallocDumpProvider::OnMemoryDump(const MemoryDumpArgs& args,
size_t resident_size = 0;
size_t allocated_objects_size = 0;
size_t allocated_objects_count = 0;
#if defined(USE_TCMALLOC)
#if BUILDFLAG(USE_TCMALLOC)
bool res =
allocator::GetNumericProperty("generic.heap_size", &total_virtual_size);
DCHECK(res);
......
......@@ -86,12 +86,6 @@
#define OS_POSIX 1
#endif
// Use tcmalloc
#if (defined(OS_WIN) || defined(OS_LINUX) || defined(OS_ANDROID)) && \
!defined(NO_TCMALLOC)
#define USE_TCMALLOC 1
#endif
// Compiler detection.
#if defined(__GNUC__)
#define COMPILER_GCC 1
......
......@@ -2,7 +2,6 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/config/allocator.gni")
import("//build/config/c++/c++.gni")
import("//build/config/chrome_build.gni")
import("//build/config/chromecast_build.gni")
......@@ -21,7 +20,6 @@ import("//build/config/pch.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//build/config/ui.gni")
import("//build/toolchain/goma.gni")
if (is_android) {
import("//build/config/android/abi.gni")
}
......@@ -96,9 +94,6 @@ config("feature_flags") {
if (use_x11) {
defines += [ "USE_X11=1" ]
}
if (use_allocator != "tcmalloc") {
defines += [ "NO_TCMALLOC" ]
}
if (is_asan || is_hwasan || is_lsan || is_tsan || is_msan) {
defines += [ "MEMORY_TOOL_REPLACES_ALLOCATOR" ]
}
......
......@@ -2,57 +2,13 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/config/sanitizers/sanitizers.gni")
# Temporarily disable tcmalloc on arm64 linux to get rid of compilation errors.
if (is_android || is_mac || is_ios || is_asan || is_lsan || is_tsan ||
is_msan || is_win || is_fuchsia || (is_linux && target_cpu == "arm64")) {
_default_allocator = "none"
} else {
_default_allocator = "tcmalloc"
}
# The debug CRT on Windows has some debug features that are incompatible with
# the shim. NaCl in particular does seem to link some binaries statically
# against the debug CRT with "is_nacl=false".
if ((is_linux || is_android || is_mac ||
(is_win && !is_component_build && !is_debug)) && !is_asan && !is_hwasan &&
!is_lsan && !is_tsan && !is_msan) {
_default_use_allocator_shim = true
} else {
_default_use_allocator_shim = false
}
declare_args() {
# Memory allocator to use. Set to "none" to use default allocator.
use_allocator = _default_allocator
# Causes all the allocations to be routed via allocator_shim.cc.
use_allocator_shim = _default_use_allocator_shim
# Partition alloc is included by default except iOS.
# TODO(thakis): Move this elsewhere, probably
# base/allocator/partition_allocator/buildflags.gni
use_partition_alloc = !is_ios
# Use the new tcmalloc. It's relevant only when use_allocator == "tcmalloc".
# TODO(https://crbug.com/989976): Remove this.
use_new_tcmalloc = true
}
if (is_nacl) {
# Turn off the build flag for NaCL builds to minimize confusion, as NaCL
# doesn't support the heap shim.
use_allocator_shim = false
}
assert(use_allocator == "none" || use_allocator == "tcmalloc")
assert(!is_win || use_allocator == "none", "Tcmalloc doesn't work on Windows.")
assert(!is_mac || use_allocator == "none", "Tcmalloc doesn't work on macOS.")
assert(
!use_allocator_shim || is_linux || is_android || is_win || is_mac,
"use_allocator_shim is supported only on Linux, Android, Windows and macOS targets")
if (is_win && use_allocator_shim) {
assert(!is_component_build,
"The allocator shim doesn't work for the component build on Windows.")
}
......@@ -50,6 +50,7 @@ template("nacl_toolchain") {
# We do not support tcmalloc in the NaCl toolchains.
use_allocator = "none"
use_allocator_shim = false
# We do not support clang code coverage in the NaCl toolchains.
use_clang_coverage = false
......
......@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//base/allocator/allocator.gni")
import("//build/config/allocator.gni")
# Windows/x86 is disabled due to https://crbug.com/969146
......
......@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//base/allocator/allocator.gni")
import("//build/config/allocator.gni")
component("client") {
......
......@@ -42,6 +42,7 @@
#include <sys/types.h>
#endif
#include <string>
#include "base/allocator/buildflags.h"
#include "base/dynamic_annotations.h"
#include "base/sysinfo.h" // for FillProcSelfMaps
#ifndef NO_HEAP_CHECK
......@@ -51,7 +52,7 @@
#include "gperftools/malloc_extension_c.h"
#include "maybe_threads.h"
#ifdef USE_TCMALLOC
#if BUILDFLAG(USE_TCMALLOC)
// Note that malloc_extension can be used without tcmalloc if gperftools'
// heap-profiler is enabled without the tcmalloc memory allocator.
#include "thread_cache.h"
......@@ -228,7 +229,7 @@ void MallocExtension::Register(MallocExtension* implementation) {
unsigned int MallocExtension::GetBytesAllocatedOnCurrentThread() {
// This function is added in Chromium for profiling.
#ifdef USE_TCMALLOC
#if BUILDFLAG(USE_TCMALLOC)
// Note that malloc_extension can be used without tcmalloc if gperftools'
// heap-profiler is enabled without the tcmalloc memory allocator.
return tcmalloc::ThreadCache::GetBytesAllocatedOnCurrentThread();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment