Add jemalloc support to cmake build (RIPD-1472):

Enable jemalloc under option. Create additional option that sets flags
appropriate for use with linux perf. Tested with jemalloc on linux and
macos.
This commit is contained in:
Mike Ellery
2017-05-12 12:13:15 -07:00
committed by seelabs
parent 1daefeb594
commit 18801b81de
3 changed files with 81 additions and 7 deletions

View File

@@ -156,11 +156,17 @@ macro(setup_build_cache)
set(assert false CACHE BOOL "Enables asserts, even in release builds")
set(static false CACHE BOOL
"On linux, link protobuf, openssl, libc++, and boost statically")
set(jemalloc false CACHE BOOL "Enables jemalloc for heap profiling")
set(perf false CACHE BOOL "Enables flags that assist with perf recording")
if (static AND (WIN32 OR APPLE))
message(FATAL_ERROR "Static linking is only supported on linux.")
endif()
if (perf AND (WIN32 OR APPLE))
message(FATAL_ERROR "perf flags are only supported on linux.")
endif()
if (${CMAKE_GENERATOR} STREQUAL "Unix Makefiles" AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
@@ -507,6 +513,10 @@ macro(setup_build_boilerplate)
endif()
endif()
if (perf)
add_compile_options(-fno-omit-frame-pointer)
endif()
############################################################
add_definitions(
@@ -525,8 +535,18 @@ macro(setup_build_boilerplate)
execute_process(
COMMAND ${CMAKE_CXX_COMPILER} -fuse-ld=gold -Wl,--version
ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
if ("${LD_VERSION}" MATCHES "GNU gold")
append_flags(CMAKE_EXE_LINKER_FLAGS -fuse-ld=gold)
# NOTE: THE gold linker inserts -rpath as DT_RUNPATH by default
# intead of DT_RPATH, so you might have slightly unexpected
# runtime ld behavior if you were expecting DT_RPATH.
# Specify --disable-new-dtags to gold if you do not want
# the default DT_RUNPATH behavior. This rpath treatment as well
# as static/dynamic selection means that gold does not currently
# have ideal default behavior when we are using jemalloc. Thus
# for simplicity we don't use it when jemalloc is requested.
# An alternative to disabling would be to figure out all the settings
# required to make gold play nicely with jemalloc.
if (("${LD_VERSION}" MATCHES "GNU gold") AND (NOT jemalloc))
append_flags(CMAKE_EXE_LINKER_FLAGS -fuse-ld=gold)
endif ()
unset(LD_VERSION)
endif()
@@ -555,6 +575,15 @@ macro(setup_build_boilerplate)
STRING(REGEX REPLACE "[-/]DNDEBUG" "" CMAKE_C_FLAGS_RELEASECLASSIC "${CMAKE_C_FLAGS_RELEASECLASSIC}")
endif()
if (jemalloc)
find_package(jemalloc REQUIRED)
add_definitions(-DPROFILE_JEMALLOC)
include_directories(SYSTEM ${JEMALLOC_INCLUDE_DIRS})
link_libraries(${JEMALLOC_LIBRARIES})
get_filename_component(JEMALLOC_LIB_PATH ${JEMALLOC_LIBRARIES} DIRECTORY)
set(CMAKE_BUILD_RPATH ${CMAKE_BUILD_RPATH} ${JEMALLOC_LIB_PATH})
endif()
if (NOT WIN32)
add_definitions(-D_FILE_OFFSET_BITS=64)
append_flags(CMAKE_CXX_FLAGS -frtti -std=c++14 -Wno-invalid-offsetof

View File

@@ -0,0 +1,47 @@
# - Try to find jemalloc
# Once done this will define
# JEMALLOC_FOUND - System has jemalloc
# JEMALLOC_INCLUDE_DIRS - The jemalloc include directories
# JEMALLOC_LIBRARIES - The libraries needed to use jemalloc
if(NOT USE_BUNDLED_JEMALLOC)
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(PC_JEMALLOC QUIET jemalloc)
endif()
else()
set(PC_JEMALLOC_INCLUDEDIR)
set(PC_JEMALLOC_INCLUDE_DIRS)
set(PC_JEMALLOC_LIBDIR)
set(PC_JEMALLOC_LIBRARY_DIRS)
set(LIMIT_SEARCH NO_DEFAULT_PATH)
endif()
set(JEMALLOC_DEFINITIONS ${PC_JEMALLOC_CFLAGS_OTHER})
find_path(JEMALLOC_INCLUDE_DIR jemalloc/jemalloc.h
PATHS ${PC_JEMALLOC_INCLUDEDIR} ${PC_JEMALLOC_INCLUDE_DIRS}
${LIMIT_SEARCH})
# If we're asked to use static linkage, add libjemalloc.a as a preferred library name.
if(JEMALLOC_USE_STATIC)
list(APPEND JEMALLOC_NAMES
"${CMAKE_STATIC_LIBRARY_PREFIX}jemalloc${CMAKE_STATIC_LIBRARY_SUFFIX}")
endif()
list(APPEND JEMALLOC_NAMES jemalloc)
find_library(JEMALLOC_LIBRARY NAMES ${JEMALLOC_NAMES}
HINTS ${PC_JEMALLOC_LIBDIR} ${PC_JEMALLOC_LIBRARY_DIRS}
${LIMIT_SEARCH})
set(JEMALLOC_LIBRARIES ${JEMALLOC_LIBRARY})
set(JEMALLOC_INCLUDE_DIRS ${JEMALLOC_INCLUDE_DIR})
include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set JEMALLOC_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(JeMalloc DEFAULT_MSG
JEMALLOC_LIBRARY JEMALLOC_INCLUDE_DIR)
mark_as_advanced(JEMALLOC_INCLUDE_DIR JEMALLOC_LIBRARY)

View File

@@ -15,11 +15,9 @@
# * ninja builds
# * check openssl version on linux
# * static builds (swd TBD: needs to be tested by building & deploying on different systems)
#
# TBD:
# * jemalloc support
# * count
# * Windows protobuf compiler puts generated file in src directory instead of build directory.
# * jemalloc enabled builds (linux and macos only)
# * perf builds (linux only) - which just sets recommended compiler flags
# for running perf on the executable
#
# Notes:
# * Use the -G"Visual Studio 14 2015 Win64" generator on Windows. Without this