From f0a5268d32a829167cfbd68236c827d58380cbb3 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 22 May 2021 10:09:43 +0200 Subject: [PATCH] Avoid to use the filesystem mutex when it's not necessary This speed-up a bit the check and fix that are monothreads --- cmdline/elem.c | 20 ++++++++++++++++---- cmdline/elem.h | 8 +++++++- cmdline/io.c | 7 +++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/cmdline/elem.c b/cmdline/elem.c index a597c07d..228a3c81 100644 --- a/cmdline/elem.c +++ b/cmdline/elem.c @@ -736,7 +736,7 @@ int dir_name_compare(const void* void_arg, const void* void_data) return strcmp(arg, dir->sub); } -struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip) +struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip_access) { struct snapraid_disk* disk; @@ -750,6 +750,7 @@ struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev #if HAVE_PTHREAD thread_mutex_init(&disk->fs_mutex, 0); + disk->fs_mutex_enabled = 0; /* lock will be enabled at threads start */ #endif disk->smartctl[0] = 0; @@ -767,7 +768,7 @@ struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev disk->has_unsupported_uuid = *uuid == 0; /* empty UUID means unsupported */ disk->had_empty_uuid = 0; disk->mapping_idx = -1; - disk->skip_access = skip; + disk->skip_access = skip_access; tommy_list_init(&disk->filelist); tommy_list_init(&disk->deletedlist); tommy_hashdyn_init(&disk->inodeset); @@ -804,10 +805,20 @@ void disk_free(struct snapraid_disk* disk) free(disk); } +void disk_start_thread(struct snapraid_disk* disk) +{ +#if HAVE_PTHREAD + disk->fs_mutex_enabled = 1; +#else + (void)disk; +#endif +} + static inline void fs_lock(struct snapraid_disk* disk) { #if HAVE_PTHREAD - thread_mutex_lock(&disk->fs_mutex); + if (disk->fs_mutex_enabled) + thread_mutex_lock(&disk->fs_mutex); #else (void)disk; #endif @@ -816,7 +827,8 @@ static inline void fs_lock(struct snapraid_disk* disk) static inline void fs_unlock(struct snapraid_disk* disk) { #if HAVE_PTHREAD - thread_mutex_unlock(&disk->fs_mutex); + if (disk->fs_mutex_enabled) + thread_mutex_unlock(&disk->fs_mutex); #else (void)disk; #endif diff --git a/cmdline/elem.h b/cmdline/elem.h index 0c4a555a..a29a0b0c 100644 --- a/cmdline/elem.h +++ b/cmdline/elem.h @@ -368,6 +368,7 @@ struct snapraid_disk { * change during multithread processing. */ pthread_mutex_t fs_mutex; + int fs_mutex_enabled; /*< If the lock has to be used. */ #endif /** @@ -968,13 +969,18 @@ static inline tommy_uint32_t dir_name_hash(const char* name) /** * Allocate a disk. */ -struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip); +struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev, const char* uuid, int skip_access); /** * Deallocate a disk. */ void disk_free(struct snapraid_disk* disk); +/** + * Enable multithread support for the disk. + */ +void disk_start_thread(struct snapraid_disk* disk); + /** * Get the size of the disk in blocks. */ diff --git a/cmdline/io.c b/cmdline/io.c index 156d512f..8611002c 100644 --- a/cmdline/io.c +++ b/cmdline/io.c @@ -770,6 +770,13 @@ static void io_start_thread(struct snapraid_io* io, int (*block_is_enabled)(void* arg, block_off_t), void* blockarg) { unsigned i; + tommy_node* j; + + /* enable the filesystem mutex in all disks */ + for (j = io->state->disklist; j != 0; j = j->next) { + struct snapraid_disk* disk = j->data; + disk_start_thread(disk); + } io->block_start = blockstart; io->block_max = blockmax;