Skip to contentSkip to conversation

Add throttling for discards

PATCH Superseded Jens Axboe 2 participants 10 messages May 3, 2018 – May 7, 2018 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

cover Add throttling for discards

I implemented support for discards in blk-wbt, so we treat them like background writes. If we have competing foreground IO, then we may throttle discards. Otherwise they should run at full speed.

Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw

PATCH 1/3 block: break discard submissions into the user defined size

Don't build discards bigger than what the user asked for, if the user decided to limit the size by writing to 'discard_max_bytes'.

---
 block/blk-lib.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
block/blk-lib.c +4 -3
index a676084d4740..7417d617091b 100644
@@ -62,10 +62,11 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 		unsigned int req_sects;
 		sector_t end_sect, tmp;
 
-		/* Make sure bi_size doesn't overflow */
-		req_sects = min_t(sector_t, nr_sects, UINT_MAX >> 9);
+		/* Issue in chunks of the user defined max discard setting */
+		req_sects = min_t(sector_t, nr_sects,
+					q->limits.max_discard_sectors);
 
-		/**
+		/*
 		 * If splitting a request, and the next starting sector would be
 		 * misaligned, stop the discard at the previous aligned sector.
 		 */
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 1/3] block: break discard submissions into the user defined size

Quoted 4 lines · Jens Axboe

Don't build discards bigger than what the user asked for, if the user decided to limit the size by writing to 'discard_max_bytes'.

Why do this here when blk_bio_discard_split already takes care of it?

author maintainer
#3 lore raw

Re: [PATCH 1/3] block: break discard submissions into the user defined size

Quoted 7 lines · Christoph Hellwig
Quoted 4 lines · Jens Axboe

Don't build discards bigger than what the user asked for, if the user decided to limit the size by writing to 'discard_max_bytes'.

Why do this here when blk_bio_discard_split already takes care of it?

Functionally it should be the same, just seems pointless to defer the chopping if we can build them the right size to begin with.

Signature · 1 line
Jens Axboe
author maintainer
#4 lore raw

PATCH 2/3 blk-wbt: account any writing command as a write

In reply to Jens Axboe

We currently special case WRITE and FLUSH, but we should really just include any command with the write bit set. This ensures that we account DISCARD.

---
 block/blk-wbt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-wbt.c +1 -1
index f92fc84b5e2c..3e34b41bcefc 100644
@@ -701,7 +701,7 @@ static int wbt_data_dir(const struct request *rq)
 
 	if (op == REQ_OP_READ)
 		return READ;
-	else if (op == REQ_OP_WRITE || op == REQ_OP_FLUSH)
+	else if (op_is_write(op))
 		return WRITE;
 
 	/* don't account */
Signature · 1 line
2.7.4
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] blk-wbt: account any writing command as a write

Quoted 3 lines · Jens Axboe

We currently special case WRITE and FLUSH, but we should really just include any command with the write bit set. This ensures that we account DISCARD.

Looks fine,

block/blk-wbt.c static int wbt_data_dir(const struct request *rq) · line 707

Quoted 13 lines
block/blk-wbt.c +1 -1
index f92fc84b5e2c..3e34b41bcefc 100644
@@ -701,7 +701,7 @@ static int wbt_data_dir(const struct request *rq)
 
 	if (op == REQ_OP_READ)
 		return READ;
-	else if (op == REQ_OP_WRITE || op == REQ_OP_FLUSH)
+	else if (op_is_write(op))
 		return WRITE;
 
 	/* don't account */

But this whole function smells, the return value is an array index, so instead of abusing read/write this should return 0 or 1 and be called _idx or similar. But I guess that is a question for the higher level interface.

author maintainer
#6 lore raw

PATCH 3/3 blk-wbt: throttle discards like background writes

In reply to Jens Axboe

Throttle discards like we would any background write. Discards should be background activity, so if they are impacting foreground IO, then we will throttle them down.

---
 block/blk-stat.h |  6 +++---
 block/blk-wbt.c  | 52 ++++++++++++++++++++++++++++++++++------------------
 block/blk-wbt.h  |  9 +++++++--
 3 files changed, 44 insertions(+), 23 deletions(-)
