Make background writeback great again for the first time
Subsystems (12)
PATCH 2/8 writeback: add wbc_to_write_cmd()
In reply to Jens AxboeAdd wbc_to_write_cmd(), which returns the write type 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/block_dev.c | 2 +- fs/buffer.c | 2 +- fs/f2fs/data.c | 2 +- fs/f2fs/node.c | 2 +- fs/gfs2/meta_io.c | 3 +-- fs/mpage.c | 9 ++++----- fs/xfs/xfs_aops.c | 2 +- include/linux/writeback.h | 8 ++++++++ 8 files changed, 18 insertions(+), 12 deletions(-)
fs/block_dev.c +1 -1
@@ -432,7 +432,7 @@ int bdev_write_page(struct block_device *bdev, sector_t sector, struct page *page, struct writeback_control *wbc) { int result; -int rw = (wbc->sync_mode ==WB_SYNC_ALL ) ?WRITE_SYNC :WRITE ; +int rw =wbc_to_write_cmd (wbc);const struct block_device_operations *ops = bdev->bd_disk->fops;if (!ops->rw_page ||bdev_get_integrity (bdev))
fs/buffer.c +1 -1
@@ -1697,7 +1697,7 @@ static 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_op = (wbc->sync_mode ==WB_SYNC_ALL ?WRITE_SYNC :WRITE ); +int write_op =wbc_to_write_cmd (wbc); head =create_page_buffers (page, inode, (1 << BH_Dirty)|(1 << BH_Uptodate));
fs/f2fs/data.c +1 -1
@@ -1153,7 +1153,7 @@ static int f2fs_write_data_page(struct page *page,struct f2fs_io_info fio = { .sbi = sbi, .type =DATA , - .rw = (wbc->sync_mode ==WB_SYNC_ALL ) ?WRITE_SYNC :WRITE , + .rw =wbc_to_write_cmd (wbc), .page = page, .encrypted_page =NULL , };
fs/f2fs/node.c +1 -1
@@ -1397,7 +1397,7 @@ static int f2fs_write_node_page(struct page *page,struct f2fs_io_info fio = { .sbi = sbi, .type =NODE , - .rw = (wbc->sync_mode ==WB_SYNC_ALL ) ?WRITE_SYNC :WRITE , + .rw =wbc_to_write_cmd (wbc), .page = page, .encrypted_page =NULL , };
fs/gfs2/meta_io.c +1 -2
@@ -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_op =REQ_META |REQ_PRIO | - (wbc->sync_mode ==WB_SYNC_ALL ?WRITE_SYNC :WRITE ); +int write_op =REQ_META |REQ_PRIO |wbc_to_write_cmd (wbc);BUG_ON (!PageLocked (page));BUG_ON (!page_has_buffers (page));
fs/mpage.c +4 -5
@@ -486,7 +486,6 @@ 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 wr = (wbc->sync_mode ==WB_SYNC_ALL ?WRITE_SYNC :WRITE );if (page_has_buffers (page)) {struct buffer_head *head =page_buffers (page);
@@ -595,7 +594,7 @@ page_is_mapped:* This page will go to BIO. Do we need to send this BIO off first? */ if (bio && mpd->last_block_in_bio != blocks[0 ] -1 ) - bio =mpage_bio_submit (wr, bio); + bio =mpage_bio_submit (wbc_to_write_cmd (wbc), bio); alloc_new:if (bio ==NULL ) {
@@ -622,7 +621,7 @@ alloc_new:wbc_account_io (wbc, page,PAGE_SIZE ); length = first_unmapped << blkbits;if (bio_add_page (bio, page, length,0 ) < length) { - bio =mpage_bio_submit (wr, bio); + bio =mpage_bio_submit (wbc_to_write_cmd (wbc), bio); goto alloc_new; }
@@ -632,7 +631,7 @@ alloc_new:set_page_writeback (page);unlock_page (page);if (boundary || (first_unmapped != blocks_per_page)) { - bio =mpage_bio_submit (wr, bio); + bio =mpage_bio_submit (wbc_to_write_cmd (wbc), bio);if (boundary_block) {write_boundary_block (boundary_bdev, boundary_block,1 << blkbits);
@@ -644,7 +643,7 @@ alloc_new: confused:if (bio) - bio =mpage_bio_submit (wr, bio); + bio =mpage_bio_submit (wbc_to_write_cmd (wbc), bio);if (mpd->use_writepage) { ret = mapping->a_ops->writepage (page, wbc);
fs/xfs/xfs_aops.c +1 -1
@@ -393,7 +393,7 @@ xfs_submit_ioend_bio(atomic_inc (&ioend->io_remaining); bio->bi_private = ioend; bio->bi_end_io = xfs_end_bio; -submit_bio (wbc->sync_mode ==WB_SYNC_ALL ?WRITE_SYNC :WRITE , bio); +submit_bio (wbc_to_write_cmd (wbc), bio); } STATIC struct bio *
include/linux/writeback.h +8 -0
@@ -100,6 +100,14 @@ struct writeback_control {#endif }; +static inline int wbc_to_write_cmd (struct writeback_control *wbc) +{ +if (wbc->sync_mode ==WB_SYNC_ALL ) +return WRITE_SYNC ; + +return WRITE ; +} +/* * 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
Signature · 1 line
2.8.0.rc4.6.g7e4ba36
PATCH 3/8 writeback: use WRITE_BG for kupdate and background writeback
In reply to Jens AxboeIf we're doing background type writes, then use the appropriate write command for that.
No functional changes in this patch.
--- include/linux/writeback.h | 2 ++ 1 file changed, 2 insertions(+)
include/linux/writeback.h +2 -0
@@ -104,6 +104,8 @@ static inline int wbc_to_write_cmd(struct writeback_control *wbc) {if (wbc->sync_mode ==WB_SYNC_ALL )return WRITE_SYNC ; +else if (wbc->for_kupdate || wbc->for_background) +return WRITE_BG ;return WRITE ; }
Signature · 1 line
2.8.0.rc4.6.g7e4ba36
PATCH 4/8 writeback: track if we're sleeping on progress in balance_dirty_pages()
In reply to Jens AxboeNote in the bdi_writeback structure if a task is currently being limited in balance_dirty_pages(), waiting for writeback to proceed.
--- include/linux/backing-dev-defs.h | 2 ++ mm/backing-dev.c | 1 + mm/page-writeback.c | 2 ++ 3 files changed, 5 insertions(+)
include/linux/backing-dev-defs.h +2 -0
@@ -116,6 +116,8 @@ struct bdi_writeback {struct list_head work_list;struct delayed_work dwork;/* work item used for writeback */ +atomic_t dirty_sleeping;/* waiting on dirty limit exceeded */ +struct list_head bdi_node;/* anchored at bdi->wb_list */ #ifdef CONFIG_CGROUP_WRITEBACK
mm/backing-dev.c +1 -0
@@ -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); +atomic_set (&wb->dirty_sleeping,0 ); wb->congested =wb_congested_get_create (bdi, blkcg_id, gfp);if (!wb->congested)
mm/page-writeback.c +2 -0
@@ -1746,7 +1746,9 @@ pause: pause, start_time);__set_current_state (TASK_KILLABLE ); +atomic_inc (&wb->dirty_sleeping);io_schedule_timeout (pause); +atomic_dec (&wb->dirty_sleeping); current->dirty_paused_when = now + pause; current->nr_dirtied =0 ;
Signature · 1 line
2.8.0.rc4.6.g7e4ba36
PATCH 5/8 block: add code to track actual device queue depth
In reply to Jens AxboeFor 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
@@ -832,6 +832,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
@@ -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
@@ -315,6 +315,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;
@@ -681,6 +683,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 */
@@ -984,6 +994,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.8.0.rc4.6.g7e4ba36
PATCH 1/8 block: add WRITE_BG
In reply to Jens AxboeThis adds a new request flag, REQ_BG, that callers can use to tell the block layer that this is background (non-urgent) IO.
No functional changes in this patch.
--- include/linux/blk_types.h | 4 +++- include/linux/fs.h | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-)
include/linux/blk_types.h +3 -1
@@ -161,6 +161,7 @@ enum rq_flag_bits { __REQ_INTEGRITY,/* I/O includes block integrity payload */ __REQ_FUA,/* forced unit access */ __REQ_FLUSH,/* request for cache flush */ + __REQ_BG,/* background activity */ /* bio only flags */ __REQ_RAHEAD, /* read ahead, can fail anytime */
@@ -208,7 +209,7 @@ enum rq_flag_bits {#define REQ_COMMON_MASK \ (REQ_WRITE | REQ_FAILFAST_MASK | REQ_SYNC | REQ_META | REQ_PRIO | \ REQ_DISCARD | REQ_WRITE_SAME | REQ_NOIDLE | REQ_FLUSH | REQ_FUA | \ - REQ_SECURE | REQ_INTEGRITY) + REQ_SECURE | REQ_INTEGRITY | REQ_BG)#define REQ_CLONE_MASK REQ_COMMON_MASK#define BIO_NO_ADVANCE_ITER_MASK (REQ_DISCARD|REQ_WRITE_SAME)
@@ -235,6 +236,7 @@ enum rq_flag_bits {#define REQ_COPY_USER (1ULL << __REQ_COPY_USER)#define REQ_FLUSH (1ULL << __REQ_FLUSH)#define REQ_FLUSH_SEQ (1ULL << __REQ_FLUSH_SEQ) +#define REQ_BG (1ULL << __REQ_BG)#define REQ_IO_STAT (1ULL << __REQ_IO_STAT)#define REQ_MIXED_MERGE (1ULL << __REQ_MIXED_MERGE)#define REQ_SECURE (1ULL << __REQ_SECURE)
include/linux/fs.h +4 -0
@@ -192,6 +192,9 @@ typedef void (dax_iodone_t)(struct buffer_head *bh_map, int uptodate);* WRITE_FLUSH_FUA Combination of WRITE_FLUSH and FUA. The IO is preceded * by a cache flush and data is guaranteed to be on * non-volatile media on completion. +* WRITE_BG Background write. This is for background activity like +* the periodic flush and background threshold writeback +* * */ #define RW_MASK REQ_WRITE
@@ -207,6 +210,7 @@ typedef void (dax_iodone_t)(struct buffer_head *bh_map, int uptodate);#define WRITE_FLUSH (WRITE | REQ_SYNC | REQ_NOIDLE | REQ_FLUSH)#define WRITE_FUA (WRITE | REQ_SYNC | REQ_NOIDLE | REQ_FUA)#define WRITE_FLUSH_FUA (WRITE | REQ_SYNC | REQ_NOIDLE | REQ_FLUSH | REQ_FUA) +#define WRITE_BG (WRITE | REQ_NOIDLE | REQ_BG)/* * Attribute flags. These should be or-ed together to figure out what
Signature · 1 line
2.8.0.rc4.6.g7e4ba36
PATCH 6/8 block: add scalable completion tracking of requests
In reply to Jens AxboeFor 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 | 184 ++++++++++++++++++++++++++++++++++++++++++++++ block/blk-stat.h | 17 +++++ block/blk-sysfs.c | 26 +++++++ include/linux/blk_types.h | 8 ++ include/linux/blkdev.h | 4 + 10 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 block/blk-stat.c create mode 100644 block/blk-stat.h
block/Makefile +1 -1
@@ -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-cpu.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
@@ -2514,6 +2514,8 @@ void blk_start_request(struct request *req) {blk_dequeue_request (req); + req->issue_time =ktime_to_ns (ktime_get ()); +/* * We are now handing the request to the hardware, initialize * resid_len to full count and add the timeout handler.
@@ -2581,6 +2583,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
@@ -29,6 +29,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);
@@ -356,10 +357,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
@@ -403,6 +413,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); + rq->issue_time =ktime_to_ns (ktime_get ()); +blk_add_timer (rq);/*
@@ -1761,6 +1773,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
@@ -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 {
@@ -20,6 +22,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 +184 -0
184 lines not on this page · raw message
block/blk-stat.h new file +17 -0
@@ -0,0 +1,17 @@ +#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_MASK ~(BLK_STAT_NSEC - 1) + +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 *); + +#endif
block/blk-sysfs.c +26 -0
@@ -379,6 +379,26 @@ static ssize_t queue_wc_store(struct request_queue *q, const char *page,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 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,
@@ -516,6 +536,11 @@ static struct queue_sysfs_entry queue_wc_entry = { .store = queue_wc_store, }; +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,
@@ -542,6 +567,7 @@ static struct attribute *default_attrs[] = { &queue_random_entry.attr, &queue_poll_entry.attr, &queue_wc_entry.attr, + &queue_stats_entry.attr, NULL, };
include/linux/blk_types.h +8 -0
@@ -268,4 +268,12 @@ static inline unsigned int blk_qc_t_to_tag(blk_qc_t cookie) return cookie & ((1u << BLK_QC_T_SHIFT) - 1); } +struct blk_rq_stat { +s64 mean; +u64 min; +u64 max; +s64 nr_samples; +s64 time; +}; +#endif /* __LINUX_BLK_TYPES_H */
include/linux/blkdev.h +4 -0
@@ -153,6 +153,7 @@ struct request {struct gendisk *rq_disk;struct hd_struct *part;unsigned long start_time; +s64 issue_time;#ifdef CONFIG_BLK_CGROUP struct request_list *rl;/* rl this rq is alloced from */ unsigned long long start_time_ns;
@@ -402,6 +403,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.8.0.rc4.6.g7e4ba36
PATCH 7/8 wbt: add general throttling mechanism
In reply to Jens AxboeWe can hook this up to the block layer, to help throttle buffered writes. Or NFS can tap into it, to accomplish the same.
wbt registers a few trace points that can be used to track what is happening in the system:
wbt_lat: 259:0: latency 2446318wbt_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.
--- include/linux/wbt.h | 95 ++++++++ include/trace/events/wbt.h | 122 +++++++++++ lib/Kconfig | 3 + lib/Makefile | 1 + lib/wbt.c | 524 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 745 insertions(+) create mode 100644 include/linux/wbt.h create mode 100644 include/trace/events/wbt.h create mode 100644 lib/wbt.c
include/linux/wbt.h new file +95 -0
@@ -0,0 +1,95 @@ +#ifndef WB_THROTTLE_H +#define WB_THROTTLE_H + +#include <linux/atomic.h> +#include <linux/wait.h> +#include <linux/timer.h> +#include <linux/ktime.h> + +#define ISSUE_STAT_MASK (1ULL << 63) +#define ISSUE_STAT_TIME_MASK ~ISSUE_STAT_MASK + +struct wb_issue_stat { +u64 time; +}; + +static inline void wbt_issue_stat_set_time (struct wb_issue_stat *stat) +{ + stat->time = (stat->time &ISSUE_STAT_MASK ) | + (ktime_to_ns (ktime_get ()) &ISSUE_STAT_TIME_MASK ); +} + +static inline u64 wbt_issue_stat_get_time (struct wb_issue_stat *stat) +{ +return stat->time &ISSUE_STAT_TIME_MASK ; +} + +static inline void wbt_mark_tracked (struct wb_issue_stat *stat) +{ + stat->time |=ISSUE_STAT_MASK ; +} + +static inline void wbt_clear_tracked (struct wb_issue_stat *stat) +{ + stat->time &= ~ISSUE_STAT_MASK ; +} + +static inline bool wbt_tracked (struct wb_issue_stat *stat) +{ +return (stat->time &ISSUE_STAT_MASK ) !=0 ; +} + +struct wb_stat_ops { +void (*get)(void *,struct blk_rq_stat *); +void (*clear)(void *); +}; + +struct rq_wb { +/* +* Settings that govern how we throttle +*/ +unsigned int wb_background;/* background writeback */ +unsigned int wb_normal;/* normal writeback */ +unsigned int wb_max;/* max throughput writeback */ +unsigned int scale_step; + +u64 win_nsec;/* default window size */ +u64 cur_win_nsec;/* current window size */ + +unsigned int unknown_cnt; + +struct timer_list window_timer; + +s64 sync_issue; +void *sync_cookie; + +unsigned int wc; +unsigned int queue_depth; + +unsigned long last_issue;/* last non-throttled issue */ +unsigned long last_comp;/* last non-throttled comp */ +unsigned long min_lat_nsec; +struct backing_dev_info *bdi; +struct request_queue *q; +wait_queue_head_t wait; +atomic_t inflight; + +struct wb_stat_ops *stat_ops; +void *ops_data; +}; + +struct backing_dev_info ; + +void __wbt_done (struct rq_wb *); +void wbt_done (struct rq_wb *,struct wb_issue_stat *); +bool wbt_wait (struct rq_wb *,unsigned int ,spinlock_t *); +struct rq_wb *wbt_init (struct backing_dev_info *,struct wb_stat_ops *,void *); +void wbt_exit (struct rq_wb *); +void wbt_update_limits (struct rq_wb *); +void wbt_requeue (struct rq_wb *,struct wb_issue_stat *); +void wbt_issue (struct rq_wb *,struct wb_issue_stat *); + +void wbt_set_queue_depth (struct rq_wb *,unsigned int ); +void wbt_set_write_cache (struct rq_wb *,bool ); + +#endif
include/trace/events/wbt.h new file +122 -0
122 lines not on this page · raw message
lib/Kconfig +3 -0
@@ -540,4 +540,7 @@ config STACKDEPOT bool select STACKTRACE +config WBT + bool + endmenu
lib/Makefile +1 -0
@@ -180,6 +180,7 @@ obj-$(CONFIG_GENERIC_NET_UTILS) += net_utils.o obj-$(CONFIG_SG_SPLIT) += sg_split.o obj-$(CONFIG_STMP_DEVICE) += stmp_device.o obj-$(CONFIG_IRQ_POLL) += irq_poll.o +obj-$(CONFIG_WBT) += wbt.o obj-$(CONFIG_STACKDEPOT) += stackdepot.o KASAN_SANITIZE_stackdepot.o := n
lib/wbt.c new file +524 -0
524 lines not on this page · raw message
Signature · 1 line
2.8.0.rc4.6.g7e4ba36
PATCH 8/8 writeback: throttle buffered writeback
In reply to Jens AxboeTest patch that throttles 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.
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.
--- Documentation/block/queue-sysfs.txt | 13 ++++ block/Kconfig | 1 + block/blk-core.c | 21 ++++++- block/blk-mq.c | 32 +++++++++- block/blk-settings.c | 3 + block/blk-stat.c | 5 +- block/blk-sysfs.c | 119 ++++++++++++++++++++++++++++++++++++ include/linux/blkdev.h | 6 +- 8 files changed, 191 insertions(+), 9 deletions(-)
Documentation/block/queue-sysfs.txt +13 -0
@@ -151,5 +151,18 @@ device state. This means that it might not be safe to toggle the setting from "write back" to "write through", since that will also eliminate cache flushes issued by the kernel. +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 +1 -0
@@ -4,6 +4,7 @@ menuconfig BLOCK bool "Enable the block layer" if EXPERT default y + select WBT help Provide block layer support for the kernel.
block/blk-core.c +18 -3
@@ -33,6 +33,7 @@#include <linux/ratelimit.h> #include <linux/pm_runtime.h> #include <linux/blk-cgroup.h> +#include <linux/wbt.h> #define CREATE_TRACE_POINTS #include <trace/events/block.h>
@@ -880,6 +881,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);
@@ -1395,6 +1398,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->wb_stat);if (rq->cmd_flags &REQ_QUEUED )blk_queue_end_tag (q, rq);
@@ -1485,6 +1489,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->wb_stat); +/* * Request may not have originated from ll_rw_blk. if not, * it didn't come out of our reserved rq pools
@@ -1714,6 +1720,7 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio)int el_ret, rw_flags, where =ELEVATOR_INSERT_SORT ;struct request *req;unsigned int request_count =0 ; +bool wb_acct;/* * low level driver can indicate that it wants pages above a
@@ -1766,6 +1773,8 @@ static blk_qc_t blk_queue_bio(struct request_queue *q, struct bio *bio) } get_rq: + wb_acct =wbt_wait (q->rq_wb, bio->bi_rw, q->queue_lock); +/* * This sync check and mask will be re-done in init_request_from_bio(), * but we need to set it earlier to expose the sync flag to the
@@ -1781,11 +1790,16 @@ get_rq:*/ req =get_request (q, rw_flags, bio,GFP_NOIO );if (IS_ERR (req)) { +if (wb_acct) +__wbt_done (q->rq_wb); bio->bi_error =PTR_ERR (req);bio_endio (bio); goto out_unlock; } +if (wb_acct) +wbt_mark_tracked (&req->wb_stat); +/* * After dropping the lock and possibly sleeping here, our request * may now be mergeable after it had proven unmergeable (above).
@@ -2514,7 +2528,7 @@ void blk_start_request(struct request *req) {blk_dequeue_request (req); - req->issue_time =ktime_to_ns (ktime_get ()); +wbt_issue (req->q->rq_wb, &req->wb_stat);/* * We are now handing the request to the hardware, initialize
@@ -2752,9 +2766,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->wb_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 +29 -3
@@ -22,6 +22,7 @@#include <linux/sched/sysctl.h> #include <linux/delay.h> #include <linux/crash_dump.h> +#include <linux/wbt.h> #include <trace/events/block.h>
@@ -275,6 +276,8 @@ static void __blk_mq_free_request(struct blk_mq_hw_ctx *hctx,if (rq->cmd_flags &REQ_MQ_INFLIGHT )atomic_dec (&hctx->nr_active); + +wbt_done (q->rq_wb, &rq->wb_stat); rq->cmd_flags =0 ;clear_bit (REQ_ATOM_STARTED , &rq->atomic_flags);
@@ -307,6 +310,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->wb_stat); rq->end_io (rq, error); }else {if (unlikely (blk_bidi_rq (rq)))
@@ -413,7 +417,7 @@ 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); - rq->issue_time =ktime_to_ns (ktime_get ()); +wbt_issue (q->rq_wb, &rq->wb_stat);blk_add_timer (rq);
@@ -450,6 +454,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->wb_stat);if (test_and_clear_bit (REQ_ATOM_STARTED , &rq->atomic_flags)) {if (q->dma_drain_size &&blk_rq_bytes (rq))
@@ -1265,6 +1270,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; +bool wb_acct;blk_queue_bounce (q, &bio);
@@ -1282,9 +1288,17 @@ static blk_qc_t blk_mq_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->bi_rw,NULL ); + rq =blk_mq_map_request (q, bio, &data); -if (unlikely (!rq)) +if (unlikely (!rq)) { +if (wb_acct) +__wbt_done (q->rq_wb);return BLK_QC_T_NONE ; + } + +if (wb_acct) +wbt_mark_tracked (&rq->wb_stat); cookie =blk_tag_to_qc_t (rq->tag, data.hctx->queue_num);
@@ -1361,6 +1375,7 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio)struct blk_map_ctx data;struct request *rq;blk_qc_t cookie; +bool wb_acct;blk_queue_bounce (q, &bio);
@@ -1375,9 +1390,17 @@ static blk_qc_t blk_sq_make_request(struct request_queue *q, struct bio *bio) blk_attempt_plug_merge(q, bio, &request_count, NULL))return BLK_QC_T_NONE ; + wb_acct =wbt_wait (q->rq_wb, bio->bi_rw,NULL ); + rq =blk_mq_map_request (q, bio, &data); -if (unlikely (!rq)) +if (unlikely (!rq)) { +if (wb_acct) +__wbt_done (q->rq_wb);return BLK_QC_T_NONE ; + } + +if (wb_acct) +wbt_mark_tracked (&rq->wb_stat); cookie =blk_tag_to_qc_t (rq->tag, data.hctx->queue_num);
@@ -2111,6 +2134,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 +3 -0
@@ -840,6 +840,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);
@@ -863,6 +864,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-stat.c +3 -2
@@ -143,15 +143,16 @@ void blk_stat_init(struct blk_rq_stat *stat)void blk_stat_add (struct blk_rq_stat *stat,struct request *rq) {s64 delta, now, value; +u64 rq_time =wbt_issue_stat_get_time (&rq->wb_stat); now =ktime_to_ns (ktime_get ()); -if (now < rq->issue_time) +if (now < rq_time)return ;if ((now &BLK_STAT_MASK ) != (stat->time &BLK_STAT_MASK ))__blk_stat_init (stat, now); - value = now - rq->issue_time; + value = now - rq_time;if (value > stat->max) stat->max = value;if (value < stat->min)
block/blk-sysfs.c +119 -0
161 lines not on this page · raw message
include/linux/blkdev.h +5 -1
@@ -24,6 +24,7 @@#include <linux/rcupdate.h> #include <linux/percpu-refcount.h> #include <linux/scatterlist.h> +#include <linux/wbt.h> struct module ;struct scsi_ioctl_command ;
@@ -37,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 */
@@ -153,7 +155,7 @@ struct request {struct gendisk *rq_disk;struct hd_struct *part;unsigned long start_time; -s64 issue_time; +struct wb_issue_stat wb_stat;#ifdef CONFIG_BLK_CGROUP struct request_list *rl;/* rl this rq is alloced from */ unsigned long long start_time_ns;
@@ -291,6 +293,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.8.0.rc4.6.g7e4ba36
PATCH v5 1/1 Make background writeback great again for the first time
Hi,
Since the dawn of time, our background buffered writeback has sucked. When we do background buffered writeback, it should have little impact on foreground activity. That's the definition of background activity... But for as long as I can remember, heavy buffered writers have not behaved like that. For instance, if I do something like this:
$ dd if=/dev/zero of=foo bs=1M count=10k
on my laptop, and then try and start chrome, it basically won't start before the buffered writeback is done. Or, for server oriented workloads, where installation of a big RPM (or similar) adversely impacts database reads or sync writes. When that happens, I get people yelling at me.
I have posted plenty of results previously, I'll keep it shorter this time. Here's a run on my laptop, using read-to-pipe-async for reading a 5g file, and rewriting it. You can find this test program in the fio git repo.
4.6-rc3:
$ t/read-to-pipe-async -f ~/5g > 5g-new
Latency percentiles (usec) (READERS)
50.0000th: 2 75.0000th: 3 90.0000th: 5 95.0000th: 7 99.0000th: 43 99.5000th: 77 99.9000th: 9008 99.9900th: 91008 99.9990th: 286208 99.9999th: 347648 Over=1251, min=0, max=358081 Latency percentiles (usec) (WRITERS) 50.0000th: 4 75.0000th: 8 90.0000th: 13 95.0000th: 15 99.0000th: 32 99.5000th: 43 99.9000th: 81 99.9900th: 2372 99.9990th: 104320 99.9999th: 349696 Over=63, min=1, max=358321
Read rate (KB/sec) : 91859
Write rate (KB/sec): 91859
4.6-rc3 + wb-buf-throttle
Latency percentiles (usec) (READERS)
50.0000th: 2 75.0000th: 3 90.0000th: 5 95.0000th: 8 99.0000th: 48 99.5000th: 79 99.9000th: 5304 99.9900th: 22496 99.9990th: 29408 99.9999th: 33728 Over=860, min=0, max=37599 Latency percentiles (usec) (WRITERS) 50.0000th: 4 75.0000th: 9 90.0000th: 14 95.0000th: 16 99.0000th: 34 99.5000th: 45 99.9000th: 87 99.9900th: 1342 99.9990th: 13648 99.9999th: 21280 Over=29, min=1, max=30457
Read rate (KB/sec) : 95832
Write rate (KB/sec): 95832
Better throughput and tighter latencies, for both reads and writes. That's hard not to like.
The above was the why. The how is basically throttling background writeback. We still want to issue big writes from the vm side of things, so we get nice and big extents on the file system end. But we don't need to flood the device with THOUSANDS of requests for background writeback. For most devices, we don't need a whole lot to get decent throughput.
This adds some simple blk-wb code that keeps limits how much buffered writeback we keep in flight on the device end. It's all about managing the queues on the hardware side. The big change in this version is that it should be pretty much auto-tuning - you no longer have to set a given percentage of writeback bandwidth. I've implemented something similar to CoDel to manage the writeback queue. See the last patch for a full description, but the tldr is that we monitor min latencies over a window of time, and scale up/down the queue based on that. This needs a minimum of tunables, and it stays out of the way, if your device is fast enough. There's a single tunable now, wb_last_usec, that simply sets this latency target. Most people won't have to touch this, it'll work pretty well just being in the ballpark.
I welcome testing. If you are sick of Linux bogging down when buffered writes are happening, then this is for you, laptop or server. The patchset is fully stable, I have not observed problems. It passes full xfstest runs, and a variety of benchmarks as well. It works equally well on blk-mq/scsi-mq, and "classic" setups.
You can also find this in a branch in the block git repo:
git://git.kernel.dk/linux-block.git wb-buf-throttle
Note that I rebase this branch when I collapse patches. The wb-buf-throttle-v5 will remain the same as this version. I've folded the device write cache changes into my 4.7 branches, so they are not a part of this posting. Get the full wb-buf-throttle branch, or apply the patches here on top of my for-next. A full patch against Linus' current tree can also be downloaded here:
http://brick.kernel.dk/snaps/wb-buf-throttle-v5.patch
Changes since v4
- Add some documentation for the two queue sysfs files
- Kill off wb_stats sysfs file. Use the trace points to get this info now.
- Various work around making this block layer agnostic. The main code now resides in lib/wbt.c and can be plugged into NFS as well, for instance.
- Fix an issue with double completions on the block layer side.
- Fix an issue where a long sync issue was disregarded, if the stat sample weren't valid.
- Speed up the division in rwb_arm_timer().
- Add logic to scale back up for 'unknown' latency events.
- Don't track sync issue timestamp of wbt is disabled.
- Drop the dirty/writeback page inc/dec patch. We don't need it, and it was racy.
- Move block/blk-wb.c to lib/wbt.c
Changes since v3
- Re-do the mm/ writheback parts. Add REQ_BG for background writes, and don't overload the wbc 'reason' for writeback decisions.
- Add tracking for when apps are sleeping waiting for a page to complete.
- Change wbc_to_write() to wbc_to_write_cmd().
- Use atomic_t for the balance_dirty_pages() sleep count.
- Add a basic scalable block stats tracking framework.
- Rewrite blk-wb core as described above, to dynamically adapt. This is a big change, see the last patch for a full description of it.
- Add tracing to blk-wb, instead of using debug printk's.
- Rebased to 4.6-rc3 (ish)
Changes since v2
- Switch from wb_depth to wb_percent, as that's an easier tunable.
- Add the patch to track device depth on the block layer side.
- Cleanup the limiting code.
- Don't use a fixed limit in the wb wait, since it can change between wakeups.
- Minor tweaks, fixups, cleanups.
Changes since v1
- Drop sync() WB_SYNC_NONE -> WB_SYNC_ALL change
- wb_start_writeback() fills in background/reclaim/sync info in the writeback work, based on writeback reason.
- Use WRITE_SYNC for reclaim/sync IO
- Split balance_dirty_pages() sleep change into separate patch
- Drop get_request() u64 flag change, set the bit on the request directly after-the-fact.
- Fix wrong sysfs return value
- Various small cleanups
Documentation/block/queue-sysfs.txt | 22 + Documentation/block/writeback_cache_control.txt | 4 arch/um/drivers/ubd_kern.c | 2 block/Kconfig | 1 block/Makefile | 2 block/blk-core.c | 26 + block/blk-flush.c | 11 block/blk-mq-sysfs.c | 47 ++ block/blk-mq.c | 44 +- block/blk-mq.h | 3 block/blk-settings.c | 59 +- block/blk-stat.c | 185 ++++++++ block/blk-stat.h | 17 block/blk-sysfs.c | 184 ++++++++ drivers/block/drbd/drbd_main.c | 2 drivers/block/loop.c | 2 drivers/block/mtip32xx/mtip32xx.c | 6 drivers/block/nbd.c | 4 drivers/block/osdblk.c | 2 drivers/block/ps3disk.c | 2 drivers/block/skd_main.c | 2 drivers/block/virtio_blk.c | 6 drivers/block/xen-blkback/xenbus.c | 2 drivers/block/xen-blkfront.c | 3 drivers/ide/ide-disk.c | 6 drivers/md/bcache/super.c | 2 drivers/md/dm-table.c | 20 drivers/md/md.c | 2 drivers/md/raid5-cache.c | 3 drivers/mmc/card/block.c | 2 drivers/mtd/mtd_blkdevs.c | 2 drivers/nvme/host/core.c | 7 drivers/scsi/scsi.c | 3 drivers/scsi/sd.c | 8 drivers/target/target_core_iblock.c | 6 fs/block_dev.c | 2 fs/buffer.c | 2 fs/f2fs/data.c | 2 fs/f2fs/node.c | 2 fs/gfs2/meta_io.c | 3 fs/mpage.c | 9 fs/xfs/xfs_aops.c | 2 include/linux/backing-dev-defs.h | 2 include/linux/blk_types.h | 12 include/linux/blkdev.h | 28 + include/linux/fs.h | 4 include/linux/wbt.h | 95 ++++ include/linux/writeback.h | 10 include/trace/events/wbt.h | 122 +++++ lib/Kconfig | 3 lib/Makefile | 1 lib/wbt.c | 524 ++++++++++++++++++++++++ mm/backing-dev.c | 1 mm/page-writeback.c | 2 54 files changed, 1429 insertions(+), 96 deletions(-)
Signature · 1 line
Jens Axboe
Re: [PATCH 7/8] wbt: add general throttling mechanism
In reply to Jens AxboeQuoted 51 lines
lib/wbt.c new file +50
-0+return rwb && rwb->wb_normal !=0 ; +} + +/* +* Increment 'v', if 'v' is below 'below'. Returns true if we succeeded, +* false if 'v' + 1 would be bigger than 'below'. +*/ +static bool atomic_inc_below (atomic_t *v,int below) +{ +int cur =atomic_read (v); + +for (;;) { +int old; + +if (cur >= below) +return false; + old =atomic_cmpxchg (v, cur, cur +1 ); +if (old == cur) +break ; + cur = old; + } + +return true; +} + +static void wb_timestamp (struct rq_wb *rwb,unsigned long *var) +{ +if (rwb_enabled (rwb)) { +const unsigned long cur = jiffies; + +if (cur != *var) + *var = cur; + } +} + +void __wbt_done (struct rq_wb *rwb) +{ +int inflight, limit = rwb->wb_normal; + +/* +* If the device does write back caching, drop further down +* before we wake people up. +*/ +if (rwb->wc && !atomic_read (&rwb->bdi->wb.dirty_sleeping)) + limit =0 ; +else + limit = rwb->wb_normal; + +/* +* Don't wake anyone up if we are above the normal limit. If
lib/wbt.c line 112
Quoted 10 lines
lib/wbt.c new file +10
-0+* throttling got disabled (limit == 0) with waiters, ensure +* that we wake them up. +*/ + inflight =atomic_dec_return (&rwb->inflight); +if (limit && inflight >= limit) { +if (!rwb->wb_max) +wake_up_all (&rwb->wait); +return ; + } +
Hi Jens,
Just a little confused about this. The rwb->wb_max can't be 0 if the variable 'limit' does not equal to 0. So the if (!rwb->wb_max) branch maybe does not make sense.
Quoted 62 lines
lib/wbt.c new file +62
-0+if (waitqueue_active (&rwb->wait)) { +int diff = limit - inflight; + +if (!inflight || diff >= rwb->wb_background /2 ) +wake_up_nr (&rwb->wait,1 ); + } +} + +/* +* Called on completion of a request. Note that it's also called when +* a request is merged, when the request gets freed. +*/ +void wbt_done (struct rq_wb *rwb,struct wb_issue_stat *stat) +{ +if (!rwb) +return ; + +if (!wbt_tracked (stat)) { +if (rwb->sync_cookie == stat) { + rwb->sync_issue =0 ; + rwb->sync_cookie =NULL ; + } + +wb_timestamp (rwb, &rwb->last_comp); + }else { +WARN_ON_ONCE (stat == rwb->sync_cookie); +__wbt_done (rwb); +wbt_clear_tracked (stat); + } +} + +static void calc_wb_limits (struct rq_wb *rwb) +{ +unsigned int depth; + +if (!rwb->min_lat_nsec) { + rwb->wb_max = rwb->wb_normal = rwb->wb_background =0 ; +return ; + } + + depth = min_t(unsigned int, RWB_MAX_DEPTH, rwb->queue_depth); + +/* +* Reduce max depth by 50%, and re-calculate normal/bg based on that +*/ + rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); + rwb->wb_normal = (rwb->wb_max +1 ) /2 ; + rwb->wb_background = (rwb->wb_max +3 ) /4 ; +} + +static bool inline stat_sample_valid (struct blk_rq_stat *stat) +{ +/* +* We need at least one read sample, and a minimum of +* RWB_MIN_WRITE_SAMPLES. We require some write samples to know +* that it's writes impacting us, and not just some sole read on +* a device that is in a lower power state. +*/ +return stat[0 ].nr_samples >=1 && + stat[1 ].nr_samples >=RWB_MIN_WRITE_SAMPLES ; +} +
Re: [PATCH 7/8] wbt: add general throttling mechanism
Quoted 27 lines · xiakaixu
Quoted 15 lines
(quoted diff) +15
-0+void __wbt_done(struct rq_wb *rwb) +{ + int inflight, limit = rwb->wb_normal; + + /* + * If the device does write back caching, drop further down + * before we wake people up. + */ + if (rwb->wc && !atomic_read(&rwb->bdi->wb.dirty_sleeping)) + limit = 0; + else + limit = rwb->wb_normal; + + /* + * Don't wake anyone up if we are above the normal limit. IfReview
Quoted 10 lines
(quoted diff) +10
-0+ * throttling got disabled (limit == 0) with waiters, ensure + * that we wake them up. + */ + inflight = atomic_dec_return(&rwb->inflight); + if (limit && inflight >= limit) { + if (!rwb->wb_max) + wake_up_all(&rwb->wait); + return; + } +Hi Jens,
Quoted 3 lines
Just a little confused about this. The
rwb->wb_maxcan't be 0 if the variable 'limit' does not equal to 0. So the if (!rwb->wb_max) branch maybe does not make sense.
You are right, it doesn't make a lot of sense. I think it suffers from code shuffling. How about the attached? The important part is that we wake up waiters, if wbt got disabled while we had tracked IO in flight.
Signature · 1 line
Jens Axboe
lib/wbt.c +12 -7
@@ -98,18 +98,23 @@ void __wbt_done(struct rq_wb *rwb)else limit = rwb->wb_normal; + inflight =atomic_dec_return (&rwb->inflight); +/* -* Don't wake anyone up if we are above the normal limit. If -* throttling got disabled (limit == 0) with waiters, ensure -* that we wake them up. +* wbt got disabled with IO in flight. Wake up any potential +* waiters, we don't have to do more than that. */ - inflight =atomic_dec_return (&rwb->inflight); -if (limit && inflight >= limit) { -if (!rwb->wb_max) -wake_up_all (&rwb->wait); +if (!rwb_enabled (rwb)) { +wake_up_all (&rwb->wait);return ; } +/* +* Don't wake anyone up if we are above the normal limit. +*/ +if (inflight >= limit) +return ; +if (waitqueue_active (&rwb->wait)) {int diff = limit - inflight;
Jan Kara
maintainerHi,
Quoted 14 lines · Jens Axboe
Since the dawn of time, our background buffered writeback has sucked. When we do background buffered writeback, it should have little impact on foreground activity. That's the definition of background activity... But for as long as I can remember, heavy buffered writers have not behaved like that. For instance, if I do something like this:
$ dd if=/dev/zero of=foo bs=1M count=10kon my laptop, and then try and start chrome, it basically won't start before the buffered writeback is done. Or, for server oriented workloads, where installation of a big RPM (or similar) adversely impacts database reads or sync writes. When that happens, I get people yelling at me.
Quoted 4 lines
I have posted plenty of results previously, I'll keep it shorter this time. Here's a run on my laptop, using read-to-pipe-async for reading a 5g file, and rewriting it. You can find this test program in the fio git repo.
I have tested your patchset on my test system. Generally I have observed noticeable drop in average throughput for heavy background writes without any other disk activity and also somewhat increased variance in the runtimes. It is most visible on this simple testcases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
and
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
The machine has 4GB of ram, /mnt is an ext3 filesystem that is freshly created before each dd run on a dedicated disk.
Without your patches I get pretty stable dd runtimes for both cases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 87.9611 87.3279 87.2554
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 93.3502 93.2086 93.541
With your patches the numbers look like:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 108.183, 97.184, 99.9587
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.9, 102.775, 102.892
I have checked whether the variance is due to some interaction with CFQ which is used for the disk. When I switched the disk to deadline, I still get some variance although, the throughput is still ~10% lower:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 100.417 100.643 100.866
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.208 106.341 105.483
The disk is rotational SATA drive with writeback cache, queue depth of the disk reported in /sys/block/sdb/device/queue_depth is 1.
So I think we still need some tweaking on the low end of the storage spectrum so that we don't lose 10% of throughput for simple cases like this.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 62 lines · Jan Kara
Hi,
Quoted 14 lines · Jens Axboe
Since the dawn of time, our background buffered writeback has sucked. When we do background buffered writeback, it should have little impact on foreground activity. That's the definition of background activity... But for as long as I can remember, heavy buffered writers have not behaved like that. For instance, if I do something like this:
$ dd if=/dev/zero of=foo bs=1M count=10kon my laptop, and then try and start chrome, it basically won't start before the buffered writeback is done. Or, for server oriented workloads, where installation of a big RPM (or similar) adversely impacts database reads or sync writes. When that happens, I get people yelling at me.
Quoted 4 lines
I have posted plenty of results previously, I'll keep it shorter this time. Here's a run on my laptop, using read-to-pipe-async for reading a 5g file, and rewriting it. You can find this test program in the fio git repo.
I have tested your patchset on my test system. Generally I have observed noticeable drop in average throughput for heavy background writes without any other disk activity and also somewhat increased variance in the runtimes. It is most visible on this simple testcases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
and
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
The machine has 4GB of ram, /mnt is an ext3 filesystem that is freshly created before each dd run on a dedicated disk.
Without your patches I get pretty stable dd runtimes for both cases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 87.9611 87.3279 87.2554dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 93.3502 93.2086 93.541With your patches the numbers look like:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 108.183, 97.184, 99.9587dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.9, 102.775, 102.892I have checked whether the variance is due to some interaction with CFQ which is used for the disk. When I switched the disk to deadline, I still get some variance although, the throughput is still ~10% lower:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 100.417 100.643 100.866dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.208 106.341 105.483
Quoted 6 lines
The disk is rotational SATA drive with writeback cache, queue depth of the disk reported in
/sys/block/sdb/device/queue_depthis 1.So I think we still need some tweaking on the low end of the storage spectrum so that we don't lose 10% of throughput for simple cases like this.
Thanks for testing, Jan! I haven't tried old QD=1 SATA. I wonder if you are seeing smaller requests, and that is why it both varies and you get lower throughput? I'll try and setup a test here similar to yours.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 66 lines · Jens Axboe
Quoted 65 lines · Jan Kara
Hi,
Quoted 14 lines · Jens Axboe
Since the dawn of time, our background buffered writeback has sucked. When we do background buffered writeback, it should have little impact on foreground activity. That's the definition of background activity... But for as long as I can remember, heavy buffered writers have not behaved like that. For instance, if I do something like this:
$ dd if=/dev/zero of=foo bs=1M count=10kon my laptop, and then try and start chrome, it basically won't start before the buffered writeback is done. Or, for server oriented workloads, where installation of a big RPM (or similar) adversely impacts database reads or sync writes. When that happens, I get people yelling at me.
Quoted 4 lines
I have posted plenty of results previously, I'll keep it shorter this time. Here's a run on my laptop, using read-to-pipe-async for reading a 5g file, and rewriting it. You can find this test program in the fio git repo.
I have tested your patchset on my test system. Generally I have observed noticeable drop in average throughput for heavy background writes without any other disk activity and also somewhat increased variance in the runtimes. It is most visible on this simple testcases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
and
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
The machine has 4GB of ram, /mnt is an ext3 filesystem that is freshly created before each dd run on a dedicated disk.
Without your patches I get pretty stable dd runtimes for both cases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 87.9611 87.3279 87.2554dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 93.3502 93.2086 93.541With your patches the numbers look like:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 108.183, 97.184, 99.9587dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.9, 102.775, 102.892I have checked whether the variance is due to some interaction with CFQ which is used for the disk. When I switched the disk to deadline, I still get some variance although, the throughput is still ~10% lower:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 100.417 100.643 100.866dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.208 106.341 105.483The disk is rotational SATA drive with writeback cache, queue depth of the disk reported in
/sys/block/sdb/device/queue_depthis 1.
Quoted 8 lines
Quoted 3 lines
So I think we still need some tweaking on the low end of the storage spectrum so that we don't lose 10% of throughput for simple cases like this.
Thanks for testing, Jan! I haven't tried old QD=1 SATA. I wonder if you are seeing smaller requests, and that is why it both varies and you get lower throughput? I'll try and setup a test here similar to yours.
Jan, care to try the below patch? I can't fully reproduce your issue on a SCSI disk limited to QD=1, but I have a feeling this might help. It's a bit of a hack, but the general idea is to allow one more request to build up for QD=1 devices. That eliminates wait time between one request finishing, and the next being submitted.
lib/wbt.c +18 -8
@@ -93,23 +93,30 @@ void __wbt_done(struct rq_wb *rwb)* If the device does write back caching, drop further down * before we wake people up. */ -if (rwb->wc && !atomic_read (&rwb->bdi->wb.dirty_sleeping)) +if (rwb->queue_depth ==1 ) + limit =2 ; +else if (rwb->wc && !atomic_read (&rwb->bdi->wb.dirty_sleeping)) limit =0 ;else limit = rwb->wb_normal; + inflight =atomic_dec_return (&rwb->inflight); +/* -* Don't wake anyone up if we are above the normal limit. If -* throttling got disabled (limit == 0) with waiters, ensure -* that we wake them up. +* wbt got disabled with IO in flight. Wake up any potential +* waiters, we don't have to do more than that. */ - inflight =atomic_dec_return (&rwb->inflight); -if (limit && inflight >= limit) { -if (!rwb->wb_max) -wake_up_all (&rwb->wait); +if (!rwb_enabled (rwb)) { +wake_up_all (&rwb->wait);return ; } +/* +* Don't wake anyone up if we are above the normal limit. +*/ +if (inflight >= limit) +return ; +if (waitqueue_active (&rwb->wait)) {int diff = limit - inflight;
@@ -366,6 +373,9 @@ static inline unsigned int get_limit(struct rq_wb *rwb, unsigned long rw) }else limit = rwb->wb_normal; +if (rwb->queue_depth ==1 ) + limit =2 ; +return limit; }
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 76 lines · Jens Axboe
Quoted 66 lines · Jens Axboe
Quoted 65 lines · Jan Kara
Hi,
Quoted 14 lines · Jens Axboe
Since the dawn of time, our background buffered writeback has sucked. When we do background buffered writeback, it should have little impact on foreground activity. That's the definition of background activity... But for as long as I can remember, heavy buffered writers have not behaved like that. For instance, if I do something like this:
$ dd if=/dev/zero of=foo bs=1M count=10kon my laptop, and then try and start chrome, it basically won't start before the buffered writeback is done. Or, for server oriented workloads, where installation of a big RPM (or similar) adversely impacts database reads or sync writes. When that happens, I get people yelling at me.
Quoted 4 lines
I have posted plenty of results previously, I'll keep it shorter this time. Here's a run on my laptop, using read-to-pipe-async for reading a 5g file, and rewriting it. You can find this test program in the fio git repo.
I have tested your patchset on my test system. Generally I have observed noticeable drop in average throughput for heavy background writes without any other disk activity and also somewhat increased variance in the runtimes. It is most visible on this simple testcases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
and
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
The machine has 4GB of ram, /mnt is an ext3 filesystem that is freshly created before each dd run on a dedicated disk.
Without your patches I get pretty stable dd runtimes for both cases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 87.9611 87.3279 87.2554dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 93.3502 93.2086 93.541With your patches the numbers look like:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 108.183, 97.184, 99.9587dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.9, 102.775, 102.892I have checked whether the variance is due to some interaction with CFQ which is used for the disk. When I switched the disk to deadline, I still get some variance although, the throughput is still ~10% lower:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 100.417 100.643 100.866dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.208 106.341 105.483The disk is rotational SATA drive with writeback cache, queue depth of the disk reported in
/sys/block/sdb/device/queue_depthis 1.Quoted 8 lines
Quoted 3 lines
So I think we still need some tweaking on the low end of the storage spectrum so that we don't lose 10% of throughput for simple cases like this.
Thanks for testing, Jan! I haven't tried old QD=1 SATA. I wonder if you are seeing smaller requests, and that is why it both varies and you get lower throughput? I'll try and setup a test here similar to yours.
Quoted 5 lines
Jan, care to try the below patch? I can't fully reproduce your issue on a SCSI disk limited to QD=1, but I have a feeling this might help. It's a bit of a hack, but the general idea is to allow one more request to build up for QD=1 devices. That eliminates wait time between one request finishing, and the next being submitted.
That accidentally added a potentially stall, this one is both cleaner and should have that fixed.
lib/wbt.c +30 -13
@@ -98,18 +98,23 @@ void __wbt_done(struct rq_wb *rwb)else limit = rwb->wb_normal; + inflight =atomic_dec_return (&rwb->inflight); +/* -* Don't wake anyone up if we are above the normal limit. If -* throttling got disabled (limit == 0) with waiters, ensure -* that we wake them up. +* wbt got disabled with IO in flight. Wake up any potential +* waiters, we don't have to do more than that. */ - inflight =atomic_dec_return (&rwb->inflight); -if (limit && inflight >= limit) { -if (!rwb->wb_max) -wake_up_all (&rwb->wait); +if (!rwb_enabled (rwb)) { +wake_up_all (&rwb->wait);return ; } +/* +* Don't wake anyone up if we are above the normal limit. +*/ +if (inflight && inflight >= limit) +return ; +if (waitqueue_active (&rwb->wait)) {int diff = limit - inflight;
@@ -150,14 +155,26 @@ static void calc_wb_limits(struct rq_wb *rwb)return ; } - depth = min_t(unsigned int, RWB_MAX_DEPTH, rwb->queue_depth); -/* -* Reduce max depth by 50%, and re-calculate normal/bg based on that +* For QD=1 devices, this is a special case. It's important for those +* to have one request ready when one completes, so force a depth of +* 2 for those devices. On the backend, it'll be a depth of 1 anyway, +* since the device can't have more than that in flight. */ - rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); - rwb->wb_normal = (rwb->wb_max +1 ) /2 ; - rwb->wb_background = (rwb->wb_max +3 ) /4 ; +if (rwb->queue_depth ==1 ) { + rwb->wb_max = rwb->wb_normal =2 ; + rwb->wb_background =1 ; + }else { + depth = min_t(unsigned int, RWB_MAX_DEPTH, rwb->queue_depth); + +/* +* Reduce max depth by 50%, and re-calculate normal/bg based on +* that. +*/ + rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); + rwb->wb_normal = (rwb->wb_max +1 ) /2 ; + rwb->wb_background = (rwb->wb_max +3 ) /4 ; + } }static bool inline stat_sample_valid (struct blk_rq_stat *stat)
Signature · 1 line
Jens Axboe
Re: [PATCH 7/8] wbt: add general throttling mechanism
In reply to Jens AxboeQuoted 28 lines · Jens Axboe
Quoted 27 lines · xiakaixu
Quoted 15 lines
(quoted diff) +15
-0+void __wbt_done(struct rq_wb *rwb) +{ + int inflight, limit = rwb->wb_normal; + + /* + * If the device does write back caching, drop further down + * before we wake people up. + */ + if (rwb->wc && !atomic_read(&rwb->bdi->wb.dirty_sleeping)) + limit = 0; + else + limit = rwb->wb_normal; + + /* + * Don't wake anyone up if we are above the normal limit. IfReview
Quoted 10 lines
(quoted diff) +10
-0+ * throttling got disabled (limit == 0) with waiters, ensure + * that we wake them up. + */ + inflight = atomic_dec_return(&rwb->inflight); + if (limit && inflight >= limit) { + if (!rwb->wb_max) + wake_up_all(&rwb->wait); + return; + } +Hi Jens,
Quoted 6 lines
Quoted 3 lines
Just a little confused about this. The
rwb->wb_maxcan't be 0 if the variable 'limit' does not equal to 0. So the if (!rwb->wb_max) branch maybe does not make sense.You are right, it doesn't make a lot of sense. I think it suffers from code shuffling. How about the attached? The important part is that we wake up waiters, if wbt got disabled while we had tracked IO in flight.
Hi Jens,
The modified patch in another mail looks better. Maybe there are still some places coube be improved. You can find them in that mail.
Signature · 2 lines
Regards Kaixu Xia
Quoted 91 lines · Jens Axboe
Quoted 76 lines · Jens Axboe
Quoted 66 lines · Jens Axboe
Quoted 65 lines · Jan Kara
Hi,
Quoted 14 lines · Jens Axboe
Since the dawn of time, our background buffered writeback has sucked. When we do background buffered writeback, it should have little impact on foreground activity. That's the definition of background activity... But for as long as I can remember, heavy buffered writers have not behaved like that. For instance, if I do something like this:
$ dd if=/dev/zero of=foo bs=1M count=10kon my laptop, and then try and start chrome, it basically won't start before the buffered writeback is done. Or, for server oriented workloads, where installation of a big RPM (or similar) adversely impacts database reads or sync writes. When that happens, I get people yelling at me.
Quoted 4 lines
I have posted plenty of results previously, I'll keep it shorter this time. Here's a run on my laptop, using read-to-pipe-async for reading a 5g file, and rewriting it. You can find this test program in the fio git repo.
I have tested your patchset on my test system. Generally I have observed noticeable drop in average throughput for heavy background writes without any other disk activity and also somewhat increased variance in the runtimes. It is most visible on this simple testcases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
and
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
The machine has 4GB of ram, /mnt is an ext3 filesystem that is freshly created before each dd run on a dedicated disk.
Without your patches I get pretty stable dd runtimes for both cases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 87.9611 87.3279 87.2554dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 93.3502 93.2086 93.541With your patches the numbers look like:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 108.183, 97.184, 99.9587dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.9, 102.775, 102.892I have checked whether the variance is due to some interaction with CFQ which is used for the disk. When I switched the disk to deadline, I still get some variance although, the throughput is still ~10% lower:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 100.417 100.643 100.866dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.208 106.341 105.483The disk is rotational SATA drive with writeback cache, queue depth of the disk reported in
/sys/block/sdb/device/queue_depthis 1.Quoted 8 lines
Quoted 3 lines
So I think we still need some tweaking on the low end of the storage spectrum so that we don't lose 10% of throughput for simple cases like this.
Thanks for testing, Jan! I haven't tried old QD=1 SATA. I wonder if you are seeing smaller requests, and that is why it both varies and you get lower throughput? I'll try and setup a test here similar to yours.
Quoted 5 lines
Jan, care to try the below patch? I can't fully reproduce your issue on a SCSI disk limited to QD=1, but I have a feeling this might help. It's a bit of a hack, but the general idea is to allow one more request to build up for QD=1 devices. That eliminates wait time between one request finishing, and the next being submitted.
That accidentally added a potentially stall, this one is both cleaner and should have that fixed.
lib/wbt.c +0
-0
lib/wbt.c void __wbt_done(struct rq_wb *rwb) · line 99
Quoted 2 lines
lib/wbt.c +0
-0@@ -98,18 +98,23 @@ void __wbt_done(struct rq_wb *rwb)else limit = rwb->wb_normal;
Hi Jens,
This statement 'limit = rwb->wb_normal' is executed twice, maybe once is enough. It is not a big deal anyway :)
Another question about this if branch:
if (rwb->wc && !atomic_read(&rwb->bdi->wb.dirty_sleeping))
limit = 0;
I can't follow the logic of this if branch. why set limit equal to 0 when the device supports write back caches and there are tasks being limited in balance_dirty_pages(). Could you pelase give more info about this ? Thanks!
Quoted 8 lines
lib/wbt.c +3
-3+ inflight =atomic_dec_return (&rwb->inflight); +/* -* Don't wake anyone up if we are above the normal limit. If -* throttling got disabled (limit == 0) with waiters, ensure -* that we wake them up. +* wbt got disabled with IO in flight. Wake up any potential
lib/wbt.c void __wbt_done(struct rq_wb *rwb) · line 110
Quoted 10 lines
lib/wbt.c +3
-4+* waiters, we don't have to do more than that. */ - inflight =atomic_dec_return (&rwb->inflight); -if (limit && inflight >= limit) { -if (!rwb->wb_max) -wake_up_all (&rwb->wait); +if (!rwb_enabled (rwb)) { +wake_up_all (&rwb->wait);return ; }
Maybe it is better that executing this if branch earlier. So we can wake up potential waiters in time when wbt got disabled.
Quoted 44 lines
lib/wbt.c +24
-6+/* +* Don't wake anyone up if we are above the normal limit. +*/ +if (inflight && inflight >= limit) +return ; +if (waitqueue_active (&rwb->wait)) {int diff = limit - inflight;@@ -150,14 +155,26 @@ static void calc_wb_limits(struct rq_wb *rwb)return ; } - depth = min_t(unsigned int, RWB_MAX_DEPTH, rwb->queue_depth); -/* -* Reduce max depth by 50%, and re-calculate normal/bg based on that +* For QD=1 devices, this is a special case. It's important for those +* to have one request ready when one completes, so force a depth of +* 2 for those devices. On the backend, it'll be a depth of 1 anyway, +* since the device can't have more than that in flight. */ - rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); - rwb->wb_normal = (rwb->wb_max +1 ) /2 ; - rwb->wb_background = (rwb->wb_max +3 ) /4 ; +if (rwb->queue_depth ==1 ) { + rwb->wb_max = rwb->wb_normal =2 ; + rwb->wb_background =1 ; + }else { + depth = min_t(unsigned int, RWB_MAX_DEPTH, rwb->queue_depth); + +/* +* Reduce max depth by 50%, and re-calculate normal/bg based on +* that. +*/ + rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); + rwb->wb_normal = (rwb->wb_max +1 ) /2 ; + rwb->wb_background = (rwb->wb_max +3 ) /4 ; + } }static bool inline stat_sample_valid (struct blk_rq_stat *stat)
Signature · 2 lines
Regards Kaixu Xia
Jan Kara
maintainerRe: [PATCH 7/8] wbt: add general throttling mechanism
In reply to Jens AxboeQuoted 8 lines · Jens Axboe
We can hook this up to the block layer, to help throttle buffered writes. Or NFS can tap into it, to accomplish the same.
wbt registers a few trace points that can be used to track what is happening in the system:
wbt_lat: 259:0: latency 2446318wbt_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=32This shows a sync issue event (
wbt_lat) that exceeded it's time.wbt_statdumps the current read/write stats for that window, andwbt_stepshows a step down event where we now scale back writes. Each trace includes the device, 259:0 in this case.
I have some comments below...
include/linux/wbt.h line 59
Quoted 13 lines
include/linux/wbt.h new file +13
-0+struct rq_wb { +/* +* Settings that govern how we throttle +*/ +unsigned int wb_background;/* background writeback */ +unsigned int wb_normal;/* normal writeback */ +unsigned int wb_max;/* max throughput writeback */ +unsigned int scale_step; + +u64 win_nsec;/* default window size */ +u64 cur_win_nsec;/* current window size */ + +unsigned int unknown_cnt;
It would be useful to have a comment here explaining that 'unknown_cnt' is a number of consecutive periods in which we didn't have enough data to decide about queue scaling (at least this is what I understood from the code).
include/linux/wbt.h line 64
Quoted 5 lines
include/linux/wbt.h new file +5
-0+ +struct timer_list window_timer; + +s64 sync_issue; +void *sync_cookie;
So I'm somewhat wondering: What is protecting consistency of this structure? The limits, scale_step, cur_win_nsec, unknown_cnt are updated only from timer so those should be safe. However sync_issue & sync_cookie are accessed from IO submission and completion path and there we need some protection to keep those two in sync. It seems q->queue_lock should mostly achieve those except for blk-mq submission path calling wbt_wait() which doesn't hold queue_lock.
It seems you were aware of the possible races and the code handles them mostly fine (although I wouldn't bet too much there is not some weird corner case). However it would be good to comment on this somewhere and explain what the rules for these two fields are.
Quoted 15 lines
include/linux/wbt.h new file +15
-0+ +unsigned int wc; +unsigned int queue_depth; + +unsigned long last_issue;/* last non-throttled issue */ +unsigned long last_comp;/* last non-throttled comp */ +unsigned long min_lat_nsec; +struct backing_dev_info *bdi; +struct request_queue *q; +wait_queue_head_t wait; +atomic_t inflight; + +struct wb_stat_ops *stat_ops; +void *ops_data; +};
...
Quoted 7 lines
lib/wbt.c new file +1
-0@@ -0,0 +1,524 @@ +/*
lib/wbt.c line 11
Quoted 10 lines
lib/wbt.c new file +10
-0+* buffered writeback throttling. losely based on CoDel. We can't drop +* packets for IO scheduling, so the logic is something like this: +* +* - Monitor latencies in a defined window of time. +* - If the minimum latency in the above window exceeds some target, increment +* scaling step and scale down queue depth by a factor of 2x. The monitoring +* window is then shrunk to 100 / sqrt(scaling step + 1). +* - For any window where we don't have solid data on what the latencies +* look like, retain status quo. +* - If latencies look good, decrement scaling step.
I'm wondering about two things:
1) There is a logic somewhat in this direction in blk_queue_start_tag(). Probably it should be removed after your patches land?
2) As far as I can see in patch 8/8, you have plugged the throttling above the IO scheduler. When there are e.g. multiple cgroups with different IO limits operating, this throttling can lead to strange results (like a cgroup with low limit using up all available background "slots" and thus effectively stopping background writeback for other cgroups)? So won't it make more sense to plug this below the IO scheduler? Now I understand there may be other problems with this but I think we should put more though to that and provide some justification in changelogs.
Quoted 4 lines
lib/wbt.c new file +4
-0+static void calc_wb_limits (struct rq_wb *rwb) +{ +unsigned int depth; +
lib/wbt.c line 157
Quoted 10 lines
lib/wbt.c new file +10
-0+if (!rwb->min_lat_nsec) { + rwb->wb_max = rwb->wb_normal = rwb->wb_background =0 ; +return ; + } + + depth = min_t(unsigned int, RWB_MAX_DEPTH, rwb->queue_depth); + +/* +* Reduce max depth by 50%, and re-calculate normal/bg based on that +*/
The comment looks a bit out of place here since we don't reduce max depth here. We just use whatever is set in scale_step...
Quoted 39 lines
lib/wbt.c new file +39
-0+ rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); + rwb->wb_normal = (rwb->wb_max +1 ) /2 ; + rwb->wb_background = (rwb->wb_max +3 ) /4 ; +} + +static bool inline stat_sample_valid (struct blk_rq_stat *stat) +{ +/* +* We need at least one read sample, and a minimum of +* RWB_MIN_WRITE_SAMPLES. We require some write samples to know +* that it's writes impacting us, and not just some sole read on +* a device that is in a lower power state. +*/ +return stat[0 ].nr_samples >=1 && + stat[1 ].nr_samples >=RWB_MIN_WRITE_SAMPLES ; +} + +static u64 rwb_sync_issue_lat (struct rq_wb *rwb) +{ +u64 now, issue =ACCESS_ONCE (rwb->sync_issue); + +if (!issue || !rwb->sync_cookie) +return 0 ; + + now =ktime_to_ns (ktime_get ()); +return now - issue; +} + +enum { +LAT_OK , +LAT_UNKNOWN , +LAT_EXCEEDED , +}; + +static int __latency_exceeded (struct rq_wb *rwb,struct blk_rq_stat *stat) +{ +u64 thislat; + +/*
lib/wbt.c line 206
Quoted 10 lines
lib/wbt.c new file +10
-0+* If our stored sync issue exceeds the window size, or it +* exceeds our min target AND we haven't logged any entries, +* flag the latency as exceeded. +*/ + thislat =rwb_sync_issue_lat (rwb); +if (thislat > rwb->cur_win_nsec || + (thislat > rwb->min_lat_nsec && !stat[0 ].nr_samples)) { +trace_wbt_lat (rwb->bdi, thislat); +return LAT_EXCEEDED ; + }
So I'm trying to wrap my head around this. If I read the code right, rwb_sync_issue_lat() this returns time that has passed since issuing sync request that is still running. We basically randomly pick which sync request we track as we always start tracking a sync request when some is issued and we are not tracking any at that moment. This is to detect the case when latency of sync IO is very large compared to measurement window so we would not get enough samples to make it valid?
Probably the comment could explain more of "why we do this?" than pure "what we do".
Quoted 9 lines
lib/wbt.c new file +9
-0+ +if (!stat_sample_valid (stat)) +return LAT_UNKNOWN ; + +/* +* If the 'min' latency exceeds our target, step down. +*/ +if (stat[0 ].min > rwb->min_lat_nsec) { +trace_wbt_lat (rwb->bdi, stat[0 ].min);
lib/wbt.c line 225
Quoted 10 lines
lib/wbt.c new file +10
-0+trace_wbt_stat (rwb->bdi, stat); +return LAT_EXCEEDED ; + } + +if (rwb->scale_step) +trace_wbt_stat (rwb->bdi, stat); + +return LAT_OK ; +} +
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
maintainerQuoted 86 lines · Jens Axboe
Quoted 76 lines · Jens Axboe
Quoted 66 lines · Jens Axboe
Quoted 65 lines · Jan Kara
Hi,
Quoted 14 lines · Jens Axboe
Since the dawn of time, our background buffered writeback has sucked. When we do background buffered writeback, it should have little impact on foreground activity. That's the definition of background activity... But for as long as I can remember, heavy buffered writers have not behaved like that. For instance, if I do something like this:
$ dd if=/dev/zero of=foo bs=1M count=10kon my laptop, and then try and start chrome, it basically won't start before the buffered writeback is done. Or, for server oriented workloads, where installation of a big RPM (or similar) adversely impacts database reads or sync writes. When that happens, I get people yelling at me.
Quoted 4 lines
I have posted plenty of results previously, I'll keep it shorter this time. Here's a run on my laptop, using read-to-pipe-async for reading a 5g file, and rewriting it. You can find this test program in the fio git repo.
I have tested your patchset on my test system. Generally I have observed noticeable drop in average throughput for heavy background writes without any other disk activity and also somewhat increased variance in the runtimes. It is most visible on this simple testcases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
and
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
The machine has 4GB of ram, /mnt is an ext3 filesystem that is freshly created before each dd run on a dedicated disk.
Without your patches I get pretty stable dd runtimes for both cases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 87.9611 87.3279 87.2554dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 93.3502 93.2086 93.541With your patches the numbers look like:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 108.183, 97.184, 99.9587dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.9, 102.775, 102.892I have checked whether the variance is due to some interaction with CFQ which is used for the disk. When I switched the disk to deadline, I still get some variance although, the throughput is still ~10% lower:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 100.417 100.643 100.866dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.208 106.341 105.483The disk is rotational SATA drive with writeback cache, queue depth of the disk reported in
/sys/block/sdb/device/queue_depthis 1.Quoted 8 lines
Quoted 3 lines
So I think we still need some tweaking on the low end of the storage spectrum so that we don't lose 10% of throughput for simple cases like this.
Thanks for testing, Jan! I haven't tried old QD=1 SATA. I wonder if you are seeing smaller requests, and that is why it both varies and you get lower throughput? I'll try and setup a test here similar to yours.
Quoted 5 lines
Jan, care to try the below patch? I can't fully reproduce your issue on a SCSI disk limited to QD=1, but I have a feeling this might help. It's a bit of a hack, but the general idea is to allow one more request to build up for QD=1 devices. That eliminates wait time between one request finishing, and the next being submitted.
That accidentally added a potentially stall, this one is both cleaner and should have that fixed.
..
lib/wbt.c static void calc_wb_limits(struct rq_wb *rwb) · line 166
Quoted 6 lines
lib/wbt.c +3
-3- rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); - rwb->wb_normal = (rwb->wb_max +1 ) /2 ; - rwb->wb_background = (rwb->wb_max +3 ) /4 ; +if (rwb->queue_depth ==1 ) { + rwb->wb_max = rwb->wb_normal =2 ; + rwb->wb_background =1 ;
This breaks the detection of too big scale_step in scale_up() where we key of wb_max == 1 value. However even with that fixed no luck :(:
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtime: 105.126 107.125 105.641
So about the same as before. I'll try to debug this later today...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jens Axboe
maintainerQuoted 11 lines · xiakaixu
lib/wbt.c void __wbt_done(struct rq_wb *rwb) · line 99
Quoted 7 lines
lib/wbt.c +0
-0@@ -98,18 +98,23 @@ void __wbt_done(struct rq_wb *rwb)else limit = rwb->wb_normal;Hi Jens,
This statement 'limit =
rwb->wb_normal' is executed twice, maybe once is enough. It is not a big deal anyway :)
I'll clean that up, thanks for noticing. No functional difference.
Quoted 9 lines
Another question about this if branch:
if (rwb->wc && !atomic_read(&rwb->bdi->wb.dirty_sleeping)) limit = 0;I can't follow the logic of this if branch. why set limit equal to 0 when the device supports write back caches and there are tasks being limited in
balance_dirty_pages(). Could you pelase give more info about this ? Thanks!
Sure. So for write back caching, we have to try a bit harder to ensure that the device doesn't build up long internal queues with a lot of dirty data in the cache. So for the case where we have write back caching AND we don't have anyone waiting for the IO, allow the queue depth to drain to zero before building it back up again.
Does that make sense?
Quoted 13 lines
Quoted 13 lines
lib/wbt.c +4
-6+ inflight =atomic_dec_return (&rwb->inflight); +/* -* Don't wake anyone up if we are above the normal limit. If -* throttling got disabled (limit == 0) with waiters, ensure -* that we wake them up. +* wbt got disabled with IO in flight. Wake up any potential +* waiters, we don't have to do more than that. */ - inflight =atomic_dec_return (&rwb->inflight); -if (limit && inflight >= limit) { -if (!rwb->wb_max)
Quoted 8 lines
lib/wbt.c void __wbt_done(struct rq_wb *rwb) · line 110
Quoted 5 lines
lib/wbt.c +2
-1-wake_up_all (&rwb->wait); +if (!rwb_enabled (rwb)) { +wake_up_all (&rwb->wait);return ; }Maybe it is better that executing this if branch earlier. So we can wake up potential waiters in time when wbt got disabled.
The !rwb_enabled() case will only happen if someone disabled wbt while we had tracked IO in flight. We have to it below the atomic_dec_return(), so we could reorder that to be at the front. Ideally we just want it out-of-line instead, as it's the unexpected slower path.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 88 lines · Jan Kara
Quoted 86 lines · Jens Axboe
Quoted 76 lines · Jens Axboe
Quoted 66 lines · Jens Axboe
Quoted 65 lines · Jan Kara
Hi,
Quoted 14 lines · Jens Axboe
Since the dawn of time, our background buffered writeback has sucked. When we do background buffered writeback, it should have little impact on foreground activity. That's the definition of background activity... But for as long as I can remember, heavy buffered writers have not behaved like that. For instance, if I do something like this:
$ dd if=/dev/zero of=foo bs=1M count=10kon my laptop, and then try and start chrome, it basically won't start before the buffered writeback is done. Or, for server oriented workloads, where installation of a big RPM (or similar) adversely impacts database reads or sync writes. When that happens, I get people yelling at me.
Quoted 4 lines
I have posted plenty of results previously, I'll keep it shorter this time. Here's a run on my laptop, using read-to-pipe-async for reading a 5g file, and rewriting it. You can find this test program in the fio git repo.
I have tested your patchset on my test system. Generally I have observed noticeable drop in average throughput for heavy background writes without any other disk activity and also somewhat increased variance in the runtimes. It is most visible on this simple testcases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
and
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
The machine has 4GB of ram, /mnt is an ext3 filesystem that is freshly created before each dd run on a dedicated disk.
Without your patches I get pretty stable dd runtimes for both cases:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 87.9611 87.3279 87.2554dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 93.3502 93.2086 93.541With your patches the numbers look like:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 108.183, 97.184, 99.9587dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.9, 102.775, 102.892I have checked whether the variance is due to some interaction with CFQ which is used for the disk. When I switched the disk to deadline, I still get some variance although, the throughput is still ~10% lower:
dd if=/dev/zero of=/mnt/file bs=1M count=10000
Runtimes: 100.417 100.643 100.866dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtimes: 104.208 106.341 105.483The disk is rotational SATA drive with writeback cache, queue depth of the disk reported in
/sys/block/sdb/device/queue_depthis 1.Quoted 8 lines
Quoted 3 lines
So I think we still need some tweaking on the low end of the storage spectrum so that we don't lose 10% of throughput for simple cases like this.
Thanks for testing, Jan! I haven't tried old QD=1 SATA. I wonder if you are seeing smaller requests, and that is why it both varies and you get lower throughput? I'll try and setup a test here similar to yours.
Quoted 5 lines
Jan, care to try the below patch? I can't fully reproduce your issue on a SCSI disk limited to QD=1, but I have a feeling this might help. It's a bit of a hack, but the general idea is to allow one more request to build up for QD=1 devices. That eliminates wait time between one request finishing, and the next being submitted.
That accidentally added a potentially stall, this one is both cleaner and should have that fixed.
..
Quoted 9 lines
lib/wbt.c static void calc_wb_limits(struct rq_wb *rwb) · line 166
Quoted 6 lines
lib/wbt.c +3
-3- rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); - rwb->wb_normal = (rwb->wb_max +1 ) /2 ; - rwb->wb_background = (rwb->wb_max +3 ) /4 ; +if (rwb->queue_depth ==1 ) { + rwb->wb_max = rwb->wb_normal =2 ; + rwb->wb_background =1 ;This breaks the detection of too big
scale_stepinscale_up()where we key ofwb_max== 1 value. However even with that fixed no luck :(:
Yeah, I need to look at that. For QD=1, I think the only sensible values for max/normal/bg is 2/2/1 and 1/1/1 if we step down.
Quoted 4 lines
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtime: 105.126 107.125 105.641So about the same as before. I'll try to debug this later today...
Thanks, I'm very interested in what you find!
Signature · 1 line
Jens Axboe
Re: [PATCH 7/8] wbt: add general throttling mechanism
In reply to Jan KaraQuoted 12 lines · Jan Kara
I have some comments below...
Quoted 10 lines
include/linux/wbt.h new file +10
-0+struct rq_wb { +/* +* Settings that govern how we throttle +*/ +unsigned int wb_background;/* background writeback */ +unsigned int wb_normal;/* normal writeback */ +unsigned int wb_max;/* max throughput writeback */ +unsigned int scale_step; + +u64 win_nsec;/* default window size */
Quoted 8 lines
include/linux/wbt.h line 59
Quoted 3 lines
include/linux/wbt.h new file +3
-0+u64 cur_win_nsec;/* current window size */ + +unsigned int unknown_cnt;It would be useful to have a comment here explaining that '
unknown_cnt' is a number of consecutive periods in which we didn't have enough data to decide about queue scaling (at least this is what I understood from the code).
Agree, I'll add that comment.
Quoted 6 lines
include/linux/wbt.h line 64
Quoted 5 lines
include/linux/wbt.h new file +5
-0+ +struct timer_list window_timer; + +s64 sync_issue; +void *sync_cookie;
Quoted 7 lines
So I'm somewhat wondering: What is protecting consistency of this structure? The limits,
scale_step,cur_win_nsec,unknown_cntare updated only from timer so those should be safe. Howeversync_issue&sync_cookieare accessed from IO submission and completion path and there we need some protection to keep those two in sync. It seemsq->queue_lockshould mostly achieve those except for blk-mq submission path callingwbt_wait()which doesn't holdqueue_lock.
Right, it's designed such that only the timer will be updating these values, and that part is serialized. For sync_issue and sync_cookie, the important part there is that we never dereference sync_cookie. That's why it's a void * now. So we just use it as a hint. And yes, if the IO happens to complete at just the time we are looking at it, we could get a false positive or false negative. That's going to be noise, and nothing we need to worry about. It's deliberate that I don't do any locking for that, the only reason we pass in the queue_lock is to be able to drop it for sleeping.
Quoted 4 lines
It seems you were aware of the possible races and the code handles them mostly fine (although I wouldn't bet too much there is not some weird corner case). However it would be good to comment on this somewhere and explain what the rules for these two fields are.
Agree, it does warrant a good code comment. If we look at the edge cases, one would be:
We look at sync_issue and decide that we're now too late, at the same time as the sync_cookie gets cleared. For this case, we'll count it as an exceed and scale down. In reality we were late, so it doesn't matter. Even if it was the exact time, it's still prudent to scale down as we're going to miss soon.
A more worrying case would be two issues that happen at the same time, and only one gets set. Let's assume the one that doesn't get set is the one that ends up taking a long time to complete. We'll miss scaling down in this case, we'll only notice when it completes and shows up in the stats. Not idea, but it's still being handled in the fashion that was originally intended, at completion time.
Quoted 20 lines
Quoted 7 lines
lib/wbt.c new file +1
-0@@ -0,0 +1,524 @@ +/* lib/wbt.c line 11
Quoted 10 lines
lib/wbt.c new file +10
-0+* buffered writeback throttling. losely based on CoDel. We can't drop +* packets for IO scheduling, so the logic is something like this: +* +* - Monitor latencies in a defined window of time. +* - If the minimum latency in the above window exceeds some target, increment +* scaling step and scale down queue depth by a factor of 2x. The monitoring +* window is then shrunk to 100 / sqrt(scaling step + 1). +* - For any window where we don't have solid data on what the latencies +* look like, retain status quo. +* - If latencies look good, decrement scaling step. I'm wondering about two things:
Quoted 2 lines
1) There is a logic somewhat in this direction in
blk_queue_start_tag(). Probably it should be removed after your patches land?
You're referring to the read/write separation in the legacy tagging? Yes agree, we can kill that once this goes in.
Quoted 8 lines
2) As far as I can see in patch 8/8, you have plugged the throttling above the IO scheduler. When there are e.g. multiple cgroups with different IO limits operating, this throttling can lead to strange results (like a cgroup with low limit using up all available background "slots" and thus effectively stopping background writeback for other cgroups)? So won't it make more sense to plug this below the IO scheduler? Now I understand there may be other problems with this but I think we should put more though to that and provide some justification in changelogs.
One complexity is that we have to do this early for blk-mq, since once you get a request, you're already sitting on the hw tag. CoDel should actually work fine at each hop, so hopefully this will as well.
But yes, fairness is something that we have to pay attention to. Right now the wait queue has no priority associated with it, that should probably be improved to be able to wakeup in a more appropriate order. Needs testing, but hopefully it works out since if you do run into starvation, then you'll go to the back of the queue for the next attempt.
Quoted 9 lines
Quoted 9 lines
lib/wbt.c new file +9
-0+static void calc_wb_limits (struct rq_wb *rwb) +{ +unsigned int depth; + +if (!rwb->min_lat_nsec) { + rwb->wb_max = rwb->wb_normal = rwb->wb_background =0 ; +return ; + } +
Quoted 8 lines
lib/wbt.c line 157
Quoted 5 lines
lib/wbt.c new file +5
-0+ depth = min_t(unsigned int, RWB_MAX_DEPTH, rwb->queue_depth); + +/* +* Reduce max depth by 50%, and re-calculate normal/bg based on that +*/ The comment looks a bit out of place here since we don't reduce max depth here. We just use whatever is set in
scale_step...
True, it does get called for both scaling up and down now. I'll update the comment.
Quoted 16 lines
Quoted 5 lines
lib/wbt.c new file +5
-0+static int __latency_exceeded (struct rq_wb *rwb,struct blk_rq_stat *stat) +{ +u64 thislat; + +/* lib/wbt.c line 206
Quoted 10 lines
lib/wbt.c new file +10
-0+* If our stored sync issue exceeds the window size, or it +* exceeds our min target AND we haven't logged any entries, +* flag the latency as exceeded. +*/ + thislat =rwb_sync_issue_lat (rwb); +if (thislat > rwb->cur_win_nsec || + (thislat > rwb->min_lat_nsec && !stat[0 ].nr_samples)) { +trace_wbt_lat (rwb->bdi, thislat); +return LAT_EXCEEDED ; + }
Quoted 7 lines
So I'm trying to wrap my head around this. If I read the code right,
rwb_sync_issue_lat()this returns time that has passed since issuing sync request that is still running. We basically randomly pick which sync request we track as we always start tracking a sync request when some is issued and we are not tracking any at that moment. This is to detect the case when latency of sync IO is very large compared to measurement window so we would not get enough samples to make it valid?
Right, that's pretty close. Since wbt uses the completion latencies to make decisions, if an IO hasn't completed, we don't know about it. If the device is flooded with writes, and we then issue a read, maybe that read won't complete for multiple monitoring windows. During that time, we keep thinking everything is fine. But in reality, it's not completing because of the write load. So this logic attempts to track the single sync IO request case. If that exceeds a monitoring window of time and we saw no other sync IO in that window, then treat that case as if it had completed but exceeded the min latency. And then scale back.
We'll always treat a state sample with 1 read as valuable, but for this case, we don't have that sample until it completes.
Does that make more sense?
Quoted 2 lines
Probably the comment could explain more of "why we do this?" than pure "what we do".
Agree, if you find it confusing, then it needs updating. I'll update the comment.
Signature · 1 line
Jens Axboe
Re: [PATCH 7/8] wbt: add general throttling mechanism
Quoted 6 lines · Jens Axboe
Quoted 3 lines
Probably the comment could explain more of "why we do this?" than pure "what we do".
Agree, if you find it confusing, then it needs updating. I'll update the comment.
http://git.kernel.dk/cgit/linux-block/commit/?h=wb-buf-throttle
This should address your review comments, I believe.
Signature · 1 line
Jens Axboe
Jan Kara
maintainerRe: [PATCH 7/8] wbt: add general throttling mechanism
In reply to Jens AxboeQuoted 9 lines · Jens Axboe
Quoted 8 lines
2) As far as I can see in patch 8/8, you have plugged the throttling above the IO scheduler. When there are e.g. multiple cgroups with different IO limits operating, this throttling can lead to strange results (like a cgroup with low limit using up all available background "slots" and thus effectively stopping background writeback for other cgroups)? So won't it make more sense to plug this below the IO scheduler? Now I understand there may be other problems with this but I think we should put more though to that and provide some justification in changelogs.
Quoted 3 lines
One complexity is that we have to do this early for blk-mq, since once you get a request, you're already sitting on the hw tag. CoDel should actually work fine at each hop, so hopefully this will as well.
OK, I see. But then this suggests that any IO scheduling and / or cgroup-related throttling should happen before we get a request for blk-mq as well? And then we can still do writeback throttling below that layer?
Quoted 5 lines
But yes, fairness is something that we have to pay attention to. Right now the wait queue has no priority associated with it, that should probably be improved to be able to wakeup in a more appropriate order. Needs testing, but hopefully it works out since if you do run into starvation, then you'll go to the back of the queue for the next attempt.
Yeah, once I'll hunt down that regression with old disk, I can have a look into how writeback throttling plays together with blkio-controller.
Quoted 34 lines
Quoted 16 lines
Quoted 5 lines
lib/wbt.c new file +5
-0+static int __latency_exceeded (struct rq_wb *rwb,struct blk_rq_stat *stat) +{ +u64 thislat; + +/* lib/wbt.c line 206
Quoted 10 lines
lib/wbt.c new file +10
-0+* If our stored sync issue exceeds the window size, or it +* exceeds our min target AND we haven't logged any entries, +* flag the latency as exceeded. +*/ + thislat =rwb_sync_issue_lat (rwb); +if (thislat > rwb->cur_win_nsec || + (thislat > rwb->min_lat_nsec && !stat[0 ].nr_samples)) { +trace_wbt_lat (rwb->bdi, thislat); +return LAT_EXCEEDED ; + }Quoted 7 lines
So I'm trying to wrap my head around this. If I read the code right,
rwb_sync_issue_lat()this returns time that has passed since issuing sync request that is still running. We basically randomly pick which sync request we track as we always start tracking a sync request when some is issued and we are not tracking any at that moment. This is to detect the case when latency of sync IO is very large compared to measurement window so we would not get enough samples to make it valid?Right, that's pretty close. Since wbt uses the completion latencies to make decisions, if an IO hasn't completed, we don't know about it. If the device is flooded with writes, and we then issue a read, maybe that read won't complete for multiple monitoring windows. During that time, we keep thinking everything is fine. But in reality, it's not completing because of the write load. So this logic attempts to track the single sync IO request case. If that exceeds a monitoring window of time and we saw no other sync IO in that window, then treat that case as if it had completed but exceeded the min latency. And then scale back.
Quoted 4 lines
We'll always treat a state sample with 1 read as valuable, but for this case, we don't have that sample until it completes.
Does that make more sense?
OK, makes sense. Thanks for explanation.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
maintainerQuoted 13 lines · Jens Axboe
Quoted 9 lines
lib/wbt.c static void calc_wb_limits(struct rq_wb *rwb) · line 166
Quoted 6 lines
lib/wbt.c +3
-3- rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); - rwb->wb_normal = (rwb->wb_max +1 ) /2 ; - rwb->wb_background = (rwb->wb_max +3 ) /4 ; +if (rwb->queue_depth ==1 ) { + rwb->wb_max = rwb->wb_normal =2 ; + rwb->wb_background =1 ;This breaks the detection of too big
scale_stepinscale_up()where we key ofwb_max== 1 value. However even with that fixed no luck :(:Yeah, I need to look at that. For QD=1, I think the only sensible values for max/normal/bg is 2/2/1 and 1/1/1 if we step down.
Quoted 6 lines
Quoted 4 lines
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtime: 105.126 107.125 105.641So about the same as before. I'll try to debug this later today...
Thanks, I'm very interested in what you find!
OK, so the reason was relatively standard in the end. I was using ext3 (or more exactly ext4 without delayed allocation) for the test. The throttling of background writes gave more priority to writes from the journalling thread which happen with WRITE_SYNC and thus are not throttled. Thus the journalling thread ended up having to do more data writeback to be able to commit a transaction (due to requirements of data=ordered mode) and it is less efficient at that than the normal flusher thread.
So this is an example where throttling background writeback effectively just pushes more work into another context which does it less efficiently and indirectly makes everyone wait for it. ext3 has been always sensitive to issues like this. ext4 is using delayed allocation and thus only data writes into holes end up being part of a transaction -> simple dd test case doesn't hit that path. And indeed when I repeat the same test with ext4, the numbers with and without your patch are exactly the same.
The question remains how common a pattern where throttling of background writeback delays also something else is. I'll schedule a couple of benchmarks to measure impact of your patches for a wider range of workloads (but sadly pretty limited set of hw). If ext3 is the only one seeing issues, I would be willing to accept that ext3 takes the hit since it is doing something rather stupid (but inherent in its journal design) and we have a way to deal with this either by enabling delayed allocation or by turning off the writeback throttling...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 37 lines · Jan Kara
Quoted 13 lines · Jens Axboe
Quoted 9 lines
lib/wbt.c static void calc_wb_limits(struct rq_wb *rwb) · line 166
Quoted 6 lines
lib/wbt.c +3
-3- rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); - rwb->wb_normal = (rwb->wb_max +1 ) /2 ; - rwb->wb_background = (rwb->wb_max +3 ) /4 ; +if (rwb->queue_depth ==1 ) { + rwb->wb_max = rwb->wb_normal =2 ; + rwb->wb_background =1 ;This breaks the detection of too big
scale_stepinscale_up()where we key ofwb_max== 1 value. However even with that fixed no luck :(:Yeah, I need to look at that. For QD=1, I think the only sensible values for max/normal/bg is 2/2/1 and 1/1/1 if we step down.
Quoted 6 lines
Quoted 4 lines
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtime: 105.126 107.125 105.641So about the same as before. I'll try to debug this later today...
Thanks, I'm very interested in what you find!
OK, so the reason was relatively standard in the end. I was using ext3 (or more exactly ext4 without delayed allocation) for the test. The throttling of background writes gave more priority to writes from the journalling thread which happen with
WRITE_SYNCand thus are not throttled. Thus the journalling thread ended up having to do more data writeback to be able to commit a transaction (due to requirements of data=ordered mode) and it is less efficient at that than the normal flusher thread.So this is an example where throttling background writeback effectively just pushes more work into another context which does it less efficiently and indirectly makes everyone wait for it. ext3 has been always sensitive to issues like this. ext4 is using delayed allocation and thus only data writes into holes end up being part of a transaction -> simple dd test case doesn't hit that path. And indeed when I repeat the same test with ext4, the numbers with and without your patch are exactly the same.
Quoted 8 lines
The question remains how common a pattern where throttling of background writeback delays also something else is. I'll schedule a couple of benchmarks to measure impact of your patches for a wider range of workloads (but sadly pretty limited set of hw). If ext3 is the only one seeing issues, I would be willing to accept that ext3 takes the hit since it is doing something rather stupid (but inherent in its journal design) and we have a way to deal with this either by enabling delayed allocation or by turning off the writeback throttling...
At least in the case of io that we know is going to be data=ordered, we can bump the prio of those pages?
-chris
Jan Kara
maintainerQuoted 47 lines · Chris Mason
Quoted 37 lines · Jan Kara
Quoted 13 lines · Jens Axboe
Quoted 9 lines
lib/wbt.c static void calc_wb_limits(struct rq_wb *rwb) · line 166
Quoted 6 lines
lib/wbt.c +3
-3- rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); - rwb->wb_normal = (rwb->wb_max +1 ) /2 ; - rwb->wb_background = (rwb->wb_max +3 ) /4 ; +if (rwb->queue_depth ==1 ) { + rwb->wb_max = rwb->wb_normal =2 ; + rwb->wb_background =1 ;This breaks the detection of too big
scale_stepinscale_up()where we key ofwb_max== 1 value. However even with that fixed no luck :(:Yeah, I need to look at that. For QD=1, I think the only sensible values for max/normal/bg is 2/2/1 and 1/1/1 if we step down.
Quoted 6 lines
Quoted 4 lines
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtime: 105.126 107.125 105.641So about the same as before. I'll try to debug this later today...
Thanks, I'm very interested in what you find!
OK, so the reason was relatively standard in the end. I was using ext3 (or more exactly ext4 without delayed allocation) for the test. The throttling of background writes gave more priority to writes from the journalling thread which happen with
WRITE_SYNCand thus are not throttled. Thus the journalling thread ended up having to do more data writeback to be able to commit a transaction (due to requirements of data=ordered mode) and it is less efficient at that than the normal flusher thread.So this is an example where throttling background writeback effectively just pushes more work into another context which does it less efficiently and indirectly makes everyone wait for it. ext3 has been always sensitive to issues like this. ext4 is using delayed allocation and thus only data writes into holes end up being part of a transaction -> simple dd test case doesn't hit that path. And indeed when I repeat the same test with ext4, the numbers with and without your patch are exactly the same.
Quoted 8 lines
The question remains how common a pattern where throttling of background writeback delays also something else is. I'll schedule a couple of benchmarks to measure impact of your patches for a wider range of workloads (but sadly pretty limited set of hw). If ext3 is the only one seeing issues, I would be willing to accept that ext3 takes the hit since it is doing something rather stupid (but inherent in its journal design) and we have a way to deal with this either by enabling delayed allocation or by turning off the writeback throttling...
Quoted 2 lines
At least in the case of io that we know is going to be data=ordered, we can bump the prio of those pages?
But how would flusher thread, which is submitting IO, know that? We would have to somehow mark inodes that are part of the running transaction and flusher thread could give more priority to such writeback - e.g. by using WRITE_SYNC or at least plain writes. Hmm, if we use an inode flag for that, it could be doable.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 48 lines · Jan Kara
Quoted 47 lines · Chris Mason
Quoted 45 lines · Jan Kara
Quoted 13 lines · Jens Axboe
Quoted 9 lines
lib/wbt.c static void calc_wb_limits(struct rq_wb *rwb) · line 166
Quoted 6 lines
lib/wbt.c +3
-3- rwb->wb_max =1 + ((depth -1 ) >>min (31U , rwb->scale_step)); - rwb->wb_normal = (rwb->wb_max +1 ) /2 ; - rwb->wb_background = (rwb->wb_max +3 ) /4 ; +if (rwb->queue_depth ==1 ) { + rwb->wb_max = rwb->wb_normal =2 ; + rwb->wb_background =1 ;This breaks the detection of too big
scale_stepinscale_up()where we key ofwb_max== 1 value. However even with that fixed no luck :(:Yeah, I need to look at that. For QD=1, I think the only sensible values for max/normal/bg is 2/2/1 and 1/1/1 if we step down.
Quoted 6 lines
Quoted 4 lines
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtime: 105.126 107.125 105.641So about the same as before. I'll try to debug this later today...
Thanks, I'm very interested in what you find!
OK, so the reason was relatively standard in the end. I was using ext3 (or more exactly ext4 without delayed allocation) for the test. The throttling of background writes gave more priority to writes from the journalling thread which happen with
WRITE_SYNCand thus are not throttled. Thus the journalling thread ended up having to do more data writeback to be able to commit a transaction (due to requirements of data=ordered mode) and it is less efficient at that than the normal flusher thread.So this is an example where throttling background writeback effectively just pushes more work into another context which does it less efficiently and indirectly makes everyone wait for it. ext3 has been always sensitive to issues like this. ext4 is using delayed allocation and thus only data writes into holes end up being part of a transaction -> simple dd test case doesn't hit that path. And indeed when I repeat the same test with ext4, the numbers with and without your patch are exactly the same.
The question remains how common a pattern where throttling of background writeback delays also something else is. I'll schedule a couple of benchmarks to measure impact of your patches for a wider range of workloads (but sadly pretty limited set of hw). If ext3 is the only one seeing issues, I would be willing to accept that ext3 takes the hit since it is doing something rather stupid (but inherent in its journal design) and we have a way to deal with this either by enabling delayed allocation or by turning off the writeback throttling...
Quoted 8 lines
Quoted 2 lines
At least in the case of io that we know is going to be data=ordered, we can bump the prio of those pages?
But how would flusher thread, which is submitting IO, know that? We would have to somehow mark inodes that are part of the running transaction and flusher thread could give more priority to such writeback - e.g. by using
WRITE_SYNCor at least plain writes. Hmm, if we use an inode flag for that, it could be doable.
This would be specific to the data=ordered code in the FS. If there's some way to test for an inode or a page's status in the data=ordered list, the FS writepages call could flag the IO as higher prio?
-chris
Jan Kara
maintainerQuoted 58 lines · Chris Mason
Quoted 48 lines · Jan Kara
Quoted 47 lines · Chris Mason
Quoted 45 lines · Jan Kara
Quoted 13 lines · Jens Axboe
Quoted 9 lines
Review
Quoted 6 lines
(quoted diff) +3
-3- rwb->wb_max = 1 + ((depth - 1) >> min(31U, rwb->scale_step)); - rwb->wb_normal = (rwb->wb_max + 1) / 2; - rwb->wb_background = (rwb->wb_max + 3) / 4; + if (rwb->queue_depth == 1) { + rwb->wb_max = rwb->wb_normal = 2; + rwb->wb_background = 1;This breaks the detection of too big
scale_stepinscale_up()where we key ofwb_max== 1 value. However even with that fixed no luck :(:Yeah, I need to look at that. For QD=1, I think the only sensible values for max/normal/bg is 2/2/1 and 1/1/1 if we step down.
Quoted 6 lines
Quoted 4 lines
dd if=/dev/zero of=/mnt/file bs=1M count=10000 conv=fsync
Runtime: 105.126 107.125 105.641So about the same as before. I'll try to debug this later today...
Thanks, I'm very interested in what you find!
OK, so the reason was relatively standard in the end. I was using ext3 (or more exactly ext4 without delayed allocation) for the test. The throttling of background writes gave more priority to writes from the journalling thread which happen with
WRITE_SYNCand thus are not throttled. Thus the journalling thread ended up having to do more data writeback to be able to commit a transaction (due to requirements of data=ordered mode) and it is less efficient at that than the normal flusher thread.So this is an example where throttling background writeback effectively just pushes more work into another context which does it less efficiently and indirectly makes everyone wait for it. ext3 has been always sensitive to issues like this. ext4 is using delayed allocation and thus only data writes into holes end up being part of a transaction -> simple dd test case doesn't hit that path. And indeed when I repeat the same test with ext4, the numbers with and without your patch are exactly the same.
The question remains how common a pattern where throttling of background writeback delays also something else is. I'll schedule a couple of benchmarks to measure impact of your patches for a wider range of workloads (but sadly pretty limited set of hw). If ext3 is the only one seeing issues, I would be willing to accept that ext3 takes the hit since it is doing something rather stupid (but inherent in its journal design) and we have a way to deal with this either by enabling delayed allocation or by turning off the writeback throttling...
Quoted 8 lines
Quoted 2 lines
At least in the case of io that we know is going to be data=ordered, we can bump the prio of those pages?
But how would flusher thread, which is submitting IO, know that? We would have to somehow mark inodes that are part of the running transaction and flusher thread could give more priority to such writeback - e.g. by using
WRITE_SYNCor at least plain writes. Hmm, if we use an inode flag for that, it could be doable.
Quoted 3 lines
This would be specific to the data=ordered code in the FS. If there's some way to test for an inode or a page's status in the data=ordered list, the FS writepages call could flag the IO as higher prio?
Oh, right, we could do that. I can experiment with that later.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 7/8] wbt: add general throttling mechanism
In reply to Jan KaraQuoted 10 lines · Jan Kara
Quoted 9 lines · Jens Axboe
Quoted 8 lines
2) As far as I can see in patch 8/8, you have plugged the throttling above the IO scheduler. When there are e.g. multiple cgroups with different IO limits operating, this throttling can lead to strange results (like a cgroup with low limit using up all available background "slots" and thus effectively stopping background writeback for other cgroups)? So won't it make more sense to plug this below the IO scheduler? Now I understand there may be other problems with this but I think we should put more though to that and provide some justification in changelogs.
Quoted 7 lines
Quoted 3 lines
One complexity is that we have to do this early for blk-mq, since once you get a request, you're already sitting on the hw tag. CoDel should actually work fine at each hop, so hopefully this will as well.
OK, I see. But then this suggests that any IO scheduling and / or cgroup-related throttling should happen before we get a request for blk-mq as well? And then we can still do writeback throttling below that layer?
Not necessarily. For IO scheduling, basically we care about two parts:
1) Are you allowed to allocate the resources to queue some IO
2) Are you allowed to dispatch
The latter part can still be handled independently, and the former as well of course, wbt just deals with throttling back #1 for buffered writes.
Quoted 8 lines
Quoted 5 lines
But yes, fairness is something that we have to pay attention to. Right now the wait queue has no priority associated with it, that should probably be improved to be able to wakeup in a more appropriate order. Needs testing, but hopefully it works out since if you do run into starvation, then you'll go to the back of the queue for the next attempt.
Yeah, once I'll hunt down that regression with old disk, I can have a look into how writeback throttling plays together with blkio-controller.
Thanks!
Signature · 1 line
Jens Axboe
Jan Kara
maintainerRe: [PATCH 7/8] wbt: add general throttling mechanism
Quoted 15 lines · Jens Axboe
Quoted 14 lines · Jan Kara
Quoted 12 lines · Jens Axboe
Quoted 8 lines
2) As far as I can see in patch 8/8, you have plugged the throttling above the IO scheduler. When there are e.g. multiple cgroups with different IO limits operating, this throttling can lead to strange results (like a cgroup with low limit using up all available background "slots" and thus effectively stopping background writeback for other cgroups)? So won't it make more sense to plug this below the IO scheduler? Now I understand there may be other problems with this but I think we should put more though to that and provide some justification in changelogs.
One complexity is that we have to do this early for blk-mq, since once you get a request, you're already sitting on the hw tag. CoDel should actually work fine at each hop, so hopefully this will as well.
Quoted 8 lines
Quoted 3 lines
OK, I see. But then this suggests that any IO scheduling and / or cgroup-related throttling should happen before we get a request for blk-mq as well? And then we can still do writeback throttling below that layer?
Not necessarily. For IO scheduling, basically we care about two parts:
1) Are you allowed to allocate the resources to queue some IO
2) Are you allowed to dispatch
But then it seems suboptimal to waste a relatively scarce resource (which HW tag is AFAIU) just because you happen to run from a cgroup that is bandwidth limited and thus are not allowed to dispatch?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 7/8] wbt: add general throttling mechanism
Quoted 20 lines · Jan Kara
Quoted 19 lines · Jens Axboe
Quoted 17 lines · Jan Kara
Quoted 9 lines · Jens Axboe
Quoted 8 lines
2) As far as I can see in patch 8/8, you have plugged the throttling above the IO scheduler. When there are e.g. multiple cgroups with different IO limits operating, this throttling can lead to strange results (like a cgroup with low limit using up all available background "slots" and thus effectively stopping background writeback for other cgroups)? So won't it make more sense to plug this below the IO scheduler? Now I understand there may be other problems with this but I think we should put more though to that and provide some justification in changelogs.
Quoted 3 lines
One complexity is that we have to do this early for blk-mq, since once you get a request, you're already sitting on the hw tag. CoDel should actually work fine at each hop, so hopefully this will as well.
OK, I see. But then this suggests that any IO scheduling and / or cgroup-related throttling should happen before we get a request for blk-mq as well? And then we can still do writeback throttling below that layer?
Quoted 8 lines
Quoted 4 lines
Not necessarily. For IO scheduling, basically we care about two parts:
1) Are you allowed to allocate the resources to queue some IO
2) Are you allowed to dispatchBut then it seems suboptimal to waste a relatively scarce resource (which HW tag is AFAIU) just because you happen to run from a cgroup that is bandwidth limited and thus are not allowed to dispatch?
For some cases, you are absolutely right, and #1 is the main one. For your case of QD=1, that's obviously the case. For SATA, it's a bit more grey zone, and for others (nvme, scsi, etc), it's not really a scarce resource so #2 is the bigger part of it.
Signature · 1 line
Jens Axboe
Jan Kara
maintainerRe: [PATCH 7/8] wbt: add general throttling mechanism
In reply to Jan KaraQuoted 2 lines · Jan Kara
Yeah, once I'll hunt down that regression with old disk, I can have a look into how writeback throttling plays together with blkio-controller.
So I've tried the following script (note that you need cgroup v2 for writeback IO to be throttled):
---
mkdir /sys/fs/cgroup/group1
echo 1000 >/sys/fs/cgroup/group1/io.weight
dd if=/dev/zero of=/mnt/file1 bs=1M count=10000&
DD1=$!
echo $DD1 >/sys/fs/cgroup/group1/cgroup.procs
mkdir /sys/fs/cgroup/group2
echo 100 >/sys/fs/cgroup/group2/io.weight
#echo "259:65536 wbps=5000000" >/sys/fs/cgroup/group2/io.max echo "259:65536 wbps=max" >/sys/fs/cgroup/group2/io.max dd if=/dev/zero of=/mnt/file2 bs=1M count=10000& DD2=$!
echo $DD2 >/sys/fs/cgroup/group2/cgroup.procs
while true; do
sleep 1
kill -USR1 $DD1
kill -USR1 $DD2
echo '======================================================='done
---
and watched the progress of the dd processes in different cgroups. The 1/10 weight difference has no effect with your writeback patches - the situation after one minute:
3120+1 records in
3120+1 records out
3272392704 bytes (3.3 GB) copied, 63.7119 s, 51.4 MB/s 3217+1 records in
3217+1 records out
3374010368 bytes (3.4 GB) copied, 63.5819 s, 53.1 MB/s
I should add that even without your patches the progress doesn't quite correspond to the weight ratio:
...
but still there is noticeable difference to cgroups with different weights.
OTOH blk-throttle combines well with your patches: Limiting one cgroup to 5 M/s results in numbers like:
3883+2 records in
3883+2 records out
4072091648 bytes (4.1 GB) copied, 36.6713 s, 111 MB/s 413+0 records in
413+0 records out
433061888 bytes (433 MB) copied, 36.8939 s, 11.7 MB/s
which is fine and comparable with unpatched kernel. Higher throughput number is because we do buffered writes and dd reports what it wrote into page cache. And there is no wonder blk-throttle combines fine - it throttles bios which happens before we reach writeback throttling mechanism.
So I belive this demonstrates that your writeback throttling just doesn't work well with selective scheduling policy that happens below it because it can essentially lead to IO priority inversion issues...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
maintainerRe: [PATCH 7/8] wbt: add general throttling mechanism
Quoted 41 lines · Jan Kara
Quoted 2 lines · Jan Kara
Yeah, once I'll hunt down that regression with old disk, I can have a look into how writeback throttling plays together with blkio-controller.
So I've tried the following script (note that you need cgroup v2 for writeback IO to be throttled):
---
mkdir/sys/fs/cgroup/group1
echo 1000 >/sys/fs/cgroup/group1/io.weight
dd if=/dev/zero of=/mnt/file1 bs=1M count=10000&
DD1=$!
echo $DD1 >/sys/fs/cgroup/group1/cgroup.procsmkdir
/sys/fs/cgroup/group2
echo 100 >/sys/fs/cgroup/group2/io.weight
#echo "259:65536 wbps=5000000" >/sys/fs/cgroup/group2/io.max echo "259:65536 wbps=max" >/sys/fs/cgroup/group2/io.max dd if=/dev/zero of=/mnt/file2 bs=1M count=10000& DD2=$!
echo $DD2 >/sys/fs/cgroup/group2/cgroup.procswhile true; do
sleep 1 kill -USR1 $DD1 kill -USR1 $DD2 echo '======================================================='done
---and watched the progress of the dd processes in different cgroups. The 1/10 weight difference has no effect with your writeback patches - the situation after one minute:
3120+1 records in
3120+1 records out
3272392704 bytes (3.3 GB) copied, 63.7119 s, 51.4 MB/s 3217+1 records in
3217+1 records out
3374010368 bytes (3.4 GB) copied, 63.5819 s, 53.1 MB/s
Quoted 2 lines
I should add that even without your patches the progress doesn't quite correspond to the weight ratio:
Forgot to fill in corresponding data for unpatched kernel here:
5962+2 records in
5962+2 records out
6252281856 bytes (6.3 GB) copied, 64.1719 s, 97.4 MB/s 1502+0 records in
1502+0 records out
1574961152 bytes (1.6 GB) copied, 64.207 s, 24.5 MB/s
Quoted 26 lines
but still there is noticeable difference to cgroups with different weights.
OTOH blk-throttle combines well with your patches: Limiting one cgroup to 5 M/s results in numbers like:
3883+2 records in
3883+2 records out
4072091648 bytes (4.1 GB) copied, 36.6713 s, 111 MB/s 413+0 records in
413+0 records out
433061888 bytes (433 MB) copied, 36.8939 s, 11.7 MB/swhich is fine and comparable with unpatched kernel. Higher throughput number is because we do buffered writes and dd reports what it wrote into page cache. And there is no wonder blk-throttle combines fine - it throttles bios which happens before we reach writeback throttling mechanism.
So I belive this demonstrates that your writeback throttling just doesn't work well with selective scheduling policy that happens below it because it can essentially lead to IO priority inversion issues...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jens Axboe
maintainerRe: [PATCH 7/8] wbt: add general throttling mechanism
Quoted 45 lines · Jan Kara
Quoted 41 lines · Jan Kara
Quoted 2 lines · Jan Kara
Yeah, once I'll hunt down that regression with old disk, I can have a look into how writeback throttling plays together with blkio-controller.
So I've tried the following script (note that you need cgroup v2 for writeback IO to be throttled):
---
mkdir/sys/fs/cgroup/group1
echo 1000 >/sys/fs/cgroup/group1/io.weight
dd if=/dev/zero of=/mnt/file1 bs=1M count=10000&
DD1=$!
echo $DD1 >/sys/fs/cgroup/group1/cgroup.procsmkdir
/sys/fs/cgroup/group2
echo 100 >/sys/fs/cgroup/group2/io.weight
#echo "259:65536 wbps=5000000" >/sys/fs/cgroup/group2/io.max echo "259:65536 wbps=max" >/sys/fs/cgroup/group2/io.max dd if=/dev/zero of=/mnt/file2 bs=1M count=10000& DD2=$!
echo $DD2 >/sys/fs/cgroup/group2/cgroup.procswhile true; do
sleep 1 kill -USR1 $DD1 kill -USR1 $DD2 echo '======================================================='done
---and watched the progress of the dd processes in different cgroups. The 1/10 weight difference has no effect with your writeback patches - the situation after one minute:
3120+1 records in
3120+1 records out
3272392704 bytes (3.3 GB) copied, 63.7119 s, 51.4 MB/s 3217+1 records in
3217+1 records out
3374010368 bytes (3.4 GB) copied, 63.5819 s, 53.1 MB/sQuoted 2 lines
I should add that even without your patches the progress doesn't quite correspond to the weight ratio:
Quoted 8 lines
Forgot to fill in corresponding data for unpatched kernel here:
5962+2 records in
5962+2 records out
6252281856 bytes (6.3 GB) copied, 64.1719 s, 97.4 MB/s 1502+0 records in
1502+0 records out
1574961152 bytes (1.6 GB) copied, 64.207 s, 24.5 MB/s
Thanks for testing this, I'll see what we can do about that. It stands to reason that we'll throttle a heavier writer more, statistically. But I'm assuming this above test was run basically with just the writes going, so no real competition? And hence we end up throttling them equally much, destroying the weighting in the process. But for both cases, we basically don't pay any attention to cgroup weights.
Quoted 12 lines
Quoted 12 lines
but still there is noticeable difference to cgroups with different weights.
OTOH blk-throttle combines well with your patches: Limiting one cgroup to 5 M/s results in numbers like:
3883+2 records in
3883+2 records out
4072091648 bytes (4.1 GB) copied, 36.6713 s, 111 MB/s 413+0 records in
413+0 records out
433061888 bytes (433 MB) copied, 36.8939 s, 11.7 MB/s
Quoted 5 lines
Quoted 5 lines
which is fine and comparable with unpatched kernel. Higher throughput number is because we do buffered writes and dd reports what it wrote into page cache. And there is no wonder blk-throttle combines fine - it throttles bios which happens before we reach writeback throttling mechanism.
OK, that's good, at least that part works fine. And yes, the throttle path is hit before we end up in the make_request_fn, which is where wbt drops in.
Quoted 3 lines
Quoted 3 lines
So I belive this demonstrates that your writeback throttling just doesn't work well with selective scheduling policy that happens below it because it can essentially lead to IO priority inversion issues...
It this testing still done on the QD=1 ATA disk? Not too surprising that this falls apart, since we have very little room to maneuver. I wonder if a normal SATA with NCQ would behave better in this regard. I'll have to test a bit and think about how we can best handle this case.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerRe: [PATCH 7/8] wbt: add general throttling mechanism
Quoted 96 lines · Jens Axboe
Quoted 48 lines · Jan Kara
Quoted 44 lines · Jan Kara
Quoted 3 lines · Jan Kara
Yeah, once I'll hunt down that regression with old disk, I can have a look
into how writeback throttling plays together with blkio-controller.So I've tried the following script (note that you need cgroup v2 for writeback IO to be throttled):
---
mkdir/sys/fs/cgroup/group1
echo 1000 >/sys/fs/cgroup/group1/io.weight
dd if=/dev/zero of=/mnt/file1 bs=1M count=10000&
DD1=$!
echo $DD1 >/sys/fs/cgroup/group1/cgroup.procsmkdir
/sys/fs/cgroup/group2
echo 100 >/sys/fs/cgroup/group2/io.weight
#echo "259:65536 wbps=5000000" >/sys/fs/cgroup/group2/io.max echo "259:65536 wbps=max" >/sys/fs/cgroup/group2/io.max dd if=/dev/zero of=/mnt/file2 bs=1M count=10000& DD2=$!
echo $DD2 >/sys/fs/cgroup/group2/cgroup.procswhile true; do
sleep 1 kill -USR1 $DD1 kill -USR1 $DD2 echo '======================================================='done
---and watched the progress of the dd processes in different cgroups. The 1/10
weight difference has no effect with your writeback patches - the situation
after one minute:3120+1 records in
3120+1 records out
3272392704 bytes (3.3 GB) copied, 63.7119 s, 51.4 MB/s 3217+1 records in
3217+1 records out
3374010368 bytes (3.4 GB) copied, 63.5819 s, 53.1 MB/sQuoted 2 lines
I should add that even without your patches the progress doesn't quite correspond to the weight ratio:
Quoted 8 lines
Forgot to fill in corresponding data for unpatched kernel here:
5962+2 records in
5962+2 records out
6252281856 bytes (6.3 GB) copied, 64.1719 s, 97.4 MB/s 1502+0 records in
1502+0 records out
1574961152 bytes (1.6 GB) copied, 64.207 s, 24.5 MB/sThanks for testing this, I'll see what we can do about that. It stands to reason that we'll throttle a heavier writer more, statistically. But I'm assuming this above test was run basically with just the writes going, so no real competition? And hence we end up throttling them equally much, destroying the weighting in the process. But for both cases, we basically don't pay any attention to cgroup weights.
Quoted 14 lines
Quoted 14 lines
but still there is noticeable difference to cgroups with different weights.
OTOH blk-throttle combines well with your patches: Limiting one cgroup to
5 M/s results in numbers like:3883+2 records in
3883+2 records out
4072091648 bytes (4.1 GB) copied, 36.6713 s, 111 MB/s 413+0 records in
413+0 records out
433061888 bytes (433 MB) copied, 36.8939 s, 11.7 MB/sQuoted 6 lines
Quoted 6 lines
which is fine and comparable with unpatched kernel. Higher throughput number is because we do buffered writes and dd reports what it wrote into
page cache. And there is no wonder blk-throttle combines fine - it throttles bios which happens before we reach writeback throttling mechanism.OK, that's good, at least that part works fine. And yes, the throttle path is hit before we end up in the
make_request_fn, which is where wbt drops in.Quoted 5 lines
Quoted 5 lines
So I belive this demonstrates that your writeback throttling just doesn't
work well with selective scheduling policy that happens below it because it
can essentially lead to IO priority inversion issues...
Quoted 4 lines
It this testing still done on the QD=1 ATA disk? Not too surprising that this falls apart, since we have very little room to maneuver. I wonder if a normal SATA with NCQ would behave better in this regard. I'll have to test a bit and think about how we can best handle this case.
I think what we'll do for now is just disable wbt IFF we have a non-root cgroup attached to CFQ. Done here:
We don't have a strong need for wbt (supposedly) since CFQ should take care of most of it, if you have policies set for proportional sharing.
Longer term it's not a concern either, as we'll move away from that model anyway.
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerRe: [PATCH 7/8] wbt: add general throttling mechanism
Quoted 103 lines · Jens Axboe
Quoted 97 lines · Jens Axboe
Quoted 49 lines · Jan Kara
Quoted 45 lines · Jan Kara
Quoted 3 lines · Jan Kara
Yeah, once I'll hunt down that regression with old disk, I can have a look
into how writeback throttling plays together with blkio-controller.So I've tried the following script (note that you need cgroup v2 for writeback IO to be throttled):
---
mkdir/sys/fs/cgroup/group1
echo 1000 >/sys/fs/cgroup/group1/io.weight
dd if=/dev/zero of=/mnt/file1 bs=1M count=10000&
DD1=$!
echo $DD1 >/sys/fs/cgroup/group1/cgroup.procsmkdir
/sys/fs/cgroup/group2
echo 100 >/sys/fs/cgroup/group2/io.weight
#echo "259:65536 wbps=5000000" >/sys/fs/cgroup/group2/io.max echo "259:65536 wbps=max" >/sys/fs/cgroup/group2/io.max dd if=/dev/zero of=/mnt/file2 bs=1M count=10000& DD2=$!
echo $DD2 >/sys/fs/cgroup/group2/cgroup.procswhile true; do
sleep 1 kill -USR1 $DD1 kill -USR1 $DD2 echo'=======================================================' done
---and watched the progress of the dd processes in different cgroups. The 1/10
weight difference has no effect with your writeback patches - the situation
after one minute:3120+1 records in
3120+1 records out
3272392704 bytes (3.3 GB) copied, 63.7119 s, 51.4 MB/s 3217+1 records in
3217+1 records out
3374010368 bytes (3.4 GB) copied, 63.5819 s, 53.1 MB/sQuoted 2 lines
I should add that even without your patches the progress doesn't quite correspond to the weight ratio:
Quoted 8 lines
Forgot to fill in corresponding data for unpatched kernel here:
5962+2 records in
5962+2 records out
6252281856 bytes (6.3 GB) copied, 64.1719 s, 97.4 MB/s 1502+0 records in
1502+0 records out
1574961152 bytes (1.6 GB) copied, 64.207 s, 24.5 MB/sThanks for testing this, I'll see what we can do about that. It stands to reason that we'll throttle a heavier writer more, statistically. But I'm assuming this above test was run basically with just the writes going, so no real competition? And hence we end up throttling them equally much, destroying the weighting in the process. But for both cases, we basically don't pay any attention to cgroup weights.
Quoted 14 lines
Quoted 14 lines
but still there is noticeable difference to cgroups with different weights.
OTOH blk-throttle combines well with your patches: Limiting one cgroup to
5 M/s results in numbers like:3883+2 records in
3883+2 records out
4072091648 bytes (4.1 GB) copied, 36.6713 s, 111 MB/s 413+0 records in
413+0 records out
433061888 bytes (433 MB) copied, 36.8939 s, 11.7 MB/sQuoted 6 lines
Quoted 6 lines
which is fine and comparable with unpatched kernel. Higher throughput number is because we do buffered writes and dd reports what it wrote into
page cache. And there is no wonder blk-throttle combines fine - it throttles bios which happens before we reach writeback throttling mechanism.OK, that's good, at least that part works fine. And yes, the throttle path is hit before we end up in the
make_request_fn, which is where wbt drops in.Quoted 5 lines
Quoted 5 lines
So I belive this demonstrates that your writeback throttling just doesn't
work well with selective scheduling policy that happens below it because it
can essentially lead to IO priority inversion issues...Quoted 4 lines
It this testing still done on the QD=1 ATA disk? Not too surprising that this falls apart, since we have very little room to maneuver. I wonder if a normal SATA with NCQ would behave better in this regard. I'll have to test a bit and think about how we can best handle this case.
Quoted 4 lines
I think what we'll do for now is just disable wbt IFF we have a non-root cgroup attached to CFQ. Done here:
That was a bit too untested.. This should be better, it taps into where cfq normally notices a difference in blkcg:
Signature · 1 line
Jens Axboe
Re: [PATCH 6/8] block: add scalable completion tracking of requests
In reply to Jens AxboeQuoted 52 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.
Signed-off-by Jens Axboe <[email protected]>--- 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 | 184 ++++++++++++++++++++++++++++++++++++++++++++++ block/blk-stat.h | 17 +++++ block/blk-sysfs.c | 26 +++++++ include/linux/blk_types.h | 8 ++ include/linux/blkdev.h | 4 + 10 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 block/blk-stat.c create mode 100644 block/blk-stat.hblock/Makefile +1
-1@@ -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-cpu.o blk-mq-cpumap.o ioctl.o \ genhd.o scsi_ioctl.o partition-generic.o ioprio.o \ badblocks.o partitions/block/blk-core.c +2
-0@@ -2514,6 +2514,8 @@ void blk_start_request(struct request *req) {blk_dequeue_request (req); + req->issue_time =ktime_to_ns (ktime_get ()); +/* * We are now handing the request to the hardware, initialize * resid_len to full count and add the timeout handler.
block/blk-core.c bool blk_update_request(struct request *req, int error, unsigned int nr_bytes) · line 2586
Quoted 4 lines
block/blk-core.c +1
-0@@ -2581,6 +2583,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);
blk_update_request() is often run lockless, so it might be a problem to add into queue's status here in case of non-blk-mq. Maybe it is better to do it in blk_finish_request()?
For blk-mq, blk_stat_add() should be avoided here.
Quoted 433 lines
block/blk-core.c +1
-0+if (!req->bio)return false;block/blk-mq-sysfs.c +47
-0@@ -247,6 +247,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,@@ -304,6 +345,11 @@ static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_poll = { .attr = {.name = "io_poll", .mode = S_IRUGO }, .show = blk_mq_hw_sysfs_poll_show, }; +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,@@ -314,6 +360,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@@ -29,6 +29,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);@@ -356,10 +357,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 @@ -403,6 +413,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); + rq->issue_time =ktime_to_ns (ktime_get ()); +blk_add_timer (rq);/* @@ -1761,6 +1773,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@@ -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 {@@ -20,6 +22,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 +184
-0@@ -0,0 +1,184 @@ +/* +* 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" + +void blk_stat_sum (struct blk_rq_stat *dst,struct blk_rq_stat *src) +{ +if (!src->nr_samples) +return ; + + 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; +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 ; + +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); +} + +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->time = time_now &BLK_STAT_MASK ; +} + +void blk_stat_init (struct blk_rq_stat *stat) +{ +__blk_stat_init (stat,ktime_to_ns (ktime_get ())); +} + +void blk_stat_add (struct blk_rq_stat *stat,struct request *rq) +{ +s64 delta, now, value; + + now =ktime_to_ns (ktime_get ()); +if (now < rq->issue_time) +return ; + +if ((now &BLK_STAT_MASK ) != (stat->time &BLK_STAT_MASK )) +__blk_stat_init (stat, now); + + value = now - rq->issue_time; +if (value > stat->max) + stat->max = value; +if (value < stat->min) + stat->min = value; + + delta = value - stat->mean; +if (delta) + stat->mean +=div64_s64 (delta, stat->nr_samples +1 ); + + stat->nr_samples++; +} + +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 ]); + } +}block/blk-stat.h new file +17
-0@@ -0,0 +1,17 @@ +#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_MASK ~(BLK_STAT_NSEC - 1) + +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 *); + +#endif block/blk-sysfs.c +26
-0@@ -379,6 +379,26 @@ static ssize_t queue_wc_store(struct request_queue *q, const char *page,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 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,@@ -516,6 +536,11 @@ static struct queue_sysfs_entry queue_wc_entry = { .store = queue_wc_store, }; +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,@@ -542,6 +567,7 @@ static struct attribute *default_attrs[] = { &queue_random_entry.attr, &queue_poll_entry.attr, &queue_wc_entry.attr, + &queue_stats_entry.attr, NULL, };include/linux/blk_types.h +8
-0@@ -268,4 +268,12 @@ static inline unsigned int blk_qc_t_to_tag(blk_qc_t cookie) return cookie & ((1u << BLK_QC_T_SHIFT) - 1); } +struct blk_rq_stat { +s64 mean; +u64 min; +u64 max; +s64 nr_samples; +s64 time; +}; +#endif /* __LINUX_BLK_TYPES_H */ include/linux/blkdev.h +0
-0
include/linux/blkdev.h struct request { · line 156
Quoted 4 lines
include/linux/blkdev.h +1
-0@@ -153,6 +153,7 @@ struct request {struct gendisk *rq_disk;struct hd_struct *part;unsigned long start_time; +s64 issue_time;
io_start_time_ns may be reused for same purpose.
Quoted 20 lines
include/linux/blkdev.h +3
-1#ifdef CONFIG_BLK_CGROUP struct request_list *rl;/* rl this rq is alloced from */ unsigned long long start_time_ns;@@ -402,6 +403,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 -- 2.8.0.rc4.6.g7e4ba36
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 1 line
Ming Lei
Jan Kara
maintainerQuoted 8 lines · Jan Kara
The question remains how common a pattern where throttling of background writeback delays also something else is. I'll schedule a couple of benchmarks to measure impact of your patches for a wider range of workloads (but sadly pretty limited set of hw). If ext3 is the only one seeing issues, I would be willing to accept that ext3 takes the hit since it is doing something rather stupid (but inherent in its journal design) and we have a way to deal with this either by enabling delayed allocation or by turning off the writeback throttling...
So I've run some benchmarks on a machine with 6 GB of RAM and SSD with queue depth 32. The filesystem on the disk was XFS this time. I've found couple of regressions. A clear one is with dbench (version 4). The average throughput numbers look like:
Baseline WBT
Hmean mb/sec-1 30.26 ( 0.00%) 18.67 (-38.28%) Hmean mb/sec-2 40.71 ( 0.00%) 31.25 (-23.23%) Hmean mb/sec-4 52.67 ( 0.00%) 46.83 (-11.09%) Hmean mb/sec-8 69.51 ( 0.00%) 64.35 ( -7.42%) Hmean mb/sec-16 91.07 ( 0.00%) 86.46 ( -5.07%) Hmean mb/sec-32 115.10 ( 0.00%) 110.29 ( -4.18%) Hmean mb/sec-64 145.14 ( 0.00%) 134.97 ( -7.00%) Hmean mb/sec-512 93.99 ( 0.00%) 133.85 ( 42.41%)
There were also some losses in a filebench webproxy workload (I can give you exact details of the settings if you want to reproduce it).
Also, and this really puzzles me, I've seen higher read latencies in some cases (I've verified they are not just noise by rerunning the test for kernel with writeback throttling patches). For example with the following fio job file:
[global]
direct=0
ioengine=sync
runtime=300time_based
invalidate=1
blocksize=4096
size=10g # Just random value, we are running time based workload
log_avg_msec=10
group_reporting=1
[writer]
nrfiles=1
filesize=1g
fdatasync=256
readwrite=randwrite
numjobs=4
[reader]
# Simulate random reading from different files, switching to different file
# after 16 ios. This somewhat simulates application startup. new_group filesize=100m nrfiles=20 file_service_type=random:16 readwrite=randread
I get the following results:
Throughput Baseline WBT
Hmean kb/sec-writer-write 591.60 ( 0.00%) 507.00 (-14.30%) Hmean kb/sec-reader-read 211.81 ( 0.00%) 137.53 (-35.07%)
So both read and write throughput have suffered. And latencies don't offset for the loss either:
FIO read latency
Min latency-read 1383.00 ( 0.00%) 1519.00 ( -9.83%) 1st-qrtle latency-read 3485.00 ( 0.00%) 5235.00 (-50.22%) 2nd-qrtle latency-read 4708.00 ( 0.00%) 15028.00 (-219.20%) 3rd-qrtle latency-read 10286.00 ( 0.00%) 57622.00 (-460.20%) Max-90% latency-read 195834.00 ( 0.00%) 167149.00 ( 14.65%) Max-93% latency-read 273145.00 ( 0.00%) 200319.00 ( 26.66%) Max-95% latency-read 335434.00 ( 0.00%) 220695.00 ( 34.21%) Max-99% latency-read 537017.00 ( 0.00%) 347174.00 ( 35.35%) Max latency-read 991101.00 ( 0.00%) 485835.00 ( 50.98%) Mean latency-read 51282.79 ( 0.00%) 49953.95 ( 2.59%)
So we have reduced the extra high read latencies which is nice but on average there is no change.
And another fio jobfile which doesn't look great:
[global]
direct=0
ioengine=sync
runtime=300
blocksize=4096
invalidate=1time_based
ramp_time=5 # Let the flusher thread start before taking measurements
log_avg_msec=10
group_reporting=1
[writer]
nrfiles=1
filesize=$((MEMTOTAL_BYTES*2))
readwrite=randwrite
[reader]
# Simulate random reading from different files, switching to different file
# after 16 ios. This somewhat simulates application startup. new_group filesize=100m nrfiles=20 file_service_type=random:16 readwrite=randread
The throughput numbers look like:
Hmean kb/sec-writer-write 24707.22 ( 0.00%) 19912.23 (-19.41%) Hmean kb/sec-reader-read 886.65 ( 0.00%) 905.71 ( 2.15%)
So we've got significant hit in writes not really offset by a big increase in reads. Read latency numbers look like (I show the WBT numbers for two runs just so that one can see how variable the latency numbers are because I was puzzled by very high max latency for WBT kernels - quartiles seem rather stable higher percentiles and min/max are rather variable):
Baseline WBT WBT Min latency-read 1230.00 ( 0.00%) 1560.00 (-26.83%) 1100.00 ( 10.57%) 1st-qrtle latency-read 3357.00 ( 0.00%) 3351.00 ( 0.18%) 3351.00 ( 0.18%) 2nd-qrtle latency-read 4074.00 ( 0.00%) 4056.00 ( 0.44%) 4022.00 ( 1.28%) 3rd-qrtle latency-read 5198.00 ( 0.00%) 5145.00 ( 1.02%) 5095.00 ( 1.98%) Max-90% latency-read 6594.00 ( 0.00%) 6370.00 ( 3.40%) 6130.00 ( 7.04%) Max-93% latency-read 11251.00 ( 0.00%) 9410.00 ( 16.36%) 6654.00 ( 40.86%) Max-95% latency-read 14769.00 ( 0.00%) 13231.00 ( 10.41%) 10306.00 ( 30.22%) Max-99% latency-read 27826.00 ( 0.00%) 28728.00 ( -3.24%) 25077.00 ( 9.88%) Max latency-read 80202.00 ( 0.00%) 186491.00 (-132.53%) 141346.00 (-76.24%) Mean latency-read 5356.12 ( 0.00%) 5229.00 ( 2.37%) 4927.23 ( 8.01%)
I have run also other tests but they have mostly shown no significant difference.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jens Axboe
maintainerQuoted 133 lines · Jan Kara
Quoted 8 lines · Jan Kara
The question remains how common a pattern where throttling of background writeback delays also something else is. I'll schedule a couple of benchmarks to measure impact of your patches for a wider range of workloads (but sadly pretty limited set of hw). If ext3 is the only one seeing issues, I would be willing to accept that ext3 takes the hit since it is doing something rather stupid (but inherent in its journal design) and we have a way to deal with this either by enabling delayed allocation or by turning off the writeback throttling...
So I've run some benchmarks on a machine with 6 GB of RAM and SSD with queue depth 32. The filesystem on the disk was XFS this time. I've found couple of regressions. A clear one is with dbench (version 4). The average throughput numbers look like:
Baseline WBT
Hmean mb/sec-1 30.26 ( 0.00%) 18.67 (-38.28%) Hmean mb/sec-2 40.71 ( 0.00%) 31.25 (-23.23%) Hmean mb/sec-4 52.67 ( 0.00%) 46.83 (-11.09%) Hmean mb/sec-8 69.51 ( 0.00%) 64.35 ( -7.42%) Hmean mb/sec-16 91.07 ( 0.00%) 86.46 ( -5.07%) Hmean mb/sec-32 115.10 ( 0.00%) 110.29 ( -4.18%) Hmean mb/sec-64 145.14 ( 0.00%) 134.97 ( -7.00%) Hmean mb/sec-512 93.99 ( 0.00%) 133.85 ( 42.41%)There were also some losses in a filebench webproxy workload (I can give you exact details of the settings if you want to reproduce it).
Also, and this really puzzles me, I've seen higher read latencies in some cases (I've verified they are not just noise by rerunning the test for kernel with writeback throttling patches). For example with the following fio job file:
[global]
direct=0
ioengine=sync
runtime=300time_based
invalidate=1
blocksize=4096
size=10g # Just random value, we are running time based workload
log_avg_msec=10
group_reporting=1[writer]
nrfiles=1
filesize=1g
fdatasync=256
readwrite=randwrite
numjobs=4[reader]
# Simulate random reading from different files, switching to different file# after 16 ios. This somewhat simulates application startup. new_group filesize=100m nrfiles=20 file_service_type=random:16 readwrite=randreadI get the following results:
Throughput Baseline WBT
Hmean kb/sec-writer-write 591.60 ( 0.00%) 507.00 (-14.30%) Hmean kb/sec-reader-read 211.81 ( 0.00%) 137.53 (-35.07%)So both read and write throughput have suffered. And latencies don't offset for the loss either:
FIO read latency
Min latency-read 1383.00 ( 0.00%) 1519.00 ( -9.83%) 1st-qrtle latency-read 3485.00 ( 0.00%) 5235.00 (-50.22%) 2nd-qrtle latency-read 4708.00 ( 0.00%) 15028.00 (-219.20%) 3rd-qrtle latency-read 10286.00 ( 0.00%) 57622.00 (-460.20%) Max-90% latency-read 195834.00 ( 0.00%) 167149.00 ( 14.65%) Max-93% latency-read 273145.00 ( 0.00%) 200319.00 ( 26.66%) Max-95% latency-read 335434.00 ( 0.00%) 220695.00 ( 34.21%) Max-99% latency-read 537017.00 ( 0.00%) 347174.00 ( 35.35%) Max latency-read 991101.00 ( 0.00%) 485835.00 ( 50.98%) Mean latency-read 51282.79 ( 0.00%) 49953.95 ( 2.59%)So we have reduced the extra high read latencies which is nice but on average there is no change.
And another fio jobfile which doesn't look great:
[global]
direct=0
ioengine=sync
runtime=300
blocksize=4096
invalidate=1time_based
ramp_time=5 # Let the flusher thread start before taking measurements
log_avg_msec=10
group_reporting=1[writer]
nrfiles=1
filesize=$((MEMTOTAL_BYTES*2))
readwrite=randwrite[reader]
# Simulate random reading from different files, switching to different file# after 16 ios. This somewhat simulates application startup. new_group filesize=100m nrfiles=20 file_service_type=random:16 readwrite=randreadThe throughput numbers look like:
Hmean kb/sec-writer-write 24707.22 ( 0.00%) 19912.23 (-19.41%) Hmean kb/sec-reader-read 886.65 ( 0.00%) 905.71 ( 2.15%)So we've got significant hit in writes not really offset by a big increase in reads. Read latency numbers look like (I show the WBT numbers for two runs just so that one can see how variable the latency numbers are because I was puzzled by very high max latency for WBT kernels - quartiles seem rather stable higher percentiles and min/max are rather variable):
Baseline WBT WBT Min latency-read 1230.00 ( 0.00%) 1560.00 (-26.83%) 1100.00 ( 10.57%) 1st-qrtle latency-read 3357.00 ( 0.00%) 3351.00 ( 0.18%) 3351.00 ( 0.18%) 2nd-qrtle latency-read 4074.00 ( 0.00%) 4056.00 ( 0.44%) 4022.00 ( 1.28%) 3rd-qrtle latency-read 5198.00 ( 0.00%) 5145.00 ( 1.02%) 5095.00 ( 1.98%) Max-90% latency-read 6594.00 ( 0.00%) 6370.00 ( 3.40%) 6130.00 ( 7.04%) Max-93% latency-read 11251.00 ( 0.00%) 9410.00 ( 16.36%) 6654.00 ( 40.86%) Max-95% latency-read 14769.00 ( 0.00%) 13231.00 ( 10.41%) 10306.00 ( 30.22%) Max-99% latency-read 27826.00 ( 0.00%) 28728.00 ( -3.24%) 25077.00 ( 9.88%) Max latency-read 80202.00 ( 0.00%) 186491.00 (-132.53%) 141346.00 (-76.24%) Mean latency-read 5356.12 ( 0.00%) 5229.00 ( 2.37%) 4927.23 ( 8.01%)
Quoted 2 lines
I have run also other tests but they have mostly shown no significant difference.
Thanks Jan, this is great and super useful! I'm revamping certain parts of it to deal with write back caching better, and I'll take a look at the regressions that you reported.
What kind of SSD is this? I'm assuming it's SATA (QD=32), and then it would probably be a safe assumption that it's flagging itself as having a volatile write back cache, would that be a correct assumption?
Are you using scsi-mq, or do you have an IO scheduler attached to it?
Signature · 1 line
Jens Axboe
Jan Kara
maintainerQuoted 7 lines · Jens Axboe
Thanks Jan, this is great and super useful! I'm revamping certain parts of it to deal with write back caching better, and I'll take a look at the regressions that you reported.
What kind of SSD is this? I'm assuming it's SATA (QD=32), and then it would probably be a safe assumption that it's flagging itself as having a volatile write back cache, would that be a correct assumption?
Yes, it is SATA with writeback cache.
Quoted 1 line
Are you using scsi-mq, or do you have an IO scheduler attached to it?
The disk was using IO scheduler, however at this point I'm not 100% sure which scheduler (deadline or cfq) was the default one for the distro that was installed. The machine is currently testing something else so I cannot reinstall it and check. Maybe I can rerun some tests later in the week when the machine gets freed with scsi-mq or deadline IO scheduler so that we have 100% certain config.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR