This is an automated email from the ASF dual-hosted git repository.

philo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-gluten.git


The following commit(s) were added to refs/heads/main by this push:
     new dc6abe54a [VL] Link lib jemalloc produced by custom building (#4747)
dc6abe54a is described below

commit dc6abe54a246a1f789bfdd54b2bd7c1f2bf239ab
Author: PHILO-HE <[email protected]>
AuthorDate: Thu Jun 27 09:02:35 2024 +0800

    [VL] Link lib jemalloc produced by custom building (#4747)
    
    Co-authored-by: BInwei Yang <[email protected]>
---
 ...djemalloc_pic.cmake => Buildjemalloc_pic.cmake} | 15 +---
 cpp/CMake/Findjemalloc_pic.cmake                   | 78 +++++----------------
 cpp/core/CMakeLists.txt                            | 10 ---
 cpp/velox/CMakeLists.txt                           | 11 +++
 cpp/velox/memory/VeloxMemoryManager.cc             |  7 ++
 dev/vcpkg/CONTRIBUTING.md                          |  6 +-
 dev/vcpkg/ports/jemalloc/fix-configure-ac.patch    | 13 ++++
 dev/vcpkg/ports/jemalloc/portfile.cmake            | 79 ++++++++++++++++++++++
 dev/vcpkg/ports/jemalloc/preprocessor.patch        | 12 ++++
 dev/vcpkg/ports/jemalloc/vcpkg.json                |  8 +++
 docs/get-started/build-guide.md                    |  2 +-
 11 files changed, 155 insertions(+), 86 deletions(-)

diff --git a/cpp/CMake/Findjemalloc_pic.cmake 
b/cpp/CMake/Buildjemalloc_pic.cmake
similarity index 88%
copy from cpp/CMake/Findjemalloc_pic.cmake
copy to cpp/CMake/Buildjemalloc_pic.cmake
index fae9f0d7a..7c2316ea9 100644
--- a/cpp/CMake/Findjemalloc_pic.cmake
+++ b/cpp/CMake/Buildjemalloc_pic.cmake
@@ -15,17 +15,6 @@
 # specific language governing permissions and limitations
 # under the License.
 
-# Find Jemalloc
-macro(find_jemalloc)
-  # Find the existing Protobuf
-  set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
-  find_package(jemalloc_pic)
-  if("${Jemalloc_LIBRARY}" STREQUAL "Jemalloc_LIBRARY-NOTFOUND")
-    message(FATAL_ERROR "Jemalloc Library Not Found")
-  endif()
-  set(PROTOC_BIN ${Jemalloc_PROTOC_EXECUTABLE})
-endmacro()
-
 # Building Jemalloc
 macro(build_jemalloc)
   message(STATUS "Building Jemalloc from Source")
@@ -58,6 +47,8 @@ macro(build_jemalloc)
       "--disable-shared"
       "--disable-cxx"
       "--disable-libdl"
+      # For fixing an issue when loading native lib: cannot allocate memory in
+      # static TLS block.
       "--disable-initial-exec-tls"
       "CFLAGS=-fPIC"
       "CXXFLAGS=-fPIC")
@@ -79,5 +70,5 @@ macro(build_jemalloc)
     PROPERTIES INTERFACE_LINK_LIBRARIES Threads::Threads
                IMPORTED_LOCATION "${JEMALLOC_STATIC_LIB}"
                INTERFACE_INCLUDE_DIRECTORIES "${JEMALLOC_INCLUDE_DIR}")
-  add_dependencies(jemalloc::libjemalloc protobuf_ep)
+  add_dependencies(jemalloc::libjemalloc jemalloc_ep)
 endmacro()
diff --git a/cpp/CMake/Findjemalloc_pic.cmake b/cpp/CMake/Findjemalloc_pic.cmake
index fae9f0d7a..ca7b7d213 100644
--- a/cpp/CMake/Findjemalloc_pic.cmake
+++ b/cpp/CMake/Findjemalloc_pic.cmake
@@ -17,67 +17,25 @@
 
 # Find Jemalloc
 macro(find_jemalloc)
-  # Find the existing Protobuf
+  # Find the existing jemalloc
   set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
