2 * IO verification helpers
15 #include "crc/crc64.h"
16 #include "crc/crc32.h"
17 #include "crc/crc32c.h"
18 #include "crc/crc16.h"
20 #include "crc/sha256.h"
21 #include "crc/sha512.h"
23 static void fill_random_bytes(struct thread_data *td, void *p, unsigned int len)
29 r = os_random_long(&td->verify_state);
32 * lrand48_r seems to be broken and only fill the bottom
33 * 32-bits, even on 64-bit archs with 64-bit longs
46 static void fill_pattern(struct thread_data *td, void *p, unsigned int len)
48 switch (td->o.verify_pattern_bytes) {
50 dprint(FD_VERIFY, "fill random bytes len=%u\n", len);
51 fill_random_bytes(td, p, len);
54 dprint(FD_VERIFY, "fill verify pattern b=0 len=%u\n", len);
55 memset(p, td->o.verify_pattern, len);
60 unsigned int pattern = td->o.verify_pattern;
62 unsigned char c1, c2, c3, c4;
65 dprint(FD_VERIFY, "fill verify pattern b=%d len=%u\n",
66 td->o.verify_pattern_bytes, len);
81 if (td->o.verify_pattern_bytes == 2 || i == len)
84 if (td->o.verify_pattern_bytes == 3 || i == len)
93 static void memswp(void *buf1, void *buf2, unsigned int len)
97 assert(len <= sizeof(swap));
99 memcpy(&swap, buf1, len);
100 memcpy(buf1, buf2, len);
101 memcpy(buf2, &swap, len);
104 static void hexdump(void *buffer, int len)
106 unsigned char *p = buffer;
109 for (i = 0; i < len; i++)
110 log_info("%02x", p[i]);
115 * Prepare for seperation of verify_header and checksum header
117 static inline unsigned int __hdr_size(int verify_type)
119 unsigned int len = len;
121 switch (verify_type) {
127 len = sizeof(struct vhdr_md5);
130 len = sizeof(struct vhdr_crc64);
134 case VERIFY_CRC32C_INTEL:
135 len = sizeof(struct vhdr_crc32);
138 len = sizeof(struct vhdr_crc16);
141 len = sizeof(struct vhdr_crc7);
144 len = sizeof(struct vhdr_sha256);
147 len = sizeof(struct vhdr_sha512);
150 len = sizeof(struct vhdr_meta);
153 log_err("fio: unknown verify header!\n");
157 return len + sizeof(struct verify_header);
160 static inline unsigned int hdr_size(struct verify_header *hdr)
162 return __hdr_size(hdr->verify_type);
165 static void *hdr_priv(struct verify_header *hdr)
169 return priv + sizeof(struct verify_header);
173 * Return data area 'header_num'
175 static inline void *io_u_verify_off(struct verify_header *hdr,
176 struct io_u *io_u, unsigned char header_num)
178 return io_u->buf + header_num * hdr->len + hdr_size(hdr);
181 static int verify_io_u_meta(struct verify_header *hdr, struct thread_data *td,
182 struct io_u *io_u, unsigned int header_num)
184 struct vhdr_meta *vh = hdr_priv(hdr);
186 dprint(FD_VERIFY, "meta verify io_u %p, len %u\n", io_u, hdr->len);
188 if (vh->offset != io_u->offset + header_num * td->o.verify_interval) {
189 log_err("meta: verify failed at %llu/%u\n",
190 io_u->offset + header_num * hdr->len, hdr->len);
197 static int verify_io_u_sha512(struct verify_header *hdr, struct io_u *io_u,
198 unsigned int header_num)
200 void *p = io_u_verify_off(hdr, io_u, header_num);
201 struct vhdr_sha512 *vh = hdr_priv(hdr);
203 struct sha512_ctx sha512_ctx = {
207 dprint(FD_VERIFY, "sha512 verify io_u %p, len %u\n", io_u, hdr->len);
209 sha512_init(&sha512_ctx);
210 sha512_update(&sha512_ctx, p, hdr->len - hdr_size(hdr));
212 if (memcmp(vh->sha512, sha512_ctx.buf, sizeof(sha512))) {
213 log_err("sha512: verify failed at %llu/%u\n",
214 io_u->offset + header_num * hdr->len, hdr->len);
215 hexdump(vh->sha512, sizeof(vh->sha512));
216 hexdump(sha512_ctx.buf, sizeof(sha512));
223 static int verify_io_u_sha256(struct verify_header *hdr, struct io_u *io_u,
224 unsigned int header_num)
226 void *p = io_u_verify_off(hdr, io_u, header_num);
227 struct vhdr_sha256 *vh = hdr_priv(hdr);
229 struct sha256_ctx sha256_ctx = {
233 dprint(FD_VERIFY, "sha256 verify io_u %p, len %u\n", io_u, hdr->len);
235 sha256_init(&sha256_ctx);
236 sha256_update(&sha256_ctx, p, hdr->len - hdr_size(hdr));
238 if (memcmp(vh->sha256, sha256_ctx.buf, sizeof(sha256))) {
239 log_err("sha256: verify failed at %llu/%u\n",
240 io_u->offset + header_num * hdr->len, hdr->len);
241 hexdump(vh->sha256, sizeof(vh->sha256));
242 hexdump(sha256_ctx.buf, sizeof(sha256));
249 static int verify_io_u_crc7(struct verify_header *hdr, struct io_u *io_u,
250 unsigned char header_num)
252 void *p = io_u_verify_off(hdr, io_u, header_num);
253 struct vhdr_crc7 *vh = hdr_priv(hdr);
256 dprint(FD_VERIFY, "crc7 verify io_u %p, len %u\n", io_u, hdr->len);
258 c = crc7(p, hdr->len - hdr_size(hdr));
261 log_err("crc7: verify failed at %llu/%u\n",
262 io_u->offset + header_num * hdr->len, hdr->len);
263 log_err("crc7: wanted %x, got %x\n", vh->crc7, c);
270 static int verify_io_u_crc16(struct verify_header *hdr, struct io_u *io_u,
271 unsigned int header_num)
273 void *p = io_u_verify_off(hdr, io_u, header_num);
274 struct vhdr_crc16 *vh = hdr_priv(hdr);
277 dprint(FD_VERIFY, "crc16 verify io_u %p, len %u\n", io_u, hdr->len);
279 c = crc16(p, hdr->len - hdr_size(hdr));
281 if (c != vh->crc16) {
282 log_err("crc16: verify failed at %llu/%u\n",
283 io_u->offset + header_num * hdr->len, hdr->len);
284 log_err("crc16: wanted %x, got %x\n", vh->crc16, c);
291 static int verify_io_u_crc64(struct verify_header *hdr, struct io_u *io_u,
292 unsigned int header_num)
294 void *p = io_u_verify_off(hdr, io_u, header_num);
295 struct vhdr_crc64 *vh = hdr_priv(hdr);
296 unsigned long long c;
298 dprint(FD_VERIFY, "crc64 verify io_u %p, len %u\n", io_u, hdr->len);
300 c = crc64(p, hdr->len - hdr_size(hdr));
302 if (c != vh->crc64) {
303 log_err("crc64: verify failed at %llu/%u\n",
304 io_u->offset + header_num * hdr->len,
306 log_err("crc64: wanted %llx, got %llx\n",
307 (unsigned long long) vh->crc64, c);
314 static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u,
315 unsigned int header_num)
317 void *p = io_u_verify_off(hdr, io_u, header_num);
318 struct vhdr_crc32 *vh = hdr_priv(hdr);
321 dprint(FD_VERIFY, "crc32 verify io_u %p, len %u\n", io_u, hdr->len);
323 c = crc32(p, hdr->len - hdr_size(hdr));
325 if (c != vh->crc32) {
326 log_err("crc32: verify failed at %llu/%u\n",
327 io_u->offset + header_num * hdr->len, hdr->len);
328 log_err("crc32: wanted %x, got %x\n", vh->crc32, c);
335 static int verify_io_u_crc32c(struct verify_header *hdr, struct io_u *io_u,
336 unsigned int header_num)
338 void *p = io_u_verify_off(hdr, io_u, header_num);
339 struct vhdr_crc32 *vh = hdr_priv(hdr);
342 dprint(FD_VERIFY, "crc32c verify io_u %p, len %u\n", io_u, hdr->len);
344 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
345 c = crc32c_intel(p, hdr->len - hdr_size(hdr));
347 c = crc32c(p, hdr->len - hdr_size(hdr));
349 if (c != vh->crc32) {
350 log_err("crc32c: verify failed at %llu/%u\n",
351 io_u->offset + header_num * hdr->len, hdr->len);
352 log_err("crc32c: wanted %x, got %x\n", vh->crc32, c);
359 static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u,
360 unsigned int header_num)
362 void *p = io_u_verify_off(hdr, io_u, header_num);
363 struct vhdr_md5 *vh = hdr_priv(hdr);
364 uint32_t hash[MD5_HASH_WORDS];
365 struct md5_ctx md5_ctx = {
369 dprint(FD_VERIFY, "md5 verify io_u %p, len %u\n", io_u, hdr->len);
372 md5_update(&md5_ctx, p, hdr->len - hdr_size(hdr));
374 if (memcmp(vh->md5_digest, md5_ctx.hash, sizeof(hash))) {
375 log_err("md5: verify failed at %llu/%u\n",
376 io_u->offset + header_num * hdr->len, hdr->len);
377 hexdump(vh->md5_digest, sizeof(vh->md5_digest));
378 hexdump(md5_ctx.hash, sizeof(hash));
385 static unsigned int hweight8(unsigned int w)
387 unsigned int res = w - ((w >> 1) & 0x55);
389 res = (res & 0x33) + ((res >> 2) & 0x33);
390 return (res + (res >> 4)) & 0x0F;
393 int verify_io_u_pattern(unsigned long pattern, unsigned long pattern_size,
394 char *buf, unsigned int len, unsigned int mod)
397 char split_pattern[4];
399 for (i = 0; i < 4; i++) {
400 split_pattern[i] = pattern & 0xff;
404 for (i = 0; i < len; i++) {
405 if (buf[i] != split_pattern[mod]) {
408 bits = hweight8(buf[i] ^ split_pattern[mod]);
409 log_err("fio: got pattern %x, wanted %x. Bad bits %d\n",
410 buf[i], split_pattern[mod], bits);
411 log_err("fio: bad pattern block offset %u\n", i);
415 if (mod == pattern_size)
423 * Push IO verification to a separate thread
425 int verify_io_u_async(struct thread_data *td, struct io_u *io_u)
428 put_file_log(td, io_u->file);
432 pthread_mutex_lock(&td->io_u_lock);
433 flist_del(&io_u->list);
434 flist_add_tail(&io_u->list, &td->verify_list);
435 pthread_mutex_unlock(&td->io_u_lock);
437 pthread_cond_signal(&td->verify_cond);
438 io_u->flags |= IO_U_F_FREE_DEF;
442 int verify_io_u(struct thread_data *td, struct io_u *io_u)
444 struct verify_header *hdr;
445 unsigned int hdr_size, hdr_inc, hdr_num = 0;
449 if (td->o.verify == VERIFY_NULL || io_u->ddir != DDIR_READ)
452 hdr_inc = io_u->buflen;
453 if (td->o.verify_interval)
454 hdr_inc = td->o.verify_interval;
457 for (p = io_u->buf; p < io_u->buf + io_u->buflen;
458 p += hdr_inc, hdr_num++) {
459 if (ret && td->o.verify_fatal) {
463 hdr_size = __hdr_size(td->o.verify);
464 if (td->o.verify_offset)
465 memswp(p, p + td->o.verify_offset, hdr_size);
468 if (hdr->fio_magic != FIO_HDR_MAGIC) {
469 log_err("Bad verify header %x\n", hdr->fio_magic);
473 if (td->o.verify_pattern_bytes) {
474 dprint(FD_VERIFY, "pattern verify io_u %p, len %u\n",
476 ret = verify_io_u_pattern(td->o.verify_pattern,
477 td->o.verify_pattern_bytes,
482 * Also verify the meta data, if applicable
484 if (hdr->verify_type == VERIFY_META)
485 ret |= verify_io_u_meta(hdr, td, io_u, hdr_num);
488 log_err("fio: verify failed at %llu/%u\n",
489 io_u->offset + hdr_num * hdr->len,
494 switch (hdr->verify_type) {
496 ret = verify_io_u_md5(hdr, io_u, hdr_num);
499 ret = verify_io_u_crc64(hdr, io_u, hdr_num);
502 case VERIFY_CRC32C_INTEL:
503 ret = verify_io_u_crc32c(hdr, io_u, hdr_num);
506 ret = verify_io_u_crc32(hdr, io_u, hdr_num);
509 ret = verify_io_u_crc16(hdr, io_u, hdr_num);
512 ret = verify_io_u_crc7(hdr, io_u, hdr_num);
515 ret = verify_io_u_sha256(hdr, io_u, hdr_num);
518 ret = verify_io_u_sha512(hdr, io_u, hdr_num);
521 ret = verify_io_u_meta(hdr, td, io_u, hdr_num);
524 log_err("Bad verify type %u\n", hdr->verify_type);
532 static void fill_meta(struct verify_header *hdr, struct thread_data *td,
533 struct io_u *io_u, unsigned int header_num)
535 struct vhdr_meta *vh = hdr_priv(hdr);
537 vh->thread = td->thread_number;
539 vh->time_sec = io_u->start_time.tv_sec;
540 vh->time_usec = io_u->start_time.tv_usec;
542 vh->numberio = td->io_issues[DDIR_WRITE];
544 vh->offset = io_u->offset + header_num * td->o.verify_interval;
547 static void fill_sha512(struct verify_header *hdr, void *p, unsigned int len)
549 struct vhdr_sha512 *vh = hdr_priv(hdr);
550 struct sha512_ctx sha512_ctx = {
554 sha512_init(&sha512_ctx);
555 sha512_update(&sha512_ctx, p, len);
558 static void fill_sha256(struct verify_header *hdr, void *p, unsigned int len)
560 struct vhdr_sha256 *vh = hdr_priv(hdr);
561 struct sha256_ctx sha256_ctx = {
565 sha256_init(&sha256_ctx);
566 sha256_update(&sha256_ctx, p, len);
569 static void fill_crc7(struct verify_header *hdr, void *p, unsigned int len)
571 struct vhdr_crc7 *vh = hdr_priv(hdr);
573 vh->crc7 = crc7(p, len);
576 static void fill_crc16(struct verify_header *hdr, void *p, unsigned int len)
578 struct vhdr_crc16 *vh = hdr_priv(hdr);
580 vh->crc16 = crc16(p, len);
583 static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
585 struct vhdr_crc32 *vh = hdr_priv(hdr);
587 vh->crc32 = crc32(p, len);
590 static void fill_crc32c(struct verify_header *hdr, void *p, unsigned int len)
592 struct vhdr_crc32 *vh = hdr_priv(hdr);
594 if (hdr->verify_type == VERIFY_CRC32C_INTEL)
595 vh->crc32 = crc32c_intel(p, len);
597 vh->crc32 = crc32c(p, len);
600 static void fill_crc64(struct verify_header *hdr, void *p, unsigned int len)
602 struct vhdr_crc64 *vh = hdr_priv(hdr);
604 vh->crc64 = crc64(p, len);
607 static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
609 struct vhdr_md5 *vh = hdr_priv(hdr);
610 struct md5_ctx md5_ctx = {
611 .hash = (uint32_t *) vh->md5_digest,
615 md5_update(&md5_ctx, p, len);
619 * fill body of io_u->buf with random data and add a header with the
620 * crc32 or md5 sum of that data.
622 void populate_verify_io_u(struct thread_data *td, struct io_u *io_u)
624 struct verify_header *hdr;
625 void *p = io_u->buf, *data;
626 unsigned int hdr_inc, data_len, header_num = 0;
628 if (td->o.verify == VERIFY_NULL)
631 fill_pattern(td, p, io_u->buflen);
633 hdr_inc = io_u->buflen;
634 if (td->o.verify_interval)
635 hdr_inc = td->o.verify_interval;
637 for (; p < io_u->buf + io_u->buflen; p += hdr_inc) {
640 hdr->fio_magic = FIO_HDR_MAGIC;
641 hdr->verify_type = td->o.verify;
643 data_len = hdr_inc - hdr_size(hdr);
645 data = p + hdr_size(hdr);
646 switch (td->o.verify) {
648 dprint(FD_VERIFY, "fill md5 io_u %p, len %u\n",
650 fill_md5(hdr, data, data_len);
653 dprint(FD_VERIFY, "fill crc64 io_u %p, len %u\n",
655 fill_crc64(hdr, data, data_len);
658 case VERIFY_CRC32C_INTEL:
659 dprint(FD_VERIFY, "fill crc32c io_u %p, len %u\n",
661 fill_crc32c(hdr, data, data_len);
664 dprint(FD_VERIFY, "fill crc32 io_u %p, len %u\n",
666 fill_crc32(hdr, data, data_len);
669 dprint(FD_VERIFY, "fill crc16 io_u %p, len %u\n",
671 fill_crc16(hdr, data, data_len);
674 dprint(FD_VERIFY, "fill crc7 io_u %p, len %u\n",
676 fill_crc7(hdr, data, data_len);
679 dprint(FD_VERIFY, "fill sha256 io_u %p, len %u\n",
681 fill_sha256(hdr, data, data_len);
684 dprint(FD_VERIFY, "fill sha512 io_u %p, len %u\n",
686 fill_sha512(hdr, data, data_len);
689 dprint(FD_VERIFY, "fill meta io_u %p, len %u\n",
691 fill_meta(hdr, td, io_u, header_num);
694 log_err("fio: bad verify type: %d\n", td->o.verify);
697 if (td->o.verify_offset)
698 memswp(p, p + td->o.verify_offset, hdr_size(hdr));
703 int get_next_verify(struct thread_data *td, struct io_u *io_u)
705 struct io_piece *ipo = NULL;
708 * this io_u is from a requeue, we already filled the offsets
713 if (!RB_EMPTY_ROOT(&td->io_hist_tree)) {
714 struct rb_node *n = rb_first(&td->io_hist_tree);
716 ipo = rb_entry(n, struct io_piece, rb_node);
717 rb_erase(n, &td->io_hist_tree);
718 } else if (!flist_empty(&td->io_hist_list)) {
719 ipo = flist_entry(td->io_hist_list.next, struct io_piece, list);
720 flist_del(&ipo->list);
724 io_u->offset = ipo->offset;
725 io_u->buflen = ipo->len;
726 io_u->file = ipo->file;
728 if (!fio_file_open(io_u->file)) {
729 int r = td_io_open_file(td, io_u->file);
732 dprint(FD_VERIFY, "failed file %s open\n",
733 io_u->file->file_name);
739 assert(fio_file_open(io_u->file));
740 io_u->ddir = DDIR_READ;
741 io_u->xfer_buf = io_u->buf;
742 io_u->xfer_buflen = io_u->buflen;
744 dprint(FD_VERIFY, "get_next_verify: ret io_u %p\n", io_u);
748 dprint(FD_VERIFY, "get_next_verify: empty\n");
752 static void *verify_async_thread(void *data)
754 struct thread_data *td = data;
758 if (td->o.verify_cpumask_set &&
759 fio_setaffinity(td->pid, td->o.verify_cpumask)) {
760 log_err("fio: failed setting verify thread affinity\n");
768 if (td->verify_thread_exit)
771 pthread_mutex_lock(&td->io_u_lock);
773 while (flist_empty(&td->verify_list) &&
774 !td->verify_thread_exit) {
775 ret = pthread_cond_wait(&td->verify_cond,
778 pthread_mutex_unlock(&td->io_u_lock);
783 flist_splice_init(&td->verify_list, &list);
784 pthread_mutex_unlock(&td->io_u_lock);
786 if (flist_empty(&list))
789 while (!flist_empty(&list)) {
790 io_u = flist_entry(list.next, struct io_u, list);
791 flist_del_init(&io_u->list);
793 ret = verify_io_u(td, io_u);
797 if (td->o.continue_on_error &&
798 td_non_fatal_error(ret)) {
799 update_error_count(td, ret);
807 td_verror(td, ret, "async_verify");
812 pthread_mutex_lock(&td->io_u_lock);
813 td->nr_verify_threads--;
814 pthread_mutex_unlock(&td->io_u_lock);
816 pthread_cond_signal(&td->free_cond);
820 int verify_async_init(struct thread_data *td)
824 td->verify_thread_exit = 0;
826 td->verify_threads = malloc(sizeof(pthread_t) * td->o.verify_async);
827 for (i = 0; i < td->o.verify_async; i++) {
828 ret = pthread_create(&td->verify_threads[i], NULL,
829 verify_async_thread, td);
831 log_err("fio: async verify creation failed: %s\n",
835 ret = pthread_detach(td->verify_threads[i]);
837 log_err("fio: async verify thread detach failed: %s\n",
841 td->nr_verify_threads++;
844 if (i != td->o.verify_async) {
845 log_err("fio: only %d verify threads started, exiting\n", i);
846 td->verify_thread_exit = 1;
848 pthread_cond_broadcast(&td->verify_cond);
855 void verify_async_exit(struct thread_data *td)
857 td->verify_thread_exit = 1;
859 pthread_cond_broadcast(&td->verify_cond);
861 pthread_mutex_lock(&td->io_u_lock);
863 while (td->nr_verify_threads)
864 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
866 pthread_mutex_unlock(&td->io_u_lock);
867 free(td->verify_threads);
868 td->verify_threads = NULL;