From df681ce745bd2f982400da5520d7249bbb98bb0c Mon Sep 17 00:00:00 2001 From: Tomasz Jamroszczak Date: Mon, 5 Feb 2018 17:03:54 +0100 Subject: [PATCH 1/6] jemalloc: Make code compliant with It's not needed to use C99 compilation but jemalloc #includes . --- src/builtin.cmod | 6 +++--- src/modules/HTTPLoop/requestobject.c | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/builtin.cmod b/src/builtin.cmod index f4e4e2f1a8..59f077b2a9 100644 --- a/src/builtin.cmod +++ b/src/builtin.cmod @@ -5962,15 +5962,15 @@ static void get_val_module(void) /* Always do the lookup in the Val module dynamically to allow the * values to be replaced. */ #define GET_VAL(NAME) \ - PMOD_EXPORT struct object *PIKE_CONCAT (get_val_, NAME) (void) \ + PMOD_EXPORT struct object *get_val_ ## NAME (void) \ { \ struct svalue index, res; \ if (!val_module) get_val_module(); \ SET_SVAL(index, T_STRING, 0, string, NULL); \ - MAKE_CONST_STRING (index.u.string, TOSTR (NAME)); \ + MAKE_CONST_STRING (index.u.string, #NAME); \ object_index_no_free (&res, val_module, 0, &index); \ if (TYPEOF(res) != T_OBJECT) \ - Pike_error ("\"Val." TOSTR (NAME) "\" didn't resolve to an object.\n"); \ + Pike_error ("\"Val." #NAME "\" didn't resolve to an object.\n"); \ return res.u.object; \ } diff --git a/src/modules/HTTPLoop/requestobject.c b/src/modules/HTTPLoop/requestobject.c index fc22c23590..dbbc7b3b24 100644 --- a/src/modules/HTTPLoop/requestobject.c +++ b/src/modules/HTTPLoop/requestobject.c @@ -654,8 +654,8 @@ static void actually_send(struct send_args *a) #if defined(TCP_CORK) && defined(SOL_TCP) DWERROR("cork... \n"); { - int true=1; - fd_setsockopt( a->to->fd, SOL_TCP, TCP_CORK, &true, sizeof(true) ); + int true_val=1; + fd_setsockopt( a->to->fd, SOL_TCP, TCP_CORK, &true_val, sizeof(true_val) ); } #endif fail = WRITE(a->to->fd, (char *)data, data_len); @@ -745,8 +745,8 @@ static void actually_send(struct send_args *a) DWERROR("all written.. \n"); #if defined(TCP_CORK) && defined(SOL_TCP) { - int false = 0; - fd_setsockopt( a->to->fd, SOL_TCP, TCP_CORK, &false, sizeof(false) ); + int false_val = 0; + fd_setsockopt( a->to->fd, SOL_TCP, TCP_CORK, &false_val, sizeof(false_val) ); } #endif { From 118576b955f05b78eab8db46839a0d6b2cf58272 Mon Sep 17 00:00:00 2001 From: Tomasz Jamroszczak Date: Mon, 5 Feb 2018 17:09:46 +0100 Subject: [PATCH 2/6] jemalloc: Handle jemalloc in builtin_functions.c builtin_functions.c uses non-standard dlmallinfo() or mallinfo(). These utility functions are not implemented by jemalloc. Instead it provides malloc_stats_print() and mallctl(). --- src/builtin_functions.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/builtin_functions.c b/src/builtin_functions.c index 9b923a7fd6..341635964e 100644 --- a/src/builtin_functions.c +++ b/src/builtin_functions.c @@ -7538,7 +7538,9 @@ PMOD_EXPORT void f__memory_usage(INT32 args) { size_t num,size; struct svalue *ss; -#ifdef USE_DL_MALLOC +#ifdef USE_JEMALLOC + /* No mallinfo. */ +#elif USE_DL_MALLOC struct mallinfo mi = dlmallinfo(); #elif HAVE_MALLINFO struct mallinfo mi = mallinfo(); @@ -7551,7 +7553,20 @@ PMOD_EXPORT void f__memory_usage(INT32 args) * statistics from our bundled Doug Lea malloc, and not the * underlying system malloc. Ideally we should include both. */ -#if defined(HAVE_MALLINFO) || defined(USE_DL_MALLOC) +#if defined(USE_JEMALLOC) + size_t sz, allocated, active, resident, mapped; + + sz = sizeof(size_t); + mallctl("stats.allocated", (void *)&allocated, &sz, NULL, 0); + mallctl("stats.active", (void *)&active, &sz, NULL, 0); + mallctl("stats.resident", (void *)&resident, &sz, NULL, 0); + mallctl("stats.mapped", (void *)&mapped, &sz, NULL, 0); + + push_text("allocated");push_ulongest(allocated); + push_text("active");push_ulongest(active); + push_text("resident");push_ulongest(resident); + push_text("mapped");push_ulongest(mapped); +#elif defined(HAVE_MALLINFO) || defined(USE_DL_MALLOC) push_static_text("num_malloc_blocks"); push_ulongest(1 + mi.hblks); /* 1 for the arena. */ @@ -7617,7 +7632,7 @@ PMOD_EXPORT void f__memory_usage(INT32 args) count_string_types(); -#endif +#endif /* if defined(USE_JEMALLOC) */ #define COUNT(TYPE) do { \ PIKE_CONCAT3(count_memory_in_, TYPE, s)(&num, &size); \ From ae8fa3e0a924069107adcef4a5f155cb1ef0ed07 Mon Sep 17 00:00:00 2001 From: Tomasz Jamroszczak Date: Mon, 5 Feb 2018 17:12:36 +0100 Subject: [PATCH 3/6] jemalloc: Handle jemalloc in global.h Just add conditional include. Depends on configure.in. --- src/global.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/global.h b/src/global.h index 3558ef5e3e..7aefaa3926 100644 --- a/src/global.h +++ b/src/global.h @@ -325,12 +325,14 @@ void *alloca(); #include #include -#ifdef HAVE_MALLOC_H +#if defined(USE_JEMALLOC) +#include +#elif defined(HAVE_MALLOC_H) #if !defined(__FreeBSD__) && !defined(__OpenBSD__) /* FreeBSD and OpenBSD has , but it just contains a warning... */ #include #endif /* !__FreeBSD__ && !__OpenBSD */ -#endif +#endif /* if defined(USE_JEMALLOC) */ #ifdef HAVE_UNISTD_H #include From d3639fd455f9b32c404c55f9aa9124281d5b359f Mon Sep 17 00:00:00 2001 From: Tomasz Jamroszczak Date: Mon, 5 Feb 2018 17:16:40 +0100 Subject: [PATCH 4/6] jemalloc: Add jemalloc support to configure.in script When configure.in sees --enable-jemalloc it does following: 1. adds -ljemalloc to LIBS and 2. adds USE_JEMALLOC C preprocessor define. 3. adds HAVE_JEMALLOC_JEMALLOC_H C preprocessor definition if the library is present during autoconfigure. 4. Removes #include . --- src/configure.in | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/configure.in b/src/configure.in index bb198ac127..b5b0fcbb68 100644 --- a/src/configure.in +++ b/src/configure.in @@ -1026,6 +1026,16 @@ else enable_dlmalloc=no fi +AC_ARG_ENABLE(jemalloc, MY_DESCR([--enable-jemalloc], + [use jemalloc implementation instead of system malloc]), + [enable_jemalloc=yes], [enable_jemalloc=no]) +if test "x$enable_jemalloc" = xyes; then + AC_DEFINE(USE_JEMALLOC, [1], [Set to 1 if jemalloc should be used for memory allocation]) + LIBS="-ljemalloc ${LIBS}" +else + enable_jemalloc=no +fi + MY_AC_ARG_WITH(cleanup-on-exit, MY_DESCR([--with-cleanup-on-exit], [Do full cleanup at exit to detect leaks better.]), @@ -2818,7 +2828,8 @@ AC_CHECK_HEADERS(winsock2.h sys/rusage.h sys/time.h \ sys/id.h mach-o/dyld.h sys/ptrace.h \ thread.h dlfcn.h dld.h dl.h sys/times.h sched.h \ sys/procfs.h sys/socket.h sys/uio.h fcntl.h \ - malloc.h netinet/in.h sys/wait.h windows.h grp.h pwd.h \ + malloc.h jemalloc/jemalloc.h netinet/in.h sys/wait.h \ + windows.h grp.h pwd.h \ passwd.h group.h winsock.h sys/file.h poll.h \ sys/poll.h socket.h ieeefp.h fp_class.h floatingpoint.h \ sys/priocntl.h sys/sched.h winbase.h \ @@ -4396,7 +4407,7 @@ AC_MSG_RESULT($pike_cv_has_struct_sockaddr_in6) ############################################################################# -if test $ac_cv_header_malloc_h = yes; then +if test $ac_cv_header_malloc_h = yes && test "x$enable_jemalloc" = xno; then AC_MSG_CHECKING(struct mallinfo in malloc.h) AC_CACHE_VAL(pike_cv_struct_mallinfo, [ AC_TRY_LINK([ @@ -7826,6 +7837,7 @@ PAD_FEATURE([cdebug])$with_cdebug PAD_FEATURE([rtldebug])$with_rtldebug PAD_FEATURE([dmalloc])$with_dmalloc PAD_FEATURE([dlmalloc])$enable_dlmalloc +PAD_FEATURE([jemalloc])$enable_jemalloc PAD_FEATURE([byte code format])$byte_code_format PAD_FEATURE([module reloc])${with_relocatable_dumped_modules:-no} PAD_FEATURE([use machine code])$with_machine_code From 01f81f1ce6fe31eb136a33c51dca0f551be1eacb Mon Sep 17 00:00:00 2001 From: Tomasz Jamroszczak Date: Mon, 5 Feb 2018 17:23:14 +0100 Subject: [PATCH 5/6] jemalloc: Add jemalloc support to acconfig.h Don't do fancy plumbering with missing mallinfo if USE_JEMALLOC is defined. --- src/acconfig.h | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/acconfig.h b/src/acconfig.h index cf5bbdda44..cf6e0292cb 100644 --- a/src/acconfig.h +++ b/src/acconfig.h @@ -550,19 +550,18 @@ #define PIKE_OOB_WORKS -1 /* dlmalloc has mallinfo. */ -#if defined(USE_DL_MALLOC) && !defined(HAVE_MALLINFO) -#define HAVE_MALLINFO +#if !defined(USE_JEMALLOC) +# if defined(USE_DL_MALLOC) && !defined(HAVE_MALLINFO) +# define HAVE_MALLINFO -#if defined (HAVE_MALLOC_H) && defined (HAVE_STRUCT_MALLINFO) -#include -#else /* HAVE_MALLOC_H && HAVE_STRUCT_MALLINFO */ - -#ifndef MALLINFO_FIELD_TYPE -#define MALLINFO_FIELD_TYPE size_t -#endif /* MALLINFO_FIELD_TYPE */ +# if defined (HAVE_MALLOC_H) && defined (HAVE_STRUCT_MALLINFO) +# include +# elif !defined(MALLINFO_FIELD_TYPE) +# define MALLINFO_FIELD_TYPE size_t +# endif /* defined (HAVE_MALLOC_H) && defined (HAVE_STRUCT_MALLINFO) */ /* Needed for size_t. */ -#include +# include /* dlmalloc definition of struct mallinfo. */ struct mallinfo { @@ -578,8 +577,8 @@ struct mallinfo { MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */ }; -#endif /* HAVE_USR_INCLUDE_MALLOC_H */ +# endif /* defined(USE_DL_MALLOC) && !defined(HAVE_MALLINFO) */ -#endif +#endif /* !defined(USE_JEMALLOC) */ #endif /* MACHINE_H */ From 2ab58d6958a7c96d6bd291bfbf3cbff15a52f120 Mon Sep 17 00:00:00 2001 From: Tomasz Jamroszczak Date: Tue, 6 Feb 2018 15:44:11 +0100 Subject: [PATCH 6/6] fixup! jemalloc: Handle jemalloc in global.h --- src/global.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/global.h b/src/global.h index 7aefaa3926..86fe8039bb 100644 --- a/src/global.h +++ b/src/global.h @@ -325,7 +325,7 @@ void *alloca(); #include #include -#if defined(USE_JEMALLOC) +#if defined(USE_JEMALLOC) && defined(HAVE_JEMALLOC_JEMALLOC_H) #include #elif defined(HAVE_MALLOC_H) #if !defined(__FreeBSD__) && !defined(__OpenBSD__)