Skip to content

Commit 8315947

Browse files
committed
new(cmake,ci): added support for using jemalloc allocator instead of glibc one.
The jemalloc allocator is enabled by default for published packages. Signed-off-by: Federico Di Pierro <[email protected]>
1 parent 0a8526d commit 8315947

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

.github/workflows/reusable_build_packages.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ jobs:
8484
-DBUILD_DRIVER=Off \
8585
-DBUILD_BPF=Off \
8686
-DUSE_ASAN=${{ (inputs.sanitizers == true && inputs.arch == 'x86_64' && 'ON') || 'OFF' }} \
87+
-DUSE_JEMALLOC=On \
8788
-DFALCO_VERSION=${{ inputs.version }}
8889
8990
- name: Build project

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ option(BUILD_FALCO_UNIT_TESTS "Build falco unit tests" OFF)
2929
option(USE_ASAN "Build with AddressSanitizer" OFF)
3030
option(USE_UBSAN "Build with UndefinedBehaviorSanitizer" OFF)
3131
option(UBSAN_HALT_ON_ERROR "Halt on error when building with UBSan" ON)
32+
option(USE_JEMALLOC "Use jemalloc allocator" OFF)
3233

3334
if(WIN32)
3435
if(POLICY CMP0091)
@@ -141,6 +142,10 @@ set(CMD_MAKE make)
141142

142143
include(ExternalProject)
143144

145+
if(USE_JEMALLOC)
146+
include(jemalloc)
147+
endif()
148+
144149
# libs
145150
include(falcosecurity-libs)
146151

cmake/modules/jemalloc.cmake

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# Copyright (C) 2024 The Falco Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6+
# in compliance with the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software distributed under the License
11+
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12+
# or implied. See the License for the specific language governing permissions and limitations under
13+
# the License.
14+
#
15+
16+
option(USE_BUNDLED_JEMALLOC "Use bundled jemalloc allocator" ${USE_BUNDLED_DEPS})
17+
18+
if(JEMALLOC_LIB)
19+
# we already have JEMALLOC
20+
elseif(NOT USE_BUNDLED_JEMALLOC)
21+
if(BUILD_SHARED_LIBS)
22+
set(JEMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
23+
else()
24+
set(JEMALLOC_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
25+
endif()
26+
find_library(JEMALLOC_LIB NAMES jemalloc${JEMALLOC_LIB_SUFFIX})
27+
if(JEMALLOC_LIB)
28+
message(STATUS "Found JEMALLOC: lib: ${JEMALLOC_LIB}")
29+
else()
30+
message(FATAL_ERROR "Couldn't find system jemalloc")
31+
endif()
32+
else()
33+
if(BUILD_SHARED_LIBS)
34+
set(JEMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX})
35+
else()
36+
set(JEMALLOC_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX})
37+
endif()
38+
set(JEMALLOC_SRC "${PROJECT_BINARY_DIR}/jemalloc-prefix/src")
39+
set(JEMALLOC_LIB "${JEMALLOC_SRC}/jemalloc/lib/libjemalloc${JEMALLOC_LIB_SUFFIX}")
40+
ExternalProject_Add(
41+
jemalloc
42+
PREFIX "${PROJECT_BINARY_DIR}/jemalloc-prefix"
43+
URL "https://github.com/jemalloc/jemalloc/archive/refs/tags/5.3.0.tar.gz"
44+
URL_HASH "SHA256=ef6f74fd45e95ee4ef7f9e19ebe5b075ca6b7fbe0140612b2a161abafb7ee179"
45+
CONFIGURE_COMMAND ./autogen.sh --enable-prof --disable-libdl
46+
BUILD_IN_SOURCE 1
47+
BUILD_COMMAND make build_lib_static
48+
INSTALL_COMMAND ""
49+
UPDATE_COMMAND ""
50+
BUILD_BYPRODUCTS ${JEMALLOC_LIB}
51+
)
52+
message(STATUS "Using bundled jemalloc: lib: ${JEMALLOC_LIB}")
53+
install(
54+
FILES "${JEMALLOC_LIB}"
55+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/${LIBS_PACKAGE_NAME}"
56+
COMPONENT "libs-deps"
57+
)
58+
endif()
59+
60+
# We add a custom target, in this way we can always depend on `jemalloc` without distinguishing
61+
# between "bundled" and "not-bundled" case
62+
if(NOT TARGET jemalloc)
63+
add_custom_target(jemalloc)
64+
endif()

userspace/falco/CMakeLists.txt

+6-1
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ set(FALCO_INCLUDE_DIRECTORIES
6868
)
6969

7070
set(FALCO_DEPENDENCIES cxxopts)
71-
7271
set(FALCO_LIBRARIES falco_engine sinsp yaml-cpp)
7372

73+
if(USE_JEMALLOC)
74+
list(APPEND FALCO_DEPENDENCIES jemalloc)
75+
list(APPEND FALCO_LIBRARIES ${JEMALLOC_LIB})
76+
endif()
77+
7478
if(NOT WIN32)
7579
target_sources(falco_application PRIVATE outputs_program.cpp outputs_syslog.cpp)
7680
endif()
@@ -96,6 +100,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT MINIMAL_BUILD)
96100
list(
97101
APPEND
98102
FALCO_INCLUDE_DIRECTORIES
103+
FALCO_INCLUDE_DIRECTORIES
99104
"${OPENSSL_INCLUDE_DIR}"
100105
"${GRPC_INCLUDE}"
101106
"${GRPCPP_INCLUDE}"

0 commit comments

Comments
 (0)