Skip to contentSkip to conversation

btrfs: sysfs: show discard stats and tunables in non-debug build

PATCH Superseded David Sterba 4 participants 6 messages Jul 27, 2022 – Jul 29, 2022 Btrfs file system

lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

PATCH 1/1 btrfs: sysfs: show discard stats and tunables in non-debug build

When discard=async was introduced there were also sysfs knobs and stats for debugging and tuning, hidden under CONFIG_BTRFS_DEBUG. The defaults have been set and so far seem to satisfy all users on a range of workloads. As there are not only tunables (like iops or kbps) but also stats tracking amount of discardable bytes, that should be available when the async discard is on (otherwise it's not).

The stats are moved from the per-fs debug directory, so it's under
/sys/fs/btrfs/FSID/discard

- discard_bitmap_bytes - amount of discarded bytes from data tracked as bitmaps
- discard_extent_bytes - dtto but as extents
- discard_bytes_saved -
- discardable_bytes - amount of bytes that can be discarded
- discardable_extents - number of extents to be discarded
- iops_limit - tunable limit of number of discard IOs to be issued
- kbps_limit - tunable limit of kilobytes per second issued as discard IO
- max_discard_size - tunable limit for size of one IO discard request

---
 fs/btrfs/ctree.h |  2 +-
 fs/btrfs/sysfs.c | 36 ++++++++++++++++--------------------
 2 files changed, 17 insertions(+), 21 deletions(-)
fs/btrfs/ctree.h +1 -1
index 4db85b9dc7ed..9631059d2733 100644
@@ -891,6 +891,7 @@ struct btrfs_fs_info {
 
 	struct kobject *space_info_kobj;
 	struct kobject *qgroups_kobj;
+	struct kobject *discard_kobj;
 
 	/* used to keep from writing metadata until there is a nice batch */
 	struct percpu_counter dirty_metadata_bytes;
@@ -1102,7 +1103,6 @@ struct btrfs_fs_info {
 
 #ifdef CONFIG_BTRFS_DEBUG
 	struct kobject *debug_kobj;
-	struct kobject *discard_debug_kobj;
 	struct list_head allocated_roots;
 
 	spinlock_t eb_leak_lock;
fs/btrfs/sysfs.c +16 -20
index d5d0717fd09a..95782f3b55da 100644
@@ -35,12 +35,12 @@
  * qgroup_attrs				/sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
  * space_info_attrs			/sys/fs/btrfs/<uuid>/allocation/<bg-type>
  * raid_attrs				/sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
+ * discard_attrs			/sys/fs/btrfs/<uuid>/discard
  *
  * When built with BTRFS_CONFIG_DEBUG:
  *
  * btrfs_debug_feature_attrs		/sys/fs/btrfs/debug
  * btrfs_debug_mount_attrs		/sys/fs/btrfs/<uuid>/debug
- * discard_debug_attrs			/sys/fs/btrfs/<uuid>/debug/discard
  */
 
 struct btrfs_feature_attr {
@@ -429,12 +429,10 @@ static const struct attribute_group btrfs_static_feature_attr_group = {
 	.attrs = btrfs_supported_static_feature_attrs,
 };
 
-#ifdef CONFIG_BTRFS_DEBUG
-
 /*
  * Discard statistics and tunables
  */
-#define discard_to_fs_info(_kobj)	to_fs_info((_kobj)->parent->parent)
+#define discard_to_fs_info(_kobj)	to_fs_info(get_btrfs_kobj(_kobj))
 
 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
 					    struct kobj_attribute *a,
@@ -583,11 +581,11 @@ BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
 	      btrfs_discard_max_discard_size_store);
 
 /*
- * Per-filesystem debugging of discard (when mounted with discard=async).
+ * Per-filesystem stats for discard (when mounted with discard=async).
  *
- * Path: /sys/fs/btrfs/<uuid>/debug/discard/
+ * Path: /sys/fs/btrfs/<uuid>/discard/
  */
-static const struct attribute *discard_debug_attrs[] = {
+static const struct attribute *discard_attrs[] = {
 	BTRFS_ATTR_PTR(discard, discardable_bytes),
 	BTRFS_ATTR_PTR(discard, discardable_extents),
 	BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
@@ -599,6 +597,8 @@ static const struct attribute *discard_debug_attrs[] = {
 	NULL,
 };
 
+#ifdef CONFIG_BTRFS_DEBUG
+
 /*
  * Per-filesystem runtime debugging exported via sysfs.
  *
@@ -1427,19 +1427,17 @@ void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
 		kobject_del(fs_info->space_info_kobj);
 		kobject_put(fs_info->space_info_kobj);
 	}
-#ifdef CONFIG_BTRFS_DEBUG
-	if (fs_info->discard_debug_kobj) {
-		sysfs_remove_files(fs_info->discard_debug_kobj,
-				   discard_debug_attrs);
-		kobject_del(fs_info->discard_debug_kobj);
-		kobject_put(fs_info->discard_debug_kobj);
+	if (fs_info->discard_kobj) {
+		sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
+		kobject_del(fs_info->discard_kobj);
+		kobject_put(fs_info->discard_kobj);
 	}
 	if (fs_info->debug_kobj) {
 		sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
 		kobject_del(fs_info->debug_kobj);
 		kobject_put(fs_info->debug_kobj);
 	}
-#endif
+
 	addrm_unknown_feature_attrs(fs_info, false);
 	sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
 	sysfs_remove_files(fsid_kobj, btrfs_attrs);
@@ -2001,20 +1999,18 @@ int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
 	error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
 	if (error)
 		goto failure;
+#endif
 
 	/* Discard directory */
-	fs_info->discard_debug_kobj = kobject_create_and_add("discard",
-						     fs_info->debug_kobj);
-	if (!fs_info->discard_debug_kobj) {
+	fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
+	if (!fs_info->discard_kobj) {
 		error = -ENOMEM;
 		goto failure;
 	}
 
-	error = sysfs_create_files(fs_info->discard_debug_kobj,
-				   discard_debug_attrs);
+	error = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
 	if (error)
 		goto failure;
-#endif
 
 	error = addrm_unknown_feature_attrs(fs_info, true);
 	if (error)
Signature · 1 line
2.36.1
#1 lore raw

fs/btrfs/ctree.h struct btrfs_fs_info { · line 1105

Quoted 8 lines
fs/btrfs/ctree.h +0 -0
@@ -1102,7 +1103,6 @@ struct btrfs_fs_info {
 
  #ifdef CONFIG_BTRFS_DEBUG
  	struct kobject *debug_kobj;

debug_kobj is defined inside the CONFIG_BTRFS_DEBUG;;

Quoted 7 lines
fs/btrfs/sysfs.c +0 -2
-	struct kobject *discard_debug_kobj;
  	struct list_head allocated_roots;
 
  	spinlock_t eb_leak_lock;
 
 
-#ifdef CONFIG_BTRFS_DEBUG

fs/btrfs/sysfs.c void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)

Quoted 10 lines
fs/btrfs/sysfs.c +4 -5
-	if (fs_info->discard_debug_kobj) {
-		sysfs_remove_files(fs_info->discard_debug_kobj,
-				   discard_debug_attrs);
-		kobject_del(fs_info->discard_debug_kobj);
-		kobject_put(fs_info->discard_debug_kobj);
+	if (fs_info->discard_kobj) {
+		sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
+		kobject_del(fs_info->discard_kobj);
+		kobject_put(fs_info->discard_kobj);
  	} >   	if (fs_info->debug_kobj) {

but here it got accessed outside of CONFIG_BTRFS_DEBUG define.

fs/btrfs/sysfs.c void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) · line 1440

Quoted 6 lines
  		sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
  		kobject_del(fs_info->debug_kobj);
  		kobject_put(fs_info->debug_kobj);
  	}
fs/btrfs/sysfs.c +1 -1
-#endif
+

I wonder if compile will fail? or if diff showing it wrongly? Sorry I didn't try compile the patch without CONFIG_BTRFS_DEBUG defined.

#2 lore raw
Quoted 31 lines · Anand Jain

fs/btrfs/ctree.h struct btrfs_fs_info { · line 1105

Quoted 8 lines
fs/btrfs/ctree.h +0 -0
@@ -1102,7 +1103,6 @@ struct btrfs_fs_info {
 
  #ifdef CONFIG_BTRFS_DEBUG
  	struct kobject *debug_kobj;

debug_kobj is defined inside the CONFIG_BTRFS_DEBUG;;

Quoted 7 lines
fs/btrfs/sysfs.c +0 -2
-	struct kobject *discard_debug_kobj;
  	struct list_head allocated_roots;
 
  	spinlock_t eb_leak_lock;
 
 
-#ifdef CONFIG_BTRFS_DEBUG

fs/btrfs/sysfs.c void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)

Quoted 10 lines
fs/btrfs/sysfs.c +4 -5
-	if (fs_info->discard_debug_kobj) {
-		sysfs_remove_files(fs_info->discard_debug_kobj,
-				   discard_debug_attrs);
-		kobject_del(fs_info->discard_debug_kobj);
-		kobject_put(fs_info->discard_debug_kobj);
+	if (fs_info->discard_kobj) {
+		sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
+		kobject_del(fs_info->discard_kobj);
+		kobject_put(fs_info->discard_kobj);
  	} >   	if (fs_info->debug_kobj) {

but here it got accessed outside of CONFIG_BTRFS_DEBUG define.

Quoted 10 lines

fs/btrfs/sysfs.c void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info) · line 1440

Quoted 6 lines
  		sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
  		kobject_del(fs_info->debug_kobj);
  		kobject_put(fs_info->debug_kobj);
  	}
fs/btrfs/sysfs.c +1 -1
-#endif
+

I wonder if compile will fail? or if diff showing it wrongly? Sorry I didn't try compile the patch without CONFIG_BTRFS_DEBUG defined.

Yes it fails, now fixed, thanks.

#3 lore raw
In reply to David Sterba
I love your patch! Yet something to improve:

Hi David,

I love your patch! Yet something to improve:

[auto build test ERROR on kdave/for-next]
[also build test ERROR on linus/master v5.19-rc8 next-20220727] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Sterba/btrfs-sysfs-show-discard-stats-and-tunables-in-non-debug-build/20220728-025841
base:   https://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux.git for-next
config: arc-randconfig-r043-20220728 (https://download.01.org/0day-ci/archive/20220728/[email protected]/config)
compiler: arc-elf-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/7fe92f8e456bf61b528a969be287e7bfda2bb03d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review David-Sterba/btrfs-sysfs-show-discard-stats-and-tunables-in-non-debug-build/20220728-025841 git checkout 7fe92f8e456bf61b528a969be287e7bfda2bb03d
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash fs/

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

fs/btrfs/sysfs.c: In function 'btrfs_sysfs_remove_mounted':

>> fs/btrfs/sysfs.c:1435:20: error: 'struct btrfs_fs_info' has no member named 'debug_kobj'
    1435 |         if (fs_info->debug_kobj) {
         |                    ^~
   fs/btrfs/sysfs.c:1436:43: error: 'struct btrfs_fs_info' has no member named 'debug_kobj'
    1436 |                 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
         |                                           ^~
>> fs/btrfs/sysfs.c:1436:57: error: 'btrfs_debug_mount_attrs' undeclared (first use in this function)
    1436 |                 sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
         |                                                         ^~~~~~~~~~~~~~~~~~~~~~~
   fs/btrfs/sysfs.c:1436:57: note: each undeclared identifier is reported only once for each function it appears in
   fs/btrfs/sysfs.c:1437:36: error: 'struct btrfs_fs_info' has no member named 'debug_kobj'
    1437 |                 kobject_del(fs_info->debug_kobj);
         |                                    ^~
   fs/btrfs/sysfs.c:1438:36: error: 'struct btrfs_fs_info' has no member named 'debug_kobj'
    1438 |                 kobject_put(fs_info->debug_kobj);
         |                                    ^~

vim +1435 fs/btrfs/sysfs.c

53f8a74cbeffd45 Anand Jain      2020-09-05  1418  
6618a59bfc0a049 Anand Jain      2015-08-14  1419  void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
5ac1d209f11271f Jeff Mahoney    2013-11-01  1420  {
3092c68fc58c7bd Nikolay Borisov 2020-07-03  1421  	struct kobject *fsid_kobj = &fs_info->fs_devices->fsid_kobj;
3092c68fc58c7bd Nikolay Borisov 2020-07-03  1422  
3092c68fc58c7bd Nikolay Borisov 2020-07-03  1423  	sysfs_remove_link(fsid_kobj, "bdi");
3092c68fc58c7bd Nikolay Borisov 2020-07-03  1424  
e453d989e0bb33d Jeff Mahoney    2013-11-21  1425  	if (fs_info->space_info_kobj) {
6ab0a2029ceaedb Jeff Mahoney    2013-11-01  1426  		sysfs_remove_files(fs_info->space_info_kobj, allocation_attrs);
6ab0a2029ceaedb Jeff Mahoney    2013-11-01  1427  		kobject_del(fs_info->space_info_kobj);
6ab0a2029ceaedb Jeff Mahoney    2013-11-01  1428  		kobject_put(fs_info->space_info_kobj);
e453d989e0bb33d Jeff Mahoney    2013-11-21  1429  	}
7fe92f8e456bf61 David Sterba    2022-07-27  1430  	if (fs_info->discard_kobj) {
7fe92f8e456bf61 David Sterba    2022-07-27  1431  		sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
7fe92f8e456bf61 David Sterba    2022-07-27  1432  		kobject_del(fs_info->discard_kobj);
7fe92f8e456bf61 David Sterba    2022-07-27  1433  		kobject_put(fs_info->discard_kobj);
e4faab844a55edb Dennis Zhou     2019-12-13  1434  	}
93945cb43ead1e2 Dennis Zhou     2019-12-13 @1435  	if (fs_info->debug_kobj) {
93945cb43ead1e2 Dennis Zhou     2019-12-13 @1436  		sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
93945cb43ead1e2 Dennis Zhou     2019-12-13  1437  		kobject_del(fs_info->debug_kobj);
93945cb43ead1e2 Dennis Zhou     2019-12-13  1438  		kobject_put(fs_info->debug_kobj);
93945cb43ead1e2 Dennis Zhou     2019-12-13  1439  	}
7fe92f8e456bf61 David Sterba    2022-07-27  1440  
e453d989e0bb33d Jeff Mahoney    2013-11-21  1441  	addrm_unknown_feature_attrs(fs_info, false);
3092c68fc58c7bd Nikolay Borisov 2020-07-03  1442  	sysfs_remove_group(fsid_kobj, &btrfs_feature_attr_group);
3092c68fc58c7bd Nikolay Borisov 2020-07-03  1443  	sysfs_remove_files(fsid_kobj, btrfs_attrs);
53f8a74cbeffd45 Anand Jain      2020-09-05  1444  	btrfs_sysfs_remove_fs_devices(fs_info->fs_devices);
5ac1d209f11271f Jeff Mahoney    2013-11-01  1445  }
5ac1d209f11271f Jeff Mahoney    2013-11-01  1446  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
author maintainer
#4 lore raw

PATCH v2 1/1 btrfs: sysfs: show discard stats and tunables in non-debug build

In reply to David Sterba

When discard=async was introduced there were also sysfs knobs and stats for debugging and tuning, hidden under CONFIG_BTRFS_DEBUG. The defaults have been set and so far seem to satisfy all users on a range of workloads. As there are not only tunables (like iops or kbps) but also stats tracking amount of discardable bytes, that should be available when the async discard is on (otherwise it's not).

The stats are moved from the per-fs debug directory, so it's under
/sys/fs/btrfs/FSID/discard

- discard_bitmap_bytes - amount of discarded bytes from data tracked as bitmaps
- discard_extent_bytes - dtto but as extents
- discard_bytes_saved -
- discardable_bytes - amount of bytes that can be discarded
- discardable_extents - number of extents to be discarded
- iops_limit - tunable limit of number of discard IOs to be issued
- kbps_limit - tunable limit of kilobytes per second issued as discard IO
- max_discard_size - tunable limit for size of one IO discard request

---

v2:
- add CONFIG_BTRFS_DEBUG back to btrfs_sysfs_remove_mounted when debug_kobj is removed

 fs/btrfs/ctree.h |  2 +-
 fs/btrfs/sysfs.c | 35 ++++++++++++++++-------------------
 2 files changed, 17 insertions(+), 20 deletions(-)
fs/btrfs/ctree.h +1 -1
index 4db85b9dc7ed..9631059d2733 100644
@@ -891,6 +891,7 @@ struct btrfs_fs_info {
 
 	struct kobject *space_info_kobj;
 	struct kobject *qgroups_kobj;
+	struct kobject *discard_kobj;
 
 	/* used to keep from writing metadata until there is a nice batch */
 	struct percpu_counter dirty_metadata_bytes;
@@ -1102,7 +1103,6 @@ struct btrfs_fs_info {
 
 #ifdef CONFIG_BTRFS_DEBUG
 	struct kobject *debug_kobj;
-	struct kobject *discard_debug_kobj;
 	struct list_head allocated_roots;
 
 	spinlock_t eb_leak_lock;
fs/btrfs/sysfs.c +16 -19
index d5d0717fd09a..32714ef8e22b 100644
@@ -35,12 +35,12 @@
  * qgroup_attrs				/sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
  * space_info_attrs			/sys/fs/btrfs/<uuid>/allocation/<bg-type>
  * raid_attrs				/sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
+ * discard_attrs			/sys/fs/btrfs/<uuid>/discard
  *
  * When built with BTRFS_CONFIG_DEBUG:
  *
  * btrfs_debug_feature_attrs		/sys/fs/btrfs/debug
  * btrfs_debug_mount_attrs		/sys/fs/btrfs/<uuid>/debug
- * discard_debug_attrs			/sys/fs/btrfs/<uuid>/debug/discard
  */
 
 struct btrfs_feature_attr {
@@ -429,12 +429,10 @@ static const struct attribute_group btrfs_static_feature_attr_group = {
 	.attrs = btrfs_supported_static_feature_attrs,
 };
 
-#ifdef CONFIG_BTRFS_DEBUG
-
 /*
  * Discard statistics and tunables
  */
-#define discard_to_fs_info(_kobj)	to_fs_info((_kobj)->parent->parent)
+#define discard_to_fs_info(_kobj)	to_fs_info(get_btrfs_kobj(_kobj))
 
 static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
 					    struct kobj_attribute *a,
@@ -583,11 +581,11 @@ BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
 	      btrfs_discard_max_discard_size_store);
 
 /*
- * Per-filesystem debugging of discard (when mounted with discard=async).
+ * Per-filesystem stats for discard (when mounted with discard=async).
  *
- * Path: /sys/fs/btrfs/<uuid>/debug/discard/
+ * Path: /sys/fs/btrfs/<uuid>/discard/
  */
-static const struct attribute *discard_debug_attrs[] = {
+static const struct attribute *discard_attrs[] = {
 	BTRFS_ATTR_PTR(discard, discardable_bytes),
 	BTRFS_ATTR_PTR(discard, discardable_extents),
 	BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
@@ -599,6 +597,8 @@ static const struct attribute *discard_debug_attrs[] = {
 	NULL,
 };
 
+#ifdef CONFIG_BTRFS_DEBUG
+
 /*
  * Per-filesystem runtime debugging exported via sysfs.
  *
@@ -1427,13 +1427,12 @@ void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
 		kobject_del(fs_info->space_info_kobj);
 		kobject_put(fs_info->space_info_kobj);
 	}
-#ifdef CONFIG_BTRFS_DEBUG
-	if (fs_info->discard_debug_kobj) {
-		sysfs_remove_files(fs_info->discard_debug_kobj,
-				   discard_debug_attrs);
-		kobject_del(fs_info->discard_debug_kobj);
-		kobject_put(fs_info->discard_debug_kobj);
+	if (fs_info->discard_kobj) {
+		sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
+		kobject_del(fs_info->discard_kobj);
+		kobject_put(fs_info->discard_kobj);
 	}
+#ifdef CONFIG_BTRFS_DEBUG
 	if (fs_info->debug_kobj) {
 		sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
 		kobject_del(fs_info->debug_kobj);
@@ -2001,20 +2000,18 @@ int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
 	error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
 	if (error)
 		goto failure;
+#endif
 
 	/* Discard directory */
-	fs_info->discard_debug_kobj = kobject_create_and_add("discard",
-						     fs_info->debug_kobj);
-	if (!fs_info->discard_debug_kobj) {
+	fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
+	if (!fs_info->discard_kobj) {
 		error = -ENOMEM;
 		goto failure;
 	}
 
-	error = sysfs_create_files(fs_info->discard_debug_kobj,
-				   discard_debug_attrs);
+	error = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
 	if (error)
 		goto failure;
-#endif
 
 	error = addrm_unknown_feature_attrs(fs_info, true);
 	if (error)
Signature · 1 line
2.36.1
Reviewed-by on v2 1/1
#5 lore raw
Quoted 20 lines · David Sterba

When discard=async was introduced there were also sysfs knobs and stats for debugging and tuning, hidden under CONFIG_BTRFS_DEBUG. The defaults have been set and so far seem to satisfy all users on a range of workloads. As there are not only tunables (like iops or kbps) but also stats tracking amount of discardable bytes, that should be available when the async discard is on (otherwise it's not).

The stats are moved from the per-fs debug directory, so it's under
/sys/fs/btrfs/FSID/discard

- discard_bitmap_bytes - amount of discarded bytes from data tracked as bitmaps
- discard_extent_bytes - dtto but as extents
- discard_bytes_saved -
- discardable_bytes - amount of bytes that can be discarded
- discardable_extents - number of extents to be discarded
- iops_limit - tunable limit of number of discard IOs to be issued
- kbps_limit - tunable limit of kilobytes per second issued as discard IO
- max_discard_size - tunable limit for size of one IO discard request

Quoted 1 line
Quoted 130 lines

---

v2:
- add CONFIG_BTRFS_DEBUG back to btrfs_sysfs_remove_mounted when debug_kobj is removed

  fs/btrfs/ctree.h |  2 +-
  fs/btrfs/sysfs.c | 35 ++++++++++++++++-------------------
  2 files changed, 17 insertions(+), 20 deletions(-)
fs/btrfs/ctree.h +1 -1
index 4db85b9dc7ed..9631059d2733 100644
@@ -891,6 +891,7 @@ struct btrfs_fs_info {
 
  	struct kobject *space_info_kobj;
  	struct kobject *qgroups_kobj;
+	struct kobject *discard_kobj;
 
  	/* used to keep from writing metadata until there is a nice batch */
  	struct percpu_counter dirty_metadata_bytes;
@@ -1102,7 +1103,6 @@ struct btrfs_fs_info {
 
  #ifdef CONFIG_BTRFS_DEBUG
  	struct kobject *debug_kobj;
-	struct kobject *discard_debug_kobj;
  	struct list_head allocated_roots;
 
  	spinlock_t eb_leak_lock;
fs/btrfs/sysfs.c +16 -19
index d5d0717fd09a..32714ef8e22b 100644
@@ -35,12 +35,12 @@
   * qgroup_attrs				/sys/fs/btrfs/<uuid>/qgroups/<level>_<qgroupid>
   * space_info_attrs			/sys/fs/btrfs/<uuid>/allocation/<bg-type>
   * raid_attrs				/sys/fs/btrfs/<uuid>/allocation/<bg-type>/<bg-profile>
+ * discard_attrs			/sys/fs/btrfs/<uuid>/discard
   *
   * When built with BTRFS_CONFIG_DEBUG:
   *
   * btrfs_debug_feature_attrs		/sys/fs/btrfs/debug
   * btrfs_debug_mount_attrs		/sys/fs/btrfs/<uuid>/debug
- * discard_debug_attrs			/sys/fs/btrfs/<uuid>/debug/discard
   */
 
  struct btrfs_feature_attr {
@@ -429,12 +429,10 @@ static const struct attribute_group btrfs_static_feature_attr_group = {
  	.attrs = btrfs_supported_static_feature_attrs,
  };
 
-#ifdef CONFIG_BTRFS_DEBUG
-
  /*
   * Discard statistics and tunables
   */
-#define discard_to_fs_info(_kobj)	to_fs_info((_kobj)->parent->parent)
+#define discard_to_fs_info(_kobj)	to_fs_info(get_btrfs_kobj(_kobj))
 
  static ssize_t btrfs_discardable_bytes_show(struct kobject *kobj,
  					    struct kobj_attribute *a,
@@ -583,11 +581,11 @@ BTRFS_ATTR_RW(discard, max_discard_size, btrfs_discard_max_discard_size_show,
  	      btrfs_discard_max_discard_size_store);
 
  /*
- * Per-filesystem debugging of discard (when mounted with discard=async).
+ * Per-filesystem stats for discard (when mounted with discard=async).
   *
- * Path: /sys/fs/btrfs/<uuid>/debug/discard/
+ * Path: /sys/fs/btrfs/<uuid>/discard/
   */
-static const struct attribute *discard_debug_attrs[] = {
+static const struct attribute *discard_attrs[] = {
  	BTRFS_ATTR_PTR(discard, discardable_bytes),
  	BTRFS_ATTR_PTR(discard, discardable_extents),
  	BTRFS_ATTR_PTR(discard, discard_bitmap_bytes),
@@ -599,6 +597,8 @@ static const struct attribute *discard_debug_attrs[] = {
  	NULL,
  };
 
+#ifdef CONFIG_BTRFS_DEBUG
+
  /*
   * Per-filesystem runtime debugging exported via sysfs.
   *
@@ -1427,13 +1427,12 @@ void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
  		kobject_del(fs_info->space_info_kobj);
  		kobject_put(fs_info->space_info_kobj);
  	}
-#ifdef CONFIG_BTRFS_DEBUG
-	if (fs_info->discard_debug_kobj) {
-		sysfs_remove_files(fs_info->discard_debug_kobj,
-				   discard_debug_attrs);
-		kobject_del(fs_info->discard_debug_kobj);
-		kobject_put(fs_info->discard_debug_kobj);
+	if (fs_info->discard_kobj) {
+		sysfs_remove_files(fs_info->discard_kobj, discard_attrs);
+		kobject_del(fs_info->discard_kobj);
+		kobject_put(fs_info->discard_kobj);
  	}
+#ifdef CONFIG_BTRFS_DEBUG
  	if (fs_info->debug_kobj) {
  		sysfs_remove_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
  		kobject_del(fs_info->debug_kobj);
@@ -2001,20 +2000,18 @@ int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
  	error = sysfs_create_files(fs_info->debug_kobj, btrfs_debug_mount_attrs);
  	if (error)
  		goto failure;
+#endif
 
  	/* Discard directory */
-	fs_info->discard_debug_kobj = kobject_create_and_add("discard",
-						     fs_info->debug_kobj);
-	if (!fs_info->discard_debug_kobj) {
+	fs_info->discard_kobj = kobject_create_and_add("discard", fsid_kobj);
+	if (!fs_info->discard_kobj) {
  		error = -ENOMEM;
  		goto failure;
  	}
 
-	error = sysfs_create_files(fs_info->discard_debug_kobj,
-				   discard_debug_attrs);
+	error = sysfs_create_files(fs_info->discard_kobj, discard_attrs);
  	if (error)
  		goto failure;
-#endif
 
  	error = addrm_unknown_feature_attrs(fs_info, true);
  	if (error)