Linux IO Scheduler--CFQ(下)

前文介绍了CFQ调度器的一些概念和结构之间的关系,这里再结合实际的代码,来分析CFQ的工作流程。CFQ调度器的定义如下:
[cpp] view plain copy print ?
  1. static struct elevator_type iosched_cfq = {
  2. .ops = {
  3. .elevator_merge_fn = cfq_merge,
  4. .elevator_merged_fn = cfq_merged_request,
  5. .elevator_merge_req_fn = cfq_merged_requests,
  6. .elevator_allow_merge_fn = cfq_allow_merge,
  7. .elevator_dispatch_fn = cfq_dispatch_requests,
  8. .elevator_add_req_fn = cfq_insert_request,
  9. .elevator_activate_req_fn = cfq_activate_request,
  10. .elevator_deactivate_req_fn = cfq_deactivate_request,
  11. .elevator_queue_empty_fn = cfq_queue_empty,
  12. .elevator_completed_req_fn = cfq_completed_request,
  13. .elevator_former_req_fn = elv_rb_former_request,
  14. .elevator_latter_req_fn = elv_rb_latter_request,
  15. .elevator_set_req_fn = cfq_set_request,
  16. .elevator_put_req_fn = cfq_put_request,
  17. .elevator_may_queue_fn = cfq_may_queue,
  18. .elevator_init_fn = cfq_init_queue,
  19. .elevator_exit_fn = cfq_exit_queue,
  20. .trim = cfq_free_io_context,
  21. },
  22. .elevator_attrs = cfq_attrs,
  23. .elevator_name = "cfq",
  24. .elevator_owner = THIS_MODULE,
  25. };
static struct elevator_type iosched_cfq = {
	.ops = {
		.elevator_merge_fn = 		cfq_merge,
		.elevator_merged_fn =		cfq_merged_request,
		.elevator_merge_req_fn =	cfq_merged_requests,
		.elevator_allow_merge_fn =	cfq_allow_merge,
		.elevator_dispatch_fn =		cfq_dispatch_requests,
		.elevator_add_req_fn =		cfq_insert_request,
		.elevator_activate_req_fn =	cfq_activate_request,
		.elevator_deactivate_req_fn =	cfq_deactivate_request,
		.elevator_queue_empty_fn =	cfq_queue_empty,
		.elevator_completed_req_fn =	cfq_completed_request,
		.elevator_former_req_fn =	elv_rb_former_request,
		.elevator_latter_req_fn =	elv_rb_latter_request,
		.elevator_set_req_fn =		cfq_set_request,
		.elevator_put_req_fn =		cfq_put_request,
		.elevator_may_queue_fn =	cfq_may_queue,
		.elevator_init_fn =		cfq_init_queue,
		.elevator_exit_fn =		cfq_exit_queue,
		.trim =				cfq_free_io_context,
	},
	.elevator_attrs =	cfq_attrs,
	.elevator_name =	"cfq",
	.elevator_owner =	THIS_MODULE,
};

可以看到CFQ调度器涉及到的操作函数还是比较多的,这里我只打算选一些和提交bio以及request相关的函数进行分析。在提交bio的时候,如果在通用层寻找可以合并bio的途径失败,要通过cfq_merge()来判断是否能够将bio插入到某个request的bio链表首部

[cpp] view plain copy print ?
  1. static struct request *
  2. cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
  3. {
  4. struct task_struct *tsk = current;
  5. struct cfq_io_context *cic;
  6. struct cfq_queue *cfqq;
  7. //在进程的io_context中,找到进程特定于块设备的cfq_io_context
  8. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  9. if (!cic)
  10. return NULL;
  11. //根据同步还是异步,确定cfq_queue
  12. cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
  13. if (cfqq) {
  14. sector_t sector = bio->bi_sector + bio_sectors(bio);//得到末尾扇区号
  15. //从cfq_queue的红黑树中查找对应的节点
  16. return elv_rb_find(&cfqq->sort_list, sector);
  17. }
  18. return NULL;
  19. }
static struct request *
cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
{
	struct task_struct *tsk = current;
	struct cfq_io_context *cic;
	struct cfq_queue *cfqq;

	//在进程的io_context中,找到进程特定于块设备的cfq_io_context
	cic = cfq_cic_lookup(cfqd, tsk->io_context);
	if (!cic)
		return NULL;

	//根据同步还是异步,确定cfq_queue
	cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
	if (cfqq) {
		sector_t sector = bio->bi_sector + bio_sectors(bio);//得到末尾扇区号

		//从cfq_queue的红黑树中查找对应的节点
		return elv_rb_find(&cfqq->sort_list, sector);
	}

	return NULL;
}

