From c184f5c91ebfd3be985b9700585025bac31e809d Mon Sep 17 00:00:00 2001 From: Wang Shilong Date: Wed, 9 Sep 2020 15:07:10 +0800 Subject: [PATCH] e2fsck: adjust number of threads number of threads should not exceed flex bg numbers, and output messages if we adjust threads number. E2fsprogs-commit: 56070582ad951aa55b6eeecd7e8e2a19d53bbff6 Change-Id: I8bc87e6ca99205d7fc9e0c9f35d0ba9c3eed38ca Signed-off-by: Wang Shilong Reviewed-by: Andreas Dilger Signed-off-by: Theodore Ts'o --- e2fsck/pass1.c | 32 +++++++++++++++----------------- lib/ext2fs/ext2fs.h | 32 +++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/e2fsck/pass1.c b/e2fsck/pass1.c index 1b927ba..808476d 100644 --- a/e2fsck/pass1.c +++ b/e2fsck/pass1.c @@ -1287,6 +1287,7 @@ static void e2fsck_pass1_set_thread_num(e2fsck_t ctx) } out: ctx->fs_num_threads = num_threads; + ctx->fs->fs_num_threads = num_threads; } #endif @@ -2587,14 +2588,14 @@ static void e2fsck_pass1_merge_invalid_bitmaps(e2fsck_t global_ctx, } static errcode_t e2fsck_pass1_thread_prepare(e2fsck_t global_ctx, e2fsck_t *thread_ctx, - int thread_index, int num_threads) + int thread_index, int num_threads, + dgrp_t average_group) { errcode_t retval; e2fsck_t thread_context; ext2_filsys thread_fs; ext2_filsys global_fs = global_ctx->fs; struct e2fsck_thread *tinfo; - dgrp_t average_group; assert(global_ctx->inode_used_map == NULL); assert(global_ctx->inode_dir_map == NULL); @@ -2641,16 +2642,9 @@ static errcode_t e2fsck_pass1_thread_prepare(e2fsck_t global_ctx, e2fsck_t *thre thread_context->thread_info.et_thread_index = thread_index; set_up_logging(thread_context); - /* - * Distribute work to multiple threads: - * Each thread work on fs->group_desc_count / nthread groups. - */ tinfo = &thread_context->thread_info; - average_group = thread_fs->group_desc_count / num_threads; - if (average_group == 0) - average_group = 1; tinfo->et_group_start = average_group * thread_index; - if (thread_index == num_threads - 1) + if (thread_index == global_fs->fs_num_threads - 1) tinfo->et_group_end = thread_fs->group_desc_count; else tinfo->et_group_end = average_group * (thread_index + 1); @@ -3166,12 +3160,13 @@ static int e2fsck_pass1_thread_join(e2fsck_t global_ctx, e2fsck_t thread_ctx) } static int e2fsck_pass1_threads_join(struct e2fsck_thread_info *infos, - int num_threads, e2fsck_t global_ctx) + e2fsck_t global_ctx) { errcode_t rc; errcode_t ret = 0; int i; struct e2fsck_thread_info *pinfo; + int num_threads = global_ctx->fs_num_threads; /* merge invalid bitmaps will recalculate it */ global_ctx->invalid_bitmaps = 0; @@ -3253,7 +3248,7 @@ out: } static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo, - int num_threads, e2fsck_t global_ctx) + e2fsck_t global_ctx) { struct e2fsck_thread_info *infos; pthread_attr_t attr; @@ -3262,6 +3257,8 @@ static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo, struct e2fsck_thread_info *tmp_pinfo; int i; e2fsck_t thread_ctx; + dgrp_t average_group; + int num_threads = global_ctx->fs_num_threads; #ifdef DEBUG_THREADS struct e2fsck_thread_debug thread_debug = {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0}; @@ -3285,6 +3282,7 @@ static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo, return retval; } + average_group = ext2fs_get_avg_group(global_ctx->fs); for (i = 0; i < num_threads; i++) { tmp_pinfo = &infos[i]; tmp_pinfo->eti_thread_index = i; @@ -3292,7 +3290,8 @@ static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo, tmp_pinfo->eti_debug = &thread_debug; #endif retval = e2fsck_pass1_thread_prepare(global_ctx, &thread_ctx, - i, num_threads); + i, num_threads, + average_group); if (retval) { com_err(global_ctx->program_name, retval, _("while preparing pass1 thread\n")); @@ -3322,7 +3321,7 @@ static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo, } if (retval) { - e2fsck_pass1_threads_join(infos, num_threads, global_ctx); + e2fsck_pass1_threads_join(infos, global_ctx); return retval; } *pinfo = infos; @@ -3332,17 +3331,16 @@ static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo, static void e2fsck_pass1_multithread(e2fsck_t global_ctx) { struct e2fsck_thread_info *infos = NULL; - int num_threads = global_ctx->fs_num_threads; errcode_t retval; - retval = e2fsck_pass1_threads_start(&infos, num_threads, global_ctx); + retval = e2fsck_pass1_threads_start(&infos, global_ctx); if (retval) { com_err(global_ctx->program_name, retval, _("while starting pass1 threads\n")); goto out_abort; } - retval = e2fsck_pass1_threads_join(infos, num_threads, global_ctx); + retval = e2fsck_pass1_threads_join(infos, global_ctx); if (retval) { com_err(global_ctx->program_name, retval, _("while joining pass1 threads\n")); diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h index 127170e..416a79d 100644 --- a/lib/ext2fs/ext2fs.h +++ b/lib/ext2fs/ext2fs.h @@ -275,10 +275,11 @@ struct struct_ext2_filsys { int cluster_ratio_bits; __u16 default_bitmap_type; __u16 pad; + __u32 fs_num_threads; /* * Reserved for future expansion */ - __u32 reserved[5]; + __u32 reserved[4]; /* * Reserved for the use of the calling application. @@ -2178,6 +2179,35 @@ ext2fs_const_inode(const struct ext2_inode_large * large_inode) return (const struct ext2_inode *) large_inode; } +static dgrp_t ext2fs_get_avg_group(ext2_filsys fs) +{ +#ifdef HAVE_PTHREAD + dgrp_t average_group; + unsigned flexbg_size; + + if (fs->fs_num_threads <= 1) + return fs->group_desc_count; + + average_group = fs->group_desc_count / fs->fs_num_threads; + if (average_group <= 1) + return 1; + + if (ext2fs_has_feature_flex_bg(fs->super)) { + int times = 1; + + flexbg_size = 1 << fs->super->s_log_groups_per_flex; + if (average_group % flexbg_size) { + times = average_group / flexbg_size; + average_group = times * flexbg_size; + } + } + + return average_group; +#else + return fs->group_desc_count; +#endif +} + #undef _INLINE_ #endif -- 1.8.3.1