From 48f42d24536ede98c1bb1c4b8046ca505815e698 Mon Sep 17 00:00:00 2001 From: Chris Kennelly Date: Mon, 14 Sep 2026 07:55:01 -0700 Subject: [PATCH] Add create_percpu_tcmalloc_testsuite macro. Cover the 4 variants of percpu_tcmalloc_test.cc in Bazel and CMake: default, flat, no_glibc_rseq, and real (#144). PiperOrigin-RevId: 981150913 --- tcmalloc/internal/BUILD | 7 +--- tcmalloc/internal/CMakeLists.txt | 3 +- tcmalloc/internal/percpu.bzl | 70 ++++++++++++++++++++++++++++++++ tcmalloc/tcmalloc_helpers.cmake | 48 ++++++++++++++++++++++ 4 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 tcmalloc/internal/percpu.bzl diff --git a/tcmalloc/internal/BUILD b/tcmalloc/internal/BUILD index 300843ae5..69e708766 100644 --- a/tcmalloc/internal/BUILD +++ b/tcmalloc/internal/BUILD @@ -21,6 +21,7 @@ load("@rules_cc//cc:cc_library.bzl", "cc_library") load("@rules_cc//cc:cc_test.bzl", "cc_test") load("//tcmalloc:copts.bzl", "TCMALLOC_DEFAULT_COPTS", "TCMALLOC_DEFAULT_CXXOPTS") load("//tcmalloc:variants.bzl", "create_tcmalloc_benchmark") +load(":percpu.bzl", "create_percpu_tcmalloc_testsuite") package(default_visibility = ["//visibility:private"]) @@ -933,13 +934,10 @@ cc_library( ], ) -cc_test( +create_percpu_tcmalloc_testsuite( name = "percpu_tcmalloc_test", - timeout = "long", srcs = ["percpu_tcmalloc_test.cc"], copts = TCMALLOC_DEFAULT_COPTS, - linkstatic = 1, - malloc = ":system_malloc", tags = [ "nohwasan", "noubsan", @@ -955,7 +953,6 @@ cc_test( ":sysinfo", ":util", "//tcmalloc:malloc_extension", - "//tcmalloc/testing:rseq_util", "//tcmalloc/testing:testutil", "@com_github_google_benchmark//:benchmark", "@com_google_absl//absl/base", diff --git a/tcmalloc/internal/CMakeLists.txt b/tcmalloc/internal/CMakeLists.txt index 26524b448..d3d6152cc 100644 --- a/tcmalloc/internal/CMakeLists.txt +++ b/tcmalloc/internal/CMakeLists.txt @@ -938,7 +938,7 @@ tcmalloc_cc_library( "tcmalloc::internal_sysinfo" ) -tcmalloc_cc_test( +create_percpu_tcmalloc_testsuite( NAME tcmalloc_internal_percpu_tcmalloc_test SRCS @@ -968,7 +968,6 @@ tcmalloc_cc_test( "tcmalloc::internal_sysinfo" "tcmalloc::internal_util" "tcmalloc::malloc_extension" - "tcmalloc::testing_rseq_util" "tcmalloc::testing_testutil" ) diff --git a/tcmalloc/internal/percpu.bzl b/tcmalloc/internal/percpu.bzl new file mode 100644 index 000000000..3c6d0b92e --- /dev/null +++ b/tcmalloc/internal/percpu.bzl @@ -0,0 +1,70 @@ +# Copyright 2019 The TCMalloc Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Helper functions to simplify TCMalloc percpu tests.""" + +load("@rules_cc//cc:cc_test.bzl", "cc_test") + +percpu_test_variants = [ + { + "name": "", + "env": {}, + }, + { + "name": "_flat", + "env": {"PERCPU_VCPU_MODE": "flat"}, + }, + { + "name": "_no_glibc_rseq", + "env": {"GLIBC_TUNABLES": "glibc.pthread.rseq=0"}, + }, + { + "name": "_real", + "env": {"PERCPU_VCPU_MODE": "none"}, + }, +] + +def create_percpu_tcmalloc_testsuite( + name, + srcs = ["percpu_tcmalloc_test.cc"], + copts = [], + deps = [], + env = {}, + linkstatic = 1, + malloc = ":system_malloc", + tags = [], + timeout = "long", + **kwargs): + """Creates percpu test targets for all percpu modes.""" + targets = [] + for variant in percpu_test_variants: + test_name = name + variant["name"] + targets.append(test_name) + variant_env = dict(variant["env"]) + variant_env.update(env) + cc_test( + name = test_name, + srcs = srcs, + copts = copts, + deps = deps, + env = variant_env, + linkstatic = linkstatic, + malloc = malloc, + tags = tags, + timeout = timeout, + **kwargs + ) + return targets + +percpu_testsuite = create_percpu_tcmalloc_testsuite diff --git a/tcmalloc/tcmalloc_helpers.cmake b/tcmalloc/tcmalloc_helpers.cmake index caf0a3fbe..b92ae6f9d 100644 --- a/tcmalloc/tcmalloc_helpers.cmake +++ b/tcmalloc/tcmalloc_helpers.cmake @@ -89,3 +89,51 @@ endfunction() function(tcmalloc_cc_binary) tcmalloc_cc_test(${ARGN}) endfunction() + +function(create_percpu_tcmalloc_testsuite) + cmake_parse_arguments(TCMALLOC "" "NAME;ALIAS" "SRCS;HDRS;COPTS;LINKOPTS;DEPS;ENV" ${ARGN}) + string(REGEX REPLACE "_test$" "" BASE_NAME ${TCMALLOC_NAME}) + + set(COMMON_ARGS) + if(TCMALLOC_SRCS) + list(APPEND COMMON_ARGS SRCS ${TCMALLOC_SRCS}) + endif() + if(TCMALLOC_HDRS) + list(APPEND COMMON_ARGS HDRS ${TCMALLOC_HDRS}) + endif() + if(TCMALLOC_COPTS) + list(APPEND COMMON_ARGS COPTS ${TCMALLOC_COPTS}) + endif() + if(TCMALLOC_LINKOPTS) + list(APPEND COMMON_ARGS LINKOPTS ${TCMALLOC_LINKOPTS}) + endif() + if(TCMALLOC_DEPS) + list(APPEND COMMON_ARGS DEPS ${TCMALLOC_DEPS}) + endif() + + set(DEFAULT_ENV) + if(TCMALLOC_ENV) + set(DEFAULT_ENV ENV ${TCMALLOC_ENV}) + endif() + + tcmalloc_cc_test( + NAME ${BASE_NAME}_test + ${COMMON_ARGS} + ${DEFAULT_ENV} + ) + tcmalloc_cc_test( + NAME ${BASE_NAME}_flat_test + ${COMMON_ARGS} + ENV ${TCMALLOC_ENV} "PERCPU_VCPU_MODE=flat" + ) + tcmalloc_cc_test( + NAME ${BASE_NAME}_no_glibc_rseq_test + ${COMMON_ARGS} + ENV ${TCMALLOC_ENV} "GLIBC_TUNABLES=glibc.pthread.rseq=0" + ) + tcmalloc_cc_test( + NAME ${BASE_NAME}_real_test + ${COMMON_ARGS} + ENV ${TCMALLOC_ENV} "PERCPU_VCPU_MODE=none" + ) +endfunction()