cfq_find_rq_fmerge()进行实际的搜索工作,要确定bio的归属request,必须先确定进程的通信对象是谁(因为一个进程有可能和多个块设备通信),也就是要找到进程对应的cfq_io_context结构,其中包含了进程的同步请求队列和异步请求队列的地址,只要找到了相应的cfq_io_context,就可以通过bio的同异步性确定对应的cfq_queue了,最后再判断对应的cfq_queue中是否存在可以容纳bio的request。推导cfq_io_context的关键在于以块设备CFQ调度器的描述结构cfq_data的地址为关键值,在进程的io_context的基数树中进行搜索

[html] view plain copy print ?
  1. <SPAN style="FONT-SIZE: 12px">static struct request *
  2. cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
  3. {
  4. struct task_struct *tsk = current;
  5. struct cfq_io_context *cic;
  6. struct cfq_queue *cfqq;
  7. //在进程的io_context中,找到进程特定于块设备的cfq_io_context
  8. cic = cfq_cic_lookup(cfqd, tsk->io_context);
  9. if (!cic)
  10. return NULL;
  11. //根据同步还是异步,确定cfq_queue
  12. cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
  13. if (cfqq) {
  14. sector_t sector = bio->bi_sector + bio_sectors(bio);//得到末尾扇区号
  15. //从cfq_queue的红黑树中查找对应的节点
  16. return elv_rb_find(&cfqq->sort_list, sector);
  17. }
  18. return NULL;
  19. }
  20. </SPAN>
static struct request *
cfq_find_rq_fmerge(struct cfq_data *cfqd, struct bio *bio)
{
	struct task_struct *tsk = current;
	struct cfq_io_context *cic;
	struct cfq_queue *cfqq;

	//在进程的io_context中,找到进程特定于块设备的cfq_io_context
	cic = cfq_cic_lookup(cfqd, tsk->io_context);
	if (!cic)
		return NULL;

	//根据同步还是异步,确定cfq_queue
	cfqq = cic_to_cfqq(cic, cfq_bio_sync(bio));
	if (cfqq) {
		sector_t sector = bio->bi_sector + bio_sectors(bio);//得到末尾扇区号

		//从cfq_queue的红黑树中查找对应的节点
		return elv_rb_find(&cfqq->sort_list, sector);
	}

	return NULL;
}


通过基数树寻找对应设备的cfq_io_context:

[cpp] view plain copy print ?
  1. <SPAN style="FONT-SIZE: 12px">static struct cfq_io_context *
  2. cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
  3. {
  4. struct cfq_io_context *cic;
  5. unsigned long flags;
  6. void *k;
  7. if (unlikely(!ioc))
  8. return NULL;
  9. rcu_read_lock();
  10. /*
  11. * we maintain a last-hit cache, to avoid browsing over the tree
  12. */
  13. //由于进程很有可能连续访问同一块设备,因此先将cic中的关键值直接与cfqd比较
  14. cic = rcu_dereference(ioc->ioc_data);
  15. if (cic && cic->key == cfqd) {
  16. rcu_read_unlock();
  17. return cic;
  18. }
  19. do {//在进程io_context的基数树中寻找对应访问的块设备的cfq_data结构
  20. cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
  21. rcu_read_unlock();
  22. if (!cic)
  23. break;
  24. /* ->key must be copied to avoid race with cfq_exit_queue() */
  25. k = cic->key;
  26. if (unlikely(!k)) {
  27. cfq_drop_dead_cic(cfqd, ioc, cic);
  28. rcu_read_lock();
  29. continue;
  30. }
  31. //保存cic到ioc->ioc_data
  32. spin_lock_irqsave(&ioc->lock, flags);
  33. rcu_assign_pointer(ioc->ioc_data, cic);
  34. spin_unlock_irqrestore(&ioc->lock, flags);
  35. break;
  36. } while (1);
  37. return cic;
  38. }</SPAN>
static struct cfq_io_context *
cfq_cic_lookup(struct cfq_data *cfqd, struct io_context *ioc)
{
	struct cfq_io_context *cic;
	unsigned long flags;
	void *k;

	if (unlikely(!ioc))
		return NULL;

	rcu_read_lock();

	/*
	 * we maintain a last-hit cache, to avoid browsing over the tree
	 */

	//由于进程很有可能连续访问同一块设备,因此先将cic中的关键值直接与cfqd比较
	cic = rcu_dereference(ioc->ioc_data);
	if (cic && cic->key == cfqd) {
		rcu_read_unlock();
		return cic;
	}

	do {//在进程io_context的基数树中寻找对应访问的块设备的cfq_data结构
		cic = radix_tree_lookup(&ioc->radix_root, (unsigned long) cfqd);
		rcu_read_unlock();
		if (!cic)
			break;
		/* ->key must be copied to avoid race with cfq_exit_queue() */
		k = cic->key;
		if (unlikely(!k)) {
			cfq_drop_dead_cic(cfqd, ioc, cic);
			rcu_read_lock();
			continue;
		}

		//保存cic到ioc->ioc_data
		spin_lock_irqsave(&ioc->lock, flags);
		rcu_assign_pointer(ioc->ioc_data, cic);
		spin_unlock_irqrestore(&ioc->lock, flags);
		break;
	} while (1);

	return cic;
}

