From: Augustin Degomme Date: Mon, 17 Jun 2013 16:12:28 +0000 (+0200) Subject: add option "smpi/use_shared_malloc", with true as default, to allow disabling the... X-Git-Tag: v3_9_90~254 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/964e31805a6409921f1e3a2cd497c15898b4b62b add option "smpi/use_shared_malloc", with true as default, to allow disabling the shared memory SMPI stuff dynamically Useful for debug purposes, to see if the shared memory is the cause of a bug --- diff --git a/src/simgrid/sg_config.c b/src/simgrid/sg_config.c index 3022b610d4..4dfa5d3762 100644 --- a/src/simgrid/sg_config.c +++ b/src/simgrid/sg_config.c @@ -708,6 +708,13 @@ void sg_config_init(int *argc, char **argv) NULL); xbt_cfg_setdefault_boolean(_sg_cfg_set, "smpi/display_timing", default_value); + default_value = xbt_strdup("yes"); + xbt_cfg_register(&_sg_cfg_set, "smpi/use_shared_malloc", + "Boolean indicating whether we should use shared memory when using SMPI_SHARED_MALLOC. Allows user to disable it for debug purposes.", + xbt_cfgelm_boolean, &default_value, 1, 1, NULL, + NULL); + xbt_cfg_setdefault_boolean(_sg_cfg_set, "smpi/use_shared_malloc", default_value); + double default_threshold = 1e-6; xbt_cfg_register(&_sg_cfg_set, "smpi/cpu_threshold", "Minimal computation time (in seconds) not discarded.", diff --git a/src/smpi/smpi_bench.c b/src/smpi/smpi_bench.c index 357e6f806b..cdb3c5a50b 100644 --- a/src/smpi/smpi_bench.c +++ b/src/smpi/smpi_bench.c @@ -350,42 +350,47 @@ void *smpi_shared_malloc(size_t size, const char *file, int line) int fd; void* mem; shared_data_t *data; - - for(i = 0; i < len; i++) { - /* Make the 'loc' ID be a flat filename */ - if(loc[i] == '/') { - loc[i] = '_'; - } - } - if (!allocs) { - allocs = xbt_dict_new_homogeneous(free); - } - data = xbt_dict_get_or_null(allocs, loc); - if(!data) { - fd = shm_open(loc, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if(fd < 0) { - switch(errno) { - case EEXIST: - xbt_die("Please cleanup /dev/shm/%s", loc); - default: - xbt_die("An unhandled error occured while opening %s: %s", loc, strerror(errno)); + if (sg_cfg_get_boolean("smpi/use_shared_malloc")){ + for(i = 0; i < len; i++) { + /* Make the 'loc' ID be a flat filename */ + if(loc[i] == '/') { + loc[i] = '_'; } } - data = xbt_new(shared_data_t, 1); - data->fd = fd; - data->count = 1; - data->loc = loc; - mem = shm_map(fd, size, data); - if(shm_unlink(loc) < 0) { - XBT_WARN("Could not early unlink %s: %s", loc, strerror(errno)); + if (!allocs) { + allocs = xbt_dict_new_homogeneous(free); } - xbt_dict_set(allocs, loc, data, NULL); - XBT_DEBUG("Mapping %s at %p through %d", loc, mem, fd); - } else { - mem = shm_map(data->fd, size, data); - data->count++; + data = xbt_dict_get_or_null(allocs, loc); + if(!data) { + fd = shm_open(loc, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if(fd < 0) { + switch(errno) { + case EEXIST: + xbt_die("Please cleanup /dev/shm/%s", loc); + default: + xbt_die("An unhandled error occured while opening %s: %s", loc, strerror(errno)); + } + } + data = xbt_new(shared_data_t, 1); + data->fd = fd; + data->count = 1; + data->loc = loc; + mem = shm_map(fd, size, data); + if(shm_unlink(loc) < 0) { + XBT_WARN("Could not early unlink %s: %s", loc, strerror(errno)); + } + xbt_dict_set(allocs, loc, data, NULL); + XBT_DEBUG("Mapping %s at %p through %d", loc, mem, fd); + } else { + mem = shm_map(data->fd, size, data); + data->count++; + } + XBT_DEBUG("Shared malloc %zu in %p (metadata at %p)", size, mem, data); + }else{ + mem = xbt_malloc(size); + XBT_DEBUG("Classic malloc %zu in %p", size, mem); } - XBT_DEBUG("Malloc %zu in %p (metadata at %p)", size, mem, data); + return mem; } void smpi_shared_free(void *ptr) @@ -393,33 +398,40 @@ void smpi_shared_free(void *ptr) char loc[PTR_STRLEN]; shared_metadata_t* meta; shared_data_t* data; - - if (!allocs) { - XBT_WARN("Cannot free: nothing was allocated"); - return; - } - if(!allocs_metadata) { - XBT_WARN("Cannot free: no metadata was allocated"); - } - snprintf(loc, PTR_STRLEN, "%p", ptr); - meta = (shared_metadata_t*)xbt_dict_get_or_null(allocs_metadata, loc); - if (!meta) { - XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr); - return; - } - data = meta->data; - if(!data) { - XBT_WARN("Cannot free: something is broken in the metadata link"); - return; - } - if(munmap(ptr, meta->size) < 0) { - XBT_WARN("Unmapping of fd %d failed: %s", data->fd, strerror(errno)); - } - data->count--; - if (data->count <= 0) { - close(data->fd); - xbt_dict_remove(allocs, data->loc); - free(data->loc); + if (sg_cfg_get_boolean("smpi/use_shared_malloc")){ + + if (!allocs) { + XBT_WARN("Cannot free: nothing was allocated"); + return; + } + if(!allocs_metadata) { + XBT_WARN("Cannot free: no metadata was allocated"); + } + snprintf(loc, PTR_STRLEN, "%p", ptr); + meta = (shared_metadata_t*)xbt_dict_get_or_null(allocs_metadata, loc); + if (!meta) { + XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr); + return; + } + data = meta->data; + if(!data) { + XBT_WARN("Cannot free: something is broken in the metadata link"); + return; + } + if(munmap(ptr, meta->size) < 0) { + XBT_WARN("Unmapping of fd %d failed: %s", data->fd, strerror(errno)); + } + data->count--; + XBT_DEBUG("Shared free - no removal - of %p, count = %d", ptr, data->count); + if (data->count <= 0) { + close(data->fd); + xbt_dict_remove(allocs, data->loc); + free(data->loc); + XBT_DEBUG("Shared free - with removal - of %p", ptr); + } + }else{ + XBT_DEBUG("Classic free of %p", ptr); + xbt_free(ptr); } } #endif