Speed up verify fill
[fio.git] / verify.c
1 /*
2  * IO verification helpers
3  */
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <assert.h>
8
9 #include "fio.h"
10
11 static void fill_random_bytes(struct thread_data *td,
12                               unsigned char *p, unsigned int len)
13 {
14         unsigned int todo;
15         int r;
16
17         while (len) {
18                 r = os_random_long(&td->verify_state);
19
20                 /*
21                  * lrand48_r seems to be broken and only fill the bottom
22                  * 32-bits, even on 64-bit archs with 64-bit longs
23                  */
24                 todo = sizeof(r);
25                 if (todo > len)
26                         todo = len;
27
28                 memcpy(p, &r, todo);
29
30                 len -= todo;
31                 p += todo;
32         }
33 }
34
35 static void hexdump(void *buffer, int len)
36 {
37         unsigned char *p = buffer;
38         int i;
39
40         for (i = 0; i < len; i++)
41                 log_info("%02x", p[i]);
42         log_info("\n");
43 }
44
45 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u)
46 {
47         unsigned char *p = io_u->buf;
48         unsigned char c;
49
50         p += sizeof(*hdr);
51         c = crc7(p, hdr->len - sizeof(*hdr));
52
53         if (c != hdr->crc7) {
54                 log_err("crc7: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
55                 log_err("crc7: wanted %x, got %x\n", hdr->crc7, c);
56                 return 1;
57         }
58
59         return 0;
60 }
61
62 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u)
63 {
64         unsigned char *p = io_u->buf;
65         unsigned short c;
66
67         p += sizeof(*hdr);
68         c = crc16(p, hdr->len - sizeof(*hdr));
69
70         if (c != hdr->crc16) {
71                 log_err("crc16: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
72                 log_err("crc16: wanted %x, got %x\n", hdr->crc16, c);
73                 return 1;
74         }
75
76         return 0;
77 }
78
79 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
80 {
81         unsigned char *p = io_u->buf;
82         unsigned long c;
83
84         p += sizeof(*hdr);
85         c = crc32(p, hdr->len - sizeof(*hdr));
86
87         if (c != hdr->crc32) {
88                 log_err("crc32: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
89                 log_err("crc32: wanted %lx, got %lx\n", hdr->crc32, c);
90                 return 1;
91         }
92
93         return 0;
94 }
95
96 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
97 {
98         unsigned char *p = io_u->buf;
99         struct md5_ctx md5_ctx;
100
101         memset(&md5_ctx, 0, sizeof(md5_ctx));
102         p += sizeof(*hdr);
103         md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
104
105         if (memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash))) {
106                 log_err("md5: verify failed at %llu/%lu\n", io_u->offset, io_u->buflen);
107                 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
108                 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
109                 return 1;
110         }
111
112         return 0;
113 }
114
115 int verify_io_u(struct thread_data *td, struct io_u *io_u)
116 {
117         struct verify_header *hdr = (struct verify_header *) io_u->buf;
118         int ret;
119
120         if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
121                 return 0;
122
123         if (hdr->fio_magic != FIO_HDR_MAGIC) {
124                 log_err("Bad verify header %x\n", hdr->fio_magic);
125                 return EIO;
126         }
127
128         if (hdr->verify_type == VERIFY_MD5)
129                 ret = verify_io_u_md5(hdr, io_u);
130         else if (hdr->verify_type == VERIFY_CRC32)
131                 ret = verify_io_u_crc32(hdr, io_u);
132         else if (hdr->verify_type == VERIFY_CRC16)
133                 ret = verify_io_u_crc16(hdr, io_u);
134         else if (hdr->verify_type == VERIFY_CRC7)
135                 ret = verify_io_u_crc7(hdr, io_u);
136         else {
137                 log_err("Bad verify type %u\n", hdr->verify_type);
138                 ret = 1;
139         }
140
141         if (ret)
142                 return EIO;
143
144         return 0;
145 }
146
147 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
148 {
149         hdr->crc7 = crc7(p, len);
150 }
151
152 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
153 {
154         hdr->crc16 = crc16(p, len);
155 }
156
157 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
158 {
159         hdr->crc32 = crc32(p, len);
160 }
161
162 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
163 {
164         struct md5_ctx md5_ctx;
165
166         memset(&md5_ctx, 0, sizeof(md5_ctx));
167         md5_update(&md5_ctx, p, len);
168         memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
169 }
170
171 /*
172  * fill body of io_u->buf with random data and add a header with the
173  * crc32 or md5 sum of that data.
174  */
175 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
176 {
177         const unsigned int len = io_u->buflen - sizeof(struct verify_header);
178         struct verify_header *hdr;
179         unsigned char *p;
180
181         if (td->o.verify == VERIFY_NULL)
182                 return;
183
184         hdr = (struct verify_header *) io_u->buf;
185         hdr->fio_magic = FIO_HDR_MAGIC;
186         hdr->len = io_u->buflen;
187         hdr->verify_type = td->o.verify;
188
189         p = io_u->buf + sizeof(*hdr);
190         fill_random_bytes(td, p, len);
191
192         switch (td->o.verify) {
193         case VERIFY_MD5:
194                 fill_md5(hdr, p, len);
195                 break;
196         case VERIFY_CRC32:
197                 fill_crc32(hdr, p, len);
198                 break;
199         case VERIFY_CRC16:
200                 fill_crc16(hdr, p, len);
201                 break;
202         case VERIFY_CRC7:
203                 fill_crc7(hdr, p, len);
204                 break;
205         default:
206                 log_err("fio: bad verify type: %d\n", td->o.verify);
207                 assert(0);
208         }
209 }
210
211 int get_next_verify(struct thread_data *td, struct io_u *io_u)
212 {
213         struct io_piece *ipo = NULL;
214
215         /*
216          * this io_u is from a requeue, we already filled the offsets
217          */
218         if (io_u->file)
219                 return 0;
220
221         if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
222                 struct rb_node *n = rb_first(&td->io_hist_tree);
223
224                 ipo = rb_entry(n, struct io_piece, rb_node);
225                 rb_erase(n, &td->io_hist_tree);
226         } else if (!list_empty(&td->io_hist_list)) {
227                 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
228                 list_del(&ipo->list);
229         }
230
231         if (ipo) {
232                 io_u->offset = ipo->offset;
233                 io_u->buflen = ipo->len;
234                 io_u->file = ipo->file;
235
236                 if ((io_u->file->flags & FIO_FILE_OPEN) == 0) {
237                         int r = td_io_open_file(td, io_u->file);
238
239                         if (r)
240                                 return 1;
241                 }
242
243                 get_file(ipo->file);
244                 assert(io_u->file->flags & FIO_FILE_OPEN);
245                 io_u->ddir = DDIR_READ;
246                 io_u->xfer_buf = io_u->buf;
247                 io_u->xfer_buflen = io_u->buflen;
248                 free(ipo);
249                 return 0;
250         }
251
252         return 1;
253 }