[11/19] btrfs: add bps discard rate limit
diff mbox series

Message ID 17152a4b1f9a0719623af4ef98e5e8670dd70799.1570479299.git.dennis@kernel.org
State New
Headers show
Series
  • btrfs: async discard support
Related show

Commit Message

Dennis Zhou Oct. 7, 2019, 8:17 p.m. UTC
Provide an ability to rate limit based on mbps in addition to the iops
delay calculated from number of discardable extents.

Signed-off-by: Dennis Zhou <dennis@kernel.org>
---
 fs/btrfs/ctree.h   |  2 ++
 fs/btrfs/discard.c | 11 +++++++++++
 fs/btrfs/sysfs.c   | 30 ++++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+)

Comments

Josef Bacik Oct. 10, 2019, 3:47 p.m. UTC | #1
On Mon, Oct 07, 2019 at 04:17:42PM -0400, Dennis Zhou wrote:
> Provide an ability to rate limit based on mbps in addition to the iops
> delay calculated from number of discardable extents.
> 
> Signed-off-by: Dennis Zhou <dennis@kernel.org>
> ---
>  fs/btrfs/ctree.h   |  2 ++
>  fs/btrfs/discard.c | 11 +++++++++++
>  fs/btrfs/sysfs.c   | 30 ++++++++++++++++++++++++++++++
>  3 files changed, 43 insertions(+)
> 
> diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
> index b0823961d049..e81f699347e0 100644
> --- a/fs/btrfs/ctree.h
> +++ b/fs/btrfs/ctree.h
> @@ -447,10 +447,12 @@ struct btrfs_discard_ctl {
>  	spinlock_t lock;
>  	struct btrfs_block_group_cache *cache;
>  	struct list_head discard_list[BTRFS_NR_DISCARD_LISTS];
> +	u64 prev_discard;
>  	atomic_t discard_extents;
>  	atomic64_t discardable_bytes;
>  	atomic_t delay;
>  	atomic_t iops_limit;
> +	atomic64_t bps_limit;
>  };
>  
>  /* delayed seq elem */
> diff --git a/fs/btrfs/discard.c b/fs/btrfs/discard.c
> index c7afb5f8240d..072c73f48297 100644
> --- a/fs/btrfs/discard.c
> +++ b/fs/btrfs/discard.c
> @@ -176,6 +176,13 @@ void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl,
>  	cache = find_next_cache(discard_ctl, now);
>  	if (cache) {
>  		u64 delay = atomic_read(&discard_ctl->delay);
> +		s64 bps_limit = atomic64_read(&discard_ctl->bps_limit);
> +
> +		if (bps_limit)
> +			delay = max_t(u64, delay,
> +				      msecs_to_jiffies(MSEC_PER_SEC *
> +						discard_ctl->prev_discard /
> +						bps_limit));

I forget, are we allowed to do 0 / some value?  I feel like I did this at some
point with io.latency and it panic'ed and was very confused.  Maybe I'm just
misremembering.

And a similar nit, maybe we just do

u64 delay = atomic_read(&discard_ctl->delay);
u64 bps_delay = atomic64_read(&discard_ctl->bps_limit);
if (bps_delay)
	bps_delay = msecs_to_jiffies(MSEC_PER_SEC * blah)

delay = max(delay, bps_delay);

Or something else.  Thanks,

Josef
Dennis Zhou Oct. 14, 2019, 7:56 p.m. UTC | #2
On Thu, Oct 10, 2019 at 11:47:19AM -0400, Josef Bacik wrote:
> On Mon, Oct 07, 2019 at 04:17:42PM -0400, Dennis Zhou wrote:
> > Provide an ability to rate limit based on mbps in addition to the iops
> > delay calculated from number of discardable extents.
> > 
> > Signed-off-by: Dennis Zhou <dennis@kernel.org>
> > ---
> >  fs/btrfs/ctree.h   |  2 ++
> >  fs/btrfs/discard.c | 11 +++++++++++
> >  fs/btrfs/sysfs.c   | 30 ++++++++++++++++++++++++++++++
> >  3 files changed, 43 insertions(+)
> > 
> > diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
> > index b0823961d049..e81f699347e0 100644
> > --- a/fs/btrfs/ctree.h
> > +++ b/fs/btrfs/ctree.h
> > @@ -447,10 +447,12 @@ struct btrfs_discard_ctl {
> >  	spinlock_t lock;
> >  	struct btrfs_block_group_cache *cache;
> >  	struct list_head discard_list[BTRFS_NR_DISCARD_LISTS];
> > +	u64 prev_discard;
> >  	atomic_t discard_extents;
> >  	atomic64_t discardable_bytes;
> >  	atomic_t delay;
> >  	atomic_t iops_limit;
> > +	atomic64_t bps_limit;
> >  };
> >  
> >  /* delayed seq elem */
> > diff --git a/fs/btrfs/discard.c b/fs/btrfs/discard.c
> > index c7afb5f8240d..072c73f48297 100644
> > --- a/fs/btrfs/discard.c
> > +++ b/fs/btrfs/discard.c
> > @@ -176,6 +176,13 @@ void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl,
> >  	cache = find_next_cache(discard_ctl, now);
> >  	if (cache) {
> >  		u64 delay = atomic_read(&discard_ctl->delay);
> > +		s64 bps_limit = atomic64_read(&discard_ctl->bps_limit);
> > +
> > +		if (bps_limit)
> > +			delay = max_t(u64, delay,
> > +				      msecs_to_jiffies(MSEC_PER_SEC *
> > +						discard_ctl->prev_discard /
> > +						bps_limit));
> 
> I forget, are we allowed to do 0 / some value?  I feel like I did this at some
> point with io.latency and it panic'ed and was very confused.  Maybe I'm just
> misremembering.
> 

I don't remember there being an issue there, but I'd rather not find
out. I added discard_ctl->prev_discard to the if statement.

> And a similar nit, maybe we just do
> 
> u64 delay = atomic_read(&discard_ctl->delay);
> u64 bps_delay = atomic64_read(&discard_ctl->bps_limit);
> if (bps_delay)
> 	bps_delay = msecs_to_jiffies(MSEC_PER_SEC * blah)
> 
> delay = max(delay, bps_delay);
> 

/*
 * A single delayed workqueue item is responsible for
 * discarding, so we can manage the bytes rate limit by keeping
 * track of the previous discard.
 */
if (bps_limit && discard_ctl->prev_discard) {
	u64 bps_delay = (MSEC_PER_SEC *
	 	 discard_ctl->prev_discard / bps_limit);

	delay = max_t(u64, delay, msecs_to_jiffies(bps_delay));
}

This is what I changed it to. I'm not sure I quite grasped what you're
getting at from above.

Thanks,
Dennis

Patch
diff mbox series

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index b0823961d049..e81f699347e0 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -447,10 +447,12 @@  struct btrfs_discard_ctl {
 	spinlock_t lock;
 	struct btrfs_block_group_cache *cache;
 	struct list_head discard_list[BTRFS_NR_DISCARD_LISTS];
+	u64 prev_discard;
 	atomic_t discard_extents;
 	atomic64_t discardable_bytes;
 	atomic_t delay;
 	atomic_t iops_limit;
+	atomic64_t bps_limit;
 };
 
 /* delayed seq elem */
diff --git a/fs/btrfs/discard.c b/fs/btrfs/discard.c
index c7afb5f8240d..072c73f48297 100644
--- a/fs/btrfs/discard.c
+++ b/fs/btrfs/discard.c
@@ -176,6 +176,13 @@  void btrfs_discard_schedule_work(struct btrfs_discard_ctl *discard_ctl,
 	cache = find_next_cache(discard_ctl, now);
 	if (cache) {
 		u64 delay = atomic_read(&discard_ctl->delay);
+		s64 bps_limit = atomic64_read(&discard_ctl->bps_limit);
+
+		if (bps_limit)
+			delay = max_t(u64, delay,
+				      msecs_to_jiffies(MSEC_PER_SEC *
+						discard_ctl->prev_discard /
+						bps_limit));
 
 		if (now < cache->discard_delay)
 			delay = max_t(u64, delay,
@@ -213,6 +220,8 @@  static void btrfs_discard_workfn(struct work_struct *work)
 		btrfs_trim_block_group(cache, &trimmed, cache->discard_cursor,
 				       btrfs_block_group_end(cache), 0, true);
 
+	discard_ctl->prev_discard = trimmed;
+
 	if (cache->discard_cursor >= btrfs_block_group_end(cache)) {
 		if (btrfs_discard_bitmaps(cache)) {
 			remove_from_discard_list(discard_ctl, cache);
@@ -324,10 +333,12 @@  void btrfs_discard_init(struct btrfs_fs_info *fs_info)
 	for (i = 0; i < BTRFS_NR_DISCARD_LISTS; i++)
 		 INIT_LIST_HEAD(&discard_ctl->discard_list[i]);
 
+	discard_ctl->prev_discard = 0;
 	atomic_set(&discard_ctl->discard_extents, 0);
 	atomic64_set(&discard_ctl->discardable_bytes, 0);
 	atomic_set(&discard_ctl->delay, BTRFS_DISCARD_MAX_DELAY);
 	atomic_set(&discard_ctl->iops_limit, BTRFS_DISCARD_MAX_IOPS);
+	atomic64_set(&discard_ctl->bps_limit, 0);
 }
 
 void btrfs_discard_cleanup(struct btrfs_fs_info *fs_info)
diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index b9a62e470316..6fc4d644401b 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -522,10 +522,40 @@  static ssize_t btrfs_discard_iops_limit_store(struct kobject *kobj,
 BTRFS_ATTR_RW(discard, iops_limit, btrfs_discard_iops_limit_show,
 	      btrfs_discard_iops_limit_store);
 
+static ssize_t btrfs_discard_bps_limit_show(struct kobject *kobj,
+					     struct kobj_attribute *a,
+					     char *buf)
+{
+	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
+
+	return snprintf(buf, PAGE_SIZE, "%lld\n",
+			atomic64_read(&fs_info->discard_ctl.bps_limit));
+}
+
+static ssize_t btrfs_discard_bps_limit_store(struct kobject *kobj,
+					      struct kobj_attribute *a,
+					      const char *buf, size_t len)
+{
+	struct btrfs_fs_info *fs_info = to_fs_info(kobj->parent);
+	s64 bps_limit;
+	int ret;
+
+	ret = kstrtos64(buf, 10, &bps_limit);
+	if (ret || bps_limit < 0)
+		return -EINVAL;
+
+	atomic64_set(&fs_info->discard_ctl.bps_limit, bps_limit);
+
+	return len;
+}
+BTRFS_ATTR_RW(discard, bps_limit, btrfs_discard_bps_limit_show,
+	      btrfs_discard_bps_limit_store);
+
 static const struct attribute *discard_attrs[] = {
 	BTRFS_ATTR_PTR(discard, discard_extents),
 	BTRFS_ATTR_PTR(discard, discardable_bytes),
 	BTRFS_ATTR_PTR(discard, iops_limit),
+	BTRFS_ATTR_PTR(discard, bps_limit),
 	NULL,
 };