engines: don't use printf or stderr directly
[fio.git] / engines / fusion-aw.c
CommitLineData
6ef63580
SK
1/*
2 * Custom fio(1) engine that submits synchronous atomic writes to file.
3 *
b2c77c25
SK
4 * Copyright (C) 2013 Fusion-io, Inc.
5 * Author: Santhosh Kumar Koundinya (skoundinya@fusionio.com).
6ef63580
SK
6 *
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
15a6f166 9 * Software Foundation; under version 2 of the License.
6ef63580
SK
10 *
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
14 * 2 for more details.
15 *
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/>
18 */
19
20#include <stdlib.h>
21#include <stdint.h>
22
23#include "../fio.h"
24
b2c77c25 25#include <nvm/nvm_primitives.h>
6ef63580 26
4728b3c8
CM
27#define NUM_ATOMIC_CAPABILITIES (5)
28
b2c77c25
SK
29struct fas_data {
30 nvm_handle_t nvm_handle;
31 size_t xfer_buf_align;
32 size_t xfer_buflen_align;
33 size_t xfer_buflen_max;
34 size_t sector_size;
6ef63580
SK
35};
36
37static int queue(struct thread_data *td, struct io_u *io_u)
38{
39 int rc;
b2c77c25 40 struct fas_data *d = (struct fas_data *) io_u->file->engine_data;
6ef63580
SK
41
42 if (io_u->ddir != DDIR_WRITE) {
b2c77c25
SK
43 td_vmsg(td, EINVAL, "only writes supported", "io_u->ddir");
44 rc = -EINVAL;
6ef63580
SK
45 goto out;
46 }
b2c77c25
SK
47
48 if ((size_t) io_u->xfer_buf % d->xfer_buf_align) {
49 td_vmsg(td, EINVAL, "unaligned data buffer", "io_u->xfer_buf");
50 rc = -EINVAL;
6ef63580
SK
51 goto out;
52 }
b2c77c25
SK
53
54 if (io_u->xfer_buflen % d->xfer_buflen_align) {
55 td_vmsg(td, EINVAL, "unaligned data size", "io_u->xfer_buflen");
56 rc = -EINVAL;
6ef63580
SK
57 goto out;
58 }
59
b2c77c25
SK
60 if (io_u->xfer_buflen > d->xfer_buflen_max) {
61 td_vmsg(td, EINVAL, "data too big", "io_u->xfer_buflen");
62 rc = -EINVAL;
63 goto out;
6ef63580 64 }
6ef63580 65
b2c77c25
SK
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);
6ef63580 68 if (rc == -1) {
b2c77c25
SK
69 td_verror(td, errno, "nvm_atomic_write");
70 rc = -errno;
6ef63580 71 goto out;
6ef63580 72 }
b2c77c25 73 rc = FIO_Q_COMPLETED;
6ef63580
SK
74out:
75 if (rc < 0)
b2c77c25 76 io_u->error = -rc;
6ef63580
SK
77
78 return rc;
79}
80
81static int open_file(struct thread_data *td, struct fio_file *f)
82{
83 int rc;
b2c77c25
SK
84 int fio_unused close_file_rc;
85 struct fas_data *d;
86 nvm_version_t nvm_version;
4728b3c8 87 nvm_capability_t nvm_capability[NUM_ATOMIC_CAPABILITIES];
b2c77c25 88
6ef63580
SK
89
90 d = malloc(sizeof(*d));
91 if (!d) {
b2c77c25
SK
92 td_verror(td, ENOMEM, "malloc");
93 rc = ENOMEM;
6ef63580
SK
94 goto error;
95 }
b2c77c25 96 d->nvm_handle = -1;
c11a9ddf 97 f->engine_data = (uintptr_t) d;
6ef63580
SK
98
99 rc = generic_open_file(td, f);
100
b2c77c25
SK
101 if (rc)
102 goto free_engine_data;
103
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;
108
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");
112 rc = errno;
113 goto close_file;
114 }
115
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;
4728b3c8
CM
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);
b2c77c25
SK
123 if (rc == -1) {
124 td_vmsg(td, errno, "error in getting atomic write capabilities", "nvm_get_capabilities");
125 rc = errno;
126 goto close_file;
4728b3c8 127 } else if (rc < NUM_ATOMIC_CAPABILITIES) {
b2c77c25
SK
128 td_vmsg(td, EINVAL, "couldn't get all the atomic write capabilities" , "nvm_get_capabilities");
129 rc = ECANCELED;
130 goto close_file;
131 }
132 /* Reset rc to 0 because we got all capabilities we needed */
133 rc = 0;
134 d->xfer_buf_align = nvm_capability[0].cap_value;
135 d->xfer_buflen_align = nvm_capability[1].cap_value;
4728b3c8 136 d->xfer_buflen_max = d->xfer_buflen_align * nvm_capability[2].cap_value * nvm_capability[4].cap_value;
b2c77c25
SK
137 d->sector_size = nvm_capability[3].cap_value;
138
6ef63580
SK
139out:
140 return rc;
b2c77c25
SK
141close_file:
142 close_file_rc = generic_close_file(td, f);
143free_engine_data:
144 free(d);
6ef63580
SK
145error:
146 f->fd = -1;
c11a9ddf 147 f->engine_data = 0;
6ef63580
SK
148 goto out;
149}
150
151static int close_file(struct thread_data *td, struct fio_file *f)
152{
b2c77c25
SK
153 struct fas_data *d = (struct fas_data *) f->engine_data;
154
155 if (d) {
156 if (d->nvm_handle != -1)
157 nvm_release_handle(d->nvm_handle);
158 free(d);
c11a9ddf 159 f->engine_data = 0;
6ef63580
SK
160 }
161
162 return generic_close_file(td, f);
163}
164
165static struct ioengine_ops ioengine = {
166 .name = "fusion-aw-sync",
167 .version = FIO_IOOPS_VERSION,
168 .queue = queue,
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
173};
174
6ef63580
SK
175static void fio_init fio_fusion_aw_init(void)
176{
177 register_ioengine(&ioengine);
178}
179
180static void fio_exit fio_fusion_aw_exit(void)
181{
182 unregister_ioengine(&ioengine);
183}