Fix: mmap write() for large subbuffers and handle EINTR (v2)
[lttng-tools.git] / src / common / ust-consumer / ust-consumer.c
index e81f05041249a164ce09151e9907c0602b41d6be..47c0d460ffb60cc612d9806325602c6eac459023 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
@@ -43,14 +42,14 @@ extern volatile int consumer_quit;
 /*
  * Mmap the ring buffer, read it and write the data to the tracefile.
  *
- * Returns the number of bytes written
+ * Returns the number of bytes written, else negative value on error.
  */
-int lttng_ustconsumer_on_read_subbuffer_mmap(
+ssize_t lttng_ustconsumer_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;
+       long ret = 0, written = 0;
        off_t orig_offset = stream->out_fd_offset;
        int outfd = stream->out_fd;
 
@@ -58,31 +57,41 @@ int lttng_ustconsumer_on_read_subbuffer_mmap(
        ret = ustctl_get_mmap_read_offset(stream->chan->handle,
                stream->buf, &mmap_offset);
        if (ret != 0) {
-               ret = -errno;
-               perror("ustctl_get_mmap_read_offset");
+               errno = -ret;
+               PERROR("ustctl_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;
-                       perror("Error in file write");
+               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;
 }
 
 /*
@@ -90,7 +99,7 @@ end:
  *
  * Returns the number of bytes spliced.
  */
-int lttng_ustconsumer_on_read_subbuffer_splice(
+ssize_t lttng_ustconsumer_on_read_subbuffer_splice(
                struct lttng_consumer_local_data *ctx,
                struct lttng_consumer_stream *stream, unsigned long len)
 {
@@ -109,8 +118,8 @@ int lttng_ustconsumer_take_snapshot(struct lttng_consumer_local_data *ctx,
 
        ret = ustctl_snapshot(stream->chan->handle, stream->buf);
        if (ret != 0) {
-               ret = errno;
-               perror("Getting sub-buffer snapshot.");
+               errno = -ret;
+               PERROR("Getting sub-buffer snapshot.");
        }
 
        return ret;
@@ -131,8 +140,8 @@ int lttng_ustconsumer_get_produced_snapshot(
        ret = ustctl_snapshot_get_produced(stream->chan->handle,
                        stream->buf, pos);
        if (ret != 0) {
-               ret = errno;
-               perror("kernctl_snapshot_get_produced");
+               errno = -ret;
+               PERROR("kernctl_snapshot_get_produced");
        }
 
        return ret;
@@ -258,11 +267,20 @@ int lttng_ustconsumer_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;
 }
@@ -347,7 +365,7 @@ int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
        if (!stream->hangup_flush_done) {
                do {
                        readlen = read(stream->wait_fd, &dummy, 1);
-               } while (readlen == -1 && errno == -EINTR);
+               } while (readlen == -1 && errno == EINTR);
                if (readlen == -1) {
                        ret = readlen;
                        goto end;
@@ -376,7 +394,7 @@ int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream *stream,
        assert(err == 0);
        /* 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
@@ -401,7 +419,7 @@ int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
                                stream->uid, stream->gid);
                if (ret < 0) {
                        ERR("Opening %s", stream->path_name);
-                       perror("open");
+                       PERROR("open");
                        goto error;
                }
                stream->out_fd = ret;
This page took 0.027854 seconds and 4 git commands to generate.