Skip to content

Commit

Permalink
opal/memory: Move Memory Allocation Hooks usage from openib
Browse files Browse the repository at this point in the history
These changes fix issue open-mpi#1336

opal/memory/linux component should be single place that opeartes with
Memory Allocation Hooks. It is safe because it is linked statically.
  • Loading branch information
igor-ivanov committed Feb 10, 2016
1 parent 8558def commit 9f2a9ed
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 136 deletions.
4 changes: 2 additions & 2 deletions ompi/runtime/ompi_mpi_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@
#endif
#include "ompi/runtime/ompi_cr.h"

#if defined(MEMORY_LINUX_PTMALLOC2) && MEMORY_LINUX_PTMALLOC2
#if MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT
#include "opal/mca/memory/linux/memory_linux.h"
/* So this sucks, but with OPAL in its own library that is brought in
implicity from libmpi, there are times when the malloc initialize
hook in the memory component doesn't work. So we have to do it
from here, since any MPI code is going to call MPI_Init... */
OPAL_DECLSPEC void (*__malloc_initialize_hook) (void) =
opal_memory_linux_malloc_init_hook;
#endif
#endif /* MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT */

/* This is required for the boundaries of the hash tables used to store
* the F90 types returned by the MPI_Type_create_f90_XXX functions.
Expand Down
5 changes: 0 additions & 5 deletions opal/mca/btl/openib/btl_openib.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,6 @@ struct mca_btl_openib_component_t {
#if BTL_OPENIB_FAILOVER_ENABLED
int verbose_failover;
#endif
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
int use_memalign;
size_t memalign_threshold;
void* (*previous_malloc_hook)(size_t __size, const void*);
#endif
#if OPAL_CUDA_SUPPORT
bool cuda_async_send;
bool cuda_async_recv;
Expand Down
71 changes: 0 additions & 71 deletions opal/mca/btl/openib/btl_openib_component.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,6 @@
#include <fcntl.h>
#include <stdlib.h>
#include <stddef.h>
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/*
* The include of malloc.h below breaks abstractions in OMPI (by
* directly including a header file from another component), but has
* been ruled "ok" because the openib component is only supported on
* Linux.
*
* The malloc hooks in newer glibc were deprecated, including stock
* malloc.h causes compilation warnings. Instead, we use the internal
* linux component malloc.h which does not cause these warnings.
* Internally, OMPI uses the built-in ptmalloc from the linux memory
* component anyway.
*/
#include "opal/mca/memory/linux/malloc.h"
#endif

#include "opal/mca/event/event.h"
#include "opal/align.h"
Expand Down Expand Up @@ -123,7 +108,6 @@ static void btl_openib_handle_incoming_completion(mca_btl_base_module_t* btl,
* Local variables
*/
static mca_btl_openib_device_t *receive_queues_device = NULL;
static bool malloc_hook_set = false;
static int num_devices_intentionally_ignored = 0;

mca_btl_openib_component_t mca_btl_openib_component = {
Expand All @@ -147,30 +131,6 @@ mca_btl_openib_component_t mca_btl_openib_component = {
}
};

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/* This is a memory allocator hook. The purpose of this is to make
* every malloc aligned since this speeds up IB HCA work.
* There two basic cases here:
*
* 1. Memory manager for Open MPI is enabled. Then memalign below will
* be overridden by __memalign_hook which is set to
* opal_memory_linux_memalign_hook. Thus, _malloc_hook is going to
* use opal_memory_linux_memalign_hook.
*
* 2. No memory manager support. The memalign below is just regular glibc
* memalign which will be called through __malloc_hook instead of malloc.
*/
static void *btl_openib_malloc_hook(size_t sz, const void* caller)
{
if (sz < mca_btl_openib_component.memalign_threshold &&
malloc_hook_set) {
return mca_btl_openib_component.previous_malloc_hook(sz, caller);
} else {
return memalign(mca_btl_openib_component.use_memalign, sz);
}
}
#endif

static int btl_openib_component_register(void)
{
int ret;
Expand Down Expand Up @@ -257,16 +217,6 @@ static int btl_openib_component_close(void)
free(mca_btl_openib_component.default_recv_qps);
}

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/* Must check to see whether the malloc hook was set before
assigning it back because ompi_info will call _register() and
then _close() (which won't set the hook) */
if (malloc_hook_set) {
__malloc_hook = mca_btl_openib_component.previous_malloc_hook;
malloc_hook_set = false;
}
#endif

/* close memory registration debugging output */
opal_output_close (mca_btl_openib_component.memory_registration_verbose);

Expand Down Expand Up @@ -2547,20 +2497,6 @@ btl_openib_component_init(int *num_btl_modules,
*num_btl_modules = 0;
num_devs = 0;

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/* If we got this far, then setup the memory alloc hook (because
we're most likely going to be using this component). The hook
is to be set up as early as possible in this function since we
want most of the allocated resources be aligned.*/
if (mca_btl_openib_component.use_memalign > 0 &&
(opal_mem_hooks_support_level() &
(OPAL_MEMORY_FREE_SUPPORT | OPAL_MEMORY_CHUNK_SUPPORT)) != 0) {
mca_btl_openib_component.previous_malloc_hook = __malloc_hook;
__malloc_hook = btl_openib_malloc_hook;
malloc_hook_set = true;
}
#endif

/* Per https://svn.open-mpi.org/trac/ompi/ticket/1305, check to
see if $sysfsdir/class/infiniband exists. If it does not,
assume that the RDMA hardware drivers are not loaded, and
Expand Down Expand Up @@ -2960,13 +2896,6 @@ btl_openib_component_init(int *num_btl_modules,

mca_btl_openib_component.ib_num_btls = 0;
btl_openib_modex_send();
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
/*Unset malloc hook since the component won't start*/
if (malloc_hook_set) {
__malloc_hook = mca_btl_openib_component.previous_malloc_hook;
malloc_hook_set = false;
}
#endif
if (NULL != btls) {
free(btls);
}
Expand Down
32 changes: 0 additions & 32 deletions opal/mca/btl/openib/btl_openib_mca.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,27 +703,6 @@ int btl_openib_register_mca_params(void)
0, &mca_btl_openib_component.gid_index,
REGINT_GE_ZERO));

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
CHECK(reg_int("memalign", NULL,
"[64 | 32 | 0] - Enable (64bit or 32bit)/Disable(0) memory"
"alignment for all malloc calls if btl openib is used.",
32, &mca_btl_openib_component.use_memalign,
REGINT_GE_ZERO));

