-
Notifications
You must be signed in to change notification settings - Fork 1
/
io_async_sendfile.c
152 lines (127 loc) · 3.17 KB
/
io_async_sendfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <sys/socket.h>
#include <errno.h>
#include <inttypes.h>
#include <libaio.h>
#include <ashttpd.h>
#include <ashttpd-conn.h>
#include <ashttpd-fio.h>
#include <nbio-eventfd.h>
#include <hgang.h>
#if 1
#define dprintf printf
#else
#define dprintf(x...) do {} while(0)
#endif
#define AIO_QUEUE_SIZE 4096
static io_context_t aio_ctx;
static hgang_t aio_iocbs;
static struct nbio *efd;
static unsigned in_flight;
static int aio_submit(struct iothread *t, http_conn_t h)
{
struct iocb *iocb;
size_t data_len;
off_t data_off;
int ret, fd;
data_len = http_conn_data(h, &fd, &data_off);
assert(data_len);
iocb = hgang_alloc(aio_iocbs);
if ( NULL == iocb )
return 0;
io_prep_sendfile(iocb, fd, data_len, data_off, http_conn_socket(h));
iocb->data = h;
io_set_eventfd(iocb, efd->fd);
ret = io_submit(aio_ctx, 1, &iocb);
if ( ret <= 0 ) {
errno = -ret;
fprintf(stderr, "io_submit: %s\n", os_err());
return 0;
}
dprintf("io_submit: sendfile: %zu bytes\n", data_len);
in_flight++;
return 1;
}
static void handle_completion(struct iothread *t, struct iocb *iocb,
http_conn_t h, int ret)
{
hgang_return(aio_iocbs, iocb);
in_flight--;
if ( ret > 0 ) {
size_t data_len;
data_len = http_conn_data_read(h, ret);
if ( data_len ) {
printf("re-submit from completion\n");
if ( !aio_submit(t, h) )
http_conn_abort(t, h);
}else{
dprintf("aio_sendfile: done\n");
/* automatically removes from waitq */
http_conn_data_complete(t, h);
}
return;
}
if ( ret == -EAGAIN ) {
dprintf("aio_sendfile: failed EAGAIN\n");
http_conn_wait_on(t, h, NBIO_WRITE);
return;
}else if (ret < 0 ) {
errno = -ret;
printf("aio_sendfile: %s\n", os_err());
}
http_conn_abort(t, h);
}
static void aio_event(struct iothread *t, void *priv, eventfd_t val)
{
struct io_event ev[in_flight];
struct timespec tmo;
int ret, i;
memset(&tmo, 0, sizeof(tmo));
dprintf("aio_event ready, %"PRIu64"/%u in flight\n", val, in_flight);
ret = io_getevents(aio_ctx, 1, in_flight, ev, &tmo);
if ( ret < 0 ) {
fprintf(stderr, "io_getevents: %s\n", os_err());
return;
}
for(i = 0; i < ret; i++)
handle_completion(t, ev[i].obj, ev[i].data, ev[i].res);
}
static int io_async_sendfile_init(struct iothread *t)
{
memset(&aio_ctx, 0, sizeof(aio_ctx));
if ( io_queue_init(AIO_QUEUE_SIZE, &aio_ctx) ) {
fprintf(stderr, "io_queue_init: %s\n", os_err());
return 0;
}
aio_iocbs = hgang_new(sizeof(struct iocb), 0);
if ( NULL == aio_iocbs )
return 0;
efd = nbio_eventfd_new(0, aio_event, NULL);
if ( NULL == efd )
return 0;
nbio_eventfd_add(t, efd);
return 1;
}
static int io_async_sendfile_write(struct iothread *t, http_conn_t h)
{
dprintf("aio_sendfile: socket went writable\n");
http_conn_to_waitq(t, h, NULL);
return aio_submit(t, h);
}
static int io_async_sendfile_prep(struct iothread *t, http_conn_t h)
{
return 1;
}
static void io_async_sendfile_abort(http_conn_t h)
{
}
static void io_async_sendfile_fini(struct iothread *t)
{
}
struct http_fio fio_async_sendfile = {
.label = "Kernel AIO sendfile",
.prep = io_async_sendfile_prep,
.write = io_async_sendfile_write,
.abort = io_async_sendfile_abort,
.init = io_async_sendfile_init,
.fini = io_async_sendfile_fini,
};