deadline 调度算法

deadline算法的核心就是在传统的电梯算法中加入了请求超时的机制,该机制主要体现在两点:
1、请求超时时,对超时请求的选择。
2、没有请求超时时,当扫描完电梯最后一个request后,准备返回时,对第一个request的选择。基于以上两点,平衡了系统i/o吞吐量和响应时间。
此外,该算法还考虑到了读操作对写操作造成的饥饿。

定义了elevator_deadline调度器类型:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static struct elevator_type iosched_deadline = {   
  2.  .ops = {   
  3.   .elevator_merge_fn = deadline_merge,   
  4.   .elevator_merged_fn =  deadline_merged_request,   
  5.   .elevator_merge_req_fn =  deadline_merged_requests,   
  6.   .elevator_dispatch_fn =    deadline_dispatch_requests,   
  7.   .elevator_add_req_fn =     deadline_add_request,   
  8.   .elevator_queue_empty_fn =    deadline_queue_empty,   
  9.   .elevator_former_req_fn = elv_rb_former_request,   
  10.   .elevator_latter_req_fn = elv_rb_latter_request,   
  11.   .elevator_init_fn =    deadline_init_queue,   
  12.   .elevator_exit_fn =    deadline_exit_queue,   
  13.  },   
  14.  .elevator_attrs = deadline_attrs,   
  15.  .elevator_name = "deadline",   
  16.  .elevator_owner = THIS_MODULE,   
  17. };  

定义了管理deadline调度器的数据结构:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct deadline_data   
  2. {   
  3. /*  
  4. * run time data  
  5. */   
  6.   
  7. /*  
  8. * requests (deadline_rq s) are present on both sort_list and fifo_list  
  9. */   
  10. struct rb_root sort_list[2];   
  11. struct list_head fifo_list[2];   
  12.   
  13. /*  
  14. * next in sort order. read, write or both are NULL  
  15. */   
  16. struct request *next_rq[2];   
  17. unsigned int batching; /* number of sequential requests made */   
  18. sector_t last_sector; /* head position */   
  19. unsigned int starved; /* times reads have starved writes */   
  20.   
  21. /*  
  22. * settings that change how the i/o scheduler behaves  
  23. */   
  24. int fifo_expire[2];   
  25. int fifo_batch;   
  26. int writes_starved;   
  27. int front_merges;   
  28. };   

可以看到这有4个队列 
struct rb_root sort_list[2]; 
struct list_head fifo_list[2]; 
sort_list是红黑树的根,用于对io请求按起始扇区编号进行排序,这里有两颗树,一颗读请求树,一颗写请求树。 
要理解是怎么排序和查找的,需要你去学一下数据结构里面的红黑树了。 
然后你可以去试着理解下 lib/rbtree.c和include/linux/rbtree.h,这两个文件就是内核中红黑树的具体代码。 
再后就可以去看看block/elevator.c中是如何使用红黑树来组织io请求的。 

如果你无法理解红黑树,可以暂时这么理解下,用红黑树是为了缩短查找时间,具体为啥不用管,先简单的将它看成一颗普通的二叉树。 
树上每个节点可以有左右两个孩子。
如果有左孩子,那么左孩子请求的起始扇区是小于它的父节点请求的起始扇区的;如果有右孩子,那么右孩子的起始扇区是大于或等于父节点的起始扇区的。 
首先,应该明白扇区号是不断递增的,所以为了找到与当前io请求的起始扇区最接近的节点,只需从右子树上去找最小值,也就是不断的去找左孩子,直到 
找到没有左孩子的节点(想一想,左边代表小于,一直都在小于,直到无法再小于了,那自然就是最小值了)。 
如果当前节点没有右孩子怎么办,就回到父节点,并且如果它是父节点的左孩子,那就表示它比父节点小,父节点就满足递增关系且最接近当前节点,此时 
返回父节点就可以了。如果它在父节点的右边,那就退回到父节点,迭代这一操作直到找到一个为其左孩子的父节点。如果一直回到了根,就返回NULL。 

回到正题,这里我们看到了"按起始扇区编号排序",这相当于合理的插了个小队。 
试想一下,你们在学校食堂排队吃饭时,第1,3,5号人说他要水煮牛肉,而第2,4,6号人说他要鱼香肉丝,那聪明的厨师会一次做3人量(合并)的水煮牛肉先给1,3,5,再一次性地做3人量的鱼香肉丝分别给2,4,6号人,并且大家对这种"插队"没有异议。 
这回厨师可高兴:"6个人只做两次"。 
试想一下,不管是对于机械硬盘也好,还是以闪存为基础的ssd,u盘,sd卡也好,将请求合并,意味着和这些东东上的控制器打交道的次数少了。 
传输变成更大块的连续传输(bulk),性能自然就提高了。尤其是对u盘和sd卡,因为它们的控制器太慢了。 
有人可能会觉得ssd不需要,虽然ssd的4k随机性能很好,但是再好也被它的连续性能完爆。机械硬盘更是如此。所以这种合并是合理的。 
实际情况中,后端合并比较容易发生,所以内核调度框架 block/elevator.c 直接把后端合并给做了,所以不管你用啥调度,都必须要进行后端合并的。

来看看源码
a、首先是请求过来了,如何入队
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /*  
  2. * add rq to rbtree and fifo  
  3. */   
  4. static void deadline_add_request(struct request_queue *q, struct request *rq)   
  5. {   
  6. struct deadline_data *dd = q->elevator->elevator_data;   
  7. const int data_dir = rq_data_dir(rq);   
  8.   
  9. deadline_add_rq_rb(dd, rq);   
  10.   
  11. /*  
  12. * set expire time and add to fifo list  
  13. */   
  14. rq_set_fifo_time(rq, jiffies + dd->fifo_expire[data_dir]);   
  15. list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]);   
  16. }  

