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);
31 static void fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
34 void fill_buffer_pattern(struct thread_data *td, void *p, unsigned int len)
36 fill_pattern(p, len, td->o.buffer_pattern, td->o.buffer_pattern_bytes);
39 void __fill_buffer(struct thread_options *o, unsigned long seed, void *p,
42 __fill_random_buf_percentage(seed, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
45 unsigned long fill_buffer(struct thread_data *td, void *p, unsigned int len)
47 struct frand_state *fs = &td->verify_state;
48 struct thread_options *o = &td->o;
50 return fill_random_buf_percentage(fs, p, o->compress_percentage, len, len, o->buffer_pattern, o->buffer_pattern_bytes);
53 void fill_verify_pattern(struct thread_data *td, void *p, unsigned int len,
54 struct io_u *io_u, unsigned long seed, int use_seed)
56 struct thread_options *o = &td->o;
58 if (!o->verify_pattern_bytes) {
59 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
62 __fill_buffer(o, seed, p, len);
64 io_u->rand_seed = fill_buffer(td, p, len);
68 if (io_u->buf_filled_len >= len) {
69 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
70 o->verify_pattern_bytes, len);
74 fill_pattern(p, len, o->verify_pattern, o->verify_pattern_bytes);
75 io_u->buf_filled_len = len;
78 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
82 hdr_inc = io_u->buflen;
83 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
84 hdr_inc = td->o.verify_interval;
89 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
90 unsigned long seed, int use_seed)
92 unsigned int hdr_inc, header_num;
93 struct verify_header *hdr;
96 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
98 hdr_inc = get_hdr_inc(td, io_u);
100 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
102 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
107 static void memswp(void *buf1, void *buf2, unsigned int len)
111 assert(len <= sizeof(swap));
113 memcpy(&swap, buf1, len);
114 memcpy(buf1, buf2, len);
115 memcpy(buf2, &swap, len);
118 static void hexdump(void *buffer, int len)
120 unsigned char *p = buffer;
123 for (i = 0; i < len; i++)
124 log_err("%02x", p[i]);
129 * Prepare for separation of verify_header and checksum header
131 static inline unsigned int __hdr_size(int verify_type)
133 unsigned int len = 0;
135 switch (verify_type) {
141 len = sizeof(struct vhdr_md5);
144 len = sizeof(struct vhdr_crc64);
148 case VERIFY_CRC32C_INTEL:
149 len = sizeof(struct vhdr_crc32);
152 len = sizeof(struct vhdr_crc16);
155 len = sizeof(struct vhdr_crc7);
158 len = sizeof(struct vhdr_sha256);
161 len = sizeof(struct vhdr_sha512);
164 len = sizeof(struct vhdr_xxhash);
167 len = sizeof(struct vhdr_meta);
170 len = sizeof(struct vhdr_sha1);
173 case VERIFY_PATTERN_NO_HDR:
177 log_err("fio: unknown verify header!\n");
181 return len + sizeof(struct verify_header);
184 static inline unsigned int hdr_size(struct verify_header *hdr)
186 return __hdr_size(hdr->verify_type);
189 static void *hdr_priv(struct verify_header *hdr)
193 return priv + sizeof(struct verify_header);
197 * Verify container, pass info to verify handlers and allow them to
198 * pass info back in case of error
205 unsigned int hdr_num;
206 struct thread_data *td;
209 * Output, only valid in case of error
214 unsigned int crc_len;
217 #define DUMP_BUF_SZ 255
218 static int dump_buf_warned;
220 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
221 const char *type, struct fio_file *f)
223 char *ptr, fname[DUMP_BUF_SZ];
224 size_t buf_left = DUMP_BUF_SZ;
227 ptr = strdup(f->file_name);
229 fname[DUMP_BUF_SZ - 1] = '\0';
230 strncpy(fname, basename(ptr), DUMP_BUF_SZ - 1);
232 buf_left -= strlen(fname);
234 if (!dump_buf_warned) {
235 log_err("fio: verify failure dump buffer too small\n");
242 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
244 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
246 perror("open verify buf file");
251 ret = write(fd, buf, len);
255 perror("write verify buf file");
263 log_err(" %s data dumped as %s\n", type, fname);
268 * Dump the contents of the read block and re-generate the correct data
271 static void __dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
273 struct thread_data *td = vc->td;
274 struct io_u *io_u = vc->io_u;
275 unsigned long hdr_offset;
279 if (!td->o.verify_dump)
283 * Dump the contents we just read off disk
285 hdr_offset = vc->hdr_num * hdr->len;
287 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
288 "received", vc->io_u->file);
291 * Allocate a new buf and re-generate the original data
293 buf = malloc(io_u->buflen);
296 dummy.rand_seed = hdr->rand_seed;
297 dummy.buf_filled_len = 0;
298 dummy.buflen = io_u->buflen;
300 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
302 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
303 "expected", vc->io_u->file);
307 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
309 struct thread_data *td = vc->td;
310 struct verify_header shdr;
312 if (td->o.verify == VERIFY_PATTERN_NO_HDR) {
313 fill_hdr(&shdr, td->o.verify, vc->io_u->buflen, 0);
317 __dump_verify_buffers(hdr, vc);
320 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
322 unsigned long long offset;
324 offset = vc->io_u->offset;
325 offset += vc->hdr_num * hdr->len;
326 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
327 vc->name, vc->io_u->file->file_name, offset, hdr->len);
329 if (vc->good_crc && vc->bad_crc) {
330 log_err(" Expected CRC: ");
331 hexdump(vc->good_crc, vc->crc_len);
332 log_err(" Received CRC: ");
333 hexdump(vc->bad_crc, vc->crc_len);
336 dump_verify_buffers(hdr, vc);
340 * Return data area 'header_num'
342 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
344 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
347 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
349 struct thread_data *td = vc->td;
350 struct io_u *io_u = vc->io_u;
352 unsigned int header_size = __hdr_size(td->o.verify);
353 unsigned int len, mod, i, size, pattern_size;
355 pattern = td->o.verify_pattern;
356 pattern_size = td->o.verify_pattern_bytes;
357 if (pattern_size <= 1)
358 pattern_size = MAX_PATTERN_SIZE;
359 buf = (void *) hdr + header_size;
360 len = get_hdr_inc(td, io_u) - header_size;
361 mod = header_size % pattern_size;
363 for (i = 0; i < len; i += size) {
364 size = pattern_size - mod;
365 if (size > (len - i))
367 if (memcmp(buf + i, pattern + mod, size))
368 /* Let the slow compare find the first mismatch byte. */
373 for (; i < len; i++) {
374 if (buf[i] != pattern[mod]) {
377 bits = hweight8(buf[i] ^ pattern[mod]);
378 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
379 buf[i], pattern[mod], bits);
380 log_err("fio: bad pattern block offset %u\n", i);
381 dump_verify_buffers(hdr, vc);
385 if (mod == td->o.verify_pattern_bytes)
392 static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
394 struct thread_data *td = vc->td;
395 struct vhdr_meta *vh = hdr_priv(hdr);
396 struct io_u *io_u = vc->io_u;
399 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
401 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
404 if (td->o.verify_pattern_bytes)
405 ret |= verify_io_u_pattern(hdr, vc);
408 * For read-only workloads, the program cannot be certain of the
409 * last numberio written to a block. Checking of numberio will be
410 * done only for workloads that write data. For verify_only,
411 * numberio will be checked in the last iteration when the correct
412 * state of numberio, that would have been written to each block
413 * in a previous run of fio, has been reached.
415 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
417 if (!td->o.verify_only || td->o.loops == 0)
418 if (vh->numberio != io_u->numberio)
425 log_verify_failure(hdr, vc);
429 static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
431 void *p = io_u_verify_off(hdr, vc);
432 struct vhdr_xxhash *vh = hdr_priv(hdr);
436 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
438 state = XXH32_init(1);
439 XXH32_update(state, p, hdr->len - hdr_size(hdr));
440 hash = XXH32_digest(state);
442 if (vh->hash == hash)
446 vc->good_crc = &vh->hash;
448 vc->crc_len = sizeof(hash);
449 log_verify_failure(hdr, vc);
453 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
455 void *p = io_u_verify_off(hdr, vc);
456 struct vhdr_sha512 *vh = hdr_priv(hdr);
458 struct fio_sha512_ctx sha512_ctx = {
462 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
464 fio_sha512_init(&sha512_ctx);
465 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
467 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
471 vc->good_crc = vh->sha512;
472 vc->bad_crc = sha512_ctx.buf;
473 vc->crc_len = sizeof(vh->sha512);
474 log_verify_failure(hdr, vc);
478 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
480 void *p = io_u_verify_off(hdr, vc);
481 struct vhdr_sha256 *vh = hdr_priv(hdr);
483 struct fio_sha256_ctx sha256_ctx = {
487 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
489 fio_sha256_init(&sha256_ctx);
490 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
491 fio_sha256_final(&sha256_ctx);
493 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
497 vc->good_crc = vh->sha256;
498 vc->bad_crc = sha256_ctx.buf;
499 vc->crc_len = sizeof(vh->sha256);
500 log_verify_failure(hdr, vc);
504 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
506 void *p = io_u_verify_off(hdr, vc);
507 struct vhdr_sha1 *vh = hdr_priv(hdr);
509 struct fio_sha1_ctx sha1_ctx = {
513 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
515 fio_sha1_init(&sha1_ctx);
516 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
517 fio_sha1_final(&sha1_ctx);
519 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
523 vc->good_crc = vh->sha1;
524 vc->bad_crc = sha1_ctx.H;
525 vc->crc_len = sizeof(vh->sha1);
526 log_verify_failure(hdr, vc);
530 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
532 void *p = io_u_verify_off(hdr, vc);
533 struct vhdr_crc7 *vh = hdr_priv(hdr);
536 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
538 c = fio_crc7(p, hdr->len - hdr_size(hdr));
544 vc->good_crc = &vh->crc7;
547 log_verify_failure(hdr, vc);
551 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
553 void *p = io_u_verify_off(hdr, vc);
554 struct vhdr_crc16 *vh = hdr_priv(hdr);
557 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
559 c = fio_crc16(p, hdr->len - hdr_size(hdr));
565 vc->good_crc = &vh->crc16;
568 log_verify_failure(hdr, vc);
572 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
574 void *p = io_u_verify_off(hdr, vc);
575 struct vhdr_crc64 *vh = hdr_priv(hdr);
576 unsigned long long c;
578 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
580 c = fio_crc64(p, hdr->len - hdr_size(hdr));
586 vc->good_crc = &vh->crc64;
589 log_verify_failure(hdr, vc);
593 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
595 void *p = io_u_verify_off(hdr, vc);
596 struct vhdr_crc32 *vh = hdr_priv(hdr);
599 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
601 c = fio_crc32(p, hdr->len - hdr_size(hdr));
607 vc->good_crc = &vh->crc32;
610 log_verify_failure(hdr, vc);
614 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
616 void *p = io_u_verify_off(hdr, vc);
617 struct vhdr_crc32 *vh = hdr_priv(hdr);
620 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
622 c = fio_crc32c(p, hdr->len - hdr_size(hdr));
628 vc->good_crc = &vh->crc32;
631 log_verify_failure(hdr, vc);
635 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
637 void *p = io_u_verify_off(hdr, vc);
638 struct vhdr_md5 *vh = hdr_priv(hdr);
639 uint32_t hash[MD5_HASH_WORDS];
640 struct fio_md5_ctx md5_ctx = {
644 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
646 fio_md5_init(&md5_ctx);
647 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
648 fio_md5_final(&md5_ctx);
650 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
654 vc->good_crc = vh->md5_digest;
655 vc->bad_crc = md5_ctx.hash;
656 vc->crc_len = sizeof(hash);
657 log_verify_failure(hdr, vc);
662 * Push IO verification to a separate thread
664 int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
666 struct io_u *io_u = *io_u_ptr;
668 pthread_mutex_lock(&td->io_u_lock);
671 put_file_log(td, io_u->file);
673 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
675 io_u_clear(io_u, IO_U_F_IN_CUR_DEPTH);
677 flist_add_tail(&io_u->verify_list, &td->verify_list);
679 pthread_mutex_unlock(&td->io_u_lock);
681 pthread_cond_signal(&td->verify_cond);
685 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
687 static char zero_buf[1024];
688 unsigned int this_len, len;
692 if (!td->o.trim_zero)
698 this_len = sizeof(zero_buf);
701 if (memcmp(p, zero_buf, this_len)) {
712 log_err("trim: verify failed at file %s offset %llu, length %lu"
713 ", block offset %lu\n",
714 io_u->file->file_name, io_u->offset, io_u->buflen,
715 (unsigned long) (p - io_u->buf));
719 static int verify_header(struct io_u *io_u, struct verify_header *hdr,
720 unsigned int hdr_num, unsigned int hdr_len)
725 if (hdr->magic != FIO_HDR_MAGIC) {
726 log_err("verify: bad magic header %x, wanted %x",
727 hdr->magic, FIO_HDR_MAGIC);
730 if (hdr->len != hdr_len) {
731 log_err("verify: bad header length %u, wanted %u",
735 if (hdr->rand_seed != io_u->rand_seed) {
736 log_err("verify: bad header rand_seed %"PRIu64
738 hdr->rand_seed, io_u->rand_seed);
742 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
743 if (crc != hdr->crc32) {
744 log_err("verify: bad header crc %x, calculated %x",
751 log_err(" at file %s offset %llu, length %u\n",
752 io_u->file->file_name,
753 io_u->offset + hdr_num * hdr_len, hdr_len);
757 int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
759 struct verify_header *hdr;
760 struct io_u *io_u = *io_u_ptr;
761 unsigned int header_size, hdr_inc, hdr_num = 0;
765 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
768 * If the IO engine is faking IO (like null), then just pretend
769 * we verified everything.
771 if (td->io_ops->flags & FIO_FAKEIO)
774 if (io_u->flags & IO_U_F_TRIMMED) {
775 ret = verify_trimmed_io_u(td, io_u);
779 hdr_inc = get_hdr_inc(td, io_u);
782 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
783 p += hdr_inc, hdr_num++) {
789 unsigned int verify_type;
791 if (ret && td->o.verify_fatal)
794 header_size = __hdr_size(td->o.verify);
795 if (td->o.verify_offset)
796 memswp(p, p + td->o.verify_offset, header_size);
800 * Make rand_seed check pass when have verifysort or
803 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
804 io_u->rand_seed = hdr->rand_seed;
806 if (td->o.verify != VERIFY_PATTERN_NO_HDR) {
807 ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
812 if (td->o.verify != VERIFY_NONE)
813 verify_type = td->o.verify;
815 verify_type = hdr->verify_type;
817 switch (verify_type) {
819 ret = verify_io_u_md5(hdr, &vc);
822 ret = verify_io_u_crc64(hdr, &vc);
825 case VERIFY_CRC32C_INTEL:
826 ret = verify_io_u_crc32c(hdr, &vc);
829 ret = verify_io_u_crc32(hdr, &vc);
832 ret = verify_io_u_crc16(hdr, &vc);
835 ret = verify_io_u_crc7(hdr, &vc);
838 ret = verify_io_u_sha256(hdr, &vc);
841 ret = verify_io_u_sha512(hdr, &vc);
844 ret = verify_io_u_xxhash(hdr, &vc);
847 ret = verify_io_u_meta(hdr, &vc);
850 ret = verify_io_u_sha1(hdr, &vc);
853 case VERIFY_PATTERN_NO_HDR:
854 ret = verify_io_u_pattern(hdr, &vc);
857 log_err("Bad verify type %u\n", hdr->verify_type);
861 if (ret && verify_type != hdr->verify_type)
862 log_err("fio: verify type mismatch (%u media, %u given)\n",
863 hdr->verify_type, verify_type);
867 if (ret && td->o.verify_fatal)
868 fio_mark_td_terminate(td);
873 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
874 struct io_u *io_u, unsigned int header_num)
876 struct vhdr_meta *vh = hdr_priv(hdr);
878 vh->thread = td->thread_number;
880 vh->time_sec = io_u->start_time.tv_sec;
881 vh->time_usec = io_u->start_time.tv_usec;
883 vh->numberio = io_u->numberio;
885 vh->offset = io_u->offset + header_num * td->o.verify_interval;
888 static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
890 struct vhdr_xxhash *vh = hdr_priv(hdr);
893 state = XXH32_init(1);
894 XXH32_update(state, p, len);
895 vh->hash = XXH32_digest(state);
898 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
900 struct vhdr_sha512 *vh = hdr_priv(hdr);
901 struct fio_sha512_ctx sha512_ctx = {
905 fio_sha512_init(&sha512_ctx);
906 fio_sha512_update(&sha512_ctx, p, len);
909 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
911 struct vhdr_sha256 *vh = hdr_priv(hdr);
912 struct fio_sha256_ctx sha256_ctx = {
916 fio_sha256_init(&sha256_ctx);
917 fio_sha256_update(&sha256_ctx, p, len);
918 fio_sha256_final(&sha256_ctx);
921 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
923 struct vhdr_sha1 *vh = hdr_priv(hdr);
924 struct fio_sha1_ctx sha1_ctx = {
928 fio_sha1_init(&sha1_ctx);
929 fio_sha1_update(&sha1_ctx, p, len);
930 fio_sha1_final(&sha1_ctx);
933 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
935 struct vhdr_crc7 *vh = hdr_priv(hdr);
937 vh->crc7 = fio_crc7(p, len);
940 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
942 struct vhdr_crc16 *vh = hdr_priv(hdr);
944 vh->crc16 = fio_crc16(p, len);
947 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
949 struct vhdr_crc32 *vh = hdr_priv(hdr);
951 vh->crc32 = fio_crc32(p, len);
954 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
956 struct vhdr_crc32 *vh = hdr_priv(hdr);
958 vh->crc32 = fio_crc32c(p, len);
961 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
963 struct vhdr_crc64 *vh = hdr_priv(hdr);
965 vh->crc64 = fio_crc64(p, len);
968 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
970 struct vhdr_md5 *vh = hdr_priv(hdr);
971 struct fio_md5_ctx md5_ctx = {
972 .hash = (uint32_t *) vh->md5_digest,
975 fio_md5_init(&md5_ctx);
976 fio_md5_update(&md5_ctx, p, len);
977 fio_md5_final(&md5_ctx);
980 static void fill_hdr(struct verify_header *hdr, int verify_type, uint32_t len,
985 hdr->magic = FIO_HDR_MAGIC;
986 hdr->verify_type = verify_type;
988 hdr->rand_seed = rand_seed;
989 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
992 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
993 struct verify_header *hdr, unsigned int header_num,
994 unsigned int header_len)
996 unsigned int data_len;
999 if (td->o.verify == VERIFY_PATTERN_NO_HDR)
1004 fill_hdr(hdr, td->o.verify, header_len, io_u->rand_seed);
1006 data_len = header_len - hdr_size(hdr);
1008 data = p + hdr_size(hdr);
1009 switch (td->o.verify) {
1011 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
1013 fill_md5(hdr, data, data_len);
1016 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
1018 fill_crc64(hdr, data, data_len);
1021 case VERIFY_CRC32C_INTEL:
1022 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
1024 fill_crc32c(hdr, data, data_len);
1027 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
1029 fill_crc32(hdr, data, data_len);
1032 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
1034 fill_crc16(hdr, data, data_len);
1037 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
1039 fill_crc7(hdr, data, data_len);
1042 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
1044 fill_sha256(hdr, data, data_len);
1047 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
1049 fill_sha512(hdr, data, data_len);
1052 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1054 fill_xxhash(hdr, data, data_len);
1057 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1059 fill_meta(hdr, td, io_u, header_num);
1062 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1064 fill_sha1(hdr, data, data_len);
1066 case VERIFY_PATTERN:
1067 /* nothing to do here */
1070 log_err("fio: bad verify type: %d\n", td->o.verify);
1073 if (td->o.verify_offset)
1074 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
1078 * fill body of io_u->buf with random data and add a header with the
1079 * checksum of choice
1081 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1083 if (td->o.verify == VERIFY_NULL)
1086 io_u->numberio = td->io_issues[io_u->ddir];
1088 fill_pattern_headers(td, io_u, 0, 0);
1091 int get_next_verify(struct thread_data *td, struct io_u *io_u)
1093 struct io_piece *ipo = NULL;
1096 * this io_u is from a requeue, we already filled the offsets
1101 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1102 struct rb_node *n = rb_first(&td->io_hist_tree);
1104 ipo = rb_entry(n, struct io_piece, rb_node);
1107 * Ensure that the associated IO has completed
1110 if (ipo->flags & IP_F_IN_FLIGHT)
1113 rb_erase(n, &td->io_hist_tree);
1114 assert(ipo->flags & IP_F_ONRB);
1115 ipo->flags &= ~IP_F_ONRB;
1116 } else if (!flist_empty(&td->io_hist_list)) {
1117 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
1120 * Ensure that the associated IO has completed
1123 if (ipo->flags & IP_F_IN_FLIGHT)
1126 flist_del(&ipo->list);
1127 assert(ipo->flags & IP_F_ONLIST);
1128 ipo->flags &= ~IP_F_ONLIST;
1134 io_u->offset = ipo->offset;
1135 io_u->buflen = ipo->len;
1136 io_u->numberio = ipo->numberio;
1137 io_u->file = ipo->file;
1138 io_u_set(io_u, IO_U_F_VER_LIST);
1140 if (ipo->flags & IP_F_TRIMMED)
1141 io_u_set(io_u, IO_U_F_TRIMMED);
1143 if (!fio_file_open(io_u->file)) {
1144 int r = td_io_open_file(td, io_u->file);
1147 dprint(FD_VERIFY, "failed file %s open\n",
1148 io_u->file->file_name);
1153 get_file(ipo->file);
1154 assert(fio_file_open(io_u->file));
1155 io_u->ddir = DDIR_READ;
1156 io_u->xfer_buf = io_u->buf;
1157 io_u->xfer_buflen = io_u->buflen;
1159 remove_trim_entry(td, ipo);
1161 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
1163 if (!td->o.verify_pattern_bytes) {
1164 io_u->rand_seed = __rand(&td->verify_state);
1165 if (sizeof(int) != sizeof(long *))
1166 io_u->rand_seed *= __rand(&td->verify_state);
1172 dprint(FD_VERIFY, "get_next_verify: empty\n");
1176 void fio_verify_init(struct thread_data *td)
1178 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1179 td->o.verify == VERIFY_CRC32C) {
1180 crc32c_intel_probe();
1184 static void *verify_async_thread(void *data)
1186 struct thread_data *td = data;
1190 if (fio_option_is_set(&td->o, verify_cpumask) &&
1191 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1192 log_err("fio: failed setting verify thread affinity\n");
1200 if (td->verify_thread_exit)
1203 pthread_mutex_lock(&td->io_u_lock);
1205 while (flist_empty(&td->verify_list) &&
1206 !td->verify_thread_exit) {
1207 ret = pthread_cond_wait(&td->verify_cond,
1210 pthread_mutex_unlock(&td->io_u_lock);
1215 flist_splice_init(&td->verify_list, &list);
1216 pthread_mutex_unlock(&td->io_u_lock);
1218 if (flist_empty(&list))
1221 while (!flist_empty(&list)) {
1222 io_u = flist_first_entry(&list, struct io_u, verify_list);
1223 flist_del_init(&io_u->verify_list);
1225 io_u_set(io_u, IO_U_F_NO_FILE_PUT);
1226 ret = verify_io_u(td, &io_u);
1231 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
1232 update_error_count(td, ret);
1240 td_verror(td, ret, "async_verify");
1241 if (td->o.verify_fatal)
1242 fio_mark_td_terminate(td);
1246 pthread_mutex_lock(&td->io_u_lock);
1247 td->nr_verify_threads--;
1248 pthread_mutex_unlock(&td->io_u_lock);
1250 pthread_cond_signal(&td->free_cond);
1254 int verify_async_init(struct thread_data *td)
1257 pthread_attr_t attr;
1259 pthread_attr_init(&attr);
1260 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1262 td->verify_thread_exit = 0;
1264 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1265 for (i = 0; i < td->o.verify_async; i++) {
1266 ret = pthread_create(&td->verify_threads[i], &attr,
1267 verify_async_thread, td);
1269 log_err("fio: async verify creation failed: %s\n",
1273 ret = pthread_detach(td->verify_threads[i]);
1275 log_err("fio: async verify thread detach failed: %s\n",
1279 td->nr_verify_threads++;
1282 pthread_attr_destroy(&attr);
1284 if (i != td->o.verify_async) {
1285 log_err("fio: only %d verify threads started, exiting\n", i);
1286 td->verify_thread_exit = 1;
1288 pthread_cond_broadcast(&td->verify_cond);
1295 void verify_async_exit(struct thread_data *td)
1297 td->verify_thread_exit = 1;
1299 pthread_cond_broadcast(&td->verify_cond);
1301 pthread_mutex_lock(&td->io_u_lock);
1303 while (td->nr_verify_threads)
1304 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1306 pthread_mutex_unlock(&td->io_u_lock);
1307 free(td->verify_threads);
1308 td->verify_threads = NULL;
1311 struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1313 struct all_io_list *rep;
1314 struct thread_data *td;
1319 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1322 * Calculate reply space needed. We need one 'io_state' per thread,
1323 * and the size will vary depending on depth.
1327 for_each_td(td, i) {
1328 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1331 td->flags |= TD_F_VSTATE_SAVED;
1332 depth += td->o.iodepth;
1340 *sz += nr * sizeof(struct thread_io_list);
1341 *sz += depth * sizeof(uint64_t);
1344 rep->threads = cpu_to_le64((uint64_t) nr);
1346 next = &rep->state[0];
1347 for_each_td(td, i) {
1348 struct thread_io_list *s = next;
1351 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1354 if (td->last_write_comp) {
1357 if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1358 comps = td->io_blocks[DDIR_WRITE];
1360 comps = td->o.iodepth;
1362 k = td->last_write_idx - 1;
1363 for (j = 0; j < comps; j++) {
1365 k = td->o.iodepth - 1;
1366 s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
1372 s->no_comps = cpu_to_le64((uint64_t) comps);
1373 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1374 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1375 s->index = cpu_to_le64((uint64_t) i);
1376 if (td->random_state.use64) {
1377 s->rand.state64.s[0] = cpu_to_le64(td->random_state.state64.s1);
1378 s->rand.state64.s[1] = cpu_to_le64(td->random_state.state64.s2);
1379 s->rand.state64.s[2] = cpu_to_le64(td->random_state.state64.s3);
1380 s->rand.state64.s[3] = cpu_to_le64(td->random_state.state64.s4);
1381 s->rand.state64.s[4] = cpu_to_le64(td->random_state.state64.s5);
1382 s->rand.state64.s[5] = 0;
1383 s->rand.use64 = cpu_to_le64((uint64_t)1);
1385 s->rand.state32.s[0] = cpu_to_le32(td->random_state.state32.s1);
1386 s->rand.state32.s[1] = cpu_to_le32(td->random_state.state32.s2);
1387 s->rand.state32.s[2] = cpu_to_le32(td->random_state.state32.s3);
1388 s->rand.state32.s[3] = 0;
1391 s->name[sizeof(s->name) - 1] = '\0';
1392 strncpy((char *) s->name, td->o.name, sizeof(s->name) - 1);
1393 next = io_list_next(s);
1399 static int open_state_file(const char *name, const char *prefix, int num,
1407 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1411 verify_state_gen_name(out, sizeof(out), name, prefix, num);
1413 fd = open(out, flags, 0644);
1415 perror("fio: open state file");
1422 static int write_thread_list_state(struct thread_io_list *s,
1425 struct verify_state_hdr hdr;
1430 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1434 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1436 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1437 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1438 hdr.crc = cpu_to_le64(crc);
1439 ret = write(fd, &hdr, sizeof(hdr));
1440 if (ret != sizeof(hdr))
1443 ret = write(fd, s, thread_io_list_sz(s));
1444 if (ret != thread_io_list_sz(s)) {
1447 perror("fio: write state file");
1448 log_err("fio: failed to write state file\n");
1457 void __verify_save_state(struct all_io_list *state, const char *prefix)
1459 struct thread_io_list *s = &state->state[0];
1462 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1463 write_thread_list_state(s, prefix);
1464 s = io_list_next(s);
1468 void verify_save_state(void)
1470 struct all_io_list *state;
1473 state = get_all_io_list(IO_LIST_ALL, &sz);
1475 __verify_save_state(state, "local");
1480 void verify_free_state(struct thread_data *td)
1486 static struct thread_io_list *convert_v1_list(struct thread_io_list_v1 *s)
1488 struct thread_io_list *til;
1491 til = malloc(__thread_io_list_sz(s->no_comps));
1492 til->no_comps = s->no_comps;
1493 til->depth = s->depth;
1494 til->numberio = s->numberio;
1495 til->index = s->index;
1496 memcpy(til->name, s->name, sizeof(til->name));
1498 til->rand.use64 = 0;
1499 for (i = 0; i < 4; i++)
1500 til->rand.state32.s[i] = s->rand.s[i];
1502 for (i = 0; i < s->no_comps; i++)
1503 til->offsets[i] = s->offsets[i];
1508 void verify_convert_assign_state(struct thread_data *td, void *p, int version)
1510 struct thread_io_list *til;
1514 struct thread_io_list_v1 *s = p;
1516 s->no_comps = le64_to_cpu(s->no_comps);
1517 s->depth = le64_to_cpu(s->depth);
1518 s->numberio = le64_to_cpu(s->numberio);
1519 for (i = 0; i < 4; i++)
1520 s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
1521 for (i = 0; i < s->no_comps; i++)
1522 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1524 til = convert_v1_list(s);
1527 struct thread_io_list *s = p;
1529 s->no_comps = le64_to_cpu(s->no_comps);
1530 s->depth = le64_to_cpu(s->depth);
1531 s->numberio = le64_to_cpu(s->numberio);
1532 s->rand.use64 = le64_to_cpu(s->rand.use64);
1534 if (s->rand.use64) {
1535 for (i = 0; i < 6; i++)
1536 s->rand.state64.s[i] = le64_to_cpu(s->rand.state64.s[i]);
1538 for (i = 0; i < 4; i++)
1539 s->rand.state32.s[i] = le32_to_cpu(s->rand.state32.s[i]);
1541 for (i = 0; i < s->no_comps; i++)
1542 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1550 int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s,
1555 hdr->version = le64_to_cpu(hdr->version);
1556 hdr->size = le64_to_cpu(hdr->size);
1557 hdr->crc = le64_to_cpu(hdr->crc);
1559 if (hdr->version != VSTATE_HDR_VERSION ||
1560 hdr->version != VSTATE_HDR_VERSION_V1)
1563 crc = fio_crc32c((void *)s, hdr->size);
1564 if (crc != hdr->crc)
1567 *version = hdr->version;
1571 int verify_load_state(struct thread_data *td, const char *prefix)
1573 struct verify_state_hdr hdr;
1579 if (!td->o.verify_state)
1582 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1586 ret = read(fd, &hdr, sizeof(hdr));
1587 if (ret != sizeof(hdr)) {
1589 td_verror(td, errno, "read verify state hdr");
1590 log_err("fio: failed reading verify state header\n");
1594 hdr.version = le64_to_cpu(hdr.version);
1595 hdr.size = le64_to_cpu(hdr.size);
1596 hdr.crc = le64_to_cpu(hdr.crc);
1598 if (hdr.version != VSTATE_HDR_VERSION &&
1599 hdr.version != VSTATE_HDR_VERSION_V1) {
1600 log_err("fio: bad version in verify state header\n");
1604 s = malloc(hdr.size);
1605 ret = read(fd, s, hdr.size);
1606 if (ret != hdr.size) {
1608 td_verror(td, errno, "read verify state");
1609 log_err("fio: failed reading verity state\n");
1613 crc = fio_crc32c(s, hdr.size);
1614 if (crc != hdr.crc) {
1615 log_err("fio: verify state is corrupt\n");
1621 verify_convert_assign_state(td, s, hdr.version);
1631 * Use the loaded verify state to know when to stop doing verification
1633 int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1635 struct thread_io_list *s = td->vstate;
1642 * If we're not into the window of issues - depth yet, continue. If
1643 * issue is shorter than depth, do check.
1645 if ((td->io_blocks[DDIR_READ] < s->depth ||
1646 s->numberio - td->io_blocks[DDIR_READ] > s->depth) &&
1647 s->numberio > s->depth)
1651 * We're in the window of having to check if this io was
1652 * completed or not. If the IO was seen as completed, then
1655 for (i = 0; i < s->no_comps; i++)
1656 if (io_u->offset == s->offsets[i])
1660 * Not found, we have to stop