在通用层__make_request()函数中,如果bio找不到接收对象,那么就要重新创建一个request来接收它。分配一个request必须进行初始化,需要调用cfq_set_request()函数。和前面的过程比较类似的是,request要先找到接收它的cfq_queue,然后这里出现了一个问题,如果这个request是进程的第一个同步请求或者异步请求,那么就要分配新的cfq_queue来接收request。同步请求和异步请求的处理情况有点区别,因为进程独自拥有自己的同步请求队列,而进程的异步请求都是共享cfq_data中的异步请求队列的,所以只有当request是同步请求时才需要进行cfq_queue的分配

[cpp] view plain copy print ?
  1. static int
  2. cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
  3. {
  4. struct cfq_data *cfqd = q->elevator->elevator_data;
  5. struct cfq_io_context *cic;
  6. const int rw = rq_data_dir(rq);
  7. const bool is_sync = rq_is_sync(rq);
  8. struct cfq_queue *cfqq;
  9. unsigned long flags;
  10. might_sleep_if(gfp_mask & __GFP_WAIT);
  11. //获取进程特定于块设备的cfq_io_context结构
  12. cic = cfq_get_io_context(cfqd, gfp_mask);
  13. spin_lock_irqsave(q->queue_lock, flags);
  14. if (!cic)
  15. goto queue_fail;
  16. new_queue:
  17. cfqq = cic_to_cfqq(cic, is_sync);//根据同异步情况,获取进程中对应的cfq_queue
  18. if (!cfqq || cfqq == &cfqd->oom_cfqq) {//如果还没有相应的cfqq则进行分配
  19. cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);//分配cfq_queue
  20. cic_set_cfqq(cic, cfqq, is_sync);//设置cic->cfqq[is_sync] = cfqq
  21. } else {
  22. /*
  23. * If the queue was seeky for too long, break it apart.
  24. */
  25. if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
  26. cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
  27. cfqq = split_cfqq(cic, cfqq);
  28. if (!cfqq)
  29. goto new_queue;
  30. }
  31. /*
  32. * Check to see if this queue is scheduled to merge with
  33. * another, closely cooperating queue. The merging of
  34. * queues happens here as it must be done in process context.
  35. * The reference on new_cfqq was taken in merge_cfqqs.
  36. */
  37. if (cfqq->new_cfqq)
  38. cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
  39. }
  40. cfqq->allocated[rw]++;
  41. atomic_inc(&cfqq->ref);
  42. spin_unlock_irqrestore(q->queue_lock, flags);
  43. rq->elevator_private = cic;//保存cfq_io_context到request
  44. rq->elevator_private2 = cfqq;//保存cfq_queue到request
  45. return 0;
  46. queue_fail:
  47. if (cic)
  48. put_io_context(cic->ioc);
  49. cfq_schedule_dispatch(cfqd);
  50. spin_unlock_irqrestore(q->queue_lock, flags);
  51. cfq_log(cfqd, "set_request fail");
  52. return 1;
  53. }
static int
cfq_set_request(struct request_queue *q, struct request *rq, gfp_t gfp_mask)
{
	struct cfq_data *cfqd = q->elevator->elevator_data;
	struct cfq_io_context *cic;
	const int rw = rq_data_dir(rq);
	const bool is_sync = rq_is_sync(rq);
	struct cfq_queue *cfqq;
	unsigned long flags;

	might_sleep_if(gfp_mask & __GFP_WAIT);

	//获取进程特定于块设备的cfq_io_context结构
	cic = cfq_get_io_context(cfqd, gfp_mask);

	spin_lock_irqsave(q->queue_lock, flags);

	if (!cic)
		goto queue_fail;

new_queue:
	cfqq = cic_to_cfqq(cic, is_sync);//根据同异步情况,获取进程中对应的cfq_queue
	if (!cfqq || cfqq == &cfqd->oom_cfqq) {//如果还没有相应的cfqq则进行分配
		cfqq = cfq_get_queue(cfqd, is_sync, cic->ioc, gfp_mask);//分配cfq_queue
		cic_set_cfqq(cic, cfqq, is_sync);//设置cic->cfqq[is_sync] = cfqq
	} else {
		/*
		 * If the queue was seeky for too long, break it apart.
		 */
		if (cfq_cfqq_coop(cfqq) && should_split_cfqq(cfqq)) {
			cfq_log_cfqq(cfqd, cfqq, "breaking apart cfqq");
			cfqq = split_cfqq(cic, cfqq);
			if (!cfqq)
				goto new_queue;
		}

		/*
		 * Check to see if this queue is scheduled to merge with
		 * another, closely cooperating queue.  The merging of
		 * queues happens here as it must be done in process context.
		 * The reference on new_cfqq was taken in merge_cfqqs.
		 */
		if (cfqq->new_cfqq)
			cfqq = cfq_merge_cfqqs(cfqd, cic, cfqq);
	}

	cfqq->allocated[rw]++;
	atomic_inc(&cfqq->ref);

	spin_unlock_irqrestore(q->queue_lock, flags);

	rq->elevator_private = cic;//保存cfq_io_context到request
	rq->elevator_private2 = cfqq;//保存cfq_queue到request
	return 0;

queue_fail:
	if (cic)
		put_io_context(cic->ioc);

	cfq_schedule_dispatch(cfqd);
	spin_unlock_irqrestore(q->queue_lock, flags);
	cfq_log(cfqd, "set_request fail");
	return 1;
}


