From 07982b7243b4e0d7775c637cf5fd935bc3c8a047 Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Wed, 29 Jun 2022 10:59:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=99=E6=80=81=E8=BF=9E=E6=8E=A5=E5=88=B0je?= =?UTF-8?q?malloc=E5=BA=93:=20#1759?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 12 ++++++++++ cmake/FindJEMALLOC.cmake | 51 +++++++++++++++++++++++++++++++++------- cmake/Jemalloc.cmake | 50 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 cmake/Jemalloc.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 1dc1b7db..a2edf606 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,10 @@ endif() set(CMAKE_CXX_STANDARD 11) #加载自定义模块 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +set(DEP_ROOT_DIR ${CMAKE_SOURCE_DIR}/3rdpart/external-${CMAKE_SYSTEM_NAME}) +if(NOT EXISTS ${DEP_ROOT_DIR}) + file(MAKE_DIRECTORY ${DEP_ROOT_DIR}) +endif() find_program(CCACHE_FOUND ccache) if(CCACHE_FOUND) @@ -108,6 +112,7 @@ option(ENABLE_MSVC_MT "Enable MSVC Mt/Mtd lib" true) option(ENABLE_API_STATIC_LIB "Enable mk_api static lib" false) option(USE_SOLUTION_FOLDERS "Enable solution dir supported" ON) option(ENABLE_SRT "Enable SRT" true) +option(ENABLE_JEMALLOC_STATIC "Enable static linking to the jemalloc library" false) # ---------------------------------------------------------------------------- # Solution folders: # ---------------------------------------------------------------------------- @@ -299,6 +304,13 @@ if (ENABLE_FFMPEG) endif () +if(ENABLE_JEMALLOC_STATIC) + include(cmake/Jemalloc.cmake) + include_directories(${DEP_ROOT_DIR}/${JEMALLOC_NAME}/include/jemalloc) + link_directories(${DEP_ROOT_DIR}/${JEMALLOC_NAME}/lib) + set(JEMALLOC_ROOT_DIR "${DEP_ROOT_DIR}/${JEMALLOC_NAME}") +endif () + #默认链接jemalloc库避免内存碎片 find_package(JEMALLOC QUIET) if (JEMALLOC_FOUND) diff --git a/cmake/FindJEMALLOC.cmake b/cmake/FindJEMALLOC.cmake index 35184347..e2946e90 100644 --- a/cmake/FindJEMALLOC.cmake +++ b/cmake/FindJEMALLOC.cmake @@ -1,16 +1,49 @@ -find_path(JEMALLOC_INCLUDE_DIR - NAMES jemalloc/jemalloc.h - ) +# Tries to find Jemalloc headers and libraries. +# +# Usage of this module as follows: +# +# find_package(jemalloc) +# +# Variables used by this module, they can change the default behaviour and need +# to be set before calling find_package: +# +# JEMALLOC_ROOT_DIR Set this variable to the root installation of +# Jemalloc if the module has problems finding +# the proper installation path. +# +# Variables defined by this module: +# +# JEMALLOC_FOUND System has Jemalloc libs/headers +# JEMALLOC_LIBRARIES The Jemalloc libraries +# JEMALLOC_INCLUDE_DIR The location of Jemalloc headers +if (ENABLE_JEMALLOC_STATIC) + find_path(JEMALLOC_INCLUDE_DIR + NAMES jemalloc.h + HINTS ${JEMALLOC_ROOT_DIR}/include/jemalloc + NO_DEFAULT_PATH) -find_library(JEMALLOC_LIBRARY - NAMES jemalloc - ) + find_library(JEMALLOC_LIBRARIES + NAMES jemalloc + HINTS ${JEMALLOC_ROOT_DIR}/lib + NO_DEFAULT_PATH) +else () + find_path(JEMALLOC_INCLUDE_DIR + NAMES jemalloc/jemalloc.h + ) -set(JEMALLOC_INCLUDE_DIRS ${JEMALLOC_INCLUDE_DIR}) -set(JEMALLOC_LIBRARIES ${JEMALLOC_LIBRARY}) + find_library(JEMALLOC_LIBRARIES + NAMES jemalloc + ) +endif () include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(JEMALLOC DEFAULT_MSG + JEMALLOC_LIBRARIES + JEMALLOC_INCLUDE_DIR) -find_package_handle_standard_args(JEMALLOC DEFAULT_MSG JEMALLOC_LIBRARY JEMALLOC_INCLUDE_DIR) +mark_as_advanced( + JEMALLOC_ROOT_DIR + JEMALLOC_LIBRARIES + JEMALLOC_INCLUDE_DIR) diff --git a/cmake/Jemalloc.cmake b/cmake/Jemalloc.cmake new file mode 100644 index 00000000..94d46b64 --- /dev/null +++ b/cmake/Jemalloc.cmake @@ -0,0 +1,50 @@ +# Download and build Jemalloc + +set(JEMALLOC_VERSION 5.2.1) +set(JEMALLOC_NAME jemalloc-${JEMALLOC_VERSION}) +set(JEMALLOC_TAR_PATH ${DEP_ROOT_DIR}/${JEMALLOC_NAME}.tar.bz2) + +list(APPEND jemalloc_CONFIG_ARGS --disable-initial-exec-tls) +list(APPEND jemalloc_CONFIG_ARGS --without-export) +list(APPEND jemalloc_CONFIG_ARGS --disable-stats) +list(APPEND jemalloc_CONFIG_ARGS --disable-libdl) +#list(APPEND jemalloc_CONFIG_ARGS --disable-cxx) +#list(APPEND jemalloc_CONFIG_ARGS --with-jemalloc-prefix=je_) +#list(APPEND jemalloc_CONFIG_ARGS --enable-debug) + +if(NOT EXISTS ${JEMALLOC_TAR_PATH}) + message(STATUS "Downloading ${JEMALLOC_NAME}...") + file(DOWNLOAD https://github.com/jemalloc/jemalloc/releases/download/${JEMALLOC_VERSION}/${JEMALLOC_NAME}.tar.bz2 + ${JEMALLOC_TAR_PATH}) +endif() + +SET( DIR_CONTAINING_JEMALLOC ${DEP_ROOT_DIR}/${JEMALLOC_NAME} ) + +if(NOT EXISTS ${DIR_CONTAINING_JEMALLOC}) + message(STATUS "Extracting jemalloc...") + execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzf ${JEMALLOC_TAR_PATH} WORKING_DIRECTORY ${DEP_ROOT_DIR}) +endif() + + +if(NOT EXISTS ${DIR_CONTAINING_JEMALLOC}/Makefile) + message("Configuring jemalloc locally...") + # Builds with "--with-jemalloc-prefix=je_" on OSX +# SET( BASH_COMMAND_TO_RUN bash -l -c "cd ${DIR_CONTAINING_JEMALLOC} && ./configure ${jemalloc_CONFIG_ARGS}" ) +# +# EXECUTE_PROCESS( COMMAND ${BASH_COMMAND_TO_RUN} +# WORKING_DIRECTORY ${DIR_CONTAINING_JEMALLOC} RESULT_VARIABLE JEMALLOC_CONFIGURE ) + + execute_process(COMMAND ./configure ${jemalloc_CONFIG_ARGS} WORKING_DIRECTORY ${DIR_CONTAINING_JEMALLOC} RESULT_VARIABLE JEMALLOC_CONFIGURE) + if(NOT JEMALLOC_CONFIGURE EQUAL 0) + message(FATAL_ERROR "${JEMALLOC_NAME} configure failed!") + message("${JEMALLOC_CONFIGURE}") + endif() +endif() + +if(NOT EXISTS ${DIR_CONTAINING_JEMALLOC}/lib/libjemalloc.a) + message("Building jemalloc locally...") + execute_process(COMMAND make "build_lib_static" WORKING_DIRECTORY ${DIR_CONTAINING_JEMALLOC}) + if(NOT EXISTS ${DIR_CONTAINING_JEMALLOC}/lib/libjemalloc.a) + message(FATAL_ERROR "${JEMALLOC_NAME} build failed!") + endif() +endif() \ No newline at end of file