Priorities for I/O
Subsystems (2)
Hi Jens,
one of the shortcomings of Linux process priority system is that it only affects CPU resources (and even those are not affected strongly enough for some applications).
As soon as processes waits for I/O, they are all equal.
I wonder how difficult it was to just add priorities to your I/O scheduler. Basically, I think everything is there: You sort requests already and we have deadlines. For sorting scores are used.
So the idea is: Why not just give I/O submitted on behalf of -19 processes (and RT) some higher score and a shorter deadline than +19 ones? Probably, it would only affect reads, as there we have the processes wait on them; writes are often triggered asynchronously anyway.
This should have the effects that we want: When there's no fight for I/O bandwidth, everybody just gets maximum performance as the I/O scheduler's queue will be short. As soon as processes fight for reads, unniced processes have a higher chance of getting served first.
Just think of nightly updatedb on a webserver for a real-world example why this may matter.
Looks like something not too difficult to do, but my current knowledge on 2.5 code is somewhat sparse :-(
Regards,
Signature · 3 lines
Kurt Garloff <[email protected]> Eindhoven, NL GPG key: See mail header, key servers SuSE Labs SuSE Linux AG, Nuernberg, DE SCSI, Security
Quoted 6 lines · Kurt Garloff
Hi Jens,
one of the shortcomings of Linux process priority system is that it only affects CPU resources (and even those are not affected strongly enough for some applications).
As soon as processes waits for I/O, they are all equal.
I agree that it's a feature that would be nice to have, but I also don't think it's that important.
Quoted 7 lines
I wonder how difficult it was to just add priorities to your I/O scheduler. Basically, I think everything is there: You sort requests already and we have deadlines. For sorting scores are used.
So the idea is: Why not just give I/O submitted on behalf of -19 processes (and RT) some higher score and a shorter deadline than +19 ones? Probably, it would only affect reads, as there we have the processes wait on them; writes are often triggered asynchronously anyway.
You are missing one little detail -- yes there is an expire list for reads, and it is indeed sorted by deadline. Right now this operates in O(1) time for insert and retrieval, since it's a plain FIFO (well sometimes we move entries from the middle of the list as well).
So if you want to starting basing deadlines on process priority, you would have to sort insert instead.
It's not a big issue, but cycles spent in the io scheduler was very much considered when I wrote it (it's faster than the old one). And we could always just replace the simple doubly linked list with something else. Back in the early days of bio, I actually had the sorting changed to a binomial heap. Given that both the read+write sort lists and expire lists are just priority queues now, it would work easily. Anyways, getting off track...
Quoted 10 lines
This should have the effects that we want: When there's no fight for I/O bandwidth, everybody just gets maximum performance as the I/O scheduler's queue will be short. As soon as processes fight for reads, unniced processes have a higher chance of getting served first.
Just think of nightly updatedb on a webserver for a real-world example why this may matter.
Looks like something not too difficult to do, but my current knowledge on 2.5 code is somewhat sparse :-(
If you want to give it a try, it's pretty easy to add. dd->read_fifo is our fifo expire list right now, you'd want to change that to dd->read_prio or something. And in deadline_add_request(), the very last thing we do is add the read to the tail of the expire list:
drq->expires = jiffies + dd->read_expire; list_add_tail(&drq->fifo, &dd->read_fifo);
you'd just want something ala
struct list_head *foo = &dd->read_prio;
struct deadline_rq *__drq;
drq->expires = jiffies + deadline_process_expire();
while ((foo = foo->prev) != &dd->read_prio) {
__drq = list_entry_fifo(foo);
if (__drq->expires > drq->expires)
break;
}
list_add(&drq->fifo, foo);you get the picture. And that should be it, really. Everything else should just work as-is, iirc.
Signature · 1 line
Jens Axboe
I've worked on this a well.
Here is a patch that does what you want.
I don't know if it applies cleanly to the latest 2.5. I worked fine around 2.5.40. I havent' given the patch the extensive testing that is needed but it works for me ;)
On Mon, Oct 21, 2002 at 10:24:29AM +0200, Jens Axboe wrote:
|> On Mon, Oct 21 2002, Kurt Garloff wrote:
|> > Hi Jens,
|> >
|> > one of the shortcomings of Linux process priority system is that it only
|> > affects CPU resources (and even those are not affected strongly enough for
|> > some applications).
|> > As soon as processes waits for I/O, they are all equal.
|>
|> I agree that it's a feature that would be nice to have, but I also don't
|> think it's that important.
|>
|> > I wonder how difficult it was to just add priorities to your I/O scheduler.
|> > Basically, I think everything is there: You sort requests already and we
|> > have deadlines. For sorting scores are used.
|> > So the idea is: Why not just give I/O submitted on behalf of -19 processes
|> > (and RT) some higher score and a shorter deadline than +19 ones?
|> > Probably, it would only affect reads, as there we have the processes wait
|> > on them; writes are often triggered asynchronously anyway.
|>
|> You are missing one little detail -- yes there is an expire list for
|> reads, and it is indeed sorted by deadline. Right now this operates in
|> O(1) time for insert and retrieval, since it's a plain FIFO (well
|> sometimes we move entries from the middle of the list as well).
|>
|> So if you want to starting basing deadlines on process priority, you
|> would have to sort insert instead.
|>
|> It's not a big issue, but cycles spent in the io scheduler was very much
|> considered when I wrote it (it's faster than the old one). And we could
|> always just replace the simple doubly linked list with something else.
|> Back in the early days of bio, I actually had the sorting changed to a
|> binomial heap. Given that both the read+write sort lists and expire
|> lists are just priority queues now, it would work easily. Anyways,
|> getting off track...
|>
|> > This should have the effects that we want: When there's no fight for I/O
|> > bandwidth, everybody just gets maximum performance as the I/O scheduler's
|> > queue will be short. As soon as processes fight for reads, unniced processes
|> > have a higher chance of getting served first.
|> >
|> > Just think of nightly updatedb on a webserver for a real-world example why
|> > this may matter.
|> >
|> > Looks like something not too difficult to do, but my current knowledge on
|> > 2.5 code is somewhat sparse :-(
|>
|> If you want to give it a try, it's pretty easy to add. dd->read_fifo is
|> our fifo expire list right now, you'd want to change that to
|> dd->read_prio or something. And in deadline_add_request(), the very last
|> thing we do is add the read to the tail of the expire list:
|>
|> drq->expires = jiffies + dd->read_expire;
|> list_add_tail(&drq->fifo, &dd->read_fifo);
|>
|> you'd just want something ala
|>
|> struct list_head *foo = &dd->read_prio;
|> struct deadline_rq *__drq;
|>
|> drq->expires = jiffies + deadline_process_expire();
|> while ((foo = foo->prev) != &dd->read_prio) {
|> __drq = list_entry_fifo(foo);
|> if (__drq->expires > drq->expires)
|> break;
|> }
|>
|> list_add(&drq->fifo, foo);
|>
|> you get the picture. And that should be it, really. Everything else
|> should just work as-is, iirc.Signature · 1 line
Christoffer
# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or higher. # This patch includes the following deltas:
# ChangeSet 1.675 -> 1.677
# drivers/block/ll_rw_blk.c 1.115 -> 1.117
# include/linux/blkdev.h 1.70 -> 1.72
# include/linux/fs.h 1.165 -> 1.166
# include/linux/backing-dev.h 1.3 -> 1.4
# include/linux/bio.h 1.21 -> 1.22
# drivers/block/deadline-iosched.c 1.4 -> 1.5
# (new) -> 1.2 include/linux/blk_io_prio.h
#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 02/10/04 [email protected] 1.676
#
# Added priorities to bio's.
# Changed the allocation of requests. Allocation groups are now used. # --------------------------------------------
# 02/10/06 [email protected] 1.677
# Fixes.
# --------------------------------------------
#
drivers/block/deadline-iosched.c +135 -50
@@ -24,7 +24,8 @@* fifo_batch is how many steps along the sorted list we will take when the * front fifo request expires. */ -static int read_expire =HZ /2 ;/* 500ms start timeout */ +static int write_expire =HZ *4 ;/* 2s start timeout for writes */ +static int read_expire =HZ /2 ;/* 500ms start timeout for read */ static int fifo_batch =32 ;/* 4 seeks, or 64 contig */ static int seek_cost =16 ;/* seek is 16 times more expensive */
@@ -49,7 +50,8 @@* run time data */ struct list_head sort_list[2 ];/* sorted listed */ -struct list_head read_fifo;/* fifo list */ +struct list_head read_fifo;/* prio list */ +struct list_head write_fifo;/* prio list */ struct list_head *dispatch;/* driver dispatch queue */ struct list_head *hash;/* request hash */ sector_t last_sector;/* last sector sent to drive */
@@ -61,6 +63,7 @@*/ unsigned int fifo_batch;unsigned long read_expire; +unsigned long write_expire;unsigned int seek_cost;unsigned int writes_starved; };
@@ -322,17 +325,21 @@* returns 0 if there are no expired reads on the fifo, 1 otherwise */ #define list_entry_fifo (ptr) list_entry((ptr), struct deadline_rq, fifo) -static inline int deadline_check_fifo (struct deadline_data *dd) +static inline int deadline_check_fifo (struct deadline_data *dd,int ddir) { -if (!list_empty (&dd->read_fifo)) { -struct deadline_rq *drq =list_entry_fifo (dd->read_fifo.next); +struct deadline_rq *drq =NULL ; -/* -* drq is expired! -*/ -if (time_after (jiffies, drq->expires)) +if (ddir==READ && !list_empty (&dd->read_fifo)) + drq =list_entry_fifo (dd->read_fifo.next); + +if (ddir==WRITE && !list_empty (&dd->write_fifo)) + drq =list_entry_fifo (dd->write_fifo.next); + +/* +* drq is expired? +*/ +if (drq &&time_after (jiffies, drq->expires))return 1 ; - }return 0 ; }
@@ -360,7 +367,7 @@/* * if we have expired entries on the fifo list, move some to dispatch */ -if (deadline_check_fifo (dd)) { +if (deadline_check_fifo (dd,READ )) {if (writes && (dd->starved++ >= dd->writes_starved)) goto dispatch_writes;
@@ -385,8 +392,15 @@*/ if (writes) { dispatch_writes: - nxt = dd->sort_list[WRITE ].next; -deadline_move_requests (dd,list_entry_rq (nxt)); +if (deadline_check_fifo (dd,WRITE )) { + nxt = dd->write_fifo.next; + drq =list_entry_fifo (nxt); +deadline_move_requests (dd, drq->request); + }else { + nxt = dd->sort_list[WRITE ].next; +deadline_move_requests (dd,list_entry_rq (nxt)); + } + dd->starved =0 ; goto dispatch; }
@@ -396,12 +410,63 @@return NULL ; } +static inline +void deadline_add_request_sorted (struct deadline_rq *drq,struct list_head *prio_list) +{ +struct list_head *entry; +struct deadline_rq *tmp; + + entry = prio_list; +while ((entry = entry->prev) != prio_list) { + tmp =list_entry_fifo (entry); + +if (tmp->expires <= drq->expires) +break ; + } + +list_add_tail (&drq->fifo, entry); +} + +static inline +int deadline_prio_to_expire (int expire,int prio) +{ + +int expire_tmp = expire /2 ; +int ticks_pr_prio = expire/BIO_PRIO_MAX ; + +if (prio ==BIO_PRIO_IDLE ) +return 0 ; + +return expire_tmp + prio * ticks_pr_prio; +} + +int deadline_calc_expire (struct deadline_data *dd,struct request *rq) +{ +int prio, ddir, expire =0 ; + + ddir =rq_data_dir (rq); + prio =bio_prio (rq->bio); + +switch (ddir) { +case READ : + expire = dd->read_expire; +break ; +case WRITE : + expire = dd->write_expire; +break ; +default : +BUG (); + }; + +return deadline_prio_to_expire (expire, prio); +} +static void deadline_add_request (request_queue_t *q,struct request *rq,struct list_head *insert_here) {struct deadline_data *dd = q->elevator.elevator_data;struct deadline_rq *drq =RQ_DATA (rq); -const int data_dir =rq_data_dir (rq); +int expire, data_dir =rq_data_dir (rq);/* * flush hash on barrier insert, as not to allow merges before a
@@ -430,12 +495,23 @@ q->last_merge = &rq->queuelist; } + expire =deadline_calc_expire (dd, rq); + +if (!expire)/* Idle priority doesn't expire */ +return ; +if (data_dir ==READ ) {/* -* set expire time and add to fifo list +* set expire time and add to prio list +*/ + drq->expires = jiffies + expire; +deadline_add_request_sorted (drq, &dd->read_fifo); + }else if (data_dir ==WRITE ) { +/* +* set expire time and add to prio list */ - drq->expires = jiffies + dd->read_expire; -list_add_tail (&drq->fifo, &dd->read_fifo); + drq->expires = jiffies + expire; +deadline_add_request_sorted (drq, &dd->write_fifo); } }
@@ -459,6 +535,7 @@return 0 ;BUG_ON (!list_empty (&dd->read_fifo)); +BUG_ON (!list_empty (&dd->write_fifo));return 1 ; }
@@ -475,27 +552,30 @@struct deadline_data *dd = e->elevator_data;struct deadline_rq *drq;struct request *rq; -int i; +int j, i;BUG_ON (!list_empty (&dd->read_fifo)); +BUG_ON (!list_empty (&dd->write_fifo));BUG_ON (!list_empty (&dd->sort_list[READ ]));BUG_ON (!list_empty (&dd->sort_list[WRITE ])); -for (i =READ ; i <=WRITE ; i++) { -struct request_list *rl = &q->rq[i]; -struct list_head *entry = &rl->free; +for (j =0 ; j <QUEUE_NR_AGS ; j++) { +for (i =READ ; i <=WRITE ; i++) { +struct request_list *rl = &q->ags[j].rq[i]; +struct list_head *entry = &rl->free; -if (list_empty (&rl->free)) -continue ; - -while ((entry = entry->next) != &rl->free) { - rq =list_entry_rq (entry); - -if ((drq =RQ_DATA (rq)) ==NULL ) +if (list_empty (&rl->free))continue ; - rq->elevator_private =NULL ; -kmem_cache_free (drq_pool, drq); +while ((entry = entry->next) != &rl->free) { + rq =list_entry_rq (entry); + +if ((drq =RQ_DATA (rq)) ==NULL ) +continue ; + + rq->elevator_private =NULL ; +kmem_cache_free (drq_pool, drq); + } } }
@@ -512,7 +592,7 @@struct deadline_data *dd;struct deadline_rq *drq;struct request *rq; -int i, ret =0 ; +int j, i, ret =0 ;if (!drq_pool)return -ENOMEM ;
@@ -532,37 +612,42 @@INIT_LIST_HEAD (&dd->hash[i]);INIT_LIST_HEAD (&dd->read_fifo); +INIT_LIST_HEAD (&dd->write_fifo);INIT_LIST_HEAD (&dd->sort_list[READ ]);INIT_LIST_HEAD (&dd->sort_list[WRITE ]); dd->dispatch = &q->queue_head; dd->fifo_batch = fifo_batch; dd->read_expire = read_expire; + dd->read_expire = write_expire; + dd->write_expire = write_expire; dd->seek_cost = seek_cost; dd->hash_valid_count =1 ; dd->writes_starved = writes_starved; e->elevator_data = dd; -for (i =READ ; i <=WRITE ; i++) { -struct request_list *rl = &q->rq[i]; -struct list_head *entry = &rl->free; +for (j =0 ; j <QUEUE_NR_AGS ; j++) { +for (i =READ ; i <=WRITE ; i++) { +struct request_list *rl = &q->ags[j].rq[i]; +struct list_head *entry = &rl->free; -if (list_empty (&rl->free)) -continue ; - -while ((entry = entry->next) != &rl->free) { - rq =list_entry_rq (entry); - - drq =kmem_cache_alloc (drq_pool,GFP_KERNEL ); -if (!drq) { - ret = -ENOMEM ; -break ; - } +if (list_empty (&rl->free)) +continue ; -memset (drq,0 ,sizeof (*drq)); -INIT_LIST_HEAD (&drq->fifo); -INIT_LIST_HEAD (&drq->hash); - drq->request = rq; - rq->elevator_private = drq; +while ((entry = entry->next) != &rl->free) { + rq =list_entry_rq (entry); + + drq =kmem_cache_alloc (drq_pool,GFP_KERNEL ); +if (!drq) { + ret = -ENOMEM ; +break ; + } + +memset (drq,0 ,sizeof (*drq)); +INIT_LIST_HEAD (&drq->fifo); +INIT_LIST_HEAD (&drq->hash); + drq->request = rq; + rq->elevator_private = drq; + } } }
drivers/block/ll_rw_blk.c +115 -76
359 lines not on this page · raw message
include/linux/backing-dev.h +22 -3
@@ -9,21 +9,26 @@#define _LINUX_BACKING_DEV_H#include <asm/atomic.h> +#include <linux/blk_io_prio.h> /* * Bits in backing_dev_info.state */ enum bdi_state { BDI_pdflush,/* A pdflush thread is working this device */ + BDI_unused,/* Available bits start here */ +}; + +enum bdi_ag_state { BDI_write_congested,/* The write queue is getting full */ BDI_read_congested,/* The read queue is getting full */ - BDI_unused,/* Available bits start here */ };struct backing_dev_info {unsigned long ra_pages;/* max readahead in PAGE_CACHE_SIZE units */ unsigned long state;/* Always use atomic bitops on this */ int memory_backed;/* Cannot clean pages with writepage */ +unsigned long agstate[QUEUE_NR_AGS ];/* State per allocation group */ };extern struct backing_dev_info default_backing_dev_info;
@@ -32,14 +37,28 @@int writeback_in_progress (struct backing_dev_info *bdi);void writeback_release (struct backing_dev_info *bdi); +static inline int bdi_read_congested_prio (struct backing_dev_info *bdi,int prio) +{ +int ag =prio_to_ag (prio); +return test_bit (BDI_read_congested, &bdi->agstate[ag]); +} +static inline int bdi_read_congested (struct backing_dev_info *bdi) { -return test_bit (BDI_read_congested, &bdi->state); +int prio =get_io_prio (current); +return bdi_read_congested_prio (bdi, prio); +} + +static inline int bdi_write_congested_prio (struct backing_dev_info *bdi,int prio) +{ +int ag =prio_to_ag (prio); +return test_bit (BDI_write_congested, &bdi->agstate[ag]); }static inline int bdi_write_congested (struct backing_dev_info *bdi) { -return test_bit (BDI_write_congested, &bdi->state); +int prio =get_io_prio (current); +return bdi_write_congested_prio (bdi, prio); }#endif /* _LINUX_BACKING_DEV_H */
include/linux/bio.h +25 -0
@@ -110,10 +110,26 @@* bit 0 -- read (not set) or write (set) * bit 1 -- rw-ahead when set * bit 2 -- barrier +* +* bit 8 -- first prio bit +* bit 15 -- last prio bit +* */ #define BIO_RW 0#define BIO_RW_AHEAD 1#define BIO_RW_BARRIER 2 +#define BIO_PRIO_IDLE 0 +#define BIO_PRIO_LOW 10 +#define BIO_PRIO_MED 20 +#define BIO_PRIO_HIGH 30 + +#define BIO_PRIO_MIN BIO_PRIO_IDLE +#define BIO_PRIO_MAX 42 + + +#define BIO_PRIO_START 8 +#define BIO_PRIO_MASK (255 << BIO_PRIO_START) +/* * various member access, note that bio_data should of course not be used
@@ -126,6 +142,15 @@#define bio_sectors (bio) ((bio)->bi_size >> 9)#define bio_data (bio) (page_address(bio_page((bio))) + bio_offset((bio)))#define bio_barrier (bio) ((bio)->bi_rw & (1 << BIO_RW_BARRIER)) + +#define bio_prio (bio) (((bio)->bi_rw & BIO_PRIO_MASK)>>BIO_PRIO_START) +#define bio_set_prio (bio, prio) do { \ + int __n = ((prio)<<BIO_PRIO_START); \ + __n &= BIO_PRIO_MASK; \ + (bio)->bi_rw &= ~BIO_PRIO_MASK; \ + (bio)->bi_rw |= __n; \ + } while (0) +/* * will die
include/linux/blk_io_prio.h new file +51 -0
51 lines not on this page · raw message
include/linux/blkdev.h +10 -2
@@ -16,7 +16,7 @@typedef struct elevator_s elevator_t ;struct request_list { -unsigned int count; +unsigned int count, ag;struct list_head free;wait_queue_head_t wait; };
@@ -139,6 +139,14 @@ };/* +* Request allocation group +*/ +struct request_ag { +struct request_list rq[2 ];/* one list list for writes and one for read */ +int outstanding[2 ];/* # allocated requests read/write */ +}; + +/* * Default nr free requests per queue, ll_rw_blk will scale it down * according to available RAM at init time */
@@ -156,7 +164,7 @@/* * the queue request freelist, one for reads and one for writes */ -struct request_list rq[2 ]; +struct request_ag ags[QUEUE_NR_AGS ];request_fn_proc *request_fn;merge_request_fn *back_merge_fn;
include/linux/fs.h +6 -0
@@ -1129,6 +1129,11 @@* return READ, READA, or WRITE */ #define bio_rw (bio) ((bio)->bi_rw & (RW_MASK | RWA_MASK)) +#define bio_set_rw (bio,rw) do {\ + ((bio)->bi_rw &= ~(RW_MASK|RWA_MASK)); \ + ((bio)->bi_rw |= (rw & (RW_MASK|RWA_MASK))); \ + } while (0) +/* * return data direction, READ or WRITE
@@ -1234,6 +1239,7 @@extern void file_move (struct file *f,struct list_head *list);struct bio ;extern int submit_bio (int ,struct bio *); +extern int submit_bio_prio (int ,int ,struct bio *);extern int bdev_read_only (struct block_device *);extern int set_blocksize (struct block_device *,int );extern int sb_set_blocksize (struct super_block *,int );