mca_btl_openib_component.memalign_threshold =
mca_btl_openib_module.super.btl_eager_limit;
tmp = mca_base_component_var_register(&mca_btl_openib_component.super.btl_version,
"memalign_threshold",
"Allocating memory more than btl_openib_memalign_threshhold"
"bytes will automatically be algined to the value of btl_openib_memalign bytes."
"memalign_threshhold defaults to the same value as mca_btl_openib_eager_limit.",
MCA_BASE_VAR_TYPE_SIZE_T, NULL, 0, 0,
OPAL_INFO_LVL_9,
MCA_BASE_VAR_SCOPE_READONLY,
&mca_btl_openib_component.memalign_threshold);
if (0 > tmp) ret = tmp;
#endif

/* Register any MCA params for the connect pseudo-components */
if (OPAL_SUCCESS == ret) {
ret = opal_btl_openib_connect_base_register();
Expand Down Expand Up @@ -823,16 +802,5 @@ int btl_openib_verify_mca_params (void)
}
#endif

#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
if (mca_btl_openib_component.use_memalign != 32
&& mca_btl_openib_component.use_memalign != 64
&& mca_btl_openib_component.use_memalign != 0){
opal_show_help("help-mpi-btl-openib.txt", "invalid mca param value",
true, "Wrong btl_openib_memalign parameter value. Allowed values: 64, 32, 0.",
"btl_openib_memalign is reset to 32");
mca_btl_openib_component.use_memalign = 32;
}
#endif

return OPAL_SUCCESS;
}
22 changes: 0 additions & 22 deletions opal/mca/btl/openib/configure.m4
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,6 @@ AC_DEFUN([MCA_opal_btl_openib_CONFIG],[
[enable openib BTL failover])
AM_CONDITIONAL([MCA_btl_openib_enable_failover], [test "x$btl_openib_failover_enabled" = "x1"])

# Check for __malloc_hook availability
AC_ARG_ENABLE(btl-openib-malloc-alignment,
AC_HELP_STRING([--enable-btl-openib-malloc-alignment], [Enable support for allocated memory alignment. Default: enabled if supported, disabled otherwise.]))

btl_openib_malloc_hooks_enabled=0
AS_IF([test "$enable_btl_openib_malloc_alignment" != "no"],
[AC_CHECK_HEADER([malloc.h],
[AC_CHECK_FUNC([__malloc_hook],
[AC_CHECK_FUNC([__realloc_hook],
[AC_CHECK_FUNC([__free_hook],
[btl_openib_malloc_hooks_enabled=1])])])])])

AS_IF([test "$enable_btl_openib_malloc_alignment" = "yes" && test "$btl_openib_malloc_hooks_enabled" = "0"],
[AC_MSG_ERROR([openib malloc alignment is requested but __malloc_hook is not available])])
AC_MSG_CHECKING([whether the openib BTL will use malloc hooks])
AS_IF([test "$btl_openib_malloc_hooks_enabled" = "0"],
[AC_MSG_RESULT([no])],
[AC_MSG_RESULT([yes])])

AC_DEFINE_UNQUOTED(BTL_OPENIB_MALLOC_HOOKS_ENABLED, [$btl_openib_malloc_hooks_enabled],
[Whether the openib BTL malloc hooks are enabled])

# make sure that CUDA-aware checks have been done
AC_REQUIRE([OPAL_CHECK_CUDA])

Expand Down
36 changes: 36 additions & 0 deletions opal/mca/memory/linux/configure.m4
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,42 @@ AC_DEFUN([MCA_opal_memory_linux_CONFIG],[
[memory_linux_ptmalloc2_happy=no
memory_linux_ummu_happy=no])])


