Skip to contentSkip to conversation

Throttled buffered writeback

PATCH New Jens Axboe 6 participants 38 messages Nov 1, 2016 – Nov 12, 2016

Subsystems (12)
lore
Share & tools
lore

Jens Axboe

author
#0 lore raw

PATCH 1/1 Throttled buffered writeback

I have addressed the (small) review comments from Christoph, and rebased it on top of for-4.10/block, since that now has the flag unification and fs side cleanups as well. This impacted the prep patches, and the wbt code.

I'd really like to get this merged for 4.10. It's block specific at this point, and defaults to just being enabled for blk-mq managed devices.

Let me know if there are any objections.

author
#1 lore raw

PATCH 1/8 block: add WRITE_BACKGROUND

This adds a new request flag, REQ_BACKGROUND, that callers can use to tell the block layer that this is background (non-urgent) IO.

---
 include/linux/blk_types.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/blk_types.h +2 -0
index bb921028e7c5..562ac46cb790 100644
@@ -177,6 +177,7 @@ enum req_flag_bits {
 	__REQ_FUA,		/* forced unit access */
 	__REQ_PREFLUSH,		/* request for cache flush */
 	__REQ_RAHEAD,		/* read ahead, can fail anytime */
+	__REQ_BACKGROUND,	/* background IO */
 	__REQ_NR_BITS,		/* stops here */
 };
 
