forked from asifazamali-zz/kernel
/
IOtrace.c
202 lines (172 loc) · 4.3 KB
/
IOtrace.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
#include <linux/root_dev.h>
#include <linux/ctype.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/tty.h>
#include <linux/sched.h>
#include <linux/timekeeping.h>
extern void (*block_fun)(struct task_struct *,struct bio*);
extern void (*block_requeue)(struct request_queue *q,struct request *rq);
extern void (*block_comp)(struct request *rq);
int dummy=1;
struct request_queue *queue;
struct gendisk *disk;
struct bio *bio;
char *device="/dev/sda1";
dev_t dev;
char RW,F;
unsigned long long sector;
unsigned int size;
unsigned long seq,diff_sec;
unsigned long diff_nsec;
int timer=10;
struct timespec init_time,current_time;
int cpu;
int pid;
module_param(device,charp,S_IRUGO|S_IWUSR);
module_param(timer,int,0);
dev_t name_to_dev_t(char *name)
{
char s[32];
char *p;
dev_t res = 0;
int part;
if (strncmp(name, "/dev/", 5) != 0) {
unsigned maj, min;
if (sscanf(name, "%u:%u", &maj, &min) == 2) {
res = MKDEV(maj, min);
if (maj != MAJOR(res) || min != MINOR(res))
goto fail;
} else {
res = new_decode_dev(simple_strtoul(name, &p, 16));
if (*p)
goto fail;
}
goto done;
}
name += 5;
res = Root_NFS;
if (strcmp(name, "nfs") == 0)
goto done;
res = Root_RAM0;
if (strcmp(name, "ram") == 0)
goto done;
if (strlen(name) > 31)
goto fail;
strcpy(s, name);
for (p = s; *p; p++)
if (*p == '/')
*p = '!';
res = blk_lookup_devt(s, 0);
if (res)
{
goto done;
}
while (p > s && isdigit(p[-1]))
p--;
if (p == s || !*p || *p == '0')
goto fail;
part = simple_strtoul(p, NULL, 10);
*p = '\0';
res = blk_lookup_devt(s, part);
if (res)
goto done;
if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
goto fail;
p[-1] = '\0';
res = blk_lookup_devt(s, part);
if (res)
goto done;
fail:
return 0;
done:
return res;
}
static void my_block_requeue(struct request_queue *q,struct request *rq)
{
seq++;
diff_sec=current_time.tv_sec-init_time.tv_sec;
diff_nsec=current_time.tv_nsec-init_time.tv_nsec;
bio=rq->bio;
if(bio==NULL)
printk("%d,%d %d %ld %ld.%ld 'R' \n",MAJOR(dev),MINOR(dev),rq->cpu,seq,diff_sec,diff_nsec);
else
{
RW=(bio->bi_rw)?'R':'W';
F='R';
sector=(bio->bi_io_vec->bv_offset);
size=bio_sectors(bio);
printk("%d,%d %d %ld %ld.%ld %c %c %lld+%d \n",MAJOR(dev),MINOR(dev),rq->cpu,seq,diff_sec,diff_nsec,F,RW,sector,size);
}
}
static void my_block_comp(struct request *rq)
{
seq++;
diff_sec=current_time.tv_sec-init_time.tv_sec;
diff_nsec=current_time.tv_nsec-init_time.tv_nsec;
bio=rq->bio;
if(bio==NULL)
{
printk("%d,%d %ld %ld.%ld 'C' \n",MAJOR(dev),MINOR(dev),seq,diff_sec,diff_nsec);
}
else
{
RW=(bio->bi_rw)?'R':'W';
F='C';
sector=(bio->bi_io_vec->bv_offset);
size=bio_sectors(bio);
printk("%d,%d %d %ld %ld.%ld %c %c %lld+%d \n",MAJOR(dev),MINOR(dev),rq->cpu,seq,diff_sec,diff_nsec,F,RW,sector,size);
}
}
static void my_block_fun(struct task_struct *task,struct bio* bio)
{
getrawmonotonic(¤t_time);
if(strcmp(disk->disk_name,bio->bi_bdev->bd_disk->disk_name)==0)
{
seq++;
RW=(bio->bi_rw)?'R':'W';
F=(task->bio_list)?'M':'I';
sector=(bio->bi_io_vec->bv_offset);
size=bio_sectors(bio);
cpu=task_thread_info(task)->cpu;
diff_sec=current_time.tv_sec-init_time.tv_sec;
diff_nsec=current_time.tv_nsec-init_time.tv_nsec;
printk("%d,%d %d %ld %ld.%ld %d %c %c %lld+%d %s\n",MAJOR(dev),MINOR(dev),cpu,seq,diff_sec,diff_nsec,task->pid,F,RW,sector,size,task->comm);
}
if((current_time.tv_sec-init_time.tv_sec)>timer)
{
block_fun=NULL;
block_requeue=NULL;
block_comp=NULL;
}
}
static __init int start_module(void)
{
dev=name_to_dev_t(device);
disk = get_gendisk(dev,&dummy);
if(!disk)
return 0;
queue=disk->queue;
getrawmonotonic(&init_time);
block_fun=my_block_fun;
block_requeue=my_block_requeue;
block_comp=my_block_comp;
return 0;
}
static __exit void end_module(void)
{
block_fun=NULL;
block_requeue=NULL;
block_comp=NULL;
}
module_init(start_module);
module_exit(end_module);
MODULE_LICENSE("Dual BSD/GPL");