]> arthur.barton.de Git - netdata.git/commitdiff
allow linking with jemalloc or tcmalloc
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 1 Oct 2016 11:12:11 +0000 (14:12 +0300)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Sat, 1 Oct 2016 11:12:11 +0000 (14:12 +0300)
configure.ac
m4/jemalloc.m4 [new file with mode: 0644]
m4/tcmalloc.m4 [new file with mode: 0644]
src/common.h
src/web_server.c

index d32ef94de22ce5cd738f5e5fe9bde9c3c34749eb..1c0dfda1d1b2203a87e8c1c1ccd10edfe0354f4d 100644 (file)
@@ -84,13 +84,19 @@ LIBS="${PTHREAD_LIBS} ${LIBS}"
 CFLAGS="${CFLAGS} ${PTHREAD_CFLAGS}"
 CC="${PTHREAD_CC}"
 
+TS_CHECK_JEMALLOC
+if test "$has_jemalloc" = "0"; then
+       TS_CHECK_TCMALLOC
+fi
+if test "$has_jemalloc$has_tcmalloc" = "00"; then
+       AC_C_MALLOPT
+       AC_C_MALLINFO
+fi
 AC_TYPE_UINT8_T
 AC_TYPE_UINT16_T
 AC_TYPE_UINT32_T
 AC_C_INLINE
 AC_FUNC_STRERROR_R
-AC_C_MALLOPT
-AC_C_MALLINFO
 AC_C__GENERIC
 AC_C___ATOMIC
 AC_CHECK_SIZEOF([void *])
