From: liubaolin <liubaolin12138@163.com>
To: Andreas Dilger <adilger@dilger.ca>
Cc: tytso@mit.edu, linux-ext4@vger.kernel.org,
linux-kernel@vger.kernel.org, wangguanyu@vivo.com,
Baolin Liu <liubaolin@kylinos.cn>
Subject: Re: [PATCH v1] ext4: add mb_stats_clear for mballoc statistics
Date: Thu, 16 Apr 2026 15:11:45 +0800 [thread overview]
Message-ID: <15dddf71-1f8c-45eb-9333-fb2e9174004d@163.com> (raw)
In-Reply-To: <C713AA9C-D640-4C5B-98BF-A5D88AE3B2A0@dilger.ca>
> Dear Andreas,
> Alright, thank you for your review.
> I will revise the patch according to your suggestions and submit a second version as soon as possible.
>
> Regards,
> Baolin
在 2026/4/16 9:14, Andreas Dilger 写道:
> On Apr 14, 2026, at 04:02, Baolin Liu <liubaolin12138@163.com> wrote:
>>
>> From: Baolin Liu <liubaolin@kylinos.cn>
>>
>> Add a write-only mb_stats_clear sysfs knob to reset ext4 mballoc
>> runtime statistics. This makes it easier to inspect allocator
>> activity for a specific workload instead of using counters
>> accumulated since mount.
>
> Rather than having a read-only "mb_stats" procfs file and a separate
> write-only "mb_stats_clear" sysfs file to clear "mb_stats", IMHO it
> would be more obvious to write directly to "/proc/fs/ext4/DEV/mb_stats"
> file to clear it. Writing "0" would be logical to zero out the stats.
>
> Cheers, Andreas
>
>>
>> Signed-off-by: Baolin Liu <liubaolin@kylinos.cn>
>> ---
>> fs/ext4/ext4.h | 1 +
>> fs/ext4/mballoc.c | 31 +++++++++++++++++++++++++++++++
>> fs/ext4/sysfs.c | 24 ++++++++++++++++++++++++
>> 3 files changed, 56 insertions(+)
>>
>> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
>> index 7617e2d454ea..3a32e1a515dd 100644
>> --- a/fs/ext4/ext4.h
>> +++ b/fs/ext4/ext4.h
>> @@ -2995,6 +2995,7 @@ int ext4_fc_record_regions(struct super_block *sb, int ino,
>> extern const struct seq_operations ext4_mb_seq_groups_ops;
>> extern const struct seq_operations ext4_mb_seq_structs_summary_ops;
>> extern int ext4_seq_mb_stats_show(struct seq_file *seq, void *offset);
>> +extern void ext4_mb_stats_clear(struct ext4_sb_info *sbi);
>> extern int ext4_mb_init(struct super_block *);
>> extern void ext4_mb_release(struct super_block *);
>> extern ext4_fsblk_t ext4_mb_new_blocks(handle_t *,
>> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
>> index bb58eafb87bc..382c91586b26 100644
>> --- a/fs/ext4/mballoc.c
>> +++ b/fs/ext4/mballoc.c
>> @@ -3219,6 +3219,8 @@ int ext4_seq_mb_stats_show(struct seq_file *seq, void *offset)
>> }
>> seq_printf(seq, "\treqs: %u\n", atomic_read(&sbi->s_bal_reqs));
>> seq_printf(seq, "\tsuccess: %u\n", atomic_read(&sbi->s_bal_success));
>> + seq_printf(seq, "\tblocks_allocated: %u\n",
>> + atomic_read(&sbi->s_bal_allocated));
>>
>> seq_printf(seq, "\tgroups_scanned: %u\n",
>> atomic_read(&sbi->s_bal_groups_scanned));
>> @@ -4721,6 +4723,35 @@ static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
>> trace_ext4_mballoc_prealloc(ac);
>> }
>>
>> +void ext4_mb_stats_clear(struct ext4_sb_info *sbi)
>> +{
>> + int i;
>> +
>> + atomic_set(&sbi->s_bal_reqs, 0);
>> + atomic_set(&sbi->s_bal_success, 0);
>> + atomic_set(&sbi->s_bal_allocated, 0);
>> + atomic_set(&sbi->s_bal_groups_scanned, 0);
>> +
>> + for (i = 0; i < EXT4_MB_NUM_CRS; i++) {
>> + atomic64_set(&sbi->s_bal_cX_hits[i], 0);
>> + atomic64_set(&sbi->s_bal_cX_groups_considered[i], 0);
>> + atomic_set(&sbi->s_bal_cX_ex_scanned[i], 0);
>> + atomic64_set(&sbi->s_bal_cX_failed[i], 0);
>> + }
>> +
>> + atomic_set(&sbi->s_bal_ex_scanned, 0);
>> + atomic_set(&sbi->s_bal_goals, 0);
>> + atomic_set(&sbi->s_bal_stream_goals, 0);
>> + atomic_set(&sbi->s_bal_len_goals, 0);
>> + atomic_set(&sbi->s_bal_2orders, 0);
>> + atomic_set(&sbi->s_bal_breaks, 0);
>> + atomic_set(&sbi->s_mb_lost_chunks, 0);
>> + atomic_set(&sbi->s_mb_buddies_generated, 0);
>> + atomic64_set(&sbi->s_mb_generation_time, 0);
>> + atomic_set(&sbi->s_mb_preallocated, 0);
>> + atomic_set(&sbi->s_mb_discarded, 0);
>> +}
>> +
>> /*
>> * Called on failure; free up any blocks from the inode PA for this
>> * context. We don't need this for MB_GROUP_PA because we only change
>> diff --git a/fs/ext4/sysfs.c b/fs/ext4/sysfs.c
>> index 923b375e017f..a5bd88a99f22 100644
>> --- a/fs/ext4/sysfs.c
>> +++ b/fs/ext4/sysfs.c
>> @@ -41,6 +41,7 @@ typedef enum {
>> attr_pointer_atomic,
>> attr_journal_task,
>> attr_err_report_sec,
>> + attr_mb_stats_clear,
>> } attr_id_t;
>>
>> typedef enum {
>> @@ -161,6 +162,25 @@ static ssize_t err_report_sec_store(struct ext4_sb_info *sbi,
>> return count;
>> }
>>
>> +static ssize_t mb_stats_clear_store(struct ext4_sb_info *sbi,
>> + const char *buf, size_t count)
>> +{
>> + int val;
>> + int ret;
>> +
>> + if (!capable(CAP_SYS_ADMIN))
>> + return -EPERM;
>> +
>> + ret = kstrtoint(skip_spaces(buf), 0, &val);
>> + if (ret)
>> + return ret;
>> + if (val != 1)
>> + return -EINVAL;
>> +
>> + ext4_mb_stats_clear(sbi);
>> + return count;
>> +}
>> +
>> static ssize_t journal_task_show(struct ext4_sb_info *sbi, char *buf)
>> {
>> if (!sbi->s_journal)
>> @@ -251,6 +271,7 @@ EXT4_ATTR_OFFSET(mb_best_avail_max_trim_order, 0644, mb_order,
>> EXT4_ATTR_OFFSET(err_report_sec, 0644, err_report_sec, ext4_sb_info, s_err_report_sec);
>> EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
>> EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
>> +EXT4_ATTR(mb_stats_clear, 0200, mb_stats_clear);
>> EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
>> EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
>> EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
>> @@ -301,6 +322,7 @@ static struct attribute *ext4_attrs[] = {
>> ATTR_LIST(inode_readahead_blks),
>> ATTR_LIST(inode_goal),
>> ATTR_LIST(mb_stats),
>> + ATTR_LIST(mb_stats_clear),
>> ATTR_LIST(mb_max_to_scan),
>> ATTR_LIST(mb_min_to_scan),
>> ATTR_LIST(mb_order2_req),
>> @@ -561,6 +583,8 @@ static ssize_t ext4_attr_store(struct kobject *kobj,
>> return trigger_test_error(sbi, buf, len);
>> case attr_err_report_sec:
>> return err_report_sec_store(sbi, buf, len);
>> + case attr_mb_stats_clear:
>> + return mb_stats_clear_store(sbi, buf, len);
>> default:
>> return ext4_generic_attr_store(a, sbi, buf, len);
>> }
>> --
>> 2.51.0
>>
>
>
> Cheers, Andreas
>
>
>
>
next prev parent reply other threads:[~2026-04-16 7:12 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-14 10:02 [PATCH v1] ext4: add mb_stats_clear for mballoc statistics Baolin Liu
2026-04-14 10:07 ` liubaolin
2026-04-15 19:26 ` Ojaswin Mujoo
2026-04-16 7:07 ` liubaolin
2026-04-16 1:14 ` Andreas Dilger
2026-04-16 7:11 ` liubaolin [this message]
2026-04-16 1:44 ` Ritesh Harjani
2026-04-16 7:16 ` liubaolin
2026-04-16 8:53 ` Zhang Yi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=15dddf71-1f8c-45eb-9333-fb2e9174004d@163.com \
--to=liubaolin12138@163.com \
--cc=adilger@dilger.ca \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=liubaolin@kylinos.cn \
--cc=tytso@mit.edu \
--cc=wangguanyu@vivo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox