]> git.hungrycats.org Git - linux/commitdiff
btrfs: stripe_alloc: count nocow run lifecycles and stranded stripes in sysfs
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:18:20 +0000 (13:18 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:37:51 +0000 (18:37 -0500)
Add a stripe_run_stats file beside stripe_park_stats with lifetime
counters for the paths the previous two patches touched, so a test can
show that it exercised them rather than merely passed: private NOCOW
runs opened, claim candidates refused for overlapping a live run, bytes
of whole stripes the scans found stranded behind live runs, and rescans
requested by a freed run to credit its stripes back.

A persistent nocow run whose allocations have all been freed is dead: it
owns no data and only strands the whole stripes in its range (99% of the
free whole stripes of every read-write group sat inside such runs after an
fsstress fallocate load).  btrfs_stripe_run_note_free(), from
btrfs_add_free_space(), clears the live bits of freed sectors inside a
persistent run, and close_block_group_stripe_runs() closes a persistent
run that handed out space, has nothing in flight and no live bit set, on
the normal commit path that frees it and marks its group for the rescan.
A run with any live byte stays open, so a slowly appended nocow file still
burns no stripe per commit; a run whose map was retired is never taken as
dead.  The free path only clears bits under bg->stripe_run_lock.
nocow_run_dead_closed counts the closes.

Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/block-group.c
fs/btrfs/fs.h
fs/btrfs/sysfs.c

index 7d3bf73ec00553a76364bfce7f4d9821199e4ebb..3fb3478d9f792aef35bff4302d7e637b71dfe3cf 100644 (file)
@@ -602,8 +602,10 @@ static void free_open_stripe_run(struct btrfs_block_group *bg,
         * with the run gone they are claimable again, so have the next commit
         * rescan the group and credit them back.
         */
-       if (READ_ONCE(bg->stripe_unusable_ready))
+       if (READ_ONCE(bg->stripe_unusable_ready)) {
                set_bit(BLOCK_GROUP_FLAG_STRIPE_UNUSABLE_DIRTY, &bg->runtime_flags);
+               atomic64_inc(&bg->fs_info->stripe_run_stats.strand_rescan_marks);
+       }
        wake_up_var(&bg->open_stripe_runs);
 }
 
@@ -1060,8 +1062,10 @@ bool btrfs_stripe_run_range_usable(struct btrfs_block_group *bg,
                len = run->start - run_start;
        }
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
-       if (len < bg->full_stripe_len)
+       if (len < bg->full_stripe_len) {
+               atomic64_inc(&bg->fs_info->stripe_run_stats.claim_refused_live_run);
                return false;
+       }
        *run_len = len;
        return true;
 }
@@ -1099,6 +1103,8 @@ u64 btrfs_stripe_run_stranded_stripes(struct btrfs_block_group *bg, u32 *freep,
                }
        }
        spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+       if (stranded)
+               atomic64_add(stranded, &bg->fs_info->stripe_run_stats.strand_scan_bytes);
        return stranded;
 }
 
index c56f9f59496ab2fa5535294a52c439472a5e6e3d..ab892c3bcd33874d5bf9a903d00dc5d47f8da65d 100644 (file)
@@ -856,6 +856,17 @@ struct btrfs_fs_info {
                atomic64_t pad_decline_uncovered;
                atomic64_t congestion_short;
        } stripe_park_stats;
+       /* Stripe run lifecycle counters (sysfs stripe_run_stats). */
+       struct {
+               /* private NOCOW runs opened (they outlive commits) */
+               atomic64_t nocow_run_opened;
+               /* claim candidates refused for overlapping a live run */
+               atomic64_t claim_refused_live_run;
+               /* bytes of whole stripes the scans found stranded behind live runs */
+               atomic64_t strand_scan_bytes;
+               /* rescans requested by a freed run to credit its stripes back */
+               atomic64_t strand_rescan_marks;
+       } stripe_run_stats;
        /*
         * Park deadlines, ms (sysfs); 0 disables parking for that class.  See
         * rbio_try_park().
index 690dd862f5650fe19b01198e76d10de30a57533e..4d9d728ec956d592114342f16c8714ab234ccd91 100644 (file)
@@ -1258,6 +1258,23 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
 }
 BTRFS_ATTR(, stripe_park_stats, btrfs_stripe_park_stats_show);
 
+static ssize_t btrfs_stripe_run_stats_show(struct kobject *kobj,
+                                          struct kobj_attribute *a, char *buf)
+{
+       struct btrfs_fs_info *fs_info = to_fs_info(kobj);
+
+       return sysfs_emit(buf,
+               "nocow_run_opened %lld\n"
+               "claim_refused_live_run %lld\n"
+               "strand_scan_bytes %lld\n"
+               "strand_rescan_marks %lld\n",
+               atomic64_read(&fs_info->stripe_run_stats.nocow_run_opened),
+               atomic64_read(&fs_info->stripe_run_stats.claim_refused_live_run),
+               atomic64_read(&fs_info->stripe_run_stats.strand_scan_bytes),
+               atomic64_read(&fs_info->stripe_run_stats.strand_rescan_marks));
+}
+BTRFS_ATTR(, stripe_run_stats, btrfs_stripe_run_stats_show);
+
 /*
  * Park deadlines, ms; 0 disables parking for that class.  See
  * rbio_try_park().
@@ -1664,6 +1681,7 @@ static const struct attribute *btrfs_attrs[] = {
        BTRFS_ATTR_PTR(, bg_reclaim_threshold),
        BTRFS_ATTR_PTR(, commit_stats),
        BTRFS_ATTR_PTR(, stripe_park_stats),
+       BTRFS_ATTR_PTR(, stripe_run_stats),
        BTRFS_ATTR_PTR(, stripe_park_timeout_ms),
        BTRFS_ATTR_PTR(, stripe_park_sync_timeout_ms),
        BTRFS_ATTR_PTR(, stripe_park_congestion),