deadline_add_rq_rb(dd, rq); 
将请求加入到deadline调度器的sort_list红黑树中,注意这个函数会去判断rq的方向(读或写)来决定加入那棵树。 

rq_set_fifo_time(rq, jiffies + dd->fifo_expire[data_dir]); 
设置请求超时的时间,也就是说这个请求在这个时间到了必须得到响应。这就参与调度了。 
这里出现了一个data_dir,dir是什么英文单词?direction (方向),所以这里就表示读方向或写方向。 

list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]); 
将请求加入deadline调度器的list_fifo链表中。 

b、入队完毕,该合并了,这就来到了
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static int deadline_merge(struct request_queue *q, struct request **req, struct bio *bio)   
  2. {   
  3. struct deadline_data *dd = q->elevator->elevator_data;   
  4. struct request *__rq;   
  5. int ret;   
  6.   
  7. /*  
  8. * check for front merge  
  9. */   
  10. if (dd->front_merges)   
  11. {   
  12. sector_t sector = bio_end_sector(bio);   
  13.   
  14. __rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector);   
  15. if (__rq)   
  16. {   
  17. BUG_ON(sector != blk_rq_pos(__rq));   
  18.   
  19. if (elv_rq_merge_ok(__rq, bio))   
  20. {   
  21. ret = ELEVATOR_FRONT_MERGE;   
  22. goto out;   
  23. }   
  24. }   
  25. }   
  26.   
  27. return ELEVATOR_NO_MERGE;   
  28. out:   
  29. *req = __rq;   
  30. return ret;   
  31. }  

if (dd->front_merges)
如果front_merges == 0 那么,就不会去做前端合并了。所以我们可以通过设置这个参数为0来关闭前端合并,后面会讲到。 

sector_t sector = bio_end_sector(bio); 
得到bio请求的结束扇区地址。 

__rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector); 
在同方向(读或写)的红黑树上去找请求的起始扇区地址与带合并请求的结束扇区地址相同的请求。两个请求的都是读或写,而且还在地址上连续,  不就可以合并成一个了嘛。 
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if (__rq)   
  2. {   
  3. BUG_ON(sector != blk_rq_pos(__rq));   
  4. if (elv_rq_merge_ok(__rq, bio))   
  5. {   
  6. ret = ELEVATOR_FRONT_MERGE;   
  7. goto out;   
  8. }   
  9. }  

如果找到了就合并(elv_rq_merge_ok(__rq, bio)),如果合并成功了,就返回ELEVATOR_FRONT_MERGE,表示进行了前端合并。 
为什么只有前端合并,前面说noop时已经就说过,elevator会自己做后端合并,并且后端合并优先于前端合并。


*req = __rq;
要注意这里返回的是被合并的请求

如果做了前端合并,还会调用
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static void deadline_merged_request(struct request_queue *q, struct request *req, int type)   
  2. {   
  3. struct deadline_data *dd = q->elevator->elevator_data;   
  4.   
  5. /*  
  6. * if the merge was a front merge, we need to reposition request  
  7. */   
  8. if (type == ELEVATOR_FRONT_MERGE)   
  9. {   
  10. elv_rb_del(deadline_rb_root(dd, req), req);   
  11. deadline_add_rq_rb(dd, req);   
  12. }   
  13. }  

用于响应前端合并,这里的操作就是把请求从红黑树上删了又重新加回来。 

然后是合并后处理:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static void deadline_merged_requests(struct request_queue *q, struct request *req, struct request *next)   
  2. {   
  3. /*  
  4. * if next expires before rq, assign its expire time to rq  
  5. * and move into next position (next will be deleted) in fifo  
  6. */   
  7. if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist))   
  8. {   
  9. if (time_before(rq_fifo_time(next), rq_fifo_time(req)))   
  10. {   
  11. list_move(&req->queuelist, &next->queuelist);   
  12. rq_set_fifo_time(req, rq_fifo_time(next));   
  13. }   
  14. }   
  15.   
  16. /*  
  17. * kill knowledge of next, this one is a goner  
  18. */   
  19. deadline_remove_request(q, next);   
  20. }  

要注意这个函数的名字,与前面一个响应前端合并的函数很像(只差一个字母s,别混淆了)。这个函数是干嘛的?想想刚才举的厨师的例子,厨师把1,3,5号人的请求合并成第一次了,那对应的第3次和第5次就可以不做了。 
所以这里也是删掉多余的请求:deadline_remove_request(q, next);因为是前端合并,所以总是删后一个(next)。 
那么前面一个if语句是干嘛的呢?还记得为了实时性考虑,我们在add_request时给每个请求都增加了一个截止响应时间吗? 
如果要被删除的next时间上比合并后的这一个早该咋整,自然是,把合并后的时间改成next的。那为什么又要 list_move(&req->queuelist, &next->queuelist); 
因为改变了时间,排序就应该也跟着变。

