2 * Copyright (C) 2018 Western Digital Corporation or its affiliates.
4 * This file is released under the GPL.
19 #include "oslib/asprintf.h"
25 * zbd_get_zoned_model - Get a device zoned model
26 * @td: FIO thread data
27 * @f: FIO file for which to get model information
29 int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
30 enum zbd_zoned_model *model)
34 ret = blkzoned_get_zoned_model(td, f, model);
36 td_verror(td, errno, "get zoned model failed");
37 log_err("%s: get zoned model failed (%d).\n",
45 * zbd_report_zones - Get zone information
46 * @td: FIO thread data.
47 * @f: FIO file for which to get zone information
48 * @offset: offset from which to report zones
49 * @zones: Array of struct zbd_zone
50 * @nr_zones: Size of @zones array
52 * Get zone information into @zones starting from the zone at offset @offset
53 * for the device specified by @f.
55 * Returns the number of zones reported upon success and a negative error code
56 * upon failure. If the zone report is empty, always assume an error (device
57 * problem) and return -EIO.
59 int zbd_report_zones(struct thread_data *td, struct fio_file *f,
60 uint64_t offset, struct zbd_zone *zones,
61 unsigned int nr_zones)
65 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
67 td_verror(td, errno, "report zones failed");
68 log_err("%s: report zones from sector %llu failed (%d).\n",
69 f->file_name, (unsigned long long)offset >> 9, errno);
70 } else if (ret == 0) {
71 td_verror(td, errno, "Empty zone report");
72 log_err("%s: report zones from sector %llu is empty.\n",
73 f->file_name, (unsigned long long)offset >> 9);
81 * zbd_reset_wp - reset the write pointer of a range of zones
82 * @td: FIO thread data.
83 * @f: FIO file for which to reset zones
84 * @offset: Starting offset of the first zone to reset
85 * @length: Length of the range of zones to reset
87 * Reset the write pointer of all zones in the range @offset...@offset+@length.
88 * Returns 0 upon success and a negative error code upon failure.
90 int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
91 uint64_t offset, uint64_t length)
95 ret = blkzoned_reset_wp(td, f, offset, length);
97 td_verror(td, errno, "resetting wp failed");
98 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
99 f->file_name, (unsigned long long)length >> 9,
100 (unsigned long long)offset >> 9, errno);
107 * zbd_zone_idx - convert an offset into a zone number
109 * @offset: offset in bytes. If this offset is in the first zone_size bytes
110 * past the disk size then the index of the sentinel is returned.
112 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
116 if (f->zbd_info->zone_size_log2 > 0)
117 zone_idx = offset >> f->zbd_info->zone_size_log2;
119 zone_idx = offset / f->zbd_info->zone_size;
121 return min(zone_idx, f->zbd_info->nr_zones);
125 * zbd_zone_swr - Test whether a zone requires sequential writes
126 * @z: zone info pointer.
128 static inline bool zbd_zone_swr(struct fio_zone_info *z)
130 return z->type == ZBD_ZONE_TYPE_SWR;
134 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
136 * @z: zone info pointer.
137 * @required: minimum number of bytes that must remain in a zone.
139 * The caller must hold z->mutex.
141 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
144 assert((required & 511) == 0);
146 return zbd_zone_swr(z) &&
147 z->wp + required > z->start + f->zbd_info->zone_size;
150 static void zone_lock(struct thread_data *td, struct fio_zone_info *z)
153 * Lock the io_u target zone. The zone will be unlocked if io_u offset
154 * is changed or when io_u completes and zbd_put_io() executed.
155 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
156 * other waiting for zone locks when building an io_u batch, first
157 * only trylock the zone. If the zone is already locked by another job,
158 * process the currently queued I/Os so that I/O progress is made and
161 if (pthread_mutex_trylock(&z->mutex) != 0) {
162 if (!td_ioengine_flagged(td, FIO_SYNCIO))
164 pthread_mutex_lock(&z->mutex);
168 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
170 return (uint64_t)(offset - f->file_offset) < f->io_size;
173 /* Verify whether direct I/O is used for all host-managed zoned drives. */
174 static bool zbd_using_direct_io(void)
176 struct thread_data *td;
181 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
183 for_each_file(td, f, j) {
185 f->zbd_info->model == ZBD_HOST_MANAGED)
193 /* Whether or not the I/O range for f includes one or more sequential zones */
194 static bool zbd_is_seq_job(struct fio_file *f)
196 uint32_t zone_idx, zone_idx_b, zone_idx_e;
201 zone_idx_b = zbd_zone_idx(f, f->file_offset);
202 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
203 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
204 if (zbd_zone_swr(&f->zbd_info->zone_info[zone_idx]))
211 * Verify whether offset and size parameters are aligned with zone boundaries.
213 static bool zbd_verify_sizes(void)
215 const struct fio_zone_info *z;
216 struct thread_data *td;
218 uint64_t new_offset, new_end;
223 for_each_file(td, f, j) {
226 if (f->file_offset >= f->real_file_size)
228 if (!zbd_is_seq_job(f))
231 if (!td->o.zone_size) {
232 td->o.zone_size = f->zbd_info->zone_size;
233 if (!td->o.zone_size) {
234 log_err("%s: invalid 0 zone size\n",
238 } else if (td->o.zone_size != f->zbd_info->zone_size) {
239 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
240 f->file_name, (unsigned long long) td->o.zone_size,
241 (unsigned long long) f->zbd_info->zone_size);
245 if (td->o.zone_skip &&
246 (td->o.zone_skip < td->o.zone_size ||
247 td->o.zone_skip % td->o.zone_size)) {
248 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
249 f->file_name, (unsigned long long) td->o.zone_skip,
250 (unsigned long long) td->o.zone_size);
254 zone_idx = zbd_zone_idx(f, f->file_offset);
255 z = &f->zbd_info->zone_info[zone_idx];
256 if (f->file_offset != z->start) {
257 new_offset = (z+1)->start;
258 if (new_offset >= f->file_offset + f->io_size) {
259 log_info("%s: io_size must be at least one zone\n",
263 log_info("%s: rounded up offset from %llu to %llu\n",
264 f->file_name, (unsigned long long) f->file_offset,
265 (unsigned long long) new_offset);
266 f->io_size -= (new_offset - f->file_offset);
267 f->file_offset = new_offset;
269 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
270 z = &f->zbd_info->zone_info[zone_idx];
272 if (f->file_offset + f->io_size != new_end) {
273 if (new_end <= f->file_offset) {
274 log_info("%s: io_size must be at least one zone\n",
278 log_info("%s: rounded down io_size from %llu to %llu\n",
279 f->file_name, (unsigned long long) f->io_size,
280 (unsigned long long) new_end - f->file_offset);
281 f->io_size = new_end - f->file_offset;
289 static bool zbd_verify_bs(void)
291 struct thread_data *td;
297 for_each_file(td, f, j) {
300 zone_size = f->zbd_info->zone_size;
301 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
302 if (td->o.verify != VERIFY_NONE &&
303 zone_size % td->o.bs[k] != 0) {
304 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
305 f->file_name, td->o.bs[k],
315 static int ilog2(uint64_t i)
327 * Initialize f->zbd_info for devices that are not zoned block devices. This
328 * allows to execute a ZBD workload against a non-ZBD device.
330 static int init_zone_info(struct thread_data *td, struct fio_file *f)
333 struct fio_zone_info *p;
334 uint64_t zone_size = td->o.zone_size;
335 struct zoned_block_device_info *zbd_info = NULL;
336 pthread_mutexattr_t attr;
339 if (zone_size == 0) {
340 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
345 if (zone_size < 512) {
346 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
351 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
352 zbd_info = scalloc(1, sizeof(*zbd_info) +
353 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
357 pthread_mutexattr_init(&attr);
358 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
359 pthread_mutexattr_setpshared(&attr, true);
360 pthread_mutex_init(&zbd_info->mutex, &attr);
361 zbd_info->refcount = 1;
362 p = &zbd_info->zone_info[0];
363 for (i = 0; i < nr_zones; i++, p++) {
364 pthread_mutex_init(&p->mutex, &attr);
365 p->start = i * zone_size;
366 p->wp = p->start + zone_size;
367 p->type = ZBD_ZONE_TYPE_SWR;
368 p->cond = ZBD_ZONE_COND_EMPTY;
371 p->start = nr_zones * zone_size;
373 f->zbd_info = zbd_info;
374 f->zbd_info->zone_size = zone_size;
375 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
376 ilog2(zone_size) : 0;
377 f->zbd_info->nr_zones = nr_zones;
378 pthread_mutexattr_destroy(&attr);
383 * Maximum number of zones to report in one operation.
385 #define ZBD_REPORT_MAX_ZONES 8192U
388 * Parse the device zone report and store it in f->zbd_info. Must be called
389 * only for devices that are zoned, namely those with a model != ZBD_NONE.
391 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
394 struct zbd_zone *zones, *z;
395 struct fio_zone_info *p;
396 uint64_t zone_size, offset;
397 struct zoned_block_device_info *zbd_info = NULL;
398 pthread_mutexattr_t attr;
401 pthread_mutexattr_init(&attr);
402 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
403 pthread_mutexattr_setpshared(&attr, true);
405 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
409 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
412 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
417 zone_size = zones[0].len;
418 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
420 if (td->o.zone_size == 0) {
421 td->o.zone_size = zone_size;
422 } else if (td->o.zone_size != zone_size) {
423 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
424 f->file_name, (unsigned long long) td->o.zone_size,
425 (unsigned long long) zone_size);
430 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
431 nr_zones, (unsigned long long) zone_size / 1024);
433 zbd_info = scalloc(1, sizeof(*zbd_info) +
434 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
438 pthread_mutex_init(&zbd_info->mutex, &attr);
439 zbd_info->refcount = 1;
440 p = &zbd_info->zone_info[0];
441 for (offset = 0, j = 0; j < nr_zones;) {
443 for (i = 0; i < nrz; i++, j++, z++, p++) {
444 pthread_mutex_init(&p->mutex, &attr);
447 case ZBD_ZONE_COND_NOT_WP:
448 case ZBD_ZONE_COND_FULL:
449 p->wp = p->start + zone_size;
452 assert(z->start <= z->wp);
453 assert(z->wp <= z->start + zone_size);
459 if (j > 0 && p->start != p[-1].start + zone_size) {
460 log_info("%s: invalid zone data\n",
467 offset = z->start + z->len;
470 nrz = zbd_report_zones(td, f, offset,
471 zones, ZBD_REPORT_MAX_ZONES);
474 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
475 (unsigned long long)offset,
482 zbd_info->zone_info[nr_zones].start = offset;
484 f->zbd_info = zbd_info;
485 f->zbd_info->zone_size = zone_size;
486 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
487 ilog2(zone_size) : 0;
488 f->zbd_info->nr_zones = nr_zones;
495 pthread_mutexattr_destroy(&attr);
500 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
502 * Returns 0 upon success and a negative error code upon failure.
504 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
506 enum zbd_zoned_model zbd_model;
509 assert(td->o.zone_mode == ZONE_MODE_ZBD);
511 ret = zbd_get_zoned_model(td, f, &zbd_model);
519 case ZBD_HOST_MANAGED:
520 ret = parse_zone_info(td, f);
523 ret = init_zone_info(td, f);
526 td_verror(td, EINVAL, "Unsupported zoned model");
527 log_err("Unsupported zoned model\n");
532 f->zbd_info->model = zbd_model;
536 void zbd_free_zone_info(struct fio_file *f)
543 pthread_mutex_lock(&f->zbd_info->mutex);
544 refcount = --f->zbd_info->refcount;
545 pthread_mutex_unlock(&f->zbd_info->mutex);
547 assert((int32_t)refcount >= 0);
554 * Initialize f->zbd_info.
556 * Returns 0 upon success and a negative error code upon failure.
558 * Note: this function can only work correctly if it is called before the first
561 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
563 struct thread_data *td2;
567 for_each_td(td2, i) {
568 for_each_file(td2, f2, j) {
569 if (td2 == td && f2 == file)
572 strcmp(f2->file_name, file->file_name) != 0)
574 file->zbd_info = f2->zbd_info;
575 file->zbd_info->refcount++;
580 ret = zbd_create_zone_info(td, file);
582 td_verror(td, -ret, "zbd_create_zone_info() failed");
586 int zbd_init(struct thread_data *td)
591 for_each_file(td, f, i) {
592 if (zbd_init_zone_info(td, f))
596 if (!zbd_using_direct_io()) {
597 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
601 if (!zbd_verify_sizes())
604 if (!zbd_verify_bs())
611 * zbd_reset_range - reset zones for a range of sectors
612 * @td: FIO thread data.
613 * @f: Fio file for which to reset zones
614 * @sector: Starting sector in units of 512 bytes
615 * @nr_sectors: Number of sectors in units of 512 bytes
617 * Returns 0 upon success and a negative error code upon failure.
619 static int zbd_reset_range(struct thread_data *td, struct fio_file *f,
620 uint64_t offset, uint64_t length)
622 uint32_t zone_idx_b, zone_idx_e;
623 struct fio_zone_info *zb, *ze, *z;
627 assert(is_valid_offset(f, offset + length - 1));
629 switch (f->zbd_info->model) {
631 case ZBD_HOST_MANAGED:
632 ret = zbd_reset_wp(td, f, offset, length);
640 zone_idx_b = zbd_zone_idx(f, offset);
641 zb = &f->zbd_info->zone_info[zone_idx_b];
642 zone_idx_e = zbd_zone_idx(f, offset + length);
643 ze = &f->zbd_info->zone_info[zone_idx_e];
644 for (z = zb; z < ze; z++) {
645 pthread_mutex_lock(&z->mutex);
646 pthread_mutex_lock(&f->zbd_info->mutex);
647 f->zbd_info->sectors_with_data -= z->wp - z->start;
648 pthread_mutex_unlock(&f->zbd_info->mutex);
651 pthread_mutex_unlock(&z->mutex);
654 td->ts.nr_zone_resets += ze - zb;
659 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
660 struct fio_zone_info *zone)
662 return zone - zbd_info->zone_info;
666 * zbd_reset_zone - reset the write pointer of a single zone
667 * @td: FIO thread data.
668 * @f: FIO file associated with the disk for which to reset a write pointer.
671 * Returns 0 upon success and a negative error code upon failure.
673 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
674 struct fio_zone_info *z)
676 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
677 zbd_zone_nr(f->zbd_info, z));
679 return zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
683 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
684 * @td: fio thread data.
685 * @f: fio file for which to reset zones
686 * @zb: first zone to reset.
687 * @ze: first zone not to reset.
688 * @all_zones: whether to reset all zones or only those zones for which the
689 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
691 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
692 struct fio_zone_info *const zb,
693 struct fio_zone_info *const ze, bool all_zones)
695 struct fio_zone_info *z;
696 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
700 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
701 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
703 for (z = zb; z < ze; z++) {
704 if (!zbd_zone_swr(z))
707 reset_wp = all_zones ? z->wp != z->start :
708 (td->o.td_ddir & TD_DDIR_WRITE) &&
711 dprint(FD_ZBD, "%s: resetting zone %u\n",
713 zbd_zone_nr(f->zbd_info, z));
714 if (zbd_reset_zone(td, f, z) < 0)
717 pthread_mutex_unlock(&z->mutex);
724 * Reset zbd_info.write_cnt, the counter that counts down towards the next
727 static void zbd_reset_write_cnt(const struct thread_data *td,
728 const struct fio_file *f)
730 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
732 pthread_mutex_lock(&f->zbd_info->mutex);
733 f->zbd_info->write_cnt = td->o.zrf.u.f ?
734 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
735 pthread_mutex_unlock(&f->zbd_info->mutex);
738 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
739 const struct fio_file *f)
741 uint32_t write_cnt = 0;
743 pthread_mutex_lock(&f->zbd_info->mutex);
744 assert(f->zbd_info->write_cnt);
745 if (f->zbd_info->write_cnt)
746 write_cnt = --f->zbd_info->write_cnt;
748 zbd_reset_write_cnt(td, f);
749 pthread_mutex_unlock(&f->zbd_info->mutex);
751 return write_cnt == 0;
759 /* Calculate the number of sectors with data (swd) and perform action 'a' */
760 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
762 struct fio_zone_info *zb, *ze, *z;
765 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
766 ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
768 for (z = zb; z < ze; z++) {
769 pthread_mutex_lock(&z->mutex);
770 swd += z->wp - z->start;
772 pthread_mutex_lock(&f->zbd_info->mutex);
775 assert(f->zbd_info->sectors_with_data == swd);
778 f->zbd_info->sectors_with_data = swd;
781 pthread_mutex_unlock(&f->zbd_info->mutex);
782 for (z = zb; z < ze; z++)
783 pthread_mutex_unlock(&z->mutex);
789 * The swd check is useful for debugging but takes too much time to leave
790 * it enabled all the time. Hence it is disabled by default.
792 static const bool enable_check_swd = false;
794 /* Check whether the value of zbd_info.sectors_with_data is correct. */
795 static void zbd_check_swd(const struct fio_file *f)
797 if (!enable_check_swd)
800 zbd_process_swd(f, CHECK_SWD);
803 static void zbd_init_swd(struct fio_file *f)
807 if (!enable_check_swd)
810 swd = zbd_process_swd(f, SET_SWD);
811 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
815 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
817 struct fio_zone_info *zb, *ze;
823 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
824 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size);
825 ze = &f->zbd_info->zone_info[zone_idx_e];
828 * If data verification is enabled reset the affected zones before
829 * writing any data to avoid that a zone reset has to be issued while
830 * writing data, which causes data loss.
832 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
833 (td->o.td_ddir & TD_DDIR_WRITE) &&
834 td->runstate != TD_VERIFYING);
835 zbd_reset_write_cnt(td, f);
838 /* The caller must hold f->zbd_info->mutex. */
839 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
840 unsigned int zone_idx)
842 struct zoned_block_device_info *zbdi = f->zbd_info;
845 assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones));
846 assert(zbdi->num_open_zones <= td->o.max_open_zones);
848 for (i = 0; i < zbdi->num_open_zones; i++)
849 if (zbdi->open_zones[i] == zone_idx)
856 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
857 * already open or if opening a new zone is allowed. Returns false if the zone
858 * was not yet open and opening a new zone would cause the zone limit to be
861 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
864 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
865 const struct fio_file *f = io_u->file;
866 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
869 if (z->cond == ZBD_ZONE_COND_OFFLINE)
873 * Skip full zones with data verification enabled because resetting a
874 * zone causes data loss and hence causes verification to fail.
876 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
879 /* Zero means no limit */
880 if (!td->o.max_open_zones)
883 pthread_mutex_lock(&f->zbd_info->mutex);
884 if (is_zone_open(td, f, zone_idx))
887 if (f->zbd_info->num_open_zones >= td->o.max_open_zones)
889 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
890 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
895 pthread_mutex_unlock(&f->zbd_info->mutex);
899 /* The caller must hold f->zbd_info->mutex */
900 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
901 unsigned int open_zone_idx)
905 assert(open_zone_idx < f->zbd_info->num_open_zones);
906 zone_idx = f->zbd_info->open_zones[open_zone_idx];
907 memmove(f->zbd_info->open_zones + open_zone_idx,
908 f->zbd_info->open_zones + open_zone_idx + 1,
909 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
910 sizeof(f->zbd_info->open_zones[0]));
911 f->zbd_info->num_open_zones--;
912 f->zbd_info->zone_info[zone_idx].open = 0;
915 /* Anything goes as long as it is not a constant. */
916 static uint32_t pick_random_zone_idx(const struct fio_file *f,
917 const struct io_u *io_u)
919 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
923 * Modify the offset of an I/O unit that does not refer to an open zone such
924 * that it refers to an open zone. Close an open zone and open a new zone if
925 * necessary. This algorithm can only work correctly if all write pointers are
926 * a multiple of the fio block size. The caller must neither hold z->mutex
927 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
929 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
932 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
933 const struct fio_file *f = io_u->file;
934 struct fio_zone_info *z;
935 unsigned int open_zone_idx = -1;
936 uint32_t zone_idx, new_zone_idx;
939 assert(is_valid_offset(f, io_u->offset));
941 if (td->o.max_open_zones) {
943 * This statement accesses f->zbd_info->open_zones[] on purpose
946 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
948 zone_idx = zbd_zone_idx(f, io_u->offset);
950 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
951 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
954 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
955 * lock it can happen that the state of the zone with index zone_idx
956 * has changed after 'z' has been assigned and before f->zbd_info->mutex
957 * has been obtained. Hence the loop.
962 z = &f->zbd_info->zone_info[zone_idx];
965 pthread_mutex_lock(&f->zbd_info->mutex);
966 if (td->o.max_open_zones == 0)
968 if (f->zbd_info->num_open_zones == 0) {
969 pthread_mutex_unlock(&f->zbd_info->mutex);
970 pthread_mutex_unlock(&z->mutex);
971 dprint(FD_ZBD, "%s(%s): no zones are open\n",
972 __func__, f->file_name);
977 * List of opened zones is per-device, shared across all threads.
978 * Start with quasi-random candidate zone.
979 * Ignore zones which don't belong to thread's offset/size area.
981 open_zone_idx = pick_random_zone_idx(f, io_u);
982 assert(open_zone_idx < f->zbd_info->num_open_zones);
983 tmp_idx = open_zone_idx;
984 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
987 if (tmp_idx >= f->zbd_info->num_open_zones)
989 tmpz = f->zbd_info->open_zones[tmp_idx];
991 if (is_valid_offset(f, f->zbd_info->zone_info[tmpz].start)) {
992 open_zone_idx = tmp_idx;
993 goto found_candidate_zone;
999 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1000 __func__, f->file_name);
1003 found_candidate_zone:
1004 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1005 if (new_zone_idx == zone_idx)
1007 zone_idx = new_zone_idx;
1008 pthread_mutex_unlock(&f->zbd_info->mutex);
1009 pthread_mutex_unlock(&z->mutex);
1012 /* Both z->mutex and f->zbd_info->mutex are held. */
1015 if (z->wp + min_bs <= (z+1)->start) {
1016 pthread_mutex_unlock(&f->zbd_info->mutex);
1019 dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
1021 if (td->o.max_open_zones)
1022 zbd_close_zone(td, f, open_zone_idx);
1023 pthread_mutex_unlock(&f->zbd_info->mutex);
1025 /* Only z->mutex is held. */
1027 /* Zone 'z' is full, so try to open a new zone. */
1028 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1030 pthread_mutex_unlock(&z->mutex);
1032 if (!is_valid_offset(f, z->start)) {
1034 zone_idx = zbd_zone_idx(f, f->file_offset);
1035 z = &f->zbd_info->zone_info[zone_idx];
1037 assert(is_valid_offset(f, z->start));
1041 if (zbd_open_zone(td, io_u, zone_idx))
1045 /* Only z->mutex is held. */
1047 /* Check whether the write fits in any of the already opened zones. */
1048 pthread_mutex_lock(&f->zbd_info->mutex);
1049 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1050 zone_idx = f->zbd_info->open_zones[i];
1051 pthread_mutex_unlock(&f->zbd_info->mutex);
1052 pthread_mutex_unlock(&z->mutex);
1054 z = &f->zbd_info->zone_info[zone_idx];
1057 if (z->wp + min_bs <= (z+1)->start)
1059 pthread_mutex_lock(&f->zbd_info->mutex);
1061 pthread_mutex_unlock(&f->zbd_info->mutex);
1062 pthread_mutex_unlock(&z->mutex);
1063 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1068 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1070 io_u->offset = z->start;
1074 /* The caller must hold z->mutex. */
1075 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1077 struct fio_zone_info *z)
1079 const struct fio_file *f = io_u->file;
1080 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1082 if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1083 pthread_mutex_unlock(&z->mutex);
1084 z = zbd_convert_to_open_zone(td, io_u);
1088 if (z->verify_block * min_bs >= f->zbd_info->zone_size)
1089 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1090 min_bs, (unsigned long long) f->zbd_info->zone_size);
1091 io_u->offset = z->start + z->verify_block++ * min_bs;
1096 * Find another zone for which @io_u fits below the write pointer. Start
1097 * searching in zones @zb + 1 .. @zl and continue searching in zones
1100 * Either returns NULL or returns a zone pointer and holds the mutex for that
1103 static struct fio_zone_info *
1104 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1105 struct fio_zone_info *zb, struct fio_zone_info *zl)
1107 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1108 const struct fio_file *f = io_u->file;
1109 struct fio_zone_info *z1, *z2;
1110 const struct fio_zone_info *const zf =
1111 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1114 * Skip to the next non-empty zone in case of sequential I/O and to
1115 * the nearest non-empty zone in case of random I/O.
1117 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1118 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1119 pthread_mutex_lock(&z1->mutex);
1120 if (z1->start + min_bs <= z1->wp)
1122 pthread_mutex_unlock(&z1->mutex);
1123 } else if (!td_random(td)) {
1126 if (td_random(td) && z2 >= zf &&
1127 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1128 pthread_mutex_lock(&z2->mutex);
1129 if (z2->start + min_bs <= z2->wp)
1131 pthread_mutex_unlock(&z2->mutex);
1134 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1140 * zbd_queue_io - update the write pointer of a sequential zone
1142 * @success: Whether or not the I/O unit has been queued successfully
1143 * @q: queueing status (busy, completed or queued).
1145 * For write and trim operations, update the write pointer of the I/O unit
1148 static void zbd_queue_io(struct io_u *io_u, int q, bool success)
1150 const struct fio_file *f = io_u->file;
1151 struct zoned_block_device_info *zbd_info = f->zbd_info;
1152 struct fio_zone_info *z;
1159 zone_idx = zbd_zone_idx(f, io_u->offset);
1160 assert(zone_idx < zbd_info->nr_zones);
1161 z = &zbd_info->zone_info[zone_idx];
1163 if (!zbd_zone_swr(z))
1170 "%s: queued I/O (%lld, %llu) for zone %u\n",
1171 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1173 switch (io_u->ddir) {
1175 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1177 pthread_mutex_lock(&zbd_info->mutex);
1179 * z->wp > zone_end means that one or more I/O errors
1182 if (z->wp <= zone_end)
1183 zbd_info->sectors_with_data += zone_end - z->wp;
1184 pthread_mutex_unlock(&zbd_info->mutex);
1188 assert(z->wp == z->start);
1195 if (!success || q != FIO_Q_QUEUED) {
1196 /* BUSY or COMPLETED: unlock the zone */
1197 pthread_mutex_unlock(&z->mutex);
1198 io_u->zbd_put_io = NULL;
1203 * zbd_put_io - Unlock an I/O unit target zone lock
1206 static void zbd_put_io(const struct io_u *io_u)
1208 const struct fio_file *f = io_u->file;
1209 struct zoned_block_device_info *zbd_info = f->zbd_info;
1210 struct fio_zone_info *z;
1216 zone_idx = zbd_zone_idx(f, io_u->offset);
1217 assert(zone_idx < zbd_info->nr_zones);
1218 z = &zbd_info->zone_info[zone_idx];
1220 if (!zbd_zone_swr(z))
1224 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1225 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1227 assert(pthread_mutex_unlock(&z->mutex) == 0);
1231 bool zbd_unaligned_write(int error_code)
1233 switch (error_code) {
1242 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1243 * @td: FIO thread data.
1244 * @io_u: FIO I/O unit.
1246 * For sequential workloads, change the file offset to skip zoneskip bytes when
1247 * no more IO can be performed in the current zone.
1248 * - For read workloads, zoneskip is applied when the io has reached the end of
1249 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1250 * - For write workloads, zoneskip is applied when the zone is full.
1251 * This applies only to read and write operations.
1253 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1255 struct fio_file *f = io_u->file;
1256 enum fio_ddir ddir = io_u->ddir;
1257 struct fio_zone_info *z;
1260 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1261 assert(td->o.zone_size);
1264 * zone_skip is valid only for sequential workloads.
1266 if (td_random(td) || !td->o.zone_skip)
1270 * It is time to switch to a new zone if:
1271 * - zone_bytes == zone_size bytes have already been accessed
1272 * - The last position reached the end of the current zone.
1273 * - For reads with td->o.read_beyond_wp == false, the last position
1274 * reached the zone write pointer.
1276 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1277 z = &f->zbd_info->zone_info[zone_idx];
1279 if (td->zone_bytes >= td->o.zone_size ||
1280 f->last_pos[ddir] >= (z+1)->start ||
1281 (ddir == DDIR_READ &&
1282 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1287 f->file_offset += td->o.zone_size + td->o.zone_skip;
1290 * Wrap from the beginning, if we exceed the file size
1292 if (f->file_offset >= f->real_file_size)
1293 f->file_offset = get_start_offset(td, f);
1295 f->last_pos[ddir] = f->file_offset;
1296 td->io_skip_bytes += td->o.zone_skip;
1301 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1302 * @td: FIO thread data.
1303 * @io_u: FIO I/O unit.
1305 * Locking strategy: returns with z->mutex locked if and only if z refers
1306 * to a sequential zone and if io_u_accept is returned. z is the zone that
1307 * corresponds to io_u->offset at the end of this function.
1309 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1311 struct fio_file *f = io_u->file;
1312 uint32_t zone_idx_b;
1313 struct fio_zone_info *zb, *zl, *orig_zb;
1314 uint32_t orig_len = io_u->buflen;
1315 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1322 assert(is_valid_offset(f, io_u->offset));
1323 assert(io_u->buflen);
1324 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1325 zb = &f->zbd_info->zone_info[zone_idx_b];
1328 /* Accept the I/O offset for conventional zones. */
1329 if (!zbd_zone_swr(zb))
1333 * Accept the I/O offset for reads if reading beyond the write pointer
1336 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1337 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1344 switch (io_u->ddir) {
1346 if (td->runstate == TD_VERIFYING) {
1347 zb = zbd_replay_write_order(td, io_u, zb);
1351 * Check that there is enough written data in the zone to do an
1352 * I/O of at least min_bs B. If there isn't, find a new zone for
1355 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1356 zb->wp - zb->start : 0;
1357 if (range < min_bs ||
1358 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1359 pthread_mutex_unlock(&zb->mutex);
1360 zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1361 f->file_offset + f->io_size)];
1362 zb = zbd_find_zone(td, io_u, zb, zl);
1365 "%s: zbd_find_zone(%lld, %llu) failed\n",
1366 f->file_name, io_u->offset,
1371 * zbd_find_zone() returned a zone with a range of at
1374 range = zb->wp - zb->start;
1375 assert(range >= min_bs);
1378 io_u->offset = zb->start;
1381 * Make sure the I/O is within the zone valid data range while
1382 * maximizing the I/O size and preserving randomness.
1384 if (range <= io_u->buflen)
1385 io_u->offset = zb->start;
1386 else if (td_random(td))
1387 io_u->offset = zb->start +
1388 ((io_u->offset - orig_zb->start) %
1389 (range - io_u->buflen)) / min_bs * min_bs;
1391 * Make sure the I/O does not cross over the zone wp position.
1393 new_len = min((unsigned long long)io_u->buflen,
1394 (unsigned long long)(zb->wp - io_u->offset));
1395 new_len = new_len / min_bs * min_bs;
1396 if (new_len < io_u->buflen) {
1397 io_u->buflen = new_len;
1398 dprint(FD_IO, "Changed length from %u into %llu\n",
1399 orig_len, io_u->buflen);
1401 assert(zb->start <= io_u->offset);
1402 assert(io_u->offset + io_u->buflen <= zb->wp);
1405 if (io_u->buflen > f->zbd_info->zone_size)
1407 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1408 pthread_mutex_unlock(&zb->mutex);
1409 zb = zbd_convert_to_open_zone(td, io_u);
1412 zone_idx_b = zb - f->zbd_info->zone_info;
1414 /* Check whether the zone reset threshold has been exceeded */
1415 if (td->o.zrf.u.f) {
1416 if (f->zbd_info->sectors_with_data >=
1417 f->io_size * td->o.zrt.u.f &&
1418 zbd_dec_and_reset_write_cnt(td, f)) {
1422 /* Reset the zone pointer if necessary */
1423 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1424 assert(td->o.verify == VERIFY_NONE);
1426 * Since previous write requests may have been submitted
1427 * asynchronously and since we will submit the zone
1428 * reset synchronously, wait until previously submitted
1429 * write requests have completed before issuing a
1434 if (zbd_reset_zone(td, f, zb) < 0)
1437 /* Make writes occur at the write pointer */
1438 assert(!zbd_zone_full(f, zb, min_bs));
1439 io_u->offset = zb->wp;
1440 if (!is_valid_offset(f, io_u->offset)) {
1441 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1446 * Make sure that the buflen is a multiple of the minimal
1447 * block size. Give up if shrinking would make the request too
1450 new_len = min((unsigned long long)io_u->buflen,
1451 (zb + 1)->start - io_u->offset);
1452 new_len = new_len / min_bs * min_bs;
1453 if (new_len == io_u->buflen)
1455 if (new_len >= min_bs) {
1456 io_u->buflen = new_len;
1457 dprint(FD_IO, "Changed length from %u into %llu\n",
1458 orig_len, io_u->buflen);
1461 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1462 ((zb + 1)->start - io_u->offset),
1469 case DDIR_SYNC_FILE_RANGE:
1480 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1481 assert(!io_u->zbd_queue_io);
1482 assert(!io_u->zbd_put_io);
1483 io_u->zbd_queue_io = zbd_queue_io;
1484 io_u->zbd_put_io = zbd_put_io;
1489 pthread_mutex_unlock(&zb->mutex);
1493 /* Return a string with ZBD statistics */
1494 char *zbd_write_status(const struct thread_stat *ts)
1498 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)