Cleanup generic_file_open()
[fio.git] / engines / null.c
CommitLineData
a94ea28b 1/*
da751ca9
JA
2 * null engine
3 *
4 * IO engine that doesn't do any real IO transfers, it just pretends to.
5 * The main purpose is to test fio itself.
a94ea28b
JA
6 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
13
14#include "../fio.h"
15#include "../os.h"
16
65afa5f2
JA
17struct null_data {
18 struct io_u **io_us;
19 int queued;
20 int events;
21};
22
23static struct io_u *fio_null_event(struct thread_data *td, int event)
24{
25 struct null_data *nd = td->io_ops->data;
26
27 return nd->io_us[event];
28}
29
7401c088 30static int fio_null_getevents(struct thread_data *td, int min_events,
65afa5f2
JA
31 int fio_unused max, struct timespec fio_unused *t)
32{
33 struct null_data *nd = td->io_ops->data;
7401c088
JA
34 int ret = 0;
35
36 if (min_events) {
37 ret = nd->events;
38 nd->events = 0;
39 }
65afa5f2 40
65afa5f2
JA
41 return ret;
42}
43
44static int fio_null_commit(struct thread_data *td)
45{
46 struct null_data *nd = td->io_ops->data;
47
ed8bd849
JA
48 if (!nd->events) {
49 nd->events = nd->queued;
50 nd->queued = 0;
51 }
52
65afa5f2
JA
53 return 0;
54}
55
36167d82 56static int fio_null_queue(struct thread_data fio_unused *td, struct io_u *io_u)
a94ea28b 57{
65afa5f2
JA
58 struct null_data *nd = td->io_ops->data;
59
60 if (td->io_ops->flags & FIO_SYNCIO)
61 return FIO_Q_COMPLETED;
ed8bd849
JA
62 if (nd->events)
63 return FIO_Q_BUSY;
65afa5f2
JA
64
65 nd->io_us[nd->queued++] = io_u;
66 return FIO_Q_QUEUED;
a94ea28b
JA
67}
68
2fd233b7
JA
69static int fio_null_setup(struct thread_data *td)
70{
71 struct fio_file *f;
af52b345 72 unsigned int i;
2fd233b7 73
2dc1bbeb 74 if (!td->o.size) {
2fd233b7
JA
75 log_err("fio: need size= set\n");
76 return 1;
77 }
78
2dc1bbeb 79 td->io_size = td->o.size;
2fd233b7
JA
80 td->total_io_size = td->io_size;
81
82 for_each_file(td, f, i) {
2dc1bbeb 83 f->real_file_size = td->total_io_size / td->o.nr_files;
2fd233b7
JA
84 f->file_size = f->real_file_size;
85 }
86
2fd233b7
JA
87 return 0;
88}
89
b5af8293
JA
90static int fio_null_open(struct thread_data fio_unused *td,
91 struct fio_file fio_unused *f)
92{
640e9421 93 f->fd = 0;
b5af8293
JA
94 return 0;
95}
96
65afa5f2
JA
97static void fio_null_cleanup(struct thread_data *td)
98{
99 struct null_data *nd = td->io_ops->data;
100
101 if (nd) {
102 if (nd->io_us)
103 free(nd->io_us);
104 free(nd);
105 td->io_ops->data = NULL;
106 }
107}
108
109static int fio_null_init(struct thread_data *td)
110{
111 struct null_data *nd = malloc(sizeof(*nd));
112
113 memset(nd, 0, sizeof(*nd));
114
2dc1bbeb
JA
115 if (td->o.iodepth != 1) {
116 nd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
117 memset(nd->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
65afa5f2
JA
118 } else
119 td->io_ops->flags |= FIO_SYNCIO;
120
121 td->io_ops->data = nd;
122 return 0;
123}
124
a94ea28b
JA
125static struct ioengine_ops ioengine = {
126 .name = "null",
127 .version = FIO_IOOPS_VERSION,
2fd233b7 128 .setup = fio_null_setup,
a94ea28b 129 .queue = fio_null_queue,
65afa5f2
JA
130 .commit = fio_null_commit,
131 .getevents = fio_null_getevents,
132 .event = fio_null_event,
133 .init = fio_null_init,
134 .cleanup = fio_null_cleanup,
b5af8293 135 .open_file = fio_null_open,
65afa5f2 136 .flags = FIO_DISKLESSIO,
a94ea28b
JA
137};
138
139static void fio_init fio_null_register(void)
140{
141 register_ioengine(&ioengine);
142}
143
144static void fio_exit fio_null_unregister(void)
145{
146 unregister_ioengine(&ioengine);
147}