1
1

temporarily bring back the malloc_hooks code so that Galen and I can do

some regression testing

This commit was SVN r8548.
Этот коммит содержится в:
Brian Barrett 2005-12-19 21:08:51 +00:00
родитель ad7ee9ee26
Коммит 7801b9045f
5 изменённых файлов: 411 добавлений и 0 удалений

28
opal/mca/memory/malloc_hooks/Makefile.am Обычный файл
Просмотреть файл

@ -0,0 +1,28 @@
#
# Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
# 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.
# 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.
# $COPYRIGHT$
#
# Additional copyrights may follow
#
# $HEADER$
#
noinst_LTLIBRARIES = libmca_memory_malloc_hooks.la
# Source code files
libmca_memory_malloc_hooks_la_SOURCES = \
memory_malloc_hooks.c \
memory_malloc_hooks_component.c
libmca_memory_malloc_hooks_la_LIBADD = \
$(memory_malloc_hooks_LIBS)

89
opal/mca/memory/malloc_hooks/configure.m4 Обычный файл
Просмотреть файл

@ -0,0 +1,89 @@
# -*- shell-script -*-
#
# Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
# 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.
# 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.
# $COPYRIGHT$
#
# Additional copyrights may follow
#
# $HEADER$
#
AC_DEFUN([MCA_memory_malloc_hooks_COMPILE_MODE], [
AC_MSG_CHECKING([for MCA component $2:$3 compile mode])
$4="static"
AC_MSG_RESULT([$$4])
])
# MCA_memory_malloc_hooks_CONFIG(action-if-can-compile,
# [action-if-cant-compile])
# ------------------------------------------------
AC_DEFUN([MCA_memory_malloc_hooks_CONFIG],[
AC_ARG_WITH([memory-manager],
[AC_HELP_STRING([--with-memory-manager=TYPE],
[Use TYPE for intercepting memory management
calls to control memory pinning.])])
AS_IF([test "$with_memory_manager" = "malloc_hooks"],
[memory_malloc_hooks_happy="yes"
memory_malloc_hooks_should_use=1],
[memory_malloc_hooks_should_use=0
AS_IF([test "$with_memory_manager" = ""],
[memory_malloc_hooks_happy="yes"],
[memory_malloc_hooks_happy="no"])])
AS_IF([test "$memory_malloc_hooks_happy" = "yes"],
[AS_IF([test "$enable_mpi_threads" = "yes" -o \
"$enable_progress_threads" = "yes"],
[memory_malloc_hooks_happy="no"])])
AS_IF([test "$memory_malloc_hooks_happy" = "yes"],
[# check for malloc.h
AC_CHECK_HEADER([malloc.h],
[memory_malloc_hooks_happy="yes"],
[memory_malloc_hooks_happy="no"])])
AS_IF([test "$memory_malloc_hooks_happy" = "yes"],
[# check for init hook symbol
AC_CHECK_DECL([__malloc_initialize_hook],
[memory_malloc_hooks_happy="yes"],
[memory_malloc_hooks_happy="no"],
[AC_INCLUDES_DEFAULT
#include <malloc.h>])])
AS_IF([test "$memory_malloc_hooks_happy" = "yes"],
[# check for the chunk size function
AC_CHECK_FUNC([malloc_usable_size],
[memory_malloc_hooks_happy="yes"],
[memory_malloc_hooks_happy="no"])])
memory_malloc_hooks_LIBS_SAVE="$LIBS"
AS_IF([test "$memory_malloc_hooks_happy" = "yes"],
[AC_CHECK_LIB([dl],
[dlsym],
[memory_malloc_hooks_happy="yes"],
[memory_malloc_hooks_happy="no"])])
LIBS="$memory_malloc_hooks_LIBS_SAVE"
AS_IF([test "$memory_malloc_hooks_happy" = "yes"],
[memory_malloc_hooks_WRAPPER_EXTRA_LIBS="-ldl"
memory_malloc_hooks_LIBS="-ldl"])
AS_IF([test "$memory_malloc_hooks_happy" = "no" -a \
"$memory_malloc_hoooks_should_use" = "1"],
[AC_MSG_ERROR([malloc hooks memory management requested but not available. Aborting.])])
AC_SUBST(memory_malloc_hooks_LIBS)
AS_IF([test "$memory_malloc_hooks_happy" = "yes"],
[$1], [$2])
])

Просмотреть файл

@ -0,0 +1,23 @@
# -*- shell-script -*-
#
# Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
# 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.
# 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.
# $COPYRIGHT$
#
# Additional copyrights may follow
#
# $HEADER$
#
# Specific to this module
PARAM_CONFIG_PRIORITY=10
PARAM_CONFIG_FILES="Makefile"

Просмотреть файл

@ -0,0 +1,198 @@
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* 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.
* 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.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <stdio.h>
#define __USE_GNU
#include <dlfcn.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
#include "opal/memoryhooks/memory_internal.h"
/* Prototypes for our hooks. */
void opal_memory_malloc_hooks_init(void);
static void local_free_hook(void*, const void*);
static void* local_malloc_hook(size_t, const void*);
static void* local_realloc_hook(void*, size_t, const void*);
/* Override initializing hook from the C library. */
void (*__malloc_initialize_hook) (void) = opal_memory_malloc_hooks_init;
/* local variable - next in stack of free hooks */
static void (*old_free_hook)(void*, const void*);
static void* (*old_realloc_hook)(void*, size_t, const void*);
static void* (*old_malloc_hook)(size_t, const void*);
int opal_memory_malloc_hooks_initialized = 0;
void
opal_memory_malloc_hooks_init(void)
{
if (opal_memory_malloc_hooks_initialized != 0) {
return;
}
opal_memory_malloc_hooks_initialized = 1;
old_free_hook = __free_hook;
old_malloc_hook = __malloc_hook;
old_realloc_hook = __realloc_hook;
__free_hook = local_free_hook;
__malloc_hook = local_malloc_hook;
__realloc_hook = local_realloc_hook;
opal_mem_hooks_set_support(OPAL_MEMORY_FREE_SUPPORT|OPAL_MEMORY_MALLOC_SUPPORT);
assert(__malloc_hook == local_malloc_hook);
}
static void
local_free_hook(void *ptr, const void *caller)
{
if (__malloc_hook != local_malloc_hook) abort();
/* dispatch about the pending free */
opal_mem_hooks_release_hook(ptr, malloc_usable_size(ptr), 1);
__free_hook = old_free_hook;
/* call the next chain down */
free(ptr);
/* save the hooks again and restore our hook again */
old_free_hook = __free_hook;
__free_hook = local_free_hook;
}
static void*
local_malloc_hook(size_t size, const void *caller)
{
void *ret;
__malloc_hook = old_malloc_hook;
/* call the next chain down */
ret = malloc(size);
/* save the hooks again and restory our hack again */
old_malloc_hook = __malloc_hook;
__malloc_hook = local_malloc_hook;
opal_mem_hooks_alloc_hook(ret, malloc_usable_size(ret), 1);
assert(__malloc_hook == local_malloc_hook);
return ret;
}
/* for better or worse, we must assume that the buffer being passed to
realloc is not going to be expandable and therefore is going to be
free()ed. */
static void*
local_realloc_hook(void *ptr, size_t size, const void *caller)
{
void *ret;
assert(__malloc_hook == local_malloc_hook);
/* dispatch about the pending free */
opal_mem_hooks_release_hook(ptr, malloc_usable_size(ptr), 1);
/* realloc can call malloc (but not free). Doing so with the
* memory hooks causes us some interesting problems and causes
* the malloc_hook to be left as NULL. Pop the stack now so
* that we don't see the memory registration twice.
*/
__realloc_hook = old_realloc_hook;
__malloc_hook = old_malloc_hook;
/* call the next chain down */
ret = realloc(ptr, size);
/* save the hooks again and restore our hook again */
old_realloc_hook = __realloc_hook;
old_malloc_hook = __malloc_hook;
__realloc_hook = local_realloc_hook;
__malloc_hook = local_malloc_hook;
opal_mem_hooks_alloc_hook(ret, malloc_usable_size(ret), 1);
assert(__malloc_hook == local_malloc_hook);
return ret;
}
#if 0
/* mmap is a weak symbol on any platform that I know of that
supports malloc hooks, so we can just intercept it like this... */
void*
mmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset)
{
void *ret;
static void* (*realmmap)(void*, size_t, int, int, int, off_t);
if (NULL == realmmap) {
union {
void* (*mmap_fp)(void*, size_t, int, int, int, off_t);
void *mmap_p;
} tmp;
tmp.mmap_p = dlsym(RTLD_NEXT, "mmap");
realmmap = tmp.mmap_fp;
}
ret = realmmap(addr, len, prot, flags, fd, offset);
opal_mem_hooks_alloc_hook(ret, len, 0);
return ret;
}
#endif
/* munmap is a weak symbol on any platform that I know of that
supports malloc hooks, so we can just intercept it like this... */
int
munmap(void* addr, size_t len)
{
static int (*realmunmap)(void*, size_t);
/* dispatch about the pending release */
opal_mem_hooks_release_hook(addr, len, 1);
assert(__malloc_hook == local_malloc_hook);
if (NULL == realmunmap) {
union {
int (*munmap_fp)(void*, size_t);
void *munmap_p;
} tmp;
tmp.munmap_p = dlsym(RTLD_NEXT, "munmap");
realmunmap = tmp.munmap_fp;
}
return realmunmap(addr, len);
}

Просмотреть файл

@ -0,0 +1,73 @@
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* 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.
* 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.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "ompi_config.h"
#include <stdlib.h>
#include "opal/mca/memory/memory.h"
#include "opal/include/constants.h"
extern int opal_memory_malloc_hooks_initialized;
extern void opal_memory_malloc_hooks_init(void);
static int opal_memory_malloc_open(void);
const opal_memory_base_component_1_0_0_t mca_memory_malloc_hooks_component = {
/* First, the mca_component_t struct containing meta information
about the component itself */
{
/* Indicate that we are a memory v1.0.0 component (which also
implies a specific MCA version) */
OPAL_MEMORY_BASE_VERSION_1_0_0,
/* Component name and version */
"malloc_hooks",
OPAL_MAJOR_VERSION,
OPAL_MINOR_VERSION,
OPAL_RELEASE_VERSION,
/* Component open and close functions */
opal_memory_malloc_open,
NULL
},
/* Next the MCA v1.0.0 component meta data */
{
/* Whether the component is checkpointable or not */
true
},
};
static int
opal_memory_malloc_open(void)
{
void *a;
/* allocate some memory to make sure that malloc has been called,
then make sure we initialized */
a = malloc(1);
free(a);
if (0 == opal_memory_malloc_hooks_initialized) {
printf("memory_malloc_hooks not properly initialized\n");
}
return OPAL_SUCCESS;
}