1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
/*
* 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/constants.h"
#include "opal/runtime/opal.h"
#include "opal/memoryhooks/memory.h"
/*
* The counter variable is volatile to avoid (wrong) compiler optimisations,
* which can lead to wrong code.
*/
volatile int counter = 0;
const int bigsize = 100 * 1024 * 1024;
static void
alloc_callback(void *buf, size_t length, void *cbdata, bool extra)
{
counter++;
}
static void
release_callback(void *buf, size_t length, void *cbdata, bool extra)
{
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;
}
/* make some big malloc that should always trip a release on free */
printf(" - malloc big buffer\n");
counter = 0;
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);
if (opal_mem_hooks_support_level() & OPAL_MEMORY_MMAP_SUPPORT) {
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;
}
|