Skip to content

Commit 5d84f89

Browse files
AliSQLAliSQL
authored andcommitted
[Perf] Issue#10 BUILD JEMALLOC STATICALLY INTO MYSQLD
Description: ------------ Port CMake scripts from mariadb revno:4025, jemalloc version is the latest stable 4.2.1, jemalloc website: http://www.canonware.com/jemalloc/ Use the jemalloc memory allocator to replace the default pkmalloc(in glibc), so mysqld can have higher performance for multiple threads. The corresponding overhead is some more physical memory(RES) usage. Also add malloc library info, there are two ways to see this info: 1. `mysqld --version` will show malloc library 2. mysqld startup info in the error log and for now, cause we build jemalloc's source code statically, you will see the malloc library as *bundled jemalloc*. After the introducing of jemalloc, if you run MTR with valgrind option, you will see a lot of annoying message like '... are still reachable in loss record ...' from initialize_performance_schema(). You can safely ignore this, maybe in the future, we will find a way to fix this issue.
1 parent c3a8ca1 commit 5d84f89

204 files changed

Lines changed: 69788 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ INCLUDE(readline)
190190
INCLUDE(mysql_version)
191191
INCLUDE(libutils)
192192
INCLUDE(dtrace)
193+
INCLUDE(jemalloc)
193194
INCLUDE(plugin)
194195
INCLUDE(install_macros)
195196
INCLUDE(install_layout)
@@ -409,6 +410,11 @@ ENDIF()
409410
# Run platform tests
410411
INCLUDE(configure.cmake)
411412

413+
# Find header files from the bundled libraries
414+
# (jemalloc, yassl, readline, pcre, etc)
415+
# before the ones installed in the system
416+
SET(CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE ON)
417+
412418
# Common defines and includes
413419
ADD_DEFINITIONS(-DHAVE_CONFIG_H)
414420
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/include)
@@ -422,6 +428,11 @@ MYSQL_CHECK_EDITLINE()
422428
# Add libevent
423429
MYSQL_CHECK_LIBEVENT()
424430

431+
SET(MALLOC_LIBRARY "system")
432+
IF(NOT WITH_DEBUG)
433+
CHECK_JEMALLOC()
434+
ENDIF()
435+
425436
#
426437
# Setup maintainer mode options by the end. Platform checks are
427438
# not run with the warning options as to not perturb fragile checks

cmake/jemalloc.cmake

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# old cmake does not have ExternalProject file
2+
IF(CMAKE_VERSION VERSION_LESS "2.8.6")
3+
MACRO (CHECK_JEMALLOC)
4+
ENDMACRO()
5+
RETURN()
6+
ENDIF()
7+
8+
INCLUDE(ExternalProject)
9+
10+
MACRO (USE_BUNDLED_JEMALLOC)
11+
SET(SOURCE_DIR "${CMAKE_SOURCE_DIR}/extra/jemalloc")
12+
SET(BINARY_DIR "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/extra/jemalloc/build")
13+
SET(LIBJEMALLOC "libjemalloc")
14+
SET(JEMALLOC_CONFIGURE_OPTS "CC=${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}" "--with-private-namespace=jemalloc_internal_" "--enable-cc-silence")
15+
IF (CMAKE_BUILD_TYPE MATCHES "Debug" AND NOT APPLE) # see the comment in CMakeLists.txt
16+
LIST(APPEND JEMALLOC_CONFIGURE_OPTS --enable-debug)
17+
ENDIF()
18+
19+
IF(CMAKE_GENERATOR MATCHES "Makefiles")
20+
SET(MAKE_COMMAND ${CMAKE_MAKE_PROGRAM})
21+
ELSE() # Xcode/Ninja generators
22+
SET(MAKE_COMMAND make)
23+
ENDIF()
24+
25+
ExternalProject_Add(jemalloc
26+
PREFIX extra/jemalloc
27+
SOURCE_DIR ${SOURCE_DIR}
28+
BINARY_DIR ${BINARY_DIR}
29+
STAMP_DIR ${BINARY_DIR}
30+
CONFIGURE_COMMAND "${SOURCE_DIR}/configure" ${JEMALLOC_CONFIGURE_OPTS}
31+
BUILD_COMMAND ${MAKE_COMMAND} "build_lib_static"
32+
INSTALL_COMMAND ""
33+
)
34+
ADD_LIBRARY(libjemalloc STATIC IMPORTED)
35+
SET_TARGET_PROPERTIES(libjemalloc PROPERTIES IMPORTED_LOCATION "${BINARY_DIR}/lib/libjemalloc_pic.a")
36+
ADD_DEPENDENCIES(libjemalloc jemalloc)
37+
ENDMACRO()
38+
39+
IF(CMAKE_SYSTEM_NAME MATCHES "Linux" OR APPLE)
40+
# Linux and OSX are the only systems where bundled jemalloc can be built without problems,
41+
# as they both have GNU make and jemalloc actually compiles.
42+
# Also, BSDs use jemalloc as malloc already
43+
SET(WITH_JEMALLOC_DEFAULT "yes")
44+
ELSE()
45+
SET(WITH_JEMALLOC_DEFAULT "no")
46+
ENDIF()
47+
48+
SET(WITH_JEMALLOC ${WITH_JEMALLOC_DEFAULT} CACHE STRING
49+
"Which jemalloc to use. Possible values are 'no', 'bundled', 'system', 'yes' (system if possible, otherwise bundled)")
50+
51+
MACRO (CHECK_JEMALLOC)
52+
IF(WITH_JEMALLOC STREQUAL "system" OR WITH_JEMALLOC STREQUAL "yes")
53+
CHECK_LIBRARY_EXISTS(jemalloc malloc_stats_print "" HAVE_JEMALLOC)
54+
IF (HAVE_JEMALLOC)
55+
SET(LIBJEMALLOC jemalloc)
56+
SET(MALLOC_LIBRARY "system jemalloc")
57+
ELSEIF (WITH_JEMALLOC STREQUAL "system")
58+
MESSAGE(FATAL_ERROR "system jemalloc is not found")
59+
ELSEIF (WITH_JEMALLOC STREQUAL "yes")
60+
SET(trybundled 1)
61+
ENDIF()
62+
ENDIF()
63+
IF(WITH_JEMALLOC STREQUAL "bundled" OR trybundled)
64+
USE_BUNDLED_JEMALLOC()
65+
SET(MALLOC_LIBRARY "bundled jemalloc")
66+
ENDIF()
67+
ENDMACRO()

config.h.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@
653653
#define VERSION "@VERSION@"
654654
#define PROTOCOL_VERSION 10
655655

656+
#define MALLOC_LIBRARY "@MALLOC_LIBRARY@"
656657

657658
/* time_t related defines */
658659

extra/jemalloc/COPYING

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Unless otherwise specified, files in the jemalloc source distribution are
2+
subject to the following license:
3+
--------------------------------------------------------------------------------
4+
Copyright (C) 2002-2016 Jason Evans <[email protected]>.
5+
All rights reserved.
6+
Copyright (C) 2007-2012 Mozilla Foundation. All rights reserved.
7+
Copyright (C) 2009-2016 Facebook, Inc. All rights reserved.
8+
9+
Redistribution and use in source and binary forms, with or without
10+
modification, are permitted provided that the following conditions are met:
11+
1. Redistributions of source code must retain the above copyright notice(s),
12+
this list of conditions and the following disclaimer.
13+
2. Redistributions in binary form must reproduce the above copyright notice(s),
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
18+
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20+
EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)