Skip to content
This repository was archived by the owner on Sep 30, 2022. It is now read-only.

Commit 22f0a75

Browse files
committed
opal/memory: Move Memory Allocation Hooks usage from openib
These changes fix issue open-mpi/ompi#1336 - improve abstractions: opal/memory/linux component should be single place that opeartes with Memory Allocation Hooks. - avoid collisions in case dynamic component open/close: it is safe because it is linked statically. - does not change original behaivour.
1 parent b763687 commit 22f0a75

File tree

11 files changed

+227
-134
lines changed

11 files changed

+227
-134
lines changed

ompi/runtime/ompi_mpi_init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@
9292
#include "ompi/dpm/dpm.h"
9393
#include "ompi/mpiext/mpiext.h"
9494

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

105105
/* This is required for the boundaries of the hash tables used to store
106106
* the F90 types returned by the MPI_Type_create_f90_XXX functions.

opal/mca/btl/base/btl_base_select.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@
3535
#include "opal/mca/btl/btl.h"
3636
#include "opal/mca/btl/base/btl_base_error.h"
3737
#include "opal/mca/btl/base/base.h"
38+
#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
39+
/*
40+
* The include of malloc.h below breaks abstractions in OMPI (by
41+
* directly including a header file from another component), but has
42+
* been ruled "ok" because the openib component is only supported on
43+
* Linux.
44+
*
45+
* The malloc hooks in newer glibc were deprecated, including stock
46+
* malloc.h causes compilation warnings. Instead, we use the internal
47+
* linux component malloc.h which does not cause these warnings.
48+
* Internally, OMPI uses the built-in ptmalloc from the linux memory
49+
* component anyway.
50+
*/
51+
#include "opal/mca/memory/linux/memory_linux.h"
52+
#endif
3853

