手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux I/O Scheduler--CFQ(下)

[日期:2012-12-18] 来源:Linux社区  作者:vanbreake [字体:大 中 小]

最后分析的依然是每个调度器最重要的函数--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

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为当前运行的队列

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查找一个在扇区地址上满足要求的队列,这个操作只对同步请求队列有效

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执行实际的工作

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;
}

 

 

 

 

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款