2005-08-14 03:11:54 +00:00
|
|
|
/*
|
2007-03-16 23:11:45 +00:00
|
|
|
* Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
|
2005-11-05 19:57:48 +00:00
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2005-08-14 03:11:54 +00:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2009-04-01 17:52:16 +00:00
|
|
|
* Copyright (c) 2009 Cisco Systems, Inc. All rights reserved.
|
2005-08-14 03:11:54 +00:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2009-04-01 17:52:16 +00:00
|
|
|
/* The goal of this component is to wholly replace the underlying
|
|
|
|
allocator with our internal ptmalloc2 allocator.
|
|
|
|
|
|
|
|
See the file README-open-mpi.txt for details of how it works. */
|
|
|
|
|
2006-02-12 01:33:29 +00:00
|
|
|
#include "opal_config.h"
|
2005-08-14 03:11:54 +00:00
|
|
|
|
2009-04-01 18:20:23 +00:00
|
|
|
#include <malloc.h>
|
2009-04-04 00:43:17 +00:00
|
|
|
#include <sys/mman.h>
|
2009-04-01 17:52:16 +00:00
|
|
|
|
2009-03-13 02:10:32 +00:00
|
|
|
#include "opal/constants.h"
|
2005-08-14 03:11:54 +00:00
|
|
|
#include "opal/mca/memory/memory.h"
|
2008-06-13 22:32:49 +00:00
|
|
|
#include "opal/memoryhooks/memory.h"
|
|
|
|
|
2009-04-15 19:09:10 +00:00
|
|
|
/* Need to call a function in hooks.c to ensure that all those symbols
|
|
|
|
get pulled in at link time (e.g., when building libmpi.a, so that
|
|
|
|
those symbols end up in the final executable -- especially if we
|
|
|
|
use --disable-dlopen and therefore -Wl,--export-dynamic isn't used
|
|
|
|
when we build OMPI). */
|
|
|
|
extern void *opal_memory_ptmalloc2_hook_pull(void);
|
|
|
|
|
2009-04-01 17:52:16 +00:00
|
|
|
static int ptmalloc2_open(void);
|
2005-08-14 03:11:54 +00:00
|
|
|
|
2008-07-28 22:40:57 +00:00
|
|
|
const opal_memory_base_component_2_0_0_t mca_memory_ptmalloc2_component = {
|
2005-08-14 03:11:54 +00:00
|
|
|
/* First, the mca_component_t struct containing meta information
|
|
|
|
about the component itself */
|
|
|
|
{
|
2008-07-28 22:40:57 +00:00
|
|
|
OPAL_MEMORY_BASE_VERSION_2_0_0,
|
2005-08-14 03:11:54 +00:00
|
|
|
|
|
|
|
/* Component name and version */
|
|
|
|
"ptmalloc2",
|
|
|
|
OPAL_MAJOR_VERSION,
|
|
|
|
OPAL_MINOR_VERSION,
|
|
|
|
OPAL_RELEASE_VERSION,
|
|
|
|
|
|
|
|
/* Component open and close functions */
|
2009-04-01 17:52:16 +00:00
|
|
|
ptmalloc2_open,
|
2005-08-14 03:11:54 +00:00
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
2007-03-16 23:11:45 +00:00
|
|
|
/* The component is checkpoint ready */
|
|
|
|
MCA_BASE_METADATA_PARAM_CHECKPOINT
|
2005-08-14 03:11:54 +00:00
|
|
|
},
|
|
|
|
};
|
2008-06-13 22:32:49 +00:00
|
|
|
|
2009-04-01 17:52:16 +00:00
|
|
|
/* Public symbols */
|
|
|
|
bool opal_memory_ptmalloc2_free_invoked = false;
|
|
|
|
bool opal_memory_ptmalloc2_malloc_invoked = false;
|
|
|
|
bool opal_memory_ptmalloc2_realloc_invoked = false;
|
|
|
|
bool opal_memory_ptmalloc2_memalign_invoked = false;
|
2009-04-04 00:43:17 +00:00
|
|
|
bool opal_memory_ptmalloc2_munmap_invoked = false;
|
2008-06-13 22:32:49 +00:00
|
|
|
|
2009-04-01 17:52:16 +00:00
|
|
|
static int ptmalloc2_open(void)
|
2008-06-13 22:32:49 +00:00
|
|
|
{
|
2009-04-01 18:20:23 +00:00
|
|
|
void *p;
|
2009-04-04 00:43:17 +00:00
|
|
|
int val = 0;
|
2009-04-01 17:52:16 +00:00
|
|
|
|
2009-04-15 19:09:10 +00:00
|
|
|
/* Call a dummy function in hooks.c. ***Do not remove this
|
|
|
|
call!*** See comment at the beginning of this file explaining
|
|
|
|
why it is here. */
|
|
|
|
p = opal_memory_ptmalloc2_hook_pull();
|
|
|
|
|
2009-04-01 17:52:16 +00:00
|
|
|
/* We will also provide malloc/free support if we've been
|
|
|
|
activated. We don't rely on the __malloc_initialize_hook()
|
|
|
|
previously being called because it's possible that our hook was
|
|
|
|
called, but then someone else reset the hooks to point to
|
|
|
|
something else (i.e., before MPI_INIT). So explicitly test
|
|
|
|
here if our hooks are still in place. If they are, then enable
|
|
|
|
FREE|CUNK_SUPPORT. If not, then don't enable that support --
|
|
|
|
just leave it at MUNMAP_SUPPORT.
|
|
|
|
|
|
|
|
(Look in hooks.c for the __malloc_initialize_hook setup) */
|
|
|
|
|
|
|
|
/* Do a simple set of tests to see if our hooks are still the ones
|
|
|
|
installed. Explicitly reset the flags indicating that our
|
|
|
|
functions were invoked */
|
|
|
|
opal_memory_ptmalloc2_malloc_invoked = false;
|
|
|
|
opal_memory_ptmalloc2_realloc_invoked = false;
|
|
|
|
opal_memory_ptmalloc2_memalign_invoked = false;
|
|
|
|
opal_memory_ptmalloc2_free_invoked = false;
|
2009-04-04 00:43:17 +00:00
|
|
|
opal_memory_ptmalloc2_munmap_invoked = false;
|
2009-04-01 17:52:16 +00:00
|
|
|
|
2009-04-01 18:20:23 +00:00
|
|
|
p = malloc(1024 * 1024 * 4);
|
|
|
|
if (NULL == p) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
p = realloc(p, 1024 * 1024 * 4 + 32);
|
2009-04-01 17:52:16 +00:00
|
|
|
if (NULL == p) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
free(p);
|
2009-07-29 20:33:38 +00:00
|
|
|
p = memalign(4, 1024 * 1024);
|
2009-04-01 18:20:23 +00:00
|
|
|
if (NULL == p) {
|
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
2009-04-01 17:52:16 +00:00
|
|
|
free(p);
|
|
|
|
|
2009-10-01 23:51:48 +00:00
|
|
|
#if HAVE_POSIX_MEMALIGN
|
|
|
|
/* Double check for posix_memalign, too */
|
|
|
|
if (opal_memory_ptmalloc2_memalign_invoked) {
|
|
|
|
opal_memory_ptmalloc2_memalign_invoked = false;
|
2009-10-07 20:01:50 +00:00
|
|
|
if (0 != posix_memalign(&p, sizeof(void*), 1024 * 1024)) {
|
|
|
|
return OPAL_ERR_IN_ERRNO;
|
2009-10-01 23:51:48 +00:00
|
|
|
}
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-01 17:52:16 +00:00
|
|
|
if (opal_memory_ptmalloc2_malloc_invoked &&
|
|
|
|
opal_memory_ptmalloc2_realloc_invoked &&
|
|
|
|
opal_memory_ptmalloc2_memalign_invoked &&
|
|
|
|
opal_memory_ptmalloc2_free_invoked) {
|
|
|
|
/* Happiness; our functions were invoked */
|
|
|
|
val |= OPAL_MEMORY_FREE_SUPPORT | OPAL_MEMORY_CHUNK_SUPPORT;
|
|
|
|
}
|
|
|
|
|
2009-10-07 12:39:01 +00:00
|
|
|
p = mmap(NULL, 4096, PROT_READ, (MAP_ANONYMOUS | MAP_PRIVATE), -1, 0);
|
2009-10-07 14:20:18 +00:00
|
|
|
if (MAP_FAILED == p) {
|
2009-10-06 17:44:14 +00:00
|
|
|
return OPAL_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
2009-04-04 00:43:17 +00:00
|
|
|
munmap(p, 4096);
|
|
|
|
|
|
|
|
if (opal_memory_ptmalloc2_munmap_invoked) {
|
|
|
|
val |= OPAL_MEMORY_MUNMAP_SUPPORT;
|
|
|
|
}
|
|
|
|
|
2009-04-01 17:52:16 +00:00
|
|
|
/* Set the support level */
|
|
|
|
opal_mem_hooks_set_support(val);
|
|
|
|
|
2008-06-13 22:32:49 +00:00
|
|
|
return OPAL_SUCCESS;
|
|
|
|
}
|