2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 18:19:42 +00:00

Use system allocator when jemalloc is unavailable

This commit adds support for systems where the jemalloc library is not
available as a package, here's the quick summary:

  * On Linux - the jemalloc is usually available as a package, if
    configured --without-jemalloc, the shim would be used around
    malloc(), free(), realloc() and malloc_usable_size()

  * On macOS - the jemalloc is available from homebrew or macports, if
    configured --without-jemalloc, the shim would be used around
    malloc(), free(), realloc() and malloc_size()

  * On FreeBSD - the jemalloc is *the* system allocator, we just need
    to check for <malloc_np.h> header to get access to non-standard API

  * On NetBSD - the jemalloc is *the* system allocator, we just need to
    check for <jemalloc/jemalloc.h> header to get access to non-standard
    API

  * On a system hostile to users and developers (read OpenBSD) - the
    jemalloc API is emulated by using ((size_t *)ptr)[-1] field to hold
    the size information.  The OpenBSD developers care only for
    themselves, so why should we care about speed on OpenBSD?
This commit is contained in:
Ondřej Surý 2021-05-25 12:46:00 +02:00
parent 68a28cbc0a
commit e20cc41e56
6 changed files with 214 additions and 8 deletions

View File

@ -1377,15 +1377,26 @@ AC_SUBST([CMOCKA_LIBS])
AM_CONDITIONAL([HAVE_CMOCKA], [test "$with_cmocka" = "yes"]) AM_CONDITIONAL([HAVE_CMOCKA], [test "$with_cmocka" = "yes"])
# #
# Compile with jemalloc (either provided as package or wired in the system on FreeBSD and NetBSD)
# #
# # [pairwise: --with-jemalloc=detect, --with-jemalloc=yes, --without-jemalloc]
AC_MSG_CHECKING([for jemalloc]) AC_ARG_WITH([jemalloc],
PKG_CHECK_MODULES([JEMALLOC], [jemalloc >= 5], [] [AS_HELP_STRING([--with-jemalloc=detect],[enable jemalloc memory allocator (default is detect)])],
[AC_MSG_WARN([Using jemalloc 5 is recommended]) [],[with_jemalloc=detect])
PKG_CHECK_MODULES([JEMALLOC], [jemalloc], [],
[AC_MSG_ERROR([jemalloc not found])])]) AS_CASE([$with_jemalloc],
AC_SUBST([JEMALLOC_CFLAGS]) [no],[],
AC_SUBST([JEMALLOC_LIBS]) [yes],[AX_CHECK_JEMALLOC(
[AC_DEFINE([HAVE_JEMALLOC], [1], [Define to 1 if jemalloc is available])],
[AC_MSG_ERROR([jemalloc not found])])],
[AX_CHECK_JEMALLOC(
[AC_DEFINE([HAVE_JEMALLOC], [1], [Define to 1 if jemalloc is available])
with_jemalloc=yes],
[AC_MSG_WARN([jemalloc not found; performance will be reduced])
with_jemalloc=no])])
AS_IF([test "$with_jemalloc" = "no"],
[AC_CHECK_FUNCS([malloc_size malloc_usable_size])])
# #
# was --with-tuning specified? # was --with-tuning specified?
@ -1698,6 +1709,9 @@ report() {
echo "Configuration summary:" echo "Configuration summary:"
echo "-------------------------------------------------------------------------------" echo "-------------------------------------------------------------------------------"
echo "Optional features enabled:" echo "Optional features enabled:"
if test "yes" = "$with_jemalloc"; then
echo " Memory allocator: jemalloc"
fi
if test "yes" = "$enable_full_report" -o "standard" = "$with_locktype"; then if test "yes" = "$enable_full_report" -o "standard" = "$with_locktype"; then
echo " Mutex lock type: $with_locktype" echo " Mutex lock type: $with_locktype"
fi fi
@ -1754,6 +1768,14 @@ report() {
echo "-------------------------------------------------------------------------------" echo "-------------------------------------------------------------------------------"
echo "Features disabled or unavailable on this platform:" echo "Features disabled or unavailable on this platform:"
if test "no" = "$with_jemalloc"; then
echo " Memory allocator: system"
echo " WARNING: This is not a recommended configuration"
echo " WARNING: Using system memory allocator causes"
echo " WARNING: reduced performance and increased memory"
echo " WARNING: fragmentation. Installing jemalloc >= 4.0.0"
echo " WARNING: memory allocator is strongly recommended."
fi
test "small" = "$with_tuning" || echo " Small-system tuning (--with-tuning)" test "small" = "$with_tuning" || echo " Small-system tuning (--with-tuning)"
test "no" = "$enable_dnstap" && \ test "no" = "$enable_dnstap" && \

View File

@ -167,6 +167,7 @@ libisc_la_SOURCES = \
httpd.c \ httpd.c \
interfaceiter.c \ interfaceiter.c \
iterated_hash.c \ iterated_hash.c \
jemalloc_shim.h \
lex.c \ lex.c \
lib.c \ lib.c \
log.c \ log.c \

118
lib/isc/jemalloc_shim.h Normal file
View File

@ -0,0 +1,118 @@
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
#pragma once
#if !defined(HAVE_JEMALLOC)
#include <isc/util.h>
const char *malloc_conf = NULL;
#if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
#include <stdlib.h>
static inline void *
mallocx(size_t size, int flags) {
UNUSED(flags);
void *__ptr = malloc(size);
REQUIRE(__ptr != NULL);
return (__ptr);
}
static inline void
sdallocx(void *ptr, size_t size, int flags) {
UNUSED(size);
UNUSED(flags);
free(ptr);
}
static inline void *
rallocx(void *ptr, size_t size, int flags) {
UNUSED(flags);
REQUIRE(size != 0);
void *__ptr = realloc(ptr, size);
REQUIRE(__ptr != NULL);
return (__ptr);
}
#ifdef HAVE_MALLOC_SIZE
#include <malloc/malloc.h>
static inline size_t
sallocx(void *ptr, int flags) {
UNUSED(flags);
return (malloc_size(ptr));
}
#elif HAVE_MALLOC_USABLE_SIZE
#include <malloc.h>
static inline size_t
sallocx(void *ptr, int flags) {
UNUSED(flags);
return (malloc_usable_size(ptr));
}
#endif /* HAVE_MALLOC_SIZE */
#else /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
#include <stdlib.h>
static inline void *
mallocx(size_t size, int flags) {
UNUSED(flags);
size_t *__ptr = malloc(size + sizeof(size_t));
REQUIRE(__ptr != NULL);
__ptr[0] = size;
return (&__ptr[1]);
}
static inline void
sdallocx(void *ptr, size_t size, int flags) {
UNUSED(size);
UNUSED(flags);
free(&((size_t *)ptr)[-1]);
}
static inline size_t
sallocx(void *ptr, int flags) {
UNUSED(flags);
return (((size_t *)ptr)[-1]);
}
static inline void *
rallocx(void *ptr, size_t size, int flags) {
UNUSED(flags);
size_t *__ptr = realloc(&((size_t *)ptr)[-1], size);
REQUIRE(__ptr != NULL);
__ptr[0] = size;
return (&__ptr[1]);
}
#endif /* defined(HAVE_MALLOC_SIZE) || defined (HAVE_MALLOC_USABLE_SIZE) */
#endif /* !defined(HAVE_JEMALLOC) */

View File

@ -42,8 +42,19 @@
#include <json_object.h> #include <json_object.h>
#endif /* HAVE_JSON_C */ #endif /* HAVE_JSON_C */
#if defined(HAVE_MALLOC_NP_H)
#include <malloc_np.h>
#elif defined(HAVE_JEMALLOC)
#include <jemalloc/jemalloc.h> #include <jemalloc/jemalloc.h>
#if JEMALLOC_VERSION_MAJOR < 4
#define sdallocx(ptr, size, flags) dallocx(ptr, flags)
#endif /* JEMALLOC_VERSION_MAJOR < 4 */
#else
#include "jemalloc_shim.h"
#endif
#include "mem_p.h" #include "mem_p.h"
#define MCTXLOCK(m) LOCK(&m->lock) #define MCTXLOCK(m) LOCK(&m->lock)

53
m4/ax_jemalloc.m4 Normal file
View File

@ -0,0 +1,53 @@
# ===========================================================================
# https://gitlab.isc.org/isc-projects/autoconf-archive/ax_jemalloc.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_CHECK_JEMALLOC([, ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
#
# DESCRIPTION
#
# Test for the jemalloc library in a path
#
# LICENSE
#
# Copyright (c) 2021 Internet Systems Consortium
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 1
#
AC_DEFUN([AX_CHECK_JEMALLOC], [
found=false
PKG_CHECK_MODULES(
[JEMALLOC], [jemalloc],
[
found=true
], [
AC_CHECK_HEADERS([malloc_np.h jemalloc/jemalloc.h],
[
save_LIBS="$LIBS"
save_LDFLAGS="$LDFLAGS"
save_CPPFLAGS="$CPPFLAGS"
AC_SEARCH_LIBS([mallocx], [jemalloc],
[
found=true
AS_IF([test "$ac_cv_search_mallocx" != "none required"],
[JEMALLOC_LIBS="$ac_cv_search_mallocx"])
])
CPPFLAGS="$save_CPPFLAGS"
LDFLAGS="$save_LDFLAGS"
LIBS="$save_LIBS"
])
])
AS_IF([$found], [$1], [$2])
AC_SUBST([JEMALLOC_CFLAGS])
AC_SUBST([JEMALLOC_LIBS])
])

View File

@ -1787,6 +1787,7 @@
./lib/isc/include/pkcs11/pkcs11.h X 2019,2020,2021 ./lib/isc/include/pkcs11/pkcs11.h X 2019,2020,2021
./lib/isc/interfaceiter.c C 1999,2000,2001,2002,2003,2004,2005,2007,2008,2014,2016,2017,2018,2019,2020,2021 ./lib/isc/interfaceiter.c C 1999,2000,2001,2002,2003,2004,2005,2007,2008,2014,2016,2017,2018,2019,2020,2021
./lib/isc/iterated_hash.c C 2006,2008,2009,2016,2018,2019,2020,2021 ./lib/isc/iterated_hash.c C 2006,2008,2009,2016,2018,2019,2020,2021
./lib/isc/jemalloc_shim.h C 2021
./lib/isc/lex.c C 1998,1999,2000,2001,2002,2003,2004,2005,2007,2013,2014,2015,2016,2017,2018,2019,2020,2021 ./lib/isc/lex.c C 1998,1999,2000,2001,2002,2003,2004,2005,2007,2013,2014,2015,2016,2017,2018,2019,2020,2021
./lib/isc/lib.c C 1999,2000,2001,2004,2005,2007,2009,2013,2014,2015,2016,2018,2019,2020,2021 ./lib/isc/lib.c C 1999,2000,2001,2004,2005,2007,2009,2013,2014,2015,2016,2018,2019,2020,2021
./lib/isc/log.c C 1999,2000,2001,2002,2003,2004,2005,2006,2007,2009,2011,2012,2013,2014,2016,2017,2018,2019,2020,2021 ./lib/isc/log.c C 1999,2000,2001,2002,2003,2004,2005,2006,2007,2009,2011,2012,2013,2014,2016,2017,2018,2019,2020,2021