3954
OBJ_CLASS_INSTANCE( mca_btl_base_selected_module_t,
4055
opal_list_item_t,
@@ -107,6 +122,16 @@ int mca_btl_base_select(bool enable_progress_threads,
107122
"select: no init function; ignoring component %s",
108123
component->btl_version.mca_component_name);
109124
} else {
125+
#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
126+
if (strcmp(component->btl_version.mca_component_name, "openib") == 0) {
127+
/* If we got this far, then setup the memory alloc hook (because
128+
we're most likely going to be using this component). The hook
129+
is to be set up as early as possible in this function since we
130+
want most of the allocated resources be aligned.
131+
*/
132+
opal_memory_linux_malloc_set_alignment(32, 12288);
133+
}
134+
#endif /* MEMORY_LINUX_MALLOC_ALIGN_ENABLED */
110135
modules = component->btl_init(&num_btls, enable_progress_threads,
111136
enable_mpi_threads);
112137

opal/mca/btl/openib/btl_openib.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,6 @@ struct mca_btl_openib_component_t {
302302
#if BTL_OPENIB_FAILOVER_ENABLED
303303
int verbose_failover;
304304
#endif
305-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
306-
int use_memalign;
307-
size_t memalign_threshold;
308-
void* (*previous_malloc_hook)(size_t __size, const void*);
309-
#endif
310305
#if OPAL_CUDA_SUPPORT
311306
bool cuda_async_send;
312307
bool cuda_async_recv;

opal/mca/btl/openib/btl_openib_component.c

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,6 @@
4242
#include <fcntl.h>
4343
#include <stdlib.h>
4444
#include <stddef.h>
45-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
46-
/*
47-
* The include of malloc.h below breaks abstractions in OMPI (by
48-
* directly including a header file from another component), but has
49-
* been ruled "ok" because the openib component is only supported on
50-
* Linux.
51-
*
52-
* The malloc hooks in newer glibc were deprecated, including stock
53-
* malloc.h causes compilation warnings. Instead, we use the internal
54-
* linux component malloc.h which does not cause these warnings.
55-
* Internally, OMPI uses the built-in ptmalloc from the linux memory
56-
* component anyway.
57-
*/
58-
#include "opal/mca/memory/linux/malloc.h"
59-
#endif
6045

6146
#include "opal/mca/event/event.h"
6247
#include "opal/align.h"
@@ -123,7 +108,6 @@ static void btl_openib_handle_incoming_completion(mca_btl_base_module_t* btl,
123108
* Local variables
124109
*/
125110
static mca_btl_openib_device_t *receive_queues_device = NULL;
126-
static bool malloc_hook_set = false;
127111
static int num_devices_intentionally_ignored = 0;
128112

129113
mca_btl_openib_component_t mca_btl_openib_component = {
@@ -147,30 +131,6 @@ mca_btl_openib_component_t mca_btl_openib_component = {
147131
}
148132
};
149133

150-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
151-
/* This is a memory allocator hook. The purpose of this is to make
152-
* every malloc aligned since this speeds up IB HCA work.
153-
* There two basic cases here:
154-
*
155-
* 1. Memory manager for Open MPI is enabled. Then memalign below will
156-
* be overridden by __memalign_hook which is set to
157-
* opal_memory_linux_memalign_hook. Thus, _malloc_hook is going to
158-
* use opal_memory_linux_memalign_hook.
159-
*
160-
* 2. No memory manager support. The memalign below is just regular glibc
161-
* memalign which will be called through __malloc_hook instead of malloc.
162-
*/
163-
static void *btl_openib_malloc_hook(size_t sz, const void* caller)
164-
{
165-
if (sz < mca_btl_openib_component.memalign_threshold &&
166-
malloc_hook_set) {
167-
return mca_btl_openib_component.previous_malloc_hook(sz, caller);
168-
} else {
169-
return memalign(mca_btl_openib_component.use_memalign, sz);
170-
}
171-
}
172-
#endif
173-
174134
static int btl_openib_component_register(void)
175135
{
176136
int ret;
@@ -257,16 +217,6 @@ static int btl_openib_component_close(void)
257217
free(mca_btl_openib_component.default_recv_qps);
258218
}
259219

260-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
261-
/* Must check to see whether the malloc hook was set before
262-
assigning it back because ompi_info will call _register() and
263-
then _close() (which won't set the hook) */
264-
if (malloc_hook_set) {
265-
__malloc_hook = mca_btl_openib_component.previous_malloc_hook;
266-
malloc_hook_set = false;
267-
}
268-
#endif
269-
270220
/* close memory registration debugging output */
271221
opal_output_close (mca_btl_openib_component.memory_registration_verbose);
272222

@@ -2538,20 +2488,6 @@ btl_openib_component_init(int *num_btl_modules,
25382488
*num_btl_modules = 0;
25392489
num_devs = 0;
25402490

2541-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
2542-
/* If we got this far, then setup the memory alloc hook (because
2543-
we're most likely going to be using this component). The hook
2544-
is to be set up as early as possible in this function since we
2545-
want most of the allocated resources be aligned.*/
2546-
if (mca_btl_openib_component.use_memalign > 0 &&
2547-
(opal_mem_hooks_support_level() &
2548-
(OPAL_MEMORY_FREE_SUPPORT | OPAL_MEMORY_CHUNK_SUPPORT)) != 0) {
2549-
mca_btl_openib_component.previous_malloc_hook = __malloc_hook;
2550-
__malloc_hook = btl_openib_malloc_hook;
2551-
malloc_hook_set = true;
2552-
}
2553-
#endif
2554-
25552491
/* Per https://svn.open-mpi.org/trac/ompi/ticket/1305, check to
25562492
see if $sysfsdir/class/infiniband exists. If it does not,
25572493
assume that the RDMA hardware drivers are not loaded, and
@@ -2951,13 +2887,6 @@ btl_openib_component_init(int *num_btl_modules,
29512887

29522888
mca_btl_openib_component.ib_num_btls = 0;
29532889
btl_openib_modex_send();
2954-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
2955-
/*Unset malloc hook since the component won't start*/
2956-
if (malloc_hook_set) {
2957-
__malloc_hook = mca_btl_openib_component.previous_malloc_hook;
2958-
malloc_hook_set = false;
2959-
}
2960-
#endif
29612890
if (NULL != btls) {
29622891
free(btls);
29632892
}

opal/mca/btl/openib/btl_openib_mca.c

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -703,26 +703,19 @@ int btl_openib_register_mca_params(void)
703703
0, &mca_btl_openib_component.gid_index,
704704
REGINT_GE_ZERO));
705705

706-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
707-
CHECK(reg_int("memalign", NULL,
708-
"[64 | 32 | 0] - Enable (64bit or 32bit)/Disable(0) memory"
709-
"alignment for all malloc calls if btl openib is used.",
710-
32, &mca_btl_openib_component.use_memalign,
711-
REGINT_GE_ZERO));
706+
#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
707+
tmp = mca_base_var_find ("opal", "memory", "linux", "memalign");
708+
if (0 <= tmp) {
709+
(void) mca_base_var_register_synonym(tmp, "opal", "btl", "openib", "memalign",
710+
MCA_BASE_VAR_SYN_FLAG_DEPRECATED);
711+
}
712712

713-
mca_btl_openib_component.memalign_threshold =
714-
mca_btl_openib_module.super.btl_eager_limit;
715-
tmp = mca_base_component_var_register(&mca_btl_openib_component.super.btl_version,
716-
"memalign_threshold",
717-
"Allocating memory more than btl_openib_memalign_threshhold"
718-
"bytes will automatically be algined to the value of btl_openib_memalign bytes."
719-
"memalign_threshhold defaults to the same value as mca_btl_openib_eager_limit.",
720-
MCA_BASE_VAR_TYPE_SIZE_T, NULL, 0, 0,
721-
OPAL_INFO_LVL_9,
722-
MCA_BASE_VAR_SCOPE_READONLY,
723-
&mca_btl_openib_component.memalign_threshold);
724-
if (0 > tmp) ret = tmp;
725-
#endif
713+
tmp = mca_base_var_find ("opal", "memory", "linux", "memalign_threshold");
714+
if (0 <= tmp) {
715+
(void) mca_base_var_register_synonym(tmp, "opal", "btl", "openib", "memalign_threshold",
716+
MCA_BASE_VAR_SYN_FLAG_DEPRECATED);
717+
}
718+
#endif /* MEMORY_LINUX_MALLOC_ALIGN_ENABLED */
726719

727720
/* Register any MCA params for the connect pseudo-components */
728721
if (OPAL_SUCCESS == ret) {
@@ -823,16 +816,5 @@ int btl_openib_verify_mca_params (void)
823816
}
824817
#endif
825818

826-
#if BTL_OPENIB_MALLOC_HOOKS_ENABLED
827-
if (mca_btl_openib_component.use_memalign != 32
828-
&& mca_btl_openib_component.use_memalign != 64
829-
&& mca_btl_openib_component.use_memalign != 0){
830-
opal_show_help("help-mpi-btl-openib.txt", "invalid mca param value",
831-
true, "Wrong btl_openib_memalign parameter value. Allowed values: 64, 32, 0.",
832-
"btl_openib_memalign is reset to 32");
833-
mca_btl_openib_component.use_memalign = 32;
834-
}
835-
#endif
836-
837819
return OPAL_SUCCESS;
838820
}

opal/mca/btl/openib/configure.m4

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,6 @@ AC_DEFUN([MCA_opal_btl_openib_CONFIG],[
120120
[enable openib BTL failover])
121121
AM_CONDITIONAL([MCA_btl_openib_enable_failover], [test "x$btl_openib_failover_enabled" = "x1"])
122122

123-
# Check for __malloc_hook availability
124-
AC_ARG_ENABLE(btl-openib-malloc-alignment,
125-
AC_HELP_STRING([--enable-btl-openib-malloc-alignment], [Enable support for allocated memory alignment. Default: enabled if supported, disabled otherwise.]))
126-
127-
btl_openib_malloc_hooks_enabled=0
128-
AS_IF([test "$enable_btl_openib_malloc_alignment" != "no"],
129-
[AC_CHECK_HEADER([malloc.h],
130-
[AC_CHECK_FUNC([__malloc_hook],
131-
[AC_CHECK_FUNC([__realloc_hook],
132-
[AC_CHECK_FUNC([__free_hook],
133-
[btl_openib_malloc_hooks_enabled=1])])])])])
134-
135-
AS_IF([test "$enable_btl_openib_malloc_alignment" = "yes" && test "$btl_openib_malloc_hooks_enabled" = "0"],
136-
[AC_MSG_ERROR([openib malloc alignment is requested but __malloc_hook is not available])])
137-
AC_MSG_CHECKING([whether the openib BTL will use malloc hooks])
138-
AS_IF([test "$btl_openib_malloc_hooks_enabled" = "0"],
139-
[AC_MSG_RESULT([no])],
140-
[AC_MSG_RESULT([yes])])
141-
142-
AC_DEFINE_UNQUOTED(BTL_OPENIB_MALLOC_HOOKS_ENABLED, [$btl_openib_malloc_hooks_enabled],
143-
[Whether the openib BTL malloc hooks are enabled])
144-
145123
# make sure that CUDA-aware checks have been done
146124
AC_REQUIRE([OPAL_CHECK_CUDA])
147125

opal/mca/memory/linux/configure.m4

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,42 @@ AC_DEFUN([MCA_opal_memory_linux_CONFIG],[
6363
[memory_linux_ptmalloc2_happy=no
6464
memory_linux_ummu_happy=no])])
6565

66+
67+
######################################################################
68+
# if memory hook available
69+
######################################################################
70+
memory_hook_found=1
71+
AS_IF([test "$memory_hook_found" -eq 1],
72+
[memory_hook_found=0 AC_CHECK_HEADER([malloc.h],
73+
[AC_CHECK_FUNC([__malloc_initialize_hook],
74+
[AC_CHECK_FUNC([__malloc_hook],
75+
[AC_CHECK_FUNC([__realloc_hook],
76+
[AC_CHECK_FUNC([__free_hook],
77+
[memory_hook_found=1])])])])])])
78+
AC_MSG_CHECKING([whether the system can use malloc hooks])
79+
AS_IF([test "$memory_hook_found" = "0"],
80+
[AC_MSG_RESULT([no])],
81+
[AC_MSG_RESULT([yes])])
82+
AC_DEFINE_UNQUOTED([MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT], [$memory_hook_found],
83+
[Whether the system has Memory Allocation Hooks])
84+
85+
AC_ARG_ENABLE(memory-linux-malloc-alignment,
86+
AC_HELP_STRING([--enable-memory-linux-malloc-alignment], [Enable support for allocated memory alignment. Default: enabled if supported, disabled otherwise.]))
87+
88+
malloc_align_enabled=0
89+
AS_IF([test "$enable_memory_linux_malloc_alignment" != "no"],
90+
[malloc_align_enabled=$memory_hook_found])
91+
92+
AS_IF([test "$enable_memory_linux_malloc_alignment" = "yes" && test "$malloc_align_enabled" = "0"],
93+
[AC_MSG_ERROR([memory linux malloc alignment is requested but __malloc_hook is not available])])
94+
AC_MSG_CHECKING([whether the memory linux will use malloc alignment])
95+
AS_IF([test "$malloc_align_enabled" = "0"],
96+
[AC_MSG_RESULT([no])],
97+
[AC_MSG_RESULT([yes])])
98+
99+
AC_DEFINE_UNQUOTED(MEMORY_LINUX_MALLOC_ALIGN_ENABLED, [$malloc_align_enabled],
100+
[Whether the memory linux malloc alignment is enabled])
101+
66102
######################################################################
67103
# ptmalloc2
68104
######################################################################

opal/mca/memory/linux/help-opal-memory-linux.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ alternate memory hook manager *may* be used instead (if available).
2727
Local host: %s
2828
UMMU device: %s
2929
Error: %s (%d)
30+
#
31+
[invalid mca param value]
32+
WARNING: An invalid MCA parameter value was found for memory/linux
33+
component.
34+
35+
Problem: %s
36+
Resolution: %s

opal/mca/memory/linux/hooks.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "opal/mca/mca.h"
3434
#include "opal/mca/memory/memory.h"
3535
#include "opal/constants.h"
36+
#include "opal/memoryhooks/memory.h"
3637

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

@@ -734,7 +735,10 @@ static check_result_t check(const char *name)
734735
}
735736
}
736737

