Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
feat: support jemalloc (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
empiredan authored Sep 27, 2021
1 parent cafadac commit eff1acb
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
81 changes: 81 additions & 0 deletions bin/FindJemalloc.cmake
Original file line number Diff line number Diff line change
@@ -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()
21 changes: 21 additions & 0 deletions bin/dsn.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()

# ================================================================== #


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type> build with sanitizer to check potential problems,
Expand Down Expand Up @@ -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=""
Expand Down Expand Up @@ -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
;;
Expand Down Expand Up @@ -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
Expand All @@ -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
}

#####################
Expand Down
8 changes: 8 additions & 0 deletions scripts/linux/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions thirdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down

0 comments on commit eff1acb

Please sign in to comment.