c、最后需要将请求放到系统的request_queue队列中去
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static int deadline_dispatch_requests(struct request_queue *q, int force)   
  2. {   
  3. struct deadline_data *dd = q->elevator->elevator_data;   
  4. const int reads = !list_empty(&dd->fifo_list[READ]);   
  5. const int writes = !list_empty(&dd->fifo_list[WRITE]);   
  6. struct request *rq;   
  7. int data_dir;   
  8.   
  9. /*  
  10. * batches are currently reads XOR writes  
  11. */   
  12. if (dd->next_rq[WRITE])   
  13. rq = dd->next_rq[WRITE];   
  14. else   
  15. rq = dd->next_rq[READ];   
  16.   
  17. if (rq && dd->batching < dd->fifo_batch)   
  18. /* we have a next request are still entitled to batch */   
  19. goto dispatch_request;   
  20.   
  21. /*  
  22. * at this point we are not running a batch. select the appropriate  
  23. * data direction (read / write)  
  24. */   
  25.   
  26. if (reads)   
  27. {   
  28. BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));   
  29.   
  30. if (writes && (dd->starved++ >= dd->writes_starved))   
  31. goto dispatch_writes;   
  32.   
  33. data_dir = READ;   
  34.   
  35. goto dispatch_find_request;   
  36. }   
  37.   
  38. /*  
  39. * there are either no reads or writes have been starved  
  40. */   
  41.   
  42. if (writes)   
  43. {   
  44. dispatch_writes:   
  45.   
  46. BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[WRITE]));   
  47.   
  48. dd->starved = 0;   
  49.   
  50. data_dir = WRITE;   
  51.   
  52. goto dispatch_find_request;   
  53. }   
  54.   
  55. return 0;   
  56.   
  57. dispatch_find_request:   
  58. /*  
  59. * we are not running a batch, find best request for selected data_dir  
  60. */   
  61. if (deadline_check_fifo(dd, data_dir) || !dd->next_rq[data_dir])   
  62. {   
  63. /*  
  64. * A deadline has expired, the last request was in the other  
  65. * direction, or we have run out of higher-sectored requests.  
  66. * Start again from the request with the earliest expiry time.  
  67. */   
  68. rq = rq_entry_fifo(dd->fifo_list[data_dir].next);   
  69. }   
  70. else   
  71. {   
  72. /*  
  73. * The last req was the same dir and we have a next request in  
  74. * sort order. No expired requests so continue on from here.  
  75. */   
  76. rq = dd->next_rq[data_dir];   
  77. }   
  78.   
  79. dd->batching = 0;   
  80.   
  81. dispatch_request:   
  82. /*  
  83. * rq is the selected appropriate request.  
  84. */   
  85. dd->batching++;   
  86. deadline_move_request(dd, rq);   
  87.   
  88. return 1;   
  89. }  

这个函数稍微有点复杂。

首先是next_rq,在deadline的数据结构体中是这么定义的 
struct request *next_rq[2]; 
这两个指针一个用于指向读方向上的下一个请求,另一个指向写方向上的下一个请求。
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if (dd->next_rq[WRITE])   
  2.     rq = dd->next_rq[WRITE];   
  3. else   
  4.     rq = dd->next_rq[READ];   
  5. if (rq && dd->batching < dd->fifo_batch)   
  6.     /* we have a next request are still entitled to batch */   
  7.     goto dispatch_request;   

取得下一个请求,如果rq不为空指针,并且进一步的batching < fifo_batch, 那么就继续将下一个请求发送到系统的request_queue上去。 
rq怎样才能不为空呢,后面分析到有关next_rq的赋值才能说到,所以第一次进来时,这个rq是NULL。 
因此第一次进来时会走到后面的代码。
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. const int reads = !list_empty(&dd->fifo_list[READ]);   
  2. const int writes = !list_empty(&dd->fifo_list[WRITE]);   
  3. ...   
  4. if (reads)   
  5. {   
  6. BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));   
  7.   
  8. if (writes && (dd->starved++ >= dd->writes_starved))   
  9. goto dispatch_writes;   
  10.   
  11. data_dir = READ;   
  12.   
  13. goto dispatch_find_request;   
  14. }   

先看读方向的fifo队列是不是空的,如果不是就先满足读请求,但是如果写队列不为空,并且starved++ >= writes_starved, 就把方向改为写,这里writes_starved是写写请求最大可被饥饿的次数,而starved表示写请求已经被饥饿的次数,只有当starved >= writeds_starved,才会去处理写请求。
可以看到只有这个地方会发生starved增加1的操作,所以并不是读请求每发送一次就+1,应该是一批读请求过后再+1。如何确定一批读请求呢,这又回到了上一段与next_rq有关的代码,后面会讲到。 
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if (writes)   
  2. {   
  3. dispatch_writes:   
  4. BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[WRITE]));   
  5. dd->starved = 0;   
  6. data_dir = WRITE;   
  7. goto dispatch_find_request;   
  8. }   

如果读方向上的fifo队列是空的,或者写请求被饥饿的次数已达上限,代码就会走到这里,进行写请求的处理。 
先把starved置0,不难理解,写请求即将被处理,不饥饿了。 
这一个代码和上一段代码都只会设置  data_dir = XXX; 
这样一个状态,表示方向。然后跳转到dispatch_find_request 

return 0; 
中间还穿插着这么一小句代码,表示既没有读请求,又没有写请求,直接返回0
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. dispatch_find_request:   
  2. /*  
  3. * we are not running a batch, find best request for selected data_dir  
  4. */   
  5. if (deadline_check_fifo(dd, data_dir) || !dd->next_rq[data_dir])   
  6. {   
  7. /*  
  8. * A deadline has expired, the last request was in the other  
  9. * direction, or we have run out of higher-sectored requests.  
  10. * Start again from the request with the earliest expiry time.  
  11. */   
  12. rq = rq_entry_fifo(dd->fifo_list[data_dir].next);   
  13. }   
  14. else   
  15. {   
  16. /*  
  17. * The last req was the same dir and we have a next request in  
  18. * sort order. No expired requests so continue on from here.  
  19. */   
  20. rq = dd->next_rq[data_dir];   
  21. }   
  22. dd->batching = 0;   