当一个request创建初始化,就要将其插入到相应的队列中,cfq_insert_request()函数完成这个功能,和deadline调度器相似,request会被放入两个队列里,一个是按照起始扇区号排列的红黑树(sort_list),一个是按响应期限排列的链表(fifo)

[cpp] view plain copy print ?
  1. static void cfq_insert_request(struct request_queue *q, struct request *rq)
  2. {
  3. struct cfq_data *cfqd = q->elevator->elevator_data;
  4. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  5. cfq_log_cfqq(cfqd, cfqq, "insert_request");
  6. cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);//根据ioc设定cfq_queue的优先级类和优先级
  7. cfq_add_rq_rb(rq);//将rq添加到cfq_queue的红黑树
  8. //设置期限值
  9. rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
  10. list_add_tail(&rq->queuelist, &cfqq->fifo);//添加至fifo
  11. cfq_rq_enqueued(cfqd, cfqq, rq);
  12. }
static void cfq_insert_request(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, "insert_request");
	cfq_init_prio_data(cfqq, RQ_CIC(rq)->ioc);//根据ioc设定cfq_queue的优先级类和优先级

	cfq_add_rq_rb(rq);//将rq添加到cfq_queue的红黑树

	//设置期限值
	rq_set_fifo_time(rq, jiffies + cfqd->cfq_fifo_expire[rq_is_sync(rq)]);
	list_add_tail(&rq->queuelist, &cfqq->fifo);//添加至fifo

	cfq_rq_enqueued(cfqd, cfqq, rq);
}


当一个request纳入一个新的bio后,要考虑能否和其他request进行合并,假如是将bio插入到链表的末尾,那么就要在sort_list中获取request后面的一个request(通过函数elv_rb_latter_request()来获取),然后判断前者的结束扇区和前者的起始扇区是否一样,这些工作都是再通用层代码中完成的,当两个request通过了通用层的审核和,并完成合并操作后,将调用cfq_merged_requests进行一些针对队列的额外工作

[cpp] view plain copy print ?
  1. tatic void
  2. cfq_merged_requests(struct request_queue *q, struct request *rq,
  3. struct request *next)
  4. {
  5. /*
  6. * reposition in fifo if next is older than rq
  7. */
  8. if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
  9. time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
  10. list_move(&rq->queuelist, &next->queuelist);
  11. rq_set_fifo_time(rq, rq_fifo_time(next));
  12. }
  13. cfq_remove_request(next);
  14. }
tatic void
cfq_merged_requests(struct request_queue *q, struct request *rq,
		    struct request *next)
{
	/*
	 * reposition in fifo if next is older than rq
	 */
	if (!list_empty(&rq->queuelist) && !list_empty(&next->queuelist) &&
	    time_before(rq_fifo_time(next), rq_fifo_time(rq))) {
		list_move(&rq->queuelist, &next->queuelist);
		rq_set_fifo_time(rq, rq_fifo_time(next));
	}

	cfq_remove_request(next);
}

这里主要是考察两个request的期限,因为合并时都是将后者合并入前者,因此当后者的期限时间小于前者时,要进行相应的调整,这一点和deadline调度器是一样的。

最后分析的依然是每个调度器最重要的函数--elevator_dispatch_fn

CFQ调度器在发送request到底层块设备时的流程大致如下:

1.选择一个cfq_queue

2.从cfq_queue中选择一个request进行发送

选择cfq_queue的思路如下:

1.如果当前的cfq_queue的时间片还没用完,则继续当前的cfq_queue

2.如果当前的cfq_queue的时间片消耗完了,则优先在对应的prio_tree中选择一个cfq_queue,该cfq_queue的第一个访问扇区与整个调度器最后处理的扇区之间的差值必须小于一个阈值,如果OK的话就选择这个cfq_queue

3.如果找不到这样的cfq_queue,再从service_tree中调度其他的cfq_queue

