Commit | Line | Data |
---|---|---|
fb7b71a3 JA |
1 | /* |
2 | * blktrace support code for fio | |
3 | */ | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
8c1fdf04 | 6 | |
fb7b71a3 JA |
7 | #include "list.h" |
8 | #include "fio.h" | |
9 | #include "blktrace_api.h" | |
10 | ||
8c1fdf04 JA |
11 | /* |
12 | * Just discard the pdu by seeking past it. | |
13 | */ | |
fb7b71a3 JA |
14 | static int discard_pdu(int fd, struct blk_io_trace *t) |
15 | { | |
16 | if (t->pdu_len == 0) | |
17 | return 0; | |
18 | ||
19 | if (lseek(fd, t->pdu_len, SEEK_CUR) < 0) | |
20 | return errno; | |
21 | ||
22 | return 0; | |
23 | } | |
24 | ||
8c1fdf04 JA |
25 | /* |
26 | * Check if this is a blktrace binary data file. We read a single trace | |
27 | * into memory and check for the magic signature. | |
28 | */ | |
fb7b71a3 JA |
29 | int is_blktrace(const char *filename) |
30 | { | |
31 | struct blk_io_trace t; | |
32 | int fd, ret; | |
33 | ||
34 | fd = open(filename, O_RDONLY); | |
35 | if (fd < 0) { | |
36 | perror("open blktrace"); | |
37 | return 0; | |
38 | } | |
39 | ||
40 | ret = read(fd, &t, sizeof(t)); | |
41 | close(fd); | |
42 | ||
43 | if (ret < 0) { | |
44 | perror("read blktrace"); | |
45 | return 0; | |
46 | } else if (ret != sizeof(t)) { | |
47 | log_err("fio: short read on blktrace file\n"); | |
48 | return 0; | |
49 | } | |
50 | ||
51 | if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) | |
52 | return 1; | |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
8c1fdf04 JA |
57 | /* |
58 | * Store blk_io_trace data in an ipo for later retrieval. | |
59 | */ | |
fdefd987 | 60 | static void store_ipo(struct thread_data *td, unsigned long long offset, |
8c1fdf04 | 61 | unsigned int bytes, int rw, unsigned long long ttime) |
fdefd987 JA |
62 | { |
63 | struct io_piece *ipo = malloc(sizeof(*ipo)); | |
64 | ||
65 | memset(ipo, 0, sizeof(*ipo)); | |
66 | INIT_LIST_HEAD(&ipo->list); | |
a2eea81b JA |
67 | /* |
68 | * the 512 is wrong here, it should be the hardware sector size... | |
69 | */ | |
70 | ipo->offset = offset * 512; | |
fdefd987 | 71 | ipo->len = bytes; |
8c1fdf04 | 72 | ipo->delay = ttime / 1000; |
fdefd987 JA |
73 | if (rw) |
74 | ipo->ddir = DDIR_WRITE; | |
75 | else | |
76 | ipo->ddir = DDIR_READ; | |
77 | ||
78 | list_add_tail(&ipo->list, &td->io_log_list); | |
79 | } | |
80 | ||
8c1fdf04 JA |
81 | /* |
82 | * We only care for queue traces, most of the others are side effects | |
83 | * due to internal workings of the block layer. | |
84 | */ | |
85 | static void handle_trace(struct thread_data *td, struct blk_io_trace *t, | |
86 | unsigned long long ttime, unsigned long *ios) | |
fb7b71a3 | 87 | { |
fdefd987 JA |
88 | int rw; |
89 | ||
90 | if ((t->action & 0xffff) != __BLK_TA_QUEUE) | |
91 | return; | |
a2eea81b JA |
92 | if (t->action & BLK_TC_ACT(BLK_TC_PC)) |
93 | return; | |
94 | ||
95 | /* | |
96 | * should not happen, need to look into that... | |
97 | */ | |
98 | if (!t->bytes) | |
99 | return; | |
fdefd987 | 100 | |
e7a7d70b | 101 | rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0; |
8c1fdf04 JA |
102 | ios[rw]++; |
103 | store_ipo(td, t->sector, t->bytes, rw, ttime); | |
fb7b71a3 JA |
104 | } |
105 | ||
8c1fdf04 JA |
106 | /* |
107 | * Load a blktrace file by reading all the blk_io_trace entries, and storing | |
108 | * them as io_pieces like the fio text version would do. | |
109 | */ | |
fb7b71a3 JA |
110 | int load_blktrace(struct thread_data *td, const char *filename) |
111 | { | |
8c1fdf04 | 112 | unsigned long long ttime; |
fb7b71a3 | 113 | struct blk_io_trace t; |
8c1fdf04 | 114 | unsigned long ios[2]; |
fb7b71a3 JA |
115 | int fd; |
116 | ||
117 | fd = open(filename, O_RDONLY); | |
118 | if (fd < 0) { | |
119 | td_verror(td, errno, "open blktrace file"); | |
120 | return 1; | |
121 | } | |
122 | ||
8c1fdf04 JA |
123 | ios[0] = ios[1] = 0; |
124 | ttime = 0; | |
fb7b71a3 | 125 | do { |
8c1fdf04 JA |
126 | /* |
127 | * Once this is working fully, I'll add a layer between | |
128 | * here and read to cache trace data. Then we can avoid | |
129 | * doing itsy bitsy reads, but instead pull in a larger | |
130 | * chunk of data at the time. | |
131 | */ | |
fb7b71a3 JA |
132 | int ret = read(fd, &t, sizeof(t)); |
133 | ||
134 | if (ret < 0) { | |
135 | td_verror(td, errno, "read blktrace file"); | |
8c1fdf04 | 136 | goto err; |
fb7b71a3 JA |
137 | } else if (!ret) { |
138 | break; | |
139 | } else if (ret != sizeof(t)) { | |
140 | log_err("fio: short read on blktrace file\n"); | |
8c1fdf04 | 141 | goto err; |
fb7b71a3 JA |
142 | } |
143 | ||
144 | if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) { | |
145 | log_err("fio: bad magic in blktrace data\n"); | |
8c1fdf04 | 146 | goto err; |
fb7b71a3 JA |
147 | } |
148 | if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) { | |
149 | log_err("fio: bad blktrace version %d\n", t.magic & 0xff); | |
8c1fdf04 | 150 | goto err; |
fb7b71a3 JA |
151 | } |
152 | ret = discard_pdu(fd, &t); | |
153 | if (ret) { | |
154 | td_verror(td, ret, "blktrace lseek"); | |
8c1fdf04 | 155 | goto err; |
fb7b71a3 | 156 | } |
8c1fdf04 JA |
157 | if (!ttime) |
158 | ttime = t.time; | |
159 | handle_trace(td, &t, t.time - ttime, ios); | |
160 | ttime = t.time; | |
fb7b71a3 JA |
161 | } while (1); |
162 | ||
163 | close(fd); | |
8c1fdf04 JA |
164 | |
165 | if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) { | |
166 | log_err("fio: found no ios in blktrace data\n"); | |
167 | return 1; | |
168 | } else if (ios[DDIR_READ] && !ios[DDIR_READ]) | |
169 | td->o.td_ddir = TD_DDIR_READ; | |
170 | else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) | |
171 | td->o.td_ddir = TD_DDIR_WRITE; | |
172 | else | |
173 | td->o.td_ddir = TD_DDIR_RW; | |
174 | ||
175 | /* | |
176 | * We need to do direct/raw ios to the device, to avoid getting | |
177 | * read-ahead in our way. | |
178 | */ | |
179 | td->o.odirect = 1; | |
180 | ||
fb7b71a3 | 181 | return 0; |
8c1fdf04 JA |
182 | err: |
183 | close(fd); | |
184 | return 1; | |
fb7b71a3 | 185 | } |