diff --git a/bazel/config/BUILD.bazel b/bazel/config/BUILD.bazel index f9c92730ff9..a5b833371f9 100644 --- a/bazel/config/BUILD.bazel +++ b/bazel/config/BUILD.bazel @@ -1,4 +1,4 @@ -load("//bazel/config:configs.bzl", "build_mode", "compiler_type", "use_libunwind", "use_gdbserver", "spider_monkey_dbg") +load("//bazel/config:configs.bzl", "build_mode", "compiler_type", "use_libunwind", "use_gdbserver", "spider_monkey_dbg", "allocator") package(default_visibility = ["//visibility:public"]) @@ -255,5 +255,41 @@ config_setting( name = "spider_monkey_dbg_enabled", flag_values = { "//bazel/config:spider_monkey_dbg": "True", + } +) + +# -------------------------------------- +# allocator options +# -------------------------------------- + +allocator( + name = "allocator", + build_setting_default = "system", +) + +config_setting( + name = "auto_allocator_linux", + constraint_values = [ + "@platforms//os:linux", + ], + flag_values = { + "//bazel/config:allocator": "auto", + }, +) + +config_setting( + name = "auto_allocator_windows", + constraint_values = [ + "@platforms//os:windows", + ], + flag_values = { + "//bazel/config:allocator": "auto", + }, +) + +config_setting( + name = "tcmalloc_allocator", + flag_values = { + "//bazel/config:allocator": "tcmalloc", }, ) diff --git a/bazel/config/configs.bzl b/bazel/config/configs.bzl index 6a85f2d23c6..c5597608ef4 100644 --- a/bazel/config/configs.bzl +++ b/bazel/config/configs.bzl @@ -71,3 +71,25 @@ spider_monkey_dbg = rule( implementation = lambda ctx: spider_monkey_dbg_provider(enabled = ctx.build_setting_value), build_setting = config.bool(flag = True), ) + +# ========= +# allocator +# ========= + +allocator_values = ["auto", "system", "tcmalloc"] + +allocator_provider = provider( + doc = "Allocator to use (use \"auto\" for best choice for current platform)", + fields = {"allocator": "choose one of " + ".".join(allocator_values)}, +) + +def allocator_impl(ctx): + allocator_value = ctx.build_setting_value + if allocator_value not in allocator_values: + fail(str(ctx.label) + " allocator allowed to take values {" + ", ".join(allocator_values) + "} but was set to unallowed value " + allocator_value) + return allocator_provider(allocator = allocator_value) + +allocator = rule( + implementation = allocator_impl, + build_setting = config.string(flag = True), +) diff --git a/bazel/mongo_src_rules.bzl b/bazel/mongo_src_rules.bzl index 95bbf1ca5bb..3d1024121db 100644 --- a/bazel/mongo_src_rules.bzl +++ b/bazel/mongo_src_rules.bzl @@ -153,5 +153,11 @@ def mongo_cc_binary( tags = tags, linkstatic = linkstatic, local_defines = MONGO_GLOBAL_DEFINES + LIBUNWIND_DEFINES + local_defines, + malloc = select({ + "//bazel/config:tcmalloc_allocator": "//src/third_party/gperftools:tcmalloc_minimal", + "//bazel/config:auto_allocator_windows": "//src/third_party/gperftools:tcmalloc_minimal", + "//bazel/config:auto_allocator_linux": "//src/third_party/gperftools:tcmalloc_minimal", + "//conditions:default": "@bazel_tools//tools/cpp:malloc", + }), includes = [], ) diff --git a/buildscripts/bazel_testbuilds/unit_test.cpp b/buildscripts/bazel_testbuilds/unit_test.cpp index da64451ccae..fa11aa25002 100644 --- a/buildscripts/bazel_testbuilds/unit_test.cpp +++ b/buildscripts/bazel_testbuilds/unit_test.cpp @@ -1,8 +1,20 @@ // C++ compilation file used for testing bazel compilation. +#include #include int main() { - printf("Hello World!\n"); + void* tcmalloc_so = dlopen( + "/home/ubuntu/mongo/bazel-bin/_solib_arm64/" + "libsrc_Sthird_Uparty_Sgperftools_Slibtcmalloc_Uminimal.so", + RTLD_NOW); + void* tcmalloc = dlsym(tcmalloc_so, "malloc"); + + void* libcmalloc_so = dlopen("/lib/aarch64-linux-gnu/libc.so.6", RTLD_NOW); + void* libcmalloc = dlsym(libcmalloc_so, "malloc"); + + printf("%p [unit test malloc]\n", malloc); + printf("%p [tcmalloc]\n", tcmalloc); + printf("%p [libc malloc]\n", libcmalloc); return 0; } diff --git a/site_scons/site_tools/integrate_bazel.py b/site_scons/site_tools/integrate_bazel.py index c706c3c7006..91f64204883 100644 --- a/site_scons/site_tools/integrate_bazel.py +++ b/site_scons/site_tools/integrate_bazel.py @@ -332,12 +332,17 @@ def generate(env: SCons.Environment.Environment) -> None: else: build_mode = f"opt_{mongo_generators.get_opt_options(env)}" # one of "on", "size", "debug" + # Deprecate tcmalloc-experimental + allocator = "tcmalloc" if env.GetOption( + "allocator") == "tcmalloc-experimental" else env.GetOption("allocator") + bazel_internal_flags = [ f'--//bazel/config:compiler_type={env.ToolchainName()}', f'--//bazel/config:build_mode={build_mode}', f'--//bazel/config:use_libunwind={env["USE_VENDORED_LIBUNWIND"]}', f'--//bazel/config:use_gdbserver={False if env.GetOption("gdbserver") is None else True}', f'--//bazel/config:spider_monkey_dbg={True if env.GetOption("spider-monkey-dbg") == "on" else False}', + f'--//bazel/config:allocator={allocator}', '--compilation_mode=dbg', # always build this compilation mode as we always build with -g '--dynamic_mode=%s' % ('off' if static_link else 'fully'), ] diff --git a/src/third_party/gperftools/BUILD.bazel b/src/third_party/gperftools/BUILD.bazel index 4c525c94881..6532943ba32 100644 --- a/src/third_party/gperftools/BUILD.bazel +++ b/src/third_party/gperftools/BUILD.bazel @@ -17,7 +17,10 @@ config_setting( mongo_cc_library( name = "tcmalloc_minimal", - hdrs = glob(["**/*.h"]), + hdrs = glob(["**/*.h"]) + [ + # debugallocation.cc sources this as an include. + "dist/src/tcmalloc.cc" + ], srcs = [ "dist/src/base/dynamic_annotations.c", "dist/src/base/elf_mem_image.cc",