[cpp] view plain copy print ?
  1. <SPAN style="FONT-SIZE: 12px">static int cfq_dispatch_requests(struct request_queue *q, int force)
  2. {
  3. struct cfq_data *cfqd = q->elevator->elevator_data;
  4. struct cfq_queue *cfqq;
  5. if (!cfqd->busy_queues)
  6. return 0;
  7. if (unlikely(force))
  8. return cfq_forced_dispatch(cfqd);
  9. //先要选择一个队列
  10. cfqq = cfq_select_queue(cfqd);
  11. if (!cfqq)
  12. return 0;
  13. /*
  14. * Dispatch a request from this cfqq, if it is allowed
  15. */
  16. //从选择的cfq_queue中选择request进行分派
  17. if (!cfq_dispatch_request(cfqd, cfqq))
  18. return 0;
  19. cfqq->slice_dispatch++;
  20. cfq_clear_cfqq_must_dispatch(cfqq);
  21. /*
  22. * expire an async queue immediately if it has used up its slice. idle
  23. * queue always expire after 1 dispatch round.
  24. */
  25. /*如果service_tree中有其他队列等待调度,而刚刚发送请求的
  26. 队列是异步队列并且发送数已经超过了时间片的内最大请求数,
  27. 则将异步队列挂起,如果是idle队列则在发送一个请求后直接将队列挂起
  28. */
  29. if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
  30. cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
  31. cfq_class_idle(cfqq))) {
  32. cfqq->slice_end = jiffies + 1;
  33. cfq_slice_expired(cfqd, 0);
  34. }
  35. cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
  36. return 1;
  37. }
  38. </SPAN>
static int cfq_dispatch_requests(struct request_queue *q, int force)
{
	struct cfq_data *cfqd = q->elevator->elevator_data;
	struct cfq_queue *cfqq;

	if (!cfqd->busy_queues)
		return 0;

	if (unlikely(force))
		return cfq_forced_dispatch(cfqd);

	//先要选择一个队列
	cfqq = cfq_select_queue(cfqd);
	if (!cfqq)
		return 0;

	/*
	 * Dispatch a request from this cfqq, if it is allowed
	 */
	 //从选择的cfq_queue中选择request进行分派
	if (!cfq_dispatch_request(cfqd, cfqq))
		return 0;

	cfqq->slice_dispatch++;
	cfq_clear_cfqq_must_dispatch(cfqq);

	/*
	 * expire an async queue immediately if it has used up its slice. idle
	 * queue always expire after 1 dispatch round.
	 */
	 /*如果service_tree中有其他队列等待调度,而刚刚发送请求的
	   队列是异步队列并且发送数已经超过了时间片的内最大请求数,
	   则将异步队列挂起,如果是idle队列则在发送一个请求后直接将队列挂起
	 */
	if (cfqd->busy_queues > 1 && ((!cfq_cfqq_sync(cfqq) &&
	    cfqq->slice_dispatch >= cfq_prio_to_maxrq(cfqd, cfqq)) ||
	    cfq_class_idle(cfqq))) {
		cfqq->slice_end = jiffies + 1;
		cfq_slice_expired(cfqd, 0);
	}

	cfq_log_cfqq(cfqd, cfqq, "dispatched a request");
	return 1;
}


cfq_select_queue()用来选择一个队列,active_queue为当前运行的队列

[cpp] view plain copy print ?
  1. static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
  2. {
  3. struct cfq_queue *cfqq, *new_cfqq = NULL;
  4. /*先检查active_queue*/
  5. cfqq = cfqd->active_queue;
  6. if (!cfqq)//没有指定active_queue则跳转到new_queue去选择新的队列
  7. goto new_queue;
  8. /*
  9. * The active queue has run out of time, expire it and select new.
  10. */
  11. //指定了active_queue,这里检查该队列的时间片是否已经过去
  12. if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
  13. goto expire;
  14. /*
  15. * The active queue has requests and isn't expired, allow it to
  16. * dispatch.
  17. */
  18. /*走到这里表示时间片尚在,这里检查cfq_queue的sort_list是否为空*/
  19. if (!RB_EMPTY_ROOT(&cfqq->sort_list))
  20. goto keep_queue;
  21. /*
  22. * If another queue has a request waiting within our mean seek
  23. * distance, let it run. The expire code will check for close
  24. * cooperators and put the close queue at the front of the service
  25. * tree. If possible, merge the expiring queue with the new cfqq.
  26. */
  27. //走到这里表示active_queue内已经没有请求了,因此要找一个最适合的cfq_queue
  28. new_cfqq = cfq_close_cooperator(cfqd, cfqq);
  29. if (new_cfqq) {
  30. if (!cfqq->new_cfqq)
  31. cfq_setup_merge(cfqq, new_cfqq);
  32. goto expire;
  33. }
  34. /*
  35. * No requests pending. If the active queue still has requests in
  36. * flight or is idling for a new request, allow either of these
  37. * conditions to happen (or time out) before selecting a new queue.
  38. */
  39. if (timer_pending(&cfqd->idle_slice_timer) ||
  40. (cfqq->dispatched && cfq_cfqq_idle_window(cfqq))) {
  41. cfqq = NULL;
  42. goto keep_queue;
  43. }
  44. expire:
  45. cfq_slice_expired(cfqd, 0);//将时间片消耗完的active_queue重新插入service_tree
  46. new_queue:
  47. cfqq = cfq_set_active_queue(cfqd, new_cfqq);//设定new_cfqq为新的active_tree接收时间片
  48. keep_queue:
  49. return cfqq;
  50. }
