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 if (!td->o.verify_pattern_bytes) {
41 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
44 __fill_random_buf(p, len, seed);
46 io_u->rand_seed = fill_random_buf(&td->verify_state, p, len);
50 if (io_u->buf_filled_len >= len) {
51 dprint(FD_VERIFY, "using already filled verify pattern b=%d len=%u\n",
52 td->o.verify_pattern_bytes, len);
56 fill_pattern(p, len, td->o.verify_pattern, td->o.verify_pattern_bytes);
58 io_u->buf_filled_len = len;
61 static unsigned int get_hdr_inc(struct thread_data *td, struct io_u *io_u)
65 hdr_inc = io_u->buflen;
66 if (td->o.verify_interval && td->o.verify_interval <= io_u->buflen)
67 hdr_inc = td->o.verify_interval;
72 static void fill_pattern_headers(struct thread_data *td, struct io_u *io_u,
73 unsigned long seed, int use_seed)
75 unsigned int hdr_inc, header_num;
76 struct verify_header *hdr;
79 fill_verify_pattern(td, p, io_u->buflen, io_u, seed, use_seed);
81 hdr_inc = get_hdr_inc(td, io_u);
83 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
85 populate_hdr(td, io_u, hdr, header_num, hdr_inc);
90 static void memswp(void *buf1, void *buf2, unsigned int len)
94 assert(len <= sizeof(swap));
96 memcpy(&swap, buf1, len);
97 memcpy(buf1, buf2, len);
98 memcpy(buf2, &swap, len);
101 static void hexdump(void *buffer, int len)
103 unsigned char *p = buffer;
106 for (i = 0; i < len; i++)
107 log_err("%02x", p[i]);
112 * Prepare for separation of verify_header and checksum header
114 static inline unsigned int __hdr_size(int verify_type)
116 unsigned int len = 0;
118 switch (verify_type) {
124 len = sizeof(struct vhdr_md5);
127 len = sizeof(struct vhdr_crc64);
131 case VERIFY_CRC32C_INTEL:
132 len = sizeof(struct vhdr_crc32);
135 len = sizeof(struct vhdr_crc16);
138 len = sizeof(struct vhdr_crc7);
141 len = sizeof(struct vhdr_sha256);
144 len = sizeof(struct vhdr_sha512);
147 len = sizeof(struct vhdr_xxhash);
150 len = sizeof(struct vhdr_meta);
153 len = sizeof(struct vhdr_sha1);
159 log_err("fio: unknown verify header!\n");
163 return len + sizeof(struct verify_header);
166 static inline unsigned int hdr_size(struct verify_header *hdr)
168 return __hdr_size(hdr->verify_type);
171 static void *hdr_priv(struct verify_header *hdr)
175 return priv + sizeof(struct verify_header);
179 * Verify container, pass info to verify handlers and allow them to
180 * pass info back in case of error
187 unsigned int hdr_num;
188 struct thread_data *td;
191 * Output, only valid in case of error
196 unsigned int crc_len;
199 #define DUMP_BUF_SZ 255
200 static int dump_buf_warned;
202 static void dump_buf(char *buf, unsigned int len, unsigned long long offset,
203 const char *type, struct fio_file *f)
205 char *ptr, fname[DUMP_BUF_SZ];
206 size_t buf_left = DUMP_BUF_SZ;
209 ptr = strdup(f->file_name);
211 fname[DUMP_BUF_SZ - 1] = '\0';
212 strncpy(fname, basename(ptr), DUMP_BUF_SZ - 1);
214 buf_left -= strlen(fname);
216 if (!dump_buf_warned) {
217 log_err("fio: verify failure dump buffer too small\n");
224 snprintf(fname + strlen(fname), buf_left, ".%llu.%s", offset, type);
226 fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0644);
228 perror("open verify buf file");
233 ret = write(fd, buf, len);
237 perror("write verify buf file");
245 log_err(" %s data dumped as %s\n", type, fname);
250 * Dump the contents of the read block and re-generate the correct data
253 static void dump_verify_buffers(struct verify_header *hdr, struct vcont *vc)
255 struct thread_data *td = vc->td;
256 struct io_u *io_u = vc->io_u;
257 unsigned long hdr_offset;
261 if (!td->o.verify_dump)
265 * Dump the contents we just read off disk
267 hdr_offset = vc->hdr_num * hdr->len;
269 dump_buf(io_u->buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
270 "received", vc->io_u->file);
273 * Allocate a new buf and re-generate the original data
275 buf = malloc(io_u->buflen);
278 dummy.rand_seed = hdr->rand_seed;
279 dummy.buf_filled_len = 0;
280 dummy.buflen = io_u->buflen;
282 fill_pattern_headers(td, &dummy, hdr->rand_seed, 1);
284 dump_buf(buf + hdr_offset, hdr->len, io_u->offset + hdr_offset,
285 "expected", vc->io_u->file);
289 static void log_verify_failure(struct verify_header *hdr, struct vcont *vc)
291 unsigned long long offset;
293 offset = vc->io_u->offset;
294 offset += vc->hdr_num * hdr->len;
295 log_err("%.8s: verify failed at file %s offset %llu, length %u\n",
296 vc->name, vc->io_u->file->file_name, offset, hdr->len);
298 if (vc->good_crc && vc->bad_crc) {
299 log_err(" Expected CRC: ");
300 hexdump(vc->good_crc, vc->crc_len);
301 log_err(" Received CRC: ");
302 hexdump(vc->bad_crc, vc->crc_len);
305 dump_verify_buffers(hdr, vc);
309 * Return data area 'header_num'
311 static inline void *io_u_verify_off(struct verify_header *hdr, struct vcont *vc)
313 return vc->io_u->buf + vc->hdr_num * hdr->len + hdr_size(hdr);
316 static int verify_io_u_pattern(struct verify_header *hdr, struct vcont *vc)
318 struct thread_data *td = vc->td;
319 struct io_u *io_u = vc->io_u;
321 unsigned int header_size = __hdr_size(td->o.verify);
322 unsigned int len, mod, i, size, pattern_size;
324 pattern = td->o.verify_pattern;
325 pattern_size = td->o.verify_pattern_bytes;
326 if (pattern_size <= 1)
327 pattern_size = MAX_PATTERN_SIZE;
328 buf = (void *) hdr + header_size;
329 len = get_hdr_inc(td, io_u) - header_size;
330 mod = header_size % pattern_size;
332 for (i = 0; i < len; i += size) {
333 size = pattern_size - mod;
334 if (size > (len - i))
336 if (memcmp(buf + i, pattern + mod, size))
337 /* Let the slow compare find the first mismatch byte. */
342 for (; i < len; i++) {
343 if (buf[i] != pattern[mod]) {
346 bits = hweight8(buf[i] ^ pattern[mod]);
347 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
348 buf[i], pattern[mod], bits);
349 log_err("fio: bad pattern block offset %u\n", i);
350 dump_verify_buffers(hdr, vc);
354 if (mod == td->o.verify_pattern_bytes)
361 static int verify_io_u_meta(struct verify_header *hdr, struct vcont *vc)
363 struct thread_data *td = vc->td;
364 struct vhdr_meta *vh = hdr_priv(hdr);
365 struct io_u *io_u = vc->io_u;
368 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
370 if (vh->offset == io_u->offset + vc->hdr_num * td->o.verify_interval)
373 if (td->o.verify_pattern_bytes)
374 ret |= verify_io_u_pattern(hdr, vc);
377 * For read-only workloads, the program cannot be certain of the
378 * last numberio written to a block. Checking of numberio will be
379 * done only for workloads that write data. For verify_only,
380 * numberio will be checked in the last iteration when the correct
381 * state of numberio, that would have been written to each block
382 * in a previous run of fio, has been reached.
384 if ((td_write(td) || td_rw(td)) && (td_min_bs(td) == td_max_bs(td)) &&
386 if (!td->o.verify_only || td->o.loops == 0)
387 if (vh->numberio != io_u->numberio)
394 log_verify_failure(hdr, vc);
398 static int verify_io_u_xxhash(struct verify_header *hdr, struct vcont *vc)
400 void *p = io_u_verify_off(hdr, vc);
401 struct vhdr_xxhash *vh = hdr_priv(hdr);
405 dprint(FD_VERIFY, "xxhash verify io_u %p, len %u\n", vc->io_u, hdr->len);
407 state = XXH32_init(1);
408 XXH32_update(state, p, hdr->len - hdr_size(hdr));
409 hash = XXH32_digest(state);
411 if (vh->hash == hash)
415 vc->good_crc = &vh->hash;
417 vc->crc_len = sizeof(hash);
418 log_verify_failure(hdr, vc);
422 static int verify_io_u_sha512(struct verify_header *hdr, struct vcont *vc)
424 void *p = io_u_verify_off(hdr, vc);
425 struct vhdr_sha512 *vh = hdr_priv(hdr);
427 struct fio_sha512_ctx sha512_ctx = {
431 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", vc->io_u, hdr->len);
433 fio_sha512_init(&sha512_ctx);
434 fio_sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
436 if (!memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512)))
440 vc->good_crc = vh->sha512;
441 vc->bad_crc = sha512_ctx.buf;
442 vc->crc_len = sizeof(vh->sha512);
443 log_verify_failure(hdr, vc);
447 static int verify_io_u_sha256(struct verify_header *hdr, struct vcont *vc)
449 void *p = io_u_verify_off(hdr, vc);
450 struct vhdr_sha256 *vh = hdr_priv(hdr);
452 struct fio_sha256_ctx sha256_ctx = {
456 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", vc->io_u, hdr->len);
458 fio_sha256_init(&sha256_ctx);
459 fio_sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
461 if (!memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256)))
465 vc->good_crc = vh->sha256;
466 vc->bad_crc = sha256_ctx.buf;
467 vc->crc_len = sizeof(vh->sha256);
468 log_verify_failure(hdr, vc);
472 static int verify_io_u_sha1(struct verify_header *hdr, struct vcont *vc)
474 void *p = io_u_verify_off(hdr, vc);
475 struct vhdr_sha1 *vh = hdr_priv(hdr);
477 struct fio_sha1_ctx sha1_ctx = {
481 dprint(FD_VERIFY, "sha1 verify io_u %p, len %u\n", vc->io_u, hdr->len);
483 fio_sha1_init(&sha1_ctx);
484 fio_sha1_update(&sha1_ctx, p, hdr->len - hdr_size(hdr));
486 if (!memcmp(vh->sha1, sha1_ctx.H, sizeof(sha1)))
490 vc->good_crc = vh->sha1;
491 vc->bad_crc = sha1_ctx.H;
492 vc->crc_len = sizeof(vh->sha1);
493 log_verify_failure(hdr, vc);
497 static int verify_io_u_crc7(struct verify_header *hdr, struct vcont *vc)
499 void *p = io_u_verify_off(hdr, vc);
500 struct vhdr_crc7 *vh = hdr_priv(hdr);
503 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", vc->io_u, hdr->len);
505 c = fio_crc7(p, hdr->len - hdr_size(hdr));
511 vc->good_crc = &vh->crc7;
514 log_verify_failure(hdr, vc);
518 static int verify_io_u_crc16(struct verify_header *hdr, struct vcont *vc)
520 void *p = io_u_verify_off(hdr, vc);
521 struct vhdr_crc16 *vh = hdr_priv(hdr);
524 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", vc->io_u, hdr->len);
526 c = fio_crc16(p, hdr->len - hdr_size(hdr));
532 vc->good_crc = &vh->crc16;
535 log_verify_failure(hdr, vc);
539 static int verify_io_u_crc64(struct verify_header *hdr, struct vcont *vc)
541 void *p = io_u_verify_off(hdr, vc);
542 struct vhdr_crc64 *vh = hdr_priv(hdr);
543 unsigned long long c;
545 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", vc->io_u, hdr->len);
547 c = fio_crc64(p, hdr->len - hdr_size(hdr));
553 vc->good_crc = &vh->crc64;
556 log_verify_failure(hdr, vc);
560 static int verify_io_u_crc32(struct verify_header *hdr, struct vcont *vc)
562 void *p = io_u_verify_off(hdr, vc);
563 struct vhdr_crc32 *vh = hdr_priv(hdr);
566 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", vc->io_u, hdr->len);
568 c = fio_crc32(p, hdr->len - hdr_size(hdr));
574 vc->good_crc = &vh->crc32;
577 log_verify_failure(hdr, vc);
581 static int verify_io_u_crc32c(struct verify_header *hdr, struct vcont *vc)
583 void *p = io_u_verify_off(hdr, vc);
584 struct vhdr_crc32 *vh = hdr_priv(hdr);
587 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", vc->io_u, hdr->len);
589 c = fio_crc32c(p, hdr->len - hdr_size(hdr));
595 vc->good_crc = &vh->crc32;
598 log_verify_failure(hdr, vc);
602 static int verify_io_u_md5(struct verify_header *hdr, struct vcont *vc)
604 void *p = io_u_verify_off(hdr, vc);
605 struct vhdr_md5 *vh = hdr_priv(hdr);
606 uint32_t hash[MD5_HASH_WORDS];
607 struct fio_md5_ctx md5_ctx = {
611 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", vc->io_u, hdr->len);
613 fio_md5_init(&md5_ctx);
614 fio_md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
616 if (!memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash)))
620 vc->good_crc = vh->md5_digest;
621 vc->bad_crc = md5_ctx.hash;
622 vc->crc_len = sizeof(hash);
623 log_verify_failure(hdr, vc);
628 * Push IO verification to a separate thread
630 int verify_io_u_async(struct thread_data *td, struct io_u **io_u_ptr)
632 struct io_u *io_u = *io_u_ptr;
634 pthread_mutex_lock(&td->io_u_lock);
637 put_file_log(td, io_u->file);
639 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
641 io_u->flags &= ~IO_U_F_IN_CUR_DEPTH;
643 flist_add_tail(&io_u->verify_list, &td->verify_list);
645 pthread_mutex_unlock(&td->io_u_lock);
647 pthread_cond_signal(&td->verify_cond);
651 static int verify_trimmed_io_u(struct thread_data *td, struct io_u *io_u)
653 static char zero_buf[1024];
654 unsigned int this_len, len;
658 if (!td->o.trim_zero)
664 this_len = sizeof(zero_buf);
667 if (memcmp(p, zero_buf, this_len)) {
678 log_err("trim: verify failed at file %s offset %llu, length %lu"
679 ", block offset %lu\n",
680 io_u->file->file_name, io_u->offset, io_u->buflen,
681 (unsigned long) (p - io_u->buf));
685 static int verify_header(struct io_u *io_u, struct verify_header *hdr,
686 unsigned int hdr_num, unsigned int hdr_len)
691 if (hdr->magic != FIO_HDR_MAGIC) {
692 log_err("verify: bad magic header %x, wanted %x",
693 hdr->magic, FIO_HDR_MAGIC);
696 if (hdr->len != hdr_len) {
697 log_err("verify: bad header length %u, wanted %u",
701 if (hdr->rand_seed != io_u->rand_seed) {
702 log_err("verify: bad header rand_seed %"PRIu64
704 hdr->rand_seed, io_u->rand_seed);
708 crc = fio_crc32c(p, offsetof(struct verify_header, crc32));
709 if (crc != hdr->crc32) {
710 log_err("verify: bad header crc %x, calculated %x",
717 log_err(" at file %s offset %llu, length %u\n",
718 io_u->file->file_name,
719 io_u->offset + hdr_num * hdr_len, hdr_len);
723 int verify_io_u(struct thread_data *td, struct io_u **io_u_ptr)
725 struct verify_header *hdr;
726 struct io_u *io_u = *io_u_ptr;
727 unsigned int header_size, hdr_inc, hdr_num = 0;
731 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
734 * If the IO engine is faking IO (like null), then just pretend
735 * we verified everything.
737 if (td->io_ops->flags & FIO_FAKEIO)
740 if (io_u->flags & IO_U_F_TRIMMED) {
741 ret = verify_trimmed_io_u(td, io_u);
745 hdr_inc = get_hdr_inc(td, io_u);
748 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
749 p += hdr_inc, hdr_num++) {
755 unsigned int verify_type;
757 if (ret && td->o.verify_fatal)
760 header_size = __hdr_size(td->o.verify);
761 if (td->o.verify_offset)
762 memswp(p, p + td->o.verify_offset, header_size);
766 * Make rand_seed check pass when have verifysort or
769 if (td->o.verifysort || (td->flags & TD_F_VER_BACKLOG))
770 io_u->rand_seed = hdr->rand_seed;
772 ret = verify_header(io_u, hdr, hdr_num, hdr_inc);
776 if (td->o.verify != VERIFY_NONE)
777 verify_type = td->o.verify;
779 verify_type = hdr->verify_type;
781 switch (verify_type) {
783 ret = verify_io_u_md5(hdr, &vc);
786 ret = verify_io_u_crc64(hdr, &vc);
789 case VERIFY_CRC32C_INTEL:
790 ret = verify_io_u_crc32c(hdr, &vc);
793 ret = verify_io_u_crc32(hdr, &vc);
796 ret = verify_io_u_crc16(hdr, &vc);
799 ret = verify_io_u_crc7(hdr, &vc);
802 ret = verify_io_u_sha256(hdr, &vc);
805 ret = verify_io_u_sha512(hdr, &vc);
808 ret = verify_io_u_xxhash(hdr, &vc);
811 ret = verify_io_u_meta(hdr, &vc);
814 ret = verify_io_u_sha1(hdr, &vc);
817 ret = verify_io_u_pattern(hdr, &vc);
820 log_err("Bad verify type %u\n", hdr->verify_type);
824 if (ret && verify_type != hdr->verify_type)
825 log_err("fio: verify type mismatch (%u media, %u given)\n",
826 hdr->verify_type, verify_type);
830 if (ret && td->o.verify_fatal)
831 fio_mark_td_terminate(td);
836 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
837 struct io_u *io_u, unsigned int header_num)
839 struct vhdr_meta *vh = hdr_priv(hdr);
841 vh->thread = td->thread_number;
843 vh->time_sec = io_u->start_time.tv_sec;
844 vh->time_usec = io_u->start_time.tv_usec;
846 vh->numberio = io_u->numberio;
848 vh->offset = io_u->offset + header_num * td->o.verify_interval;
851 static void fill_xxhash(struct verify_header *hdr, void *p, unsigned int len)
853 struct vhdr_xxhash *vh = hdr_priv(hdr);
856 state = XXH32_init(1);
857 XXH32_update(state, p, len);
858 vh->hash = XXH32_digest(state);
861 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
863 struct vhdr_sha512 *vh = hdr_priv(hdr);
864 struct fio_sha512_ctx sha512_ctx = {
868 fio_sha512_init(&sha512_ctx);
869 fio_sha512_update(&sha512_ctx, p, len);
872 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
874 struct vhdr_sha256 *vh = hdr_priv(hdr);
875 struct fio_sha256_ctx sha256_ctx = {
879 fio_sha256_init(&sha256_ctx);
880 fio_sha256_update(&sha256_ctx, p, len);
883 static void fill_sha1(struct verify_header *hdr, void *p, unsigned int len)
885 struct vhdr_sha1 *vh = hdr_priv(hdr);
886 struct fio_sha1_ctx sha1_ctx = {
890 fio_sha1_init(&sha1_ctx);
891 fio_sha1_update(&sha1_ctx, p, len);
894 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
896 struct vhdr_crc7 *vh = hdr_priv(hdr);
898 vh->crc7 = fio_crc7(p, len);
901 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
903 struct vhdr_crc16 *vh = hdr_priv(hdr);
905 vh->crc16 = fio_crc16(p, len);
908 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
910 struct vhdr_crc32 *vh = hdr_priv(hdr);
912 vh->crc32 = fio_crc32(p, len);
915 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
917 struct vhdr_crc32 *vh = hdr_priv(hdr);
919 vh->crc32 = fio_crc32c(p, len);
922 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
924 struct vhdr_crc64 *vh = hdr_priv(hdr);
926 vh->crc64 = fio_crc64(p, len);
929 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
931 struct vhdr_md5 *vh = hdr_priv(hdr);
932 struct fio_md5_ctx md5_ctx = {
933 .hash = (uint32_t *) vh->md5_digest,
936 fio_md5_init(&md5_ctx);
937 fio_md5_update(&md5_ctx, p, len);
940 static void populate_hdr(struct thread_data *td, struct io_u *io_u,
941 struct verify_header *hdr, unsigned int header_num,
942 unsigned int header_len)
944 unsigned int data_len;
949 hdr->magic = FIO_HDR_MAGIC;
950 hdr->verify_type = td->o.verify;
951 hdr->len = header_len;
952 hdr->rand_seed = io_u->rand_seed;
953 hdr->crc32 = fio_crc32c(p, offsetof(struct verify_header, crc32));
955 data_len = header_len - hdr_size(hdr);
957 data = p + hdr_size(hdr);
958 switch (td->o.verify) {
960 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
962 fill_md5(hdr, data, data_len);
965 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
967 fill_crc64(hdr, data, data_len);
970 case VERIFY_CRC32C_INTEL:
971 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
973 fill_crc32c(hdr, data, data_len);
976 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
978 fill_crc32(hdr, data, data_len);
981 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
983 fill_crc16(hdr, data, data_len);
986 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
988 fill_crc7(hdr, data, data_len);
991 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
993 fill_sha256(hdr, data, data_len);
996 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
998 fill_sha512(hdr, data, data_len);
1001 dprint(FD_VERIFY, "fill xxhash io_u %p, len %u\n",
1003 fill_xxhash(hdr, data, data_len);
1006 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
1008 fill_meta(hdr, td, io_u, header_num);
1011 dprint(FD_VERIFY, "fill sha1 io_u %p, len %u\n",
1013 fill_sha1(hdr, data, data_len);
1015 case VERIFY_PATTERN:
1016 /* nothing to do here */
1019 log_err("fio: bad verify type: %d\n", td->o.verify);
1022 if (td->o.verify_offset)
1023 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
1027 * fill body of io_u->buf with random data and add a header with the
1028 * checksum of choice
1030 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
1032 if (td->o.verify == VERIFY_NULL)
1035 io_u->numberio = td->io_issues[io_u->ddir];
1037 fill_pattern_headers(td, io_u, 0, 0);
1040 int get_next_verify(struct thread_data *td, struct io_u *io_u)
1042 struct io_piece *ipo = NULL;
1045 * this io_u is from a requeue, we already filled the offsets
1050 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
1051 struct rb_node *n = rb_first(&td->io_hist_tree);
1053 ipo = rb_entry(n, struct io_piece, rb_node);
1056 * Ensure that the associated IO has completed
1059 if (ipo->flags & IP_F_IN_FLIGHT)
1062 rb_erase(n, &td->io_hist_tree);
1063 assert(ipo->flags & IP_F_ONRB);
1064 ipo->flags &= ~IP_F_ONRB;
1065 } else if (!flist_empty(&td->io_hist_list)) {
1066 ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list);
1069 * Ensure that the associated IO has completed
1072 if (ipo->flags & IP_F_IN_FLIGHT)
1075 flist_del(&ipo->list);
1076 assert(ipo->flags & IP_F_ONLIST);
1077 ipo->flags &= ~IP_F_ONLIST;
1083 io_u->offset = ipo->offset;
1084 io_u->buflen = ipo->len;
1085 io_u->numberio = ipo->numberio;
1086 io_u->file = ipo->file;
1087 io_u->flags |= IO_U_F_VER_LIST;
1089 if (ipo->flags & IP_F_TRIMMED)
1090 io_u->flags |= IO_U_F_TRIMMED;
1092 if (!fio_file_open(io_u->file)) {
1093 int r = td_io_open_file(td, io_u->file);
1096 dprint(FD_VERIFY, "failed file %s open\n",
1097 io_u->file->file_name);
1102 get_file(ipo->file);
1103 assert(fio_file_open(io_u->file));
1104 io_u->ddir = DDIR_READ;
1105 io_u->xfer_buf = io_u->buf;
1106 io_u->xfer_buflen = io_u->buflen;
1108 remove_trim_entry(td, ipo);
1110 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
1112 if (!td->o.verify_pattern_bytes) {
1113 io_u->rand_seed = __rand(&td->verify_state);
1114 if (sizeof(int) != sizeof(long *))
1115 io_u->rand_seed *= __rand(&td->verify_state);
1121 dprint(FD_VERIFY, "get_next_verify: empty\n");
1125 void fio_verify_init(struct thread_data *td)
1127 if (td->o.verify == VERIFY_CRC32C_INTEL ||
1128 td->o.verify == VERIFY_CRC32C) {
1129 crc32c_intel_probe();
1133 static void *verify_async_thread(void *data)
1135 struct thread_data *td = data;
1139 if (td->o.verify_cpumask_set &&
1140 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
1141 log_err("fio: failed setting verify thread affinity\n");
1149 if (td->verify_thread_exit)
1152 pthread_mutex_lock(&td->io_u_lock);
1154 while (flist_empty(&td->verify_list) &&
1155 !td->verify_thread_exit) {
1156 ret = pthread_cond_wait(&td->verify_cond,
1159 pthread_mutex_unlock(&td->io_u_lock);
1164 flist_splice_init(&td->verify_list, &list);
1165 pthread_mutex_unlock(&td->io_u_lock);
1167 if (flist_empty(&list))
1170 while (!flist_empty(&list)) {
1171 io_u = flist_first_entry(&list, struct io_u, verify_list);
1172 flist_del_init(&io_u->verify_list);
1174 io_u->flags |= IO_U_F_NO_FILE_PUT;
1175 ret = verify_io_u(td, &io_u);
1180 if (td_non_fatal_error(td, ERROR_TYPE_VERIFY_BIT, ret)) {
1181 update_error_count(td, ret);
1189 td_verror(td, ret, "async_verify");
1190 if (td->o.verify_fatal)
1191 fio_mark_td_terminate(td);
1195 pthread_mutex_lock(&td->io_u_lock);
1196 td->nr_verify_threads--;
1197 pthread_mutex_unlock(&td->io_u_lock);
1199 pthread_cond_signal(&td->free_cond);
1203 int verify_async_init(struct thread_data *td)
1206 pthread_attr_t attr;
1208 pthread_attr_init(&attr);
1209 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
1211 td->verify_thread_exit = 0;
1213 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
1214 for (i = 0; i < td->o.verify_async; i++) {
1215 ret = pthread_create(&td->verify_threads[i], &attr,
1216 verify_async_thread, td);
1218 log_err("fio: async verify creation failed: %s\n",
1222 ret = pthread_detach(td->verify_threads[i]);
1224 log_err("fio: async verify thread detach failed: %s\n",
1228 td->nr_verify_threads++;
1231 pthread_attr_destroy(&attr);
1233 if (i != td->o.verify_async) {
1234 log_err("fio: only %d verify threads started, exiting\n", i);
1235 td->verify_thread_exit = 1;
1237 pthread_cond_broadcast(&td->verify_cond);
1244 void verify_async_exit(struct thread_data *td)
1246 td->verify_thread_exit = 1;
1248 pthread_cond_broadcast(&td->verify_cond);
1250 pthread_mutex_lock(&td->io_u_lock);
1252 while (td->nr_verify_threads)
1253 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1255 pthread_mutex_unlock(&td->io_u_lock);
1256 free(td->verify_threads);
1257 td->verify_threads = NULL;
1260 struct all_io_list *get_all_io_list(int save_mask, size_t *sz)
1262 struct all_io_list *rep;
1263 struct thread_data *td;
1268 compiletime_assert(sizeof(struct all_io_list) == 8, "all_io_list");
1271 * Calculate reply space needed. We need one 'io_state' per thread,
1272 * and the size will vary depending on depth.
1276 for_each_td(td, i) {
1277 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1280 td->flags |= TD_F_VSTATE_SAVED;
1281 depth += td->o.iodepth;
1289 *sz += nr * sizeof(struct thread_io_list);
1290 *sz += depth * sizeof(uint64_t);
1293 rep->threads = cpu_to_le64((uint64_t) nr);
1295 next = &rep->state[0];
1296 for_each_td(td, i) {
1297 struct thread_io_list *s = next;
1300 if (save_mask != IO_LIST_ALL && (i + 1) != save_mask)
1303 if (td->last_write_comp) {
1306 if (td->io_blocks[DDIR_WRITE] < td->o.iodepth)
1307 comps = td->io_blocks[DDIR_WRITE];
1309 comps = td->o.iodepth;
1311 k = td->last_write_idx - 1;
1312 for (j = 0; j < comps; j++) {
1314 k = td->o.iodepth - 1;
1315 s->offsets[j] = cpu_to_le64(td->last_write_comp[k]);
1321 s->no_comps = cpu_to_le64((uint64_t) comps);
1322 s->depth = cpu_to_le64((uint64_t) td->o.iodepth);
1323 s->numberio = cpu_to_le64((uint64_t) td->io_issues[DDIR_WRITE]);
1324 s->index = cpu_to_le64((uint64_t) i);
1325 s->rand.s[0] = cpu_to_le32(td->random_state.s1);
1326 s->rand.s[1] = cpu_to_le32(td->random_state.s2);
1327 s->rand.s[2] = cpu_to_le32(td->random_state.s3);
1329 strncpy((char *) s->name, td->o.name, sizeof(s->name));
1330 next = io_list_next(s);
1336 static int open_state_file(const char *name, const char *prefix, int num,
1344 flags = O_CREAT | O_TRUNC | O_WRONLY | O_SYNC;
1348 verify_state_gen_name(out, sizeof(out), name, prefix, num);
1350 fd = open(out, flags, 0644);
1352 perror("fio: open state file");
1359 static int write_thread_list_state(struct thread_io_list *s,
1362 struct verify_state_hdr hdr;
1367 fd = open_state_file((const char *) s->name, prefix, s->index, 1);
1371 crc = fio_crc32c((void *)s, thread_io_list_sz(s));
1373 hdr.version = cpu_to_le64((uint64_t) VSTATE_HDR_VERSION);
1374 hdr.size = cpu_to_le64((uint64_t) thread_io_list_sz(s));
1375 hdr.crc = cpu_to_le64(crc);
1376 ret = write(fd, &hdr, sizeof(hdr));
1377 if (ret != sizeof(hdr))
1380 ret = write(fd, s, thread_io_list_sz(s));
1381 if (ret != thread_io_list_sz(s)) {
1384 perror("fio: write state file");
1385 log_err("fio: failed to write state file\n");
1394 void __verify_save_state(struct all_io_list *state, const char *prefix)
1396 struct thread_io_list *s = &state->state[0];
1399 for (i = 0; i < le64_to_cpu(state->threads); i++) {
1400 write_thread_list_state(s, prefix);
1401 s = io_list_next(s);
1405 void verify_save_state(void)
1407 struct all_io_list *state;
1410 state = get_all_io_list(IO_LIST_ALL, &sz);
1412 __verify_save_state(state, "local");
1417 void verify_free_state(struct thread_data *td)
1423 void verify_convert_assign_state(struct thread_data *td,
1424 struct thread_io_list *s)
1428 s->no_comps = le64_to_cpu(s->no_comps);
1429 s->depth = le64_to_cpu(s->depth);
1430 s->numberio = le64_to_cpu(s->numberio);
1431 for (i = 0; i < 4; i++)
1432 s->rand.s[i] = le32_to_cpu(s->rand.s[i]);
1433 for (i = 0; i < s->no_comps; i++)
1434 s->offsets[i] = le64_to_cpu(s->offsets[i]);
1439 int verify_state_hdr(struct verify_state_hdr *hdr, struct thread_io_list *s)
1443 hdr->version = le64_to_cpu(hdr->version);
1444 hdr->size = le64_to_cpu(hdr->size);
1445 hdr->crc = le64_to_cpu(hdr->crc);
1447 if (hdr->version != VSTATE_HDR_VERSION)
1450 crc = fio_crc32c((void *)s, hdr->size);
1451 if (crc != hdr->crc)
1457 int verify_load_state(struct thread_data *td, const char *prefix)
1459 struct thread_io_list *s = NULL;
1460 struct verify_state_hdr hdr;
1465 if (!td->o.verify_state)
1468 fd = open_state_file(td->o.name, prefix, td->thread_number - 1, 0);
1472 ret = read(fd, &hdr, sizeof(hdr));
1473 if (ret != sizeof(hdr)) {
1475 td_verror(td, errno, "read verify state hdr");
1476 log_err("fio: failed reading verify state header\n");
1480 hdr.version = le64_to_cpu(hdr.version);
1481 hdr.size = le64_to_cpu(hdr.size);
1482 hdr.crc = le64_to_cpu(hdr.crc);
1484 if (hdr.version != VSTATE_HDR_VERSION) {
1485 log_err("fio: bad version in verify state header\n");
1489 s = malloc(hdr.size);
1490 ret = read(fd, s, hdr.size);
1491 if (ret != hdr.size) {
1493 td_verror(td, errno, "read verify state");
1494 log_err("fio: failed reading verity state\n");
1498 crc = fio_crc32c((void *)s, hdr.size);
1499 if (crc != hdr.crc) {
1500 log_err("fio: verify state is corrupt\n");
1506 verify_convert_assign_state(td, s);
1516 * Use the loaded verify state to know when to stop doing verification
1518 int verify_state_should_stop(struct thread_data *td, struct io_u *io_u)
1520 struct thread_io_list *s = td->vstate;
1527 * If we're not into the window of issues - depth yet, continue
1529 if (td->io_blocks[DDIR_READ] < s->depth ||
1530 s->numberio - td->io_blocks[DDIR_READ] > s->depth)
1534 * We're in the window of having to check if this io was
1535 * completed or not. If the IO was seen as completed, then
1538 for (i = 0; i < s->no_comps; i++)
1539 if (io_u->offset == s->offsets[i])
1543 * Not found, we have to stop