deadline_check_fifo(dd, data_dir) 
用于检测对应读/写方向上的fifo_list链表中第一个请求的超时时间(前面在将请求放入fifo_list链表之前为每个请求都 赋予了一个截至时间),如果时间超了,就必须从这个fifo_list链表上取请求了。 
对于读来说,这个时间由deadline的结构体中的fifo_expire[READ]决定,通常是500ms。那么fifo_expire[WRITE] 决定着写请求的截止时间,通常是5s。 

!dd->next_rq[data_dir] 
如果下一个请求的方向不同了,那么本方向上的next_rq就是空,还是从fifo_list链表上取第一个节点。 

为什么是fifo_list[data_dir].next 
因为fifo_list[data_dir]是链表头(head),不是一个有效的节点(node),头的next是第一个节点。 
那么为什么总是取第一个节点,前面noop已经说过了,fifo队列,取走了第一个,第二就又变成第一个了。 

dd->batching = 0; 
将batching置0了,表示开始新的一批io请求。 

这里做一个小小的总结: 
<1> 要想处理写方向上的请求,必须让写请求被饥饿的次数(starved)达到写请求被饥饿次数的上限(write_starved); 
<2> 如果对应方向上的fifo_list链表上的请求(总是第一个)截至时间到了,那么就从fifo_list链表上取请求,因为这个请求必须要被服务了; 
<3> 如果处理某方向上的请求,已经按照请求的起始访问地址扫描到电梯的末尾,就需要从对应方向的fifo_list链表上取请求(开始新的一批请求)。 
<4> 如果处理某方向上的请求,没有扫描到电梯的末尾,那么就从next_rq[方向]取得请求(此时next_rq[方向]不会为空)

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. dispatch_request:   
  2. /*  
  3. * rq is the selected appropriate request.  
  4. */   
  5. dd->batching++;   
  6. deadline_move_request(dd, rq);   
  7. return 1;   

首先batching++,表示这批请求已经有1个了,记个数。 
然后调用deadline_move_request,这个函数会开始对next_rq赋值,所以第一次进这个函数时,next_rq[方向]是空的,从第二次开始就不一定了。
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static void deadline_move_request(struct deadline_data *dd, struct request *rq)   
  2. {   
  3. const int data_dir = rq_data_dir(rq);   
  4.   
  5. dd->next_rq[READ] = NULL;   
  6. dd->next_rq[WRITE] = NULL;   
  7. dd->next_rq[data_dir] = deadline_latter_request(rq);   
  8.   
  9. dd->last_sector = rq_end_sector(rq);   
  10.   
  11. /*  
  12. * take it off the sort and fifo list, move  
  13. * to dispatch queue  
  14. */   
  15. deadline_move_to_dispatch(dd, rq);   
  16. }   

首先确定本次请求方向(data_dir), 然后再为本次请求方向上的next_rq进行赋值, dd->next_rq[data_dir] = deadline_latter_request(rq);
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static inline struct request * deadline_latter_request(struct request *rq)   
  2. {   
  3. struct rb_node *node = rb_next(&rq->rb_node);  
  4. if (node)   
  5. return rb_entry_rq(node);   
  6.   
  7. return NULL;   
  8. }  

会在红黑树上去查找下一个节点,rb_next是内核中有关红黑树的操作,定义在lib/rbtree.c。rb_next做了什么,上面一段话已经表述过了,这里 再贴一下: 
“树上每个节点可以有左右两个孩子。如果有左孩子,那么左孩子请求的起始扇区是小于它的父节点请求的起始扇区的;  如果有右边孩子,那么右孩子的起始扇区是大于或等于父节点的起始扇区的。 
首先,应该明白扇区号是不断递增的,所以为了找到与当前请求的起始扇区最接近的节点,只需从右子树上去找最小值,也就是不断的去找左孩子,直到 找到没有左孩子的节点(想一想,左边代表小于,一直都在小于,直到无法再小于了,那自然就是最小值了)。  如果当前节点没有右孩子怎么办,就回到父节点,并且如果它在父节点左边,那就表示它比父节点小,父节点就是满足递增关系且最接近当前节点的节点,此时 返回父节点就可以了。如果它在父节点的右边,那就退回到父节点,迭代这一操作直到找到一个为其左孩子的父节点。如果回到了根,就返回NULL。” 
所谓的io排序就体现在这里。 

最后真正的dispatch在函数deadline_move_to_dispatch中
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static inline void deadline_move_to_dispatch(struct deadline_data *dd, struct request *rq)   
  2. {   
  3. struct request_queue *q = rq->q;   
  4.   
  5. deadline_remove_request(q, rq);   
  6. elv_dispatch_add_tail(q, rq);   
  7. }  

这里代码还是比较容易理解的, deadline_remove_request(q, rq);  将请求rq从deadline调度器的红黑树以及fifo_list链表中删掉,并且移除其超时时间。 
elv_dispatch_add_tail(q, rq); 将请求rq插入 到块设备的请求队列request_queue中去。 

这时再回头看看
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /*  
  2. * batches are currently reads XOR writes  
  3. */   
  4. if (dd->next_rq[WRITE])   
  5.     rq = dd->next_rq[WRITE];   
  6. else   
  7.     rq = dd->next_rq[READ];   
  8.   
  9. if (rq && dd->batching < dd->fifo_batch)   
  10.     /* we have a next request are still entitled to batch */   
  11.     goto dispatch_request;   

如果第二次进来,rq有可能不为空,而rq是从红黑树中查找到的,其起始扇区最靠近上一次请求。 
如果batching还是小于fifo_batch,就会直接去dispatch,所以从这里可以知道fifo_batch限定了一次连续提交请求的最大数量。 
所以如果fifo_batch的值很小,将不利于批处理操作,所以吞吐量会下降。但是获得了更多的重新选择读或写方向的机会,实时性变好了。 
反之则是吞吐量上升,实时性下降。(需要在吞吐量和实时性之间做一个权衡)

