From da5be1c5a740e2c395c6960f2a1b844f874ed18a Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Tue, 5 Jan 2016 14:48:39 +0100 Subject: [PATCH] Reduce memory occupation for MT The copy buffer doesn't need a copy for each thread. --- cmdline/scrub.c | 4 ++-- cmdline/sync.c | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/cmdline/scrub.c b/cmdline/scrub.c index 800a3c1a..99d27e01 100644 --- a/cmdline/scrub.c +++ b/cmdline/scrub.c @@ -271,8 +271,8 @@ static int state_scrub_process(struct snapraid_state* state, struct snapraid_par /* rehash buffers */ rehandle = malloc_nofail_align(diskmax * sizeof(struct snapraid_rehash), &rehandle_alloc); - /* we need disk + 2 for each parity level buffers */ - buffermax = diskmax + state->level * 2; + /* we need 1 * data + 2 * parity */ + buffermax = diskmax + 2 * state->level; /* initialize the io threads */ io_init(&io, state, buffermax, scrub_data_reader, handle, diskmax, scrub_parity_reader, 0, parity_handle, state->level); diff --git a/cmdline/sync.c b/cmdline/sync.c index b3923cfd..f3cb58fe 100644 --- a/cmdline/sync.c +++ b/cmdline/sync.c @@ -660,6 +660,8 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari unsigned j; void* zero_alloc; void** zero; + void* copy_alloc; + void** copy; unsigned buffermax; data_off_t countsize; block_off_t countpos; @@ -689,13 +691,16 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari /* rehash buffers */ rehandle = malloc_nofail_align(diskmax * sizeof(struct snapraid_rehash), &rehandle_alloc); - /* we need 2 * data + 1 * parity */ - buffermax = 2 * diskmax + state->level; + /* we need 1 * data + 1 * parity */ + buffermax = diskmax + state->level; /* initialize the io threads */ io_init(&io, state, buffermax, sync_data_reader, handle, diskmax, 0, sync_parity_writer, parity_handle, state->level); - /* fill up the zero buffer */ + /* allocate the copy buffer */ + copy = malloc_nofail_vector_align(diskmax, diskmax, state->block_size, ©_alloc); + + /* allocate and fill the zero buffer */ zero = malloc_nofail_align(state->block_size, &zero_alloc); memset(zero, 0, state->block_size); raid_zero(zero); @@ -991,7 +996,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari failed_mac = 0; for (j = 0; j < failed_count; ++j) { unsigned char* block_buffer = buffer[failed[j].index]; - unsigned char* block_copy = buffer[diskmax + state->level + failed[j].index]; + unsigned char* block_copy = copy[failed[j].index]; unsigned block_state = block_state_get(failed[j].block); /* we try to recover only if at least one BLK is present */ @@ -1071,7 +1076,7 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari for (j = 0; j < failed_count; ++j) { unsigned char hash[HASH_SIZE]; unsigned char* block_buffer = buffer[failed[j].index]; - unsigned char* block_copy = buffer[diskmax + state->level + failed[j].index]; + unsigned char* block_copy = copy[failed[j].index]; unsigned block_state = block_state_get(failed[j].block); if (block_state == BLOCK_STATE_BLK) { @@ -1348,6 +1353,8 @@ bail: free(handle); free(zero_alloc); + free(copy_alloc); + free(copy); free(rehandle_alloc); free(failed); free(failed_map);