-
Notifications
You must be signed in to change notification settings - Fork 0
/
clook-iosched.c
147 lines (124 loc) · 3.42 KB
/
clook-iosched.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* elevator clook
*/
#include <linux/blkdev.h>
#include <linux/elevator.h>
#include <linux/bio.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
struct clook_data {
struct list_head queue;
};
static void clook_merged_requests(struct request_queue *q, struct request *rq,
struct request *next)
{
list_del_init(&next->queuelist);
}
/**
* Instrumented to report activity.
*/
static int clook_dispatch(struct request_queue *q, int force)
{
struct clook_data *nd = q->elevator->elevator_data;
struct request *rq;
rq = list_first_entry_or_null(&nd->queue, struct request, queuelist);
if (rq) {
list_del_init(&rq->queuelist);
elv_dispatch_sort(q, rq);
char direction = ((rq->cmd_flags & 1) ? 'W' : 'R');
printk("[CLOOK] dsp %c %lu\n", direction, blk_rq_pos(rq));
return 1;
}
return 0;
}
/**
* Modified function
* Notes:
* - The list we are inserting into is sorted,
* because new entries are only added by our function.
* - Adjacent requests should be merged.
* - Instrumented to report activity.
*/
static void clook_add_request(struct request_queue *q, struct request *rq)
{
struct clook_data *nd = q->elevator->elevator_data;
//cursor for selecting where to insert
struct list_head *cur = NULL;
//We can assume the array is already sorted, so insert as if inserting into a sorted array
list_for_each(cur, &nd->queue) {
if(rq_end_sector(list_entry(cur, struct request,queuelist)) > rq_end_sector(rq)) {
break;
}
}
list_add_tail(&rq->queuelist, &nd->queue);
char direction = ((rq->cmd_flags & 1) ? 'W' : 'R');
printk("[CLOOK] add %c %lu\n", direction, blk_rq_pos(rq));
}
static struct request *
clook_former_request(struct request_queue *q, struct request *rq)
{
struct clook_data *nd = q->elevator->elevator_data;
if (rq->queuelist.prev == &nd->queue)
return NULL;
return list_prev_entry(rq, queuelist);
}
static struct request *
clook_latter_request(struct request_queue *q, struct request *rq)
{
struct clook_data *nd = q->elevator->elevator_data;
if (rq->queuelist.next == &nd->queue)
return NULL;
return list_next_entry(rq, queuelist);
}
static int clook_init_queue(struct request_queue *q, struct elevator_type *e)
{
struct clook_data *nd;
struct elevator_queue *eq;
eq = elevator_alloc(q, e);
if (!eq)
return -ENOMEM;
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);
spin_lock_irq(q->queue_lock);
q->elevator = eq;
spin_unlock_irq(q->queue_lock);
return 0;
}
static void clook_exit_queue(struct elevator_queue *e)
{
struct clook_data *nd = e->elevator_data;
BUG_ON(!list_empty(&nd->queue));
kfree(nd);
}
static struct elevator_type elevator_clook = {
.ops.sq = {
.elevator_merge_req_fn = clook_merged_requests,
.elevator_dispatch_fn = clook_dispatch,
.elevator_add_req_fn = clook_add_request,
.elevator_former_req_fn = clook_former_request,
.elevator_latter_req_fn = clook_latter_request,
.elevator_init_fn = clook_init_queue,
.elevator_exit_fn = clook_exit_queue,
},
.elevator_name = "clook",
.elevator_owner = THIS_MODULE,
};
static int __init clook_init(void)
{
return elv_register(&elevator_clook);
}
static void __exit clook_exit(void)
{
elv_unregister(&elevator_clook);
}
module_init(clook_init);
module_exit(clook_exit);
MODULE_AUTHOR("David Ramirez");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("C-LOOK IO scheduler");