You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by zw...@apache.org on 2022/01/18 15:41:13 UTC

[trafficserver] branch 9.2.x updated: add --with-mimalloc option (#8233)

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

zwoop pushed a commit to branch 9.2.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/9.2.x by this push:
     new c53db99  add --with-mimalloc option (#8233)
c53db99 is described below

commit c53db99e9d46ad7d2e0e650508db2aa9a4a0bed1
Author: Fei Deng <du...@gmail.com>
AuthorDate: Tue Jan 11 10:09:35 2022 -0600

    add --with-mimalloc option (#8233)
    
    (cherry picked from commit bda64e5491baf96f7583ba42684a936659e6ffb8)
---
 build/jemalloc.m4                                  |  3 +
 build/mimalloc.m4                                  | 95 ++++++++++++++++++++++
 build/tcmalloc.m4                                  |  3 +
 configure.ac                                       |  5 +-
 .../testing/blackbox-testing.en.rst                |  1 +
 include/tscore/ink_config.h.in                     |  1 +
 include/tscore/ink_memory.h                        |  6 +-
 src/tscore/ink_memory.cc                           |  5 ++
 tests/README.md                                    |  1 +
 9 files changed, 115 insertions(+), 5 deletions(-)

diff --git a/build/jemalloc.m4 b/build/jemalloc.m4
index 22eccd7..2af6848 100644
--- a/build/jemalloc.m4
+++ b/build/jemalloc.m4
@@ -26,6 +26,9 @@ AC_ARG_WITH([jemalloc], [AS_HELP_STRING([--with-jemalloc=DIR],[use a specific je
     if test "x${has_tcmalloc}" = "xyes"; then
       AC_MSG_ERROR([Cannot compile with both jemalloc and tcmalloc])
     fi
+    if test "x${has_mimalloc}" = "xyes"; then
+      AC_MSG_ERROR([Cannot compile with both jemalloc and mimalloc])
+    fi
     has_jemalloc=yes
     jemalloc_base_dir="$withval"
     case "$withval" in
diff --git a/build/mimalloc.m4 b/build/mimalloc.m4
new file mode 100644
index 0000000..5e6c148
--- /dev/null
+++ b/build/mimalloc.m4
@@ -0,0 +1,95 @@
+dnl -------------------------------------------------------- -*- autoconf -*-
+dnl Licensed to the Apache Software Foundation (ASF) under one or more
+dnl contributor license agreements.  See the NOTICE file distributed with
+dnl this work for additional information regarding copyright ownership.
+dnl The ASF licenses this file to You under the Apache License, Version 2.0
+dnl (the "License"); you may not use this file except in compliance with
+dnl the License.  You may obtain a copy of the License at
+dnl
+dnl     http://www.apache.org/licenses/LICENSE-2.0
+dnl
+dnl Unless required by applicable law or agreed to in writing, software
+dnl distributed under the License is distributed on an "AS IS" BASIS,
+dnl WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+dnl See the License for the specific language governing permissions and
+dnl limitations under the License.
+
+dnl
+dnl mimalloc.m4: Trafficserver's mimalloc autoconf macros
+dnl
+
+AC_DEFUN([TS_CHECK_MIMALLOC], [
+has_mimalloc=no
+AC_ARG_WITH([mimalloc], [AS_HELP_STRING([--with-mimalloc=DIR],[use a specific mimalloc library])],
+[
+  if test "$withval" != "no"; then
+    if test "x${has_tcmalloc}" = "xyes"; then
+      AC_MSG_ERROR([Cannot compile with both mimalloc and tcmalloc])
+    fi
+    if test "x${has_jemalloc}" = "xyes"; then
+      AC_MSG_ERROR([Cannot compile with both mimalloc and jemalloc])
+    fi
+    has_mimalloc=yes
+    mimalloc_base_dir="$withval"
+    case "$withval" in
+      yes)
+        mimalloc_base_dir="/usr"
+        AC_MSG_NOTICE(checking for mimalloc includes and libs in standard directories)
+        ;;
+      *":"*)
+        mimalloc_include="`echo $withval | sed -e 's/:.*$//'`"
+        mimalloc_ldflags="`echo $withval | sed -e 's/^.*://'`"
+        AC_MSG_NOTICE(checking for mimalloc includes in $mimalloc_include and libs in $mimalloc_ldflags)
+        ;;
+      *)
+        mimalloc_include="$withval/include"
+        mimalloc_ldflags="$withval/lib"
+        mimalloc_ldflags64="$withval/lib64"
+        AC_MSG_NOTICE(checking for mimalloc includes in $mimalloc_include and libs in $mimalloc_ldflags or $mimalloc_ldflags64)
+        ;;
+    esac
+  fi
+])
+
+mimalloch=0
+if test "$has_mimalloc" != "no"; then
+  saved_ldflags=$LDFLAGS
+  saved_cppflags=$CPPFLAGS
+  mimalloc_has_headers=0
+  mimalloc_has_libs=0
+  AC_CHECK_FILE([mimalloc_ldflags], [], [mimalloc_ldflags=$mimalloc_ldflags64])
+  if test "$mimalloc_base_dir" != "/usr"; then
+    TS_ADDTO(CPPFLAGS, [-I${mimalloc_include}])
+    TS_ADDTO(LDFLAGS, [-L${mimalloc_ldflags}])
+    TS_ADDTO(LDFLAGS, [-Wl,--add-needed -L${mimalloc_ldflags} -Wl,-rpath,${mimalloc_ldflags} -Wl,--no-as-needed])
+    TS_ADDTO_RPATH(${mimalloc_ldflags})
+  fi
+  AC_SEARCH_LIBS([mi_malloc], [mimalloc], [mimalloc_has_libs=1])
+  if test "$mimalloc_has_libs" != "0"; then
+    AC_CHECK_HEADERS(mimalloc.h, [mimalloc_has_headers=1])
+  fi
+  if test "$mimalloc_has_headers" != "0"; then
+    AC_RUN_IFELSE([
+      AC_LANG_PROGRAM(
+        [#include <mimalloc.h>],
+        [
+          #ifndef MI_MALLOC_VERSION
+          exit(1);
+          #endif
+
+          #if (MI_MALLOC_VERSION == 0)
+          exit(1);
+          #endif
+        ]
+      )],
+      [mimalloch=1],
+      [AC_MSG_ERROR(mimalloc has bogus version)]
+    )
+  else
+    AC_MSG_WARN([mimalloc not found])
+    CPPFLAGS=$saved_cppflags
+    LDFLAGS=$saved_ldflags
+  fi
+fi
+AC_SUBST(mimalloch)
+])
diff --git a/build/tcmalloc.m4 b/build/tcmalloc.m4
index fe9d538..d8027ad 100644
--- a/build/tcmalloc.m4
+++ b/build/tcmalloc.m4
@@ -37,6 +37,9 @@ AC_ARG_WITH([tcmalloc], [AS_HELP_STRING([--with-tcmalloc=DIR],[use the tcmalloc
     if test "x${has_jemalloc}" = "xyes"; then
       AC_MSG_ERROR([Cannot compile with both tcmalloc and jemalloc])
     fi
+    if test "x${has_mimalloc}" = "xyes"; then
+      AC_MSG_ERROR([Cannot compile with both tcmalloc and mimalloc])
+    fi
     tcmalloc_have_libs=0
     if test "x$withval" != "xyes" && test "x$withval" != "x"; then
       tcmalloc_ldflags="$withval/lib"
diff --git a/configure.ac b/configure.ac
index 7e5713f..1ba4ff1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1763,9 +1763,10 @@ AC_SUBST(use_hrw_maxminddb)
 AM_CONDITIONAL([BUILD_HEALTHCHECK_PLUGIN], [ test "$ac_cv_func_inotify_init" = "yes" ])
 
 #
-# Check for tcmalloc and jemalloc
-TS_CHECK_JEMALLOC
+# Check for tcmalloc, jemalloc and mimalloc
 TS_CHECK_TCMALLOC
+TS_CHECK_JEMALLOC
+TS_CHECK_MIMALLOC
 
 #
 # Check for libreadline/libedit
diff --git a/doc/developer-guide/testing/blackbox-testing.en.rst b/doc/developer-guide/testing/blackbox-testing.en.rst
index 3b4a5c2..25af57a 100644
--- a/doc/developer-guide/testing/blackbox-testing.en.rst
+++ b/doc/developer-guide/testing/blackbox-testing.en.rst
@@ -239,6 +239,7 @@ Condition Testing
         - TS_HAS_PIPE_BUFFER_SIZE_CONFIG
         - TS_HAS_JEMALLOC
         - TS_HAS_TCMALLOC
+        - TS_HAS_MIMALLOC
         - TS_HAS_IN6_IS_ADDR_UNSPECIFIED
         - TS_HAS_BACKTRACE
         - TS_HAS_PROFILER
diff --git a/include/tscore/ink_config.h.in b/include/tscore/ink_config.h.in
index d66e509..5b297ef 100644
--- a/include/tscore/ink_config.h.in
+++ b/include/tscore/ink_config.h.in
@@ -52,6 +52,7 @@
 /* Libraries */
 #define TS_HAS_JEMALLOC @jemalloch@
 #define TS_HAS_TCMALLOC @has_tcmalloc@
+#define TS_HAS_MIMALLOC @mimalloch@
 
 /* Features */
 #define TS_HAS_IN6_IS_ADDR_UNSPECIFIED @has_in6_is_addr_unspecified@
diff --git a/include/tscore/ink_memory.h b/include/tscore/ink_memory.h
index e1fc3e2..ae5dd33 100644
--- a/include/tscore/ink_memory.h
+++ b/include/tscore/ink_memory.h
@@ -50,10 +50,10 @@
 
 #if TS_HAS_JEMALLOC
 #include <jemalloc/jemalloc.h>
-#else
-#if HAVE_MALLOC_H
+#elif TS_HAS_MIMALLOC
+#include <mimalloc.h>
+#elif HAVE_MALLOC_H
 #include <malloc.h>
-#endif // ! HAVE_MALLOC_H
 #endif // ! TS_HAS_JEMALLOC
 
 #ifndef MADV_NORMAL
diff --git a/src/tscore/ink_memory.cc b/src/tscore/ink_memory.cc
index f8b9b86..1d8dbaf 100644
--- a/src/tscore/ink_memory.cc
+++ b/src/tscore/ink_memory.cc
@@ -27,6 +27,11 @@
 #include "tscore/Diags.h"
 #include "tscore/ink_atomic.h"
 
+#if TS_HAS_MIMALLOC
+#include <mimalloc-new-delete.h>
+#include <mimalloc-override.h>
+#endif
+
 #if !defined(kfreebsd) && defined(freebsd)
 #include <malloc_np.h> // for malloc_usable_size
 #endif
diff --git a/tests/README.md b/tests/README.md
index 898c384..e28a6b9 100644
--- a/tests/README.md
+++ b/tests/README.md
@@ -298,6 +298,7 @@ ts.Disk.remap_config.AddLine(
  * TS_HAS_PIPE_BUFFER_SIZE_CONFIG
  * TS_HAS_JEMALLOC
  * TS_HAS_TCMALLOC
+ * TS_HAS_MIMALLOC
  * TS_HAS_IN6_IS_ADDR_UNSPECIFIED
  * TS_HAS_BACKTRACE
  * TS_HAS_PROFILER