From fc2f6287abe916ee37766c561c5750097847f5a3 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Fri, 13 Jun 2025 10:37:05 +0200 Subject: [PATCH 1/2] new(cmake,userspace/falco): add `mimalloc` allocator library support. Signed-off-by: Federico Di Pierro --- CMakeLists.txt | 37 +++++++++++---- cmake/modules/jemalloc.cmake | 23 +++++----- cmake/modules/mimalloc.cmake | 82 ++++++++++++++++++++++++++++++++++ userspace/falco/CMakeLists.txt | 6 +-- userspace/falco/falco.cpp | 4 ++ 5 files changed, 129 insertions(+), 23 deletions(-) create mode 100644 cmake/modules/mimalloc.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e656b6f..2deb5551 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,18 @@ 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) + +# Mem allocators - linux only for now +if(NOT WIN32 + AND NOT APPLE + AND NOT MINIMAL_BUILD + AND NOT EMSCRIPTEN +) + # If one enables multiple allocators, cmake will fail since all of the allocators cmake modules + # create a `malloc` target. + option(USE_JEMALLOC "Use jemalloc allocator, linux only" OFF) + option(USE_MIMALLOC "Use mimalloc (microsoft) allocator, linux only" OFF) +endif() if(WIN32) if(POLICY CMP0091) @@ -142,19 +153,29 @@ set(CMD_MAKE make) include(ExternalProject) -if(USE_JEMALLOC) - if(USE_ASAN) - message(WARNING "Jemalloc and ASAN are known to have issues when combined") - endif() - include(jemalloc) -endif() - # libs include(falcosecurity-libs) # compute FALCO_VERSION (depends on libs) include(falco-version) +# Mem allocators - linux only for now +if(NOT WIN32 + AND NOT APPLE + AND NOT MINIMAL_BUILD + AND NOT EMSCRIPTEN +) + + if(USE_JEMALLOC) + include(jemalloc) + endif() + if(USE_MIMALLOC) + include(mimalloc) + endif() + + message(STATUS "Will use mem allocator library: ${MALLOC_LIB}") +endif() + # nlohmann-json include(njson) diff --git a/cmake/modules/jemalloc.cmake b/cmake/modules/jemalloc.cmake index dc3950c0..08ed0181 100644 --- a/cmake/modules/jemalloc.cmake +++ b/cmake/modules/jemalloc.cmake @@ -1,6 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 # -# Copyright (C) 2024 The Falco Authors. +# Copyright (C) 2025 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 @@ -25,9 +25,9 @@ elseif(NOT USE_BUNDLED_JEMALLOC) else() set(JEMALLOC_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) endif() - find_library(JEMALLOC_LIB NAMES libjemalloc${JEMALLOC_LIB_SUFFIX}) - if(JEMALLOC_LIB) - message(STATUS "Found JEMALLOC: include: ${JEMALLOC_INCLUDE}, lib: ${JEMALLOC_LIB}") + find_library(MALLOC_LIB NAMES libjemalloc${JEMALLOC_LIB_SUFFIX}) + if(MALLOC_LIB) + message(STATUS "Found system jemalloc: include: ${JEMALLOC_INCLUDE}, lib: ${MALLOC_LIB}") else() message(FATAL_ERROR "Couldn't find system jemalloc") endif() @@ -38,15 +38,15 @@ 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}") - set(JEMALLOC_INCLUDE "${JEMALLOC_SRC}/jemalloc/include/jemalloc") + set(MALLOC_LIB "${JEMALLOC_SRC}/malloc/lib/libjemalloc${JEMALLOC_LIB_SUFFIX}") + set(JEMALLOC_INCLUDE "${JEMALLOC_SRC}/malloc/include/jemalloc") if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64") set(JEMALLOC_ARCH_SPECIFIC_CONFIGURE_ARGS --with-lg-page=14) else() set(JEMALLOC_ARCH_SPECIFIC_CONFIGURE_ARGS "") endif() ExternalProject_Add( - jemalloc + malloc PREFIX "${PROJECT_BINARY_DIR}/jemalloc-prefix" URL "https://github.com/jemalloc/jemalloc/archive/refs/tags/5.3.0.tar.gz" URL_HASH "SHA256=ef6f74fd45e95ee4ef7f9e19ebe5b075ca6b7fbe0140612b2a161abafb7ee179" @@ -56,11 +56,10 @@ else() BUILD_COMMAND make build_lib_static INSTALL_COMMAND "" UPDATE_COMMAND "" - BUILD_BYPRODUCTS ${JEMALLOC_LIB} + BUILD_BYPRODUCTS ${MALLOC_LIB} ) - message(STATUS "Using bundled jemalloc: include: ${JEMALLOC_INCLUDE}, lib: ${JEMALLOC_LIB}") install( - FILES "${JEMALLOC_LIB}" + FILES "${MALLOC_LIB}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/${LIBS_PACKAGE_NAME}" COMPONENT "libs-deps" ) @@ -68,8 +67,8 @@ 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) +if(NOT TARGET malloc) + add_custom_target(malloc) endif() include_directories(${JEMALLOC_INCLUDE}) diff --git a/cmake/modules/mimalloc.cmake b/cmake/modules/mimalloc.cmake new file mode 100644 index 00000000..5f959d66 --- /dev/null +++ b/cmake/modules/mimalloc.cmake @@ -0,0 +1,82 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Copyright (C) 2025 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_MIMALLOC "Use bundled mimalloc (microsoft) allocator" ${USE_BUNDLED_DEPS}) + +if(MIMALLOC_INCLUDE) + # we already have MIMALLOC +elseif(NOT USE_BUNDLED_MIMALLOC) + find_path(MIMALLOC_INCLUDE mimalloc/mimalloc.h) + set(MIMALLOC_INCLUDE ${MIMALLOC_INCLUDE}/mimalloc) + if(BUILD_SHARED_LIBS) + set(MIMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) + else() + set(MIMALLOC_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) + endif() + find_library(MALLOC_LIB NAMES libmimalloc${MIMALLOC_LIB_SUFFIX}) + if(MALLOC_LIB) + message(STATUS "Found system mimalloc: include: ${MIMALLOC_INCLUDE}, lib: ${MALLOC_LIB}") + else() + message(FATAL_ERROR "Couldn't find system mimalloc") + endif() +else() + if(BUILD_SHARED_LIBS) + set(BUILD_STATIC Off) + set(MIMALLOC_LIB_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) + else() + set(BUILD_STATIC On) + set(MIMALLOC_LIB_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) + endif() + set(MIMALLOC_SRC "${PROJECT_BINARY_DIR}/mimalloc-prefix/src") + string(TOLOWER "${CMAKE_BUILD_TYPE}" _build_type) + if(_build_type STREQUAL "debug") + set(MIMALLOC_LIB_BASENAME "libmimalloc-debug") + else() + set(MIMALLOC_LIB_BASENAME "libmimalloc") + endif() + set(MALLOC_LIB "${MIMALLOC_SRC}/malloc-build/${MIMALLOC_LIB_BASENAME}${MIMALLOC_LIB_SUFFIX}") + set(MIMALLOC_INCLUDE ${MIMALLOC_SRC}/include/) + + ExternalProject_Add( + malloc + PREFIX "${PROJECT_BINARY_DIR}/mimalloc-prefix" + URL "https://github.com/microsoft/mimalloc/archive/refs/tags/v3.1.4.tar.gz" + URL_HASH "SHA256=84992bca18d6f74829b884c369de2707085b8248aaf3a1368e21f3993020171f" + CMAKE_ARGS -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} + -DMI_BUILD_SHARED=${BUILD_SHARED_LIBS} + -DMI_BUILD_STATIC=${BUILD_STATIC} + -DMI_BUILD_TESTS=Off + -DMI_BUILD_OBJECT=Off + -DMI_OPT_ARCH=On + INSTALL_COMMAND "" + UPDATE_COMMAND "" + BUILD_BYPRODUCTS ${MALLOC_LIB} + ) + install( + FILES "${MALLOC_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 `mimalloc` without distinguishing +# between "bundled" and "not-bundled" case +if(NOT TARGET malloc) + add_custom_target(malloc) +endif() + +include_directories(${MIMALLOC_INCLUDE}) +add_compile_definitions(HAS_MIMALLOC) diff --git a/userspace/falco/CMakeLists.txt b/userspace/falco/CMakeLists.txt index 9f4082bc..5ae5b836 100644 --- a/userspace/falco/CMakeLists.txt +++ b/userspace/falco/CMakeLists.txt @@ -70,9 +70,9 @@ set(FALCO_INCLUDE_DIRECTORIES set(FALCO_DEPENDENCIES cxxopts) set(FALCO_LIBRARIES falco_engine) -if(USE_JEMALLOC) - list(APPEND FALCO_DEPENDENCIES jemalloc) - list(APPEND FALCO_LIBRARIES ${JEMALLOC_LIB}) +if(USE_JEMALLOC OR USE_MIMALLOC) + list(APPEND FALCO_DEPENDENCIES malloc) + list(APPEND FALCO_LIBRARIES ${MALLOC_LIB}) endif() if(NOT WIN32) diff --git a/userspace/falco/falco.cpp b/userspace/falco/falco.cpp index 3be8a6af..d2d9e149 100644 --- a/userspace/falco/falco.cpp +++ b/userspace/falco/falco.cpp @@ -15,6 +15,10 @@ See the License for the specific language governing permissions and limitations under the License. */ +#ifdef HAS_MIMALLOC +#include +#endif + #include #include From 5a054ea1ddd28a83f6c79c3b53d189b580d03a6e Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Fri, 13 Jun 2025 10:40:49 +0200 Subject: [PATCH 2/2] new(ci): add optional input to reusable_build_packages workflow to enable jemalloc or mimalloc. Enable mimalloc in all CIs but release CI (keep it with jemalloc for now). Signed-off-by: Federico Di Pierro --- .github/workflows/ci.yml | 2 ++ .github/workflows/master.yaml | 2 ++ .github/workflows/release.yaml | 2 ++ .../workflows/reusable_build_packages.yaml | 20 ++++++++++++++++--- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 19f8bbaa..b0060803 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,7 @@ jobs: version: ${{ needs.fetch-version.outputs.version }} enable_debug: true enable_sanitizers: true + use_mimalloc: true build-dev-packages-arm64: needs: [fetch-version] @@ -35,6 +36,7 @@ jobs: arch: aarch64 version: ${{ needs.fetch-version.outputs.version }} enable_debug: true + use_mimalloc: true test-dev-packages: needs: [fetch-version, build-dev-packages-x86_64] diff --git a/.github/workflows/master.yaml b/.github/workflows/master.yaml index 9f3a1e6e..46cf0ec2 100644 --- a/.github/workflows/master.yaml +++ b/.github/workflows/master.yaml @@ -18,6 +18,7 @@ jobs: with: arch: x86_64 version: ${{ needs.fetch-version.outputs.version }} + use_mimalloc: true secrets: inherit build-dev-packages-arm64: @@ -26,6 +27,7 @@ jobs: with: arch: aarch64 version: ${{ needs.fetch-version.outputs.version }} + use_mimalloc: true secrets: inherit test-dev-packages: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c3c77db0..74e3a4da 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -56,6 +56,7 @@ jobs: with: arch: x86_64 version: ${{ github.event.release.tag_name }} + use_jemalloc: true secrets: inherit build-packages-arm64: @@ -64,6 +65,7 @@ jobs: with: arch: aarch64 version: ${{ github.event.release.tag_name }} + use_jemalloc: true secrets: inherit test-packages: diff --git a/.github/workflows/reusable_build_packages.yaml b/.github/workflows/reusable_build_packages.yaml index eb9608b9..e2d021ea 100644 --- a/.github/workflows/reusable_build_packages.yaml +++ b/.github/workflows/reusable_build_packages.yaml @@ -20,6 +20,16 @@ on: required: false type: boolean default: false + use_jemalloc: + description: Use jemalloc memory allocator + required: false + type: boolean + default: false + use_mimalloc: + description: Use mimalloc memory allocator + required: false + type: boolean + default: false permissions: contents: read @@ -88,7 +98,8 @@ jobs: -DMODERN_BPF_SKEL_DIR=/tmp \ -DBUILD_DRIVER=Off \ -DBUILD_BPF=Off \ - -DUSE_JEMALLOC=ON \ + -DUSE_JEMALLOC=${{ inputs.use_jemalloc }} \ + -DUSE_MIMALLOC=${{ inputs.use_mimalloc }} \ -DFALCO_VERSION=${{ inputs.version }} - name: Build project @@ -160,7 +171,8 @@ jobs: -DMODERN_BPF_SKEL_DIR=/tmp \ -DBUILD_DRIVER=Off \ -DBUILD_BPF=Off \ - -DUSE_JEMALLOC=On \ + -DUSE_JEMALLOC=${{ inputs.use_jemalloc }} \ + -DUSE_MIMALLOC=${{ inputs.use_mimalloc }} \ -DFALCO_VERSION=${{ inputs.version }} - name: Build project @@ -209,6 +221,7 @@ jobs: -DBUILD_DRIVER=Off \ -DBUILD_BPF=Off \ -DUSE_JEMALLOC=Off \ + -DUSE_MIMALLOC=Off \ -DUSE_ASAN=On \ -DFALCO_VERSION=${{ inputs.version }} @@ -253,7 +266,8 @@ jobs: -DCMAKE_BUILD_TYPE=Release \ -DCPACK_GENERATOR=TGZ \ -DBUILD_BPF=Off -DBUILD_DRIVER=Off \ - -DUSE_JEMALLOC=On \ + -DUSE_JEMALLOC=${{ inputs.use_jemalloc }} \ + -DUSE_MIMALLOC=${{ inputs.use_mimalloc }} \ -DUSE_BUNDLED_DEPS=On \ -DMUSL_OPTIMIZED_BUILD=On \ -DFALCO_ETC_DIR=/etc/falco \