[Ocfs2-test-devel] [PATCH 2/4] Ocfs2-test: Make aio operations become public from libocfs2test.
Tristan Ye
tristan.ye at oracle.com
Wed Jun 23 04:06:47 PDT 2010
Capsulation of all aio operations into libocfs2test provides flexsibility
and readability of testcases performing aio-tests, as in, it becomes more
convenient when we migrate from one aio library to another.
Signed-off-by: Tristan Ye <tristan.ye at oracle.com>
---
programs/libocfs2test/Makefile | 4 +-
programs/libocfs2test/aio.c | 157 ++++++++++++++++++++++++++++++++++++++++
programs/libocfs2test/aio.h | 38 ++++++++++
3 files changed, 198 insertions(+), 1 deletions(-)
create mode 100644 programs/libocfs2test/aio.c
create mode 100644 programs/libocfs2test/aio.h
diff --git a/programs/libocfs2test/Makefile b/programs/libocfs2test/Makefile
index 8c45567..aaaa16a 100644
--- a/programs/libocfs2test/Makefile
+++ b/programs/libocfs2test/Makefile
@@ -10,6 +10,7 @@ CFILES = \
dir_ops.c \
xattr_ops.c \
mpi_ops.c \
+ aio.c
ifdef OCFS2_TEST_REFLINK
CFILES += file_ops.c
@@ -18,7 +19,8 @@ endif
HFILES = \
dir_ops.h \
xattr_ops.h \
- mpi_ops.h
+ mpi_ops.h \
+ aio.h
ifdef OCFS2_TEST_REFLINK
HFILES += file_ops.h
diff --git a/programs/libocfs2test/aio.c b/programs/libocfs2test/aio.c
new file mode 100644
index 0000000..c1be591
--- /dev/null
+++ b/programs/libocfs2test/aio.c
@@ -0,0 +1,157 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * aio.c
+ *
+ * Provide generic utility fuctions on aio operations for ocfs2-tests
+ *
+ * Written by tristan.ye at oracle.com
+ *
+ * Copyright (C) 2010 Oracle. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+#define _GNU_SOURCE
+#define _XOPEN_SOURCE 500
+#define _LARGEFILE64_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <sys/ioctl.h>
+
+#include "aio.h"
+
+int o2test_aio_setup(struct o2test_aio *o2a, int nr_events)
+{
+ int ret = 0, i;
+
+ o2a->o2a_ctx = NULL;
+ o2a->o2a_nr_events = nr_events;
+ o2a->o2a_iocbs = (struct iocb **)malloc(sizeof(struct iocb *) *
+ nr_events);
+
+ for (i = 0; i < nr_events; i++)
+ o2a->o2a_iocbs[i] = (struct iocb *)malloc(sizeof(struct iocb));
+
+ ret = io_setup(nr_events, &(o2a->o2a_ctx));
+ if (ret) {
+ ret = errno;
+ fprintf(stderr, "error %s during %s\n", strerror(errno),
+ "io_setup");
+ ret = -1;
+ }
+
+ o2a->o2a_cr_event = 0;
+
+ return ret;
+}
+
+int o2test_aio_pwrite(struct o2test_aio *o2a, int fd, void *buf, size_t count,
+ off_t offset)
+{
+ int ret = 0;
+ struct iocb *iocbs[] = { o2a->o2a_iocbs[o2a->o2a_cr_event] };
+
+ if (o2a->o2a_cr_event >= o2a->o2a_nr_events) {
+ fprintf(stderr, "current aio context didn't support %d "
+ "requests\n", o2a->o2a_cr_event);
+ return -1;
+ }
+
+ io_prep_pwrite(o2a->o2a_iocbs[o2a->o2a_cr_event], fd, buf,
+ count, offset);
+ ret = io_submit(o2a->o2a_ctx, 1, iocbs);
+ if (ret != 1) {
+ ret = errno;
+ fprintf(stderr, "error %s during %s\n", strerror(errno),
+ "io_submit");
+ ret = -1;
+ }
+
+ o2a->o2a_cr_event++;
+
+ return ret;
+}
+
+int o2test_aio_pread(struct o2test_aio *o2a, int fd, void *buf, size_t count,
+ off_t offset)
+{
+ int ret = 0;
+ struct iocb *iocbs[] = { o2a->o2a_iocbs[o2a->o2a_cr_event] };
+
+ if (o2a->o2a_cr_event >= o2a->o2a_nr_events) {
+ fprintf(stderr, "current aio context didn't support %d "
+ "requests\n", o2a->o2a_cr_event);
+ return -1;
+ }
+
+ io_prep_pread(o2a->o2a_iocbs[o2a->o2a_cr_event], fd, buf,
+ count, offset);
+ ret = io_submit(o2a->o2a_ctx, 1, iocbs);
+ if (ret != 1) {
+ ret = errno;
+ fprintf(stderr, "error %s during %s\n", strerror(errno),
+ "io_submit");
+ ret = -1;
+ }
+
+ o2a->o2a_cr_event++;
+
+ return ret;
+}
+
+int o2test_aio_query(struct o2test_aio *o2a, long min_nr, long nr)
+{
+ int ret = 0;
+ struct io_event ev;
+
+ ret = io_getevents(o2a->o2a_ctx, min_nr, nr, &ev, NULL);
+ if (ret < min_nr) {
+ ret = errno;
+ fprintf(stderr, "error %s during %s\n", strerror(errno),
+ "io_getevents");
+ ret = -1;
+ }
+
+ return ret;
+}
+
+int o2test_aio_destroy(struct o2test_aio *o2a)
+{
+ int ret = 0, i;
+
+ ret = io_destroy(o2a->o2a_ctx);
+ if (ret) {
+ ret = errno;
+ fprintf(stderr, "error %s during %s\n", strerror(errno),
+ "io_destroy");
+ ret = -1;
+ }
+
+ for (i = 0; i < o2a->o2a_nr_events; i++)
+ if (o2a->o2a_iocbs[i])
+ free(o2a->o2a_iocbs[i]);
+
+ if (o2a->o2a_iocbs)
+ free(o2a->o2a_iocbs);
+
+ o2a->o2a_ctx = NULL;
+ o2a->o2a_nr_events = 0;
+ o2a->o2a_cr_event = 0;
+
+ return ret;
+}
diff --git a/programs/libocfs2test/aio.h b/programs/libocfs2test/aio.h
new file mode 100644
index 0000000..f7a1724
--- /dev/null
+++ b/programs/libocfs2test/aio.h
@@ -0,0 +1,38 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * aio.h
+ *
+ * Copyright (C) 2010 Oracle. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+#ifndef AIO_H
+#define AIO_H
+
+#include <libaio.h>
+
+struct o2test_aio {
+ struct io_context *o2a_ctx;
+ int o2a_nr_events;
+ int o2a_cr_event;
+ struct iocb **o2a_iocbs;
+};
+
+int o2test_aio_setup(struct o2test_aio *o2a, int nr_events);
+int o2test_aio_pwrite(struct o2test_aio *o2a, int fd, void *buf, size_t count,
+ off_t offset);
+int o2test_aio_pread(struct o2test_aio *o2a, int fd, void *buf, size_t count,
+ off_t offset);
+int o2test_aio_query(struct o2test_aio *o2a, long min_nr, long nr);
+int o2test_aio_destroy(struct o2test_aio *o2a);
+
+#endif
--
1.5.5
More information about the Ocfs2-test-devel
mailing list