1
1
openmpi/test/memory/opal_memory_basic.c
Brian Barrett f44bd9e067 * Intercept both allocations and deallocations from ptmalloc2 (including
both mmap and munmap), adjusting the configure script so that the
  component will only be activated on systems that use ptmalloc2 in the
  first place -- ie, Linux
* Remove the malloc_hooks component - it became an unworkable solution
  once threads and such were considered.
* Remove malloc_interpose component - it never worked quite right and
  was not going to be able to intercept malloc, so it wasn't going to
  be useful for OMPI's purposes.
* Update tests a little bit to match recent memory hooks api
  issues - still needs a bit of work.

This commit was SVN r8381.
2005-12-06 00:44:50 +00:00

128 строки
3.2 KiB
C

/*
* 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 <stdio.h>
#include <sys/mman.h>
#include <assert.h>
#include "ompi/include/constants.h"
#include "opal/runtime/opal.h"
#include "opal/memoryhooks/memory.h"
int counter = 0;
const int bigsize = 16 * 1024 * 1024;
static void
alloc_callback(void *buf, size_t length, void *cbdata)
{
counter++;
}
static void
release_callback(void *buf, size_t length, void *cbdata)
{
printf("\trelease callback with %lx, %d\n", (unsigned long) buf, (int) length);
counter--;
}
static int
free_only_test(void)
{
/* BWB - finish me! */
printf("test not written yet - skipping\n");
return 77;
}
static int
alloc_free_test(void)
{
void *foo, *bar;
int retval;
retval = opal_mem_hooks_register_release(release_callback, NULL);
retval |= opal_mem_hooks_register_alloc(alloc_callback, NULL);
if (retval != OMPI_SUCCESS) {
printf("handler registration failed\n");
return retval;
}
counter = 0;
/* make some big malloc that should always trip a release on free */
printf(" - malloc big buffer\n");
foo = malloc(bigsize);
assert(counter >= 1);
printf(" - free of big buffer\n");
counter = 1;
free(foo);
assert(counter == 0);
/* check mmap / munmap */
printf(" - mmap of buffer\n");
counter = 0;
bar = mmap(NULL, 4096, PROT_READ, MAP_ANON, -1, 0);
assert(counter >= 1);
printf(" - munmap of buffer\n");
/* mmap might call malloc internally, so do this or we might
appear to leak memory */
counter = 1;
munmap(NULL, 0);
assert(counter == 0);
retval = opal_mem_hooks_unregister_release(release_callback);
retval |= opal_mem_hooks_unregister_alloc(alloc_callback);
return OPAL_SUCCESS;
}
int
main(int argc, char *argv[])
{
int ret;
int support;
opal_init();
/* this printf needs to be here for the test to work! */
printf("running malloc hooks test\n");
support = opal_mem_hooks_support_level();
if (0 == support) {
printf("no memory registration supported. skipping test.\n");
ret = 77;
} else if ((OPAL_MEMORY_FREE_SUPPORT|OPAL_MEMORY_MALLOC_SUPPORT) ==
((OPAL_MEMORY_FREE_SUPPORT|OPAL_MEMORY_MALLOC_SUPPORT) & support)) {
ret = alloc_free_test();
} else if (OPAL_MEMORY_FREE_SUPPORT & support) {
ret = free_only_test();
} else {
printf("Odd support response: %d\n", support);
ret = 1;
}
opal_finalize();
return ret;
}