summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhiqiang Liu <liuzhiqiang26@huawei.com>2021-02-23 19:15:35 +0800
committerZhiqiang Liu <liuzhiqiang26@huawei.com>2021-02-23 20:10:26 +0800
commitfa7ba190db8eb4bbbad9118a80b782c20bbbc6ca (patch)
treeb24a80bd27b5718cfb1f037e65fee28c2946ce1e
parentb7b089a671b9d4f2d453bf6f3ca106c5bb9d5d70 (diff)
downloadliburing-fa7ba190db8eb4bbbad9118a80b782c20bbbc6ca.tar.gz
liburing-fa7ba190db8eb4bbbad9118a80b782c20bbbc6ca.tar.bz2
helpers: add io_uring_create_file() helper
cleanup: add io_uring_create_file() helper, and replace create_file() with io_uring_calloc() in tests. Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
-rw-r--r--test/cq-overflow.c25
-rw-r--r--test/d4ae271dfaae-test.c25
-rw-r--r--test/fadvise.c25
-rw-r--r--test/fsync.c24
-rw-r--r--test/helpers.c25
-rw-r--r--test/helpers.h6
-rw-r--r--test/io-cancel.c25
-rw-r--r--test/iopoll.c24
-rw-r--r--test/madvise.c25
-rw-r--r--test/open-close.c31
-rw-r--r--test/openat2.c31
-rw-r--r--test/read-write.c24
-rw-r--r--test/short-read.c24
-rw-r--r--test/sq-poll-dup.c24
-rw-r--r--test/sq-poll-share.c24
-rw-r--r--test/statx.c24
-rw-r--r--test/submit-reuse.c32
-rw-r--r--test/thread-exit.c26
18 files changed, 59 insertions, 385 deletions
diff --git a/test/cq-overflow.c b/test/cq-overflow.c
index 0303966..a22da6b 100644
--- a/test/cq-overflow.c
+++ b/test/cq-overflow.c
@@ -32,25 +32,6 @@ static int create_buffers(void)
return 0;
}
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- close(fd);
- return ret != FILE_SIZE;
-}
-
#define ENTRIES 8
static int test_io(const char *file, unsigned long usecs, unsigned *drops, int fault)
@@ -492,10 +473,8 @@ int main(int argc, char *argv[])
return ret;
}
- if (create_file(".basic-rw")) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
+ io_uring_create_file(".basic-rw", FILE_SIZE);
+
if (create_buffers()) {
fprintf(stderr, "file creation failed\n");
goto err;
diff --git a/test/d4ae271dfaae-test.c b/test/d4ae271dfaae-test.c
index 98b43cc..06ec268 100644
--- a/test/d4ae271dfaae-test.c
+++ b/test/d4ae271dfaae-test.c
@@ -16,26 +16,6 @@
#define FILE_SIZE (128 * 1024)
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- fsync(fd);
- close(fd);
- return ret != FILE_SIZE;
-}
-
int main(int argc, char *argv[])
{
struct io_uring ring;
@@ -64,10 +44,7 @@ int main(int argc, char *argv[])
fname = argv[1];
} else {
fname = ".sqpoll.tmp";
- if (create_file(fname)) {
- fprintf(stderr, "file creation failed\n");
- goto out;
- }
+ io_uring_create_file(fname, FILE_SIZE);
}
fd = open(fname, O_RDONLY | O_DIRECT);
diff --git a/test/fadvise.c b/test/fadvise.c
index 6bf58ef..69dc985 100644
--- a/test/fadvise.c
+++ b/test/fadvise.c
@@ -42,26 +42,6 @@ static unsigned long long utime_since_now(struct timeval *tv)
return utime_since(tv, &end);
}
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- fsync(fd);
- close(fd);
- return ret != FILE_SIZE;
-}
-
static int do_fadvise(struct io_uring *ring, int fd, off_t offset, off_t len,
int advice)
{
@@ -184,10 +164,7 @@ int main(int argc, char *argv[])
fname = argv[1];
} else {
fname = ".fadvise.tmp";
- if (create_file(".fadvise.tmp")) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
+ io_uring_create_file(fname, FILE_SIZE);
}
if (io_uring_queue_init(8, &ring, 0)) {
fprintf(stderr, "ring creation failed\n");
diff --git a/test/fsync.c b/test/fsync.c
index 1cd4f8d..5aa5a83 100644
--- a/test/fsync.c
+++ b/test/fsync.c
@@ -138,35 +138,13 @@ err:
#define FILE_SIZE 1024
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- close(fd);
- return ret != FILE_SIZE;
-}
-
static int test_sync_file_range(struct io_uring *ring)
{
int ret, fd, save_errno;
struct io_uring_sqe *sqe;
struct io_uring_cqe *cqe;
- if (create_file(".sync_file_range")) {
- fprintf(stderr, "file creation failed\n");
- return 1;
- }
+ io_uring_create_file(".sync_file_range", FILE_SIZE);
fd = open(".sync_file_range", O_RDWR);
save_errno = errno;
diff --git a/test/helpers.c b/test/helpers.c
index af6d936..c5a82fa 100644
--- a/test/helpers.c
+++ b/test/helpers.c
@@ -4,6 +4,10 @@
*/
#include <stdlib.h>
#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
#include "helpers.h"
@@ -41,4 +45,25 @@ void *io_uring_calloc(size_t nmemb, size_t size)
return ret;
}
+/*
+ * Helper for creating file and write @size byte buf with 0xaa value in the file.
+ */
+void io_uring_create_file(const char *file, size_t size)
+{
+ ssize_t ret;
+ char *buf;
+ int fd;
+
+ buf = io_uring_malloc(size);
+ memset(buf, 0xaa, size);
+
+ fd = open(file, O_WRONLY | O_CREAT, 0644);
+ assert(fd >= 0);
+
+ ret = write(fd, buf, size);
+ fsync(fd);
+ close(fd);
+ free(buf);
+ assert(ret == size);
+}
diff --git a/test/helpers.h b/test/helpers.h
index dc192d4..4cc357f 100644
--- a/test/helpers.h
+++ b/test/helpers.h
@@ -27,6 +27,12 @@ void io_uring_posix_memalign(void **memptr, size_t alignment, size_t size);
*/
void *io_uring_calloc(size_t nmemb, size_t size);
+
+/*
+ * Helper for creating file and write @size byte buf with 0xaa value in the file.
+ */
+void io_uring_create_file(const char *file, size_t size);
+
#ifdef __cplusplus
}
#endif
diff --git a/test/io-cancel.c b/test/io-cancel.c
index 4dfcef0..25bac7a 100644
--- a/test/io-cancel.c
+++ b/test/io-cancel.c
@@ -33,25 +33,6 @@ static int create_buffers(void)
return 0;
}
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- close(fd);
- return ret != FILE_SIZE;
-}
-
static unsigned long long utime_since(const struct timeval *s,
const struct timeval *e)
{
@@ -235,10 +216,8 @@ int main(int argc, char *argv[])
if (argc > 1)
return 0;
- if (create_file(".basic-rw")) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
+ io_uring_create_file(".basic-rw", FILE_SIZE);
+
if (create_buffers()) {
fprintf(stderr, "file creation failed\n");
goto err;
diff --git a/test/iopoll.c b/test/iopoll.c
index 9a27dac..b1dafd0 100644
--- a/test/iopoll.c
+++ b/test/iopoll.c
@@ -37,25 +37,6 @@ static int create_buffers(void)
return 0;
}
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- close(fd);
- return ret != FILE_SIZE;
-}
-
static int provide_buffers(struct io_uring *ring)
{
struct io_uring_sqe *sqe;
@@ -366,10 +347,7 @@ int main(int argc, char *argv[])
fname = argv[1];
} else {
fname = ".iopoll-rw";
- if (create_file(".iopoll-rw")) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
+ io_uring_create_file(fname, FILE_SIZE);
}
if (create_buffers()) {
diff --git a/test/madvise.c b/test/madvise.c
index 2a10779..c06465f 100644
--- a/test/madvise.c
+++ b/test/madvise.c
@@ -44,26 +44,6 @@ static unsigned long long utime_since_now(struct timeval *tv)
return utime_since(tv, &end);
}
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- fsync(fd);
- close(fd);
- return ret != FILE_SIZE;
-}
-
static int do_madvise(struct io_uring *ring, void *addr, off_t len, int advice)
{
struct io_uring_sqe *sqe;
@@ -179,10 +159,7 @@ int main(int argc, char *argv[])
fname = argv[1];
} else {
fname = ".madvise.tmp";
- if (create_file(".madvise.tmp")) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
+ io_uring_create_file(fname, FILE_SIZE);
}
if (io_uring_queue_init(8, &ring, 0)) {
diff --git a/test/open-close.c b/test/open-close.c
index 59c6d3a..6d44c6a 100644
--- a/test/open-close.c
+++ b/test/open-close.c
@@ -13,25 +13,6 @@
#include "helpers.h"
#include "liburing.h"
-static int create_file(const char *file, size_t size)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(size);
- memset(buf, 0xaa, size);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, size);
- close(fd);
- return ret != size;
-}
-
static int test_close(struct io_uring *ring, int fd, int is_ring_fd)
{
struct io_uring_cqe *cqe;
@@ -119,14 +100,10 @@ int main(int argc, char *argv[])
do_unlink = 1;
}
- if (create_file(path, 4096)) {
- fprintf(stderr, "file create failed\n");
- return 1;
- }
- if (do_unlink && create_file(path_rel, 4096)) {
- fprintf(stderr, "file create failed\n");
- return 1;
- }
+ io_uring_create_file(path, 4096);
+
+ if (do_unlink)
+ io_uring_create_file(path_rel, 4096);
ret = test_openat(&ring, path, -1);
if (ret < 0) {
diff --git a/test/openat2.c b/test/openat2.c
index 7994804..750a2b4 100644
--- a/test/openat2.c
+++ b/test/openat2.c
@@ -13,25 +13,6 @@
#include "helpers.h"
#include "liburing.h"
-static int create_file(const char *file, size_t size)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(size);
- memset(buf, 0xaa, size);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, size);
- close(fd);
- return ret != size;
-}
-
static int test_openat2(struct io_uring *ring, const char *path, int dfd)
{
struct io_uring_cqe *cqe;
@@ -88,14 +69,10 @@ int main(int argc, char *argv[])
do_unlink = 1;
}
- if (create_file(path, 4096)) {
- fprintf(stderr, "file create failed\n");
- return 1;
- }
- if (do_unlink && create_file(path_rel, 4096)) {
- fprintf(stderr, "file create failed\n");
- return 1;
- }
+ io_uring_create_file(path, 4096);
+
+ if (do_unlink)
+ io_uring_create_file(path_rel, 4096);
ret = test_openat2(&ring, path, -1);
if (ret < 0) {
diff --git a/test/read-write.c b/test/read-write.c
index cb15c26..bd6df37 100644
--- a/test/read-write.c
+++ b/test/read-write.c
@@ -55,25 +55,6 @@ static int create_nonaligned_buffers(void)
return 0;
}
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- close(fd);
- return ret != FILE_SIZE;
-}
-
static int __test_io(const char *file, struct io_uring *ring, int write,
int buffered, int sqthread, int fixed, int nonvec,
int buf_select, int seq, int exp_len)
@@ -784,10 +765,7 @@ int main(int argc, char *argv[])
fname = argv[1];
} else {
fname = ".basic-rw";
- if (create_file(fname)) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
+ io_uring_create_file(fname, FILE_SIZE);
}
if (create_buffers()) {
diff --git a/test/short-read.c b/test/short-read.c
index 4d84937..c839799 100644
--- a/test/short-read.c
+++ b/test/short-read.c
@@ -15,25 +15,6 @@
#define BUF_SIZE 4096
#define FILE_SIZE 1024
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- close(fd);
- return ret != FILE_SIZE;
-}
-
int main(int argc, char *argv[])
{
int ret, fd, save_errno;
@@ -48,10 +29,7 @@ int main(int argc, char *argv[])
vec.iov_base = io_uring_malloc(BUF_SIZE);
vec.iov_len = BUF_SIZE;
- if (create_file(".short-read")) {
- fprintf(stderr, "file creation failed\n");
- return 1;
- }
+ io_uring_create_file(".short-read", FILE_SIZE);
fd = open(".short-read", O_RDONLY);
save_errno = errno;
diff --git a/test/sq-poll-dup.c b/test/sq-poll-dup.c
index cda0b5f..18dae3f 100644
--- a/test/sq-poll-dup.c
+++ b/test/sq-poll-dup.c
@@ -39,25 +39,6 @@ static int create_buffers(void)
return 0;
}
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- close(fd);
- return ret != FILE_SIZE;
-}
-
static int wait_io(struct io_uring *ring, int nr_ios)
{
struct io_uring_cqe *cqe;
@@ -193,10 +174,7 @@ int main(int argc, char *argv[])
fname = argv[1];
} else {
fname = ".basic-rw";
- if (create_file(fname)) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
+ io_uring_create_file(fname, FILE_SIZE);
}
if (create_buffers()) {
diff --git a/test/sq-poll-share.c b/test/sq-poll-share.c
index f74278e..d2e82a0 100644
--- a/test/sq-poll-share.c
+++ b/test/sq-poll-share.c
@@ -37,25 +37,6 @@ static int create_buffers(void)
return 0;
}
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- close(fd);
- return ret != FILE_SIZE;
-}
-
static int wait_io(struct io_uring *ring, int nr_ios)
{
struct io_uring_cqe *cqe;
@@ -115,10 +96,7 @@ int main(int argc, char *argv[])
fname = argv[1];
} else {
fname = ".basic-rw";
- if (create_file(fname)) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
+ io_uring_create_file(fname, FILE_SIZE);
}
if (create_buffers()) {
diff --git a/test/statx.c b/test/statx.c
index abbc4cb..bd8b5a3 100644
--- a/test/statx.c
+++ b/test/statx.c
@@ -31,25 +31,6 @@ static int do_statx(int dfd, const char *path, int flags, unsigned mask,
}
#endif
-static int create_file(const char *file, size_t size)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(size);
- memset(buf, 0xaa, size);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, size);
- close(fd);
- return ret != size;
-}
-
static int statx_syscall_supported(void)
{
return errno == ENOSYS ? 0 : -1;
@@ -162,10 +143,7 @@ int main(int argc, char *argv[])
fname = argv[1];
} else {
fname = "/tmp/.statx";
- if (create_file(fname, 4096)) {
- fprintf(stderr, "file create failed\n");
- return 1;
- }
+ io_uring_create_file(fname, 4096);
}
ret = test_statx(&ring, fname);
diff --git a/test/submit-reuse.c b/test/submit-reuse.c
index 35109fb..3c09d8d 100644
--- a/test/submit-reuse.c
+++ b/test/submit-reuse.c
@@ -38,26 +38,6 @@ static void *flusher(void *__data)
return NULL;
}
-static int create_file(const char *file)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(FILE_SIZE);
- memset(buf, 0xaa, FILE_SIZE);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, FILE_SIZE);
- fsync(fd);
- close(fd);
- return ret != FILE_SIZE;
-}
-
static char str1[STR_SIZE];
static char str2[STR_SIZE];
@@ -177,14 +157,10 @@ static int test_reuse(int argc, char *argv[], int split, int async)
return 0;
}
- if (do_unlink && create_file(fname1)) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
- if (create_file(".reuse.2")) {
- fprintf(stderr, "file creation failed\n");
- goto err;
- }
+ if (do_unlink)
+ io_uring_create_file(fname1, FILE_SIZE);
+
+ io_uring_create_file(".reuse.2", FILE_SIZE);
fd1 = open(fname1, O_RDONLY);
if (fd1 < 0) {
diff --git a/test/thread-exit.c b/test/thread-exit.c
index 2f15aa7..dc9bfaf 100644
--- a/test/thread-exit.c
+++ b/test/thread-exit.c
@@ -20,26 +20,6 @@
#define NR_IOS 8
#define WSIZE 512
-static int create_file(const char *file, size_t size)
-{
- ssize_t ret;
- char *buf;
- int fd;
-
- buf = io_uring_malloc(size);
- memset(buf, 0xaa, size);
-
- fd = open(file, O_WRONLY | O_CREAT, 0644);
- if (fd < 0) {
- perror("open file");
- return 1;
- }
- ret = write(fd, buf, size);
- close(fd);
- free(buf);
- return ret != size;
-}
-
struct d {
int fd;
struct io_uring *ring;
@@ -108,10 +88,8 @@ int main(int argc, char *argv[])
do_unlink = 1;
}
- if (do_unlink && create_file(fname, 4096)) {
- fprintf(stderr, "file create failed\n");
- return 1;
- }
+ if (do_unlink)
+ io_uring_create_file(fname, 4096);
fd = open(fname, O_WRONLY);
if (fd < 0) {