Initial blktrace support
[fio.git] / blktrace.c
1 /*
2  * blktrace support code for fio
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "list.h"
7 #include "fio.h"
8 #include "blktrace_api.h"
9
10 static int discard_pdu(int fd, struct blk_io_trace *t)
11 {
12         if (t->pdu_len == 0)
13                 return 0;
14
15         if (lseek(fd, t->pdu_len, SEEK_CUR) < 0)
16                 return errno;
17                 
18         return 0;
19 }
20
21 int is_blktrace(const char *filename)
22 {
23         struct blk_io_trace t;
24         int fd, ret;
25
26         fd = open(filename, O_RDONLY);
27         if (fd < 0) {
28                 perror("open blktrace");
29                 return 0;
30         }
31
32         ret = read(fd, &t, sizeof(t));
33         close(fd);
34
35         if (ret < 0) {
36                 perror("read blktrace");
37                 return 0;
38         } else if (ret != sizeof(t)) {
39                 log_err("fio: short read on blktrace file\n");
40                 return 0;
41         }
42
43         if ((t.magic & 0xffffff00) == BLK_IO_TRACE_MAGIC)
44                 return 1;
45
46         return 0;
47 }
48
49 static void handle_trace(struct thread_data *td, struct blk_io_trace *t)
50 {
51 }
52
53 int load_blktrace(struct thread_data *td, const char *filename)
54 {
55         struct blk_io_trace t;
56         int fd;
57
58         fd = open(filename, O_RDONLY);
59         if (fd < 0) {
60                 td_verror(td, errno, "open blktrace file");
61                 return 1;
62         }
63
64         do {
65                 int ret = read(fd, &t, sizeof(t));
66
67                 if (ret < 0) {
68                         td_verror(td, errno, "read blktrace file");
69                         return 1;
70                 } else if (!ret) {
71                         break;
72                 } else if (ret != sizeof(t)) {
73                         log_err("fio: short read on blktrace file\n");
74                         return 1;
75                 }
76
77                 if ((t.magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
78                         log_err("fio: bad magic in blktrace data\n");
79                         return 1;
80                 }
81                 if ((t.magic & 0xff) != BLK_IO_TRACE_VERSION) {
82                         log_err("fio: bad blktrace version %d\n", t.magic & 0xff);
83                         return 1;
84                 }
85                 ret = discard_pdu(fd, &t);
86                 if (ret) {
87                         td_verror(td, ret, "blktrace lseek");
88                         return 1;
89                 }
90                 handle_trace(td, &t);
91         } while (1);
92
93         close(fd);
94         return 0;
95 }