diff --git a/Makefile b/Makefile index a105c55216b..9ac6f2b3159 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,32 @@ SHELL := bash ENABLE_FEATURES ?= +# Frame pointer is enabled by default. +# +# If you want to disable frame-pointer, please manually set the environment +# variable `PROXY_FRAME_POINTER=0 make` (this will cause CPU Profiling to get +# only the top function and not the full call stack). +ifndef PROXY_FRAME_POINTER +export PROXY_FRAME_POINTER=1 +endif + +# The Rust standard library is recompiled by default. (The purpose is to enable +# frame pointers in std) +# +# If you want to avoid recompiling the Rust standard library, please manually +# set the environment variable `PROXY_BUILD_STD=0 make` (this will lose CPU +# Profiling samples related to Rust std functions). +ifndef PROXY_BUILD_STD +export PROXY_BUILD_STD=1 +endif + +ifeq ($(PROXY_FRAME_POINTER),1) +# Enable frame pointers for stable CPU Profiling. +export RUSTFLAGS := $(RUSTFLAGS) -Cforce-frame-pointers=yes +export CFLAGS := $(CFLAGS) -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer +export CXXFLAGS := $(CXXFLAGS) -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer +endif + # Pick an allocator ifeq ($(TCMALLOC),1) ENABLE_FEATURES += tcmalloc @@ -101,6 +127,7 @@ BUILD_INFO_GIT_FALLBACK := "Unknown (no git or not git repo)" BUILD_INFO_RUSTC_FALLBACK := "Unknown" export PROXY_BUILD_TIME := $(shell date -u '+%Y-%m-%d %H:%M:%S') export PROXY_BUILD_RUSTC_VERSION := $(shell rustc --version 2> /dev/null || echo ${BUILD_INFO_RUSTC_FALLBACK}) +export PROXY_BUILD_RUSTC_TARGET := $(shell rustc -vV | awk '/host/ { print $$2 }') export PROXY_BUILD_GIT_HASH ?= $(shell git rev-parse HEAD 2> /dev/null || echo ${BUILD_INFO_GIT_FALLBACK}) export PROXY_BUILD_GIT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD 2> /dev/null || echo ${BUILD_INFO_GIT_FALLBACK}) export DOCKER_IMAGE_NAME ?= "pingcap/tikv" diff --git a/build.sh b/build.sh index f5f43bdeb10..b70f5ee893e 100755 --- a/build.sh +++ b/build.sh @@ -8,6 +8,18 @@ echo "profile is ${PROXY_PROFILE}" echo "engine is ${ENGINE_LABEL_VALUE}" echo "prometheus metric name prefix is ${PROMETHEUS_METRIC_NAME_PREFIX}" +if [[ -n "${PROXY_FRAME_POINTER}" && "${PROXY_FRAME_POINTER}" != 0 ]]; then + echo "frame pointer is enabled" +else + echo "frame pointer is disabled" +fi + +if [[ -n "${PROXY_BUILD_STD}" && "${PROXY_BUILD_STD}" != 0 ]]; then + echo "rust build-std is enabled" +else + echo "rust build-std is disabled" +fi + lib_suffix="so" if [[ $(uname -s) == "Darwin" ]]; then lib_suffix="dylib" diff --git a/cargo-build.sh b/cargo-build.sh index dad586a4dc1..87414be6d5d 100755 --- a/cargo-build.sh +++ b/cargo-build.sh @@ -7,4 +7,9 @@ fi set -ex -cargo build --no-default-features --features "${PROXY_ENABLE_FEATURES}" ${cargo_build_extra_parameter} \ No newline at end of file +if [[ -n "${PROXY_BUILD_STD}" && "${PROXY_BUILD_STD}" != "0" ]]; then + rustup component add rust-src + cargo build --no-default-features --features "${PROXY_ENABLE_FEATURES}" ${cargo_build_extra_parameter} ${PROXY_BUILD_STD_ARGS} +else + cargo build --no-default-features --features "${PROXY_ENABLE_FEATURES}" ${cargo_build_extra_parameter} +fi \ No newline at end of file diff --git a/debug.sh b/debug.sh index f06258f0720..9ebd5cc5e6d 100755 --- a/debug.sh +++ b/debug.sh @@ -5,4 +5,14 @@ set -e export CARGO_PROFILE_DEV_DEBUG="true" export CARGO_PROFILE_RELEASE_DEBUG="true" +if [[ -n "${PROXY_BUILD_STD}" && "${PROXY_BUILD_STD}" != "0" ]]; then + # When `-Z build-std` is enabled, `--target` must be specified explicitly, + # and specifying `--target` will cause the generated binary to be located + # in the `target/${TARGET}/release` directory instead of `target/release`, + # so we need to explicitly specify `--out-dir` here, to avoid errors when + # copying the output binary later. + export PROXY_BUILD_STD_ARGS="-Z build-std=core,std,alloc,proc_macro,test --target=${PROXY_BUILD_RUSTC_TARGET}" + export PROXY_BUILD_STD_ARGS="${PROXY_BUILD_STD_ARGS} -Z unstable-options --out-dir=target/debug" +fi + make build \ No newline at end of file diff --git a/release.sh b/release.sh index 19fcbb469a9..62495a0935f 100755 --- a/release.sh +++ b/release.sh @@ -13,5 +13,16 @@ if [[ $(uname -s) == "Darwin" ]]; then else export PROXY_BUILD_TYPE=release export PROXY_PROFILE=release + + if [[ -n "${PROXY_BUILD_STD}" && "${PROXY_BUILD_STD}" != "0" ]]; then + # When `-Z build-std` is enabled, `--target` must be specified explicitly, + # and specifying `--target` will cause the generated binary to be located + # in the `target/${TARGET}/release` directory instead of `target/release`, + # so we need to explicitly specify `--out-dir` here, to avoid errors when + # copying the output binary later. + export PROXY_BUILD_STD_ARGS="-Z build-std=core,std,alloc,proc_macro,test --target=${PROXY_BUILD_RUSTC_TARGET}" + export PROXY_BUILD_STD_ARGS="${PROXY_BUILD_STD_ARGS} -Z unstable-options --out-dir=target/release" + fi + make build fi