diff --git a/m4/jemalloc.m4 b/m4/jemalloc.m4
new file mode 100644 (file)
index 0000000..bb5dceb
--- /dev/null
@@ -0,0 +1,88 @@
+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 jemalloc.m4: Trafficserver's jemalloc autoconf macros
+dnl modified to skip other TS_ helpers
+dnl
+
+AC_DEFUN([TS_CHECK_JEMALLOC], [
+AC_ARG_WITH([jemalloc-prefix],
+  [AS_HELP_STRING([--with-jemalloc-prefix=PREFIX],[Specify the jemalloc prefix [default=""]])],
+  [
+  jemalloc_prefix="$withval"
+  ],[
+  if test "`uname -s`" = "Darwin"; then
+    jemalloc_prefix="je_"
+  else
+    jemalloc_prefix=""
+  fi
+  ]
+)
+AC_DEFINE_UNQUOTED([prefix_jemalloc], [${jemalloc_prefix}], [jemalloc prefix])
+
+enable_jemalloc=no
+AC_ARG_WITH([jemalloc], [AS_HELP_STRING([--with-jemalloc=DIR], [use a specific jemalloc library])],
+[
+  if test "$withval" != "no"; then
+    if test "x${enable_tcmalloc}" = "xyes"; then
+      AC_MSG_ERROR([Cannot compile with both jemalloc and tcmalloc])
+    fi
+    enable_jemalloc=yes
+    jemalloc_base_dir="$withval"
+    case "$withval" in
+      yes)
+        jemalloc_base_dir="/usr"
+        AC_MSG_CHECKING(checking for jemalloc includes standard directories)
+       ;;
+      *":"*)
+        jemalloc_include="`echo $withval |sed -e 's/:.*$//'`"
+        jemalloc_ldflags="`echo $withval |sed -e 's/^.*://'`"
+        AC_MSG_CHECKING(checking for jemalloc includes in $jemalloc_include libs in $jemalloc_ldflags)
+        ;;
+      *)
+        jemalloc_include="$withval/include"
+        jemalloc_ldflags="$withval/lib"
+        AC_MSG_CHECKING(checking for jemalloc includes in $withval)
+        ;;
+    esac
+  fi
+])
+
+has_jemalloc=0
+if test "$enable_jemalloc" != "no"; then
+  jemalloc_have_headers=0
+  jemalloc_have_libs=0
+  if test "$jemalloc_base_dir" != "/usr"; then
+    CFLAGS="${CFLAGS} -I${jemalloc_include}"
+    LDFLAGS="${LDFLAGS} -L${jemalloc_ldflags}"
+    LIBTOOL_LINK_FLAGS="${LIBTOOL_LINK_FLAGS} -R${jemalloc_ldflags}"
+  fi
+  func="${jemalloc_prefix}malloc_stats_print"
+  AC_CHECK_LIB(jemalloc, ${func}, [jemalloc_have_libs=1])
+  if test "$jemalloc_have_libs" != "0"; then
+    AC_CHECK_HEADERS([jemalloc/jemalloc.h], [jemalloc_have_headers=1])
+  fi
+  if test "$jemalloc_have_headers" != "0"; then
+    has_jemalloc=1
+    LIBS="${LIBS} -ljemalloc"
+    AC_DEFINE(has_jemalloc, [1], [Link/compile against jemalloc])
+  else
+    AC_MSG_ERROR([Couldn't find a jemalloc installation])
+  fi
+fi
+AC_SUBST(has_jemalloc)
+])
diff --git a/m4/tcmalloc.m4 b/m4/tcmalloc.m4
new file mode 100644 (file)
index 0000000..e84697d
--- /dev/null
@@ -0,0 +1,58 @@
+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 tcmalloc.m4: Trafficserver's tcmalloc autoconf macros
+dnl modified to skip other TS_ helpers
+dnl
+
+dnl This is kinda fugly, but need a way to both specify a directory and which
+dnl of the many tcmalloc libraries to use ...
+AC_DEFUN([TS_CHECK_TCMALLOC], [
+AC_ARG_WITH([tcmalloc-lib],
+  [AS_HELP_STRING([--with-tcmalloc-lib],[specify the tcmalloc library to use [default=tcmalloc]])],
+  [
+  with_tcmalloc_lib="$withval"
+  ],[
+  with_tcmalloc_lib="tcmalloc"
+  ]
+)
+
+has_tcmalloc=0
+AC_ARG_WITH([tcmalloc], [AS_HELP_STRING([--with-tcmalloc=DIR], [use the tcmalloc library])],
+[
+  if test "$withval" != "no"; then
+    if test "x${enable_jemalloc}" = "xyes"; then
+      AC_MSG_ERROR([Cannot compile with both tcmalloc and jemalloc])
+    fi
+    tcmalloc_have_lib=0
+    if test "x$withval" != "xyes" && test "x$withval" != "x"; then
+      tcmalloc_ldflags="$withval/lib"
+      LDFLAGS="${LDFLAGS} -L${tcmalloc_ldflags}"
+      LIBTOOL_LINK_FLAGS="${LIBTOOL_LINK_FLAGS} -rpath ${tcmalloc_ldflags}"
+    fi
+    AC_CHECK_LIB(${with_tcmalloc_lib}, tc_cfree, [tcmalloc_have_lib=1])
+    if test "$tcmalloc_have_lib" != "0"; then
+      LIBS="${LIBS} -l${with_tcmalloc_lib}"
+      has_tcmalloc=1      
+      AC_DEFINE(has_tcmalloc, [1], [Link/compile against tcmalloc])
+    else
+      AC_MSG_ERROR([Couldn't find a tcmalloc installation])
+    fi
+  fi
+])
+AC_SUBST(has_tcmalloc)
+])
index 4b9d75b4eca339f15b3bfc31bf00fd856363819d..407dd4859dda2815d3fb3d81c3d87f9a9a257624 100644 (file)
@@ -5,6 +5,18 @@
 #include <config.h>
 #endif
 
+#ifdef has_jemalloc
+#include <jemalloc/jemalloc.h>
+#undef HAVE_C_MALLOPT
+#undef HAVE_C_MALLINFO
+#elif has_tcmalloc
+#include <google/tcmalloc.h>
+#undef HAVE_C_MALLOPT
+#undef HAVE_C_MALLINFO
+#else
+#include <malloc.h>
+#endif
+
 #include <pthread.h>
 #include <errno.h>
 
@@ -27,7 +39,7 @@
 #include <grp.h>
 #include <pwd.h>
 #include <locale.h>
-#include <malloc.h>
+
 #include <netdb.h>
 #include <poll.h>
 #include <signal.h>
index 8c1b53ecec8676ef15f999b6dbaef9356ff1d494..cbbe6bb4056ea1e68a95f39f03b9fe61989c153e 100644 (file)
@@ -37,6 +37,10 @@ static void log_allocations(void)
 #else /* ! HAVE_C_MALLINFO */
     ;
 #endif /* ! HAVE_C_MALLINFO */
+
+#ifdef has_jemalloc
+    malloc_stats_print(NULL, NULL, NULL);
+#endif
 }
 #endif /* NETDATA_INTERNAL_CHECKS */