######################################################################
# if memory hook available
######################################################################
memory_hook_found=1
AS_IF([test "$memory_hook_found" -eq 1],
[memory_hook_found=0 AC_CHECK_HEADER([malloc.h],
[AC_CHECK_FUNC([__malloc_initialize_hook],
[AC_CHECK_FUNC([__malloc_hook],
[AC_CHECK_FUNC([__realloc_hook],
[AC_CHECK_FUNC([__free_hook],
[memory_hook_found=1])])])])])])
AC_MSG_CHECKING([whether the system can use malloc hooks])
AS_IF([test "$memory_hook_found" = "0"],
[AC_MSG_RESULT([no])],
[AC_MSG_RESULT([yes])])
AC_DEFINE_UNQUOTED([MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT], [$memory_hook_found],
[Whether the system has Memory Allocation Hooks])

AC_ARG_ENABLE(memory-linux-malloc-alignment,
AC_HELP_STRING([--enable-memory-linux-malloc-alignment], [Enable support for allocated memory alignment. Default: enabled if supported, disabled otherwise.]))

malloc_align_enabled=0
AS_IF([test "$enable_memory_linux_malloc_alignment" != "no"],
[malloc_align_enabled=$memory_hook_found])

AS_IF([test "$enable_memory_linux_malloc_alignment" = "yes" && test "$malloc_align_enabled" = "0"],
[AC_MSG_ERROR([memory linux malloc alignment is requested but __malloc_hook is not available])])
AC_MSG_CHECKING([whether the memory linux will use malloc alignment])
AS_IF([test "$malloc_align_enabled" = "0"],
[AC_MSG_RESULT([no])],
[AC_MSG_RESULT([yes])])

AC_DEFINE_UNQUOTED(MEMORY_LINUX_MALLOC_ALIGN_ENABLED, [$malloc_align_enabled],
[Whether the memory linux malloc alignment is enabled])

######################################################################
# ptmalloc2
######################################################################
Expand Down
7 changes: 7 additions & 0 deletions opal/mca/memory/linux/help-opal-memory-linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ alternate memory hook manager *may* be used instead (if available).
Local host: %s
UMMU device: %s
Error: %s (%d)
#
[invalid mca param value]
WARNING: An invalid MCA parameter value was found for memory/linux
component.

Problem: %s
Resolution: %s
6 changes: 5 additions & 1 deletion opal/mca/memory/linux/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "opal/mca/mca.h"
#include "opal/mca/memory/memory.h"
#include "opal/constants.h"
#include "opal/memoryhooks/memory.h"

#include "opal/mca/memory/linux/memory_linux.h"

Expand Down Expand Up @@ -734,7 +735,10 @@ static check_result_t check(const char *name)
}
}

/* OMPI's init function */

/* This function is called from MPI_Init in case system has Memory Allocation Hooks
* (see ompi/runtime/ompi_mpi_init.c)
*/
void opal_memory_linux_malloc_init_hook(void)
{
check_result_t r1, lp, lpp;
Expand Down
10 changes: 9 additions & 1 deletion opal/mca/memory/linux/memory_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ typedef struct opal_memory_linux_component_t {
int ummunotify_fd;
#endif

#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
int use_memalign;
size_t memalign_threshold;
#endif

#if MEMORY_LINUX_PTMALLOC2
/* Ptmalloc2-specific data. Note that these variables are all marked as volatile.
* This is needed because of what may be a buggy optimization in the GCC 4.9.2
Expand Down Expand Up @@ -64,13 +69,16 @@ int opal_memory_linux_ummunotify_close(void);
/* memory_linux_ptmalloc2.c */
int opal_memory_linux_ptmalloc2_open(void);
int opal_memory_linux_ptmalloc2_close(void);
OPAL_DECLSPEC void opal_memory_linux_malloc_init_hook(void);

/* memory_linux_munmap.c */
OPAL_DECLSPEC int opal_memory_linux_free_ptmalloc2_munmap(void *start, size_t length, int from_alloc);
OPAL_DECLSPEC int munmap(void* addr, size_t len);
#endif /* !MEMORY_LINUX_PTMALLOC2 */

#if MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT
OPAL_DECLSPEC void opal_memory_linux_malloc_init_hook(void);
#endif /* MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT */

END_C_DECLS

#endif
Loading

0 comments on commit 9f2a9ed

Please sign in to comment.