diff --git a/bin/FindJemalloc.cmake b/bin/FindJemalloc.cmake new file mode 100644 index 0000000000..465f297d11 --- /dev/null +++ b/bin/FindJemalloc.cmake @@ -0,0 +1,81 @@ +############################################################################## +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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 +# +# http://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. +############################################################################## + +find_path(Jemalloc_INCLUDE_DIRS + NAMES jemalloc/jemalloc.h + PATHS ${DSN_THIRDPARTY_ROOT}/include + NO_DEFAULT_PATH +) + +find_library(Jemalloc_SHARED_LIBRARIES + NAMES jemalloc + PATHS ${DSN_THIRDPARTY_ROOT}/lib + NO_DEFAULT_PATH +) + +find_library(Jemalloc_STATIC_LIBRARIES + NAMES libjemalloc_pic.a + PATHS ${DSN_THIRDPARTY_ROOT}/lib + NO_DEFAULT_PATH +) + +if(Jemalloc_INCLUDE_DIRS AND Jemalloc_SHARED_LIBRARIES AND Jemalloc_STATIC_LIBRARIES) + set(Jemalloc_FOUND TRUE) +else() + set(Jemalloc_FOUND FALSE) +endif() + +if(Jemalloc_FOUND) + message(STATUS "Found jemalloc header files: ${Jemalloc_INCLUDE_DIRS}") + message(STATUS "Found jemalloc shared libs: ${Jemalloc_SHARED_LIBRARIES}") + message(STATUS "Found jemalloc static libs: ${Jemalloc_STATIC_LIBRARIES}") +else() + if(Jemalloc_FIND_REQUIRED) + message(FATAL_ERROR "Not found jemalloc in ${DSN_THIRDPARTY_ROOT}") + endif() +endif() + +mark_as_advanced( + Jemalloc_INCLUDE_DIRS + Jemalloc_SHARED_LIBRARIES + Jemalloc_STATIC_LIBRARIES +) + +if(Jemalloc_FOUND AND NOT (TARGET JeMalloc::JeMalloc)) + if("${JEMALLOC_LIB_TYPE}" STREQUAL "SHARED") + add_library(JeMalloc::JeMalloc SHARED IMPORTED) + set_target_properties(JeMalloc::JeMalloc PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${Jemalloc_INCLUDE_DIRS} + IMPORTED_LOCATION ${Jemalloc_SHARED_LIBRARIES} + ) + message(STATUS "Use jemalloc lib type: ${JEMALLOC_LIB_TYPE}") + message(STATUS "Use jemalloc lib: ${Jemalloc_SHARED_LIBRARIES}") + elseif("${JEMALLOC_LIB_TYPE}" STREQUAL "STATIC") + add_library(JeMalloc::JeMalloc STATIC IMPORTED) + set_target_properties(JeMalloc::JeMalloc PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${Jemalloc_INCLUDE_DIRS} + IMPORTED_LINK_INTERFACE_LANGUAGES "C;CXX" + IMPORTED_LOCATION ${Jemalloc_STATIC_LIBRARIES} + ) + message(STATUS "Use jemalloc lib type: ${JEMALLOC_LIB_TYPE}") + message(STATUS "Use jemalloc lib: ${Jemalloc_STATIC_LIBRARIES}") + else() + message(FATAL_ERROR "Invalid jemalloc lib type: ${JEMALLOC_LIB_TYPE}") + endif() +endif() diff --git a/bin/dsn.cmake b/bin/dsn.cmake index 7f4f908989..a46fce6c9b 100644 --- a/bin/dsn.cmake +++ b/bin/dsn.cmake @@ -28,6 +28,17 @@ message(STATUS "ENABLE_GCOV = ${ENABLE_GCOV}") option(ENABLE_GPERF "Enable gperftools (for tcmalloc)" ON) message(STATUS "ENABLE_GPERF = ${ENABLE_GPERF}") +option(USE_JEMALLOC "Use jemalloc" OFF) +message(STATUS "USE_JEMALLOC = ${USE_JEMALLOC}") + +if(ENABLE_GPERF AND USE_JEMALLOC) + message(FATAL_ERROR "cannot enable both gperftools and jemalloc simultaneously") +endif() + +if(USE_JEMALLOC) + set(JEMALLOC_LIB_TYPE "SHARED") +endif() + # ================================================================== # @@ -271,6 +282,13 @@ function(dsn_setup_system_libs) add_definitions(-DDSN_ENABLE_GPERF) endif() + if(USE_JEMALLOC) + find_package(Jemalloc REQUIRED) + # also use cpu profiler provided by gperftools + set(DSN_SYSTEM_LIBS ${DSN_SYSTEM_LIBS} JeMalloc::JeMalloc profiler) + add_definitions(-DDSN_USE_JEMALLOC) + endif() + set(DSN_SYSTEM_LIBS ${DSN_SYSTEM_LIBS} ${CMAKE_THREAD_LIBS_INIT} # the thread library found by FindThreads @@ -310,6 +328,9 @@ function(dsn_setup_thirdparty_libs) find_package(snappy) find_package(zstd) find_package(lz4) + if(USE_JEMALLOC) + find_package(Jemalloc REQUIRED) + endif() find_package(RocksDB REQUIRED) # libhdfs diff --git a/run.sh b/run.sh index 85c51fc87b..0e5941db2e 100755 --- a/run.sh +++ b/run.sh @@ -52,6 +52,7 @@ function usage_build() echo " --notest build without building unit tests, default no" echo " --disable_gperf build without gperftools, this flag is mainly used" echo " to enable valgrind memcheck, default no" + echo " --use_jemalloc build with jemalloc" echo " --skip_thirdparty whether to skip building thirdparties, default no" echo " --check whether to perform code check before building" echo " --sanitizer build with sanitizer to check potential problems, @@ -80,6 +81,7 @@ function run_build() RUN_VERBOSE=NO NO_TEST=NO DISABLE_GPERF=NO + USE_JEMALLOC=NO SKIP_THIRDPARTY=NO CHECK=NO SANITIZER="" @@ -130,6 +132,10 @@ function run_build() --disable_gperf) DISABLE_GPERF=YES ;; + --use_jemalloc) + DISABLE_GPERF=YES + USE_JEMALLOC=YES + ;; --skip_thirdparty) SKIP_THIRDPARTY=YES ;; @@ -187,7 +193,7 @@ function run_build() mkdir -p build pushd build cmake .. -DCMAKE_C_COMPILER=$C_COMPILER -DCMAKE_CXX_COMPILER=$CXX_COMPILER -DCMAKE_BUILD_TYPE=Release \ - -DROCKSDB_PORTABLE=${ROCKSDB_PORTABLE} + -DROCKSDB_PORTABLE=${ROCKSDB_PORTABLE} -DUSE_JEMALLOC=${USE_JEMALLOC} make -j$JOB_NUM exit_if_fail $? popd @@ -211,7 +217,7 @@ function run_build() ONLY_BUILD="$ONLY_BUILD" CLEAR="$CLEAR" JOB_NUM="$JOB_NUM" \ ENABLE_GCOV="$ENABLE_GCOV" SANITIZER="$SANITIZER" \ RUN_VERBOSE="$RUN_VERBOSE" TEST_MODULE="$TEST_MODULE" NO_TEST="$NO_TEST" \ - DISABLE_GPERF="$DISABLE_GPERF" $scripts_dir/build.sh + DISABLE_GPERF="$DISABLE_GPERF" USE_JEMALLOC="$USE_JEMALLOC" $scripts_dir/build.sh } ##################### diff --git a/scripts/linux/build.sh b/scripts/linux/build.sh index 8908978a63..e4069eb8b5 100755 --- a/scripts/linux/build.sh +++ b/scripts/linux/build.sh @@ -87,6 +87,14 @@ else echo "DISABLE_GPERF=NO" fi +if [ "$USE_JEMALLOC" == "YES" ] +then + echo "USE_JEMALLOC=YES" + CMAKE_OPTIONS="$CMAKE_OPTIONS -DUSE_JEMALLOC=ON" +else + echo "USE_JEMALLOC=NO" +fi + if [ ! -z "$SANITIZER" ] then echo "SANITIZER=$SANITIZER" diff --git a/thirdparty/CMakeLists.txt b/thirdparty/CMakeLists.txt index 512d99a35c..05725bf194 100644 --- a/thirdparty/CMakeLists.txt +++ b/thirdparty/CMakeLists.txt @@ -316,12 +316,25 @@ ExternalProject_Add(s2geometry DEPENDS googletest ) +option(USE_JEMALLOC "use jemalloc" OFF) +message(STATUS "USE_JEMALLOC = ${USE_JEMALLOC}") + +ExternalProject_Add(jemalloc + URL https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2 + URL_MD5 3d41fbf006e6ebffd489bdb304d009ae + CONFIGURE_COMMAND ./configure --prefix=${TP_OUTPUT} --enable-cxx --enable-stats --enable-prof + BUILD_COMMAND make + INSTALL_COMMAND make install + BUILD_IN_SOURCE 1 + ) + option(ROCKSDB_PORTABLE "build a portable binary" OFF) ExternalProject_Add(rocksdb URL ${OSS_URL_PREFIX}/pegasus-rocksdb-6.6.4-compatible.zip https://github.com/XiaoMi/pegasus-rocksdb/archive/v6.6.4-compatible.zip URL_MD5 595b21fbe681dcf126c4cccda46f1cbb + DEPENDS jemalloc CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${TP_OUTPUT} -DWITH_LZ4=ON -DWITH_ZSTD=ON @@ -330,6 +343,8 @@ ExternalProject_Add(rocksdb -DWITH_TESTS=OFF -DWITH_GFLAGS=OFF -DUSE_RTTI=ON + -DWITH_JEMALLOC=${USE_JEMALLOC} + -DJEMALLOC_ROOT_DIR=${TP_OUTPUT} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} -DPORTABLE=${ROCKSDB_PORTABLE}