static struct cfq_queue *cfq_select_queue(struct cfq_data *cfqd)
{
	struct cfq_queue *cfqq, *new_cfqq = NULL;

	/*先检查active_queue*/
	cfqq = cfqd->active_queue;
	if (!cfqq)//没有指定active_queue则跳转到new_queue去选择新的队列
		goto new_queue;

	/*
	 * The active queue has run out of time, expire it and select new.
	 */
	 //指定了active_queue,这里检查该队列的时间片是否已经过去
	if (cfq_slice_used(cfqq) && !cfq_cfqq_must_dispatch(cfqq))
		goto expire;

	/*
	 * The active queue has requests and isn't expired, allow it to
	 * dispatch.
	 */
	 /*走到这里表示时间片尚在,这里检查cfq_queue的sort_list是否为空*/
	if (!RB_EMPTY_ROOT(&cfqq->sort_list))
		goto keep_queue;

	/*
	 * If another queue has a request waiting within our mean seek
	 * distance, let it run.  The expire code will check for close
	 * cooperators and put the close queue at the front of the service
	 * tree.  If possible, merge the expiring queue with the new cfqq.
	 */
	 //走到这里表示active_queue内已经没有请求了,因此要找一个最适合的cfq_queue
	new_cfqq = cfq_close_cooperator(cfqd, cfqq);
	if (new_cfqq) {
		if (!cfqq->new_cfqq)
			cfq_setup_merge(cfqq, new_cfqq);
		goto expire;
	}

	/*
	 * No requests pending. If the active queue still has requests in
	 * flight or is idling for a new request, allow either of these
	 * conditions to happen (or time out) before selecting a new queue.
	 */
	if (timer_pending(&cfqd->idle_slice_timer) ||
	    (cfqq->dispatched && cfq_cfqq_idle_window(cfqq))) {
		cfqq = NULL;
		goto keep_queue;
	}

expire:
	cfq_slice_expired(cfqd, 0);//将时间片消耗完的active_queue重新插入service_tree
new_queue:
	cfqq = cfq_set_active_queue(cfqd, new_cfqq);//设定new_cfqq为新的active_tree接收时间片
keep_queue:
	return cfqq;
}

cfq_close_cooperator查找一个在扇区地址上满足要求的队列,这个操作只对同步请求队列有效

[cpp] view plain copy print ?
  1. static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
  2. struct cfq_queue *cur_cfqq)
  3. {
  4. struct cfq_queue *cfqq;
  5. if (!cfq_cfqq_sync(cur_cfqq))
  6. return NULL;
  7. if (CFQQ_SEEKY(cur_cfqq))
  8. return NULL;
  9. /*
  10. * We should notice if some of the queues are cooperating, eg
  11. * working closely on the same area of the disk. In that case,
  12. * we can group them together and don't waste time idling.
  13. */
  14. //根据扇区的差值,寻找一个最接近的节点
  15. cfqq = cfqq_close(cfqd, cur_cfqq);
  16. if (!cfqq)
  17. return NULL;
  18. /*
  19. * It only makes sense to merge sync queues.
  20. */
  21. if (!cfq_cfqq_sync(cfqq))
  22. return NULL;
  23. if (CFQQ_SEEKY(cfqq))
  24. return NULL;
  25. return cfqq;
  26. }
static struct cfq_queue *cfq_close_cooperator(struct cfq_data *cfqd,
					      struct cfq_queue *cur_cfqq)
{
	struct cfq_queue *cfqq;

	if (!cfq_cfqq_sync(cur_cfqq))
		return NULL;
	if (CFQQ_SEEKY(cur_cfqq))
		return NULL;

	/*
	 * We should notice if some of the queues are cooperating, eg
	 * working closely on the same area of the disk. In that case,
	 * we can group them together and don't waste time idling.
	 */
	 //根据扇区的差值,寻找一个最接近的节点
	cfqq = cfqq_close(cfqd, cur_cfqq);
	if (!cfqq)
		return NULL;

	/*
	 * It only makes sense to merge sync queues.
	 */
	if (!cfq_cfqq_sync(cfqq))
		return NULL;
	if (CFQQ_SEEKY(cfqq))
		return NULL;

	return cfqq;
}

cfqq_close执行实际的工作

