04648a08ffdf8005fe87fbddf9a9ca542c823396
[fio.git] / blktrace.c
1 /*
2  * blktrace support code for fio
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/stat.h>
7 #include <dirent.h>
8
9 #include "flist.h"
10 #include "fio.h"
11 #include "blktrace_api.h"
12
13 #define TRACE_FIFO_SIZE 8192
14
15 /*
16  * fifo refill frontend, to avoid reading data in trace sized bites
17  */
18 static int refill_fifo(struct thread_data *td, struct fifo *fifo, int fd)
19 {
20         char buf[TRACE_FIFO_SIZE];
21         unsigned int total;
22         int ret;
23
24         total = sizeof(buf);
25         if (total > fifo_room(fifo))
26                 total = fifo_room(fifo);
27
28         ret = read(fd, buf, total);
29         if (ret < 0) {
30                 td_verror(td, errno, "read blktrace file");
31                 return -1;
32         }
33
34         if (ret > 0)
35                 ret = fifo_put(fifo, buf, ret);
36
37         dprint(FD_BLKTRACE, "refill: filled %d bytes\n", ret);
38         return ret;
39 }
40
41 /*
42  * Retrieve 'len' bytes from the fifo, refilling if necessary.
43  */
44 static int trace_fifo_get(struct thread_data *td, struct fifo *fifo, int fd,
45                           void *buf, unsigned int len)
46 {
47         if (fifo_len(fifo) < len) {
48                 int ret = refill_fifo(td, fifo, fd);
49
50                 if (ret < 0)
51                         return ret;
52         }
53
54         return fifo_get(fifo, buf, len);
55 }
56
57 /*
58  * Just discard the pdu by seeking past it.
59  */
60 static int discard_pdu(struct thread_data *td, struct fifo *fifo, int fd,
61                        struct blk_io_trace *t)
62 {
63         if (t->pdu_len == 0)
64                 return 0;
65
66         dprint(FD_BLKTRACE, "discard pdu len %u\n", t->pdu_len);
67         return trace_fifo_get(td, fifo, fd, NULL, t->pdu_len);
68 }
69
70 /*
71  * Check if this is a blktrace binary data file. We read a single trace
72  * into memory and check for the magic signature.
73  */
74 int is_blktrace(const char *filename, int *need_swap)
75 {
76         struct blk_io_trace t;
77         int fd, ret;
78
79         fd = open(filename, O_RDONLY);
80         if (fd < 0)
81                 return 0;
82
83         ret = read(fd, &t, sizeof(t));
84         close(fd);
85
86         if (ret < 0) {
87                 perror("read blktrace");
88                 return 0;
89         } else if (ret != sizeof(t)) {
90                 log_err("fio: short read on blktrace file\n");
91                 return 0;
92         }
93
94         if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
95                 *need_swap = 0;
96                 return 1;
97         }
98
99         /*
100          * Maybe it needs to be endian swapped...
101          */
102         t.magic = fio_swap32(t.magic);
103         if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) {
104                 *need_swap = 1;
105                 return 1;
106         }
107
108         return 0;
109 }
110
111 static int lookup_device(struct thread_data *td, char *path, unsigned int maj,
112                          unsigned int min)
113 {
114         struct dirent *dir;
115         struct stat st;
116         int found = 0;
117         DIR *D;
118
119         D = opendir(path);
120         if (!D)
121                 return 0;
122
123         while ((dir = readdir(D)) != NULL) {
124                 char full_path[256];
125
126                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
127                         continue;
128
129                 sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name);
130                 if (lstat(full_path, &st) == -1) {
131                         perror("lstat");
132                         break;
133                 }
134
135                 if (S_ISDIR(st.st_mode)) {
136                         found = lookup_device(td, full_path, maj, min);
137                         if (found) {
138                                 strcpy(path, full_path);
139                                 break;
140                         }
141                 }
142
143                 if (!S_ISBLK(st.st_mode))
144                         continue;
145
146                 /*
147                  * If replay_redirect is set then always return this device
148                  * upon lookup which overrides the device lookup based on
149                  * major minor in the actual blktrace
150                  */
151                 if (td->o.replay_redirect) {
152                         dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden"
153                                         " with: %s", maj, min,
154                                         td->o.replay_redirect);
155                         strcpy(path, td->o.replay_redirect);
156                         found = 1;
157                         break;
158                 }
159
160                 if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) {
161                         dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min);
162                         strcpy(path, full_path);
163                         found = 1;
164                         break;
165                 }
166         }
167
168         closedir(D);
169         return found;
170 }
171
172 #define FMINORBITS      20
173 #define FMINORMASK      ((1U << FMINORBITS) - 1)
174 #define FMAJOR(dev)     ((unsigned int) ((dev) >> FMINORBITS))
175 #define FMINOR(dev)     ((unsigned int) ((dev) & FMINORMASK))
176
177 static void trace_add_open_close_event(struct thread_data *td, int fileno, enum file_log_act action)
178 {
179         struct io_piece *ipo;
180
181         ipo = calloc(1, sizeof(*ipo));
182         init_ipo(ipo);
183
184         ipo->ddir = DDIR_INVAL;
185         ipo->fileno = fileno;
186         ipo->file_action = action;
187         flist_add_tail(&ipo->list, &td->io_log_list);
188 }
189
190 static int trace_add_file(struct thread_data *td, __u32 device)
191 {
192         static unsigned int last_maj, last_min, last_fileno;
193         unsigned int maj = FMAJOR(device);
194         unsigned int min = FMINOR(device);
195         struct fio_file *f;
196         char dev[256];
197         unsigned int i;
198
199         if (last_maj == maj && last_min == min)
200                 return last_fileno;
201
202         last_maj = maj;
203         last_min = min;
204
205         /*
206          * check for this file in our list
207          */
208         for_each_file(td, f, i)
209                 if (f->major == maj && f->minor == min) {
210                         last_fileno = f->fileno;
211                         return last_fileno;
212                 }
213
214         strcpy(dev, "/dev");
215         if (lookup_device(td, dev, maj, min)) {
216                 int fileno;
217
218                 dprint(FD_BLKTRACE, "add devices %s\n", dev);
219                 fileno = add_file_exclusive(td, dev);
220                 trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE);
221                 last_fileno = fileno;
222         }
223         return last_fileno;
224 }
225
226 /*
227  * Store blk_io_trace data in an ipo for later retrieval.
228  */
229 static void store_ipo(struct thread_data *td, unsigned long long offset,
230                       unsigned int bytes, int rw, unsigned long long ttime,
231                       int fileno)
232 {
233         struct io_piece *ipo = malloc(sizeof(*ipo));
234
235         init_ipo(ipo);
236
237         /*
238          * the 512 is wrong here, it should be the hardware sector size...
239          */
240         ipo->offset = offset * 512;
241         ipo->len = bytes;
242         ipo->delay = ttime / 1000;
243         if (rw)
244                 ipo->ddir = DDIR_WRITE;
245         else
246                 ipo->ddir = DDIR_READ;
247         ipo->fileno = fileno;
248
249         dprint(FD_BLKTRACE, "store ddir=%d, off=%llu, len=%lu, delay=%lu\n",
250                                                         ipo->ddir, ipo->offset,
251                                                         ipo->len, ipo->delay);
252         queue_io_piece(td, ipo);
253 }
254
255 static void handle_trace_notify(struct blk_io_trace *t)
256 {
257         switch (t->action) {
258         case BLK_TN_PROCESS:
259                 log_info("blktrace: got process notify: %x, %d\n",
260                                 t->action, t->pid);
261                 break;
262         case BLK_TN_TIMESTAMP:
263                 log_info("blktrace: got timestamp notify: %x, %d\n",
264                                 t->action, t->pid);
265                 break;
266         case BLK_TN_MESSAGE:
267                 break;
268         default:
269                 dprint(FD_BLKTRACE, "unknown trace act %x\n", t->action);
270                 break;
271         }
272 }
273
274 static void handle_trace_discard(struct thread_data *td, struct blk_io_trace *t,
275                                  unsigned long long ttime, unsigned long *ios)
276 {
277         struct io_piece *ipo = malloc(sizeof(*ipo));
278         int fileno;
279
280         init_ipo(ipo);
281         fileno = trace_add_file(td, t->device);
282
283         ios[DDIR_WRITE]++;
284         td->o.size += t->bytes;
285
286         memset(ipo, 0, sizeof(*ipo));
287         INIT_FLIST_HEAD(&ipo->list);
288
289         /*
290          * the 512 is wrong here, it should be the hardware sector size...
291          */
292         ipo->offset = t->sector * 512;
293         ipo->len = t->bytes;
294         ipo->delay = ttime / 1000;
295         ipo->ddir = DDIR_TRIM;
296         ipo->fileno = fileno;
297
298         dprint(FD_BLKTRACE, "store discard, off=%llu, len=%lu, delay=%lu\n",
299                                                         ipo->offset, ipo->len,
300                                                         ipo->delay);
301         queue_io_piece(td, ipo);
302 }
303
304 static void handle_trace_fs(struct thread_data *td, struct blk_io_trace *t,
305                             unsigned long long ttime, unsigned long *ios,
306                             unsigned int *bs)
307 {
308         int rw;
309         int fileno;
310
311         fileno = trace_add_file(td, t->device);
312
313         rw = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
314
315         if (t->bytes > bs[rw])
316                 bs[rw] = t->bytes;
317
318         ios[rw]++;
319         td->o.size += t->bytes;
320         store_ipo(td, t->sector, t->bytes, rw, ttime, fileno);
321 }
322
323 /*
324  * We only care for queue traces, most of the others are side effects
325  * due to internal workings of the block layer.
326  */
327 static void handle_trace(struct thread_data *td, struct blk_io_trace *t,
328                          unsigned long long ttime, unsigned long *ios,
329                          unsigned int *bs)
330 {
331         if ((t->action & 0xffff) != __BLK_TA_QUEUE)
332                 return;
333         if (t->action & BLK_TC_ACT(BLK_TC_PC))
334                 return;
335
336         if (t->action & BLK_TC_ACT(BLK_TC_NOTIFY))
337                 handle_trace_notify(t);
338         else if (t->action & BLK_TC_ACT(BLK_TC_DISCARD))
339                 handle_trace_discard(td, t, ttime, ios);
340         else
341                 handle_trace_fs(td, t, ttime, ios, bs);
342 }
343
344 static void byteswap_trace(struct blk_io_trace *t)
345 {
346         t->magic = fio_swap32(t->magic);
347         t->sequence = fio_swap32(t->sequence);
348         t->time = fio_swap64(t->time);
349         t->sector = fio_swap64(t->sector);
350         t->bytes = fio_swap32(t->bytes);
351         t->action = fio_swap32(t->action);
352         t->pid = fio_swap32(t->pid);
353         t->device = fio_swap32(t->device);
354         t->cpu = fio_swap32(t->cpu);
355         t->error = fio_swap16(t->error);
356         t->pdu_len = fio_swap16(t->pdu_len);
357 }
358
359 /*
360  * Load a blktrace file by reading all the blk_io_trace entries, and storing
361  * them as io_pieces like the fio text version would do.
362  */
363 int load_blktrace(struct thread_data *td, const char *filename, int need_swap)
364 {
365         unsigned long long ttime, delay;
366         struct blk_io_trace t;
367         unsigned long ios[2], skipped_writes;
368         unsigned int cpu;
369         unsigned int rw_bs[2];
370         struct fifo *fifo;
371         int fd, i;
372         struct fio_file *f;
373
374         fd = open(filename, O_RDONLY);
375         if (fd < 0) {
376                 td_verror(td, errno, "open blktrace file");
377                 return 1;
378         }
379
380         fifo = fifo_alloc(TRACE_FIFO_SIZE);
381
382         td->o.size = 0;
383
384         cpu = 0;
385         ttime = 0;
386         ios[0] = ios[1] = 0;
387         rw_bs[0] = rw_bs[1] = 0;
388         skipped_writes = 0;
389         do {
390                 int ret = trace_fifo_get(td, fifo, fd, &t, sizeof(t));
391
392                 if (ret < 0)
393                         goto err;
394                 else if (!ret)
395                         break;
396                 else if (ret < (int) sizeof(t)) {
397                         log_err("fio: short fifo get\n");
398                         break;
399                 }
400
401                 if (need_swap)
402                         byteswap_trace(&t);
403
404                 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
405                         log_err("fio: bad magic in blktrace data: %x\n",
406                                                                 t.magic);
407                         goto err;
408                 }
409                 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
410                         log_err("fio: bad blktrace version %d\n",
411                                                                 t.magic & 0xff);
412                         goto err;
413                 }
414                 ret = discard_pdu(td, fifo, fd, &t);
415                 if (ret < 0) {
416                         td_verror(td, ret, "blktrace lseek");
417                         goto err;
418                 } else if (t.pdu_len != ret) {
419                         log_err("fio: discarded %d of %d\n", ret, t.pdu_len);
420                         goto err;
421                 }
422                 if ((t.action & BLK_TC_ACT(BLK_TC_NOTIFY)) == 0) {
423                         if (!ttime) {
424                                 ttime = t.time;
425                                 cpu = t.cpu;
426                         }
427
428                         delay = 0;
429                         if (cpu == t.cpu)
430                                 delay = t.time - ttime;
431                         if ((t.action & BLK_TC_ACT(BLK_TC_WRITE)) && read_only)
432                                 skipped_writes++;
433                         else {
434                                 /*
435                                  * set delay to zero if no_stall enabled for
436                                  * fast replay
437                                  */
438                                 if (td->o.no_stall)
439                                         delay = 0;
440
441                                 handle_trace(td, &t, delay, ios, rw_bs);
442                         }
443
444                         ttime = t.time;
445                         cpu = t.cpu;
446                 } else {
447                         delay = 0;
448                         handle_trace(td, &t, delay, ios, rw_bs);
449                 }
450         } while (1);
451
452         for (i = 0; i < td->files_index; i++) {
453                 f= td->files[i];
454                 trace_add_open_close_event(td, f->fileno, FIO_LOG_CLOSE_FILE);
455         }
456
457         fifo_free(fifo);
458         close(fd);
459
460         if (skipped_writes)
461                 log_err("fio: %s skips replay of %lu writes due to read-only\n",
462                                                 td->o.name, skipped_writes);
463
464         if (!ios[DDIR_READ] && !ios[DDIR_WRITE]) {
465                 log_err("fio: found no ios in blktrace data\n");
466                 return 1;
467         } else if (ios[DDIR_READ] && !ios[DDIR_READ]) {
468                 td->o.td_ddir = TD_DDIR_READ;
469                 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
470         } else if (!ios[DDIR_READ] && ios[DDIR_WRITE]) {
471                 td->o.td_ddir = TD_DDIR_WRITE;
472                 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
473         } else {
474                 td->o.td_ddir = TD_DDIR_RW;
475                 td->o.max_bs[DDIR_READ] = rw_bs[DDIR_READ];
476                 td->o.max_bs[DDIR_WRITE] = rw_bs[DDIR_WRITE];
477         }
478
479         /*
480          * We need to do direct/raw ios to the device, to avoid getting
481          * read-ahead in our way.
482          */
483         td->o.odirect = 1;
484
485         return 0;
486 err:
487         close(fd);
488         fifo_free(fifo);
489         return 1;
490 }