From 91fb3c692449abf62b62cbe19ab5ea9093f075f9 Mon Sep 17 00:00:00 2001 From: Harvey Tuch Date: Fri, 14 Apr 2017 13:23:43 -0400 Subject: [PATCH 1/2] bazel: tcmalloc support (#415). --- bazel/BUILD | 5 +++++ bazel/envoy_build_system.bzl | 26 +++++++++++++++++++++++++- bazel/target_recipes.bzl | 2 +- ci/prebuilt/BUILD | 7 +++++++ source/common/memory/BUILD | 1 + source/common/profiler/BUILD | 1 + 6 files changed, 40 insertions(+), 2 deletions(-) diff --git a/bazel/BUILD b/bazel/BUILD index 2484a9d0c3190..436b351151439 100644 --- a/bazel/BUILD +++ b/bazel/BUILD @@ -1,3 +1,8 @@ package(default_visibility = ["//visibility:public"]) exports_files(["gen_sh_test_runner.sh"]) + +config_setting( + name = "disable_tcmalloc", + values = {"linkopt": "-fsanitize=address"}, +) diff --git a/bazel/envoy_build_system.bzl b/bazel/envoy_build_system.bzl index 3078656b763d2..2e3924048214d 100644 --- a/bazel/envoy_build_system.bzl +++ b/bazel/envoy_build_system.bzl @@ -18,12 +18,31 @@ ENVOY_COPTS = [ # "-Wold-style-cast", "-std=c++0x", "-includeprecompiled/precompiled.h", -] +] + select({ + "//bazel:disable_tcmalloc": [], + "//conditions:default": ["-DTCMALLOC"], +}) # References to Envoy external dependencies should be wrapped with this function. def envoy_external_dep_path(dep): return "//external:%s" % dep +# Dependencies on tcmalloc_and_profiler should be wrapped with this function. +def tcmalloc_external_dep(): + return select({ + "//bazel:disable_tcmalloc": None, + "//conditions:default": envoy_external_dep_path("tcmalloc_and_profiler"), + }) + +# As above, but wrapped in list form for adding to dep lists. This smell seems needed as +# SelectorValue values have to match the attribute type. See +# https://github.com/bazelbuild/bazel/issues/2273. +def tcmalloc_external_deps(): + return select({ + "//bazel:disable_tcmalloc": [], + "//conditions:default": [envoy_external_dep_path("tcmalloc_and_profiler")], + }) + # Transform the package path (e.g. include/envoy/common) into a path for # exporting the package headers at (e.g. envoy/common). Source files can then # include using this path scheme (e.g. #include "envoy/common/time.h"). @@ -39,8 +58,11 @@ def envoy_cc_library(name, copts = [], visibility = None, external_deps = [], + tcmalloc_dep = None, repository = "", deps = []): + if tcmalloc_dep: + deps += tcmalloc_external_deps() native.cc_library( name = name, srcs = srcs, @@ -73,6 +95,7 @@ def envoy_cc_binary(name, ], linkstatic = 1, visibility = visibility, + malloc = tcmalloc_external_dep(), deps = deps + [ repository + "//source/precompiled:precompiled_includes", ], @@ -104,6 +127,7 @@ def envoy_cc_test(name, copts = ENVOY_COPTS, linkopts = ["-pthread"], linkstatic = 1, + malloc = tcmalloc_external_dep(), deps = [ ":" + name + "_lib", repository + "//test:main" diff --git a/bazel/target_recipes.bzl b/bazel/target_recipes.bzl index 42ad772c22832..111348f623ac8 100644 --- a/bazel/target_recipes.bzl +++ b/bazel/target_recipes.bzl @@ -11,7 +11,7 @@ TARGET_RECIPES = { # that is external to Bazel. "gcovr": "gcovr", "googletest": "googletest", - "gperftools": "gperftools", + "tcmalloc_and_profiler": "gperftools", "http_parser": "http-parser", "lightstep": "lightstep", "nghttp2": "nghttp2", diff --git a/ci/prebuilt/BUILD b/ci/prebuilt/BUILD index 52a61c12877dc..da0fc9303b693 100644 --- a/ci/prebuilt/BUILD +++ b/ci/prebuilt/BUILD @@ -106,3 +106,10 @@ cc_library( hdrs = glob(["thirdparty/tclap-1.2.1/include/**/*.h"]), strip_include_prefix = "thirdparty/tclap-1.2.1/include", ) + +cc_library( + name = "tcmalloc_and_profiler", + srcs = ["thirdparty_build/lib/libtcmalloc_and_profiler.a"], + hdrs = glob(["thirdparty_build/include/gperftools/**/*.h"]), + strip_include_prefix = "thirdparty_build/include", +) diff --git a/source/common/memory/BUILD b/source/common/memory/BUILD index a004b3fd4012c..e9a5b2550e48d 100644 --- a/source/common/memory/BUILD +++ b/source/common/memory/BUILD @@ -6,4 +6,5 @@ envoy_cc_library( name = "stats_lib", srcs = ["stats.cc"], hdrs = ["stats.h"], + tcmalloc_dep = 1, ) diff --git a/source/common/profiler/BUILD b/source/common/profiler/BUILD index 2e4c2df25e74b..1e81fae9d3b72 100644 --- a/source/common/profiler/BUILD +++ b/source/common/profiler/BUILD @@ -6,4 +6,5 @@ envoy_cc_library( name = "profiler_lib", srcs = ["profiler.cc"], hdrs = ["profiler.h"], + tcmalloc_dep = 1, ) From c0d43b0fcd9dcddd5fabe1376688770090dc9f82 Mon Sep 17 00:00:00 2001 From: Harvey Tuch Date: Mon, 17 Apr 2017 10:03:19 -0400 Subject: [PATCH 2/2] Use --define tcmalloc=disabled to configure. --- bazel/BUILD | 2 +- tools/bazel.rc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bazel/BUILD b/bazel/BUILD index 436b351151439..d41fb5ddecc3f 100644 --- a/bazel/BUILD +++ b/bazel/BUILD @@ -4,5 +4,5 @@ exports_files(["gen_sh_test_runner.sh"]) config_setting( name = "disable_tcmalloc", - values = {"linkopt": "-fsanitize=address"}, + values = {"define": "tcmalloc=disabled"}, ) diff --git a/tools/bazel.rc b/tools/bazel.rc index b82582678dcac..0d29ea903ec6d 100644 --- a/tools/bazel.rc +++ b/tools/bazel.rc @@ -3,3 +3,4 @@ build:asan --copt -fsanitize=address build:asan --linkopt -fsanitize=address build:asan --linkopt -ldl +build:asan --define tcmalloc=disabled