annotate src/os/unix/ngx_linux_aio_read.c @ 3777:cd04f652478c

update event flags after successful io_submit()
author Igor Sysoev <igor@sysoev.ru>
date Tue, 12 Oct 2010 12:11:11 +0000
parents 04cfc09b8b8d
children 37da005a5808
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3052
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
1
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
2 /*
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
4 */
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
5
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
6
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
7 #include <ngx_config.h>
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
8 #include <ngx_core.h>
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
9 #include <ngx_event.h>
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
10
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
11
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
12 extern int ngx_eventfd;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
13 extern aio_context_t ngx_aio_ctx;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
14
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
15
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
16 static void ngx_file_aio_event_handler(ngx_event_t *ev);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
17
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
18
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
19 static long
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
20 io_submit(aio_context_t ctx, long n, struct iocb **paiocb)
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
21 {
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
22 return syscall(SYS_io_submit, ctx, n, paiocb);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
23 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
24
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
25
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
26 ssize_t
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
27 ngx_file_aio_read(ngx_file_t *file, u_char *buf, size_t size, off_t offset,
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
28 ngx_pool_t *pool)
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
29 {
3294
04cfc09b8b8d export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents: 3052
diff changeset
30 long n;
04cfc09b8b8d export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents: 3052
diff changeset
31 struct iocb *piocb[1];
04cfc09b8b8d export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents: 3052
diff changeset
32 ngx_event_t *ev;
04cfc09b8b8d export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents: 3052
diff changeset
33 ngx_event_aio_t *aio;
3052
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
34
3294
04cfc09b8b8d export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents: 3052
diff changeset
35 if (!ngx_file_aio) {
3052
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
36 return ngx_read_file(file, buf, size, offset);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
37 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
38
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
39 aio = file->aio;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
40
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
41 if (aio == NULL) {
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
42 aio = ngx_pcalloc(pool, sizeof(ngx_event_aio_t));
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
43 if (aio == NULL) {
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
44 return NGX_ERROR;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
45 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
46
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
47 aio->file = file;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
48 aio->fd = file->fd;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
49 aio->event.data = aio;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
50 aio->event.ready = 1;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
51 aio->event.log = file->log;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
52 file->aio = aio;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
53 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
54
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
55 ev = &aio->event;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
56
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
57 if (!ev->ready) {
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
58 ngx_log_error(NGX_LOG_ALERT, file->log, 0,
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
59 "second aio post for \"%V\"", &file->name);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
60 return NGX_AGAIN;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
61 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
62
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
63 ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0,
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
64 "aio complete:%d @%O:%z %V",
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
65 ev->complete, offset, size, &file->name);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
66
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
67 if (ev->complete) {
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
68 ev->active = 0;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
69 ev->complete = 0;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
70
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
71 if (aio->res >= 0) {
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
72 ngx_set_errno(0);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
73 return aio->res;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
74 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
75
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
76 ngx_set_errno(-aio->res);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
77 return NGX_ERROR;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
78 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
79
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
80 ngx_memzero(&aio->aiocb, sizeof(struct iocb));
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
81
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
82 aio->aiocb.aio_data = (uint64_t) (uintptr_t) ev;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
83 aio->aiocb.aio_lio_opcode = IOCB_CMD_PREAD;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
84 aio->aiocb.aio_fildes = file->fd;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
85 aio->aiocb.aio_buf = (uint64_t) (uintptr_t) buf;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
86 aio->aiocb.aio_nbytes = size;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
87 aio->aiocb.aio_offset = offset;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
88 aio->aiocb.aio_flags = IOCB_FLAG_RESFD;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
89 aio->aiocb.aio_resfd = ngx_eventfd;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
90
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
91 ev->handler = ngx_file_aio_event_handler;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
92
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
93 piocb[0] = &aio->aiocb;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
94
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
95 n = io_submit(ngx_aio_ctx, 1, piocb);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
96
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
97 if (n == 1) {
3777
cd04f652478c update event flags after successful io_submit()
Igor Sysoev <igor@sysoev.ru>
parents: 3294
diff changeset
98 ev->active = 1;
cd04f652478c update event flags after successful io_submit()
Igor Sysoev <igor@sysoev.ru>
parents: 3294
diff changeset
99 ev->ready = 0;
cd04f652478c update event flags after successful io_submit()
Igor Sysoev <igor@sysoev.ru>
parents: 3294
diff changeset
100 ev->complete = 0;
cd04f652478c update event flags after successful io_submit()
Igor Sysoev <igor@sysoev.ru>
parents: 3294
diff changeset
101
3052
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
102 return NGX_AGAIN;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
103 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
104
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
105 n = -n;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
106
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
107 if (n == NGX_EAGAIN) {
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
108 return ngx_read_file(file, buf, size, offset);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
109 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
110
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
111 ngx_log_error(NGX_LOG_CRIT, file->log, n,
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
112 "io_submit(\"%V\") failed", &file->name);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
113
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
114 if (n == NGX_ENOSYS) {
3294
04cfc09b8b8d export aio presence knowledge to prevent using "aio sendfile",
Igor Sysoev <igor@sysoev.ru>
parents: 3052
diff changeset
115 ngx_file_aio = 0;
3052
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
116 return ngx_read_file(file, buf, size, offset);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
117 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
118
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
119 return NGX_ERROR;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
120 }
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
121
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
122
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
123 static void
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
124 ngx_file_aio_event_handler(ngx_event_t *ev)
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
125 {
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
126 ngx_event_aio_t *aio;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
127
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
128 aio = ev->data;
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
129
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
130 ngx_log_debug2(NGX_LOG_DEBUG_CORE, ev->log, 0,
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
131 "aio event handler fd:%d %V", aio->fd, &aio->file->name);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
132
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
133 aio->handler(ev);
6060225e9261 FreeBSD and Linux AIO support
Igor Sysoev <igor@sysoev.ru>
parents:
diff changeset
134 }