Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for jemalloc #6

Open
wants to merge 6 commits into
base: 8.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/acconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <malloc.h>
#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 <malloc.h>
# 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 <stddef.h>
# include <stddef.h>

/* dlmalloc definition of struct mallinfo. */
struct mallinfo {
Expand All @@ -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 */
6 changes: 3 additions & 3 deletions src/builtin.cmod
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
}

Expand Down
21 changes: 18 additions & 3 deletions src/builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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. */
Expand Down Expand Up @@ -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); \
Expand Down
16 changes: 14 additions & 2 deletions src/configure.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.]),
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,14 @@ void *alloca();
#include <limits.h>
#include <float.h>

#ifdef HAVE_MALLOC_H
#if defined(USE_JEMALLOC) && defined(HAVE_JEMALLOC_JEMALLOC_H)
#include <jemalloc/jemalloc.h>
#elif defined(HAVE_MALLOC_H)
#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
/* FreeBSD and OpenBSD has <malloc.h>, but it just contains a warning... */
#include <malloc.h>
#endif /* !__FreeBSD__ && !__OpenBSD */
#endif
#endif /* if defined(USE_JEMALLOC) */

#ifdef HAVE_UNISTD_H
#include <unistd.h>
Expand Down
8 changes: 4 additions & 4 deletions src/modules/HTTPLoop/requestobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
{
Expand Down