From cf4076dba99c227abb293529a447371e1f20f76d Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Mon, 20 Apr 2015 09:56:35 +0200 Subject: [PATCH] test-tool: Make it possible to disable memory caching Disable memory caching in libiscsi if the environment variable LIBISCSI_CACHE_ALLOCATIONS has been set to zero. This makes Valgrind reports more meaningful. Signed-off-by: Bart Van Assche --- include/iscsi.h | 2 ++ lib/init.c | 39 ++++++++++++++++++++++++++++----------- lib/libiscsi.def | 1 + lib/libiscsi.syms | 1 + test-tool/iscsi-test-cu.c | 5 +++++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/include/iscsi.h b/include/iscsi.h index a36f5aa..c8f41bb 100644 --- a/include/iscsi.h +++ b/include/iscsi.h @@ -51,6 +51,8 @@ struct sockaddr; "[:]\"" +EXTERN void iscsi_set_cache_allocations(int ca); + /* * The following three functions are used to integrate libiscsi in an event * system. diff --git a/lib/init.c b/lib/init.c index a9885b1..ea9c5f5 100644 --- a/lib/init.c +++ b/lib/init.c @@ -33,6 +33,17 @@ #include "iscsi-private.h" #include "slist.h" +int cache_allocations = 1; + +/** + * Whether or not the internal memory allocator caches allocations. Disable + * memory allocation caching to improve the accuracy of Valgrind reports. + */ +void iscsi_set_cache_allocations(int ca) +{ + cache_allocations = ca; +} + void* iscsi_malloc(struct iscsi_context *iscsi, size_t size) { void * ptr = malloc(size); if (ptr != NULL) iscsi->mallocs++; @@ -94,19 +105,25 @@ void iscsi_sfree(struct iscsi_context *iscsi, void* ptr) { if (ptr == NULL) { return; } - if (iscsi->smalloc_free == SMALL_ALLOC_MAX_FREE) { - int i; - /* SMALL_ALLOC_MAX_FREE should be adjusted that this happens rarely */ - ISCSI_LOG(iscsi, 6, "smalloc free == SMALLOC_MAX_FREE"); - /* remove oldest half of free pointers and copy - * upper half to lower half */ - iscsi->smalloc_free >>= 1; - for (i = 0; i < iscsi->smalloc_free; i++) { - iscsi_free(iscsi, iscsi->smalloc_ptrs[i]); - iscsi->smalloc_ptrs[i] = iscsi->smalloc_ptrs[i + iscsi->smalloc_free]; + if (cache_allocations) { + if (iscsi->smalloc_free == SMALL_ALLOC_MAX_FREE) { + int i; + /* SMALL_ALLOC_MAX_FREE should be adjusted that this */ + /* happens rarely */ + ISCSI_LOG(iscsi, 6, "smalloc free == SMALLOC_MAX_FREE"); + /* remove oldest half of free pointers and copy + * upper half to lower half */ + iscsi->smalloc_free >>= 1; + for (i = 0; i < iscsi->smalloc_free; i++) { + iscsi_free(iscsi, iscsi->smalloc_ptrs[i]); + iscsi->smalloc_ptrs[i] = + iscsi->smalloc_ptrs[i + iscsi->smalloc_free]; + } } + iscsi->smalloc_ptrs[iscsi->smalloc_free++] = ptr; + } else { + iscsi_free(iscsi, ptr); } - iscsi->smalloc_ptrs[iscsi->smalloc_free++] = ptr; } struct iscsi_context * diff --git a/lib/libiscsi.def b/lib/libiscsi.def index 080c142..fa37fc2 100644 --- a/lib/libiscsi.def +++ b/lib/libiscsi.def @@ -76,6 +76,7 @@ iscsi_sanitize_crypto_erase_sync iscsi_sanitize_crypto_erase_task iscsi_sanitize_exit_failure_mode_sync iscsi_sanitize_exit_failure_mode_task +iscsi_set_cache_allocations iscsi_set_noautoreconnect iscsi_set_reconnect_max_retries iscsi_set_timeout diff --git a/lib/libiscsi.syms b/lib/libiscsi.syms index 471e06d..5ca3acf 100644 --- a/lib/libiscsi.syms +++ b/lib/libiscsi.syms @@ -74,6 +74,7 @@ iscsi_sanitize_crypto_erase_sync iscsi_sanitize_crypto_erase_task iscsi_sanitize_exit_failure_mode_sync iscsi_sanitize_exit_failure_mode_task +iscsi_set_cache_allocations iscsi_set_noautoreconnect iscsi_set_reconnect_max_retries iscsi_set_timeout diff --git a/test-tool/iscsi-test-cu.c b/test-tool/iscsi-test-cu.c index ae236b2..7aa90f1 100644 --- a/test-tool/iscsi-test-cu.c +++ b/test-tool/iscsi-test-cu.c @@ -971,6 +971,11 @@ main(int argc, char *argv[]) }; int i, c; int opt_idx = 0; + char *ca; + + ca = getenv("LIBISCSI_CACHE_ALLOCATIONS"); + if (ca && atoi(ca) == 0) + iscsi_set_cache_allocations(0); sd = malloc(sizeof(struct scsi_device)); memset(sd, '\0', sizeof(struct scsi_device));