737-
/* OMPI's init function */
738+
739+
/* This function is called on loading libmpi in case system has Memory Allocation Hooks
740+
* (see ompi/runtime/ompi_mpi_init.c for details)
741+
*/
738742
void opal_memory_linux_malloc_init_hook(void)
739743
{
740744
check_result_t r1, lp, lpp;

opal/mca/memory/linux/memory_linux.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ typedef struct opal_memory_linux_component_t {
3131
int ummunotify_fd;
3232
#endif
3333

34+
#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
35+
int use_memalign;
36+
size_t memalign_threshold;
37+
#endif
38+
3439
#if MEMORY_LINUX_PTMALLOC2
3540
/* Ptmalloc2-specific data. Note that these variables are all marked as volatile.
3641
* This is needed because of what may be a buggy optimization in the GCC 4.9.2
@@ -64,13 +69,20 @@ int opal_memory_linux_ummunotify_close(void);
6469
/* memory_linux_ptmalloc2.c */
6570
int opal_memory_linux_ptmalloc2_open(void);
6671
int opal_memory_linux_ptmalloc2_close(void);
67-
OPAL_DECLSPEC void opal_memory_linux_malloc_init_hook(void);
6872

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

78+
#if MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT
79+
OPAL_DECLSPEC void opal_memory_linux_malloc_init_hook(void);
80+
#endif /* MEMORY_LINUX_HAVE_MALLOC_HOOK_SUPPORT */
81+
82+
#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
83+
OPAL_DECLSPEC void opal_memory_linux_malloc_set_alignment(int use_memalign, size_t memalign_threshold);
84+
#endif /* MEMORY_LINUX_MALLOC_ALIGN_ENABLED */
85+
7486
END_C_DECLS
7587

7688
#endif

0 commit comments

Comments
 (0)