[cpp] view plain copy print ?
  1. static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
  2. struct cfq_queue *cur_cfqq)
  3. {
  4. struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
  5. struct rb_node *parent, *node;
  6. struct cfq_queue *__cfqq;
  7. sector_t sector = cfqd->last_position;//这里区最后访问的扇区
  8. if (RB_EMPTY_ROOT(root))
  9. return NULL;
  10. /*
  11. * First, if we find a request starting at the end of the last
  12. * request, choose it.
  13. */
  14. //首先试图在同优先级的红黑树中寻找与最近访问的扇区相接的另一个cfq_queue
  15. __cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
  16. if (__cfqq)//如果找到了的话则选定该cfq_queue进行调度
  17. return __cfqq;
  18. /*
  19. * If the exact sector wasn't found, the parent of the NULL leaf
  20. * will contain the closest sector.
  21. */
  22. //parent保存了最接近的节点的父节点,如果没找到相邻的cfq_queue,获取父节点对应的cfq_queue
  23. __cfqq = rb_entry(parent, struct cfq_queue, p_node);
  24. /*如果__cfqq的下一个派送请求(next_rq指定)的扇区地址和调度器最后派送的请求的结束扇区之间的间隙满足要求,
  25. 则选定该cfq_queue*/
  26. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  27. return __cfqq;
  28. /* 前面都不成功,则根据next_rq和sector的大小关系,
  29. 来选择最接近的比__cfqq的起始扇区大的节点或者小的节点*/
  30. if (blk_rq_pos(__cfqq->next_rq) < sector)
  31. node = rb_next(&__cfqq->p_node);
  32. else
  33. node = rb_prev(&__cfqq->p_node);
  34. if (!node)
  35. return NULL;
  36. //这里再把选择的节点进行一次间隙的判断,如果间隙小于阈值,则选择该节点,否则返回NULL
  37. __cfqq = rb_entry(node, struct cfq_queue, p_node);
  38. if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
  39. return __cfqq;
  40. return NULL;
  41. }
static struct cfq_queue *cfqq_close(struct cfq_data *cfqd,
				    struct cfq_queue *cur_cfqq)
{
	struct rb_root *root = &cfqd->prio_trees[cur_cfqq->org_ioprio];
	struct rb_node *parent, *node;
	struct cfq_queue *__cfqq;
	sector_t sector = cfqd->last_position;//这里区最后访问的扇区

	if (RB_EMPTY_ROOT(root))
		return NULL;

	/*
	 * First, if we find a request starting at the end of the last
	 * request, choose it.
	 */
	 //首先试图在同优先级的红黑树中寻找与最近访问的扇区相接的另一个cfq_queue
	__cfqq = cfq_prio_tree_lookup(cfqd, root, sector, &parent, NULL);
	if (__cfqq)//如果找到了的话则选定该cfq_queue进行调度
		return __cfqq;

	/*
	 * If the exact sector wasn't found, the parent of the NULL leaf
	 * will contain the closest sector.
	 */
	 //parent保存了最接近的节点的父节点,如果没找到相邻的cfq_queue,获取父节点对应的cfq_queue
	__cfqq = rb_entry(parent, struct cfq_queue, p_node);
	
	/*如果__cfqq的下一个派送请求(next_rq指定)的扇区地址和调度器最后派送的请求的结束扇区之间的间隙满足要求,
	  则选定该cfq_queue*/
	if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
		return __cfqq;

	/* 前面都不成功,则根据next_rq和sector的大小关系,
	   来选择最接近的比__cfqq的起始扇区大的节点或者小的节点*/
	if (blk_rq_pos(__cfqq->next_rq) < sector)
		node = rb_next(&__cfqq->p_node);
	else
		node = rb_prev(&__cfqq->p_node);
	if (!node)
		return NULL;

	//这里再把选择的节点进行一次间隙的判断,如果间隙小于阈值,则选择该节点,否则返回NULL
	__cfqq = rb_entry(node, struct cfq_queue, p_node);
	if (cfq_rq_close(cfqd, cur_cfqq, __cfqq->next_rq))
		return __cfqq;

	return NULL;
}

如果找到了这样的队列,则在cfq_set_active_queue()函数中设定该队列为运行队列,否则就从service_tree中调度时间点最邻近的队列

[cpp] view plain copy print ?
  1. static struct cfq_queue *cfq_set_active_queue(struct cfq_data *cfqd,
  2. struct cfq_queue *cfqq)
  3. {
  4. if (!cfqq)//假如没指定cfqq,则从service_tree中最前的节点
  5. cfqq = cfq_get_next_queue(cfqd);
  6. __cfq_set_active_queue(cfqd, cfqq);//设定active_queue为cfqq
  7. return cfqq;
  8. }
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进行发送

