Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/mmap_libaio.c
48529 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2018 Canonical. All rights reserved.24*/2526#include <stdio.h>27#include <stdlib.h>28#include <unistd.h>29#include <fcntl.h>30#include <sys/mman.h>31#include <sys/types.h>32#include <sys/stat.h>33#include <libaio.h>34#include <err.h>3536static io_context_t io_ctx;3738static void39do_sync_io(struct iocb *iocb)40{41struct io_event event;42struct iocb *iocbs[] = { iocb };43struct timespec ts = { 30, 0 };4445if (io_submit(io_ctx, 1, iocbs) != 1)46err(1, "io_submit failed");4748if (io_getevents(io_ctx, 0, 1, &event, &ts) != 1)49err(1, "io_getevents failed");50}5152int53main(int argc, char **argv)54{55(void) argc;56char *buf;57int page_size = getpagesize();58int buf_size = strtol(argv[2], NULL, 0);59int rwfd;60struct iocb iocb;6162if (io_queue_init(1024, &io_ctx))63err(1, "io_queue_init failed");6465rwfd = open(argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);66if (rwfd < 0)67err(1, "open failed");6869if (ftruncate(rwfd, buf_size) < 0)70err(1, "ftruncate failed");7172buf = mmap(0, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, rwfd, 0);73if (buf == MAP_FAILED)74err(1, "mmap failed");7576(void) io_prep_pwrite(&iocb, rwfd, buf, buf_size, 0);77do_sync_io(&iocb);7879(void) io_prep_pread(&iocb, rwfd, buf, buf_size, 0);80do_sync_io(&iocb);8182if (close(rwfd))83err(1, "close failed");8485if (io_queue_release(io_ctx) != 0)86err(1, "io_queue_release failed");8788return (0);89}909192