如果找到了这样的队列,则在cfq_set_active_queue()函数中设定该队列为运行队列,否则就从service_tree中调度时间点最邻近的队列
static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
struct cfq_queue *cfqq)
{
if (!cfqq)//假如没指定cfqq,则从service_tree中最前的节点
cfqq = cfq_get_next_queue(cfqd);
__cfq_set_active_queue(cfqd, cfqq);//设定active_queue为cfqq
return cfqq;
}
队列选择完毕,下面要通过cfq_dispatch_request()函数在队列中选择合适的request进行发送
static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
{
struct request *rq;
BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
//先判断是否确定发放cfqq里的请求
if (!cfq_may_dispatch(cfqd, cfqq))
return false;
/*
* follow expired path, else get first next available
*/
/*这里先检查fifo中的请求,如果fifo为空或者fifo中第一个请求的期限还没到,则不会获取到request*/
rq = cfq_check_fifo(cfqq);
if (!rq)//如果fifo中获取rq失败,则考虑备选的next_rq,next_rq总是从扇区的连续性上考虑的
rq = cfqq->next_rq;
/*
* insert request into driver dispatch list
*/
cfq_dispatch_insert(cfqd->queue, rq);//将rq插入到设备的请求队列
if (!cfqd->active_cic) {
struct cfq_io_context *cic = RQ_CIC(rq);
atomic_long_inc(&cic->ioc->refcount);
cfqd->active_cic = cic;
}
return true;
}
在发送具体的request之前,先要确定是否适合为该队列发放请求,主要是判断队列在时间片内发送的请求数有没有超额
static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
{
unsigned int max_dispatch;
/*
* Drain async requests before we start sync IO
*/
//如果cfqq可以被idle并且设备有异步请求处理,则不进行新的同步请求的发放
if (cfq_cfqq_idle_window(cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
return false;
/*
* If this is an async queue and we have sync IO in flight, let it wait
*/
//如果发放的是异步请求并且request_queue中还有同步请求在等待提交,则不方法
if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
return false;
//默认max_dispatch为预定的cfq_quantum,也就是说cfqq在时间片内最多dispatch
//cfq_quantum个请求
max_dispatch = cfqd->cfq_quantum;
//如果cfqq的优先级类为idle,则在时间片内只能dispatch一个请求
if (cfq_class_idle(cfqq))
max_dispatch = 1;
/*
* Does this cfqq already have too much IO in flight?
*/
//如果队列发放下去的请求数超额
if (cfqq->dispatched >= max_dispatch) {
/*
* idle queue must always only have a single IO in flight
*/
if (cfq_class_idle(cfqq))//如果是idle优先级,则不能进行发放
return false;
/*
* We have other queues, don't allow more IO from this one
*/
if (cfqd->busy_queues > 1)//如果有其他的队列在等待发放请求,则不进行发放
return false;
/*
* Sole queue user, allow bigger slice
*/
//如果service_tree中只有该队列,且优先级高于idle,则扩宽max_dispatch的限制
max_dispatch *= 4;
}
/*
* Async queues must wait a bit before being allowed dispatch.
* We also ramp up the dispatch depth gradually for async IO,
* based on the last sync IO we serviced
*/
/*下面根据最后一次发送的同步请求和现在的时间间隔以及同步请求时间片的值,计算出
depth,根据depth重置max_dispatch,这里的意图应该是在从同步请求的发送转为
异步请求的发送时,延迟一下异步请求的发送*/
if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
unsigned int depth;
depth = last_sync / cfqd->cfq_slice[1];
if (!depth && !cfqq->dispatched)
depth = 1;
if (depth < max_dispatch)
max_dispatch = depth;
}
/*
* If we're below the current max, allow a dispatch
*/
return cfqq->dispatched < max_dispatch;
}
通过检查后,则要正式开始选择request,选择的原则是:如果fifo中的第一个请求期限已到,则选择该请求,否则选择next_rq指针指定的请求,该指针指向的请求总是从物理扇区的连续性上考虑的。最后调用cfq_dispatch_insert()函数将request插入到request_queue中去,等待块设备的响应
static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
{
struct cfq_data *cfqd = q->elevator->elevator_data;
struct cfq_queue *cfqq = RQ_CFQQ(rq);
cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);//选择一个扇区间隙最近的请求赋给next_rq作为下一个请求的备选
cfq_remove_request(rq);//将rq从sort_list和fifo中移除
cfqq->dispatched++;
elv_dispatch_sort(q, rq);//将rq分派到request queue的请求队列等候响应
if (cfq_cfqq_sync(cfqq))
cfqd->sync_flight++;
}
