Whamcloud - gitweb
e2fsck: adjust number of threads
authorWang Shilong <wshilong@ddn.com>
Wed, 9 Sep 2020 07:07:10 +0000 (15:07 +0800)
committerLi Dongyang <dongyangli@ddn.com>
Fri, 28 Apr 2023 00:45:10 +0000 (10:45 +1000)
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 <wshilong@ddn.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
e2fsck/pass1.c
lib/ext2fs/ext2fs.h

index 96cc10b..6d56157 100644 (file)
@@ -1293,6 +1293,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
 
@@ -2620,14 +2621,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);
@@ -2674,16 +2675,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);
@@ -3199,12 +3193,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;
@@ -3286,7 +3281,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;
@@ -3295,6 +3290,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};
@@ -3318,6 +3315,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;
@@ -3325,7 +3323,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"));
@@ -3355,7 +3354,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;
@@ -3365,17 +3364,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"));
index fdc8f5b..7a3924e 100644 (file)
@@ -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.
@@ -2217,6 +2218,35 @@ ext2fs_orphan_block_tail(ext2_filsys fs, char *buf)
                sizeof(struct ext4_orphan_block_tail));
 }
 
+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