Remove the need of helgrind/drd suppression

Using a mutex for protectign the global memory counter
should not affect performance.
This commit is contained in:
Andrea Mazzoleni 2015-12-31 15:21:50 +01:00
parent b059457190
commit 7ad70c26f8
7 changed files with 45 additions and 24 deletions

View file

@ -227,6 +227,13 @@
#include <execinfo.h>
#endif
/**
* Enable thread use.
*/
#if HAVE_PTHREAD_H && HAVE_PTHREAD_CREATE
#define HAVE_PTHREAD 1
#endif
/**
* Disable case check in Windows.
*/
@ -419,7 +426,7 @@ struct devinfo_struct {
struct devinfo_struct* parent; /**< Pointer at the parent if any. */
uint64_t smart[SMART_COUNT]; /**< SMART raw attributes. */
char smart_serial[SMART_MAX]; /**< SMART serial number. */
#if HAVE_PTHREAD_CREATE
#if HAVE_PTHREAD
pthread_t thread;
#endif
tommy_node node;

View file

@ -76,7 +76,7 @@ void usage(void)
void memory(void)
{
log_tag("memory:used:%" PRIu64 "\n", (uint64_t)malloc_counter());
log_tag("memory:used:%" PRIu64 "\n", (uint64_t)malloc_counter_get());
/* size of the block */
log_tag("memory:block:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_block)));
@ -85,7 +85,7 @@ void memory(void)
log_tag("memory:link:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_link)));
log_tag("memory:dir:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_dir)));
msg_progress("Using %u MiB of memory.\n", (unsigned)(malloc_counter() / (1024 * 1024)));
msg_progress("Using %u MiB of memory.\n", (unsigned)(malloc_counter_get() / (1024 * 1024)));
}
/****************************************************************************/

View file

@ -42,7 +42,7 @@
* Multi thread for verify is instead always generally faster,
* so we enable it if possible.
*/
#if HAVE_PTHREAD_CREATE
#if HAVE_PTHREAD
/* #define HAVE_MT_WRITE 1 */
#define HAVE_MT_VERIFY 1
#endif

View file

@ -607,9 +607,35 @@ int fmtime(int f, int64_t mtime_sec, int mtime_nsec)
*/
static size_t mcounter;
size_t malloc_counter(void)
/**
* Mutex protection for the global memory counter.
*/
#if HAVE_PTHREAD
pthread_mutex_t mcounter_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
size_t malloc_counter_get(void)
{
return mcounter;
size_t ret;
#if HAVE_PTHREAD
pthread_mutex_lock(&mcounter_mutex);
#endif
ret = mcounter;
#if HAVE_PTHREAD
pthread_mutex_unlock(&mcounter_mutex);
#endif
return ret;
}
void malloc_counter_inc(size_t inc)
{
#if HAVE_PTHREAD
pthread_mutex_lock(&mcounter_mutex);
#endif
mcounter += inc;
#if HAVE_PTHREAD
pthread_mutex_unlock(&mcounter_mutex);
#endif
}
/* LCOV_EXCL_START */
@ -652,7 +678,7 @@ void malloc_fail(size_t size)
malloc_printn(f, size);
malloc_print(f, " bytes.\n");
malloc_print(f, "Already allocated ");
malloc_printn(f, malloc_counter());
malloc_printn(f, malloc_counter_get());
malloc_print(f, " bytes.\n");
if (sizeof(void*) == 4) {
malloc_print(f, "You are currently using a 32 bits executable.\n");
@ -682,7 +708,7 @@ void* malloc_nofail(size_t size)
memset(ptr, 0xA5, size);
#endif
mcounter += size;
malloc_counter_inc(size);
return ptr;
}
@ -705,7 +731,7 @@ void* calloc_nofail(size_t count, size_t size)
memset(ptr, 0, size);
mcounter += size;
malloc_counter_inc(size);
return ptr;
}
@ -728,7 +754,7 @@ char* strdup_nofail(const char* str)
memcpy(ptr, str, size);
mcounter += size;
malloc_counter_inc(size);
return ptr;
}

View file

@ -249,7 +249,7 @@ int fmtime(int f, int64_t mtime_sec, int mtime_nsec);
/**
* Return the size of the allocated memory.
*/
size_t malloc_counter(void);
size_t malloc_counter_get(void);
/**
* Safe malloc.

View file

@ -951,7 +951,7 @@ static int device_thread(tommy_list* list, void* (*func)(void* arg))
int fail = 0;
tommy_node* i;
#if HAVE_PTHREAD_CREATE
#if HAVE_PTHREAD
/* start all threads */
for (i = tommy_list_head(list); i != 0; i = i->next) {
devinfo_t* devinfo = i->data;

View file

@ -1,15 +1,3 @@
{
Global counter of allocated memory
Helgrind:Race
fun:malloc_nofail
}
{
Global counter of allocated memory
drd:ConflictingAccess
fun:malloc_nofail
}
{
printf reports false positves when printing more than one variables at time
exp-sgcheck:SorG