-  find_package(jemalloc_pic)
-  if("${Jemalloc_LIBRARY}" STREQUAL "Jemalloc_LIBRARY-NOTFOUND")
-    message(FATAL_ERROR "Jemalloc Library Not Found")
-  endif()
-  set(PROTOC_BIN ${Jemalloc_PROTOC_EXECUTABLE})
-endmacro()
-
-# Building Jemalloc
-macro(build_jemalloc)
-  message(STATUS "Building Jemalloc from Source")
-
-  if(DEFINED ENV{GLUTEN_JEMALLOC_URL})
-    set(JEMALLOC_SOURCE_URL "$ENV{GLUTEN_JEMALLOC_URL}")
+  # Find from vcpkg-installed lib path.
+  find_library(
+    JEMALLOC_LIBRARY
+    NAMES jemalloc_pic
+    PATHS
+      
${CMAKE_CURRENT_BINARY_DIR}/../../../dev/vcpkg/vcpkg_installed/x64-linux-avx/lib/
+    NO_DEFAULT_PATH)
+  if("${JEMALLOC_LIBRARY}" STREQUAL "JEMALLOC_LIBRARY-NOTFOUND")
+    message(STATUS "Jemalloc Library Not Found.")
+    set(JEMALLOC_NOT_FOUND TRUE)
   else()
-    set(JEMALLOC_BUILD_VERSION "5.2.1")
-    set(JEMALLOC_SOURCE_URL
-        
"https://github.com/jemalloc/jemalloc/releases/download/${JEMALLOC_BUILD_VERSION}/jemalloc-${JEMALLOC_BUILD_VERSION}.tar.bz2";
-        
"https://github.com/ursa-labs/thirdparty/releases/download/latest/jemalloc-${JEMALLOC_BUILD_VERSION}.tar.bz2";
-    )
+    message(STATUS "Found jemalloc: ${JEMALLOC_LIBRARY}")
+    find_path(JEMALLOC_INCLUDE_DIR jemalloc/jemalloc.h)
+    add_library(jemalloc::libjemalloc STATIC IMPORTED)
+    set_target_properties(
+      jemalloc::libjemalloc
+      PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${JEMALLOC_INCLUDE_DIR}"
+                 IMPORTED_LOCATION "${JEMALLOC_LIBRARY}")
   endif()
