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 <bart.vanassche@sandisk.com>
This commit is contained in:
Bart Van Assche
2015-04-20 09:56:35 +02:00
committed by Ronnie Sahlberg
parent d8de6531f8
commit cf4076dba9
5 changed files with 37 additions and 11 deletions

View File

@@ -51,6 +51,8 @@ struct sockaddr;
"<host>[:<port>]\""
EXTERN void iscsi_set_cache_allocations(int ca);
/*
* The following three functions are used to integrate libiscsi in an event
* system.

View File

@@ -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 *

View File

@@ -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

View File

@@ -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

View File

@@ -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));