2005-08-10 02:40:42 +04:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* 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.
|
2005-08-10 02:40:42 +04: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.
|
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ompi_config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2005-08-12 17:29:26 +04:00
|
|
|
#include <sys/mman.h>
|
2005-12-01 01:32:18 +03:00
|
|
|
#include <assert.h>
|
2005-08-10 02:40:42 +04:00
|
|
|
|
2006-02-12 22:51:24 +03:00
|
|
|
#include "ompi/constants.h"
|
2005-08-10 02:40:42 +04:00
|
|
|
#include "opal/runtime/opal.h"
|
2005-11-11 03:26:27 +03:00
|
|
|
#include "opal/memoryhooks/memory.h"
|
2005-08-10 02:40:42 +04:00
|
|
|
|
2006-11-06 17:00:43 +03:00
|
|
|
/*
|
|
|
|
* The counter variable is volatile to avoid (wrong) compiler optimisations,
|
|
|
|
* which can lead to wrong code.
|
|
|
|
*/
|
2006-11-03 13:46:18 +03:00
|
|
|
volatile int counter = 0;
|
2005-12-24 08:34:51 +03:00
|
|
|
const int bigsize = 100 * 1024 * 1024;
|
2005-08-10 02:40:42 +04:00
|
|
|
|
|
|
|
static void
|
2005-12-17 00:26:13 +03:00
|
|
|
alloc_callback(void *buf, size_t length, void *cbdata, bool extra)
|
2005-08-10 02:40:42 +04:00
|
|
|
{
|
2005-12-01 01:32:18 +03:00
|
|
|
counter++;
|
2005-11-29 07:46:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-12-17 00:26:13 +03:00
|
|
|
release_callback(void *buf, size_t length, void *cbdata, bool extra)
|
2005-11-29 07:46:14 +03:00
|
|
|
{
|
|
|
|
printf("\trelease callback with %lx, %d\n", (unsigned long) buf, (int) length);
|
2005-12-01 01:32:18 +03:00
|
|
|
counter--;
|
2005-08-10 02:40:42 +04:00
|
|
|
}
|
|
|
|
|
2005-12-01 01:32:18 +03:00
|
|
|
|
|
|
|
static int
|
|
|
|
free_only_test(void)
|
2005-08-10 02:40:42 +04:00
|
|
|
{
|
2005-12-01 01:32:18 +03:00
|
|
|
/* BWB - finish me! */
|
|
|
|
printf("test not written yet - skipping\n");
|
2005-11-29 07:46:14 +03:00
|
|
|
return 77;
|
2005-12-01 01:32:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
alloc_free_test(void)
|
|
|
|
{
|
|
|
|
void *foo, *bar;
|
|
|
|
int retval;
|
2005-08-10 02:40:42 +04:00
|
|
|
|
2005-11-29 07:46:14 +03:00
|
|
|
retval = opal_mem_hooks_register_release(release_callback, NULL);
|
|
|
|
retval |= opal_mem_hooks_register_alloc(alloc_callback, NULL);
|
2005-08-18 06:59:02 +04:00
|
|
|
if (retval != OMPI_SUCCESS) {
|
|
|
|
printf("handler registration failed\n");
|
|
|
|
return retval;
|
|
|
|
}
|
2005-08-10 02:40:42 +04:00
|
|
|
|
2005-12-01 01:32:18 +03:00
|
|
|
/* make some big malloc that should always trip a release on free */
|
|
|
|
printf(" - malloc big buffer\n");
|
2005-12-24 08:34:51 +03:00
|
|
|
counter = 0;
|
2005-12-01 01:32:18 +03:00
|
|
|
foo = malloc(bigsize);
|
|
|
|
assert(counter >= 1);
|
|
|
|
printf(" - free of big buffer\n");
|
2005-12-06 03:44:50 +03:00
|
|
|
counter = 1;
|
2005-08-10 02:40:42 +04:00
|
|
|
free(foo);
|
2005-12-01 01:32:18 +03:00
|
|
|
assert(counter == 0);
|
2005-08-10 02:40:42 +04:00
|
|
|
|
2005-12-01 01:32:18 +03:00
|
|
|
/* check mmap / munmap */
|
|
|
|
printf(" - mmap of buffer\n");
|
|
|
|
counter = 0;
|
|
|
|
bar = mmap(NULL, 4096, PROT_READ, MAP_ANON, -1, 0);
|
2005-12-24 08:34:51 +03:00
|
|
|
if (opal_mem_hooks_support_level() & OPAL_MEMORY_MMAP_SUPPORT) {
|
|
|
|
assert(counter >= 1);
|
|
|
|
}
|
2005-08-12 17:29:26 +04:00
|
|
|
|
2005-12-01 01:32:18 +03:00
|
|
|
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);
|
2005-08-13 05:08:34 +04:00
|
|
|
|
2005-11-29 07:46:14 +03:00
|
|
|
retval = opal_mem_hooks_unregister_release(release_callback);
|
|
|
|
retval |= opal_mem_hooks_unregister_alloc(alloc_callback);
|
2005-12-01 01:32:18 +03:00
|
|
|
|
|
|
|
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;
|
2005-12-06 03:44:50 +03:00
|
|
|
} else if ((OPAL_MEMORY_FREE_SUPPORT|OPAL_MEMORY_MALLOC_SUPPORT) ==
|
|
|
|
((OPAL_MEMORY_FREE_SUPPORT|OPAL_MEMORY_MALLOC_SUPPORT) & support)) {
|
2005-12-01 01:32:18 +03:00
|
|
|
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;
|
|
|
|
}
|
2005-08-10 02:40:42 +04:00
|
|
|
|
|
|
|
opal_finalize();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|