-
-  set(JEMALLOC_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/jemalloc_ep-install")
-  set(JEMALLOC_LIB_DIR "${JEMALLOC_PREFIX}/lib")
-  set(JEMALLOC_INCLUDE_DIR "${JEMALLOC_PREFIX}/include")
-  set(JEMALLOC_STATIC_LIB
-      
"${JEMALLOC_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}jemalloc_pic${CMAKE_STATIC_LIBRARY_SUFFIX}"
-  )
-  set(JEMALLOC_INCLUDE "${JEMALLOC_PREFIX}/include")
-  set(JEMALLOC_CONFIGURE_ARGS
-      "AR=${CMAKE_AR}"
-      "CC=${CMAKE_C_COMPILER}"
-      "--prefix=${JEMALLOC_PREFIX}"
-      "--libdir=${JEMALLOC_LIB_DIR}"
-      "--with-jemalloc-prefix=je_gluten_"
-      "--with-private-namespace=je_gluten_private_"
-      "--without-export"
-      "--disable-shared"
-      "--disable-cxx"
-      "--disable-libdl"
-      "--disable-initial-exec-tls"
-      "CFLAGS=-fPIC"
-      "CXXFLAGS=-fPIC")
-  set(JEMALLOC_BUILD_COMMAND ${MAKE} ${MAKE_BUILD_ARGS})
-  ExternalProject_Add(
-    jemalloc_ep
-    URL ${JEMALLOC_SOURCE_URL}
-    PATCH_COMMAND touch doc/jemalloc.3 doc/jemalloc.html
-    CONFIGURE_COMMAND "./configure" ${JEMALLOC_CONFIGURE_ARGS}
-    BUILD_COMMAND ${JEMALLOC_BUILD_COMMAND}
-    BUILD_IN_SOURCE 1
-    BUILD_BYPRODUCTS "${JEMALLOC_STATIC_LIB}"
-    INSTALL_COMMAND make install)
-
-  file(MAKE_DIRECTORY "${JEMALLOC_INCLUDE_DIR}")
-  add_library(jemalloc::libjemalloc STATIC IMPORTED)
-  set_target_properties(
-    jemalloc::libjemalloc
-    PROPERTIES INTERFACE_LINK_LIBRARIES Threads::Threads
-               IMPORTED_LOCATION "${JEMALLOC_STATIC_LIB}"
-               INTERFACE_INCLUDE_DIRECTORIES "${JEMALLOC_INCLUDE_DIR}")
-  add_dependencies(jemalloc::libjemalloc protobuf_ep)
 endmacro()
diff --git a/cpp/core/CMakeLists.txt b/cpp/core/CMakeLists.txt
index 4d7c30402..e17d13581 100644
--- a/cpp/core/CMakeLists.txt
+++ b/cpp/core/CMakeLists.txt
@@ -300,16 +300,6 @@ target_include_directories(
 set_target_properties(gluten PROPERTIES LIBRARY_OUTPUT_DIRECTORY
                                         ${root_directory}/releases)
 
-include(Findjemalloc_pic)
-# Build Jemalloc
-if(BUILD_JEMALLOC)
-  build_jemalloc(${STATIC_JEMALLOC})
-  message(STATUS "Building Jemalloc: ${STATIC_JEMALLOC}")
-else() #
-  find_jemalloc()
-  message(STATUS "Use existing Jemalloc libraries")
-endif()
-
 if(BUILD_TESTS)
   add_subdirectory(tests)
 endif()
diff --git a/cpp/velox/CMakeLists.txt b/cpp/velox/CMakeLists.txt
index c2d690a7e..716a5f68a 100644
--- a/cpp/velox/CMakeLists.txt
+++ b/cpp/velox/CMakeLists.txt
@@ -576,6 +576,17 @@ find_package(Folly REQUIRED CONFIG)
 target_include_directories(velox PUBLIC ${GTEST_INCLUDE_DIRS}
                                         ${PROTOBUF_INCLUDE})
 
+if(BUILD_JEMALLOC)
+  include(Findjemalloc_pic)
+  find_jemalloc()
+  if(JEMALLOC_NOT_FOUND)
+    include(Buildjemalloc_pic)
+    build_jemalloc()
+  endif()
+  add_definitions(-DENABLE_JEMALLOC)
+  target_link_libraries(velox PUBLIC jemalloc::libjemalloc)
+endif()
+
 target_link_libraries(velox PUBLIC gluten)
 add_velox_dependencies()
 
diff --git a/cpp/velox/memory/VeloxMemoryManager.cc 
b/cpp/velox/memory/VeloxMemoryManager.cc
index 733eb4c4b..efd165b73 100644
--- a/cpp/velox/memory/VeloxMemoryManager.cc
+++ b/cpp/velox/memory/VeloxMemoryManager.cc
@@ -16,6 +16,10 @@
  */
 
 #include "VeloxMemoryManager.h"
+#ifdef ENABLE_JEMALLOC
+#include <jemalloc/jemalloc.h>
+#endif
+
 #include "velox/common/memory/MallocAllocator.h"
 #include "velox/common/memory/MemoryPool.h"
 #include "velox/exec/MemoryReclaimer.h"
@@ -326,6 +330,9 @@ VeloxMemoryManager::~VeloxMemoryManager() {
     usleep(waitMs * 1000);
     accumulatedWaitMs += waitMs;
   }
+#ifdef ENABLE_JEMALLOC
+  je_gluten_malloc_stats_print(NULL, NULL, NULL);
+#endif
 }
 
 } // namespace gluten
diff --git a/dev/vcpkg/CONTRIBUTING.md b/dev/vcpkg/CONTRIBUTING.md
index b725f0b50..719bc91db 100644
--- a/dev/vcpkg/CONTRIBUTING.md
+++ b/dev/vcpkg/CONTRIBUTING.md
@@ -13,7 +13,7 @@ Please init vcpkg env first:
 
 Vcpkg already maintains a lot of libraries.
 You can find them by vcpkg cli.
