Skip to content

Commit 11e2d78

Browse files
committed
opal/memory: update component structure
This commit makes it possible to set relative priorities for components. Before the addition of the patched component there was only one component that would run on any system but that is no longer the case. When determining which component to open each component's query function is called and the one that returns the highest priority is opened. The default priority of the patcher component is set slightly higher than the old ptmalloc2/ummunotify component. This commit fixes a long-standing break in the abstration of the memory components. ompi_mpi_init.c was referencing the linux malloc hook initilize function to ensure the hooks are initialized for libmpi.so. The abstraction break has been fixed by adding a memory base function that calls the open memory component's malloc hook init function if it has one. The code is not yet complete but is intended to support ptmalloc in 2.0.0. In that case the base function will always call the ptmalloc hook init if exists. Signed-off-by: Nathan Hjelm <[email protected]>
1 parent 7aa03d6 commit 11e2d78

File tree

9 files changed

+118
-25
lines changed

9 files changed

+118
-25
lines changed

ompi/runtime/ompi_mpi_init.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,13 @@
9898
#endif
9999
#include "ompi/runtime/ompi_cr.h"
100100

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

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

opal/mca/memory/base/base.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ BEGIN_C_DECLS
3232
*/
3333
OPAL_DECLSPEC extern mca_base_framework_t opal_memory_base_framework;
3434

35+
OPAL_DECLSPEC void opal_memory_base_malloc_init_hook (void);
36+
3537
END_C_DECLS
3638
#endif /* OPAL_BASE_MEMORY_H */

opal/mca/memory/base/memory_base_open.c

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* Copyright (c) 2009 Cisco Systems, Inc. All rights reserved.
1414
* Copyright (c) 2016 Research Organization for Information Science
1515
* and Technology (RIST). All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1618
* $COPYRIGHT$
1719
*
1820
* Additional copyrights may follow
@@ -43,20 +45,22 @@ static int empty_process(void)
4345
return OPAL_SUCCESS;
4446
}
4547

48+
static int empty_query (int *priority)
49+
{
50+
*priority = 0;
51+
return OPAL_SUCCESS;
52+
}
4653

4754
/*
4855
* Local variables
4956
*/
5057
static opal_memory_base_component_2_0_0_t empty_component = {
51-
/* Don't care about the version info */
52-
{ 0, },
53-
/* Don't care about the data */
54-
{ 0, },
5558
/* Empty / safe functions to call if no memory componet is selected */
56-
empty_process,
57-
opal_memory_base_component_register_empty,
58-
opal_memory_base_component_deregister_empty,
59-
opal_memory_base_component_set_alignment_empty,
59+
.memoryc_query = empty_query,
60+
.memoryc_process = empty_process,
61+
.memoryc_register = opal_memory_base_component_register_empty,
62+
.memoryc_deregister = opal_memory_base_component_deregister_empty,
63+
.memoryc_set_alignment = opal_memory_base_component_set_alignment_empty,
6064
};
6165

6266

