Skip to content

Commit

Permalink
test/aio: a simple AIO test
Browse files Browse the repository at this point in the history
  • Loading branch information
atopia authored and chelmuth committed Sep 16, 2024
1 parent 42a5a70 commit dbc8847
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 0 deletions.
1 change: 1 addition & 0 deletions repos/gems/run/depot_autopilot.run
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ set default_test_pkgs {
test-init_loop
test-ldso
test-libc
test-libc_aio
test-libc_alarm
test-libc_connect_lwip
test-libc_connect_lxip
Expand Down
1 change: 1 addition & 0 deletions repos/libports/recipes/pkg/test-libc_aio/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Libc aio() test.
5 changes: 5 additions & 0 deletions repos/libports/recipes/pkg/test-libc_aio/archives
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_/src/init
_/src/libc
_/src/posix
_/src/test-libc_aio
_/src/vfs
24 changes: 24 additions & 0 deletions repos/libports/recipes/pkg/test-libc_aio/runtime
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<runtime ram="3M" caps="200" binary="test-libc_aio">

<requires> <timer/> </requires>

<fail after_seconds="8"/>
<succeed>--- test succeeded ---</succeed>

<content>
<rom label="ld.lib.so"/>
<rom label="libc.lib.so"/>
<rom label="libm.lib.so"/>
<rom label="posix.lib.so"/>
<rom label="test-libc_aio"/>
<rom label="vfs.lib.so"/>
</content>

<config>
<vfs> <dir name="dev"> <log/> <inline name="rtc">2019-08-20 15:01</inline> </dir> </vfs>
<libc stdout="/dev/log" stderr="/dev/log" rtc="/dev/rtc"/>
<arg value="test-libc_aio"/>
<arg value="/dev/log"/>
</config>

</runtime>
2 changes: 2 additions & 0 deletions repos/libports/recipes/src/test-libc_aio/content.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SRC_DIR := src/test/libc_aio
include $(GENODE_DIR)/repos/base/recipes/src/content.inc
1 change: 1 addition & 0 deletions repos/libports/recipes/src/test-libc_aio/hash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-09-09 a72b8ce44e2b3630ed6812cc037bab6834bf1003
2 changes: 2 additions & 0 deletions repos/libports/recipes/src/test-libc_aio/used_apis
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
libc
posix
115 changes: 115 additions & 0 deletions repos/libports/src/test/libc_aio/aio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* \brief AIO test
* \author Benjamin Lamowski
* \date 2031-07-31
*/
#include <aio.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/event.h>

int
main(int argc, char **argv)
{
/* Check argument for write test */
if (argc != 2)
err(EXIT_FAILURE, "Usage: %s path\n", argv[0]);

/* Create kqueue */
int kq = kqueue();
if (kq == -1)
err(EXIT_FAILURE, "kqueue() failed");

/*
* Write test
*/
char *write_string = "Hello log\n";

int write_fd = open(argv[1], O_WRONLY);

if (write_fd == -1)
err(EXIT_FAILURE, "Failed to open '%s'",argv[1]);

struct aiocb write_cb;
write_cb.aio_fildes = write_fd;
write_cb.aio_buf = write_string;
write_cb.aio_nbytes = strlen(write_string) + 1;
write_cb.aio_offset = 0;
write_cb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
write_cb.aio_sigevent.sigev_notify_kqueue = kq;
write_cb.aio_sigevent.sigev_notify_kevent_flags = 0;


aio_write(&write_cb);

while (aio_error(&write_cb) == EINPROGRESS) {
sleep(1);
}

int error = aio_error(&write_cb);
if (error == 0)
printf("Aio write retval: %li\n", aio_return(&write_cb));
else
err(EXIT_FAILURE, "Aio returned write error: %i\n", error);


/*
* Read test
*/
int read_fd = open("/dev/rtc", O_RDONLY);
if (read_fd == -1)
err(EXIT_FAILURE, "Failed to open '%s'", argv[1]);

struct aiocb read_cb;
char buf[10];

read_cb.aio_fildes =read_fd;
read_cb.aio_buf = buf;
read_cb.aio_nbytes = sizeof(buf);
read_cb.aio_offset = 0;
read_cb.aio_sigevent.sigev_notify = SIGEV_KEVENT;
read_cb.aio_sigevent.sigev_notify_kqueue = kq;
read_cb.aio_sigevent.sigev_notify_kevent_flags = 0;

aio_read(&read_cb);

while (aio_error(&read_cb) == EINPROGRESS) {
sleep(1);
}

error = aio_error(&read_cb);
if (error == 0)
printf("Aio write retval: %li\n", aio_return(&read_cb));
else
err(EXIT_FAILURE, "Aio returned read error: %i\n", error);

for (int i = 0; i < 2 ; i++) {
/* Event triggered */
struct kevent tevent;

/* Sleep until something happens. */
int ret = kevent(kq,NULL, 0, &tevent, 1, NULL);

if (ret == -1) {
err(EXIT_FAILURE, "kevent wait");
} else if (ret > 0) {
if (tevent.flags & EV_ERROR)
errx(EXIT_FAILURE, "Event error.\n");
else
printf("Got AIO event for %lu'\n", tevent.ident);
}
}

/* kqueues are destroyed upon close() */
(void)close(kq);
(void)close(write_fd);

printf("--- test succeeded ---\n");

return 0;
}
4 changes: 4 additions & 0 deletions repos/libports/src/test/libc_aio/target.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TARGET = test-libc_aio
SRC_C = aio.c
LIBS = posix libc
INC_DIR += $(LIBC_DIR)/sys/sys

0 comments on commit dbc8847

Please sign in to comment.