[cpp] view plain copy print ?
  1. static bool cfq_dispatch_request(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2. {
  3. struct request *rq;
  4. BUG_ON(RB_EMPTY_ROOT(&cfqq->sort_list));
  5. //先判断是否确定发放cfqq里的请求
  6. if (!cfq_may_dispatch(cfqd, cfqq))
  7. return false;
  8. /*
  9. * follow expired path, else get first next available
  10. */
  11. /*这里先检查fifo中的请求,如果fifo为空或者fifo中第一个请求的期限还没到,则不会获取到request*/
  12. rq = cfq_check_fifo(cfqq);
  13. if (!rq)//如果fifo中获取rq失败,则考虑备选的next_rq,next_rq总是从扇区的连续性上考虑的
  14. rq = cfqq->next_rq;
  15. /*
  16. * insert request into driver dispatch list
  17. */
  18. cfq_dispatch_insert(cfqd->queue, rq);//将rq插入到设备的请求队列
  19. if (!cfqd->active_cic) {
  20. struct cfq_io_context *cic = RQ_CIC(rq);
  21. atomic_long_inc(&cic->ioc->refcount);
  22. cfqd->active_cic = cic;
  23. }
  24. return true;
  25. }
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之前,先要确定是否适合为该队列发放请求,主要是判断队列在时间片内发送的请求数有没有超额

[cpp] view plain copy print ?
  1. static bool cfq_may_dispatch(struct cfq_data *cfqd, struct cfq_queue *cfqq)
  2. {
  3. unsigned int max_dispatch;
  4. /*
  5. * Drain async requests before we start sync IO
  6. */
  7. //如果cfqq可以被idle并且设备有异步请求处理,则不进行新的同步请求的发放
  8. if (cfq_cfqq_idle_window(cfqq) && cfqd->rq_in_driver[BLK_RW_ASYNC])
  9. return false;
  10. /*
  11. * If this is an async queue and we have sync IO in flight, let it wait
  12. */
  13. //如果发放的是异步请求并且request_queue中还有同步请求在等待提交,则不方法
  14. if (cfqd->sync_flight && !cfq_cfqq_sync(cfqq))
  15. return false;
  16. //默认max_dispatch为预定的cfq_quantum,也就是说cfqq在时间片内最多dispatch
  17. //cfq_quantum个请求
  18. max_dispatch = cfqd->cfq_quantum;
  19. //如果cfqq的优先级类为idle,则在时间片内只能dispatch一个请求
  20. if (cfq_class_idle(cfqq))
  21. max_dispatch = 1;
  22. /*
  23. * Does this cfqq already have too much IO in flight?
  24. */
  25. //如果队列发放下去的请求数超额
  26. if (cfqq->dispatched >= max_dispatch) {
  27. /*
  28. * idle queue must always only have a single IO in flight
  29. */
  30. if (cfq_class_idle(cfqq))//如果是idle优先级,则不能进行发放
  31. return false;
  32. /*
  33. * We have other queues, don't allow more IO from this one
  34. */
  35. if (cfqd->busy_queues > 1)//如果有其他的队列在等待发放请求,则不进行发放
  36. return false;
  37. /*
  38. * Sole queue user, allow bigger slice
  39. */
  40. //如果service_tree中只有该队列,且优先级高于idle,则扩宽max_dispatch的限制
  41. max_dispatch *= 4;
  42. }
  43. /*
  44. * Async queues must wait a bit before being allowed dispatch.
  45. * We also ramp up the dispatch depth gradually for async IO,
  46. * based on the last sync IO we serviced
  47. */
  48. /*下面根据最后一次发送的同步请求和现在的时间间隔以及同步请求时间片的值,计算出
  49. depth,根据depth重置max_dispatch,这里的意图应该是在从同步请求的发送转为
  50. 异步请求的发送时,延迟一下异步请求的发送*/
  51. if (!cfq_cfqq_sync(cfqq) && cfqd->cfq_latency) {
  52. unsigned long last_sync = jiffies - cfqd->last_end_sync_rq;
  53. unsigned int depth;
  54. depth = last_sync / cfqd->cfq_slice[1];
  55. if (!depth && !cfqq->dispatched)
  56. depth = 1;
  57. if (depth < max_dispatch)
  58. max_dispatch = depth;
  59. }
  60. /*
  61. * If we're below the current max, allow a dispatch
  62. */
  63. return cfqq->dispatched < max_dispatch;
  64. }
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中去,等待块设备的响应

[cpp] view plain copy print ?
  1. static void cfq_dispatch_insert(struct request_queue *q, struct request *rq)
  2. {
  3. struct cfq_data *cfqd = q->elevator->elevator_data;
  4. struct cfq_queue *cfqq = RQ_CFQQ(rq);
  5. cfq_log_cfqq(cfqd, cfqq, "dispatch_insert");
  6. cfqq->next_rq = cfq_find_next_rq(cfqd, cfqq, rq);//选择一个扇区间隙最近的请求赋给next_rq作为下一个请求的备选
  7. cfq_remove_request(rq);//将rq从sort_list和fifo中移除
  8. cfqq->dispatched++;
  9. elv_dispatch_sort(q, rq);//将rq分派到request queue的请求队列等候响应
  10. if (cfq_cfqq_sync(cfqq))
  11. cfqd->sync_flight++;
  12. }

你可能感兴趣的:(Linux IO Scheduler--CFQ(下))