2 * Custom fio(1) engine that submits synchronous atomic writes to file.
4 * Copyright (C) 2013 Fusion-io, Inc.
5 * Author: Santhosh Kumar Koundinya (skoundinya@fusionio.com).
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; under version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License version
16 * You should have received a copy of the GNU General Public License Version 2
17 * along with this program; if not see <http://www.gnu.org/licenses/>
25 #include <nvm/nvm_primitives.h>
27 #define NUM_ATOMIC_CAPABILITIES (5)
30 nvm_handle_t nvm_handle;
31 size_t xfer_buf_align;
32 size_t xfer_buflen_align;
33 size_t xfer_buflen_max;
37 static int queue(struct thread_data *td, struct io_u *io_u)
39 struct fas_data *d = FILE_ENG_DATA(io_u->file);
42 if (io_u->ddir != DDIR_WRITE) {
43 td_vmsg(td, EINVAL, "only writes supported", "io_u->ddir");
48 if ((size_t) io_u->xfer_buf % d->xfer_buf_align) {
49 td_vmsg(td, EINVAL, "unaligned data buffer", "io_u->xfer_buf");
54 if (io_u->xfer_buflen % d->xfer_buflen_align) {
55 td_vmsg(td, EINVAL, "unaligned data size", "io_u->xfer_buflen");
60 if (io_u->xfer_buflen > d->xfer_buflen_max) {
61 td_vmsg(td, EINVAL, "data too big", "io_u->xfer_buflen");
66 rc = nvm_atomic_write(d->nvm_handle, (uint64_t) io_u->xfer_buf,
67 io_u->xfer_buflen, io_u->offset / d->sector_size);
69 td_verror(td, errno, "nvm_atomic_write");
81 static int open_file(struct thread_data *td, struct fio_file *f)
84 int fio_unused close_file_rc;
86 nvm_version_t nvm_version;
87 nvm_capability_t nvm_capability[NUM_ATOMIC_CAPABILITIES];
90 d = malloc(sizeof(*d));
92 td_verror(td, ENOMEM, "malloc");
97 FILE_SET_ENG_DATA(f, d);
99 rc = generic_open_file(td, f);
102 goto free_engine_data;
104 /* Set the version of the library as seen when engine is compiled */
105 nvm_version.major = NVM_PRIMITIVES_API_MAJOR;
106 nvm_version.minor = NVM_PRIMITIVES_API_MINOR;
107 nvm_version.micro = NVM_PRIMITIVES_API_MICRO;
109 d->nvm_handle = nvm_get_handle(f->fd, &nvm_version);
110 if (d->nvm_handle == -1) {
111 td_vmsg(td, errno, "nvm_get_handle failed", "nvm_get_handle");
116 nvm_capability[0].cap_id = NVM_CAP_ATOMIC_WRITE_START_ALIGN_ID;
117 nvm_capability[1].cap_id = NVM_CAP_ATOMIC_WRITE_MULTIPLICITY_ID;
118 nvm_capability[2].cap_id = NVM_CAP_ATOMIC_WRITE_MAX_VECTOR_SIZE_ID;
119 nvm_capability[3].cap_id = NVM_CAP_SECTOR_SIZE_ID;
120 nvm_capability[4].cap_id = NVM_CAP_ATOMIC_MAX_IOV_ID;
121 rc = nvm_get_capabilities(d->nvm_handle, nvm_capability,
122 NUM_ATOMIC_CAPABILITIES, false);
124 td_vmsg(td, errno, "error in getting atomic write capabilities", "nvm_get_capabilities");
127 } else if (rc < NUM_ATOMIC_CAPABILITIES) {
128 td_vmsg(td, EINVAL, "couldn't get all the atomic write capabilities" , "nvm_get_capabilities");
132 /* Reset rc to 0 because we got all capabilities we needed */
134 d->xfer_buf_align = nvm_capability[0].cap_value;
135 d->xfer_buflen_align = nvm_capability[1].cap_value;
136 d->xfer_buflen_max = d->xfer_buflen_align * nvm_capability[2].cap_value * nvm_capability[4].cap_value;
137 d->sector_size = nvm_capability[3].cap_value;
142 close_file_rc = generic_close_file(td, f);
147 FILE_SET_ENG_DATA(f, NULL);
151 static int close_file(struct thread_data *td, struct fio_file *f)
153 struct fas_data *d = FILE_ENG_DATA(f);
156 if (d->nvm_handle != -1)
157 nvm_release_handle(d->nvm_handle);
159 FILE_SET_ENG_DATA(f, NULL);
162 return generic_close_file(td, f);
165 static struct ioengine_ops ioengine = {
166 .name = "fusion-aw-sync",
167 .version = FIO_IOOPS_VERSION,
169 .open_file = open_file,
170 .close_file = close_file,
171 .get_file_size = generic_get_file_size,
172 .flags = FIO_SYNCIO | FIO_RAWIO | FIO_MEMALIGN
175 static void fio_init fio_fusion_aw_init(void)
177 register_ioengine(&ioengine);
180 static void fio_exit fio_fusion_aw_exit(void)
182 unregister_ioengine(&ioengine);