2 * IO verification helpers
15 #include "lib/hweight.h"
18 #include "crc/crc64.h"
19 #include "crc/crc32.h"
20 #include "crc/crc32c.h"
21 #include "crc/crc16.h"
23 #include "crc/sha256.h"
24 #include "crc/sha512.h"
26 #include "crc/xxhash.h"
28 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
29 struct verify_header *hdr, unsigned int header_num,
30 unsigned int header_len);
32 void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
34 fill_pattern(p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
37 void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
38 struct io_u *io_u, unsigned long seed, int use_seed)
40 struct thread_options *o = &td->o;
42 if (!o->verify_pattern_bytes) {
43 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
46 __fill_random_buf_percentage(seed, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
48 struct frand_state *fs = &td->verify_state;
50 io_u->rand_seed = fill_random_buf_percentage(fs, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
55 if (io_u->buf_filled_len >= len) {
56 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
57 o->verify_pattern_bytes, len);
61 fill_pattern(p, len, o->verify_pattern, o->verify_pattern_bytes);
62 io_u->buf_filled_len = len;
65 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
69 hdr_inc = io_u->buflen;
70 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
71 hdr_inc = td->o.verify_interval;
76 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
77 unsigned long seed, int use_seed)
79 unsigned int hdr_inc, header_num;
80 struct verify_header *hdr;
83 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
85 hdr_inc = get_hdr_inc(td, io_u);
87 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
89 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
94 static void memswp(void *buf1, void *buf2, unsigned int len)
98 assert(len <= sizeof(swap));
100 memcpy(&swap, buf1, len);
101 memcpy(buf1, buf2, len);
102 memcpy(buf2, &swap, len);
105 static void hexdump(void *buffer, int len)
107 unsigned char *p = buffer;
110 for (i = 0; i < len; i++)
111 log_err("%02x", p[i]);
116 * Prepare for separation of verify_header and checksum header
118 static inline unsigned int __hdr_size(int verify_type)
120 unsigned int len = 0;
122 switch (verify_type) {
128 len = sizeof(struct vhdr_md5);
131 len = sizeof(struct vhdr_crc64);
135 case VERIFY_CRC32C_INTEL:
136 len = sizeof(struct vhdr_crc32);
139 len = sizeof(struct vhdr_crc16);
142 len = sizeof(struct vhdr_crc7);
145 len = sizeof(struct vhdr_sha256);
148 len = sizeof(struct vhdr_sha512);
151 len = sizeof(struct vhdr_xxhash);
154 len = sizeof(struct vhdr_meta);
157 len = sizeof(struct vhdr_sha1);
163 log_err("fio: unknown verify header!\n");
167 return len + sizeof(struct verify_header);
170 static inline unsigned int hdr_size(struct verify_header *hdr)
172 return __hdr_size(hdr->verify_type);
175 static void *hdr_priv(struct verify_header *hdr)
179 return priv + sizeof(struct verify_header);
183 * Verify container, pass info to verify handlers and allow them to
184 * pass info back in case of error
191 unsigned int hdr_num;
192 struct thread_data *td;
195 * Output, only valid in case of error
200 unsigned int crc_len;
203 #define DUMP_BUF_SZ 255
204 static int dump_buf_warned;
206 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
207 const char *type, struct fio_file *f)
209 char *ptr, fname[DUMP_BUF_SZ];
210 size_t buf_left = DUMP_BUF_SZ;
213 ptr = strdup(f->file_name);
215 fname[DUMP_BUF_SZ - 1] = '\0';
216 strncpy(fname, basename(ptr), DUMP_BUF_SZ - 1);
218 buf_left -= strlen(fname);
220 if (!dump_buf_warned) {
221 log_err("fio: verify failure dump buffer too small\n");
228 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
230 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
232 perror("open verify buf file");
237 ret = write(fd, buf, len);
241 perror("write verify buf file");
249 log_err(" %s data dumped as %s\n", type, fname);
254 * Dump the contents of the read block and re-generate the correct data
257 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
259 struct thread_data *td = vc->td;
260 struct io_u *io_u = vc->io_u;
261 unsigned long hdr_offset;
265 if (!td->o.verify_dump)
269 * Dump the contents we just read off disk
271 hdr_offset = vc->hdr_num * hdr->len;
273 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
274 "received", vc->io_u->file);
277 * Allocate a new buf and re-generate the original data
279 buf = malloc(io_u->buflen);
282 dummy.rand_seed = hdr->rand_seed;
283 dummy.buf_filled_len = 0;
284 dummy.buflen = io_u->buflen;
286 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
288 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
289 "expected", vc->io_u->file);
293 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
295 unsigned long long offset;
297 offset = vc->io_u->offset;
298 offset += vc->hdr_num * hdr->len;
299 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
300 vc->name, vc->io_u->file->file_name, offset, hdr->len);
302 if (vc->good_crc && vc->bad_crc) {
303 log_err(" Expected CRC: ");
304 hexdump(vc->good_crc, vc->crc_len);
305 log_err(" Received CRC: ");
306 hexdump(vc->bad_crc, vc->crc_len);
309 dump_verify_buffers(hdr, vc);
313 * Return data area 'header_num'
315 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
317 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
320 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
322 struct thread_data *td = vc->td;
323 struct io_u *io_u = vc->io_u;
325 unsigned int header_size = __hdr_size(td->o.verify);
326 unsigned int len, mod, i, size, pattern_size;
328 pattern = td->o.verify_pattern;
329 pattern_size = td->o.verify_pattern_bytes;
330 if (pattern_size <= 1)
331 pattern_size = MAX_PATTERN_SIZE;
332 buf = (void *) hdr + header_size;
333 len = get_hdr_inc(td, io_u) - header_size;
334 mod = header_size % pattern_size;
336 for (i = 0; i < len; i += size) {
337 size = pattern_size - mod;
338 if (size > (len - i))
340 if (memcmp(buf + i, pattern + mod, size))
341 /* Let the slow compare find the first mismatch byte. */
346 for (; i < len; i++) {
347 if (buf[i] != pattern[mod]) {
350 bits = hweight8(buf[i] ^ pattern[mod]);
351 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
352 buf[i], pattern[mod], bits);
353 log_err("fio: bad pattern block offset %u\n", i);
354 dump_verify_buffers(hdr, vc);
358 if (mod == td->o.verify_pattern_bytes)
365 static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
367 struct thread_data *td = vc->td;
368 struct vhdr_meta *vh = hdr_priv(hdr);
369 struct io_u *io_u = vc->io_u;
372 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
374 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
377 if (td->o.verify_pattern_bytes)
378 ret |= verify_io_u_pattern(hdr, vc);
381 * For read-only workloads, the program cannot be certain of the
382 * last numberio written to a block. Checking of numberio will be
383 * done only for workloads that write data. For verify_only,
384 * numberio will be checked in the last iteration when the correct
385 * state of numberio, that would have been written to each block
386 * in a previous run of fio, has been reached.
388 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
390 if (!td->o.verify_only || td->o.loops == 0)
391 if (vh->numberio != io_u->numberio)
398 log_verify_failure(hdr, vc);
402 static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
404 void *p = io_u_verify_off(hdr, vc);
405 struct vhdr_xxhash *vh = hdr_priv(hdr);
409 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
411 state = XXH32_init(1);
412 XXH32_update(state, p, hdr->len - hdr_size(hdr));
413 hash = XXH32_digest(state);
415 if (vh->hash == hash)
419 vc->good_crc = &vh->hash;
421 vc->crc_len = sizeof(hash);
422 log_verify_failure(hdr, vc);
426 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
428 void *p = io_u_verify_off(hdr, vc);
429 struct vhdr_sha512 *vh = hdr_priv(hdr);
431 struct fio_sha512_ctx sha512_ctx = {
435 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
437 fio_sha512_init(&sha512_ctx);
438 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
440 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
444 vc->good_crc = vh->sha512;
445 vc->bad_crc = sha512_ctx.buf;
446 vc->crc_len = sizeof(vh->sha512);
447 log_verify_failure(hdr, vc);
451 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
453 void *p = io_u_verify_off(hdr, vc);
454 struct vhdr_sha256 *vh = hdr_priv(hdr);
456 struct fio_sha256_ctx sha256_ctx = {
460 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
462 fio_sha256_init(&sha256_ctx);
463 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
465 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
469 vc->good_crc = vh->sha256;
470 vc->bad_crc = sha256_ctx.buf;
471 vc->crc_len = sizeof(vh->sha256);
472 log_verify_failure(hdr, vc);
476 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
478 void *p = io_u_verify_off(hdr, vc);
479 struct vhdr_sha1 *vh = hdr_priv(hdr);
481 struct fio_sha1_ctx sha1_ctx = {
485 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
487 fio_sha1_init(&sha1_ctx);
488 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
490 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
494 vc->good_crc = vh->sha1;
495 vc->bad_crc = sha1_ctx.H;
496 vc->crc_len = sizeof(vh->sha1);
497 log_verify_failure(hdr, vc);
501 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
503 void *p = io_u_verify_off(hdr, vc);
504 struct vhdr_crc7 *vh = hdr_priv(hdr);
507 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
509 c = fio_crc7(p, hdr->len - hdr_size(hdr));
515 vc->good_crc = &vh->crc7;
518 log_verify_failure(hdr, vc);
522 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
524 void *p = io_u_verify_off(hdr, vc);
525 struct vhdr_crc16 *vh = hdr_priv(hdr);
528 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
530 c = fio_crc16(p, hdr->len - hdr_size(hdr));
536 vc->good_crc = &vh->crc16;
539 log_verify_failure(hdr, vc);
543 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
545 void *p = io_u_verify_off(hdr, vc);
546 struct vhdr_crc64 *vh = hdr_priv(hdr);
547 unsigned long long c;
549 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
551 c = fio_crc64(p, hdr->len - hdr_size(hdr));
557 vc->good_crc = &vh->crc64;
560 log_verify_failure(hdr, vc);
564 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
566 void *p = io_u_verify_off(hdr, vc);
567 struct vhdr_crc32 *vh = hdr_priv(hdr);
570 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
572 c = fio_crc32(p, hdr->len - hdr_size(hdr));
578 vc->good_crc = &vh->crc32;
581 log_verify_failure(hdr, vc);
585 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
587 void *p = io_u_verify_off(hdr, vc);
588 struct vhdr_crc32 *vh = hdr_priv(hdr);
591 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
593 c = fio_crc32c(p, hdr->len - hdr_size(hdr));
599 vc->good_crc = &vh->crc32;
602 log_verify_failure(hdr, vc);
606 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
608 void *p = io_u_verify_off(hdr, vc);
609 struct vhdr_md5 *vh = hdr_priv(hdr);
610 uint32_t hash[MD5_HASH_WORDS];
611 struct fio_md5_ctx md5_ctx = {
615 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
617 fio_md5_init(&md5_ctx);
618 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
620 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
624 vc->good_crc = vh->md5_digest;
625 vc->bad_crc = md5_ctx.hash;
626 vc->crc_len = sizeof(hash);
627 log_verify_failure(hdr, vc);
632 * Push IO verification to a separate thread
634 int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
636 struct io_u *io_u = *io_u_ptr;
638 pthread_mutex_lock(&td->io_u_lock);
641 put_file_log(td, io_u->file);
643 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
645 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
647 flist_add_tail(&io_u->verify_list, &td->verify_list);
649 pthread_mutex_unlock(&td->io_u_lock);
651 pthread_cond_signal(&td->verify_cond);
655 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
657 static char zero_buf[1024];
658 unsigned int this_len, len;
662 if (!td->o.trim_zero)
668 this_len = sizeof(zero_buf);
671 if (memcmp(p, zero_buf, this_len)) {
682 log_err("trim: verify failed at file %s offset %llu, length %lu"
683 ", block offset %lu\n",
684 io_u->file->file_name, io_u->offset, io_u->buflen,
685 (unsigned long) (p - io_u->buf));
689 static int verify_header(struct io_u *io_u, struct verify_header *hdr,
690 unsigned int hdr_num, unsigned int hdr_len)
695 if (hdr->magic != FIO_HDR_MAGIC) {
696 log_err("verify: bad magic header %x, wanted %x",
697 hdr->magic, FIO_HDR_MAGIC);
700 if (hdr->len != hdr_len) {
701 log_err("verify: bad header length %u, wanted %u",
705 if (hdr->rand_seed != io_u->rand_seed) {
706 log_err("verify: bad header rand_seed %"PRIu64
708 hdr->rand_seed, io_u->rand_seed);
712 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
713 if (crc != hdr->crc32) {
714 log_err("verify: bad header crc %x, calculated %x",
721 log_err(" at file %s offset %llu, length %u\n",
722 io_u->file->file_name,
723 io_u->offset + hdr_num * hdr_len, hdr_len);
727 int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
729 struct verify_header *hdr;
730 struct io_u *io_u = *io_u_ptr;
731 unsigned int header_size, hdr_inc, hdr_num = 0;
735 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
738 * If the IO engine is faking IO (like null), then just pretend
739 * we verified everything.
741 if (td->io_ops->flags & FIO_FAKEIO)
744 if (io_u->flags & IO_U_F_TRIMMED) {
745 ret = verify_trimmed_io_u(td, io_u);
749 hdr_inc = get_hdr_inc(td, io_u);
752 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
753 p += hdr_inc, hdr_num++) {
759 unsigned int verify_type;
761 if (ret && td->o.verify_fatal)
764 header_size = __hdr_size(td->o.verify);
765 if (td->o.verify_offset)
766 memswp(p, p + td->o.verify_offset, header_size);
770 * Make rand_seed check pass when have verifysort or
773 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
774 io_u->rand_seed = hdr->rand_seed;
776 ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
780 if (td->o.verify != VERIFY_NONE)
781 verify_type = td->o.verify;
783 verify_type = hdr->verify_type;
785 switch (verify_type) {
787 ret = verify_io_u_md5(hdr, &vc);
790 ret = verify_io_u_crc64(hdr, &vc);
793 case VERIFY_CRC32C_INTEL:
794 ret = verify_io_u_crc32c(hdr, &vc);
797 ret = verify_io_u_crc32(hdr, &vc);
800 ret = verify_io_u_crc16(hdr, &vc);
803 ret = verify_io_u_crc7(hdr, &vc);
806 ret = verify_io_u_sha256(hdr, &vc);
809 ret = verify_io_u_sha512(hdr, &vc);
812 ret = verify_io_u_xxhash(hdr, &vc);
815 ret = verify_io_u_meta(hdr, &vc);
818 ret = verify_io_u_sha1(hdr, &vc);
821 ret = verify_io_u_pattern(hdr, &vc);
824 log_err("Bad verify type %u\n", hdr->verify_type);
828 if (ret && verify_type != hdr->verify_type)
829 log_err("fio: verify type mismatch (%u media, %u given)\n",
830 hdr->verify_type, verify_type);
834 if (ret && td->o.verify_fatal)
835 fio_mark_td_terminate(td);
840 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
841 struct io_u *io_u, unsigned int header_num)
843 struct vhdr_meta *vh = hdr_priv(hdr);
845 vh->thread = td->thread_number;
847 vh->time_sec = io_u->start_time.tv_sec;
848 vh->time_usec = io_u->start_time.tv_usec;
850 vh->numberio = io_u->numberio;
852 vh->offset = io_u->offset + header_num * td->o.verify_interval;
855 static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
857 struct vhdr_xxhash *vh = hdr_priv(hdr);
860 state = XXH32_init(1);
861 XXH32_update(state, p, len);
862 vh->hash = XXH32_digest(state);
865 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
867 struct vhdr_sha512 *vh = hdr_priv(hdr);
868 struct fio_sha512_ctx sha512_ctx = {
872 fio_sha512_init(&sha512_ctx);
873 fio_sha512_update(&sha512_ctx, p, len);
876 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
878 struct vhdr_sha256 *vh = hdr_priv(hdr);
879 struct fio_sha256_ctx sha256_ctx = {
883 fio_sha256_init(&sha256_ctx);
884 fio_sha256_update(&sha256_ctx, p, len);
887 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
889 struct vhdr_sha1 *vh = hdr_priv(hdr);
890 struct fio_sha1_ctx sha1_ctx = {
894 fio_sha1_init(&sha1_ctx);
895 fio_sha1_update(&sha1_ctx, p, len);
898 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
900 struct vhdr_crc7 *vh = hdr_priv(hdr);
902 vh->crc7 = fio_crc7(p, len);
905 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
907 struct vhdr_crc16 *vh = hdr_priv(hdr);
909 vh->crc16 = fio_crc16(p, len);
912 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
914 struct vhdr_crc32 *vh = hdr_priv(hdr);
916 vh->crc32 = fio_crc32(p, len);
919 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
921 struct vhdr_crc32 *vh = hdr_priv(hdr);
923 vh->crc32 = fio_crc32c(p, len);
926 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
928 struct vhdr_crc64 *vh = hdr_priv(hdr);
930 vh->crc64 = fio_crc64(p, len);
933 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
935 struct vhdr_md5 *vh = hdr_priv(hdr);
936 struct fio_md5_ctx md5_ctx = {
937 .hash = (uint32_t *) vh->md5_digest,
940 fio_md5_init(&md5_ctx);
941 fio_md5_update(&md5_ctx, p, len);
944 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
945 struct verify_header *hdr, unsigned int header_num,
946 unsigned int header_len)
948 unsigned int data_len;
953 hdr->magic = FIO_HDR_MAGIC;
954 hdr->verify_type = td->o.verify;
955 hdr->len = header_len;
956 hdr->rand_seed = io_u->rand_seed;
957 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
959 data_len = header_len - hdr_size(hdr);
961 data = p + hdr_size(hdr);
962 switch (td->o.verify) {
964 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
966 fill_md5(hdr, data, data_len);
969 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
971 fill_crc64(hdr, data, data_len);
974 case VERIFY_CRC32C_INTEL:
975 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
977 fill_crc32c(hdr, data, data_len);
980 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
982 fill_crc32(hdr, data, data_len);
985 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
987 fill_crc16(hdr, data, data_len);
990 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
992 fill_crc7(hdr, data, data_len);
995 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
997 fill_sha256(hdr, data, data_len);
1000 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1002 fill_sha512(hdr, data, data_len);
1005 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1007 fill_xxhash(hdr, data, data_len);
1010 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1012 fill_meta(hdr, td, io_u, header_num);
1015 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1017 fill_sha1(hdr, data, data_len);
1019 case VERIFY_PATTERN:
1020 /* nothing to do here */
1023 log_err("fio: bad verify type: %d\n", td->o.verify);
1026 if (td->o.verify_offset)
1027 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
1031 * fill body of io_u->buf with random data and add a header with the
1032 * checksum of choice
1034 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1036 if (td->o.verify == VERIFY_NULL)
1039 io_u->numberio = td->io_issues[io_u->ddir];
1041 fill_pattern_headers(td, io_u, 0, 0);
1044 int get_next_verify(struct thread_data *td, struct io_u *io_u)
1046 struct io_piece *ipo = NULL;
1049 * this io_u is from a requeue, we already filled the offsets
1054 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1055 struct rb_node *n = rb_first(&td->io_hist_tree);
1057 ipo = rb_entry(n, struct io_piece, rb_node);
1060 * Ensure that the associated IO has completed
1063 if (ipo->flags & IP_F_IN_FLIGHT)
1066 rb_erase(n, &td->io_hist_tree);
1067 assert(ipo->flags & IP_F_ONRB);
1068 ipo->flags &= ~IP_F_ONRB;
1069 } else if (!flist_empty(&td->io_hist_list)) {
1070 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
1073 * Ensure that the associated IO has completed
1076 if (ipo->flags & IP_F_IN_FLIGHT)
1079 flist_del(&ipo->list);
1080 assert(ipo->flags & IP_F_ONLIST);
1081 ipo->flags &= ~IP_F_ONLIST;
1087 io_u->offset = ipo->offset;
1088 io_u->buflen = ipo->len;
1089 io_u->numberio = ipo->numberio;
1090 io_u->file = ipo->file;
1091 io_u->flags |= IO_U_F_VER_LIST;
1093 if (ipo->flags & IP_F_TRIMMED)
1094 io_u->flags |= IO_U_F_TRIMMED;
1096 if (!fio_file_open(io_u->file)) {
1097 int r = td_io_open_file(td, io_u->file);
1100 dprint(FD_VERIFY, "failed file %s open\n",
1101 io_u->file->file_name);
1106 get_file(ipo->file);
1107 assert(fio_file_open(io_u->file));
1108 io_u->ddir = DDIR_READ;
1109 io_u->xfer_buf = io_u->buf;
1110 io_u->xfer_buflen = io_u->buflen;
1112 remove_trim_entry(td, ipo);
1114 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
1116 if (!td->o.verify_pattern_bytes) {
1117 io_u->rand_seed = __rand(&td->verify_state);
1118 if (sizeof(int) != sizeof(long *))
1119 io_u->rand_seed *= __rand(&td->verify_state);
1125 dprint(FD_VERIFY, "get_next_verify: empty\n");
1129 void fio_verify_init(struct thread_data *td)
1131 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1132 td->o.verify == VERIFY_CRC32C) {
1133 crc32c_intel_probe();
1137 static void *verify_async_thread(void *data)
1139 struct thread_data *td = data;
1143 if (td->o.verify_cpumask_set &&
1144 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1145 log_err("fio: failed setting verify thread affinity\n");
1153 if (td->verify_thread_exit)
1156 pthread_mutex_lock(&td->io_u_lock);
1158 while (flist_empty(&td->verify_list) &&
1159 !td->verify_thread_exit) {
1160 ret = pthread_cond_wait(&td->verify_cond,
1163 pthread_mutex_unlock(&td->io_u_lock);
1168 flist_splice_init(&td->verify_list, &list);
1169 pthread_mutex_unlock(&td->io_u_lock);
1171 if (flist_empty(&list))
1174 while (!flist_empty(&list)) {
1175 io_u = flist_first_entry(&list, struct io_u, verify_list);
1176 flist_del_init(&io_u->verify_list);
1178 io_u->flags |= IO_U_F_NO_FILE_PUT;
1179 ret = verify_io_u(td, &io_u);
1184 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
1185 update_error_count(td, ret);
1193 td_verror(td, ret, "async_verify");
1194 if (td->o.verify_fatal)
1195 fio_mark_td_terminate(td);
1199 pthread_mutex_lock(&td->io_u_lock);
1200 td->nr_verify_threads--;
1201 pthread_mutex_unlock(&td->io_u_lock);
1203 pthread_cond_signal(&td->free_cond);
1207 int verify_async_init(struct thread_data *td)
1210 pthread_attr_t attr;
1212 pthread_attr_init(&attr);
1213 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1215 td->verify_thread_exit = 0;
1217 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1218 for (i = 0; i < td->o.verify_async; i++) {
1219 ret = pthread_create(&td->verify_threads[i], &attr,
1220 verify_async_thread, td);
1222 log_err("fio: async verify creation failed: %s\n",
1226 ret = pthread_detach(td->verify_threads[i]);
1228 log_err("fio: async verify thread detach failed: %s\n",
1232 td->nr_verify_threads++;
1235 pthread_attr_destroy(&attr);
1237 if (i != td->o.verify_async) {
1238 log_err("fio: only %d verify threads started, exiting\n", i);
1239 td->verify_thread_exit = 1;
1241 pthread_cond_broadcast(&td->verify_cond);
1248 void verify_async_exit(struct thread_data *td)
1250 td->verify_thread_exit = 1;
1252 pthread_cond_broadcast(&td->verify_cond);
1254 pthread_mutex_lock(&td->io_u_lock);
1256 while (td->nr_verify_threads)
1257 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1259 pthread_mutex_unlock(&td->io_u_lock);
1260 free(td->verify_threads);
1261 td->verify_threads = NULL;
1264 struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1266 struct all_io_list *rep;
1267 struct thread_data *td;
1272 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1275 * Calculate reply space needed. We need one 'io_state' per thread,
1276 * and the size will vary depending on depth.
1280 for_each_td(td, i) {
1281 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1284 td->flags |= TD_F_VSTATE_SAVED;
1285 depth += td->o.iodepth;
1293 *sz += nr * sizeof(struct thread_io_list);
1294 *sz += depth * sizeof(uint64_t);
1297 rep->threads = cpu_to_le64((uint64_t) nr);
1299 next = &rep->state[0];
1300 for_each_td(td, i) {
1301 struct thread_io_list *s = next;
1304 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1307 if (td->last_write_comp) {
1310 if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1311 comps = td->io_blocks[DDIR_WRITE];
1313 comps = td->o.iodepth;
1315 k = td->last_write_idx - 1;
1316 for (j = 0; j < comps; j++) {
1318 k = td->o.iodepth - 1;
1319 s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
1325 s->no_comps = cpu_to_le64((uint64_t) comps);
1326 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1327 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1328 s->index = cpu_to_le64((uint64_t) i);
1329 s->rand.s[0] = cpu_to_le32(td->random_state.s1);
1330 s->rand.s[1] = cpu_to_le32(td->random_state.s2);
1331 s->rand.s[2] = cpu_to_le32(td->random_state.s3);
1333 strncpy((char *) s->name, td->o.name, sizeof(s->name));
1334 next = io_list_next(s);
1340 static int open_state_file(const char *name, const char *prefix, int num,
1348 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1352 verify_state_gen_name(out, sizeof(out), name, prefix, num);
1354 fd = open(out, flags, 0644);
1356 perror("fio: open state file");
1363 static int write_thread_list_state(struct thread_io_list *s,
1366 struct verify_state_hdr hdr;
1371 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1375 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1377 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1378 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1379 hdr.crc = cpu_to_le64(crc);
1380 ret = write(fd, &hdr, sizeof(hdr));
1381 if (ret != sizeof(hdr))
1384 ret = write(fd, s, thread_io_list_sz(s));
1385 if (ret != thread_io_list_sz(s)) {
1388 perror("fio: write state file");
1389 log_err("fio: failed to write state file\n");
1398 void __verify_save_state(struct all_io_list *state, const char *prefix)
1400 struct thread_io_list *s = &state->state[0];
1403 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1404 write_thread_list_state(s, prefix);
1405 s = io_list_next(s);
1409 void verify_save_state(void)
1411 struct all_io_list *state;
1414 state = get_all_io_list(IO_LIST_ALL, &sz);
1416 __verify_save_state(state, "local");
1421 void verify_free_state(struct thread_data *td)
1427 void verify_convert_assign_state(struct thread_data *td,
1428 struct thread_io_list *s)
1432 s->no_comps = le64_to_cpu(s->no_comps);
1433 s->depth = le64_to_cpu(s->depth);
1434 s->numberio = le64_to_cpu(s->numberio);
1435 for (i = 0; i < 4; i++)
1436 s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
1437 for (i = 0; i < s->no_comps; i++)
1438 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1443 int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s)
1447 hdr->version = le64_to_cpu(hdr->version);
1448 hdr->size = le64_to_cpu(hdr->size);
1449 hdr->crc = le64_to_cpu(hdr->crc);
1451 if (hdr->version != VSTATE_HDR_VERSION)
1454 crc = fio_crc32c((void *)s, hdr->size);
1455 if (crc != hdr->crc)
1461 int verify_load_state(struct thread_data *td, const char *prefix)
1463 struct thread_io_list *s = NULL;
1464 struct verify_state_hdr hdr;
1469 if (!td->o.verify_state)
1472 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1476 ret = read(fd, &hdr, sizeof(hdr));
1477 if (ret != sizeof(hdr)) {
1479 td_verror(td, errno, "read verify state hdr");
1480 log_err("fio: failed reading verify state header\n");
1484 hdr.version = le64_to_cpu(hdr.version);
1485 hdr.size = le64_to_cpu(hdr.size);
1486 hdr.crc = le64_to_cpu(hdr.crc);
1488 if (hdr.version != VSTATE_HDR_VERSION) {
1489 log_err("fio: bad version in verify state header\n");
1493 s = malloc(hdr.size);
1494 ret = read(fd, s, hdr.size);
1495 if (ret != hdr.size) {
1497 td_verror(td, errno, "read verify state");
1498 log_err("fio: failed reading verity state\n");
1502 crc = fio_crc32c((void *)s, hdr.size);
1503 if (crc != hdr.crc) {
1504 log_err("fio: verify state is corrupt\n");
1510 verify_convert_assign_state(td, s);
1520 * Use the loaded verify state to know when to stop doing verification
1522 int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1524 struct thread_io_list *s = td->vstate;
1531 * If we're not into the window of issues - depth yet, continue
1533 if (td->io_blocks[DDIR_READ] < s->depth ||
1534 s->numberio - td->io_blocks[DDIR_READ] > s->depth)
1538 * We're in the window of having to check if this io was
1539 * completed or not. If the IO was seen as completed, then
1542 for (i = 0; i < s->no_comps; i++)
1543 if (io_u->offset == s->offsets[i])
1547 * Not found, we have to stop