From 4dd1310611f8c12b4b9703c703f270c301357e99 Mon Sep 17 00:00:00 2001 From: Christian Heinrich Date: Tue, 18 Sep 2018 09:32:20 +0200 Subject: [PATCH] [SMPI/LB] Intercept realloc and calloc calls for memory consumption Thanks Rafael Keller Tesser for the implementation --- include/smpi/sampi.h | 4 ++++ src/smpi/plugins/ampi/ampi.cpp | 25 ++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/include/smpi/sampi.h b/include/smpi/sampi.h index c3087a86f6..6e8a536fd1 100644 --- a/include/smpi/sampi.h +++ b/include/smpi/sampi.h @@ -15,12 +15,16 @@ #ifndef HAVE_SMPI #define malloc(nbytes) _sampi_malloc(nbytes) +#define calloc(n_elm,elm_size) _sampi_calloc(n_elm,elm_size) +#define realloc(ptr,nbytes) _sampi_realloc(ptr,nbytes) #define free(ptr) _sampi_free(ptr) #endif SG_BEGIN_DECL() XBT_PUBLIC void* _sampi_malloc(size_t size); +XBT_PUBLIC void* _sampi_calloc(size_t n_elm, size_t elm_size); +XBT_PUBLIC void* _sampi_realloc(void *ptr, size_t size); XBT_PUBLIC void _sampi_free(void* ptr); AMPI_CALL(XBT_PUBLIC int, MPI_Iteration_in, (MPI_Comm comm)) diff --git a/src/smpi/plugins/ampi/ampi.cpp b/src/smpi/plugins/ampi/ampi.cpp index c88fce7152..b07629060d 100644 --- a/src/smpi/plugins/ampi/ampi.cpp +++ b/src/smpi/plugins/ampi/ampi.cpp @@ -15,8 +15,10 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(plugin_pampi, smpi, "Logging specific to the AMP static std::vector memory_size(500, 0); // FIXME cheinrich This needs to be dynamic static std::map alloc_table; // Keep track of all allocations -extern "C" XBT_PUBLIC void* _sampi_malloc(size_t); +extern "C" XBT_PUBLIC void* _sampi_malloc(size_t); // FIXME Use declarations from sampi.h instead extern "C" XBT_PUBLIC void _sampi_free(void* ptr); +extern "C" XBT_PUBLIC void* _sampi_calloc(size_t num_elm, size_t elem_size); +extern "C" XBT_PUBLIC void* _sampi_realloc(void* ptr, size_t size); extern "C" void* _sampi_malloc(size_t size) { void* result = malloc (size); // We need the space here to prevent recursive substitution @@ -35,6 +37,27 @@ extern "C" void _sampi_free(void* ptr) free(ptr); } +extern "C" void* _sampi_calloc(size_t num_elm, size_t elem_size) +{ + void* result = calloc (num_elm, elem_size); // We need the space here to prevent recursive substitution + alloc_table.insert({result, num_elm * elem_size}); + if (not simgrid::s4u::this_actor::is_maestro()) { + memory_size[simgrid::s4u::this_actor::get_pid()] += num_elm * elem_size; + } + return result; +} +extern "C" void* _sampi_realloc(void* ptr, size_t size) +{ + void* result = realloc (ptr, size); // We need the space here to prevent recursive substitution + int old_size = alloc_table.at(ptr); + alloc_table.erase(ptr); + alloc_table.insert({result, size}); + if (not simgrid::s4u::this_actor::is_maestro()) { + memory_size[simgrid::s4u::this_actor::get_pid()] += size - old_size; + } + return result; +} + #include "ampi.hpp" #include namespace simgrid { -- 2.20.1