[nginx] Used the pwritev() syscall for writing files where possi...

Valentin Bartenev vbart at nginx.com
Tue Nov 17 16:02:23 UTC 2015


details:   http://hg.nginx.org/nginx/rev/b5a87b51be24
branches:  
changeset: 6301:b5a87b51be24
user:      Valentin Bartenev <vbart at nginx.com>
date:      Tue Nov 17 19:01:41 2015 +0300
description:
Used the pwritev() syscall for writing files where possible.

It is more effective, because it doesn't require a separate lseek().

diffstat:

 auto/unix               |  16 ++++++++++++++++
 src/os/unix/ngx_files.c |  38 +++++++++++++++++++++++++++++++++++---
 2 files changed, 51 insertions(+), 3 deletions(-)

diffs (82 lines):

diff -r be6af0906a4d -r b5a87b51be24 auto/unix
--- a/auto/unix	Tue Nov 17 19:01:41 2015 +0300
+++ b/auto/unix	Tue Nov 17 19:01:41 2015 +0300
@@ -589,6 +589,22 @@ ngx_feature_test="char buf[1]; ssize_t n
 . auto/feature
 
 
+# pwritev() was introduced in FreeBSD 6 and Linux 2.6.30, glibc 2.10
+
+ngx_feature="pwritev()"
+ngx_feature_name="NGX_HAVE_PWRITEV"
+ngx_feature_run=no
+ngx_feature_incs='#include <sys/uio.h>'
+ngx_feature_path=
+ngx_feature_libs=
+ngx_feature_test="char buf[1]; struct iovec vec[1]; ssize_t n;
+                  vec[0].iov_base = buf;
+                  vec[0].iov_len = 1;
+                  n = pwritev(1, vec, 1, 0);
+                  if (n == -1) return 1"
+. auto/feature
+
+
 ngx_feature="sys_nerr"
 ngx_feature_name="NGX_SYS_NERR"
 ngx_feature_run=value
diff -r be6af0906a4d -r b5a87b51be24 src/os/unix/ngx_files.c
--- a/src/os/unix/ngx_files.c	Tue Nov 17 19:01:41 2015 +0300
+++ b/src/os/unix/ngx_files.c	Tue Nov 17 19:01:41 2015 +0300
@@ -367,6 +367,38 @@ ngx_writev_file(ngx_file_t *file, ngx_ar
     ssize_t    n;
     ngx_err_t  err;
 
+    ngx_log_debug3(NGX_LOG_DEBUG_CORE, file->log, 0,
+                   "writev: %d, %uz, %O", file->fd, size, offset);
+
+#if (NGX_HAVE_PWRITEV)
+
+eintr:
+
+    n = pwritev(file->fd, vec->elts, vec->nelts, offset);
+
+    if (n == -1) {
+        err = ngx_errno;
+
+        if (err == NGX_EINTR) {
+            ngx_log_debug0(NGX_LOG_DEBUG_CORE, file->log, err,
+                           "pwritev() was interrupted");
+            goto eintr;
+        }
+
+        ngx_log_error(NGX_LOG_CRIT, file->log, err,
+                      "pwritev() \"%s\" failed", file->name.data);
+        return NGX_ERROR;
+    }
+
+    if ((size_t) n != size) {
+        ngx_log_error(NGX_LOG_CRIT, file->log, 0,
+                      "pwritev() \"%s\" has written only %z of %uz",
+                      file->name.data, n, size);
+        return NGX_ERROR;
+    }
+
+#else
+
     if (file->sys_offset != offset) {
         if (lseek(file->fd, offset, SEEK_SET) == -1) {
             ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno,
@@ -402,10 +434,10 @@ eintr:
         return NGX_ERROR;
     }
 
-    ngx_log_debug2(NGX_LOG_DEBUG_CORE, file->log, 0,
-                   "writev: %d, %z", file->fd, n);
+    file->sys_offset += n;
 
-    file->sys_offset += n;
+#endif
+
     file->offset += n;
 
     return n;



More information about the nginx-devel mailing list