最后要分析的一个函数,也是最重要的一个--deadline_dispatch_requests 调度器如何选择request,分派给request_queue
static int deadline_dispatch_requests(struct request_queue *q, int force)
{
struct deadline_data *dd = q->elevator->elevator_data;
//确定读写fifo的状态
const int reads = !list_empty(&dd->fifo_list[READ]);
const int writes = !list_empty(&dd->fifo_list[WRITE]);
struct request *rq;
int data_dir;
/*
* batches are currently reads XOR writes
*/
//如果next_rq中指定了rq,则据此确定下一个分派的rq对象
if (dd->next_rq[WRITE])
rq = dd->next_rq[WRITE];
else
rq = dd->next_rq[READ];
//指定了rq并且当前的batching数小于预定的batch数值,则进行分派操作
if (rq && dd->batching < dd->fifo_batch)
/* we have a next request are still entitled to batch */
goto dispatch_request;
/*
* at this point we are not running a batch. select the appropriate
* data direction (read / write)
*/
/*到了这里,说明没有指定rq,也就是说要进行权衡来选择分派哪一个rq
首先要选择方向,即选定读请求还是写请求*/
if (reads) {//读请求fifo不为空
BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));
//如果写请求fifo也不为空,并且当前的starved数值已经超过了writes_starved的数值,
//也就是说之前已经连续处理了starved个读请求了,超过了写请求的饿死线,则选择
//分派一个写请求
if (writes && (dd->starved++ >= dd->writes_starved))
goto dispatch_writes;
data_dir = READ;//确定下一个请求的读写方向为读
goto dispatch_find_request;
}
/*
* there are either no reads or writes have been starved
*/
if (writes) {
dispatch_writes://走到这里,说明没有读请求,或者写请求处于饿死状态,必须被处理
BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[WRITE]));
dd->starved = 0;//starved重置为0
data_dir = WRITE;//确定下一个请求的读写方向为写
goto dispatch_find_request;
}
return 0;
dispatch_find_request://这里开始重新确定下一批连续分派的rq的第一个rq
/*
* we are not running a batch, find best request for selected data_dir
*/
/*如果jiffies已超过相应fifo list中的第一个rq的期限 或者
上一个请求和现在的请求方向是相反的,则取fifo list中的饥饿rq
一种可能的情况就是读请求过多,导致写请求饿死*/
if (deadline_check_fifo(dd, data_dir) || !dd->next_rq[data_dir]) {
/*
* A deadline has expired, the last request was in the other
* direction, or we have run out of higher-sectored requests.
* Start again from the request with the earliest expiry time.
*/
rq = rq_entry_fifo(dd->fifo_list[data_dir].next);
} else {//否则,从扇区连续性的角度考虑,接着上次分派的rq继续下一个rq
/*
* The last req was the same dir and we have a next request in
* sort order. No expired requests so continue on from here.
*/
rq = dd->next_rq[data_dir];
}
dd->batching = 0;
dispatch_request:
/*
* rq is the selected appropriate request.
*/
dd->batching++;
deadline_move_request(dd, rq);
return 1;
}
deadline_check_fifo()检查jiffies是否超过了请求的期限值
deadline_move_request()根据要分派的rq设置下一个待分派的rq,并且调用deadline_move_to_dispatch()进行分派工作
<SPAN style="FONT-SIZE: 12px">static void
deadline_move_request(struct deadline_data *dd, struct request *rq)
{
const int data_dir = rq_data_dir(rq);
/*先将next_rq都置空*/
dd->next_rq[READ] = NULL;
dd->next_rq[WRITE] = NULL;
//设置相应传输方向的下一个rq,deadline_latter_request()取红黑树中大于rq的下一个节点
dd->next_rq[data_dir] = deadline_latter_request(rq);
dd->last_sector = rq_end_sector(rq);
/*
* take it off the sort and fifo list, move
* to dispatch queue
*/
deadline_move_to_dispatch(dd, rq);
}
static inline void
deadline_move_to_dispatch(struct deadline_data *dd, struct request *rq)
{
struct request_queue *q = rq->q;
deadline_remove_request(q, rq);//将rq从fifo_list和sort_list中删除
elv_dispatch_add_tail(q, rq);//添加至所属的request_queue
}
至此,Deadline Scheduler分析完毕!
