Skip to content

Commit 97ecb9c

Browse files
author
Tor Didriksen
committed
Bug#36313839 Add "bundled" tcmalloc [patches]
Add support for cmake option -DWITH_TCMALLOC=bundled This is supported on Linux only. Add an IMPORTED library my_tcmalloc, which is linked into all executables (the external name is libtcmalloc.so.9.16.5). We build it using cmake's EXTERNALPROJECT_ADD functionality. Change-Id: I6553fdde55d651294db3112067e84ef9d423b793 (cherry picked from commit 0ce1e9e579107480ad3fb2a9371279ef76a5cc4a)
1 parent 7a60bc4 commit 97ecb9c

File tree

6 files changed

+121
-2
lines changed

6 files changed

+121
-2
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,12 @@ IF(LINUX)
20232023
HAVE_PTHREAD_SETNAME_NP)
20242024
ENDIF()
20252025

2026+
# The bundled version must be set up after all the configure stuff above.
2027+
IF(LINUX AND WITH_TCMALLOC STREQUAL "bundled")
2028+
ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/extra/gperftools)
2029+
SET(MALLOC_LIBRARY "${CMAKE_BINARY_DIR}/lib/libtcmalloc.so")
2030+
ENDIF()
2031+
20262032
# Cannot use system tirpc with custom OpenSSL
20272033
IF(WITH_SSL_PATH AND (LINUX_RHEL8 OR LINUX_RHEL9))
20282034
SET(WITH_TIRPC "bundled")

cmake/malloc_utils.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ FUNCTION(WARN_MISSING_SYSTEM_JEMALLOC)
4040
ENDFUNCTION()
4141

4242
FUNCTION(FIND_MALLOC_LIBRARY library_name)
43+
# Skip system tcmalloc, and build the bundled version instead.
44+
IF(WITH_TCMALLOC STREQUAL "bundled")
45+
RETURN()
46+
ENDIF()
4347
FIND_LIBRARY(MALLOC_LIBRARY ${library_name})
4448
IF(NOT MALLOC_LIBRARY)
4549
IF(library_name MATCHES "tcmalloc")

