def test_skip_os(): assert not skip_os('5.1') assert skip_os('10-10.0', 'Windows') assert skip_os('15.3.0', 'Darwin') assert not skip_os('5.11', 'linux') assert not skip_os('5.11', 'LINUx') assert skip_os('5.10004', 'LINUx')
def test_openat2(tmpdir): ring = io_uring() cqes = io_uring_cqes() # prep buffers = (bytearray(b'hello world'), bytearray(11)) iov = iovec(*buffers) file_path = join(tmpdir, 'openat2_test.txt').encode() try: assert io_uring_queue_init(2, ring, 0) == 0 assert io_uring_register_buffers(ring, iov, len(iov)) == 0 # open how = open_how(O_CREAT | O_RDWR, 0o700) sqe = io_uring_get_sqe(ring) io_uring_prep_openat2(sqe, AT_FDCWD, file_path, how) fd = submit_wait_result(ring, cqes) # write sqe = io_uring_get_sqe(ring) io_uring_prep_write(sqe, fd, iov[0].iov_base, iov[0].iov_len, 0) assert submit_wait_result(ring, cqes) == 11 # read sqe = io_uring_get_sqe(ring) io_uring_prep_read(sqe, fd, iov[1].iov_base, iov[1].iov_len, 0) assert submit_wait_result(ring, cqes) == 11 # confirm assert buffers[0] == buffers[1] # close sqe = io_uring_get_sqe(ring) io_uring_prep_close(sqe, fd) submit_wait_result(ring, cqes) if skip_os('5.12'): skip('RESOLVE_CACHED 5.12+ Linux required') else: # second open for resolve test how = open_how(O_RDWR, 0, RESOLVE_CACHED) sqe = io_uring_get_sqe(ring) io_uring_prep_openat2(sqe, AT_FDCWD, file_path, how) fd2 = submit_wait_result(ring, cqes) assert fd2 > 0 # close `fd2` sqe = io_uring_get_sqe(ring) io_uring_prep_close(sqe, fd2) assert submit_wait_result(ring, cqes) == 0 finally: io_uring_queue_exit(ring)
def test_file_o_direct(): # note: # - `O_DIRECT` does not work if the file path is in memory, like "/dev/shm" or "/tmp" # - currently only `MAP_PRIVATE` works with `io_uring_register_buffers`, `MAP_SHARED` will be fixed soon! ring = io_uring() cqes = io_uring_cqes() if skip_os('5.14'): # was a bug < 5.14 read = mmap(-1, PAGESIZE, flags=MAP_PRIVATE) write = mmap(-1, PAGESIZE, flags=MAP_PRIVATE) else: read = mmap(-1, PAGESIZE) # MAP_SHARED write = mmap(-1, PAGESIZE) write[0:11] = b'hello world' iov = iovec(write, read) try: assert io_uring_queue_init(2, ring, 0) == 0 assert io_uring_register_buffers(ring, iov, len(iov)) == 0 # open - create local testing file. sqe = io_uring_get_sqe(ring) io_uring_prep_openat(sqe, AT_FDCWD, b'.', O_TMPFILE | O_RDWR | O_DIRECT, 0o700) fd = submit_wait_result(ring, cqes) # write sqe = io_uring_get_sqe(ring) io_uring_prep_write_fixed(sqe, fd, iov[0].iov_base, iov[0].iov_len, 0, 0) assert submit_wait_result(ring, cqes) == PAGESIZE # read sqe = io_uring_get_sqe(ring) io_uring_prep_read_fixed(sqe, fd, iov[1].iov_base, iov[1].iov_len, 0, 1) assert submit_wait_result(ring, cqes) == PAGESIZE # confirm assert read[:20] == write[:20] # close sqe = io_uring_get_sqe(ring) io_uring_prep_close(sqe, fd) assert submit_wait_result(ring, cqes) == 0 finally: read.close() write.close() io_uring_queue_exit(ring)
from os import O_CREAT, open, close from os.path import join from stat import S_IMODE from pytest import mark from liburing import AT_FDCWD, AT_STATX_FORCE_SYNC, STATX_MODE, io_uring_queue_init, io_uring_queue_exit, \ io_uring, io_uring_cqes, io_uring_get_sqe, statx, io_uring_prep_statx, skip_os from test_helper import submit_wait_result version = '5.6' @mark.skipif(skip_os(version), reason=f'Requires Linux {version}+') def test_statx(tmpdir): ring = io_uring() cqes = io_uring_cqes() file_path = join(tmpdir, 'statx_test.txt').encode() bad_path = join(tmpdir, 'file-that-does-not-exist').encode() # create sample file fd = open(file_path, O_CREAT, 0o700) close(fd) try: assert io_uring_queue_init(2, ring, 0) == 0 stat = statx() sqe = io_uring_get_sqe(ring) io_uring_prep_statx(sqe, AT_FDCWD, file_path, AT_STATX_FORCE_SYNC, STATX_MODE, stat) sqe.user_data = 1 assert submit_wait_result(ring, cqes) == 0
ring = io_uring() try: assert lib.io_uring_queue_init(0, ring, 0) == -22 finally: assert io_uring_queue_exit(ring) is None def test_setup_polling_io(): ring = io_uring() assert io_uring_queue_init(1, ring, IORING_SETUP_IOPOLL) == 0 assert io_uring_queue_exit(ring) is None @mark.skipif(getuid() != 0, reason='`IORING_SETUP_SQPOLL` must be run as "root" user.') def test_setup_kernel_side_polling_by_root(): ring = io_uring() assert io_uring_queue_init(1, ring, IORING_SETUP_SQPOLL) == 0 assert io_uring_queue_exit(ring) is None @mark.skipif(skip_os('5.11'), reason='`IORING_SETUP_SQPOLL` Linux version is `< 5.11`') def test_setup_kernel_side_polling_by_user(): try: ring = io_uring() assert io_uring_queue_init(1, ring, IORING_SETUP_SQPOLL) == 0 assert io_uring_queue_exit(ring) is None except PermissionError: skip('CAP_SYS_NICE not enabled!')
from os import O_CREAT, O_RDWR from os.path import join from pytest import mark, skip from liburing import AT_FDCWD, RESOLVE_CACHED, io_uring, io_uring_cqes, iovec, io_uring_queue_init, \ io_uring_get_sqe, io_uring_prep_openat2, io_uring_prep_write, io_uring_prep_read, \ io_uring_prep_close, io_uring_queue_exit, io_uring_register_buffers, \ open_how, skip_os from test_helper import submit_wait_result version = '5.6' @mark.skipif(skip_os(version), reason=f'Requires Linux {version}+') def test_openat2(tmpdir): ring = io_uring() cqes = io_uring_cqes() # prep buffers = (bytearray(b'hello world'), bytearray(11)) iov = iovec(*buffers) file_path = join(tmpdir, 'openat2_test.txt').encode() try: assert io_uring_queue_init(2, ring, 0) == 0 assert io_uring_register_buffers(ring, iov, len(iov)) == 0 # open how = open_how(O_CREAT | O_RDWR, 0o700) sqe = io_uring_get_sqe(ring) io_uring_prep_openat2(sqe, AT_FDCWD, file_path, how) fd = submit_wait_result(ring, cqes)