block/blk-stat.h +3 -3
index 2dd36347252a..c22049a8125e 100644
@@ -10,11 +10,11 @@
 
 /*
  * from upper:
- * 3 bits: reserved for other usage
+ * 4 bits: reserved for other usage
  * 12 bits: size
- * 49 bits: time
+ * 48 bits: time
  */
-#define BLK_STAT_RES_BITS	3
+#define BLK_STAT_RES_BITS	4
 #define BLK_STAT_SIZE_BITS	12
 #define BLK_STAT_RES_SHIFT	(64 - BLK_STAT_RES_BITS)
 #define BLK_STAT_SIZE_SHIFT	(BLK_STAT_RES_SHIFT - BLK_STAT_SIZE_BITS)
block/blk-wbt.c +34 -18

104 lines not on this page · raw message

block/blk-wbt.h +7 -2
index a232c98fbf4d..af451876ea31 100644
@@ -14,12 +14,17 @@ enum wbt_flags {
 	WBT_TRACKED		= 1,	/* write, tracked for throttling */
 	WBT_READ		= 2,	/* read */
 	WBT_KSWAPD		= 4,	/* write, from kswapd */
+	WBT_DISCARD		= 8,
 
-	WBT_NR_BITS		= 3,	/* number of bits */
+	WBT_NR_BITS		= 4,	/* number of bits */
 };
 
 enum {
-	WBT_NUM_RWQ		= 2,
+	WBT_REQ_BG = 0,
+	WBT_REQ_KSWAPD,
+	WBT_REQ_DISCARD,
+
+	WBT_NUM_RWQ,
 };
 
 /*
Signature · 1 line
2.7.4
#7 lore raw

Re: [PATCH 3/3] blk-wbt: throttle discards like background writes

block/blk-wbt.c static bool wb_recent_wait(struct rq_wb *rwb) · line 113

Quoted 12 lines
block/blk-wbt.c +8 -2
-static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb, bool is_kswapd)
+static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb, bool is_trim,
+					  bool is_kswapd)
 {
-	return &rwb->rq_wait[is_kswapd];
+	if (is_trim)
+		return &rwb->rq_wait[WBT_REQ_DISCARD];
+	else if (is_kswapd)
+		return &rwb->rq_wait[WBT_REQ_KSWAPD];
+	else
+		return &rwb->rq_wait[WBT_REQ_BG];
 }

Wouldn't it be more useful to pass a enum wbt_flag here?

Or just have a wbt_flag_to_wait_idx helper and do the array indexing in the callers?

Quoted 14 lines

{

block/blk-wbt.c +6 -5
 	const int op = bio_op(bio);
 
-	/*
-	 * If not a WRITE, do nothing
-	 */
-	if (op != REQ_OP_WRITE)
-		return false;
+	if (op == REQ_OP_WRITE) {
+		/*
+		 * Don't throttle WRITE_ODIRECT
+		 */
+		if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
+		    (REQ_SYNC | REQ_IDLE))

block/blk-wbt.c static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio) · line 588

Quoted 10 lines
block/blk-wbt.c +4 -5
+			return false;
 
-	/*
-	 * Don't throttle WRITE_ODIRECT
-	 */
-	if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) == (REQ_SYNC | REQ_IDLE))
-		return false;
+		return true;
+	} else if (op == REQ_OP_DISCARD)
+		return true;

what about:

	switch (bio_op(bio)) {
	case REQ_OP_WRITE:
		/*
		 * Don't throttle WRITE_ODIRECT
		 */
		if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
		    (REQ_SYNC | REQ_IDLE))
			return false;
		/*FALLTHROUGH*/
	case REQ_OP_DISCARD:
		return true;
	default:
		return false;
author maintainer
#8 lore raw

Re: [PATCH 3/3] blk-wbt: throttle discards like background writes

Quoted 15 lines · Christoph Hellwig

block/blk-wbt.c static bool wb_recent_wait(struct rq_wb *rwb) · line 113

