Fix: mmap write() for large subbuffers and handle EINTR (v2)
[lttng-tools.git] / src / common / kernel-consumer / kernel-consumer.c
index f32f47b3eeb1f6faf6b615700529dea5e990c7e5..3c9687306dc2aa2e904ef35f9954bdcc37e1d436 100644 (file)
@@ -2,19 +2,18 @@
  * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
  *                      Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; only version 2
- * of the License.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2 only,
+ * as published by the Free Software Foundation.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #define _GNU_SOURCE
@@ -45,12 +44,12 @@ extern volatile int consumer_quit;
  *
  * Returns the number of bytes written
  */
-int lttng_kconsumer_on_read_subbuffer_mmap(
+ssize_t lttng_kconsumer_on_read_subbuffer_mmap(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
        unsigned long mmap_offset;
-       long ret = 0;
+       ssize_t ret = 0, written = 0;
        off_t orig_offset = stream->out_fd_offset;
        int fd = stream->wait_fd;
        int outfd = stream->out_fd;
@@ -58,32 +57,42 @@ int lttng_kconsumer_on_read_subbuffer_mmap(
        /* get the offset inside the fd to mmap */
        ret = kernctl_get_mmap_read_offset(fd, &mmap_offset);
        if (ret != 0) {
-               ret = -errno;
+               errno = -ret;
                perror("kernctl_get_mmap_read_offset");
+               written = ret;
                goto end;
        }
 
        while (len > 0) {
                ret = write(outfd, stream->mmap_base + mmap_offset, len);
-               if (ret >= len) {
-                       len = 0;
-               } else if (ret < 0) {
-                       ret = -errno;
+               if (ret < 0) {
+                       if (errno == EINTR) {
+                               /* restart the interrupted system call */
+                               continue;
+                       } else {
+                               perror("Error in file write");
+                               if (written == 0) {
+                                       written = ret;
+                               }
+                               goto end;
+                       }
+               } else if (ret > len) {
                        perror("Error in file write");
+                       written += ret;
                        goto end;
+               } else {
+                       len -= ret;
+                       mmap_offset += ret;
                }
                /* This won't block, but will start writeout asynchronously */
                lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
                                SYNC_FILE_RANGE_WRITE);
                stream->out_fd_offset += ret;
+               written += ret;
        }
-
        lttng_consumer_sync_trace_file(stream, orig_offset);
-
-       goto end;
-
 end:
-       return ret;
+       return written;
 }
 
 /*
@@ -91,11 +100,11 @@ end:
  *
  * Returns the number of bytes spliced.
  */
-int lttng_kconsumer_on_read_subbuffer_splice(
+ssize_t lttng_kconsumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
-       long ret = 0;
+       ssize_t ret = 0, written = 0;
        loff_t offset = 0;
        off_t orig_offset = stream->out_fd_offset;
        int fd = stream->wait_fd;
@@ -106,19 +115,32 @@ int lttng_kconsumer_on_read_subbuffer_splice(
                                (unsigned long)offset, fd);
                ret = splice(fd, &offset, ctx->consumer_thread_pipe[1], NULL, len,
                                SPLICE_F_MOVE | SPLICE_F_MORE);
-               DBG("splice chan to pipe ret %ld", ret);
+               DBG("splice chan to pipe ret %zd", ret);
                if (ret < 0) {
-                       ret = errno;
                        perror("Error in relay splice");
+                       if (written == 0) {
+                               written = ret;
+                       }
+                       ret = errno;
                        goto splice_error;
                }
 
                ret = splice(ctx->consumer_thread_pipe[0], NULL, outfd, NULL, ret,
                                SPLICE_F_MOVE | SPLICE_F_MORE);
-               DBG("splice pipe to file %ld", ret);
+               DBG("splice pipe to file %zd", ret);
                if (ret < 0) {
-                       ret = errno;
                        perror("Error in file splice");
+                       if (written == 0) {
+                               written = ret;
+                       }
+                       ret = errno;
+                       goto splice_error;
+               }
+               if (ret > len) {
+                       errno = EINVAL;
+                       perror("Wrote more data than requested");
+                       written += ret;
+                       ret = errno;
                        goto splice_error;
                }
                len -= ret;
@@ -126,6 +148,7 @@ int lttng_kconsumer_on_read_subbuffer_splice(
                lttng_sync_file_range(outfd, stream->out_fd_offset, ret,
                                SYNC_FILE_RANGE_WRITE);
                stream->out_fd_offset += ret;
+               written += ret;
        }
        lttng_consumer_sync_trace_file(stream, orig_offset);
 
@@ -133,7 +156,7 @@ int lttng_kconsumer_on_read_subbuffer_splice(
 
 splice_error:
        /* send the appropriate error description to sessiond */
-       switch(ret) {
+       switch (ret) {
        case EBADF:
                lttng_consumer_send_error(ctx, CONSUMERD_SPLICE_EBADF);
                break;
@@ -149,7 +172,7 @@ splice_error:
        }
 
 end:
-       return ret;
+       return written;
 }
 
 /*
@@ -165,7 +188,7 @@ int lttng_kconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
 
        ret = kernctl_snapshot(infd);
        if (ret != 0) {
-               ret = errno;
+               errno = -ret;
                perror("Getting sub-buffer snapshot.");
        }
 
@@ -187,7 +210,7 @@ int lttng_kconsumer_get_produced_snapshot(
 
        ret = kernctl_snapshot_get_produced(infd, pos);
        if (ret != 0) {
-               ret = errno;
+               errno = -ret;
                perror("kernctl_snapshot_get_produced");
        }
 
@@ -296,11 +319,20 @@ int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
                break;
        }
 end:
-       /* signal the poll thread */
-       ret = write(ctx->consumer_poll_pipe[1], "4", 1);
-       if (ret < 0) {
-               perror("write consumer poll");
-       }
+       /*
+        * Wake-up the other end by writing a null byte in the pipe
+        * (non-blocking). Important note: Because writing into the
+        * pipe is non-blocking (and therefore we allow dropping wakeup
+        * data, as long as there is wakeup data present in the pipe
+        * buffer to wake up the other end), the other end should
+        * perform the following sequence for waiting:
+        * 1) empty the pipe (reads).
+        * 2) perform update operation.
+        * 3) wait on the pipe (poll).
+        */
+       do {
+               ret = write(ctx->consumer_poll_pipe[1], "", 1);
+       } while (ret == -1UL && errno == EINTR);
 end_nosignal:
        return 0;
 }
@@ -308,19 +340,18 @@ end_nosignal:
 /*
  * Consume data on a file descriptor and write it on a trace file.
  */
-int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
+ssize_t lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
                struct lttng_consumer_local_data *ctx)
 {
        unsigned long len;
        int err;
-       long ret = 0;
+       ssize_t ret = 0;
        int infd = stream->wait_fd;
 
        DBG("In read_subbuffer (infd : %d)", infd);
        /* Get the next subbuffer */
        err = kernctl_get_next_subbuf(infd);
        if (err != 0) {
-               ret = errno;
                /*
                 * This is a debug message even for single-threaded consumer,
                 * because poll() have more relaxed criterions than get subbuf,
@@ -337,32 +368,33 @@ int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
                        /* read the whole subbuffer */
                        err = kernctl_get_padded_subbuf_size(infd, &len);
                        if (err != 0) {
-                               ret = errno;
+                               errno = -ret;
                                perror("Getting sub-buffer len failed.");
                                goto end;
                        }
 
                        /* splice the subbuffer to the tracefile */
                        ret = lttng_consumer_on_read_subbuffer_splice(ctx, stream, len);
-                       if (ret < 0) {
+                       if (ret != len) {
                                /*
                                 * display the error but continue processing to try
                                 * to release the subbuffer
                                 */
                                ERR("Error splicing to tracefile");
                        }
+
                        break;
                case LTTNG_EVENT_MMAP:
                        /* read the used subbuffer size */
                        err = kernctl_get_padded_subbuf_size(infd, &len);
                        if (err != 0) {
-                               ret = errno;
+                               errno = -ret;
                                perror("Getting sub-buffer len failed.");
                                goto end;
                        }
                        /* write the subbuffer to the tracefile */
                        ret = lttng_consumer_on_read_subbuffer_mmap(ctx, stream, len);
-                       if (ret < 0) {
+                       if (ret != len) {
                                /*
                                 * display the error but continue processing to try
                                 * to release the subbuffer
@@ -377,7 +409,7 @@ int lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
 
        err = kernctl_put_next_subbuf(infd);
        if (err != 0) {
-               ret = errno;
+               errno = -ret;
                if (errno == EFAULT) {
                        perror("Error in unreserving sub buffer\n");
                } else if (errno == EIO) {
@@ -415,7 +447,7 @@ int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
 
                ret = kernctl_get_mmap_len(stream->wait_fd, &mmap_len);
                if (ret != 0) {
-                       ret = errno;
+                       errno = -ret;
                        perror("kernctl_get_mmap_len");
                        goto error_close_fd;
                }
This page took 0.026996 seconds and 4 git commands to generate.