diff options
Diffstat (limited to 'meta-selftest')
4 files changed, 1056 insertions, 0 deletions
diff --git a/meta-selftest/recipes-test/io-uring/io-uring-writev.bb b/meta-selftest/recipes-test/io-uring/io-uring-writev.bb new file mode 100644 index 0000000000..6a08774f67 --- /dev/null +++ b/meta-selftest/recipes-test/io-uring/io-uring-writev.bb @@ -0,0 +1,25 @@ +DESCRIPTION = "Simple io_uring test" +SECTION = "examples" +LICENSE = "MIT" +LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" + +DEPENDS += "libuv-native" + +SRC_URI = "file://io-uring-writev.c \ + file://task.h \ + file://libuv-fs-copyfile.c \ +" + +S = "${WORKDIR}" + +do_compile() { + ${BUILD_CC} io-uring-writev.c -o io-uring-writev + ${BUILD_CC} -luv libuv-fs-copyfile.c -o libuv-fs-copyfile +} + +do_install() { + ${S}/io-uring-writev ${D}/test + ${S}/libuv-fs-copyfile ${S}/task.h ${D}/task-copy.h +} + +FILES:${PN} = "test test2 task-copy.h" diff --git a/meta-selftest/recipes-test/io-uring/io-uring-writev/io-uring-writev.c b/meta-selftest/recipes-test/io-uring/io-uring-writev/io-uring-writev.c new file mode 100644 index 0000000000..a5e4253b7a --- /dev/null +++ b/meta-selftest/recipes-test/io-uring/io-uring-writev/io-uring-writev.c @@ -0,0 +1,389 @@ +/* Taken from + * https://unixism.net/2020/04/io-uring-by-example-part-1-introduction/ + * with small modification to write into files instead of reading them + * to test io_uring support in pseudo (https://git.yoctoproject.org/pseudo/) + * once implemented there + * */ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <sys/ioctl.h> +#include <sys/syscall.h> +#include <sys/mman.h> +#include <sys/uio.h> +#include <linux/fs.h> +#include <fcntl.h> +#include <unistd.h> +#include <string.h> + +/* If your compilation fails because the header file below is missing, + * your kernel is probably too old to support io_uring. + * */ +#include <linux/io_uring.h> + +#define QUEUE_DEPTH 1 +#define BLOCK_SZ 1024 + +/* This is x86 specific */ +#define read_barrier() __asm__ __volatile__("":::"memory") +#define write_barrier() __asm__ __volatile__("":::"memory") + +struct app_io_sq_ring { + unsigned *head; + unsigned *tail; + unsigned *ring_mask; + unsigned *ring_entries; + unsigned *flags; + unsigned *array; +}; + +struct app_io_cq_ring { + unsigned *head; + unsigned *tail; + unsigned *ring_mask; + unsigned *ring_entries; + struct io_uring_cqe *cqes; +}; + +struct submitter { + int ring_fd; + struct app_io_sq_ring sq_ring; + struct io_uring_sqe *sqes; + struct app_io_cq_ring cq_ring; +}; + +struct file_info { + off_t file_sz; + struct iovec iovecs[]; /* Referred by readv/writev */ +}; + +/* + * This code is written in the days when io_uring-related system calls are not + * part of standard C libraries. So, we roll our own system call wrapper + * functions. + * */ + +int io_uring_setup(unsigned entries, struct io_uring_params *p) +{ + return (int) syscall(__NR_io_uring_setup, entries, p); +} + +int io_uring_enter(int ring_fd, unsigned int to_submit, + unsigned int min_complete, unsigned int flags) +{ + return (int) syscall(__NR_io_uring_enter, ring_fd, to_submit, min_complete, + flags, NULL, 0); +} + +/* + * Returns the size of the file whose open file descriptor is passed in. + * Properly handles regular file and block devices as well. Pretty. + * */ + +off_t get_file_size(int fd) { + struct stat st; + + if(fstat(fd, &st) < 0) { + perror("fstat"); + return -1; + } + if (S_ISBLK(st.st_mode)) { + unsigned long long bytes; + if (ioctl(fd, BLKGETSIZE64, &bytes) != 0) { + perror("ioctl"); + return -1; + } + return bytes; + } else if (S_ISREG(st.st_mode)) + return st.st_size; + + return -1; +} + +/* + * io_uring requires a lot of setup which looks pretty hairy, but isn't all + * that difficult to understand. Because of all this boilerplate code, + * io_uring's author has created liburing, which is relatively easy to use. + * However, you should take your time and understand this code. It is always + * good to know how it all works underneath. Apart from bragging rights, + * it does offer you a certain strange geeky peace. + * */ + +int app_setup_uring(struct submitter *s) { + struct app_io_sq_ring *sring = &s->sq_ring; + struct app_io_cq_ring *cring = &s->cq_ring; + struct io_uring_params p; + void *sq_ptr, *cq_ptr; + + /* + * We need to pass in the io_uring_params structure to the io_uring_setup() + * call zeroed out. We could set any flags if we need to, but for this + * example, we don't. + * */ + memset(&p, 0, sizeof(p)); + s->ring_fd = io_uring_setup(QUEUE_DEPTH, &p); + if (s->ring_fd < 0) { + perror("io_uring_setup"); + return 1; + } + + /* + * io_uring communication happens via 2 shared kernel-user space ring buffers, + * which can be jointly mapped with a single mmap() call in recent kernels. + * While the completion queue is directly manipulated, the submission queue + * has an indirection array in between. We map that in as well. + * */ + + int sring_sz = p.sq_off.array + p.sq_entries * sizeof(unsigned); + int cring_sz = p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe); + + /* In kernel version 5.4 and above, it is possible to map the submission and + * completion buffers with a single mmap() call. Rather than check for kernel + * versions, the recommended way is to just check the features field of the + * io_uring_params structure, which is a bit mask. If the + * IORING_FEAT_SINGLE_MMAP is set, then we can do away with the second mmap() + * call to map the completion ring. + * */ + if (p.features & IORING_FEAT_SINGLE_MMAP) { + if (cring_sz > sring_sz) { + sring_sz = cring_sz; + } + cring_sz = sring_sz; + } + + /* Map in the submission and completion queue ring buffers. + * Older kernels only map in the submission queue, though. + * */ + sq_ptr = mmap(0, sring_sz, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, + s->ring_fd, IORING_OFF_SQ_RING); + if (sq_ptr == MAP_FAILED) { + perror("mmap"); + return 1; + } + + if (p.features & IORING_FEAT_SINGLE_MMAP) { + cq_ptr = sq_ptr; + } else { + /* Map in the completion queue ring buffer in older kernels separately */ + cq_ptr = mmap(0, cring_sz, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, + s->ring_fd, IORING_OFF_CQ_RING); + if (cq_ptr == MAP_FAILED) { + perror("mmap"); + return 1; + } + } + /* Save useful fields in a global app_io_sq_ring struct for later + * easy reference */ + sring->head = sq_ptr + p.sq_off.head; + sring->tail = sq_ptr + p.sq_off.tail; + sring->ring_mask = sq_ptr + p.sq_off.ring_mask; + sring->ring_entries = sq_ptr + p.sq_off.ring_entries; + sring->flags = sq_ptr + p.sq_off.flags; + sring->array = sq_ptr + p.sq_off.array; + + /* Map in the submission queue entries array */ + s->sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe), + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, + s->ring_fd, IORING_OFF_SQES); + if (s->sqes == MAP_FAILED) { + perror("mmap"); + return 1; + } + + /* Save useful fields in a global app_io_cq_ring struct for later + * easy reference */ + cring->head = cq_ptr + p.cq_off.head; + cring->tail = cq_ptr + p.cq_off.tail; + cring->ring_mask = cq_ptr + p.cq_off.ring_mask; + cring->ring_entries = cq_ptr + p.cq_off.ring_entries; + cring->cqes = cq_ptr + p.cq_off.cqes; + + return 0; +} + +/* + * Output a string of characters of len length to stdout. + * We use buffered output here to be efficient, + * since we need to output character-by-character. + * */ +void output_to_console(char *buf, int len) { + while (len--) { + fputc(*buf++, stdout); + } +} + +/* + * Read from completion queue. + * In this function, we read completion events from the completion queue, get + * the data buffer that will have the file data and print it to the console. + * */ + +void read_from_cq(struct submitter *s) { + struct file_info *fi; + struct app_io_cq_ring *cring = &s->cq_ring; + struct io_uring_cqe *cqe; + unsigned head, reaped = 0; + + head = *cring->head; + + do { + read_barrier(); + /* + * Remember, this is a ring buffer. If head == tail, it means that the + * buffer is empty. + * */ + if (head == *cring->tail) + break; + + /* Get the entry */ + cqe = &cring->cqes[head & *s->cq_ring.ring_mask]; + fi = (struct file_info*) cqe->user_data; + if (cqe->res < 0) + fprintf(stderr, "Error: %s\n", strerror(abs(cqe->res))); + + int blocks = (int) fi->file_sz / BLOCK_SZ; + if (fi->file_sz % BLOCK_SZ) blocks++; + + for (int i = 0; i < blocks; i++) + output_to_console(fi->iovecs[i].iov_base, fi->iovecs[i].iov_len); + + head++; + } while (1); + + *cring->head = head; + write_barrier(); +} +/* + * Submit to submission queue. + * In this function, we submit requests to the submission queue. You can submit + * many types of requests. Ours is going to be the readv() request, which we + * specify via IORING_OP_READV. + * + * */ +int submit_to_sq(char *file_path, struct submitter *s) { + struct file_info *fi; + + int file_fd = open(file_path, O_WRONLY|O_CREAT, 0666); + if (file_fd < 0 ) { + perror("open"); + return 1; + } + + struct app_io_sq_ring *sring = &s->sq_ring; + unsigned index = 0, current_block = 0, tail = 0, next_tail = 0; + + char *bark = "Hello IO!"; + off_t file_sz = strlen(bark); + if (file_sz < 0) + return 1; + off_t bytes_remaining = file_sz; + int blocks = (int) file_sz / BLOCK_SZ; + if (file_sz % BLOCK_SZ) blocks++; + + fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks); + if (!fi) { + fprintf(stderr, "Unable to allocate memory\n"); + return 1; + } + fi->file_sz = file_sz; + + /* + * For each block of the file we need to read, we allocate an iovec struct + * which is indexed into the iovecs array. This array is passed in as part + * of the submission. If you don't understand this, then you need to look + * up how the readv() and writev() system calls work. + * */ + /* + while (bytes_remaining) { + off_t bytes_to_read = bytes_remaining; + if (bytes_to_read > BLOCK_SZ) + bytes_to_read = BLOCK_SZ; + + fi->iovecs[current_block].iov_len = bytes_to_read; + + void *buf; + if( posix_memalign(&buf, BLOCK_SZ, BLOCK_SZ)) { + perror("posix_memalign"); + return 1; + } + fi->iovecs[current_block].iov_base = buf; + + current_block++; + bytes_remaining -= bytes_to_read; + } + */ + fi->iovecs[current_block].iov_len = bytes_remaining; + fi->iovecs[current_block].iov_base = bark; + + + /* Add our submission queue entry to the tail of the SQE ring buffer */ + next_tail = tail = *sring->tail; + next_tail++; + read_barrier(); + index = tail & *s->sq_ring.ring_mask; + struct io_uring_sqe *sqe = &s->sqes[index]; + sqe->fd = file_fd; + sqe->flags = 0; + sqe->opcode = IORING_OP_WRITEV; + sqe->addr = (unsigned long) fi->iovecs; + sqe->len = blocks; + sqe->off = 0; + sqe->user_data = (unsigned long long) fi; + sring->array[index] = index; + tail = next_tail; + + /* Update the tail so the kernel can see it. */ + if(*sring->tail != tail) { + *sring->tail = tail; + write_barrier(); + } + + /* + * Tell the kernel we have submitted events with the io_uring_enter() system + * call. We also pass in the IOURING_ENTER_GETEVENTS flag which causes the + * io_uring_enter() call to wait until min_complete events (the 3rd param) + * complete. + * */ + int ret = io_uring_enter(s->ring_fd, 1,1, + IORING_ENTER_GETEVENTS); + if(ret < 0) { + perror("io_uring_enter"); + return 1; + } + + return 0; +} + +int main(int argc, char *argv[]) { + struct submitter *s; + + if (argc < 2) { + fprintf(stderr, "Usage: %s <filename>, barks to <filename>\n", argv[0]); + return 1; + } + + s = malloc(sizeof(*s)); + if (!s) { + perror("malloc"); + return 1; + } + memset(s, 0, sizeof(*s)); + + if(app_setup_uring(s)) { + fprintf(stderr, "Unable to setup uring!\n"); + return 1; + } + + for (int i = 1; i < argc; i++) { + if(submit_to_sq(argv[i], s)) { + fprintf(stderr, "Error writting file\n"); + return 1; + } + //read_from_cq(s); + } + + return 0; +} diff --git a/meta-selftest/recipes-test/io-uring/io-uring-writev/libuv-fs-copyfile.c b/meta-selftest/recipes-test/io-uring/io-uring-writev/libuv-fs-copyfile.c new file mode 100644 index 0000000000..28783fb60d --- /dev/null +++ b/meta-selftest/recipes-test/io-uring/io-uring-writev/libuv-fs-copyfile.c @@ -0,0 +1,258 @@ +/* + * test example from: + * https://github.com/libuv/libuv/blob/v1.x/test/test-fs-copyfile.c +*/ + +/* Copyright libuv project contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "task.h" + +#if defined(__unix__) || defined(__POSIX__) || \ + defined(__APPLE__) || defined(__sun) || \ + defined(_AIX) || defined(__MVS__) || \ + defined(__HAIKU__) || defined(__QNX__) +#include <unistd.h> /* unlink, etc. */ +#else +# include <direct.h> +# include <io.h> +# define unlink _unlink +#endif + +static const char fixture[] = "test/fixtures/load_error.node"; +//static const char dst[] = "test2"; +static const char *dst; +static int result_check_count; + + +static void fail_cb(uv_fs_t* req) { + FATAL("fail_cb should not have been called"); +} + +static void handle_result(uv_fs_t* req) { + uv_fs_t stat_req; + uint64_t size; + uint64_t mode; + int r; + + ASSERT(req->fs_type == UV_FS_COPYFILE); + ASSERT(req->result == 0); + + /* Verify that the file size and mode are the same. */ + r = uv_fs_stat(NULL, &stat_req, req->path, NULL); + ASSERT(r == 0); + size = stat_req.statbuf.st_size; + mode = stat_req.statbuf.st_mode; + uv_fs_req_cleanup(&stat_req); + r = uv_fs_stat(NULL, &stat_req, dst, NULL); + ASSERT(r == 0); + ASSERT(stat_req.statbuf.st_size == size); + ASSERT(stat_req.statbuf.st_mode == mode); + uv_fs_req_cleanup(&stat_req); + uv_fs_req_cleanup(req); + result_check_count++; +} + + +static void touch_file(const char* name, unsigned int size) { + uv_file file; + uv_fs_t req; + uv_buf_t buf; + int r; + unsigned int i; + + r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT | O_TRUNC, + S_IWUSR | S_IRUSR, NULL); + uv_fs_req_cleanup(&req); + ASSERT(r >= 0); + file = r; + + buf = uv_buf_init("a", 1); + + /* Inefficient but simple. */ + for (i = 0; i < size; i++) { + r = uv_fs_write(NULL, &req, file, &buf, 1, i, NULL); + uv_fs_req_cleanup(&req); + ASSERT(r >= 0); + } + + r = uv_fs_close(NULL, &req, file, NULL); + uv_fs_req_cleanup(&req); + ASSERT(r == 0); +} + + +TEST_IMPL(fs_copyfile) { + const char src[] = "test_file_src"; + uv_loop_t* loop; + uv_fs_t req; + int r; + + loop = uv_default_loop(); + + /* Fails with EINVAL if bad flags are passed. */ + r = uv_fs_copyfile(NULL, &req, src, dst, -1, NULL); + ASSERT(r == UV_EINVAL); + uv_fs_req_cleanup(&req); + + /* Fails with ENOENT if source does not exist. */ + unlink(src); + unlink(dst); + r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL); + ASSERT(req.result == UV_ENOENT); + ASSERT(r == UV_ENOENT); + uv_fs_req_cleanup(&req); + /* The destination should not exist. */ + r = uv_fs_stat(NULL, &req, dst, NULL); + ASSERT(r != 0); + uv_fs_req_cleanup(&req); + + /* Succeeds if src and dst files are identical. */ + touch_file(src, 12); + r = uv_fs_copyfile(NULL, &req, src, src, 0, NULL); + ASSERT(r == 0); + uv_fs_req_cleanup(&req); + /* Verify that the src file did not get truncated. */ + r = uv_fs_stat(NULL, &req, src, NULL); + ASSERT_EQ(r, 0); + ASSERT_EQ(req.statbuf.st_size, 12); + uv_fs_req_cleanup(&req); + unlink(src); + + /* Copies file synchronously. Creates new file. */ + unlink(dst); + r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL); + ASSERT(r == 0); + handle_result(&req); + + /* Copies a file of size zero. */ + unlink(dst); + touch_file(src, 0); + r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL); + ASSERT(r == 0); + handle_result(&req); + + /* Copies file synchronously. Overwrites existing file. */ + r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL); + ASSERT(r == 0); + handle_result(&req); + + /* Fails to overwrites existing file. */ + ASSERT_EQ(uv_fs_chmod(NULL, &req, dst, 0644, NULL), 0); + uv_fs_req_cleanup(&req); + r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_EXCL, NULL); + ASSERT(r == UV_EEXIST); + uv_fs_req_cleanup(&req); + + /* Truncates when an existing destination is larger than the source file. */ + ASSERT_EQ(uv_fs_chmod(NULL, &req, dst, 0644, NULL), 0); + uv_fs_req_cleanup(&req); + touch_file(src, 1); + r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL); + ASSERT_EQ(r, 0); + handle_result(&req); + + /* Copies a larger file. */ + unlink(dst); + touch_file(src, 4096 * 2); + r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL); + ASSERT(r == 0); + handle_result(&req); + unlink(src); + + /* Copies file asynchronously */ + unlink(dst); + r = uv_fs_copyfile(loop, &req, fixture, dst, 0, handle_result); + ASSERT(r == 0); + ASSERT(result_check_count == 5); + uv_run(loop, UV_RUN_DEFAULT); + ASSERT(result_check_count == 6); + /* Ensure file is user-writable (not copied from src). */ + ASSERT_EQ(uv_fs_chmod(NULL, &req, dst, 0644, NULL), 0); + uv_fs_req_cleanup(&req); + + /* If the flags are invalid, the loop should not be kept open */ + unlink(dst); + r = uv_fs_copyfile(loop, &req, fixture, dst, -1, fail_cb); + ASSERT(r == UV_EINVAL); + uv_run(loop, UV_RUN_DEFAULT); + + /* Copies file using UV_FS_COPYFILE_FICLONE. */ + unlink(dst); + r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE, NULL); + ASSERT(r == 0); + handle_result(&req); + + /* Copies file using UV_FS_COPYFILE_FICLONE_FORCE. */ + unlink(dst); + r = uv_fs_copyfile(NULL, &req, fixture, dst, UV_FS_COPYFILE_FICLONE_FORCE, + NULL); + ASSERT(r <= 0); + + if (r == 0) + handle_result(&req); + +#ifndef _WIN32 + /* Copying respects permissions/mode. */ + unlink(dst); + touch_file(dst, 0); + chmod(dst, S_IRUSR|S_IRGRP|S_IROTH); /* Sets file mode to 444 (read-only). */ + r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL); + /* On IBMi PASE, qsecofr users can overwrite read-only files */ +# ifndef __PASE__ + ASSERT(req.result == UV_EACCES); + ASSERT(r == UV_EACCES); +# endif + uv_fs_req_cleanup(&req); +#endif + + unlink(dst); /* Cleanup */ + MAKE_VALGRIND_HAPPY(loop); + return 0; +} + +int main(int argc, char *argv[]) { + uv_loop_t* loop; + uv_fs_t req; + int r; + char *src; + + if (argc < 3) { + fprintf(stderr, "Usage: %s <filename> <filename>\n", argv[0]); + return 1; + } + src = argv[1]; + dst = argv[2]; + + loop = uv_default_loop(); +/* + r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL); + ASSERT(r == 0); + uv_fs_req_cleanup(&req); +*/ + r = uv_fs_copyfile(loop, &req, src, dst, 0, handle_result); +// ASSERT(r == 0); +// ASSERT(result_check_count == 1); + uv_run(loop, UV_RUN_DEFAULT); +// ASSERT(result_check_count == 6); + uv_fs_req_cleanup(&req); +} diff --git a/meta-selftest/recipes-test/io-uring/io-uring-writev/task.h b/meta-selftest/recipes-test/io-uring/io-uring-writev/task.h new file mode 100644 index 0000000000..8b8353263d --- /dev/null +++ b/meta-selftest/recipes-test/io-uring/io-uring-writev/task.h @@ -0,0 +1,384 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#ifndef TASK_H_ +#define TASK_H_ + +#include "uv.h" + +#include <stdio.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <inttypes.h> +#include <stdint.h> + +#if !defined(_WIN32) +# include <sys/time.h> +# include <sys/resource.h> /* setrlimit() */ +#endif + +#ifdef __clang__ +# pragma clang diagnostic ignored "-Wvariadic-macros" +# pragma clang diagnostic ignored "-Wc99-extensions" +#endif + +#ifdef __GNUC__ +# pragma GCC diagnostic ignored "-Wvariadic-macros" +#endif + +#define TEST_PORT 9123 +#define TEST_PORT_2 9124 +#define TEST_PORT_3 9125 + +#ifdef _WIN32 +# define TEST_PIPENAME "\\\\.\\pipe\\uv-test" +# define TEST_PIPENAME_2 "\\\\.\\pipe\\uv-test2" +# define TEST_PIPENAME_3 "\\\\.\\pipe\\uv-test3" +#else +# define TEST_PIPENAME "/tmp/uv-test-sock" +# define TEST_PIPENAME_2 "/tmp/uv-test-sock2" +# define TEST_PIPENAME_3 "/tmp/uv-test-sock3" +#endif + +#ifdef _WIN32 +# include <io.h> +# ifndef S_IRUSR +# define S_IRUSR _S_IREAD +# endif +# ifndef S_IWUSR +# define S_IWUSR _S_IWRITE +# endif +#endif + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + +#define container_of(ptr, type, member) \ + ((type *) ((char *) (ptr) - offsetof(type, member))) + +typedef enum { + TCP = 0, + UDP, + PIPE +} stream_type; + +/* Die with fatal error. */ +#define FATAL(msg) \ + do { \ + fprintf(stderr, \ + "Fatal error in %s on line %d: %s\n", \ + __FILE__, \ + __LINE__, \ + msg); \ + fflush(stderr); \ + abort(); \ + } while (0) + +/* Have our own assert, so we are sure it does not get optimized away in + * a release build. + */ +#define ASSERT(expr) \ + do { \ + if (!(expr)) { \ + fprintf(stderr, \ + "Assertion failed in %s on line %d: %s\n", \ + __FILE__, \ + __LINE__, \ + #expr); \ + abort(); \ + } \ + } while (0) + +#define ASSERT_BASE(a, operator, b, type, conv) \ + do { \ + volatile type eval_a = (type) (a); \ + volatile type eval_b = (type) (b); \ + if (!(eval_a operator eval_b)) { \ + fprintf(stderr, \ + "Assertion failed in %s on line %d: `%s %s %s` " \ + "(%"conv" %s %"conv")\n", \ + __FILE__, \ + __LINE__, \ + #a, \ + #operator, \ + #b, \ + eval_a, \ + #operator, \ + eval_b); \ + abort(); \ + } \ + } while (0) + +#define ASSERT_BASE_STR(expr, a, operator, b, type, conv) \ + do { \ + if (!(expr)) { \ + fprintf(stderr, \ + "Assertion failed in %s on line %d: `%s %s %s` " \ + "(%"conv" %s %"conv")\n", \ + __FILE__, \ + __LINE__, \ + #a, \ + #operator, \ + #b, \ + (type)a, \ + #operator, \ + (type)b); \ + abort(); \ + } \ + } while (0) + +#define ASSERT_BASE_LEN(expr, a, operator, b, conv, len) \ + do { \ + if (!(expr)) { \ + fprintf(stderr, \ + "Assertion failed in %s on line %d: `%s %s %s` " \ + "(%.*"#conv" %s %.*"#conv")\n", \ + __FILE__, \ + __LINE__, \ + #a, \ + #operator, \ + #b, \ + (int)len, \ + a, \ + #operator, \ + (int)len, \ + b); \ + abort(); \ + } \ + } while (0) + +#define ASSERT_BASE_HEX(expr, a, operator, b, size) \ + do { \ + if (!(expr)) { \ + int i; \ + unsigned char* a_ = (unsigned char*)a; \ + unsigned char* b_ = (unsigned char*)b; \ + fprintf(stderr, \ + "Assertion failed in %s on line %d: `%s %s %s` (", \ + __FILE__, \ + __LINE__, \ + #a, \ + #operator, \ + #b); \ + for (i = 0; i < size; ++i) { \ + if (i > 0) fprintf(stderr, ":"); \ + fprintf(stderr, "%02X", a_[i]); \ + } \ + fprintf(stderr, " %s ", #operator); \ + for (i = 0; i < size; ++i) { \ + if (i > 0) fprintf(stderr, ":"); \ + fprintf(stderr, "%02X", b_[i]); \ + } \ + fprintf(stderr, ")\n"); \ + abort(); \ + } \ + } while (0) + +#define ASSERT_EQ(a, b) ASSERT_BASE(a, ==, b, int64_t, PRId64) +#define ASSERT_GE(a, b) ASSERT_BASE(a, >=, b, int64_t, PRId64) +#define ASSERT_GT(a, b) ASSERT_BASE(a, >, b, int64_t, PRId64) +#define ASSERT_LE(a, b) ASSERT_BASE(a, <=, b, int64_t, PRId64) +#define ASSERT_LT(a, b) ASSERT_BASE(a, <, b, int64_t, PRId64) +#define ASSERT_NE(a, b) ASSERT_BASE(a, !=, b, int64_t, PRId64) +#define ASSERT_OK(a) ASSERT_BASE(a, ==, 0, int64_t, PRId64) + +#define ASSERT_UINT64_EQ(a, b) ASSERT_BASE(a, ==, b, uint64_t, PRIu64) +#define ASSERT_UINT64_GE(a, b) ASSERT_BASE(a, >=, b, uint64_t, PRIu64) +#define ASSERT_UINT64_GT(a, b) ASSERT_BASE(a, >, b, uint64_t, PRIu64) +#define ASSERT_UINT64_LE(a, b) ASSERT_BASE(a, <=, b, uint64_t, PRIu64) +#define ASSERT_UINT64_LT(a, b) ASSERT_BASE(a, <, b, uint64_t, PRIu64) +#define ASSERT_UINT64_NE(a, b) ASSERT_BASE(a, !=, b, uint64_t, PRIu64) + +#define ASSERT_DOUBLE_EQ(a, b) ASSERT_BASE(a, ==, b, double, "f") +#define ASSERT_DOUBLE_GE(a, b) ASSERT_BASE(a, >=, b, double, "f") +#define ASSERT_DOUBLE_GT(a, b) ASSERT_BASE(a, >, b, double, "f") +#define ASSERT_DOUBLE_LE(a, b) ASSERT_BASE(a, <=, b, double, "f") +#define ASSERT_DOUBLE_LT(a, b) ASSERT_BASE(a, <, b, double, "f") +#define ASSERT_DOUBLE_NE(a, b) ASSERT_BASE(a, !=, b, double, "f") + +#define ASSERT_STR_EQ(a, b) \ + ASSERT_BASE_STR(strcmp(a, b) == 0, a, == , b, char*, "s") + +#define ASSERT_STR_NE(a, b) \ + ASSERT_BASE_STR(strcmp(a, b) != 0, a, !=, b, char*, "s") + +#define ASSERT_MEM_EQ(a, b, size) \ + ASSERT_BASE_LEN(memcmp(a, b, size) == 0, a, ==, b, s, size) + +#define ASSERT_MEM_NE(a, b, size) \ + ASSERT_BASE_LEN(memcmp(a, b, size) != 0, a, !=, b, s, size) + +#define ASSERT_MEM_HEX_EQ(a, b, size) \ + ASSERT_BASE_HEX(memcmp(a, b, size) == 0, a, ==, b, size) + +#define ASSERT_MEM_HEX_NE(a, b, size) \ + ASSERT_BASE_HEX(memcmp(a, b, size) != 0, a, !=, b, size) + +#define ASSERT_NULL(a) \ + ASSERT_BASE(a, ==, NULL, void*, "p") + +#define ASSERT_NOT_NULL(a) \ + ASSERT_BASE(a, !=, NULL, void*, "p") + +#define ASSERT_PTR_EQ(a, b) \ + ASSERT_BASE(a, ==, b, void*, "p") + +#define ASSERT_PTR_NE(a, b) \ + ASSERT_BASE(a, !=, b, void*, "p") + +#define ASSERT_PTR_LT(a, b) \ + ASSERT_BASE(a, <, b, void*, "p") + +/* This macro cleans up the event loop. This is used to avoid valgrind + * warnings about memory being "leaked" by the event loop. + */ +#define MAKE_VALGRIND_HAPPY(loop) \ + do { \ + close_loop(loop); \ + ASSERT_EQ(0, uv_loop_close(loop)); \ + uv_library_shutdown(); \ + } while (0) + +/* Just sugar for wrapping the main() for a task or helper. */ +#define TEST_IMPL(name) \ + int run_test_##name(void); \ + int run_test_##name(void) + +#define BENCHMARK_IMPL(name) \ + int run_benchmark_##name(void); \ + int run_benchmark_##name(void) + +#define HELPER_IMPL(name) \ + int run_helper_##name(void); \ + int run_helper_##name(void) + +/* Format big numbers nicely. */ +char* fmt(char (*buf)[32], double d); + +/* Reserved test exit codes. */ +enum test_status { + TEST_OK = 0, + TEST_SKIP = 7 +}; + +#define RETURN_OK() \ + do { \ + return TEST_OK; \ + } while (0) + +#define RETURN_SKIP(explanation) \ + do { \ + fprintf(stderr, "%s\n", explanation); \ + fflush(stderr); \ + return TEST_SKIP; \ + } while (0) + +#if !defined(_WIN32) + +# define TEST_FILE_LIMIT(num) \ + do { \ + struct rlimit lim; \ + lim.rlim_cur = (num); \ + lim.rlim_max = lim.rlim_cur; \ + if (setrlimit(RLIMIT_NOFILE, &lim)) \ + RETURN_SKIP("File descriptor limit too low."); \ + } while (0) + +#else /* defined(_WIN32) */ + +# define TEST_FILE_LIMIT(num) do {} while (0) + +#endif + +#if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900 +extern int snprintf(char*, size_t, const char*, ...); +#endif + +#if defined(__clang__) || \ + defined(__GNUC__) || \ + defined(__INTEL_COMPILER) +# define UNUSED __attribute__((unused)) +#else +# define UNUSED +#endif + +#if defined(_WIN32) +#define notify_parent_process() ((void) 0) +#else +extern void notify_parent_process(void); +#endif + +/* Fully close a loop */ +static void close_walk_cb(uv_handle_t* handle, void* arg) { + if (!uv_is_closing(handle)) + uv_close(handle, NULL); +} + +UNUSED static void close_loop(uv_loop_t* loop) { + uv_walk(loop, close_walk_cb, NULL); + uv_run(loop, UV_RUN_DEFAULT); +} + +UNUSED static int can_ipv6(void) { + uv_interface_address_t* addr; + int supported; + int count; + int i; + + if (uv_interface_addresses(&addr, &count)) + return 0; /* Assume no IPv6 support on failure. */ + + supported = 0; + for (i = 0; supported == 0 && i < count; i += 1) + supported = (AF_INET6 == addr[i].address.address6.sin6_family); + + uv_free_interface_addresses(addr, count); + return supported; +} + +#if defined(__CYGWIN__) || defined(__MSYS__) || defined(__PASE__) +# define NO_FS_EVENTS "Filesystem watching not supported on this platform." +#endif + +#if defined(__MSYS__) +# define NO_SEND_HANDLE_ON_PIPE \ + "MSYS2 runtime does not support sending handles on pipes." +#elif defined(__CYGWIN__) +# define NO_SEND_HANDLE_ON_PIPE \ + "Cygwin runtime does not support sending handles on pipes." +#endif + +#if defined(__MSYS__) +# define NO_SELF_CONNECT \ + "MSYS2 runtime hangs on listen+connect in same process." +#elif defined(__CYGWIN__) +# define NO_SELF_CONNECT \ + "Cygwin runtime hangs on listen+connect in same process." +#endif + +#if !defined(__linux__) && \ + !(defined(__FreeBSD__) && __FreeBSD_version >= 1301000) && \ + !defined(_WIN32) +# define NO_CPU_AFFINITY \ + "affinity not supported on this platform." +#endif + +#endif /* TASK_H_ */ |