@@ -66,30 +70,49 @@ static opal_memory_base_component_2_0_0_t empty_component = {
6670
opal_memory_base_component_2_0_0_t *opal_memory = &empty_component;
6771

6872

73+
void opal_memory_base_malloc_init_hook (void)
74+
{
75+
if (opal_memory->memoryc_init_hook) {
76+
opal_memory->memoryc_init_hook ();
77+
}
78+
}
6979

7080
/*
7181
* Function for finding and opening either all MCA components, or the one
7282
* that was specifically requested via a MCA parameter.
7383
*/
7484
static int opal_memory_base_open(mca_base_open_flag_t flags)
7585
{
86+
mca_base_component_list_item_t *item, *next;
87+
opal_memory_base_component_2_0_0_t *tmp;
88+
int priority, highest_priority = 0;
7689
int ret;
7790

78-
/* Open up all available components */
91+
/* can only be zero or one */
92+
OPAL_LIST_FOREACH(item, &opal_memory_base_framework.framework_components, mca_base_component_list_item_t) {
93+
tmp = (opal_memory_base_component_2_0_0_t *) item->cli_component;
94+
ret = tmp->memoryc_query (&priority);
95+
if (OPAL_SUCCESS != ret || priority < highest_priority) {
96+
continue;
97+
}
98+
99+
highest_priority = priority;
100+
opal_memory = tmp;
101+
}
102+
103+
OPAL_LIST_FOREACH_SAFE(item, next, &opal_memory_base_framework.framework_components, mca_base_component_list_item_t) {
104+
if ((void *) opal_memory != (void *) item->cli_component) {
105+
mca_base_component_unload (item->cli_component, opal_memory_base_framework.framework_output);
106+
opal_list_remove_item (&opal_memory_base_framework.framework_components, &item->super);
107+
}
108+
}
109+
110+
/* open remaining component */
79111
ret = mca_base_framework_components_open (&opal_memory_base_framework, flags);
80112
if (ret != OPAL_SUCCESS) {
81113
return ret;
82114
}
83115

84-
/* can only be zero or one */
85-
if (opal_list_get_size(&opal_memory_base_framework.framework_components) == 1) {
86-
mca_base_component_list_item_t *item;
87-
item = (mca_base_component_list_item_t*)
88-
opal_list_get_first(&opal_memory_base_framework.framework_components);
89-
opal_memory = (opal_memory_base_component_2_0_0_t*)
90-
item->cli_component;
91-
}
92-
93116
/* All done */
94117
return OPAL_SUCCESS;
95118
}

opal/mca/memory/linux/hooks.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,10 @@ void opal_memory_linux_malloc_init_hook(void)
744744
check_result_t r1, lp, lpp;
745745
bool want_rcache = false, found_driver = false;
746746

747+
if (!opal_memory_linux_opened) {
748+
return;
749+
}
750+
747751
/* First, check for a FAKEROOT environment. If we're in a
748752
fakeroot, then access() (and likely others) have been replaced
749753
and are not safe to call here in this pre-main environment. So

opal/mca/memory/linux/memory_linux.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ OPAL_DECLSPEC void opal_memory_linux_malloc_init_hook(void);
8383
OPAL_DECLSPEC void opal_memory_linux_malloc_set_alignment(int use_memalign, size_t memalign_threshold);
8484
#endif /* MEMORY_LINUX_MALLOC_ALIGN_ENABLED */
8585

86+
extern bool opal_memory_linux_opened;
87+
8688
END_C_DECLS
8789

8890
#endif

opal/mca/memory/linux/memory_linux_component.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Copyright (c) 2004-2005 The Regents of the University of California.
1212
* All rights reserved.
1313
* Copyright (c) 2009-2014 Cisco Systems, Inc. All rights reserved.
14-
* Copyright (c) 2013-2015 Los Alamos National Security, LLC. All rights
14+
* Copyright (c) 2013-2016 Los Alamos National Security, LLC. All rights
1515
* reserved.
1616
* Copyright (c) 2016 Research Organization for Information Science
1717
* and Technology (RIST). All rights reserved.
@@ -60,6 +60,7 @@
6060
static int linux_open(void);
6161
static int linux_close(void);
6262
static int linux_register(void);
63+
static int linux_query(int *);
6364

6465
#if MEMORY_LINUX_UMMUNOTIFY
6566
static bool ummunotify_opened = false;
@@ -69,6 +70,9 @@ static bool ptmalloc2_opened = false;
6970
#endif
7071

7172
bool opal_memory_linux_disable = false;
73+
static int mca_memory_linux_priority;
74+
75+
bool opal_memory_linux_opened = false;
7276

7377
opal_memory_linux_component_t mca_memory_linux_component = {
7478
/* First, the opal_memory_base_component_2_0_0_t */
@@ -96,6 +100,8 @@ opal_memory_linux_component_t mca_memory_linux_component = {
96100
/* Memory framework functions. These function pointer values
97101
are replaced by memory_linux_ummunotify.c at run time if we
98102
end up using ummunotify support. */
103+
.memoryc_init_hook = opal_memory_linux_malloc_init_hook,
104+
.memoryc_query = linux_query,
99105
.memoryc_register = opal_memory_base_component_register_empty,
100106
.memoryc_deregister = opal_memory_base_component_deregister_empty,
101107
#if MEMORY_LINUX_MALLOC_ALIGN_ENABLED
@@ -243,11 +249,25 @@ static int linux_register(void)
243249
if (0 > ret) {
244250
return ret;
245251
}
252+
253+
mca_memory_linux_priority = 50;
254+
ret = mca_base_component_var_register (&mca_memory_linux_component.super.memoryc_version,
255+
"priority", "Priority of the linux memory hook component",
256+
MCA_BASE_VAR_TYPE_INT, NULL, 0, 0, OPAL_INFO_LVL_5,
257+
MCA_BASE_VAR_SCOPE_CONSTANT, &mca_memory_linux_priority);
258+
if (0 > ret) {
259+
return ret;
260+
}
246261
#endif /* MEMORY_LINUX_MALLOC_ALIGN_ENABLED */
247262

248263
return (0 > ret) ? ret : OPAL_SUCCESS;
249264
}
250265

266+
static int linux_query (int *priority)
267+
{
268+
*priority = mca_memory_linux_priority;
269+
return OPAL_SUCCESS;
270+
}
251271

252272
static int linux_open(void)
253273
{
@@ -318,6 +338,7 @@ static int linux_open(void)
318338
__malloc_hook = _opal_memory_linux_malloc_align_hook;
319339
}
320340
#endif /* MEMORY_LINUX_MALLOC_ALIGN_ENABLED */
341+
opal_memory_linux_opened = true;
321342

322343
return OPAL_SUCCESS;
323344
}

opal/mca/memory/malloc_solaris/memory_malloc_solaris_component.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* All rights reserved.
1313
* Copyright (c) 2007-2011 Oracle and/or its affiliates. All rights reserved.
1414
* Copyright (c) 2009-2011 Cisco Systems, Inc. All rights reserved.
15-
* Copyright (c) 2015 Los Alamos National Security, LLC. All rights
15+
* Copyright (c) 2015-2016 Los Alamos National Security, LLC. All rights
1616
* reserved.
1717
* Copyright (c) 2016 Research Organization for Information Science
1818
* and Technology (RIST). All rights reserved.
@@ -46,6 +46,7 @@ int __munmap(caddr_t addr, size_t len);
4646
#endif
4747

4848
static int opal_memory_malloc_open(void);
49+
static int opal_memory_malloc_query(int *);
4950

5051
const opal_memory_base_component_2_0_0_t mca_memory_malloc_solaris_component = {
5152
/* First, the mca_component_t struct containing meta information
@@ -68,6 +69,7 @@ const opal_memory_base_component_2_0_0_t mca_memory_malloc_solaris_component = {
6869

6970
/* This component doesn't need these functions, but need to
7071
provide safe/empty register/deregister functions to call */
72+
.memoryc_query = opal_memory_malloc_query,
7173
.memoryc_register = opal_memory_base_component_register_empty,
7274
.memoryc_deregister = opal_memory_base_component_deregister_empty,
7375
.memoryc_set_alignment = opal_memory_base_component_set_alignment_empty,
@@ -93,6 +95,11 @@ opal_memory_malloc_open(void)
9395
return OPAL_SUCCESS;
9496
}
9597

98+
static int opal_memory_malloc_query (int *priority)
99+
{
100+
*priority = 79;
101+
return OPAL_SUCCESS;
102+
}
96103

97104
/*
98105
* Three ways to call munmap. Prefered is to call __munmap, which

opal/mca/memory/memory.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Copyright (c) 2004-2005 The Regents of the University of California.
1212
* All rights reserved.
1313
* Copyright (c) 2009 Cisco Systems, Inc. All rights reserved.
14-
* Copyright (c) 2015 Los Alamos National Security, LLC. All rights
14+
* Copyright (c) 2015-2016 Los Alamos National Security, LLC. All rights
1515
* reserved.
1616
* Copyright (c) 2016 Research Organization for Information Science
1717
* and Technology (RIST). All rights reserved.
@@ -78,6 +78,12 @@ BEGIN_C_DECLS
7878
*/
7979
typedef int (*opal_memory_base_component_process_fn_t)(void);
8080

81+
/**
82+
* Prototype for a function that is invoked when the memory base is
83+
* trying to select a component. This funtionality is required.
84+
*/
85+
typedef int (*opal_memory_base_component_query_fn_t)(int *priority);
86+
8187
/**
8288
* Prototype for a function that is invoked when Open MPI starts to
8389
* "care" about a specific memory region. That is, Open MPI declares
@@ -119,6 +125,11 @@ typedef int (*opal_memory_base_component_deregister_fn_t)(void *base,
119125
typedef void (*opal_memory_base_component_set_alignment_fn_t)(int use_memalign,
120126
size_t memalign_threshold);
121127

128+
/**
129+
* Function to be called when initializing malloc hooks
130+
*/
131+
typedef void (*opal_memory_base_component_init_hook_fn_t)(void);
132+
122133
/**
123134
* Structure for memory components.
124135
*/
@@ -128,6 +139,12 @@ typedef struct opal_memory_base_component_2_0_0_t {
128139
/** MCA base data */
129140
mca_base_component_data_t memoryc_data;
130141

142+
opal_memory_base_component_query_fn_t memoryc_query;
143+
144+
/** This function will be called when the malloc hooks are
145+
* initialized. It may be NULL if no hooks are needed. */
146+
opal_memory_base_component_init_hook_fn_t memoryc_init_hook;
147+
131148
/** Function to call when something has changed, as indicated by
132149
opal_memory_changed(). Will be ignored if the component does
133150
not provide an opal_memory_changed() macro that returns

opal/mca/memory/patcher/memory_patcher_component.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
static int patcher_open(void);
4848
static int patcher_close(void);
4949
static int patcher_register(void);
50+
static int patcher_query (int *);
51+
52+
static int mca_memory_patcher_priority;
5053

5154
opal_memory_patcher_component_t mca_memory_patcher_component = {
5255
.super = {
@@ -69,6 +72,7 @@ opal_memory_patcher_component_t mca_memory_patcher_component = {
6972
},
7073

7174
/* Memory framework functions. */
75+
.memoryc_query = patcher_query,
7276
.memoryc_register = opal_memory_base_component_register_empty,
7377
.memoryc_deregister = opal_memory_base_component_deregister_empty,
7478
.memoryc_set_alignment = opal_memory_base_component_set_alignment_empty,
@@ -226,6 +230,18 @@ static int intercept_brk (void *addr)
226230

227231
static int patcher_register (void)
228232
{
233+
mca_memory_patcher_priority = 80;
234+
mca_base_component_var_register (&mca_memory_patcher_component.super.memoryc_version,
235+
"priority", "Priority of the patcher memory hook component",
236+
MCA_BASE_VAR_TYPE_INT, NULL, 0, 0, OPAL_INFO_LVL_5,
237+
MCA_BASE_VAR_SCOPE_CONSTANT, &mca_memory_patcher_priority);
238+
239+
return OPAL_SUCCESS;
240+
}
241+
242+
static int patcher_query (int *priority)
243+
{
244+
*priority = mca_memory_patcher_priority;
229245
return OPAL_SUCCESS;
230246
}
231247

@@ -240,6 +256,9 @@ static int patcher_open (void)
240256

241257
was_executed_already = 1;
242258

259+
/* set memory hooks support level */
260+
opal_mem_hooks_set_support (OPAL_MEMORY_FREE_SUPPORT | OPAL_MEMORY_MUNMAP_SUPPORT);
261+
243262
rc = opal_patch_symbol ("mmap", (uintptr_t) intercept_mmap);
244263
if (OPAL_SUCCESS != rc) {
245264
return rc;

0 commit comments

Comments
 (0)