diff --git a/.github/workflows/reusable_build_packages.yaml b/.github/workflows/reusable_build_packages.yaml index 650e4317bae..d42ee16ddbd 100644 --- a/.github/workflows/reusable_build_packages.yaml +++ b/.github/workflows/reusable_build_packages.yaml @@ -84,6 +84,7 @@ jobs: -DBUILD_DRIVER=Off \ -DBUILD_BPF=Off \ -DUSE_ASAN=${{ (inputs.sanitizers == true && inputs.arch == 'x86_64' && 'ON') || 'OFF' }} \ + -DUSE_JEMALLOC=On \ -DFALCO_VERSION=${{ inputs.version }} - name: Build project diff --git a/CMakeLists.txt b/CMakeLists.txt index 51f9e71ff51..d42c0e92915 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ option(BUILD_FALCO_UNIT_TESTS "Build falco unit tests" OFF) option(USE_ASAN "Build with AddressSanitizer" OFF) option(USE_UBSAN "Build with UndefinedBehaviorSanitizer" OFF) option(UBSAN_HALT_ON_ERROR "Halt on error when building with UBSan" ON) +option(USE_JEMALLOC "Use jemalloc allocator" OFF) if(WIN32) if(POLICY CMP0091) @@ -141,6 +142,10 @@ set(CMD_MAKE make) include(ExternalProject) +if(USE_JEMALLOC) + include(jemalloc) +endif() + # libs include(falcosecurity-libs) diff --git a/cmake/modules/jemalloc.cmake b/cmake/modules/jemalloc.cmake new file mode 100644 index 00000000000..647c3bf5bf1 --- /dev/null +++ b/cmake/modules/jemalloc.cmake @@ -0,0 +1,64 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (C) 2024 The Falco Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except +# in compliance with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software distributed under the License +# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +# or implied. See the License for the specific language governing permissions and limitations under +# the License. +# + +option(USE_BUNDLED_JEMALLOC "Use bundled jemalloc allocator" ${USE_BUNDLED_DEPS}) + +if(JEMALLOC_LIB) + # we already have JEMALLOC +elseif(NOT USE_BUNDLED_JEMALLOC) + if(BUILD_SHARED_LIBS) + set(JEMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) + else() + set(JEMALLOC_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) + endif() + find_library(JEMALLOC_LIB NAMES jemalloc${JEMALLOC_LIB_SUFFIX}) + if(JEMALLOC_LIB) + message(STATUS "Found JEMALLOC: lib: ${JEMALLOC_LIB}") + else() + message(FATAL_ERROR "Couldn't find system jemalloc") + endif() +else() + if(BUILD_SHARED_LIBS) + set(JEMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) + else() + set(JEMALLOC_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) + endif() + set(JEMALLOC_SRC "${PROJECT_BINARY_DIR}/jemalloc-prefix/src") + set(JEMALLOC_LIB "${JEMALLOC_SRC}/jemalloc/lib/libjemalloc${JEMALLOC_LIB_SUFFIX}") + ExternalProject_Add( + jemalloc + PREFIX "${PROJECT_BINARY_DIR}/jemalloc-prefix" + URL "https://github.com/jemalloc/jemalloc/archive/refs/tags/5.3.0.tar.gz" + URL_HASH "SHA256=ef6f74fd45e95ee4ef7f9e19ebe5b075ca6b7fbe0140612b2a161abafb7ee179" + CONFIGURE_COMMAND ./autogen.sh --enable-prof --disable-libdl + BUILD_IN_SOURCE 1 + BUILD_COMMAND make build_lib_static + INSTALL_COMMAND "" + UPDATE_COMMAND "" + BUILD_BYPRODUCTS ${JEMALLOC_LIB} + ) + message(STATUS "Using bundled jemalloc: lib: ${JEMALLOC_LIB}") + install( + FILES "${JEMALLOC_LIB}" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/${LIBS_PACKAGE_NAME}" + COMPONENT "libs-deps" + ) +endif() + +# We add a custom target, in this way we can always depend on `jemalloc` without distinguishing +# between "bundled" and "not-bundled" case +if(NOT TARGET jemalloc) + add_custom_target(jemalloc) +endif() diff --git a/userspace/falco/CMakeLists.txt b/userspace/falco/CMakeLists.txt index 13849e6fe9a..d6c399059e1 100644 --- a/userspace/falco/CMakeLists.txt +++ b/userspace/falco/CMakeLists.txt @@ -68,9 +68,13 @@ set(FALCO_INCLUDE_DIRECTORIES ) set(FALCO_DEPENDENCIES cxxopts) - set(FALCO_LIBRARIES falco_engine sinsp yaml-cpp) +if(USE_JEMALLOC) + list(APPEND FALCO_DEPENDENCIES jemalloc) + list(APPEND FALCO_LIBRARIES ${JEMALLOC_LIB}) +endif() + if(NOT WIN32) target_sources(falco_application PRIVATE outputs_program.cpp outputs_syslog.cpp) endif() @@ -96,6 +100,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT MINIMAL_BUILD) list( APPEND FALCO_INCLUDE_DIRECTORIES + FALCO_INCLUDE_DIRECTORIES "${OPENSSL_INCLUDE_DIR}" "${GRPC_INCLUDE}" "${GRPCPP_INCLUDE}"