内核版本:3.13
deadline-iosched.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static const int read_expire = HZ / 2; /* 提交读操作前的最大时间 */
static const int write_expire = 5 * HZ; /* ditto for writes, these limits are SOFT! */
static const int writes_starved = 2; /* max times reads can starve a write */
//of sequential requests treated as one by the above parameters. For throughput.
static const int fifo_batch = 16;
//截止时间调度程序(1:batching 2:写饥饿 3:请求超时)
struct deadline_data { //相当于该算法的调度器
//requests (deadline_rq s) are present on both sort_list and fifo_list
struct rb_root sort_list[2];
struct list_head fifo_list[2];
//next in sort order. read, write or both are NULL
struct request *next_rq[2]; //指向读/写方向上的下一个请求
unsigned int batching; /* number of sequential requests made */
sector_t last_sector; /* head position */
unsigned int starved; /* 写请求被饥饿的次数 */
//settings that change how the i/o scheduler behaves
int fifo_expire[2];
int fifo_batch; //批量发送的最大值 默认值16
int writes_starved; //写请求饥饿线,默认为2
int front_merges; //表示能否进行前向合并的检查 默认值给1
};
static void deadline_move_request(struct deadline_data *, struct request *);
//返回根节点
static inline struct rb_root * deadline_rb_root(struct deadline_data *dd, struct request *rq)
{
return &dd->sort_list[rq_data_dir(rq)];
}
//返回rq的下一个请求
static inline struct request * deadline_latter_request(struct request *rq)
{
struct rb_node *node = rb_next(&rq->rb_node);
if (node)
return rb_entry_rq(node); //返回该节点对应的结构体首地址
return NULL; //如果是NULL说明已经是最后一个节点了
}
//将请求增加到红黑树里
static void deadline_add_rq_rb(struct deadline_data *dd, struct request *rq)
{
struct rb_root *root = deadline_rb_root(dd, rq); //先找到根节点
//将rq加入红黑树里
elv_rb_add(root, rq);
}
//将请求从红黑树里删除
static inline void deadline_del_rq_rb(struct deadline_data *dd, struct request *rq)
{
const int data_dir = rq_data_dir(rq);
//如果要删除的节点等于下一个要处理的节点,那么就更新下一个要处理的节点
if (dd->next_rq[data_dir] == rq)
dd->next_rq[data_dir] = deadline_latter_request(rq);
//将rq从红黑树里移除(第一个参数返回根节点,第二个参数是要删除的节点)
elv_rb_del(deadline_rb_root(dd, rq), rq);
}
//将请求增加到红黑树里和链表里,并设置该请求的jiffies值
static void deadline_add_request(struct request_queue *q, struct request *rq)
{
struct deadline_data *dd = q->elevator->elevator_data;
const int data_dir = rq_data_dir(rq); //读/写方向
deadline_add_rq_rb(dd, rq); //将rq加入红黑树里
// 设置rq的jiffies的值
rq_set_fifo_time(rq, jiffies + dd->fifo_expire[data_dir]);
//后面的参数是头节点
list_add_tail(&rq->queuelist, &dd->fifo_list[data_dir]); //将rq加入链表里
}
//remove rq from rbtree and fifo.
static void deadline_remove_request(struct request_queue *q, struct request *rq)
{
struct deadline_data *dd = q->elevator->elevator_data;
rq_fifo_clear(rq);
deadline_del_rq_rb(dd, rq);
}
//这个还不算形成一个完整的request(bio可能还要合并到request里来)
static int deadline_merge(struct request_queue *q, struct request **req, struct bio *bio)
{
struct deadline_data *dd = q->elevator->elevator_data;
struct request *__rq;
int ret;
if (dd->front_merges) { //向前合并
sector_t sector = bio_end_sector(bio); //bio的末尾扇区号
//根据bio的末尾扇区号寻找rq,看rq的起始扇区号是否等于bio的末尾扇区号,相等说明可以向前合并
__rq = elv_rb_find(&dd->sort_list[bio_data_dir(bio)], sector);
if (__rq) {
BUG_ON(sector != blk_rq_pos(__rq));
if (elv_rq_merge_ok(__rq, bio)) {
ret = ELEVATOR_FRONT_MERGE;
goto out;
}
}
}
return ELEVATOR_NO_MERGE;
out:
*req = __rq; //返回找到的request(这个req可以和bio进行前向合并)
return ret;
}
//这个是已经进行了合并以后的操作,req合并以后起始扇区号发生了改变,所以需要从
//红黑树里删除节点,然后在插入,这样就会根据合并后扇区的起始号将req插入到合适的位置
static void deadline_merged_request(struct request_queue *q, struct request *req, int type)
{
//响应合并后的处理
struct deadline_data *dd = q->elevator->elevator_data;
if (type == ELEVATOR_FRONT_MERGE) {
//先删除
elv_rb_del(deadline_rb_root(dd, req), req);
//再增加
deadline_add_rq_rb(dd, req);
}
}
static void deadline_merged_requests(struct request_queue *q, struct request *req, struct request *next)
{
/*如果next在rq之前过期,则将其过期时间分配给rq,
并在fifo中移动到下一个位置(next将被删除)
*/
if (!list_empty(&req->queuelist) && !list_empty(&next->queuelist)) {
//如果next的时间比req的时间早
if (time_before(rq_fifo_time(next), rq_fifo_time(req))) {
//这两个节点的顺序要根据时间值对换下
list_move(&req->queuelist, &next->queuelist);
//那么 合并后的req的时间值就要改成next的时间值
rq_set_fifo_time(req, rq_fifo_time(next));
}
}
//因为已经合并了,所以next可以删除了
deadline_remove_request(q, next);
}
// 将请求从排序列表移动到调度队列。
static inline void deadline_move_to_dispatch(struct deadline_data *dd, struct request *rq)
{
struct request_queue *q = rq->q;
//将请求rq从deadline调度器的红黑树以及fifo_list链表中删掉,并且移除其超时时间
deadline_remove_request(q, rq);
//将请求rq插入 到块设备的请求队列
elv_dispatch_add_tail(q, rq);
}
//将条目移动到调度队列
static void deadline_move_request(struct deadline_data *dd, struct request *rq)
{
const int data_dir = rq_data_dir(rq);
dd->next_rq[READ] = NULL;
dd->next_rq[WRITE] = NULL;
//next_rq赋值的地方,是从红黑树里面取出的(第一次时为NULL)
dd->next_rq[data_dir] = deadline_latter_request(rq);
dd->last_sector = rq_end_sector(rq);
//将其从排序和fifo列表中移除,移到调度队列中
deadline_move_to_dispatch(dd, rq);
}
/*
如果fifo上没有过期的请求,Deadline_check_fifo返回0,
否则返回1。需要! list_empty (dd - > fifo_list [data_dir])
*/
static inline int deadline_check_fifo(struct deadline_data *dd, int ddir)
{
struct request *rq = rq_entry_fifo(dd->fifo_list[ddir].next); //返回第一个节点
//rq is expired!(当前的jiffies值大于rq的jiffies值)
if (time_after_eq(jiffies, rq_fifo_time(rq)))
return 1; //返回1说明必须要处理这个request了
return 0;
}
//Deadline_dispatch_requests根据读/写过期、fif_batch等选择最佳请求
static int deadline_dispatch_requests(struct request_queue *q, int force)
{
struct deadline_data *dd = q->elevator->elevator_data;
const int reads = !list_empty(&dd->fifo_list[READ]); //链表为空 reads值为0
const int writes = !list_empty(&dd->fifo_list[WRITE]); //链表为空 writes值为0
struct request *rq;
int data_dir;
if (dd->next_rq[WRITE])
rq = dd->next_rq[WRITE]; //先判断的写请求
else
rq = dd->next_rq[READ]; //有读请求
if (rq && dd->batching < dd->fifo_batch) //fifo_batch默认值16
goto dispatch_request;
if (reads) { //为1说明链表不为空,有读请求
BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[READ]));
//判断写请求是否已经饥饿,如果是则优先处理
if (writes && (dd->starved++ >= dd->writes_starved))
goto dispatch_writes;
data_dir = READ;
goto dispatch_find_request;
}
if (writes) { //为1说明链表不为空,有写请求
dispatch_writes:
BUG_ON(RB_EMPTY_ROOT(&dd->sort_list[WRITE]));
dd->starved = 0; //饥饿已经处理了,值更新为0
data_dir = WRITE;
goto dispatch_find_request;
}
return 0;
dispatch_find_request:
/*
1:查找是否有过期的读请求或者过期的写请求,如果有,那就取出
2:next_rq为空的话req就从fifo_list里面取
*/
if (deadline_check_fifo(dd, data_dir) || !dd->next_rq[data_dir]) {
rq = rq_entry_fifo(dd->fifo_list[data_dir].next); //这个req是从链表里取的
} else {
rq = dd->next_rq[data_dir]; //这个req是从红黑树里取的
}
dd->batching = 0; //这个值的初始化应该是0
dispatch_request:
dd->batching++;
//deadline_dispatch_requests -> deadline_move_request -> deadline_move_to_dispatch
deadline_move_request(dd, rq);
return 1;
}
static void deadline_exit_queue(struct elevator_queue *e)
{
struct deadline_data *dd = e->elevator_data;
BUG_ON(!list_empty(&dd->fifo_list[READ]));
BUG_ON(!list_empty(&dd->fifo_list[WRITE]));
kfree(dd);
}
static int deadline_init_queue(struct request_queue *q, struct elevator_type *e)
{
struct deadline_data *dd;
struct elevator_queue *eq;
eq = elevator_alloc(q, e);
if (!eq)
return -ENOMEM;
dd = kzalloc_node(sizeof(*dd), GFP_KERNEL, q->node);
if (!dd) {
kobject_put(&eq->kobj);
return -ENOMEM;
}
eq->elevator_data = dd;
//链表初始化
INIT_LIST_HEAD(&dd->fifo_list[READ]);
INIT_LIST_HEAD(&dd->fifo_list[WRITE]);
//红黑树根节点初始化
dd->sort_list[READ] = RB_ROOT;
dd->sort_list[WRITE] = RB_ROOT;
dd->fifo_expire[READ] = read_expire;
dd->fifo_expire[WRITE] = write_expire;
dd->writes_starved = writes_starved;
dd->front_merges = 1;
dd->fifo_batch = fifo_batch;
spin_lock_irq(q->queue_lock);
q->elevator = eq; //赋值给request_queue的对应变量
spin_unlock_irq(q->queue_lock);
return 0;
}
static ssize_t deadline_var_show(int var, char *page)
{
return sprintf(page, "%d\n", var);
}
static ssize_t deadline_var_store(int *var, const char *page, size_t count)
{
char *p = (char *) page;
*var = simple_strtol(p, &p, 10);
return count;
}
#define SHOW_FUNCTION(__FUNC, __VAR, __CONV) \
static ssize_t __FUNC(struct elevator_queue *e, char *page) \
{ \
struct deadline_data *dd = e->elevator_data; \
int __data = __VAR; \
if (__CONV) \
__data = jiffies_to_msecs(__data); \
return deadline_var_show(__data, (page)); \
}
SHOW_FUNCTION(deadline_read_expire_show, dd->fifo_expire[READ], 1);
SHOW_FUNCTION(deadline_write_expire_show, dd->fifo_expire[WRITE], 1);
SHOW_FUNCTION(deadline_writes_starved_show, dd->writes_starved, 0);
SHOW_FUNCTION(deadline_front_merges_show, dd->front_merges, 0);
SHOW_FUNCTION(deadline_fifo_batch_show, dd->fifo_batch, 0);
#undef SHOW_FUNCTION
#define STORE_FUNCTION(__FUNC, __PTR, MIN, MAX, __CONV) \
static ssize_t __FUNC(struct elevator_queue *e, const char *page, size_t count) \
{ \
struct deadline_data *dd = e->elevator_data; \
int __data; \
int ret = deadline_var_store(&__data, (page), count); \
if (__data < (MIN)) \
__data = (MIN); \
else if (__data > (MAX)) \
__data = (MAX); \
if (__CONV) \
*(__PTR) = msecs_to_jiffies(__data); \
else \
*(__PTR) = __data; \
return ret; \
}
STORE_FUNCTION(deadline_read_expire_store, &dd->fifo_expire[READ], 0, INT_MAX, 1);
STORE_FUNCTION(deadline_write_expire_store, &dd->fifo_expire[WRITE], 0, INT_MAX, 1);
STORE_FUNCTION(deadline_writes_starved_store, &dd->writes_starved, INT_MIN, INT_MAX, 0);
STORE_FUNCTION(deadline_front_merges_store, &dd->front_merges, 0, 1, 0);
STORE_FUNCTION(deadline_fifo_batch_store, &dd->fifo_batch, 0, INT_MAX, 0);
#undef STORE_FUNCTION
#define DD_ATTR(name) \
__ATTR(name, S_IRUGO|S_IWUSR, deadline_##name##_show, \
deadline_##name##_store)
static struct elv_fs_entry deadline_attrs[] = {
DD_ATTR(read_expire),
DD_ATTR(write_expire),
DD_ATTR(writes_starved),
DD_ATTR(front_merges),
DD_ATTR(fifo_batch),
__ATTR_NULL
};
static struct elevator_type iosched_deadline = {
.ops = {
.elevator_merge_fn = deadline_merge,
.elevator_merged_fn = deadline_merged_request,
.elevator_merge_req_fn = deadline_merged_requests,
.elevator_dispatch_fn = deadline_dispatch_requests,
.elevator_add_req_fn = deadline_add_request,
.elevator_former_req_fn = elv_rb_former_request,
.elevator_latter_req_fn = elv_rb_latter_request,
.elevator_init_fn = deadline_init_queue,
.elevator_exit_fn = deadline_exit_queue,
},
.elevator_attrs = deadline_attrs,
.elevator_name = "deadline",
.elevator_owner = THIS_MODULE,
};
static int __init deadline_init(void)
{
return elv_register(&iosched_deadline);
}
static void __exit deadline_exit(void)
{
elv_unregister(&iosched_deadline);
}
module_init(deadline_init);
module_exit(deadline_exit);
MODULE_AUTHOR("Jens Axboe");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("deadline IO scheduler");
参考:
1://https://www.bilibili.com/read/cv18210887/
2://https://blog.csdn.net/moliyiye/article/details/49492929