From db3f0147be39b74c849e64f2f75cbf1629001072 Mon Sep 17 00:00:00 2001 From: alexliyu7352 Date: Mon, 9 Oct 2023 19:37:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7jemalloc=E5=88=B05.3=E7=89=88?= =?UTF-8?q?=E6=9C=AC=20(#2884)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 升级jemalloc到5.3版本,相比5.2.1内存稳定许多,去除--without-export编译参数, 解决类似glibc中分配内存却在jemalloc中释放导致崩溃问题. 比如: toolkit::LoggerWrapper::printLogV中使用的vasprintf --- CMakeLists.txt | 13 ++++++++++++- cmake/Jemalloc.cmake | 23 ++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cb85679b..39c6c517 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ option(ENABLE_FAAC "Enable FAAC" OFF) option(ENABLE_FFMPEG "Enable FFmpeg" OFF) option(ENABLE_HLS "Enable HLS" ON) option(ENABLE_JEMALLOC_STATIC "Enable static linking to the jemalloc library" OFF) +option(ENABLE_JEMALLOC_DUMP "Enable jemalloc to dump malloc statistics" OFF) option(ENABLE_MEM_DEBUG "Enable Memory Debug" OFF) option(ENABLE_MP4 "Enable MP4" ON) option(ENABLE_HLS_FMP4 "Enable HLS-FMP4" ON) @@ -335,7 +336,11 @@ if(ENABLE_JEMALLOC_STATIC) if(NOT EXISTS ${DEP_ROOT_DIR}) file(MAKE_DIRECTORY ${DEP_ROOT_DIR}) endif() - + if (ENABLE_JEMALLOC_DUMP) + set(ENABLE_JEMALLOC_STAT ON) + else () + set(ENABLE_JEMALLOC_STAT OFF) + endif () include(Jemalloc) include_directories(SYSTEM ${DEP_ROOT_DIR}/${JEMALLOC_NAME}/include/jemalloc) link_directories(${DEP_ROOT_DIR}/${JEMALLOC_NAME}/lib) @@ -349,6 +354,12 @@ if(JEMALLOC_FOUND) message(STATUS "found library: ${JEMALLOC_LIBRARIES}") include_directories(${JEMALLOC_INCLUDE_DIR}) update_cached_list(MK_LINK_LIBRARIES ${JEMALLOC_LIBRARIES}) + add_definitions(-DUSE_JEMALLOC) + message(STATUS "jemalloc will be used to avoid memory fragmentation") + if (ENABLE_JEMALLOC_DUMP) + add_definitions(-DENABLE_JEMALLOC_DUMP) + message(STATUS "jemalloc will save memory usage statistics when the program exits") + endif () endif() # 查找 openssl 是否安装 diff --git a/cmake/Jemalloc.cmake b/cmake/Jemalloc.cmake index 94d46b64..38847e58 100644 --- a/cmake/Jemalloc.cmake +++ b/cmake/Jemalloc.cmake @@ -1,21 +1,34 @@ # Download and build Jemalloc -set(JEMALLOC_VERSION 5.2.1) +set(JEMALLOC_VERSION 5.3.0) 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 --without-export) +if (ENABLE_JEMALLOC_STAT) + list(APPEND jemalloc_CONFIG_ARGS --enable-stats) + message(STATUS "Jemalloc stats enabled") +else () list(APPEND jemalloc_CONFIG_ARGS --disable-stats) + message(STATUS "Jemalloc stats disabled") +endif () 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}) + set(JEMALLOC_URL https://github.com/jemalloc/jemalloc/releases/download/${JEMALLOC_VERSION}/${JEMALLOC_NAME}.tar.bz2) + message(STATUS "Downloading ${JEMALLOC_NAME} from ${JEMALLOC_URL}") + file(DOWNLOAD ${JEMALLOC_URL} ${JEMALLOC_TAR_PATH} SHOW_PROGRESS STATUS JEMALLOC_DOWNLOAD_STATUS LOG JEMALLOC_DOWNLOAD_LOG) + list(GET JEMALLOC_DOWNLOAD_STATUS 0 JEMALLOC_DOWNLOAD_STATUS_CODE) + if(NOT JEMALLOC_DOWNLOAD_STATUS_CODE EQUAL 0) + file(REMOVE ${JEMALLOC_TAR_PATH}) + message(STATUS "${JEMALLOC_DOWNLOAD_LOG}") + message(FATAL_ERROR "${JEMALLOC_NAME} download failed! error is ${JEMALLOC_DOWNLOAD_STATUS}") + return() + endif () endif() SET( DIR_CONTAINING_JEMALLOC ${DEP_ROOT_DIR}/${JEMALLOC_NAME} )