-(NOTE: Please always use cli beacause [packages on 
vcpkg.io](https://vcpkg.io/en/packages.html) is outdate).
+(NOTE: Please always use cli because [packages on 
vcpkg.io](https://vcpkg.io/en/packages.html) is outdate).
 
 ```
 $ ./.vcpkg/vcpkg search folly
@@ -28,7 +28,7 @@ folly[zlib]                               Support zlib for 
compression
 folly[zstd]                               Support zstd for compression
 ```
 
-`[...]` means additional features. Then add depend into 
[vcpkg.json](./vcpkg.json).
+`[...]` means additional features. Then add the dependency into 
[vcpkg.json](./vcpkg.json).
 
 ``` json
 {
@@ -144,7 +144,7 @@ See [vcpkg.json 
reference](https://learn.microsoft.com/en-us/vcpkg/reference/vcp
 `portfile.cmake` is a cmake script describing how to build and install the 
package.
 A typical portfile has 3 stages:
 
-**Download and perpare source**:
+**Download and prepare source**:
 
 ``` cmake
 # Download from Github
diff --git a/dev/vcpkg/ports/jemalloc/fix-configure-ac.patch 
b/dev/vcpkg/ports/jemalloc/fix-configure-ac.patch
new file mode 100644
index 000000000..7799dfb9e
--- /dev/null
+++ b/dev/vcpkg/ports/jemalloc/fix-configure-ac.patch
@@ -0,0 +1,13 @@
+diff --git a/configure.ac b/configure.ac
+index f6d25f334..3115504e2 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -1592,7 +1592,7 @@ fi
+ [enable_uaf_detection="0"]
+ )
+ if test "x$enable_uaf_detection" = "x1" ; then
+-  AC_DEFINE([JEMALLOC_UAF_DETECTION], [ ])
++  AC_DEFINE([JEMALLOC_UAF_DETECTION], [ ], ["enable UAF"])
+ fi
+ AC_SUBST([enable_uaf_detection])
+ 
diff --git a/dev/vcpkg/ports/jemalloc/portfile.cmake 
b/dev/vcpkg/ports/jemalloc/portfile.cmake
new file mode 100644
index 000000000..6cac12ca3
--- /dev/null
+++ b/dev/vcpkg/ports/jemalloc/portfile.cmake
@@ -0,0 +1,79 @@
+vcpkg_from_github(
+    OUT_SOURCE_PATH SOURCE_PATH
+    REPO jemalloc/jemalloc
+    REF 54eaed1d8b56b1aa528be3bdd1877e59c56fa90c
+    SHA512 
527bfbf5db9a5c2b7b04df4785b6ae9d445cff8cb17298bf3e550c88890d2bd7953642d8efaa417580610508279b527d3a3b9e227d17394fd2013c88cb7ae75a
+    HEAD_REF master
+    PATCHES
+        fix-configure-ac.patch
+        preprocessor.patch
+)
+if(VCPKG_TARGET_IS_WINDOWS)
+    set(opts "ac_cv_search_log=none required"
+      "--without-private-namespace"
+      "--with-jemalloc-prefix=je_gluten_"
+      "--with-private-namespace=je_gluten_private_"
+      "--without-export"
+      "--disable-shared"
+      "--disable-cxx"
+      "--disable-libdl"
+      # For fixing an issue when loading native lib: cannot allocate memory in 
static TLS block.
+      "--disable-initial-exec-tls"
+      "CFLAGS=-fPIC"
+      "CXXFLAGS=-fPIC")
+else()
+    set(opts
+      "--with-jemalloc-prefix=je_gluten_"
+      "--with-private-namespace=je_gluten_private_"
+      "--without-export"
+      "--disable-shared"
+      "--disable-cxx"
+      "--disable-libdl"
+      # For fixing an issue when loading native lib: cannot allocate memory in 
static TLS block.
+      "--disable-initial-exec-tls"
+      "CFLAGS=-fPIC"
+      "CXXFLAGS=-fPIC")
+endif()
+
+vcpkg_configure_make(
+    SOURCE_PATH "${SOURCE_PATH}"
+    AUTOCONFIG
+    NO_WRAPPERS
+    OPTIONS ${opts}
+)
+
+vcpkg_install_make()
+
+if(VCPKG_TARGET_IS_WINDOWS)
+    file(COPY "${SOURCE_PATH}/include/msvc_compat/strings.h" DESTINATION 
"${CURRENT_PACKAGES_DIR}/include/jemalloc/msvc_compat")
+    vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/include/jemalloc/jemalloc.h" 
"<strings.h>" "\"msvc_compat/strings.h\"")
+    if(VCPKG_LIBRARY_LINKAGE STREQUAL "dynamic")
+        file(COPY 
"${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-rel/lib/jemalloc.lib" DESTINATION 
"${CURRENT_PACKAGES_DIR}/lib")
+        file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/bin")
+        file(RENAME "${CURRENT_PACKAGES_DIR}/lib/jemalloc.dll" 
"${CURRENT_PACKAGES_DIR}/bin/jemalloc.dll")
+    endif()
+    if(NOT VCPKG_BUILD_TYPE)
+        if(VCPKG_LIBRARY_LINKAGE STREQUAL "dynamic")
+            file(COPY 
"${CURRENT_BUILDTREES_DIR}/${TARGET_TRIPLET}-dbg/lib/jemalloc.lib" DESTINATION 
"${CURRENT_PACKAGES_DIR}/debug/lib")
+            file(MAKE_DIRECTORY "${CURRENT_PACKAGES_DIR}/debug/bin")
+            file(RENAME "${CURRENT_PACKAGES_DIR}/debug/lib/jemalloc.dll" 
"${CURRENT_PACKAGES_DIR}/debug/bin/jemalloc.dll")
+        endif()
+    endif()
+    if(VCPKG_LIBRARY_LINKAGE STREQUAL "static")
+        
vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/lib/pkgconfig/jemalloc.pc" 
"install_suffix=" "install_suffix=_s")
+        if(NOT VCPKG_BUILD_TYPE)
+            
vcpkg_replace_string("${CURRENT_PACKAGES_DIR}/debug/lib/pkgconfig/jemalloc.pc" 
"install_suffix=" "install_suffix=_s")
+        endif()
+    endif()
+endif()
+
+vcpkg_fixup_pkgconfig()
+
+vcpkg_copy_pdbs()
+
+file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
+file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share")
+file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/tools")
+
+# Handle copyright
+file(INSTALL "${SOURCE_PATH}/COPYING" DESTINATION 
"${CURRENT_PACKAGES_DIR}/share/${PORT}" RENAME copyright)
diff --git a/dev/vcpkg/ports/jemalloc/preprocessor.patch 
b/dev/vcpkg/ports/jemalloc/preprocessor.patch
new file mode 100644
index 000000000..6e6e2d140
--- /dev/null
+++ b/dev/vcpkg/ports/jemalloc/preprocessor.patch
@@ -0,0 +1,12 @@
+diff --git a/configure.ac b/configure.ac
+index 3115504e2..ffb504b08 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -749,6 +749,7 @@ case "${host}" in
+       so="dll"
+       if test "x$je_cv_msvc" = "xyes" ; then
+         importlib="lib"
++        JE_APPEND_VS(CPPFLAGS, -DJEMALLOC_NO_PRIVATE_NAMESPACE)
+         DSO_LDFLAGS="-LD"
+         EXTRA_LDFLAGS="-link -DEBUG"
+         CTARGET='-Fo$@'
diff --git a/dev/vcpkg/ports/jemalloc/vcpkg.json 
b/dev/vcpkg/ports/jemalloc/vcpkg.json
new file mode 100644
index 000000000..007e05b93
--- /dev/null
+++ b/dev/vcpkg/ports/jemalloc/vcpkg.json
@@ -0,0 +1,8 @@
+{
+  "name": "jemalloc",
+  "version": "5.3.0",
+  "port-version": 1,
+  "description": "jemalloc is a general purpose malloc(3) implementation that 
emphasizes fragmentation avoidance and scalable concurrency support",
+  "homepage": "https://jemalloc.net/";,
+  "license": "BSD-2-Clause"
+}
diff --git a/docs/get-started/build-guide.md b/docs/get-started/build-guide.md
index 3db2244ba..b2e4b9560 100644
--- a/docs/get-started/build-guide.md
+++ b/docs/get-started/build-guide.md
@@ -14,7 +14,7 @@ Please set them via `--`, e.g. `--build_type=Release`.
 | build_tests            | Build gluten cpp tests.                             
                                               | OFF     |
 | build_examples         | Build udf example.                                  
                                               | OFF     |
 | build_benchmarks       | Build gluten cpp benchmarks.                        
                                               | OFF     |
-| build_jemalloc         | Build with jemalloc.                                
                                               | ON      |
+| build_jemalloc         | Build with jemalloc.                                
                                               | OFF      |
 | build_protobuf         | Build protobuf lib.                                 
                                               | ON      |
 | enable_qat             | Enable QAT for shuffle data de/compression.         
                                               | OFF     |
 | enable_iaa             | Enable IAA for shuffle data de/compression.         
                                               | OFF     |


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to