Quoted 12 lines
block/blk-wbt.c +8 -2
-static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb, bool is_kswapd)
+static inline struct rq_wait *get_rq_wait(struct rq_wb *rwb, bool is_trim,
+					  bool is_kswapd)
 {
-	return &rwb->rq_wait[is_kswapd];
+	if (is_trim)
+		return &rwb->rq_wait[WBT_REQ_DISCARD];
+	else if (is_kswapd)
+		return &rwb->rq_wait[WBT_REQ_KSWAPD];
+	else
+		return &rwb->rq_wait[WBT_REQ_BG];
 }

Wouldn't it be more useful to pass a enum wbt_flag here?

Quoted 2 lines

Or just have a wbt_flag_to_wait_idx helper and do the array indexing in the callers?

It would be cleaner, but we don't have wbt_flag everywhere we need it. Though I guess we could swap the masking in wbt_wait() and do it before the __wbt_wait() call, and just use that. Since we only do the indexing in that one spot, I don't think we should add a helper.

Quoted 28 lines
Quoted 15 lines

{

block/blk-wbt.c +6 -5
 	const int op = bio_op(bio);
 
-	/*
-	 * If not a WRITE, do nothing
-	 */
-	if (op != REQ_OP_WRITE)
-		return false;
+	if (op == REQ_OP_WRITE) {
+		/*
+		 * Don't throttle WRITE_ODIRECT
+		 */
+		if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
+		    (REQ_SYNC | REQ_IDLE))

block/blk-wbt.c static inline bool wbt_should_throttle(struct rq_wb *rwb, struct bio *bio) · line 588

Quoted 10 lines
block/blk-wbt.c +4 -5
+			return false;
 
-	/*
-	 * Don't throttle WRITE_ODIRECT
-	 */
-	if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) == (REQ_SYNC | REQ_IDLE))
-		return false;
+		return true;
+	} else if (op == REQ_OP_DISCARD)
+		return true;

what about:

Quoted 13 lines
	switch (bio_op(bio)) {
	case REQ_OP_WRITE:
		/*
		 * Don't throttle WRITE_ODIRECT
		 */
		if ((bio->bi_opf & (REQ_SYNC | REQ_IDLE)) ==
		    (REQ_SYNC | REQ_IDLE))
			return false;
		/*FALLTHROUGH*/
	case REQ_OP_DISCARD:
		return true;
	default:
		return false;

Sure, I can do that. I'll spin a v2.

Signature · 1 line
Jens Axboe
author maintainer
#9 lore raw
In reply to Jens Axboe
Quoted 3 lines · Jens Axboe

I implemented support for discards in blk-wbt, so we treat them like background writes. If we have competing foreground IO, then we may throttle discards. Otherwise they should run at full speed.

Ran a quick local test case on a NVMe device. The test case here is doing reads from a file, while other files are being delete. File system is XFS. Three separate runs:

1) 'justreader' is just running the reader workload by itself.
2) 'nothrottle' is running the reader with the unlinks on the stock kernel.
3) 'throttled' is running the reader with the unlinks with the patches applied.

The fio run logs latencies in windows of 50msec, logging only the worst latency seen in that window:

[read]
filename=file0
rw=randread
bs=4k
direct=1
ioengine=libaio
iodepth=8
write_lat_log=read_lat
log_avg_msec=50
log_max_value=1

As you can see, with the throttling, the latencies and performance is the same as not having the unlinks running. They finish within 100msec of each other, after having read 10G of data. Without the throttling and unlinks running, we see spikes that are almost an order of magnitude worse. The reader also takes 1 second longer to complete. 322MB/sec vs 313MB/sec. It's also visible in the higher Pxx. Here's the throttled end of the percentiles:

     | 99.00th=[  145], 99.50th=[  153], 99.90th=[  180], 99.95th=[  198],
     | 99.99th=[  889]

and here's the unthrottled equivalent:

     | 99.00th=[  145], 99.50th=[  155], 99.90th=[  188], 99.95th=[  322],
     | 99.99th=[ 8586]
Signature · 1 line
Jens Axboe