@@ -192,6 +193,7 @@ enum req_flag_bits {
 #define REQ_FUA			(1ULL << __REQ_FUA)
 #define REQ_PREFLUSH		(1ULL << __REQ_PREFLUSH)
 #define REQ_RAHEAD		(1ULL << __REQ_RAHEAD)
+#define REQ_BACKGROUND		(1ULL << __REQ_BACKGROUND)
 
 #define REQ_FAILFAST_MASK \
 	(REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER)
Signature · 1 line
2.7.4
author
#5 lore raw

PATCH 2/8 writeback: add wbc_to_write_flags()

In reply to Jens Axboe

Add wbc_to_write_flags(), which returns the write modifier flags to use, based on a struct writeback_control. No functional changes in this patch, but it prepares us for factoring other wbc fields for write type.

---
 fs/buffer.c               | 2 +-
 fs/f2fs/data.c            | 2 +-
 fs/f2fs/node.c            | 2 +-
 fs/gfs2/meta_io.c         | 3 +--
 fs/mpage.c                | 2 +-
 fs/xfs/xfs_aops.c         | 8 ++------
 include/linux/writeback.h | 9 +++++++++
 mm/page_io.c              | 5 +----
 8 files changed, 17 insertions(+), 16 deletions(-)
fs/buffer.c +1 -1
index bc7c2bb30a9b..af5776da814a 100644
@@ -1697,7 +1697,7 @@ int __block_write_full_page(struct inode *inode, struct page *page,
 	struct buffer_head *bh, *head;
 	unsigned int blocksize, bbits;
 	int nr_underway = 0;
-	int write_flags = (wbc->sync_mode == WB_SYNC_ALL ? REQ_SYNC : 0);
+	int write_flags = wbc_to_write_flags(wbc);
 
 	head = create_page_buffers(page, inode,
 					(1 << BH_Dirty)|(1 << BH_Uptodate));
fs/f2fs/data.c +1 -1
index b80bf10603d7..9e5561fa4cb6 100644
@@ -1249,7 +1249,7 @@ static int f2fs_write_data_page(struct page *page,
 		.sbi = sbi,
 		.type = DATA,
 		.op = REQ_OP_WRITE,
-		.op_flags = (wbc->sync_mode == WB_SYNC_ALL) ? REQ_SYNC : 0,
+		.op_flags = wbc_to_write_flags(wbc),
 		.page = page,
 		.encrypted_page = NULL,
 	};
fs/f2fs/node.c +1 -1
index 932f3f8bb57b..d1e29deb4598 100644
@@ -1570,7 +1570,7 @@ static int f2fs_write_node_page(struct page *page,
 		.sbi = sbi,
 		.type = NODE,
 		.op = REQ_OP_WRITE,
-		.op_flags = (wbc->sync_mode == WB_SYNC_ALL) ? REQ_SYNC : 0,
+		.op_flags = wbc_to_write_flags(wbc),
 		.page = page,
 		.encrypted_page = NULL,
 	};
fs/gfs2/meta_io.c +1 -2
index e562b1191c9c..49db8ef13fdf 100644
@@ -37,8 +37,7 @@ static int gfs2_aspace_writepage(struct page *page, struct writeback_control *wb
 {
 	struct buffer_head *bh, *head;
 	int nr_underway = 0;
-	int write_flags = REQ_META | REQ_PRIO |
-		(wbc->sync_mode == WB_SYNC_ALL ? REQ_SYNC : 0);
+	int write_flags = REQ_META | REQ_PRIO | wbc_to_write_flags(wbc);
 
 	BUG_ON(!PageLocked(page));
 	BUG_ON(!page_has_buffers(page));
fs/mpage.c +1 -1
index f35e2819d0c6..98fc11aa7e0b 100644
@@ -489,7 +489,7 @@ static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
 	struct buffer_head map_bh;
 	loff_t i_size = i_size_read(inode);
 	int ret = 0;
-	int op_flags = (wbc->sync_mode == WB_SYNC_ALL ? REQ_SYNC : 0);
+	int op_flags = wbc_to_write_flags(wbc);
 
 	if (page_has_buffers(page)) {
 		struct buffer_head *head = page_buffers(page);
fs/xfs/xfs_aops.c +2 -6
index 594e02c485b2..6be5204a06d3 100644
@@ -495,9 +495,7 @@ xfs_submit_ioend(
 
 	ioend->io_bio->bi_private = ioend;
 	ioend->io_bio->bi_end_io = xfs_end_bio;
-	ioend->io_bio->bi_opf = REQ_OP_WRITE;
-	if (wbc->sync_mode == WB_SYNC_ALL)
-		ioend->io_bio->bi_opf |= REQ_SYNC;
+	ioend->io_bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
 
 	/*
 	 * If we are failing the IO now, just mark the ioend with an
@@ -569,9 +567,7 @@ xfs_chain_bio(
 
 	bio_chain(ioend->io_bio, new);
 	bio_get(ioend->io_bio);		/* for xfs_destroy_ioend */
-	ioend->io_bio->bi_opf = REQ_OP_WRITE;
-	if (wbc->sync_mode == WB_SYNC_ALL)
-		ioend->io_bio->bi_opf |= REQ_SYNC;
+	ioend->io_bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
 	submit_bio(ioend->io_bio);
 	ioend->io_bio = new;
 }
include/linux/writeback.h +9 -0
index e4c38703bf4e..50c96ee8108f 100644
@@ -9,6 +9,7 @@
 #include <linux/fs.h>
 #include <linux/flex_proportions.h>
 #include <linux/backing-dev-defs.h>
+#include <linux/blk_types.h>
 
 struct bio;
 
@@ -102,6 +103,14 @@ struct writeback_control {
 #endif
 };
 
+static inline int wbc_to_write_flags(struct writeback_control *wbc)
+{
+	if (wbc->sync_mode == WB_SYNC_ALL)
+		return REQ_SYNC;
+
+	return 0;
+}
+
 /*
  * A wb_domain represents a domain that wb's (bdi_writeback's) belong to
  * and are measured against each other in.  There always is one global
mm/page_io.c +1 -4
index a2651f58c86a..23f6d0d3470f 100644
@@ -320,10 +320,7 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc,
 		ret = -ENOMEM;
 		goto out;
 	}
-	if (wbc->sync_mode == WB_SYNC_ALL)
-		bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC);
-	else
-		bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
+	bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
 	count_vm_event(PSWPOUT);
 	set_page_writeback(page);
 	unlock_page(page);
Signature · 1 line
2.7.4
author
#7 lore raw

PATCH 3/8 writeback: mark background writeback as such

In reply to Jens Axboe

If we're doing background type writes, then use the appropriate background write flags for that.

---
 include/linux/writeback.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/writeback.h +2 -0
index 50c96ee8108f..c78f9f0920b5 100644
@@ -107,6 +107,8 @@ static inline int wbc_to_write_flags(struct writeback_control *wbc)
 {
 	if (wbc->sync_mode == WB_SYNC_ALL)
 		return REQ_SYNC;
+	else if (wbc->for_kupdate || wbc->for_background)
+		return REQ_BACKGROUND;
 
 	return 0;
 }
Signature · 1 line
2.7.4
author
#10 lore raw

PATCH 4/8 writeback: track if we're sleeping on progress in balance_dirty_pages()

In reply to Jens Axboe

Note in the bdi_writeback structure whenever a task ends up sleeping waiting for progress. We can use that information in the lower layers to increase the priority of writes.

---
 include/linux/backing-dev-defs.h | 2 ++
 mm/backing-dev.c                 | 1 +
 mm/page-writeback.c              | 1 +
 3 files changed, 4 insertions(+)
include/linux/backing-dev-defs.h +2 -0
index c357f27d5483..dc5f76d7f648 100644
@@ -116,6 +116,8 @@ struct bdi_writeback {
 	struct list_head work_list;
 	struct delayed_work dwork;	/* work item used for writeback */
 
+	unsigned long dirty_sleep;	/* last wait */
+
 	struct list_head bdi_node;	/* anchored at bdi->wb_list */
 
 #ifdef CONFIG_CGROUP_WRITEBACK
mm/backing-dev.c +1 -0
index 8fde443f36d7..3bfed5ab2475 100644
@@ -310,6 +310,7 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 	spin_lock_init(&wb->work_lock);
 	INIT_LIST_HEAD(&wb->work_list);
 	INIT_DELAYED_WORK(&wb->dwork, wb_workfn);
+	wb->dirty_sleep = jiffies;
 
 	wb->congested = wb_congested_get_create(bdi, blkcg_id, gfp);
 	if (!wb->congested)
mm/page-writeback.c +1 -0
index 439cc63ad903..52e2f8e3b472 100644
@@ -1778,6 +1778,7 @@ static void balance_dirty_pages(struct address_space *mapping,
 					  pause,
 					  start_time);
 		__set_current_state(TASK_KILLABLE);
+		wb->dirty_sleep = now;
 		io_schedule_timeout(pause);
 
 		current->dirty_paused_when = now + pause;
Signature · 1 line
2.7.4
author
#14 lore raw

PATCH 5/8 block: add code to track actual device queue depth

In reply to Jens Axboe

For blk-mq, ->nr_requests does track queue depth, at least at init time. But for the older queue paths, it's simply a soft setting. On top of that, it's generally larger than the hardware setting on purpose, to allow backup of requests for merging.

Fill a hole in struct request with a 'queue_depth' member, that drivers can call to more closely inform the block layer of the real queue depth.

---
 block/blk-settings.c   | 12 ++++++++++++
 drivers/scsi/scsi.c    |  3 +++
 include/linux/blkdev.h | 11 +++++++++++
 3 files changed, 26 insertions(+)
block/blk-settings.c +12 -0
index 55369a65dea2..9cf053759363 100644
@@ -837,6 +837,18 @@ void blk_queue_flush_queueable(struct request_queue *q, bool queueable)
 EXPORT_SYMBOL_GPL(blk_queue_flush_queueable);
 
 /**
+ * blk_set_queue_depth - tell the block layer about the device queue depth
+ * @q:		the request queue for the device
+ * @depth:		queue depth
+ *
+ */
+void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
+{
+	q->queue_depth = depth;
+}
+EXPORT_SYMBOL(blk_set_queue_depth);
+
+/**
  * blk_queue_write_cache - configure queue's write cache
  * @q:		the request queue for the device
  * @wc:		write back cache on or off
drivers/scsi/scsi.c +3 -0
index 1deb6adc411f..75455d4dab68 100644
@@ -621,6 +621,9 @@ int scsi_change_queue_depth(struct scsi_device *sdev, int depth)
 		wmb();
 	}
 
+	if (sdev->request_queue)
+		blk_set_queue_depth(sdev->request_queue, depth);
+
 	return sdev->queue_depth;
 }
 EXPORT_SYMBOL(scsi_change_queue_depth);
include/linux/blkdev.h +11 -0
index 8396da2bb698..0c677fb35ce4 100644
@@ -405,6 +405,8 @@ struct request_queue {
 	struct blk_mq_ctx __percpu	*queue_ctx;
 	unsigned int		nr_queues;
 
+	unsigned int		queue_depth;
+
 	/* hw dispatch queues */
 	struct blk_mq_hw_ctx	**queue_hw_ctx;
 	unsigned int		nr_hw_queues;
@@ -777,6 +779,14 @@ static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
 	return false;
 }
 
+static inline unsigned int blk_queue_depth(struct request_queue *q)
+{
+	if (q->queue_depth)
+		return q->queue_depth;
+
+	return q->nr_requests;
+}
+
 /*
  * q->prep_rq_fn return values
  */
@@ -1093,6 +1103,7 @@ extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
+extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
 extern void blk_set_default_limits(struct queue_limits *lim);
 extern void blk_set_stacking_limits(struct queue_limits *lim);
 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
Signature · 1 line
2.7.4
author
#19 lore raw

PATCH 6/8 block: add scalable completion tracking of requests

In reply to Jens Axboe

For legacy block, we simply track them in the request queue. For blk-mq, we track them on a per-sw queue basis, which we can then sum up through the hardware queues and finally to a per device state.

The stats are tracked in, roughly, 0.1s interval windows.

Add sysfs files to display the stats.

---
 block/Makefile            |   2 +-
 block/blk-core.c          |   4 +
 block/blk-mq-sysfs.c      |  47 ++++++++++
 block/blk-mq.c            |  14 +++
 block/blk-mq.h            |   3 +
 block/blk-stat.c          | 226 ++++++++++++++++++++++++++++++++++++++++++++++
 block/blk-stat.h          |  37 ++++++++
 block/blk-sysfs.c         |  26 ++++++
 include/linux/blk_types.h |  16 ++++
 include/linux/blkdev.h    |   4 +
 10 files changed, 378 insertions(+), 1 deletion(-)
 create mode 100644 block/blk-stat.c
 create mode 100644 block/blk-stat.h
block/Makefile +1 -1
index 934dac73fb37..2528c596f7ec 100644
@@ -5,7 +5,7 @@
 obj-$(CONFIG_BLOCK) := bio.o elevator.o blk-core.o blk-tag.o blk-sysfs.o \
 			blk-flush.o blk-settings.o blk-ioc.o blk-map.o \
 			blk-exec.o blk-merge.o blk-softirq.o blk-timeout.o \
-			blk-lib.o blk-mq.o blk-mq-tag.o \
+			blk-lib.o blk-mq.o blk-mq-tag.o blk-stat.o \
 			blk-mq-sysfs.o blk-mq-cpumap.o ioctl.o \
 			genhd.o scsi_ioctl.o partition-generic.o ioprio.o \
 			badblocks.o partitions/
block/blk-core.c +4 -0
index 0bfaa54d3e9f..ca77c725b4e5 100644
@@ -2462,6 +2462,8 @@ void blk_start_request(struct request *req)
 {
 	blk_dequeue_request(req);
 
+	blk_stat_set_issue_time(&req->issue_stat);
+
 	/*
 	 * We are now handing the request to the hardware, initialize
 	 * resid_len to full count and add the timeout handler.
@@ -2529,6 +2531,8 @@ bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
 
 	trace_block_rq_complete(req->q, req, nr_bytes);
 
+	blk_stat_add(&req->q->rq_stats[rq_data_dir(req)], req);
+
 	if (!req->bio)
 		return false;
 
block/blk-mq-sysfs.c +47 -0

65 lines not on this page · raw message

block/blk-mq.c +14 -0
index 2da1a0ee3318..4555a76d22a7 100644
@@ -30,6 +30,7 @@
 #include "blk.h"
 #include "blk-mq.h"
 #include "blk-mq-tag.h"
+#include "blk-stat.h"
 
 static DEFINE_MUTEX(all_q_mutex);
 static LIST_HEAD(all_q_list);
@@ -376,10 +377,19 @@ static void blk_mq_ipi_complete_request(struct request *rq)
 	put_cpu();
 }
 
+static void blk_mq_stat_add(struct request *rq)
+{
+	struct blk_rq_stat *stat = &rq->mq_ctx->stat[rq_data_dir(rq)];
+
+	blk_stat_add(stat, rq);
+}
+
 static void __blk_mq_complete_request(struct request *rq)
 {
 	struct request_queue *q = rq->q;
 
+	blk_mq_stat_add(rq);
+
 	if (!q->softirq_done_fn)
 		blk_mq_end_request(rq, rq->errors);
 	else
@@ -423,6 +433,8 @@ void blk_mq_start_request(struct request *rq)
 	if (unlikely(blk_bidi_rq(rq)))
 		rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
 
+	blk_stat_set_issue_time(&rq->issue_stat);
+
 	blk_add_timer(rq);
 
 	/*
@@ -1708,6 +1720,8 @@ static void blk_mq_init_cpu_queues(struct request_queue *q,
 		spin_lock_init(&__ctx->lock);
 		INIT_LIST_HEAD(&__ctx->rq_list);
 		__ctx->queue = q;
+		blk_stat_init(&__ctx->stat[0]);
+		blk_stat_init(&__ctx->stat[1]);
 
 		/* If the cpu isn't online, the cpu is mapped to first hctx */
 		if (!cpu_online(i))
block/blk-mq.h +3 -0
index e5d25249028c..8cf16cb69f64 100644
@@ -1,6 +1,8 @@
 #ifndef INT_BLK_MQ_H
 #define INT_BLK_MQ_H
 
+#include "blk-stat.h"
+
 struct blk_mq_tag_set;
 
 struct blk_mq_ctx {
@@ -18,6 +20,7 @@ struct blk_mq_ctx {
 
 	/* incremented at completion time */
 	unsigned long		____cacheline_aligned_in_smp rq_completed[2];
+	struct blk_rq_stat	stat[2];
 
 	struct request_queue	*queue;
 	struct kobject		kobj;
block/blk-stat.c new file +226 -0

226 lines not on this page · raw message

block/blk-stat.h new file +37 -0
new file mode 100644
index 000000000000..26b1f45dff26
@@ -0,0 +1,37 @@
+#ifndef BLK_STAT_H
+#define BLK_STAT_H
+
+/*
+ * ~0.13s window as a power-of-2 (2^27 nsecs)
+ */
+#define BLK_STAT_NSEC		134217728ULL
+#define BLK_STAT_NSEC_MASK	~(BLK_STAT_NSEC - 1)
+
+/*
+ * Upper 3 bits can be used elsewhere
+ */
+#define BLK_STAT_RES_BITS	3
+#define BLK_STAT_SHIFT		(64 - BLK_STAT_RES_BITS)
+#define BLK_STAT_TIME_MASK	((1ULL << BLK_STAT_SHIFT) - 1)
+#define BLK_STAT_MASK		~BLK_STAT_TIME_MASK
+
+void blk_stat_add(struct blk_rq_stat *, struct request *);
+void blk_hctx_stat_get(struct blk_mq_hw_ctx *, struct blk_rq_stat *);
+void blk_queue_stat_get(struct request_queue *, struct blk_rq_stat *);
+void blk_stat_clear(struct request_queue *q);
+void blk_stat_init(struct blk_rq_stat *);
+void blk_stat_sum(struct blk_rq_stat *, struct blk_rq_stat *);
+bool blk_stat_is_current(struct blk_rq_stat *);
+void blk_stat_set_issue_time(struct blk_issue_stat *);
+
+static inline u64 __blk_stat_time(u64 time)
+{
+	return time & BLK_STAT_TIME_MASK;
+}
+
+static inline u64 blk_stat_time(struct blk_issue_stat *stat)
+{
+	return __blk_stat_time(stat->time);
+}
+
+#endif
block/blk-sysfs.c +26 -0
index 488c2e28feb8..5bb4648f434a 100644
@@ -401,6 +401,26 @@ static ssize_t queue_dax_show(struct request_queue *q, char *page)
 	return queue_var_show(blk_queue_dax(q), page);
 }
 
+static ssize_t print_stat(char *page, struct blk_rq_stat *stat, const char *pre)
+{
+	return sprintf(page, "%s samples=%llu, mean=%lld, min=%lld, max=%lld\n",
+			pre, (long long) stat->nr_samples,
+			(long long) stat->mean, (long long) stat->min,
+			(long long) stat->max);
+}
+
+static ssize_t queue_stats_show(struct request_queue *q, char *page)
+{
+	struct blk_rq_stat stat[2];
+	ssize_t ret;
+
+	blk_queue_stat_get(q, stat);
+
+	ret = print_stat(page, &stat[0], "read :");
+	ret += print_stat(page + ret, &stat[1], "write:");
+	return ret;
+}
+
 static struct queue_sysfs_entry queue_requests_entry = {
 	.attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
 	.show = queue_requests_show,
@@ -553,6 +573,11 @@ static struct queue_sysfs_entry queue_dax_entry = {
 	.show = queue_dax_show,
 };
 
+static struct queue_sysfs_entry queue_stats_entry = {
+	.attr = {.name = "stats", .mode = S_IRUGO },
+	.show = queue_stats_show,
+};
+
 static struct attribute *default_attrs[] = {
 	&queue_requests_entry.attr,
 	&queue_ra_entry.attr,
@@ -582,6 +607,7 @@ static struct attribute *default_attrs[] = {
 	&queue_poll_entry.attr,
 	&queue_wc_entry.attr,
 	&queue_dax_entry.attr,
+	&queue_stats_entry.attr,
 	NULL,
 };
 
include/linux/blk_types.h +16 -0
index 562ac46cb790..4d0044d09984 100644
@@ -250,4 +250,20 @@ static inline unsigned int blk_qc_t_to_tag(blk_qc_t cookie)
 	return cookie & ((1u << BLK_QC_T_SHIFT) - 1);
 }
 
+struct blk_issue_stat {
+	u64 time;
+};
+
+#define BLK_RQ_STAT_BATCH	64
+
+struct blk_rq_stat {
+	s64 mean;
+	u64 min;
+	u64 max;
+	s32 nr_samples;
+	s32 nr_batch;
+	u64 batch;
+	s64 time;
+};
+
 #endif /* __LINUX_BLK_TYPES_H */
include/linux/blkdev.h +4 -0
index 0c677fb35ce4..6bd5eb56894e 100644
@@ -197,6 +197,7 @@ struct request {
 	struct gendisk *rq_disk;
 	struct hd_struct *part;
 	unsigned long start_time;
+	struct blk_issue_stat issue_stat;
 #ifdef CONFIG_BLK_CGROUP
 	struct request_list *rl;		/* rl this rq is alloced from */
 	unsigned long long start_time_ns;
@@ -492,6 +493,9 @@ struct request_queue {
 
 	unsigned int		nr_sorted;
 	unsigned int		in_flight[2];
+
+	struct blk_rq_stat	rq_stats[2];
+
 	/*
 	 * Number of active block driver functions for which blk_drain_queue()
 	 * must wait. Must be incremented around functions that unlock the
Signature · 1 line
2.7.4
author
#27 lore raw

PATCH 7/8 blk-wbt: add general throttling mechanism

In reply to Jens Axboe

We can hook this up to the block layer, to help throttle buffered writes.

wbt registers a few trace points that can be used to track what is happening in the system:

wbt_lat: 259:0: latency 2446318
wbt_stat: 259:0: rmean=2446318, rmin=2446318, rmax=2446318, rsamples=1,

               wmean=518866, wmin=15522, wmax=5330353, wsamples=57

wbt_step: 259:0: step down: step=1, window=72727272, background=8, normal=16, max=32

This shows a sync issue event (wbt_lat) that exceeded it's time. wbt_stat dumps the current read/write stats for that window, and wbt_step shows a step down event where we now scale back writes. Each trace includes the device, 259:0 in this case.

---
 block/Makefile             |   1 +
 block/blk-wbt.c            | 704 +++++++++++++++++++++++++++++++++++++++++++++
 block/blk-wbt.h            | 166 +++++++++++
 include/trace/events/wbt.h | 153 ++++++++++
 4 files changed, 1024 insertions(+)
 create mode 100644 block/blk-wbt.c
 create mode 100644 block/blk-wbt.h
 create mode 100644 include/trace/events/wbt.h
block/Makefile +1 -0
index 2528c596f7ec..a827f988c4e6 100644
@@ -24,3 +24,4 @@ obj-$(CONFIG_BLK_CMDLINE_PARSER)	+= cmdline-parser.o
 obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o blk-integrity.o t10-pi.o
 obj-$(CONFIG_BLK_MQ_PCI)	+= blk-mq-pci.o
 obj-$(CONFIG_BLK_DEV_ZONED)	+= blk-zoned.o
+obj-$(CONFIG_BLK_WBT)		+= blk-wbt.o
block/blk-wbt.c new file +704 -0

704 lines not on this page · raw message

block/blk-wbt.h new file +166 -0

166 lines not on this page · raw message

include/trace/events/wbt.h new file +153 -0

153 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#35 lore raw

PATCH 8/8 block: hook up writeback throttling

In reply to Jens Axboe

Enable throttling of buffered writeback to make it a lot more smooth, and has way less impact on other system activity. Background writeback should be, by definition, background activity. The fact that we flush huge bundles of it at the time means that it potentially has heavy impacts on foreground workloads, which isn't ideal. We can't easily limit the sizes of writes that we do, since that would impact file system layout in the presence of delayed allocation. So just throttle back buffered writeback, unless someone is waiting for it.

The algorithm for when to throttle takes its inspiration in the CoDel networking scheduling algorithm. Like CoDel, blk-wb monitors the minimum latencies of requests over a window of time. In that window of time, if the minimum latency of any request exceeds a given target, then a scale count is incremented and the queue depth is shrunk. The next monitoring window is shrunk accordingly. Unlike CoDel, if we hit a window that exhibits good behavior, then we simply increment the scale count and re-calculate the limits for that scale value. This prevents us from oscillating between a close-to-ideal value and max all the time, instead remaining in the windows where we get good behavior.

Unlike CoDel, blk-wb allows the scale count to to negative. This happens if we primarily have writes going on. Unlike positive scale counts, this doesn't change the size of the monitoring window. When the heavy writers finish, blk-bw quickly snaps back to it's stable state of a zero scale count.

The patch registers two sysfs entries. The first one, 'wb_window_usec', defines the window of monitoring. The second one, 'wb_lat_usec', sets the latency target for the window. It defaults to 2 msec for non-rotational storage, and 75 msec for rotational storage. Setting this value to '0' disables blk-wb. Generally, a user would not have to touch these settings.

We don't enable WBT on devices that are managed with CFQ, and have a non-root block cgroup attached. If we have a proportional share setup on this particular disk, then the wbt throttling will interfere with that. We don't have a strong need for wbt for that case, since we will rely on CFQ doing that for us.

---
 Documentation/block/queue-sysfs.txt |  13 ++++
 block/Kconfig                       |  24 +++++++
 block/blk-core.c                    |  18 ++++-
 block/blk-mq.c                      |  27 +++++++-
 block/blk-settings.c                |   4 ++
 block/blk-sysfs.c                   | 134 ++++++++++++++++++++++++++++++++++++
 block/cfq-iosched.c                 |  14 ++++
 include/linux/blkdev.h              |   3 +
 8 files changed, 233 insertions(+), 4 deletions(-)
Documentation/block/queue-sysfs.txt +13 -0
index 2a3904030dea..2847219ebd8c 100644
@@ -169,5 +169,18 @@ This is the number of bytes the device can write in a single write-same
 command.  A value of '0' means write-same is not supported by this
 device.
 
+wb_lat_usec (RW)
+----------------
+If the device is registered for writeback throttling, then this file shows
+the target minimum read latency. If this latency is exceeded in a given
+window of time (see wb_window_usec), then the writeback throttling will start
+scaling back writes.
+
+wb_window_usec (RW)
+-------------------
+If the device is registered for writeback throttling, then this file shows
+the value of the monitoring window in which we'll look at the target
+latency. See wb_lat_usec.
+
 
 Jens Axboe <[email protected]>, February 2009
block/Kconfig +24 -0
index 6b0ad08f0677..9f5d4dd7d751 100644
@@ -120,6 +120,30 @@ config BLK_CMDLINE_PARSER
 
 	See Documentation/block/cmdline-partition.txt for more information.
 
+config BLK_WBT
+	bool "Enable support for block device writeback throttling"
+	default n
+	---help---
+	Enabling this option enables the block layer to throttle buffered
+	writeback from the VM, making it more smooth and having less
+	impact on foreground operations.
+
+config BLK_WBT_SQ
+	bool "Single queue writeback throttling"
+	default n
+	depends on BLK_WBT
+	---help---
+	Enable writeback throttling by default on legacy single queue devices
+
+config BLK_WBT_MQ
+	bool "Multiqueue writeback throttling"
+	default y
+	depends on BLK_WBT
+	---help---
+	Enable writeback throttling by default on multiqueue devices.
+	Multiqueue currently doesn't have support for IO scheduling,
+	enabling this option is recommended.
+
 menu "Partition Types"
 
 source "block/partitions/Kconfig"
block/blk-core.c +16 -2
index ca77c725b4e5..c68e92acf21a 100644
@@ -39,6 +39,7 @@
 
 #include "blk.h"
 #include "blk-mq.h"
+#include "blk-wbt.h"
 
 EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap);
 EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
@@ -882,6 +883,8 @@ blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn,
 
 fail:
 	blk_free_flush_queue(q->fq);
+	wbt_exit(q->rq_wb);
+	q->rq_wb = NULL;
 	return NULL;
 }
 EXPORT_SYMBOL(blk_init_allocated_queue);
@@ -1344,6 +1347,7 @@ void blk_requeue_request(struct request_queue *q, struct request *rq)
 	blk_delete_timer(rq);
 	blk_clear_rq_complete(rq);
 	trace_block_rq_requeue(q, rq);
+	wbt_requeue(q->rq_wb, &rq->issue_stat);
 
 	if (rq->rq_flags & RQF_QUEUED)
 		blk_queue_end_tag(q, rq);
@@ -1436,6 +1440,8 @@ void __blk_put_request(struct request_queue *q, struct request *req)
 	/* this is a bio leak */
 	WARN_ON(req->bio != NULL);
 
+	wbt_done(q->rq_wb, &req->issue_stat);
+
 	/*
 	 * Request may not have originated from ll_rw_blk. if not,
 	 * it didn't come out of our reserved rq pools
@@ -1663,6 +1669,7 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
 	int el_ret, where = ELEVATOR_INSERT_SORT;
 	struct request *req;
 	unsigned int request_count = 0;
+	unsigned int wb_acct;
 
 	/*
 	 * low level driver can indicate that it wants pages above a
@@ -1715,17 +1722,22 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)
 	}
 
 get_rq:
+	wb_acct = wbt_wait(q->rq_wb, bio, q->queue_lock);
+
 	/*
 	 * Grab a free request. This is might sleep but can not fail.
 	 * Returns with the queue unlocked.
 	 */
 	req = get_request(q, bio->bi_opf, bio, GFP_NOIO);
 	if (IS_ERR(req)) {
+		__wbt_done(q->rq_wb, wb_acct);
 		bio->bi_error = PTR_ERR(req);
 		bio_endio(bio);
 		goto out_unlock;
 	}
 
+	wbt_track(&req->issue_stat, wb_acct);
+
 	/*
 	 * After dropping the lock and possibly sleeping here, our request
 	 * may now be mergeable after it had proven unmergeable (above).
@@ -2463,6 +2475,7 @@ void blk_start_request(struct request *req)
 	blk_dequeue_request(req);
 
 	blk_stat_set_issue_time(&req->issue_stat);
+	wbt_issue(req->q->rq_wb, &req->issue_stat);
 
 	/*
 	 * We are now handing the request to the hardware, initialize
@@ -2700,9 +2713,10 @@ void blk_finish_request(struct request *req, int error)
 
 	blk_account_io_done(req);
 
-	if (req->end_io)
+	if (req->end_io) {
+		wbt_done(req->q->rq_wb, &req->issue_stat);
 		req->end_io(req, error);
-	else {
+	} else {
 		if (blk_bidi_rq(req))
 			__blk_put_request(req->next_rq->q, req->next_rq);
 
block/blk-mq.c +25 -2
index 4555a76d22a7..11e461c026dc 100644
@@ -31,6 +31,7 @@
 #include "blk-mq.h"
 #include "blk-mq-tag.h"
 #include "blk-stat.h"
+#include "blk-wbt.h"
 
 static DEFINE_MUTEX(all_q_mutex);
 static LIST_HEAD(all_q_list);
@@ -299,6 +300,8 @@ static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,
 
 	if (rq->rq_flags & RQF_MQ_INFLIGHT)
 		atomic_dec(&hctx->nr_active);
+
+	wbt_done(q->rq_wb, &rq->issue_stat);
 	rq->rq_flags = 0;
 
 	clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags);
@@ -327,6 +330,7 @@ inline void __blk_mq_end_request(struct request *rq, int error)
 	blk_account_io_done(rq);
 
 	if (rq->end_io) {
+		wbt_done(rq->q->rq_wb, &rq->issue_stat);
 		rq->end_io(rq, error);
 	} else {
 		if (unlikely(blk_bidi_rq(rq)))
@@ -434,6 +438,7 @@ void blk_mq_start_request(struct request *rq)
 		rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
 
 	blk_stat_set_issue_time(&rq->issue_stat);
+	wbt_issue(q->rq_wb, &rq->issue_stat);
 
 	blk_add_timer(rq);
 
@@ -470,6 +475,7 @@ static void __blk_mq_requeue_request(struct request *rq)
 	struct request_queue *q = rq->q;
 
 	trace_block_rq_requeue(q, rq);
+	wbt_requeue(q->rq_wb, &rq->issue_stat);
 
 	if (test_and_clear_bit(REQ_ATOM_STARTED, &rq->atomic_flags)) {
 		if (q->dma_drain_size && blk_rq_bytes(rq))
@@ -1270,6 +1276,7 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
 	struct blk_plug *plug;
 	struct request *same_queue_rq = NULL;
 	blk_qc_t cookie;
+	unsigned int wb_acct;
 
 	blk_queue_bounce(q, &bio);
 
@@ -1284,9 +1291,15 @@ static blk_qc_t blk_mq_make_request(struct request_queue *q, struct bio *bio)
 	    blk_attempt_plug_merge(q, bio, &request_count, &same_queue_rq))
 		return BLK_QC_T_NONE;
 
+	wb_acct = wbt_wait(q->rq_wb, bio, NULL);
+
 	rq = blk_mq_map_request(q, bio, &data);
-	if (unlikely(!rq))
+	if (unlikely(!rq)) {
+		__wbt_done(q->rq_wb, wb_acct);
 		return BLK_QC_T_NONE;
+	}
+
+	wbt_track(&rq->issue_stat, wb_acct);
 
 	cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
 
@@ -1363,6 +1376,7 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
 	struct blk_mq_alloc_data data;
 	struct request *rq;
 	blk_qc_t cookie;
+	unsigned int wb_acct;
 
 	blk_queue_bounce(q, &bio);
 
@@ -1379,9 +1393,15 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)
 	} else
 		request_count = blk_plug_queued_count(q);
 
+	wb_acct = wbt_wait(q->rq_wb, bio, NULL);
+
 	rq = blk_mq_map_request(q, bio, &data);
-	if (unlikely(!rq))
+	if (unlikely(!rq)) {
+		__wbt_done(q->rq_wb, wb_acct);
 		return BLK_QC_T_NONE;
+	}
+
+	wbt_track(&rq->issue_stat, wb_acct);
 
 	cookie = blk_tag_to_qc_t(rq->tag, data.hctx->queue_num);
 
@@ -2052,6 +2072,9 @@ void blk_mq_free_queue(struct request_queue *q)
 	list_del_init(&q->all_q_node);
 	mutex_unlock(&all_q_mutex);
 
+	wbt_exit(q->rq_wb);
+	q->rq_wb = NULL;
+
 	blk_mq_del_queue_tag_set(q);
 
 	blk_mq_exit_hw_queues(q, set, set->nr_hw_queues);
block/blk-settings.c +4 -0
index 9cf053759363..c7ccabc0ec3e 100644
@@ -13,6 +13,7 @@
 #include <linux/gfp.h>
 
 #include "blk.h"
+#include "blk-wbt.h"
 
 unsigned long blk_max_low_pfn;
 EXPORT_SYMBOL(blk_max_low_pfn);
@@ -845,6 +846,7 @@ EXPORT_SYMBOL_GPL(blk_queue_flush_queueable);
 void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
 {
 	q->queue_depth = depth;
+	wbt_set_queue_depth(q->rq_wb, depth);
 }
 EXPORT_SYMBOL(blk_set_queue_depth);
 
@@ -868,6 +870,8 @@ void blk_queue_write_cache(struct request_queue *q, bool wc, bool fua)
 	else
 		queue_flag_clear(QUEUE_FLAG_FUA, q);
 	spin_unlock_irq(q->queue_lock);
+
+	wbt_set_write_cache(q->rq_wb, test_bit(QUEUE_FLAG_WC, &q->queue_flags));
 }
 EXPORT_SYMBOL_GPL(blk_queue_write_cache);
 
block/blk-sysfs.c +134 -0

176 lines not on this page · raw message

block/cfq-iosched.c +14 -0
index dcbed8c9c82c..0ef5ef5b5ed2 100644
@@ -16,6 +16,7 @@
 #include <linux/blktrace_api.h>
 #include <linux/blk-cgroup.h>
 #include "blk.h"
+#include "blk-wbt.h"
 
 /*
  * tunables
@@ -3762,9 +3763,11 @@ static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
 	struct cfq_data *cfqd = cic_to_cfqd(cic);
 	struct cfq_queue *cfqq;
 	uint64_t serial_nr;
+	bool nonroot_cg;
 
 	rcu_read_lock();
 	serial_nr = bio_blkcg(bio)->css.serial_nr;
+	nonroot_cg = bio_blkcg(bio) != &blkcg_root;
 	rcu_read_unlock();
 
 	/*
@@ -3775,6 +3778,17 @@ static void check_blkcg_changed(struct cfq_io_cq *cic, struct bio *bio)
 		return;
 
 	/*
+	 * If we have a non-root cgroup, we can depend on that to
+	 * do proper throttling of writes. Turn off wbt for that
+	 * case.
+	 */
+	if (nonroot_cg) {
+		struct request_queue *q = cfqd->queue;
+
+		wbt_disable(q->rq_wb);
+	}
+
+	/*
 	 * Drop reference to queues.  New queues will be assigned in new
 	 * group upon arrival of fresh requests.
 	 */
include/linux/blkdev.h +3 -0
index 6bd5eb56894e..294ee64e7f06 100644
@@ -38,6 +38,7 @@ struct bsg_job;
 struct blkcg_gq;
 struct blk_flush_queue;
 struct pr_ops;
+struct rq_wb;
 
 #define BLKDEV_MIN_RQ	4
 #define BLKDEV_MAX_RQ	128	/* Default maximum */
@@ -381,6 +382,8 @@ struct request_queue {
 	int			nr_rqs[2];	/* # allocated [a]sync rqs */
 	int			nr_rqs_elvpriv;	/* # allocated rqs w/ elvpriv */
 
+	struct rq_wb		*rq_wb;
+
 	/*
 	 * If blkcg is not used, @q->root_rl serves all requests.  If blkcg
 	 * is used, root blkg allocates from @q->root_rl and all other
Signature · 1 line
2.7.4
Reviewed-by on v1 2/8
#6 lore raw

Re: [PATCH 2/8] writeback: add wbc_to_write_flags()

In reply to Jens Axboe
Quoted 6 lines · Jens Axboe

Add wbc_to_write_flags(), which returns the write modifier flags to use, based on a struct writeback_control. No functional changes in this patch, but it prepares us for factoring other wbc fields for write type.

Looks fine,

#11 lore raw

Re: [PATCH 4/8] writeback: track if we're sleeping on progress in balance_dirty_pages()

In reply to Jens Axboe
Quoted 3 lines · Jens Axboe

Note in the bdi_writeback structure whenever a task ends up sleeping waiting for progress. We can use that information in the lower layers to increase the priority of writes.

Do we need to care about atomicy of multiple threads updating the value ?

Otherwise this looks fine.

#15 lore raw

Re: [PATCH 5/8] block: add code to track actual device queue depth

In reply to Jens Axboe
Quoted 6 lines · Jens Axboe

For blk-mq, ->nr_requests does track queue depth, at least at init time. But for the older queue paths, it's simply a soft setting. On top of that, it's generally larger than the hardware setting on purpose, to allow backup of requests for merging.

Fill a hole in struct request with a 'queue_depth' member, that

That would be struct request_queue..

block/blk-settings.c void blk_queue_flush_queueable(struct request_queue *q, bool queueable) · line 849

Quoted 11 lines

/**
+ * blk_set_queue_depth - tell the block layer about the device queue depth
+ * @q: the request queue for the device
+ * @depth: queue depth
+ *
+ */

block/blk-settings.c +5 -0
+void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
+{
+	q->queue_depth = depth;
+}
+EXPORT_SYMBOL(blk_set_queue_depth);

Do we really need this wrapper?

drivers/scsi/scsi.c int scsi_change_queue_depth(struct scsi_device *sdev, int depth) · line 627

Quoted 9 lines

+++ b/drivers/scsi/scsi.c

drivers/scsi/scsi.c +3 -0
@@ -621,6 +621,9 @@ int scsi_change_queue_depth(struct scsi_device *sdev, int depth)
 		wmb();
 	}
 
+	if (sdev->request_queue)
+		blk_set_queue_depth(sdev->request_queue, depth);
+
 	return sdev->queue_depth;

Can we kill the scsi_device queue_depth member and just always use the request_queue one?

author
#12 lore raw

Re: [PATCH 4/8] writeback: track if we're sleeping on progress in balance_dirty_pages()

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig
Quoted 3 lines · Jens Axboe

Note in the bdi_writeback structure whenever a task ends up sleeping waiting for progress. We can use that information in the lower layers to increase the priority of writes.

Do we need to care about atomicy of multiple threads updating the value ?

Otherwise this looks fine.

Don't think we have to care about it too much, it's a soft hint similar wb->dirty_exceeded.

Signature · 1 line
Jens Axboe
author
#16 lore raw

Re: [PATCH 5/8] block: add code to track actual device queue depth

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Jens Axboe

For blk-mq, ->nr_requests does track queue depth, at least at init time. But for the older queue paths, it's simply a soft setting. On top of that, it's generally larger than the hardware setting on purpose, to allow backup of requests for merging.

Fill a hole in struct request with a 'queue_depth' member, that

That would be struct request_queue..

Good catch, will fix.

Quoted 5 lines
Quoted 5 lines

/**
+ * blk_set_queue_depth - tell the block layer about the device queue depth
+ * @q: the request queue for the device
+ * @depth: queue depth
+ *

Quoted 8 lines

block/blk-settings.c void blk_queue_flush_queueable(struct request_queue *q, bool queueable) · line 849

Quoted 6 lines

+ */

block/blk-settings.c +5 -0
+void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
+{
+	q->queue_depth = depth;
+}
+EXPORT_SYMBOL(blk_set_queue_depth);

Do we really need this wrapper?

Not necessarily, just seems like a nicer API than manually setting the field. Not a big deal to me, though.

Quoted 5 lines
Quoted 5 lines

+++ b/drivers/scsi/scsi.c

(quoted diff) +0 -0
@@ -621,6 +621,9 @@ int scsi_change_queue_depth(struct scsi_device *sdev, int depth)
 		wmb();
 	}
 
Quoted 7 lines

drivers/scsi/scsi.c int scsi_change_queue_depth(struct scsi_device *sdev, int depth) · line 627

Quoted 4 lines
drivers/scsi/scsi.c +3 -0
+	if (sdev->request_queue)
+		blk_set_queue_depth(sdev->request_queue, depth);
+
 	return sdev->queue_depth;

Can we kill the scsi_device queue_depth member and just always use the request_queue one?

Yes. I'd prefer that we do that through the SCSI tree, once this is in though.

Signature · 1 line
Jens Axboe
maintainer
#3 lore raw

Re: [PATCH 1/8] block: add WRITE_BACKGROUND

In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig
Quoted 2 lines · Jens Axboe

This adds a new request flag, REQ_BACKGROUND, that callers can use to tell the block layer that this is background (non-urgent) IO.

The subject still says WRITE_BACKGROUND :)

Gah - will fix that up.

Quoted 3 lines

Otherwise looks fine:

Added, thanks.

Signature · 1 line
Jens Axboe
#17 lore raw

Re: [PATCH 5/8] block: add code to track actual device queue depth

In reply to Jens Axboe
Quoted 27 lines · Jens Axboe
Quoted 9 lines · Christoph Hellwig
Quoted 6 lines · Jens Axboe

For blk-mq, ->nr_requests does track queue depth, at least at init time. But for the older queue paths, it's simply a soft setting. On top of that, it's generally larger than the hardware setting on purpose, to allow backup of requests for merging.

Fill a hole in struct request with a 'queue_depth' member, that

That would be struct request_queue..

Good catch, will fix.

Quoted 5 lines
Quoted 5 lines

/**
+ * blk_set_queue_depth - tell the block layer about the device queue depth
+ * @q: the request queue for the device
+ * @depth: queue depth
+ *

Quoted 8 lines

block/blk-settings.c void blk_queue_flush_queueable(struct request_queue *q, bool queueable) · line 849

Quoted 6 lines

+ */

block/blk-settings.c +5 -0
+void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
+{
+	q->queue_depth = depth;
+}
+EXPORT_SYMBOL(blk_set_queue_depth);

Do we really need this wrapper?

Quoted 2 lines

Not necessarily, just seems like a nicer API than manually setting the field. Not a big deal to me, though.

A lot of block code uses this kind of setters so I _think_ it complies with the overall style. But I have no strong opinion on this either...

Byte,
Johannes

Signature · 6 lines
Johannes Thumshirn                                          Storage
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N�rnberg
GF: Felix Imend�rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N�rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
maintainer Reviewed-by on v1 3/8
#9 lore raw

Re: [PATCH 3/8] writeback: mark background writeback as such

In reply to Jens Axboe
Quoted 4 lines · Jens Axboe

If we're doing background type writes, then use the appropriate background write flags for that.

Looks good. You can add:

Honza

Quoted 20 lines
---
 include/linux/writeback.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/writeback.h +2 -0
index 50c96ee8108f..c78f9f0920b5 100644
@@ -107,6 +107,8 @@ static inline int wbc_to_write_flags(struct writeback_control *wbc)
 {
 	if (wbc->sync_mode == WB_SYNC_ALL)
 		return REQ_SYNC;
+	else if (wbc->for_kupdate || wbc->for_background)
+		return REQ_BACKGROUND;
 
 	return 0;
 }
Signature · 1 line
2.7.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Reviewed-by on v1 1/8
#4 lore raw

Re: [PATCH 1/8] block: add WRITE_BACKGROUND

In reply to Jens Axboe
Quoted 4 lines · Jens Axboe

This adds a new request flag, REQ_BACKGROUND, that callers can use to tell the block layer that this is background (non-urgent) IO.

Looks good. You can add:

Honza

Quoted 27 lines
---
 include/linux/blk_types.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/blk_types.h +2 -0
index bb921028e7c5..562ac46cb790 100644
@@ -177,6 +177,7 @@ enum req_flag_bits {
 	__REQ_FUA,		/* forced unit access */
 	__REQ_PREFLUSH,		/* request for cache flush */
 	__REQ_RAHEAD,		/* read ahead, can fail anytime */
+	__REQ_BACKGROUND,	/* background IO */
 	__REQ_NR_BITS,		/* stops here */
 };
 
@@ -192,6 +193,7 @@ enum req_flag_bits {
 #define REQ_FUA			(1ULL << __REQ_FUA)
 #define REQ_PREFLUSH		(1ULL << __REQ_PREFLUSH)
 #define REQ_RAHEAD		(1ULL << __REQ_RAHEAD)
+#define REQ_BACKGROUND		(1ULL << __REQ_BACKGROUND)
 
 #define REQ_FAILFAST_MASK \
 	(REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER)
Signature · 1 line
2.7.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Reviewed-by on v1 5/8
#18 lore raw

Re: [PATCH 5/8] block: add code to track actual device queue depth

In reply to Jens Axboe
Quoted 10 lines · Jens Axboe

For blk-mq, ->nr_requests does track queue depth, at least at init time. But for the older queue paths, it's simply a soft setting. On top of that, it's generally larger than the hardware setting on purpose, to allow backup of requests for merging.

Fill a hole in struct request with a 'queue_depth' member, that drivers can call to more closely inform the block layer of the real queue depth.

The patch looks good to me. You can add:

Honza

Quoted 82 lines
---
 block/blk-settings.c   | 12 ++++++++++++
 drivers/scsi/scsi.c    |  3 +++
 include/linux/blkdev.h | 11 +++++++++++
 3 files changed, 26 insertions(+)
block/blk-settings.c +12 -0
index 55369a65dea2..9cf053759363 100644
@@ -837,6 +837,18 @@ void blk_queue_flush_queueable(struct request_queue *q, bool queueable)
 EXPORT_SYMBOL_GPL(blk_queue_flush_queueable);
 
 /**
+ * blk_set_queue_depth - tell the block layer about the device queue depth
+ * @q:		the request queue for the device
+ * @depth:		queue depth
+ *
+ */
+void blk_set_queue_depth(struct request_queue *q, unsigned int depth)
+{
+	q->queue_depth = depth;
+}
+EXPORT_SYMBOL(blk_set_queue_depth);
+
+/**
  * blk_queue_write_cache - configure queue's write cache
  * @q:		the request queue for the device
  * @wc:		write back cache on or off
drivers/scsi/scsi.c +3 -0
index 1deb6adc411f..75455d4dab68 100644
@@ -621,6 +621,9 @@ int scsi_change_queue_depth(struct scsi_device *sdev, int depth)
 		wmb();
 	}
 
+	if (sdev->request_queue)
+		blk_set_queue_depth(sdev->request_queue, depth);
+
 	return sdev->queue_depth;
 }
 EXPORT_SYMBOL(scsi_change_queue_depth);
include/linux/blkdev.h +11 -0
index 8396da2bb698..0c677fb35ce4 100644
@@ -405,6 +405,8 @@ struct request_queue {
 	struct blk_mq_ctx __percpu	*queue_ctx;
 	unsigned int		nr_queues;
 
+	unsigned int		queue_depth;
+
 	/* hw dispatch queues */
 	struct blk_mq_hw_ctx	**queue_hw_ctx;
 	unsigned int		nr_hw_queues;
@@ -777,6 +779,14 @@ static inline bool blk_write_same_mergeable(struct bio *a, struct bio *b)
 	return false;
 }
 
+static inline unsigned int blk_queue_depth(struct request_queue *q)
+{
+	if (q->queue_depth)
+		return q->queue_depth;
+
+	return q->nr_requests;
+}
+
 /*
  * q->prep_rq_fn return values
  */
@@ -1093,6 +1103,7 @@ extern void blk_limits_io_min(struct queue_limits *limits, unsigned int min);
 extern void blk_queue_io_min(struct request_queue *q, unsigned int min);
 extern void blk_limits_io_opt(struct queue_limits *limits, unsigned int opt);
 extern void blk_queue_io_opt(struct request_queue *q, unsigned int opt);
+extern void blk_set_queue_depth(struct request_queue *q, unsigned int depth);
 extern void blk_set_default_limits(struct queue_limits *lim);
 extern void blk_set_stacking_limits(struct queue_limits *lim);
 extern int blk_stack_limits(struct queue_limits *t, struct queue_limits *b,
Signature · 1 line
2.7.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Reviewed-by on v1 4/8
#13 lore raw

Re: [PATCH 4/8] writeback: track if we're sleeping on progress in balance_dirty_pages()

In reply to Jens Axboe
Quoted 5 lines · Jens Axboe

Note in the bdi_writeback structure whenever a task ends up sleeping waiting for progress. We can use that information in the lower layers to increase the priority of writes.

The patch looks good to me. You can add:

Honza

Quoted 46 lines
---
 include/linux/backing-dev-defs.h | 2 ++
 mm/backing-dev.c                 | 1 +
 mm/page-writeback.c              | 1 +
 3 files changed, 4 insertions(+)
include/linux/backing-dev-defs.h +2 -0
index c357f27d5483..dc5f76d7f648 100644
@@ -116,6 +116,8 @@ struct bdi_writeback {
 	struct list_head work_list;
 	struct delayed_work dwork;	/* work item used for writeback */
 
+	unsigned long dirty_sleep;	/* last wait */
+
 	struct list_head bdi_node;	/* anchored at bdi->wb_list */
 
 #ifdef CONFIG_CGROUP_WRITEBACK
mm/backing-dev.c +1 -0
index 8fde443f36d7..3bfed5ab2475 100644
@@ -310,6 +310,7 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 	spin_lock_init(&wb->work_lock);
 	INIT_LIST_HEAD(&wb->work_list);
 	INIT_DELAYED_WORK(&wb->dwork, wb_workfn);
+	wb->dirty_sleep = jiffies;
 
 	wb->congested = wb_congested_get_create(bdi, blkcg_id, gfp);
 	if (!wb->congested)
mm/page-writeback.c +1 -0
index 439cc63ad903..52e2f8e3b472 100644
@@ -1778,6 +1778,7 @@ static void balance_dirty_pages(struct address_space *mapping,
 					  pause,
 					  start_time);
 		__set_current_state(TASK_KILLABLE);
+		wb->dirty_sleep = now;
 		io_schedule_timeout(pause);
 
 		current->dirty_paused_when = now + pause;
Signature · 1 line
2.7.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#20 lore raw

Re: [PATCH 6/8] block: add scalable completion tracking of requests

In reply to Jens Axboe
Quoted 10 lines · Jens Axboe

For legacy block, we simply track them in the request queue. For blk-mq, we track them on a per-sw queue basis, which we can then sum up through the hardware queues and finally to a per device state.

The stats are tracked in, roughly, 0.1s interval windows.

Add sysfs files to display the stats.

This patch looks mostly good to me but I have one concern: You track statistics in a fixed 134ms window, stats get cleared at the beginning of each window. Now this can interact with the writeback window and latency settings which are dynamic and settable from userspace - so if the writeback code observation window gets set larger than the stats window, things become strange since you'll likely miss quite some observations about read latencies. So I think you need to make sure stats window is always larger than writeback window. Or actually, why do you have something like stats window and don't leave clearing of statistics completely to the writeback tracking code?

Also as a side note - nobody currently uses the mean value of the statistics. It may be faster to track just sum and count so that mean can be computed on request which will be presumably much more rare than current situation where we recompute the mean on each batch update. Actually, that way you could get rid of the batching as well I assume.

Honza

Quoted 569 lines
---
 block/Makefile            |   2 +-
 block/blk-core.c          |   4 +
 block/blk-mq-sysfs.c      |  47 ++++++++++
 block/blk-mq.c            |  14 +++
 block/blk-mq.h            |   3 +
 block/blk-stat.c          | 226 ++++++++++++++++++++++++++++++++++++++++++++++
 block/blk-stat.h          |  37 ++++++++
 block/blk-sysfs.c         |  26 ++++++
 include/linux/blk_types.h |  16 ++++
 include/linux/blkdev.h    |   4 +
 10 files changed, 378 insertions(+), 1 deletion(-)
 create mode 100644 block/blk-stat.c
 create mode 100644 block/blk-stat.h
block/Makefile +1 -1
index 934dac73fb37..2528c596f7ec 100644
@@ -5,7 +5,7 @@
 obj-$(CONFIG_BLOCK) := bio.o elevator.o blk-core.o blk-tag.o blk-sysfs.o \
 			blk-flush.o blk-settings.o blk-ioc.o blk-map.o \
 			blk-exec.o blk-merge.o blk-softirq.o blk-timeout.o \
-			blk-lib.o blk-mq.o blk-mq-tag.o \
+			blk-lib.o blk-mq.o blk-mq-tag.o blk-stat.o \
 			blk-mq-sysfs.o blk-mq-cpumap.o ioctl.o \
 			genhd.o scsi_ioctl.o partition-generic.o ioprio.o \
 			badblocks.o partitions/
block/blk-core.c +4 -0
index 0bfaa54d3e9f..ca77c725b4e5 100644
@@ -2462,6 +2462,8 @@ void blk_start_request(struct request *req)
 {
 	blk_dequeue_request(req);
 
+	blk_stat_set_issue_time(&req->issue_stat);
+
 	/*
 	 * We are now handing the request to the hardware, initialize
 	 * resid_len to full count and add the timeout handler.
@@ -2529,6 +2531,8 @@ bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
 
 	trace_block_rq_complete(req->q, req, nr_bytes);
 
+	blk_stat_add(&req->q->rq_stats[rq_data_dir(req)], req);
+
 	if (!req->bio)
 		return false;
 
block/blk-mq-sysfs.c +47 -0
index 01fb455d3377..633c79a538ea 100644
@@ -259,6 +259,47 @@ static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
 	return ret;
 }
 
+static void blk_mq_stat_clear(struct blk_mq_hw_ctx *hctx)
+{
+	struct blk_mq_ctx *ctx;
+	unsigned int i;
+
+	hctx_for_each_ctx(hctx, ctx, i) {
+		blk_stat_init(&ctx->stat[0]);
+		blk_stat_init(&ctx->stat[1]);
+	}
+}
+
+static ssize_t blk_mq_hw_sysfs_stat_store(struct blk_mq_hw_ctx *hctx,
+					  const char *page, size_t count)
+{
+	blk_mq_stat_clear(hctx);
+	return count;
+}
+
+static ssize_t print_stat(char *page, struct blk_rq_stat *stat, const char *pre)
+{
+	return sprintf(page, "%s samples=%llu, mean=%lld, min=%lld, max=%lld\n",
+			pre, (long long) stat->nr_samples,
+			(long long) stat->mean, (long long) stat->min,
+			(long long) stat->max);
+}
+
+static ssize_t blk_mq_hw_sysfs_stat_show(struct blk_mq_hw_ctx *hctx, char *page)
+{
+	struct blk_rq_stat stat[2];
+	ssize_t ret;
+
+	blk_stat_init(&stat[0]);
+	blk_stat_init(&stat[1]);
+
+	blk_hctx_stat_get(hctx, stat);
+
+	ret = print_stat(page, &stat[0], "read :");
+	ret += print_stat(page + ret, &stat[1], "write:");
+	return ret;
+}
+
 static struct blk_mq_ctx_sysfs_entry blk_mq_sysfs_dispatched = {
 	.attr = {.name = "dispatched", .mode = S_IRUGO },
 	.show = blk_mq_sysfs_dispatched_show,
@@ -317,6 +358,11 @@ static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_poll = {
 	.show = blk_mq_hw_sysfs_poll_show,
 	.store = blk_mq_hw_sysfs_poll_store,
 };
+static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_stat = {
+	.attr = {.name = "stats", .mode = S_IRUGO | S_IWUSR },
+	.show = blk_mq_hw_sysfs_stat_show,
+	.store = blk_mq_hw_sysfs_stat_store,
+};
 
 static struct attribute *default_hw_ctx_attrs[] = {
 	&blk_mq_hw_sysfs_queued.attr,
@@ -327,6 +373,7 @@ static struct attribute *default_hw_ctx_attrs[] = {
 	&blk_mq_hw_sysfs_cpus.attr,
 	&blk_mq_hw_sysfs_active.attr,
 	&blk_mq_hw_sysfs_poll.attr,
+	&blk_mq_hw_sysfs_stat.attr,
 	NULL,
 };
 
block/blk-mq.c +14 -0
index 2da1a0ee3318..4555a76d22a7 100644
@@ -30,6 +30,7 @@
 #include "blk.h"
 #include "blk-mq.h"
 #include "blk-mq-tag.h"
+#include "blk-stat.h"
 
 static DEFINE_MUTEX(all_q_mutex);
 static LIST_HEAD(all_q_list);
@@ -376,10 +377,19 @@ static void blk_mq_ipi_complete_request(struct request *rq)
 	put_cpu();
 }
 
+static void blk_mq_stat_add(struct request *rq)
+{
+	struct blk_rq_stat *stat = &rq->mq_ctx->stat[rq_data_dir(rq)];
+
+	blk_stat_add(stat, rq);
+}
+
 static void __blk_mq_complete_request(struct request *rq)
 {
 	struct request_queue *q = rq->q;
 
+	blk_mq_stat_add(rq);
+
 	if (!q->softirq_done_fn)
 		blk_mq_end_request(rq, rq->errors);
 	else
@@ -423,6 +433,8 @@ void blk_mq_start_request(struct request *rq)
 	if (unlikely(blk_bidi_rq(rq)))
 		rq->next_rq->resid_len = blk_rq_bytes(rq->next_rq);
 
+	blk_stat_set_issue_time(&rq->issue_stat);
+
 	blk_add_timer(rq);
 
 	/*
@@ -1708,6 +1720,8 @@ static void blk_mq_init_cpu_queues(struct request_queue *q,
 		spin_lock_init(&__ctx->lock);
 		INIT_LIST_HEAD(&__ctx->rq_list);
 		__ctx->queue = q;
+		blk_stat_init(&__ctx->stat[0]);
+		blk_stat_init(&__ctx->stat[1]);
 
 		/* If the cpu isn't online, the cpu is mapped to first hctx */
 		if (!cpu_online(i))
block/blk-mq.h +3 -0
index e5d25249028c..8cf16cb69f64 100644
@@ -1,6 +1,8 @@
 #ifndef INT_BLK_MQ_H
 #define INT_BLK_MQ_H
 
+#include "blk-stat.h"
+
 struct blk_mq_tag_set;
 
 struct blk_mq_ctx {
@@ -18,6 +20,7 @@ struct blk_mq_ctx {
 
 	/* incremented at completion time */
 	unsigned long		____cacheline_aligned_in_smp rq_completed[2];
+	struct blk_rq_stat	stat[2];
 
 	struct request_queue	*queue;
 	struct kobject		kobj;
block/blk-stat.c new file +226 -0
new file mode 100644
index 000000000000..642afdc6d0f8
@@ -0,0 +1,226 @@
+/*
+ * Block stat tracking code
+ *
+ * Copyright (C) 2016 Jens Axboe
+ */
+#include <linux/kernel.h>
+#include <linux/blk-mq.h>
+
+#include "blk-stat.h"
+#include "blk-mq.h"
+
+static void blk_stat_flush_batch(struct blk_rq_stat *stat)
+{
+	if (!stat->nr_batch)
+		return;
+	if (!stat->nr_samples)
+		stat->mean = div64_s64(stat->batch, stat->nr_batch);
+	else {
+		stat->mean = div64_s64((stat->mean * stat->nr_samples) +
+					stat->batch,
+					stat->nr_samples + stat->nr_batch);
+	}
+
+	stat->nr_samples += stat->nr_batch;
+	stat->nr_batch = stat->batch = 0;
+}
+
+void blk_stat_sum(struct blk_rq_stat *dst, struct blk_rq_stat *src)
+{
+	if (!src->nr_samples)
+		return;
+
+	blk_stat_flush_batch(src);
+
+	dst->min = min(dst->min, src->min);
+	dst->max = max(dst->max, src->max);
+
+	if (!dst->nr_samples)
+		dst->mean = src->mean;
+	else {
+		dst->mean = div64_s64((src->mean * src->nr_samples) +
+					(dst->mean * dst->nr_samples),
+					dst->nr_samples + src->nr_samples);
+	}
+	dst->nr_samples += src->nr_samples;
+}
+
+static void blk_mq_stat_get(struct request_queue *q, struct blk_rq_stat *dst)
+{
+	struct blk_mq_hw_ctx *hctx;
+	struct blk_mq_ctx *ctx;
+	uint64_t latest = 0;
+	int i, j, nr;
+
+	blk_stat_init(&dst[0]);
+	blk_stat_init(&dst[1]);
+
+	nr = 0;
+	do {
+		uint64_t newest = 0;
+
+		queue_for_each_hw_ctx(q, hctx, i) {
+			hctx_for_each_ctx(hctx, ctx, j) {
+				if (!ctx->stat[0].nr_samples &&
+				    !ctx->stat[1].nr_samples)
+					continue;
+				if (ctx->stat[0].time > newest)
+					newest = ctx->stat[0].time;
+				if (ctx->stat[1].time > newest)
+					newest = ctx->stat[1].time;
+			}
+		}
+
+		/*
+		 * No samples
+		 */
+		if (!newest)
+			break;
+
+		if (newest > latest)
+			latest = newest;
+
+		queue_for_each_hw_ctx(q, hctx, i) {
+			hctx_for_each_ctx(hctx, ctx, j) {
+				if (ctx->stat[0].time == newest) {
+					blk_stat_sum(&dst[0], &ctx->stat[0]);
+					nr++;
+				}
+				if (ctx->stat[1].time == newest) {
+					blk_stat_sum(&dst[1], &ctx->stat[1]);
+					nr++;
+				}
+			}
+		}
+		/*
+		 * If we race on finding an entry, just loop back again.
+		 * Should be very rare.
+		 */
+	} while (!nr);
+
+	dst[0].time = dst[1].time = latest;
+}
+
+void blk_queue_stat_get(struct request_queue *q, struct blk_rq_stat *dst)
+{
+	if (q->mq_ops)
+		blk_mq_stat_get(q, dst);
+	else {
+		memcpy(&dst[0], &q->rq_stats[0], sizeof(struct blk_rq_stat));
+		memcpy(&dst[1], &q->rq_stats[1], sizeof(struct blk_rq_stat));
+	}
+}
+
+void blk_hctx_stat_get(struct blk_mq_hw_ctx *hctx, struct blk_rq_stat *dst)
+{
+	struct blk_mq_ctx *ctx;
+	unsigned int i, nr;
+
+	nr = 0;
+	do {
+		uint64_t newest = 0;
+
+		hctx_for_each_ctx(hctx, ctx, i) {
+			if (!ctx->stat[0].nr_samples &&
+			    !ctx->stat[1].nr_samples)
+				continue;
+
+			if (ctx->stat[0].time > newest)
+				newest = ctx->stat[0].time;
+			if (ctx->stat[1].time > newest)
+				newest = ctx->stat[1].time;
+		}
+
+		if (!newest)
+			break;
+
+		hctx_for_each_ctx(hctx, ctx, i) {
+			if (ctx->stat[0].time == newest) {
+				blk_stat_sum(&dst[0], &ctx->stat[0]);
+				nr++;
+			}
+			if (ctx->stat[1].time == newest) {
+				blk_stat_sum(&dst[1], &ctx->stat[1]);
+				nr++;
+			}
+		}
+		/*
+		 * If we race on finding an entry, just loop back again.
+		 * Should be very rare, as the window is only updated
+		 * occasionally
+		 */
+	} while (!nr);
+}
+
+static void __blk_stat_init(struct blk_rq_stat *stat, s64 time_now)
+{
+	stat->min = -1ULL;
+	stat->max = stat->nr_samples = stat->mean = 0;
+	stat->batch = stat->nr_batch = 0;
+	stat->time = time_now & BLK_STAT_NSEC_MASK;
+}
+
+void blk_stat_init(struct blk_rq_stat *stat)
+{
+	__blk_stat_init(stat, ktime_to_ns(ktime_get()));
+}
+
+static bool __blk_stat_is_current(struct blk_rq_stat *stat, s64 now)
+{
+	return (now & BLK_STAT_NSEC_MASK) == (stat->time & BLK_STAT_NSEC_MASK);
+}
+
+bool blk_stat_is_current(struct blk_rq_stat *stat)
+{
+	return __blk_stat_is_current(stat, ktime_to_ns(ktime_get()));
+}
+
+void blk_stat_add(struct blk_rq_stat *stat, struct request *rq)
+{
+	s64 now, value;
+
+	now = __blk_stat_time(ktime_to_ns(ktime_get()));
+	if (now < blk_stat_time(&rq->issue_stat))
+		return;
+
+	if (!__blk_stat_is_current(stat, now))
+		__blk_stat_init(stat, now);
+
+	value = now - blk_stat_time(&rq->issue_stat);
+	if (value > stat->max)
+		stat->max = value;
+	if (value < stat->min)
+		stat->min = value;
+
+	if (stat->batch + value < stat->batch ||
+	    stat->nr_batch + 1 == BLK_RQ_STAT_BATCH)
+		blk_stat_flush_batch(stat);
+
+	stat->batch += value;
+	stat->nr_batch++;
+}
+
+void blk_stat_clear(struct request_queue *q)
+{
+	if (q->mq_ops) {
+		struct blk_mq_hw_ctx *hctx;
+		struct blk_mq_ctx *ctx;
+		int i, j;
+
+		queue_for_each_hw_ctx(q, hctx, i) {
+			hctx_for_each_ctx(hctx, ctx, j) {
+				blk_stat_init(&ctx->stat[0]);
+				blk_stat_init(&ctx->stat[1]);
+			}
+		}
+	} else {
+		blk_stat_init(&q->rq_stats[0]);
+		blk_stat_init(&q->rq_stats[1]);
+	}
+}
+
+void blk_stat_set_issue_time(struct blk_issue_stat *stat)
+{
+	stat->time = (stat->time & BLK_STAT_MASK) |
+			(ktime_to_ns(ktime_get()) & BLK_STAT_TIME_MASK);
+}
block/blk-stat.h new file +37 -0
new file mode 100644
index 000000000000..26b1f45dff26
@@ -0,0 +1,37 @@
+#ifndef BLK_STAT_H
+#define BLK_STAT_H
+
+/*
+ * ~0.13s window as a power-of-2 (2^27 nsecs)
+ */
+#define BLK_STAT_NSEC		134217728ULL
+#define BLK_STAT_NSEC_MASK	~(BLK_STAT_NSEC - 1)
+
+/*
+ * Upper 3 bits can be used elsewhere
+ */
+#define BLK_STAT_RES_BITS	3
+#define BLK_STAT_SHIFT		(64 - BLK_STAT_RES_BITS)
+#define BLK_STAT_TIME_MASK	((1ULL << BLK_STAT_SHIFT) - 1)
+#define BLK_STAT_MASK		~BLK_STAT_TIME_MASK
+
+void blk_stat_add(struct blk_rq_stat *, struct request *);
+void blk_hctx_stat_get(struct blk_mq_hw_ctx *, struct blk_rq_stat *);
+void blk_queue_stat_get(struct request_queue *, struct blk_rq_stat *);
+void blk_stat_clear(struct request_queue *q);
+void blk_stat_init(struct blk_rq_stat *);
+void blk_stat_sum(struct blk_rq_stat *, struct blk_rq_stat *);
+bool blk_stat_is_current(struct blk_rq_stat *);
+void blk_stat_set_issue_time(struct blk_issue_stat *);
+
+static inline u64 __blk_stat_time(u64 time)
+{
+	return time & BLK_STAT_TIME_MASK;
+}
+
+static inline u64 blk_stat_time(struct blk_issue_stat *stat)
+{
+	return __blk_stat_time(stat->time);
+}
+
+#endif
block/blk-sysfs.c +26 -0
index 488c2e28feb8..5bb4648f434a 100644
@@ -401,6 +401,26 @@ static ssize_t queue_dax_show(struct request_queue *q, char *page)
 	return queue_var_show(blk_queue_dax(q), page);
 }
 
+static ssize_t print_stat(char *page, struct blk_rq_stat *stat, const char *pre)
+{
+	return sprintf(page, "%s samples=%llu, mean=%lld, min=%lld, max=%lld\n",
+			pre, (long long) stat->nr_samples,
+			(long long) stat->mean, (long long) stat->min,
+			(long long) stat->max);
+}
+
+static ssize_t queue_stats_show(struct request_queue *q, char *page)
+{
+	struct blk_rq_stat stat[2];
+	ssize_t ret;
+
+	blk_queue_stat_get(q, stat);
+
+	ret = print_stat(page, &stat[0], "read :");
+	ret += print_stat(page + ret, &stat[1], "write:");
+	return ret;
+}
+
 static struct queue_sysfs_entry queue_requests_entry = {
 	.attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
 	.show = queue_requests_show,
@@ -553,6 +573,11 @@ static struct queue_sysfs_entry queue_dax_entry = {
 	.show = queue_dax_show,
 };
 
+static struct queue_sysfs_entry queue_stats_entry = {
+	.attr = {.name = "stats", .mode = S_IRUGO },
+	.show = queue_stats_show,
+};
+
 static struct attribute *default_attrs[] = {
 	&queue_requests_entry.attr,
 	&queue_ra_entry.attr,
@@ -582,6 +607,7 @@ static struct attribute *default_attrs[] = {
 	&queue_poll_entry.attr,
 	&queue_wc_entry.attr,
 	&queue_dax_entry.attr,
+	&queue_stats_entry.attr,
 	NULL,
 };
 
include/linux/blk_types.h +16 -0
index 562ac46cb790..4d0044d09984 100644
@@ -250,4 +250,20 @@ static inline unsigned int blk_qc_t_to_tag(blk_qc_t cookie)
 	return cookie & ((1u << BLK_QC_T_SHIFT) - 1);
 }
 
+struct blk_issue_stat {
+	u64 time;
+};
+
+#define BLK_RQ_STAT_BATCH	64
+
+struct blk_rq_stat {
+	s64 mean;
+	u64 min;
+	u64 max;
+	s32 nr_samples;
+	s32 nr_batch;
+	u64 batch;
+	s64 time;
+};
+
 #endif /* __LINUX_BLK_TYPES_H */
include/linux/blkdev.h +4 -0
index 0c677fb35ce4..6bd5eb56894e 100644
@@ -197,6 +197,7 @@ struct request {
 	struct gendisk *rq_disk;
 	struct hd_struct *part;
 	unsigned long start_time;
+	struct blk_issue_stat issue_stat;
 #ifdef CONFIG_BLK_CGROUP
 	struct request_list *rl;		/* rl this rq is alloced from */
 	unsigned long long start_time_ns;
@@ -492,6 +493,9 @@ struct request_queue {
 
 	unsigned int		nr_sorted;
 	unsigned int		in_flight[2];
+
+	struct blk_rq_stat	rq_stats[2];
+
 	/*
 	 * Number of active block driver functions for which blk_drain_queue()
 	 * must wait. Must be incremented around functions that unlock the
Signature · 1 line
2.7.4
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#28 lore raw

Re: [PATCH 7/8] blk-wbt: add general throttling mechanism

In reply to Jens Axboe
Quoted 8 lines · Jens Axboe

We can hook this up to the block layer, to help throttle buffered writes.

wbt registers a few trace points that can be used to track what is happening in the system:

wbt_lat: 259:0: latency 2446318
wbt_stat: 259:0: rmean=2446318, rmin=2446318, rmax=2446318, rsamples=1,

Quoted 7 lines
               wmean=518866, wmin=15522, wmax=5330353, wsamples=57

wbt_step: 259:0: step down: step=1, window=72727272, background=8, normal=16, max=32

This shows a sync issue event (wbt_lat) that exceeded it's time. wbt_stat dumps the current read/write stats for that window, and wbt_step shows a step down event where we now scale back writes. Each trace includes the device, 259:0 in this case.

Just one serious question and one nit below:

Quoted 18 lines
block/blk-wbt.c new file +18 -0
+void __wbt_done(struct rq_wb *rwb, enum wbt_flags wb_acct)
+{
+	struct rq_wait *rqw;
+	int inflight, limit;
+
+	if (!(wb_acct & WBT_TRACKED))
+		return;
+
+	rqw = get_rq_wait(rwb, wb_acct & WBT_KSWAPD);
+	inflight = atomic_dec_return(&rqw->inflight);
+
+	/*
+	 * wbt got disabled with IO in flight. Wake up any potential
+	 * waiters, we don't have to do more than that.
+	 */
+	if (unlikely(!rwb_enabled(rwb))) {
+		rwb_wake_all(rwb);
+		return;

block/blk-wbt.c line 148

Quoted 10 lines
block/blk-wbt.c new file +10 -0
+	}
+
+	/*
+	 * If the device does write back caching, drop further down
+	 * before we wake people up.
+	 */
+	if (rwb->wc && !wb_recent_wait(rwb))
+		limit = 0;
+	else
+		limit = rwb->wb_normal;

So for devices with write cache, you will completely drain the device before waking anybody waiting to issue new requests. Isn't it too strict? In particular may_queue() will allow new writers to issue new writes once we drop below the limit so it can happen that some processes will be effectively starved waiting in may_queue?

Quoted 28 lines
block/blk-wbt.c new file +28 -0
+static void wb_timer_fn(unsigned long data)
+{
+	struct rq_wb *rwb = (struct rq_wb *) data;
+	unsigned int inflight = wbt_inflight(rwb);
+	int status;
+
+	status = latency_exceeded(rwb);
+
+	trace_wbt_timer(rwb->bdi, status, rwb->scale_step, inflight);
+
+	/*
+	 * If we exceeded the latency target, step down. If we did not,
+	 * step one level up. If we don't know enough to say either exceeded
+	 * or ok, then don't do anything.
+	 */
+	switch (status) {
+	case LAT_EXCEEDED:
+		scale_down(rwb, true);
+		break;
+	case LAT_OK:
+		scale_up(rwb);
+		break;
+	case LAT_UNKNOWN_WRITES:
+		scale_up(rwb);
+		break;
+	case LAT_UNKNOWN:
+		if (++rwb->unknown_cnt < RWB_UNKNOWN_BUMP)
+			break;

block/blk-wbt.c line 452

Quoted 10 lines
block/blk-wbt.c new file +10 -0
+		/*
+		 * We get here for two reasons:
+		 *
+		 * 1) We previously scaled reduced depth, and we currently
+		 *    don't have a valid read/write sample. For that case,
+		 *    slowly return to center state (step == 0).
+		 * 2) We started a the center step, but don't have a valid
+		 *    read/write sample, but we do have writes going on.
+		 *    Allow step to go negative, to increase write perf.
+		 */

I think part 2) of the comment now belongs to LAT_UNKNOWN_WRITES label.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#36 lore raw

Re: [PATCH 8/8] block: hook up writeback throttling

In reply to Jens Axboe
Quoted 35 lines · Jens Axboe

Enable throttling of buffered writeback to make it a lot more smooth, and has way less impact on other system activity. Background writeback should be, by definition, background activity. The fact that we flush huge bundles of it at the time means that it potentially has heavy impacts on foreground workloads, which isn't ideal. We can't easily limit the sizes of writes that we do, since that would impact file system layout in the presence of delayed allocation. So just throttle back buffered writeback, unless someone is waiting for it.

The algorithm for when to throttle takes its inspiration in the CoDel networking scheduling algorithm. Like CoDel, blk-wb monitors the minimum latencies of requests over a window of time. In that window of time, if the minimum latency of any request exceeds a given target, then a scale count is incremented and the queue depth is shrunk. The next monitoring window is shrunk accordingly. Unlike CoDel, if we hit a window that exhibits good behavior, then we simply increment the scale count and re-calculate the limits for that scale value. This prevents us from oscillating between a close-to-ideal value and max all the time, instead remaining in the windows where we get good behavior.

Unlike CoDel, blk-wb allows the scale count to to negative. This happens if we primarily have writes going on. Unlike positive scale counts, this doesn't change the size of the monitoring window. When the heavy writers finish, blk-bw quickly snaps back to it's stable state of a zero scale count.

The patch registers two sysfs entries. The first one, 'wb_window_usec', defines the window of monitoring. The second one, 'wb_lat_usec', sets the latency target for the window. It defaults to 2 msec for non-rotational storage, and 75 msec for rotational storage. Setting this value to '0' disables blk-wb. Generally, a user would not have to touch these settings.

Quoted 5 lines

We don't enable WBT on devices that are managed with CFQ, and have a non-root block cgroup attached. If we have a proportional share setup on this particular disk, then the wbt throttling will interfere with that. We don't have a strong need for wbt for that case, since we will rely on CFQ doing that for us.

Just one nit: Don't you miss wbt_exit() call for legacy block layer? I don't see where that happens.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#37 lore raw

Re: [PATCH 8/8] block: hook up writeback throttling

Quoted 36 lines · Jan Kara
Quoted 35 lines · Jens Axboe

Enable throttling of buffered writeback to make it a lot more smooth, and has way less impact on other system activity. Background writeback should be, by definition, background activity. The fact that we flush huge bundles of it at the time means that it potentially has heavy impacts on foreground workloads, which isn't ideal. We can't easily limit the sizes of writes that we do, since that would impact file system layout in the presence of delayed allocation. So just throttle back buffered writeback, unless someone is waiting for it.

The algorithm for when to throttle takes its inspiration in the CoDel networking scheduling algorithm. Like CoDel, blk-wb monitors the minimum latencies of requests over a window of time. In that window of time, if the minimum latency of any request exceeds a given target, then a scale count is incremented and the queue depth is shrunk. The next monitoring window is shrunk accordingly. Unlike CoDel, if we hit a window that exhibits good behavior, then we simply increment the scale count and re-calculate the limits for that scale value. This prevents us from oscillating between a close-to-ideal value and max all the time, instead remaining in the windows where we get good behavior.

Unlike CoDel, blk-wb allows the scale count to to negative. This happens if we primarily have writes going on. Unlike positive scale counts, this doesn't change the size of the monitoring window. When the heavy writers finish, blk-bw quickly snaps back to it's stable state of a zero scale count.

The patch registers two sysfs entries. The first one, 'wb_window_usec', defines the window of monitoring. The second one, 'wb_lat_usec', sets the latency target for the window. It defaults to 2 msec for non-rotational storage, and 75 msec for rotational storage. Setting this value to '0' disables blk-wb. Generally, a user would not have to touch these settings.

Quoted 8 lines
Quoted 5 lines

We don't enable WBT on devices that are managed with CFQ, and have a non-root block cgroup attached. If we have a proportional share setup on this particular disk, then the wbt throttling will interfere with that. We don't have a strong need for wbt for that case, since we will rely on CFQ doing that for us.

Just one nit: Don't you miss wbt_exit() call for legacy block layer? I don't see where that happens.

Huh yes, good point, that must have been lost along the way. I'll readd it.

Signature · 1 line
Jens Axboe
author
#21 lore raw

Re: [PATCH 6/8] block: add scalable completion tracking of requests

In reply to Jan Kara
Quoted 12 lines · Jan Kara
Quoted 10 lines · Jens Axboe

For legacy block, we simply track them in the request queue. For blk-mq, we track them on a per-sw queue basis, which we can then sum up through the hardware queues and finally to a per device state.

The stats are tracked in, roughly, 0.1s interval windows.

Add sysfs files to display the stats.

Quoted 10 lines

This patch looks mostly good to me but I have one concern: You track statistics in a fixed 134ms window, stats get cleared at the beginning of each window. Now this can interact with the writeback window and latency settings which are dynamic and settable from userspace - so if the writeback code observation window gets set larger than the stats window, things become strange since you'll likely miss quite some observations about read latencies. So I think you need to make sure stats window is always larger than writeback window. Or actually, why do you have something like stats window and don't leave clearing of statistics completely to the writeback tracking code?

That's a good point, and there actually used to be a comment to that effect in the code. I think the best solution here would be to make the stats code mask available somewhere, and allow a consumer of the stats to request a larger window.

Similarly, we could make the stat window be driven by the consumer, as you suggest.

Currently there are two pending submissions that depend on the stats code. One is this writeback series, and the other one is the hybrid polling code. The latter does not really care about the window size as such, since it has no monitoring window of its own, and it wants the auto-clearing as well.

I don't mind working on additions for this, but I'd prefer if we could layer them on top of the existing series instead of respinning it. There's considerable test time on the existing patchset. Would that work for you? Especially collapsing the stats and wbt windows would require some re-architecting.

Quoted 5 lines

Also as a side note - nobody currently uses the mean value of the statistics. It may be faster to track just sum and count so that mean can be computed on request which will be presumably much more rare than current situation where we recompute the mean on each batch update. Actually, that way you could get rid of the batching as well I assume.

That could be opt-in as well. The poll code uses it. And fwiw, it is exposed through sysfs as well.

Signature · 1 line
Jens Axboe
maintainer
#29 lore raw

Re: [PATCH 7/8] blk-wbt: add general throttling mechanism

In reply to Jan Kara
Quoted 45 lines · Jan Kara
Quoted 8 lines · Jens Axboe

We can hook this up to the block layer, to help throttle buffered writes.

wbt registers a few trace points that can be used to track what is happening in the system:

wbt_lat: 259:0: latency 2446318
wbt_stat: 259:0: rmean=2446318, rmin=2446318, rmax=2446318, rsamples=1,

Quoted 7 lines
               wmean=518866, wmin=15522, wmax=5330353, wsamples=57

wbt_step: 259:0: step down: step=1, window=72727272, background=8, normal=16, max=32

This shows a sync issue event (wbt_lat) that exceeded it's time. wbt_stat dumps the current read/write stats for that window, and wbt_step shows a step down event where we now scale back writes. Each trace includes the device, 259:0 in this case.

Just one serious question and one nit below:

Quoted 26 lines
block/blk-wbt.c new file +26 -0
+void __wbt_done(struct rq_wb *rwb, enum wbt_flags wb_acct)
+{
+	struct rq_wait *rqw;
+	int inflight, limit;
+
+	if (!(wb_acct & WBT_TRACKED))
+		return;
+
+	rqw = get_rq_wait(rwb, wb_acct & WBT_KSWAPD);
+	inflight = atomic_dec_return(&rqw->inflight);
+
+	/*
+	 * wbt got disabled with IO in flight. Wake up any potential
+	 * waiters, we don't have to do more than that.
+	 */
+	if (unlikely(!rwb_enabled(rwb))) {
+		rwb_wake_all(rwb);
+		return;
+	}
+
+	/*
+	 * If the device does write back caching, drop further down
+	 * before we wake people up.
+	 */
+	if (rwb->wc && !wb_recent_wait(rwb))
+		limit = 0;
Quoted 8 lines

block/blk-wbt.c line 148

Quoted 2 lines
block/blk-wbt.c new file +2 -0
+	else
+		limit = rwb->wb_normal;

So for devices with write cache, you will completely drain the device before waking anybody waiting to issue new requests. Isn't it too strict? In particular may_queue() will allow new writers to issue new writes once we drop below the limit so it can happen that some processes will be effectively starved waiting in may_queue?

It is strict, and perhaps too strict. In testing, it's the only method that's proven to keep the writeback caching devices in check. It will round robin the writers, if we have more, which isn't necessarily a bad thing. Each will get to do a burst of depth writes, then wait for a new one.

Quoted 7 lines
Quoted 7 lines
block/blk-wbt.c new file +7 -0
+	case LAT_UNKNOWN:
+		if (++rwb->unknown_cnt < RWB_UNKNOWN_BUMP)
+			break;
+		/*
+		 * We get here for two reasons:
+		 *
+		 * 1) We previously scaled reduced depth, and we currently
Quoted 8 lines

block/blk-wbt.c line 452

Quoted 6 lines
block/blk-wbt.c new file +6 -0
+		 *    don't have a valid read/write sample. For that case,
+		 *    slowly return to center state (step == 0).
+		 * 2) We started a the center step, but don't have a valid
+		 *    read/write sample, but we do have writes going on.
+		 *    Allow step to go negative, to increase write perf.
+		 */

I think part 2) of the comment now belongs to LAT_UNKNOWN_WRITES label.

Indeed, that got moved around a bit, I'll fix that up.

Signature · 1 line
Jens Axboe
maintainer
#30 lore raw

Re: [PATCH 7/8] blk-wbt: add general throttling mechanism

Quoted 55 lines · Jens Axboe
Quoted 45 lines · Jan Kara
Quoted 8 lines · Jens Axboe

We can hook this up to the block layer, to help throttle buffered writes.

wbt registers a few trace points that can be used to track what is happening in the system:

wbt_lat: 259:0: latency 2446318
wbt_stat: 259:0: rmean=2446318, rmin=2446318, rmax=2446318, rsamples=1,

Quoted 7 lines
               wmean=518866, wmin=15522, wmax=5330353, wsamples=57

wbt_step: 259:0: step down: step=1, window=72727272, background=8, normal=16, max=32

This shows a sync issue event (wbt_lat) that exceeded it's time. wbt_stat dumps the current read/write stats for that window, and wbt_step shows a step down event where we now scale back writes. Each trace includes the device, 259:0 in this case.

Just one serious question and one nit below:

Quoted 26 lines
block/blk-wbt.c new file +26 -0
+void __wbt_done(struct rq_wb *rwb, enum wbt_flags wb_acct)
+{
+	struct rq_wait *rqw;
+	int inflight, limit;
+
+	if (!(wb_acct & WBT_TRACKED))
+		return;
+
+	rqw = get_rq_wait(rwb, wb_acct & WBT_KSWAPD);
+	inflight = atomic_dec_return(&rqw->inflight);
+
+	/*
+	 * wbt got disabled with IO in flight. Wake up any potential
+	 * waiters, we don't have to do more than that.
+	 */
+	if (unlikely(!rwb_enabled(rwb))) {
+		rwb_wake_all(rwb);
+		return;
+	}
+
+	/*
+	 * If the device does write back caching, drop further down
+	 * before we wake people up.
+	 */
+	if (rwb->wc && !wb_recent_wait(rwb))
+		limit = 0;
Quoted 8 lines

block/blk-wbt.c line 148

Quoted 2 lines
block/blk-wbt.c new file +2 -0
+	else
+		limit = rwb->wb_normal;

So for devices with write cache, you will completely drain the device before waking anybody waiting to issue new requests. Isn't it too strict? In particular may_queue() will allow new writers to issue new writes once we drop below the limit so it can happen that some processes will be effectively starved waiting in may_queue?

Quoted 5 lines

It is strict, and perhaps too strict. In testing, it's the only method that's proven to keep the writeback caching devices in check. It will round robin the writers, if we have more, which isn't necessarily a bad thing. Each will get to do a burst of depth writes, then wait for a new one.

Well, I'm more concerned about a situation where one writer does a bursty write and blocks sleeping in may_queue(). Another writer produces a steady flow of write requests so that never causes the write queue to completely drain but that writer also never blocks in may_queue() when it starts queueing after write queue has somewhat drained because it never submits many requests in parallel. In such case the first writer would get starved AFAIU.

Also I'm not sure why such logic for devices with writeback cache is needed. Sure the disk is fast to accept writes but if that causes long read latencies, we should scale down the writeback limits so that we eventually end up submitting only one write request anyway - effectively the same thing as limit=0 - won't we?

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#22 lore raw

Re: [PATCH 6/8] block: add scalable completion tracking of requests

In reply to Jens Axboe
Quoted 38 lines · Jens Axboe
Quoted 12 lines · Jan Kara
Quoted 10 lines · Jens Axboe

For legacy block, we simply track them in the request queue. For blk-mq, we track them on a per-sw queue basis, which we can then sum up through the hardware queues and finally to a per device state.

The stats are tracked in, roughly, 0.1s interval windows.

Add sysfs files to display the stats.

Quoted 10 lines

This patch looks mostly good to me but I have one concern: You track statistics in a fixed 134ms window, stats get cleared at the beginning of each window. Now this can interact with the writeback window and latency settings which are dynamic and settable from userspace - so if the writeback code observation window gets set larger than the stats window, things become strange since you'll likely miss quite some observations about read latencies. So I think you need to make sure stats window is always larger than writeback window. Or actually, why do you have something like stats window and don't leave clearing of statistics completely to the writeback tracking code?

That's a good point, and there actually used to be a comment to that effect in the code. I think the best solution here would be to make the stats code mask available somewhere, and allow a consumer of the stats to request a larger window.

Similarly, we could make the stat window be driven by the consumer, as you suggest.

Currently there are two pending submissions that depend on the stats code. One is this writeback series, and the other one is the hybrid polling code. The latter does not really care about the window size as such, since it has no monitoring window of its own, and it wants the auto-clearing as well.

Quoted 5 lines

I don't mind working on additions for this, but I'd prefer if we could layer them on top of the existing series instead of respinning it. There's considerable test time on the existing patchset. Would that work for you? Especially collapsing the stats and wbt windows would require some re-architecting.

OK, that works for me. Actually, when thinking about this, I have one more
suggestion: Do we really want to expose the wbt window as a sysfs tunable? I guess it is good for initial experiments but longer term having the wbt window length be a function of target read latency might be better. Generally you want the window length to be considerably larger than the target latency but OTOH not too large so that the algorithm can react reasonably quickly so that suggests it could really be autotuned (and we scale the window anyway to adapt it to current situation).

Quoted 8 lines
Quoted 5 lines

Also as a side note - nobody currently uses the mean value of the statistics. It may be faster to track just sum and count so that mean can be computed on request which will be presumably much more rare than current situation where we recompute the mean on each batch update. Actually, that way you could get rid of the batching as well I assume.

That could be opt-in as well. The poll code uses it. And fwiw, it is exposed through sysfs as well.

Yeah, my point was that just doing the division in response to sysfs read or actual request to read the average is likely going to be less expensive than having to do it on each batch completion (actually, you seem to have that batching code only so that you don't have to do the division too often). Whether my suggestion is right depends on how often polling code actually needs to read the average...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#31 lore raw

Re: [PATCH 7/8] blk-wbt: add general throttling mechanism

In reply to Jan Kara
Quoted 12 lines · Jan Kara
Quoted 11 lines
Quoted 5 lines

So for devices with write cache, you will completely drain the device before waking anybody waiting to issue new requests. Isn't it too strict? In particular may_queue() will allow new writers to issue new writes once we drop below the limit so it can happen that some processes will be effectively starved waiting in may_queue?

It is strict, and perhaps too strict. In testing, it's the only method that's proven to keep the writeback caching devices in check. It will round robin the writers, if we have more, which isn't necessarily a bad thing. Each will get to do a burst of depth writes, then wait for a new one.

Quoted 7 lines

Well, I'm more concerned about a situation where one writer does a bursty write and blocks sleeping in may_queue(). Another writer produces a steady flow of write requests so that never causes the write queue to completely drain but that writer also never blocks in may_queue() when it starts queueing after write queue has somewhat drained because it never submits many requests in parallel. In such case the first writer would get starved AFAIU.

I see what you are saying. I can modify the logic to ensure that if we do have a waiter, we queue up others behind it. That should get rid of that concern.

Quoted 5 lines

Also I'm not sure why such logic for devices with writeback cache is needed. Sure the disk is fast to accept writes but if that causes long read latencies, we should scale down the writeback limits so that we eventually end up submitting only one write request anyway - effectively the same thing as limit=0 - won't we?

Basically we want to avoid getting into that situation. The problem with write caching is that it takes a while for you to notice that anything is wrong, and when you do, you are way down in the hole. That causes the first violations to be pretty bad.

I'm fine with playing with this logic and improving it, but I'd rather wait for a 2nd series for that.

Signature · 1 line
Jens Axboe
author
#23 lore raw

Re: [PATCH 6/8] block: add scalable completion tracking of requests

In reply to Jan Kara
Quoted 45 lines · Jan Kara
Quoted 38 lines · Jens Axboe
Quoted 12 lines · Jan Kara
Quoted 10 lines · Jens Axboe

For legacy block, we simply track them in the request queue. For blk-mq, we track them on a per-sw queue basis, which we can then sum up through the hardware queues and finally to a per device state.

The stats are tracked in, roughly, 0.1s interval windows.

Add sysfs files to display the stats.

Quoted 10 lines

This patch looks mostly good to me but I have one concern: You track statistics in a fixed 134ms window, stats get cleared at the beginning of each window. Now this can interact with the writeback window and latency settings which are dynamic and settable from userspace - so if the writeback code observation window gets set larger than the stats window, things become strange since you'll likely miss quite some observations about read latencies. So I think you need to make sure stats window is always larger than writeback window. Or actually, why do you have something like stats window and don't leave clearing of statistics completely to the writeback tracking code?

That's a good point, and there actually used to be a comment to that effect in the code. I think the best solution here would be to make the stats code mask available somewhere, and allow a consumer of the stats to request a larger window.

Similarly, we could make the stat window be driven by the consumer, as you suggest.

Currently there are two pending submissions that depend on the stats code. One is this writeback series, and the other one is the hybrid polling code. The latter does not really care about the window size as such, since it has no monitoring window of its own, and it wants the auto-clearing as well.

Quoted 5 lines

I don't mind working on additions for this, but I'd prefer if we could layer them on top of the existing series instead of respinning it. There's considerable test time on the existing patchset. Would that work for you? Especially collapsing the stats and wbt windows would require some re-architecting.

Quoted 8 lines

OK, that works for me. Actually, when thinking about this, I have one more
suggestion: Do we really want to expose the wbt window as a sysfs tunable? I guess it is good for initial experiments but longer term having the wbt window length be a function of target read latency might be better. Generally you want the window length to be considerably larger than the target latency but OTOH not too large so that the algorithm can react reasonably quickly so that suggests it could really be autotuned (and we scale the window anyway to adapt it to current situation).

That's not a bad idea, I have thought about that as well before. We don't need the window tunable, and you are right, it can be a function of the desired latency.

I'll hardwire the 100msec latency window for now and get rid of the exposed tunable. It's harder to remove sysfs files once they have made it into the kernel...

Quoted 9 lines
Quoted 8 lines
Quoted 5 lines

Also as a side note - nobody currently uses the mean value of the statistics. It may be faster to track just sum and count so that mean can be computed on request which will be presumably much more rare than current situation where we recompute the mean on each batch update. Actually, that way you could get rid of the batching as well I assume.

That could be opt-in as well. The poll code uses it. And fwiw, it is exposed through sysfs as well.

Quoted 6 lines

Yeah, my point was that just doing the division in response to sysfs read or actual request to read the average is likely going to be less expensive than having to do it on each batch completion (actually, you seem to have that batching code only so that you don't have to do the division too often). Whether my suggestion is right depends on how often polling code actually needs to read the average...

The polling code currently does it for every IO... That is not ideal for other purposes, I think I'm going to work on changing that to just keep the previous window available, so we only need to read it when the stats window changes.

With the batching, I don't see the division as a problem in micro benchmarks. That's why I added the batching, because it did show up before.

Signature · 1 line
Jens Axboe
maintainer
#24 lore raw

Re: [PATCH 6/8] block: add scalable completion tracking of requests

Quoted 62 lines · Jens Axboe
Quoted 50 lines · Jan Kara
Quoted 43 lines · Jens Axboe
Quoted 12 lines · Jan Kara
Quoted 10 lines · Jens Axboe

For legacy block, we simply track them in the request queue. For blk-mq, we track them on a per-sw queue basis, which we can then sum up through the hardware queues and finally to a per device state.

The stats are tracked in, roughly, 0.1s interval windows.

Add sysfs files to display the stats.

Quoted 15 lines

This patch looks mostly good to me but I have one concern: You track statistics in a fixed 134ms window, stats get cleared at the beginning of
each window. Now this can interact with the writeback window and latency
settings which are dynamic and settable from userspace - so if the writeback code observation window gets set larger than the stats window,
things become strange since you'll likely miss quite some observations about read latencies. So I think you need to make sure stats window is always larger than writeback window. Or actually, why do you have something
like stats window and don't leave clearing of statistics completely to the
writeback tracking code?

That's a good point, and there actually used to be a comment to that effect in the code. I think the best solution here would be to make the stats code mask available somewhere, and allow a consumer of the stats to request a larger window.

Similarly, we could make the stat window be driven by the consumer, as you suggest.

Currently there are two pending submissions that depend on the stats code. One is this writeback series, and the other one is the hybrid polling code. The latter does not really care about the window size as such, since it has no monitoring window of its own, and it wants the auto-clearing as well.

Quoted 5 lines

I don't mind working on additions for this, but I'd prefer if we could layer them on top of the existing series instead of respinning it. There's considerable test time on the existing patchset. Would that work for you? Especially collapsing the stats and wbt windows would require some re-architecting.

Quoted 10 lines

OK, that works for me. Actually, when thinking about this, I have one more
suggestion: Do we really want to expose the wbt window as a sysfs tunable?
I guess it is good for initial experiments but longer term having the wbt window length be a function of target read latency might be better. Generally you want the window length to be considerably larger than the target latency but OTOH not too large so that the algorithm can react reasonably quickly so that suggests it could really be autotuned (and we scale the window anyway to adapt it to current situation).

Quoted 7 lines

That's not a bad idea, I have thought about that as well before. We don't need the window tunable, and you are right, it can be a function of the desired latency.

I'll hardwire the 100msec latency window for now and get rid of the exposed tunable. It's harder to remove sysfs files once they have made it into the kernel...

Killed the sysfs variable, so for now it'll be a 100msec window by default.

Signature · 1 line
Jens Axboe
maintainer
#32 lore raw

Re: [PATCH 7/8] blk-wbt: add general throttling mechanism

In reply to Jens Axboe
Quoted 23 lines · Jens Axboe
Quoted 14 lines · Jan Kara
Quoted 8 lines
Quoted 7 lines

So for devices with write cache, you will completely drain the device before waking anybody waiting to issue new requests. Isn't it too strict?
In particular may_queue() will allow new writers to issue new writes once
we drop below the limit so it can happen that some processes will be effectively starved waiting in may_queue?

Quoted 5 lines

It is strict, and perhaps too strict. In testing, it's the only method that's proven to keep the writeback caching devices in check. It will round robin the writers, if we have more, which isn't necessarily a bad thing. Each will get to do a burst of depth writes, then wait for a new one.

Quoted 7 lines

Well, I'm more concerned about a situation where one writer does a bursty write and blocks sleeping in may_queue(). Another writer produces a steady flow of write requests so that never causes the write queue to completely drain but that writer also never blocks in may_queue() when it starts queueing after write queue has somewhat drained because it never submits many requests in parallel. In such case the first writer would get starved AFAIU.

Quoted 3 lines

I see what you are saying. I can modify the logic to ensure that if we do have a waiter, we queue up others behind it. That should get rid of that concern.

I added that - if we currently have a waiter, we'll add ourselves to the back of the waitqueue and wait.

Signature · 1 line
Jens Axboe
#34 lore raw

Re: [PATCH 7/8] blk-wbt: add general throttling mechanism

In reply to Jens Axboe
Quoted 11 lines · Jens Axboe
Quoted 5 lines · Jan Kara

Also I'm not sure why such logic for devices with writeback cache is needed. Sure the disk is fast to accept writes but if that causes long read latencies, we should scale down the writeback limits so that we eventually end up submitting only one write request anyway - effectively the same thing as limit=0 - won't we?

Basically we want to avoid getting into that situation. The problem with write caching is that it takes a while for you to notice that anything is wrong, and when you do, you are way down in the hole. That causes the first violations to be pretty bad.

Yeah, slow RAID devices with a large BBWC in front of them are notorious for doing this. You won't notice the actual IO performance until the write cache is filled (can be GB in size) and by then it's way too late to fix up with OS level queuing...

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]
maintainer
#33 lore raw

Re: [PATCH 7/8] blk-wbt: add general throttling mechanism

In reply to Jens Axboe
Quoted 24 lines · Jens Axboe
Quoted 23 lines · Jens Axboe
Quoted 21 lines · Jan Kara
Quoted 8 lines
Quoted 7 lines

So for devices with write cache, you will completely drain the device before waking anybody waiting to issue new requests. Isn't it too strict?
In particular may_queue() will allow new writers to issue new writes once
we drop below the limit so it can happen that some processes will be effectively starved waiting in may_queue?

Quoted 5 lines

It is strict, and perhaps too strict. In testing, it's the only method that's proven to keep the writeback caching devices in check. It will round robin the writers, if we have more, which isn't necessarily a bad thing. Each will get to do a burst of depth writes, then wait for a new one.

Well, I'm more concerned about a situation where one writer does a bursty write and blocks sleeping in may_queue(). Another writer produces a steady flow of write requests so that never causes the write queue to completely drain but that writer also never blocks in may_queue() when it starts queueing after write queue has somewhat drained because it never submits many requests in parallel. In such case the first writer would get starved AFAIU.

Quoted 6 lines
Quoted 3 lines

I see what you are saying. I can modify the logic to ensure that if we do have a waiter, we queue up others behind it. That should get rid of that concern.

I added that - if we currently have a waiter, we'll add ourselves to the back of the waitqueue and wait.

OK, sounds good to me. If the write queue draining will show to be an issue, it will be at least clearly visible with this logic.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#25 lore raw

Re: [PATCH 6/8] block: add scalable completion tracking of requests

In reply to Jens Axboe
Quoted 67 lines · Jens Axboe
Quoted 66 lines · Jens Axboe
Quoted 50 lines · Jan Kara
Quoted 43 lines · Jens Axboe
Quoted 12 lines · Jan Kara
Quoted 10 lines · Jens Axboe

For legacy block, we simply track them in the request queue. For blk-mq, we track them on a per-sw queue basis, which we can then sum up through the hardware queues and finally to a per device state.

The stats are tracked in, roughly, 0.1s interval windows.

Add sysfs files to display the stats.

Quoted 15 lines

This patch looks mostly good to me but I have one concern: You track statistics in a fixed 134ms window, stats get cleared at the beginning of
each window. Now this can interact with the writeback window and latency
settings which are dynamic and settable from userspace - so if the writeback code observation window gets set larger than the stats window,
things become strange since you'll likely miss quite some observations about read latencies. So I think you need to make sure stats window is always larger than writeback window. Or actually, why do you have something
like stats window and don't leave clearing of statistics completely to the
writeback tracking code?

That's a good point, and there actually used to be a comment to that effect in the code. I think the best solution here would be to make the stats code mask available somewhere, and allow a consumer of the stats to request a larger window.

Similarly, we could make the stat window be driven by the consumer, as you suggest.

Currently there are two pending submissions that depend on the stats code. One is this writeback series, and the other one is the hybrid polling code. The latter does not really care about the window size as such, since it has no monitoring window of its own, and it wants the auto-clearing as well.

Quoted 5 lines

I don't mind working on additions for this, but I'd prefer if we could layer them on top of the existing series instead of respinning it. There's considerable test time on the existing patchset. Would that work for you? Especially collapsing the stats and wbt windows would require some re-architecting.

Quoted 10 lines

OK, that works for me. Actually, when thinking about this, I have one more
suggestion: Do we really want to expose the wbt window as a sysfs tunable?
I guess it is good for initial experiments but longer term having the wbt window length be a function of target read latency might be better. Generally you want the window length to be considerably larger than the target latency but OTOH not too large so that the algorithm can react reasonably quickly so that suggests it could really be autotuned (and we scale the window anyway to adapt it to current situation).

That's not a bad idea, I have thought about that as well before. We don't need the window tunable, and you are right, it can be a function of the desired latency.

Quoted 6 lines
Quoted 3 lines

I'll hardwire the 100msec latency window for now and get rid of the exposed tunable. It's harder to remove sysfs files once they have made it into the kernel...

Killed the sysfs variable, so for now it'll be a 100msec window by default.

OK, I guess good enough to get this merged.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#26 lore raw

Re: [PATCH 6/8] block: add scalable completion tracking of requests

Quoted 68 lines · Jan Kara
Quoted 67 lines · Jens Axboe
Quoted 66 lines · Jens Axboe
Quoted 50 lines · Jan Kara
Quoted 43 lines · Jens Axboe
Quoted 12 lines · Jan Kara
Quoted 10 lines · Jens Axboe

For legacy block, we simply track them in the request queue. For blk-mq, we track them on a per-sw queue basis, which we can then sum up through the hardware queues and finally to a per device state.

The stats are tracked in, roughly, 0.1s interval windows.

Add sysfs files to display the stats.

Quoted 15 lines

This patch looks mostly good to me but I have one concern: You track statistics in a fixed 134ms window, stats get cleared at the beginning of
each window. Now this can interact with the writeback window and latency
settings which are dynamic and settable from userspace - so if the writeback code observation window gets set larger than the stats window,
things become strange since you'll likely miss quite some observations about read latencies. So I think you need to make sure stats window is always larger than writeback window. Or actually, why do you have something
like stats window and don't leave clearing of statistics completely to the
writeback tracking code?

That's a good point, and there actually used to be a comment to that effect in the code. I think the best solution here would be to make the stats code mask available somewhere, and allow a consumer of the stats to request a larger window.

Similarly, we could make the stat window be driven by the consumer, as you suggest.

Currently there are two pending submissions that depend on the stats code. One is this writeback series, and the other one is the hybrid polling code. The latter does not really care about the window size as such, since it has no monitoring window of its own, and it wants the auto-clearing as well.

Quoted 5 lines

I don't mind working on additions for this, but I'd prefer if we could layer them on top of the existing series instead of respinning it. There's considerable test time on the existing patchset. Would that work for you? Especially collapsing the stats and wbt windows would require some re-architecting.

Quoted 10 lines

OK, that works for me. Actually, when thinking about this, I have one more
suggestion: Do we really want to expose the wbt window as a sysfs tunable?
I guess it is good for initial experiments but longer term having the wbt window length be a function of target read latency might be better. Generally you want the window length to be considerably larger than the target latency but OTOH not too large so that the algorithm can react reasonably quickly so that suggests it could really be autotuned (and we scale the window anyway to adapt it to current situation).

That's not a bad idea, I have thought about that as well before. We don't need the window tunable, and you are right, it can be a function of the desired latency.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

I'll hardwire the 100msec latency window for now and get rid of the exposed tunable. It's harder to remove sysfs files once they have made it into the kernel...

Killed the sysfs variable, so for now it'll be a 100msec window by default.

OK, I guess good enough to get this merged.

Thanks Jan!

Signature · 1 line
Jens Axboe