comparison src/os/unix/ngx_freebsd_sendfile_chain.c @ 7985:ec2e6893caaa

Simplified sendfile(SF_NODISKIO) usage. Starting with FreeBSD 11, there is no need to use AIO operations to preload data into cache for sendfile(SF_NODISKIO) to work. Instead, sendfile() handles non-blocking loading data from disk by itself. It still can, however, return EBUSY if a page is already being loaded (for example, by a different process). If this happens, we now post an event for the next event loop iteration, so sendfile() is retried "after a short period", as manpage recommends. The limit of the number of EBUSY tolerated without any progress is preserved, but now it does not result in an alert, since on an idle system event loop iteration might be very short and EBUSY can happen many times in a row. Instead, SF_NODISKIO is simply disabled for one call once the limit is reached. With this change, sendfile(SF_NODISKIO) is now used automatically as long as sendfile() is enabled, and no longer requires "aio on;".
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 27 Dec 2021 19:48:33 +0300
parents 555533169506
children b002ad258f1d
comparison
equal deleted inserted replaced
7984:ae992b5a27b2 7985:ec2e6893caaa
30 30
31 31
32 ngx_chain_t * 32 ngx_chain_t *
33 ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) 33 ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
34 { 34 {
35 int rc, flags; 35 int rc, flags;
36 off_t send, prev_send, sent; 36 off_t send, prev_send, sent;
37 size_t file_size; 37 size_t file_size;
38 ssize_t n; 38 ssize_t n;
39 ngx_uint_t eintr, eagain; 39 ngx_err_t err;
40 ngx_err_t err; 40 ngx_buf_t *file;
41 ngx_buf_t *file; 41 ngx_uint_t eintr, eagain;
42 ngx_event_t *wev; 42 #if (NGX_HAVE_SENDFILE_NODISKIO)
43 ngx_chain_t *cl; 43 ngx_uint_t ebusy;
44 ngx_iovec_t header, trailer; 44 #endif
45 struct sf_hdtr hdtr; 45 ngx_event_t *wev;
46 struct iovec headers[NGX_IOVS_PREALLOCATE]; 46 ngx_chain_t *cl;
47 struct iovec trailers[NGX_IOVS_PREALLOCATE]; 47 ngx_iovec_t header, trailer;
48 #if (NGX_HAVE_AIO_SENDFILE) 48 struct sf_hdtr hdtr;
49 ngx_uint_t ebusy; 49 struct iovec headers[NGX_IOVS_PREALLOCATE];
50 ngx_event_aio_t *aio; 50 struct iovec trailers[NGX_IOVS_PREALLOCATE];
51 #endif
52 51
53 wev = c->write; 52 wev = c->write;
54 53
55 if (!wev->ready) { 54 if (!wev->ready) {
56 return in; 55 return in;
75 74
76 send = 0; 75 send = 0;
77 eagain = 0; 76 eagain = 0;
78 flags = 0; 77 flags = 0;
79 78
80 #if (NGX_HAVE_AIO_SENDFILE && NGX_SUPPRESS_WARN)
81 aio = NULL;
82 file = NULL;
83 #endif
84
85 header.iovs = headers; 79 header.iovs = headers;
86 header.nalloc = NGX_IOVS_PREALLOCATE; 80 header.nalloc = NGX_IOVS_PREALLOCATE;
87 81
88 trailer.iovs = trailers; 82 trailer.iovs = trailers;
89 trailer.nalloc = NGX_IOVS_PREALLOCATE; 83 trailer.nalloc = NGX_IOVS_PREALLOCATE;
90 84
91 for ( ;; ) { 85 for ( ;; ) {
92 eintr = 0; 86 eintr = 0;
93 #if (NGX_HAVE_AIO_SENDFILE) 87 #if (NGX_HAVE_SENDFILE_NODISKIO)
94 ebusy = 0; 88 ebusy = 0;
95 #endif 89 #endif
96 prev_send = send; 90 prev_send = send;
97 91
98 /* create the header iovec and coalesce the neighbouring bufs */ 92 /* create the header iovec and coalesce the neighbouring bufs */
177 header.size = 0; 171 header.size = 0;
178 } 172 }
179 173
180 sent = 0; 174 sent = 0;
181 175
182 #if (NGX_HAVE_AIO_SENDFILE) 176 #if (NGX_HAVE_SENDFILE_NODISKIO)
183 aio = file->file->aio; 177 flags = (c->busy_count <= 2) ? SF_NODISKIO : 0;
184 flags = (aio && aio->preload_handler) ? SF_NODISKIO : 0;
185 #endif 178 #endif
186 179
187 rc = sendfile(file->file->fd, c->fd, file->file_pos, 180 rc = sendfile(file->file->fd, c->fd, file->file_pos,
188 file_size + header.size, &hdtr, &sent, flags); 181 file_size + header.size, &hdtr, &sent, flags);
189 182
197 190
198 case NGX_EINTR: 191 case NGX_EINTR:
199 eintr = 1; 192 eintr = 1;
200 break; 193 break;
201 194
202 #if (NGX_HAVE_AIO_SENDFILE) 195 #if (NGX_HAVE_SENDFILE_NODISKIO)
203 case NGX_EBUSY: 196 case NGX_EBUSY:
204 ebusy = 1; 197 ebusy = 1;
205 break; 198 break;
206 #endif 199 #endif
207 200
250 243
251 c->sent += sent; 244 c->sent += sent;
252 245
253 in = ngx_chain_update_sent(in, sent); 246 in = ngx_chain_update_sent(in, sent);
254 247
255 #if (NGX_HAVE_AIO_SENDFILE) 248 #if (NGX_HAVE_SENDFILE_NODISKIO)
256 249
257 if (ebusy) { 250 if (ebusy) {
258 if (sent == 0) { 251 if (sent == 0) {
259 c->busy_count++; 252 c->busy_count++;
260 253
261 if (c->busy_count > 2) { 254 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
262 ngx_log_error(NGX_LOG_ALERT, c->log, 0, 255 "sendfile() busy, count:%d", c->busy_count);
263 "sendfile(%V) returned busy again",
264 &file->file->name);
265
266 c->busy_count = 0;
267 aio->preload_handler = NULL;
268
269 send = prev_send;
270 continue;
271 }
272 256
273 } else { 257 } else {
274 c->busy_count = 0; 258 c->busy_count = 0;
275 } 259 }
276 260
277 n = aio->preload_handler(file); 261 if (wev->posted) {
278 262 ngx_delete_posted_event(wev);
279 if (n > 0) { 263 }
280 send = prev_send + sent; 264
281 continue; 265 ngx_post_event(wev, &ngx_posted_next_events);
282 } 266
283 267 wev->ready = 0;
284 return in; 268 return in;
285 } 269 }
286 270
287 if (flags == SF_NODISKIO) { 271 c->busy_count = 0;
288 c->busy_count = 0;
289 }
290 272
291 #endif 273 #endif
292 274
293 if (eagain) { 275 if (eagain) {
294 276