至此,deadline的基本原理分析完毕。

整个deadline调度器比较简洁,总共只有400多行。它充分考虑了batching,写饥饿,请求超时这三大因素。在保证吞吐量的基础上,有考虑到了响应延时。

附:2.6.32版本下deadline调度算法的完整代码和注释
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static const int read_expire = HZ / 2;  /* max time before a read is submitted. */  
  2. static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */  
  3. static const int writes_starved = 2;    /* max times reads can starve a write */  
  4. static const int fifo_batch = 16;       /* # of sequential requests treated as one 
  5.                      by the above parameters. For throughput. */  
  6.   
  7. struct deadline_data {  
  8.     /* 
  9.      * run time data 
  10.      */  
  11.   
  12.     /* 
  13.      * requests (deadline_rq s) are present on both sort_list and fifo_list 
  14.      */  
  15.     struct rb_root sort_list[2];    //按照请求的起始地址,将请求在红黑树中排序  
  16.     struct list_head fifo_list[2];//按照请求超时的先后顺序,将请求在链表中排序  
  17.   
  18.     /* 
  19.      * next in sort order. read, write or both are NULL 
  20.      */  
  21.     struct request *next_rq[2];//指向 sort_list 中的下一个请求  
  22.     /* 当前连续提交的请求数目,只要小于fifo_batch就可以进行连续提交 */  
  23.     unsigned int batching;      /* number of sequential requests made */  
  24.     sector_t last_sector;       /* head position */  
  25.     unsigned int starved;       /* times reads have starved writes */  
  26.   
  27.     /* 
  28.      * settings that change how the i/o scheduler behaves 
  29.      */  
  30.     int fifo_expire[2];//可以设置读写请求的超时时间  
  31.     int fifo_batch;//在一次读/写请求处理方向上,最多可连续提交的请求数  
  32.     int writes_starved;//读请求最多可饥饿写请求的次数  
  33.     int front_merges;//是否开启前向merge,根据文件的一般访问模式,前向merge的效果不是很好  
  34. };  
  35.   
  36. static void deadline_move_request(struct deadline_data *, struct request *);  
  37.   
  38. static inline struct rb_root *  
  39. deadline_rb_root(struct deadline_data *dd, struct request *rq)  
  40. {  
  41.     return &dd->sort_list[rq_data_dir(rq)];  
  42. }  
  43.   
  44. /* 
  45.  * get the request after `rq' in sector-sorted order 
  46.  */  
  47. static inline struct request *  
  48. deadline_latter_request(struct request *rq)  
  49. {  
  50.     struct rb_node *node = rb_next(&rq->rb_node);  
  51.   
  52.     if (node)  
  53.         return rb_entry_rq(node);  
  54.     /* 
  55.     struct request结构中内嵌一个struct rb_node(这也正是红黑树的使用模式) 
  56.     通过获取rb_node的地址+rb_node在request中的偏移地址来获取request的地址 
  57.     */  
  58.     return NULL;  
  59. }  
  60.   
  61. static void  
  62. deadline_add_rq_rb(struct deadline_data *dd, struct request *rq)  
  63. {  
  64.     struct rb_root *root = deadline_rb_root(dd, rq);//根据请求rq的读写方向,获取相应读写请求红黑树的根结点  
  65.     struct request *__alias;  
  66.       
  67.     /* 
  68.     将请求rq插入相应的读写请求红黑树 
  69.     注意:插入成功才返回NULL,插入不成功,说明该请求原本就存在 
  70.     如果该请求原本就存在,就需要将该请求从io调度队列分发到块设备的dispatch队列 
  71.     */  
  72.     while (unlikely(__alias = elv_rb_add(root, rq)))  
  73.         deadline_move_request(dd, __alias);  
  74. }  
  75.   
  76. static inline void  
  77. deadline_del_rq_rb(struct deadline_data *dd, struct request *rq)  
  78. {  
  79.     const int data_dir = rq_data_dir(rq);  
  80.   
  81.     /* 
  82.     如果待删除的请求是next_rq请求队列中的第一个请求, 
  83.     就需要将next_rq请求队列中的第一个请求设置为待删除请求的下一个请求,再执行删除请求操作, 
  84.     否则,直接删除该请求 
  85.     */  
  86.     if (dd->next_rq[data_dir] == rq)  
  87.         dd->next_rq[data_dir] = deadline_latter_request(rq);  
  88.   
  89.     elv_rb_del(deadline_rb_root(dd, rq), rq);  
  90. }  
  91.   
  92. /* 
  93.  * add rq to rbtree and fifo 
  94.  */  
  95. static void  
  96. deadline_add_request(struct request_queue *q, struct request *rq)  
  97. {  
  98.     struct deadline_data *dd = q->elevator->elevator_data;//获取请求的io调度队列  
  99.     const int data_dir = rq_data_dir(rq);//判断请求的读写类型  
  100.   
  101.     deadline_add_rq_rb(dd, rq);  
  102.     /* 
  103.     1、请求原本存存在于调度队列中 
  104.     根据请求类型(读or写),将请求插入调度队列相应的读写红黑树中 
  105.     2、请求原本存在于调度队列中 
  106.     将请求从调度队列发往块设备的请求队列 
  107.     */  
  108.   
  109.     /* 
  110.      * set expire time and add to fifo list 
  111.      */  
  112.     rq_set_fifo_time(rq, jiffies + dd->fifo_expire[data_dir]);//设置请求的超时时间  
  113.     list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]);//将请求加入调度队列的fifo队列中  
  114. }  
  115.   
  116. /* 
  117.  * remove rq from rbtree and fifo. 
  118.  */  
  119. static void deadline_remove_request(struct request_queue *q, struct request *rq)  
  120. {  
  121.     struct deadline_data *dd = q->elevator->elevator_data;//获取请求的调度队列  
  122.   
  123.     rq_fifo_clear(rq);//将请求从调度队列的fifo_list队列中删除  
  124.     deadline_del_rq_rb(dd, rq);//将请求从调度队列的红黑树中删除  
  125. }  
  126.   
  127. /* 
  128.     该deadline_merge函数中试图将bio插入到请求的链表头。???????????????????????????? 
  129.     在该函数中通过elv_rb_find函数在读写的排序队列sort_list中(通过红黑二叉树来实现) 
  130.     查找可以把bio插入到请求的链表头的req, 
  131.     (这里只是找到可以插入的req,究竟bio是否可以插入到此req中是在执行插入的时候才做判断) 
  132.     如果找到,则返回ELEVATOR_FRONT_MERGE,否则返回ELEVATOR_NO_MERGE。 
  133. */  
  134. static int  
  135. deadline_merge(struct request_queue *q, struct request **req, struct bio *bio)  
  136. {  
  137.     struct deadline_data *dd = q->elevator->elevator_data;  
  138.     struct request *__rq;  
  139.     int ret;  
  140.   
  141.     /* 
  142.      * check for front merge 
  143.      */  
  144.     if (dd->front_merges) {//判断是否执行前向merge操作  
  145.         sector_t sector = bio->bi_sector + bio_sectors(bio);  
  146.         //通过bio请求的起始地址 + bio请求的长度 获取bio请求的结束地址  
  147.         __rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector);  
  148.         /* 
  149.         通过elv_rb_find()函数判断是否存在与该bio请求连续的请求: 
  150.         1、返回值为NULL,说明不存在前向连续的请求 
  151.         2、返回值不为NULL,说明存在前向连续的请求,有进行合并操作的可能性,并返回指向与bio请求连续的请求的指针 
  152.         */  
  153.         if (__rq) {  
  154.             BUG_ON(sector != blk_rq_pos(__rq));  
  155.   
  156.             if (elv_rq_merge_ok(__rq, bio)) {//判断是否可以对该request安全的进行merge操作  
  157.                 ret = ELEVATOR_FRONT_MERGE;//可以进行前向merge操作  
  158.                 goto out;  
  159.             }  
  160.         }  
  161.     }  
  162.   
  163.     return ELEVATOR_NO_MERGE;  
  164.     /* 
  165.     返回ELEVATOR_NO_MERGE有三种情况: 
  166.     1、front_merges被设置为0 
  167.     2、front_merges被设置为1,但是不存在与bio连续的request 
  168.     3、front_merges被设置为1,也存在与bio连续的request,但是不能针对该request进行merge操作 
  169.     */  
  170. out:  
  171.     *req = __rq;//将执行merge操作的request  
  172.     return ret;  
  173. }  
  174.   
  175. static void deadline_merged_request(struct request_queue *q,  
  176.                     struct request *req, int type)  
  177. {  
  178.     struct deadline_data *dd = q->elevator->elevator_data;  
  179.   
  180.     /* 
  181.      * if the merge was a front merge, we need to reposition request 
  182.      */  
  183.     if (type == ELEVATOR_FRONT_MERGE) {  
  184.         /* 
  185.         如果做了一个前向merge,需要将那个request先删除,然后又插入, 
  186.         是为了继续进行有可能的前向merge。 
  187.         */  
  188.         elv_rb_del(deadline_rb_root(dd, req), req);  
  189.         deadline_add_rq_rb(dd, req);  
  190.     }  
  191. }  
  192.   
  193. //删除合并之后的另一个请求next  
  194. static void  
  195. deadline_merged_requests(struct request_queue *q, struct request *req,  
  196.              struct request *next)  
  197. {  
  198.     /* 
  199.      * if next expires before rq, assign its expire time to rq 
  200.      * and move into next position (next will be deleted) in fifo 
  201.      */  
  202.     if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {  
  203.         if (time_before(rq_fifo_time(next), rq_fifo_time(req))) {  
  204.             list_move(&req->queuelist, &next->queuelist);  
  205.             rq_set_fifo_time(req, rq_fifo_time(next));  
  206.         }  
  207.     }  
  208.   
  209.     /* 
  210.      * kill knowledge of next, this one is a goner 
  211.      */  
  212.     deadline_remove_request(q, next);  
  213. }  
  214.   
  215. /* 
  216.  * move request from sort list to dispatch queue. 
  217.  */  
  218. static inline void  
  219. deadline_move_to_dispatch(struct deadline_data *dd, struct request *rq)  
  220. {  
  221.     struct request_queue *q = rq->q;  
  222.   
  223.     deadline_remove_request(q, rq);//将请求从deadline调度器的sort_list 和 fifo_list中删除  
  224.     elv_dispatch_add_tail(q, rq);//将请求加入块设备的请求队列的尾部  
  225. }  
  226.   
  227. /* 
  228.  * move an entry to dispatch queue 
  229.  */  
  230. static void  
  231. deadline_move_request(struct deadline_data *dd, struct request *rq)  
  232. {  
  233.     const int data_dir = rq_data_dir(rq);  
  234.   
  235.     dd->next_rq[READ] = NULL;  
  236.     dd->next_rq[WRITE] = NULL;  
  237.     dd->next_rq[data_dir] = deadline_latter_request(rq);  
  238.   
  239.     dd->last_sector = rq_end_sector(rq);  
  240.   
  241.     /* 
  242.      * take it off the sort and fifo list, move 
  243.      * to dispatch queue 
  244.      */  
  245.     deadline_move_to_dispatch(dd, rq);  
  246. }  
  247.   
  248. /* 
  249.  * deadline_check_fifo returns 0 if there are no expired requests on the fifo, 
  250.  * 1 otherwise. Requires !list_empty(&dd->fifo_list[data_dir]) 
  251.  */  
  252.   
  253. //判断fifo_list中的第一个请求是否超时  
  254. static inline int deadline_check_fifo(struct deadline_data *dd, int ddir)  
  255. {  
  256.     struct request *rq = rq_entry_fifo(dd->fifo_list[ddir].next);  
  257.   
  258.     /* 
  259.      * rq is expired! 
  260.      */  
  261.     if (time_after(jiffies, rq_fifo_time(rq)))  
  262.         return 1;  
  263.   
  264.     return 0;  
  265. }  
  266.   
  267. /* 
  268.  * deadline_dispatch_requests selects the best request according to 
  269.  * read/write expire, fifo_batch, etc 
  270.  */  
  271. static int deadline_dispatch_requests(struct request_queue *q, int force)  
  272. {  
  273.     struct deadline_data *dd = q->elevator->elevator_data;  
  274.     const int reads = !list_empty(&dd->fifo_list[READ]);  
  275.     const int writes = !list_empty(&dd->fifo_list[WRITE]);  
  276.     struct request *rq;  
  277.     int data_dir;  
  278.   
  279.     /* 
  280.      * batches are currently reads XOR writes 
  281.      */  
  282.     if (dd->next_rq[WRITE])  
  283.         rq = dd->next_rq[WRITE];  
  284.     else  
  285.         rq = dd->next_rq[READ];  
  286.   
  287.     if (rq && dd->batching < dd->fifo_batch)//存在请求,并且连续提交的请求数没有超过上限,可以继续分发请求  
  288.         /* we have a next request are still entitled to batch */  
  289.         goto dispatch_request;  
  290.   
  291.     /* 
  292.      * at this point we are not running a batch. select the appropriate 
  293.      * data direction (read / write) 
  294.      */  
  295.   
  296.     if (reads) {//存在读请求,优先处理读请求  
  297.         BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));  
  298.   
  299.         if (writes && (dd->starved++ >= dd->writes_starved))  
  300.             //如果存在写请求,并且饥饿写请求的次数已达上限,就需要分发写请求  
  301.             goto dispatch_writes;  
  302.   
  303.         data_dir = READ;  
  304.   
  305.         goto dispatch_find_request;  
  306.     }  
  307.   
  308.     /* 
  309.      * there are either no reads or writes have been starved 
  310.      */  
  311.   
  312.     if (writes) {//没有读请求,或者写请求被饥饿的次数已达上限,就要处理写请求  
  313. dispatch_writes:  
  314.         BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[WRITE]));  
  315.   
  316.         dd->starved = 0;//进行一次写请求处理之后,就需要重新设置写请求被饥饿的次数为0  
  317.   
  318.         data_dir = WRITE;  
  319.   
  320.         goto dispatch_find_request;  
  321.     }  
  322.   
  323.     return 0;//如果既没有读请求,又没有写请求,就返回0  
  324.   
  325. dispatch_find_request:  
  326.     /* 
  327.      * we are not running a batch, find best request for selected data_dir 
  328.      */  
  329.     if (deadline_check_fifo(dd, data_dir) || !dd->next_rq[data_dir]) {  
  330.         /* 
  331.         如果本次处理方向的fifo_list中有超时请求,或者本次处理方向扫描到电梯尾, 
  332.         都会返回来处理等待最久的request,并从这个request开始继续进行电梯扫描 
  333.         */  
  334.         /* 
  335.          * A deadline has expired, the last request was in the other 
  336.          * direction, or we have run out of higher-sectored requests. 
  337.          * Start again from the request with the earliest expiry time. 
  338.          */  
  339.         rq = rq_entry_fifo(dd->fifo_list[data_dir].next);  
  340.     } else {  
  341.         /* 
  342.         如果既没有发生超时,也没有扫描到电梯末尾, 
  343.         则沿sector递增方向上的下一个request就是当前要处理的request 
  344.         */  
  345.         /* 
  346.          * The last req was the same dir and we have a next request in 
  347.          * sort order. No expired requests so continue on from here. 
  348.          */  
  349.         rq = dd->next_rq[data_dir];  
  350.     }  
  351.   
  352.     dd->batching = 0;//进行一次处理之后,需要重新设置batching值  
  353.   
  354. dispatch_request:  
  355.     /* 
  356.      * rq is the selected appropriate request. 
  357.      */  
  358.     dd->batching++;  
  359.     deadline_move_request(dd, rq);  
  360.   
  361.     return 1;  
  362. }  
  363.   
  364. //判断deadline调度器中是否存在请求  
  365. static int deadline_queue_empty(struct request_queue *q)  
  366. {  
  367.     struct deadline_data *dd = q->elevator->elevator_data;  
  368.   
  369.     return list_empty(&dd->fifo_list[WRITE])  
  370.         && list_empty(&dd->fifo_list[READ]);  
  371. }  
  372.   
  373. static void deadline_exit_queue(struct elevator_queue *e)  
  374. {  
  375.     struct deadline_data *dd = e->elevator_data;  
  376.   
  377.     BUG_ON(!list_empty(&dd->fifo_list[READ]));  
  378.     BUG_ON(!list_empty(&dd->fifo_list[WRITE]));  
  379.   
  380.     kfree(dd);  
  381. }  
  382.   
  383. /* 
  384.  * initialize elevator private data (deadline_data). 
  385.  */  
  386. static void *deadline_init_queue(struct request_queue *q)  
  387. {  
  388.     struct deadline_data *dd;  
  389.   
  390.     dd = kmalloc_node(sizeof(*dd), GFP_KERNEL | __GFP_ZERO, q->node);  
  391.     if (!dd)  
  392.         return NULL;  
  393.   
  394.     INIT_LIST_HEAD(&dd->fifo_list[READ]);  
  395.     INIT_LIST_HEAD(&dd->fifo_list[WRITE]);  
  396.     dd->sort_list[READ] = RB_ROOT;  
  397.     dd->sort_list[WRITE] = RB_ROOT;  
  398.     dd->fifo_expire[READ] = read_expire;  
  399.     dd->fifo_expire[WRITE] = write_expire;  
  400.     dd->writes_starved = writes_starved;  
  401.     dd->front_merges = 1;  
  402.     dd->fifo_batch = fifo_batch;  
  403.     return dd;  
  404. }  
  405.   
  406. /* 
  407.  * sysfs parts below 
  408.  */  
  409.   
  410. static ssize_t  
  411. deadline_var_show(int var, char *page)  
  412. {  
  413.     return sprintf(page, "%d\n", var);  
  414. }  
  415.   
  416. static ssize_t  
  417. deadline_var_store(int *var, const char *page, size_t count)  
  418. {  
  419.     char *p = (char *) page;  
  420.   
  421.     *var = simple_strtol(p, &p, 10);  
  422.     return count;  
  423. }  
  424.   
  425. #define SHOW_FUNCTION(__FUNC, __VAR, __CONV)                \  
  426. static ssize_t __FUNC(struct elevator_queue *e, char *page)     \  
  427. {                                   \  
  428.     struct deadline_data *dd = e->elevator_data;         \  
  429.     int __data = __VAR;                     \  
  430.     if (__CONV)                         \  
  431.         __data = jiffies_to_msecs(__data);          \  
  432.     return deadline_var_show(__data, (page));           \  
  433. }  
  434. SHOW_FUNCTION(deadline_read_expire_show, dd->fifo_expire[READ], 1);  
  435. SHOW_FUNCTION(deadline_write_expire_show, dd->fifo_expire[WRITE], 1);  
  436. SHOW_FUNCTION(deadline_writes_starved_show, dd->writes_starved, 0);  
  437. SHOW_FUNCTION(deadline_front_merges_show, dd->front_merges, 0);  
  438. SHOW_FUNCTION(deadline_fifo_batch_show, dd->fifo_batch, 0);  
  439. #undef SHOW_FUNCTION  
  440.   
  441. #define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV)         \  
  442. static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \  
  443. {                                   \  
  444.     struct deadline_data *dd = e->elevator_data;         \  
  445.     int __data;                         \  
  446.     int ret = deadline_var_store(&__data, (page), count);       \  
  447.     if (__data < (MIN))                      \  
  448.         __data = (MIN);                     \  
  449.     else if (__data > (MAX))                 \  
  450.         __data = (MAX);                     \  
  451.     if (__CONV)                         \  
  452.         *(__PTR) = msecs_to_jiffies(__data);            \  
  453.     else                                \  
  454.         *(__PTR) = __data;                  \  
  455.     return ret;                         \  
  456. }  
  457. STORE_FUNCTION(deadline_read_expire_store, &dd->fifo_expire[READ], 0, INT_MAX, 1);  
  458. STORE_FUNCTION(deadline_write_expire_store, &dd->fifo_expire[WRITE], 0, INT_MAX, 1);  
  459. STORE_FUNCTION(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX, 0);  
  460. STORE_FUNCTION(deadline_front_merges_store, &dd->front_merges, 0, 1, 0);  
  461. STORE_FUNCTION(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX, 0);  
  462. #undef STORE_FUNCTION  
  463.   
  464. #define DD_ATTR(name) \  
  465.     __ATTR(name, S_IRUGO|S_IWUSR, deadline_##name##_show, \  
  466.                       deadline_##name##_store)  
  467.   
  468. static struct elv_fs_entry deadline_attrs[] = {  
  469.     DD_ATTR(read_expire),  
  470.     DD_ATTR(write_expire),  
  471.     DD_ATTR(writes_starved),  
  472.     DD_ATTR(front_merges),  
  473.     DD_ATTR(fifo_batch),  
  474.     __ATTR_NULL  
  475. };  
  476.   
  477. static struct elevator_type iosched_deadline = {  
  478.     .ops = {  
  479.         .elevator_merge_fn =        deadline_merge,  
  480.         .elevator_merged_fn =       deadline_merged_request,  
  481.         .elevator_merge_req_fn =    deadline_merged_requests,  
  482.         .elevator_dispatch_fn =     deadline_dispatch_requests,  
  483.         .elevator_add_req_fn =      deadline_add_request,  
  484.         .elevator_queue_empty_fn =  deadline_queue_empty,  
  485.         .elevator_former_req_fn =   elv_rb_former_request,  
  486.         .elevator_latter_req_fn =   elv_rb_latter_request,  
  487.         .elevator_init_fn =     deadline_init_queue,  
  488.         .elevator_exit_fn =     deadline_exit_queue,  
  489.     },  
  490.   
  491.     .elevator_attrs = deadline_attrs,  
  492.     .elevator_name = "deadline",  
  493.     .elevator_owner = THIS_MODULE,  
  494. };  
  495.   
  496. static int __init deadline_init(void)  
  497. {  
  498.     elv_register(&iosched_deadline);//注册一个调度器类型  
  499.   
  500.     return 0;  
  501. }  
  502.   
  503. static void __exit deadline_exit(void)  
  504. {  
  505.     elv_unregister(&iosched_deadline);  
  506. }  
  507.   
  508. module_init(deadline_init);  
  509. module_exit(deadline_exit);  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值