cmake/mysql_add_executable.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ FUNCTION(MYSQL_ADD_EXECUTABLE target_arg)
8383
EXCLUDE_FROM_ALL # add target, but do not build it by default
8484
EXCLUDE_FROM_PGO # add target, but do not build for PGO
8585
SKIP_INSTALL # do not install it
86+
SKIP_TCMALLOC # do not link with tcmalloc
8687
)
8788
SET(EXECUTABLE_ONE_VALUE_KW
8889
ADD_TEST # add unit test, sets SKIP_INSTALL
@@ -121,6 +122,19 @@ FUNCTION(MYSQL_ADD_EXECUTABLE target_arg)
121122
ADD_EXECUTABLE(${target} ${sources})
122123
TARGET_COMPILE_FEATURES(${target} PUBLIC cxx_std_17)
123124

125+
IF(TARGET my_tcmalloc)
126+
IF(ARG_SKIP_TCMALLOC OR target MATCHES "^rpd")
127+
# nothing, use glibc malloc/free
128+
ELSE()
129+
IF(WITH_VALGRIND)
130+
TARGET_LINK_LIBRARIES(${target} my_tcmalloc_debug)
131+
ELSE()
132+
TARGET_LINK_LIBRARIES(${target} my_tcmalloc)
133+
ENDIF()
134+
ADD_INSTALL_RPATH(${target} "\$ORIGIN/../${INSTALL_PRIV_LIBDIR}")
135+
ENDIF()
136+
ENDIF()
137+
124138
SET_PATH_TO_CUSTOM_SSL_FOR_APPLE(${target})
125139

126140
IF(ARG_DEPENDENCIES)

extra/gperftools/CMakeLists.txt

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright (c) 2024, Oracle and/or its affiliates.
2+
#
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License, version 2.0,
5+
# as published by the Free Software Foundation.
6+
#
7+
# This program is designed to work with certain software (including
8+
# but not limited to OpenSSL) that is licensed under separate terms,
9+
# as designated in a particular file or component or in included license
10+
# documentation. The authors of MySQL hereby grant you an additional
11+
# permission to link the program and your derivative works with the
12+
# separately licensed software that they have either included with
13+
# the program or referenced in the documentation.
14+
#
15+
# This program is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License, version 2.0, for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with this program; if not, write to the Free Software
22+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23+
24+
INCLUDE(ExternalProject)
25+
26+
SET(TCMALLOC_SOURCE_DIR
27+
"${CMAKE_SOURCE_DIR}/extra/gperftools/gperftools-2.15"
28+
)
29+
30+
FIND_PROGRAM(MAKE_EXECUTABLE make)
31+
32+
SET(TCMALLOC_CMAKE_ARGS
33+
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
34+
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
35+
-DGPERFTOOLS_BUILD_STATIC=OFF
36+
-Dgperftools_build_benchmark=OFF
37+
-Dgperftools_enable_libunwind=OFF
38+
-DBUILD_TESTING=OFF
39+
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/tcmalloc
40+
-DCMAKE_INSTALL_LIBDIR=${CMAKE_BINARY_DIR}/library_output_directory
41+
)
42+
43+
IF(LINUX_RHEL AND ALTERNATIVE_GCC)
44+
LIST(APPEND TCMALLOC_CMAKE_ARGS -DCMAKE_C_FLAGS="-B${GCC_B_PREFIX}")
45+
LIST(APPEND TCMALLOC_CMAKE_ARGS -DCMAKE_CXX_FLAGS="-B${GCC_B_PREFIX}")
46+
ENDIF()
47+
48+
IF(ALTERNATIVE_LD)
49+
LIST(APPEND TCMALLOC_CMAKE_ARGS -DCMAKE_LINKER=${ALTERNATIVE_LD})
50+
ENDIF()
51+
52+
IF(ALTERNATIVE_AR)
53+
LIST(APPEND TCMALLOC_CMAKE_ARGS -DCMAKE_AR=${ALTERNATIVE_AR})
54+
ENDIF()
55+
56+
IF(ALTERNATIVE_RANLIB)
57+
LIST(APPEND TCMALLOC_CMAKE_ARGS -DCMAKE_RANLIB=${ALTERNATIVE_RANLIB})
58+
ENDIF()
59+
60+
EXTERNALPROJECT_ADD(tcmalloc_ext
61+
SOURCE_DIR ${TCMALLOC_SOURCE_DIR}
62+
CMAKE_ARGS ${TCMALLOC_CMAKE_ARGS}
63+
BUILD_BYPRODUCTS
64+
"${CMAKE_BINARY_DIR}/lib/libtcmalloc.so"
65+
"${CMAKE_BINARY_DIR}/lib/libtcmalloc.so.9.16.5"
66+
"${CMAKE_BINARY_DIR}/lib/libtcmalloc_debug.so"
67+
"${CMAKE_BINARY_DIR}/lib/libtcmalloc_debug.so.9.16.5"
68+
)
69+
70+
# Cannot use INSTALL_PRIVATE_LIBRARY because these are not targets.
71+
INSTALL(FILES
72+
"${CMAKE_BINARY_DIR}/lib/libtcmalloc.so"
73+
"${CMAKE_BINARY_DIR}/lib/libtcmalloc.so.9.16.5"
74+
DESTINATION "${INSTALL_PRIV_LIBDIR}" COMPONENT SharedLibraries
75+
PERMISSIONS
76+
OWNER_READ OWNER_WRITE OWNER_EXECUTE
77+
GROUP_READ GROUP_EXECUTE
78+
WORLD_READ WORLD_EXECUTE
79+
)
80+
81+
ADD_LIBRARY(my_tcmalloc SHARED IMPORTED GLOBAL)
82+
SET_TARGET_PROPERTIES(my_tcmalloc PROPERTIES
83+
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
84+
IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/lib/libtcmalloc.so")
85+
86+
ADD_LIBRARY(my_tcmalloc_debug SHARED IMPORTED GLOBAL)
87+
SET_TARGET_PROPERTIES(my_tcmalloc_debug PROPERTIES
88+
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
89+
IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/lib/libtcmalloc_debug.so")
90+
91+
ADD_DEPENDENCIES(my_tcmalloc tcmalloc_ext)
92+
ADD_DEPENDENCIES(my_tcmalloc_debug tcmalloc_ext)

extra/gperftools/gperftools-2.15/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ install(FILES
14831483
${CMAKE_CURRENT_BINARY_DIR}/libprofiler.pc
14841484
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
14851485

1486-
message(WARNING "note: gperftools' cmake support is incomplete and is best-effort only")
1486+
# message(WARNING "note: gperftools' cmake support is incomplete and is best-effort only")
14871487

14881488
#TODO @GENERATE_CHANGELOG_RULES@
14891489
#TODO dist

sql/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,10 @@ IF(NOT mysqld_link_flags)
11871187
SET(mysqld_link_flags)
11881188
ENDIF()
11891189

1190-
IF(UNIX_INSTALL_RPATH_ORIGIN_PRIV_LIBDIR OR LINUX_INSTALL_RPATH_ORIGIN)
1190+
IF(UNIX_INSTALL_RPATH_ORIGIN_PRIV_LIBDIR
1191+
OR LINUX_INSTALL_RPATH_ORIGIN
1192+
OR TARGET my_tcmalloc
1193+
)
11911194
# Because of INSTALL_DEBUG_TARGET which does INSTALL(FILES ...)
11921195
IF(UNIX AND NOT APPLE AND CMAKE_BUILD_TYPE_UPPER STREQUAL "DEBUG")
11931196
STRING_APPEND(mysqld_link_flags

0 commit comments

Comments
 (0)