linux block noop调度算法源码阅读记录

内核版本linux3.13 noop-iosched.c

#include 
#include 
#include 
#include 
#include 
#include 

//https://www.pianshen.com/article/15821946232/s
//https://blog.csdn.net/hs794502825/article/details/24663041
struct noop_data { //相当于调度器,管理着io
	struct list_head queue; //链表头。维护接收到的request
};

static void noop_merged_requests(struct request_queue *q, struct request *rq, struct request *next)
{
	list_del_init(&next->queuelist);
}

static int noop_dispatch(struct request_queue *q, int force)
{
	struct noop_data *nd = q->elevator->elevator_data;

	//先判断是否为空
	if (!list_empty(&nd->queue)) {
		struct request *rq;
		//返回nd->queue.next指向的链表节点的首地址
		rq = list_entry(nd->queue.next, struct request, queuelist);
		//从链表里面移除(取request直接就是按顺序取的)
		list_del_init(&rq->queuelist);
		//将获取的request放入设备所属的request queue中(取出的request在queue里会重新排序)
		elv_dispatch_sort(q, rq);
		return 1;
	}
	return 0;
}

static void noop_add_request(struct request_queue *q, struct request *rq)
{
	struct noop_data *nd = q->elevator->elevator_data;

	//将rq通过其成员queuelist(struct list_head类型)加入到nd->queue链表里
	list_add_tail(&rq->queuelist, &nd->queue);
}

//寻找rq的前面一个请求
static struct request *noop_former_request(struct request_queue *q, struct request *rq)
{
	struct noop_data *nd = q->elevator->elevator_data;

	//如果相等,说明rq是最前面的请求了(rq的prev指针指向了头节点)
	if (rq->queuelist.prev == &nd->queue)
		return NULL;

	//list_entry(ptr, type, member) 返回ptr指向的链表节点对应的结构体首地址
	return list_entry(rq->queuelist.prev, struct request, queuelist);
}

static struct request *noop_latter_request(struct request_queue *q, struct request *rq)
{
	struct noop_data *nd = q->elevator->elevator_data;

	//list是双向循环链表,这个成立说明rq是最后一个节点了
	if (rq->queuelist.next == &nd->queue)
		return NULL;
	//list_entry(ptr, type, member) 返回ptr指向的链表节点对应的结构体首地址
	return list_entry(rq->queuelist.next, struct request, queuelist);
}

static int noop_init_queue(struct request_queue *q, struct elevator_type *e)
{
	struct noop_data *nd;
	struct elevator_queue *eq;

	//struct elevator_queue内存申请,各结构体变量初始化
	eq = elevator_alloc(q, e);
	if (!eq)
		return -ENOMEM;

	//struct noop_data结构体内存申请并初始化其成员,然后赋值给elevator_data
	nd = kmalloc_node(sizeof(*nd), GFP_KERNEL, q->node);
	if (!nd) {
		kobject_put(&eq->kobj);
		return -ENOMEM;
	}
	eq->elevator_data = nd;
	INIT_LIST_HEAD(&nd->queue); //充当链表头节点

	//给request_queue结构体的elevator变量进行赋值
	spin_lock_irq(q->queue_lock);
	q->elevator = eq;
	spin_unlock_irq(q->queue_lock);
	return 0;
}

static void noop_exit_queue(struct elevator_queue *e)
{
	struct noop_data *nd = e->elevator_data;

	//到这里先判断下队列是否为空?为空才是正常的?
	BUG_ON(!list_empty(&nd->queue));
	kfree(nd);
}

static struct elevator_type elevator_noop = {
	.ops = {
		.elevator_merge_req_fn		= noop_merged_requests,
		.elevator_dispatch_fn		= noop_dispatch,
		.elevator_add_req_fn		= noop_add_request,
		.elevator_former_req_fn		= noop_former_request,
		.elevator_latter_req_fn		= noop_latter_request,
		.elevator_init_fn		= noop_init_queue,
		.elevator_exit_fn		= noop_exit_queue,
	},
	.elevator_name = "noop",
	.elevator_owner = THIS_MODULE,
};

static int __init noop_init(void)
{
	return elv_register(&elevator_noop);
}

static void __exit noop_exit(void)
{
	elv_unregister(&elevator_noop);
}

module_init(noop_init);
module_exit(noop_exit);

MODULE_AUTHOR("Jens Axboe");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("No-op IO scheduler");

你可能感兴趣的:(#,block,linux)