From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [182.253.183.169]) by gnuweeb.org (Postfix) with ESMTPSA id C24538319D; Sun, 26 Feb 2023 16:03:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1677427424; bh=KGQkSC7uN8P5ShfcF5vnepkW95hzs86cm3yNydWOBj0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R6ld5PCKPWRl5RjCDcp2FJNpbcCCdOpbeexDCsQiRlWg9Dd559yFtlz35Be8FE+y9 Ypk19EReL1hhTwCICrxgwI/tb65rCwZRbbvZ3kpfE4Bs2MzJ/21JvzsHZerdvDHHG0 kdf1UL5KB9keGFhzVmGysuqNYF7VlreryhX6+J9ufrsn3/XlTqx8lf+3xKY8eTAUaN wQqk1nHXxNh3ja8g+EBUQJUsTqsfxpyocDQ/MahtwK1cpRoI54uBkv+NDxOHgXyGHh uOFw+OXISKSQ3HKIJNzciovBljZ3EE3HlrLqzcoDNMn6Py+ao1s71c2awvHBoWKysi aqej08/W5T5Uw== From: Ammar Faizi To: Chris Mason , Josef Bacik , David Sterba , Tejun Heo Cc: Ammar Faizi , Lai Jiangshan , Filipe Manana , Linux Btrfs Mailing List , Linux Kernel Mailing List , Linux Fsdevel Mailing List , GNU/Weeb Mailing List Subject: [RFC PATCH v1 5/6] btrfs: Adjust the default thread pool size when `wq_cpu_set` option is used Date: Sun, 26 Feb 2023 23:02:58 +0700 Message-Id: <20230226160259.18354-6-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230226160259.18354-1-ammarfaizi2@gnuweeb.org> References: <20230226160259.18354-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: If wq_cpu_set is specified, adjust thread_pool_size to the number of CPUs in the set plus 2 to avoid the case where the number of CPUs in the set is less than the default thread_pool_size + 2, which might cause the thread pool to be starved. The thread_pool=%u mount option overrides this adjusting behavior. Signed-off-by: Ammar Faizi --- fs/btrfs/super.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 3e061ec977b014d1..34b7c5810d34d624 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -317,6 +317,32 @@ static int parse_wq_cpu_set(struct btrfs_fs_info *info, const char *mask_str) return 0; } +static void adjust_default_thread_pool_size(struct btrfs_fs_info *info) +{ + unsigned long old_thread_pool_size; + unsigned long new_thread_pool_size; + unsigned long total_usable_cpu = 0; + unsigned long cpu; + + if (!btrfs_test_opt(info, WQ_CPU_SET)) + return; + + for_each_online_cpu(cpu) { + if (cpumask_test_cpu(cpu, info->wq_cpu_set->mask)) + total_usable_cpu++; + } + + old_thread_pool_size = info->thread_pool_size; + new_thread_pool_size = min_t(unsigned long, total_usable_cpu + 2, 8); + + if (old_thread_pool_size == new_thread_pool_size) + return; + + info->thread_pool_size = new_thread_pool_size; + btrfs_info(info, "adjusting thread_pool_size to %lu due to wq_cpu_set (you can override this with thread_pool=%%u option)", + new_thread_pool_size); +} + /* * Regular mount options parser. Everything that is needed only when * reading in a new superblock is parsed here. @@ -336,6 +362,7 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, bool saved_compress_force; int no_compress = 0; const bool remounting = test_bit(BTRFS_FS_STATE_REMOUNTING, &info->fs_state); + bool has_thread_pool_opt = false; if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); @@ -543,6 +570,7 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, goto out; } info->thread_pool_size = intarg; + has_thread_pool_opt = true; break; case Opt_max_inline: num = match_strdup(&args[0]); @@ -854,6 +882,16 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options, } if (!ret) ret = btrfs_check_mountopts_zoned(info); + + /* + * If wq_cpu_set is specified, adjust thread_pool_size to the number of + * CPUs in the set plus 2 to avoid the case where the number of CPUs in + * the set is less than the default thread_pool_size + 2, which might + * cause the thread pool to be starved. The thread_pool=%u mount option + * overrides this adjusting behavior. + */ + if (!ret && !has_thread_pool_opt) + adjust_default_thread_pool_size(info); if (!ret && !remounting) { if (btrfs_test_opt(info, SPACE_CACHE)) btrfs_info(info, "disk space caching is enabled"); -- Ammar Faizi