[sysprof/wip/chergert/mmap-writer] capture: use mmap() for SysprofCaptureWriter



commit ef79a41279f1100508288c1630564bfda3f1ad3d
Author: Christian Hergert <chergert redhat com>
Date:   Mon Feb 10 14:24:35 2020 -0800

    capture: use mmap() for SysprofCaptureWriter
    
    Currently, an inferior process needs to synchronize with the observer when
    profiling completes so that data is flushed to the respective
    file-desciptors.  This is problematic in scenarios where the inferior either
    crashes, or cannot be reliabley stopped.
    
    Additionally, it prevents the use of per-thread capture writers since we
    cannot guarantee cleanup of those threads in synchronization with
    SysprofProfiler or SysprofSource::stopped().
    
    This changes the writing to be done with mmap(), which means we need to be
    graceful in the presence of trailing zeros from posix_fallocate(). Some
    additional work will need to be done to make this portable to Windows and
    possibly some compilation fixes for other UNIX'like OS such as FreeBSD.
    Presumably for the UNIXs just mmap() flags need to be fixed.
    
    Once this has landed, we can have lock-free access to capture writers on a
    per-thread basis which helps collection of intensive operations such as
    memory tracing which requires expensive backtraces.
    
    Older versions of Sysprof will need a bit of robustness fixes so they know
    to stop when they get to a frame type of 0. They already should stop there,
    but in a non-graceful manner.

 meson.build                                     |   4 +
 src/libsysprof-capture/meson.build              |   1 +
 src/libsysprof-capture/mmap-writer.c            | 450 ++++++++++++++++++++++++
 src/libsysprof-capture/mmap-writer.h            |  92 +++++
 src/libsysprof-capture/sysprof-capture-reader.c |  51 +--
 src/libsysprof-capture/sysprof-capture-reader.h |   3 +-
 src/libsysprof-capture/sysprof-capture-writer.c | 425 +++++-----------------
 src/libsysprof-capture/sysprof-capture-writer.h |   4 -
 src/tests/test-capture.c                        |  22 +-
 src/tools/sysprof-dump.c                        |   2 +-
 10 files changed, 694 insertions(+), 360 deletions(-)
---
diff --git a/meson.build b/meson.build
index 13ef5a4..ee5926c 100644
--- a/meson.build
+++ b/meson.build
@@ -89,6 +89,10 @@ if cc.has_header('execinfo.h')
   config_h.set10('HAVE_EXECINFO_H', true)
 endif
 
+if cc.has_header('sys/mman.h')
+  config_h.set10('HAVE_SYS_MMAN_H', true)
+endif
+
 libunwind_dep = dependency('libunwind-generic', required: false)
 if libunwind_dep.found()
   config_h.set10('ENABLE_LIBUNWIND', libunwind_dep.found())
diff --git a/src/libsysprof-capture/meson.build b/src/libsysprof-capture/meson.build
index 8c499c7..3a4fcf7 100644
--- a/src/libsysprof-capture/meson.build
+++ b/src/libsysprof-capture/meson.build
@@ -23,6 +23,7 @@ libsysprof_capture_sources = files([
   'sysprof-capture-writer-cat.c',
   'sysprof-clock.c',
   'sysprof-platform.c',
+  'mmap-writer.c',
 ])
 
 configure_file(
diff --git a/src/libsysprof-capture/mmap-writer.c b/src/libsysprof-capture/mmap-writer.c
new file mode 100644
index 0000000..a7503b2
--- /dev/null
+++ b/src/libsysprof-capture/mmap-writer.c
@@ -0,0 +1,450 @@
+/* mmap-writer.c
+ *
+ * Copyright 2020 Christian Hergert <chergert redhat com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * Subject to the terms and conditions of this license, each copyright holder
+ * and contributor hereby grants to those receiving rights under this license
+ * a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
+ * irrevocable (except for failure to satisfy the conditions of this license)
+ * patent license to make, have made, use, offer to sell, sell, import, and
+ * otherwise transfer this software, where such license applies only to those
+ * patent claims, already acquired or hereafter acquired, licensable by such
+ * copyright holder or contributor that are necessarily infringed by:
+ *
+ * (a) their Contribution(s) (the licensed copyrights of copyright holders
+ *     and non-copyrightable additions of contributors, in source or binary
+ *     form) alone; or
+ *
+ * (b) combination of their Contribution(s) with the work of authorship to
+ *     which such Contribution(s) was added by such copyright holder or
+ *     contributor, if, at the time the Contribution is added, such addition
+ *     causes such combination to be necessarily infringed. The patent license
+ *     shall not apply to any other combinations which include the
+ *     Contribution.
+ *
+ * Except as expressly stated above, no rights or licenses from any copyright
+ * holder or contributor is granted under this license, whether expressly, by
+ * implication, estoppel or otherwise.
+ *
+ * DISCLAIMER
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause-Patent
+ */
+
+#include "config.h"
+
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#ifdef HAVE_SYS_MMAN_H
+# include <sys/mman.h>
+#endif
+
+#include "mmap-writer.h"
+
+#include "sysprof-capture-util-private.h"
+
+struct _MmapWriter
+{
+  /* The file-descriptor for our underlying file that is mapped
+   * into the address space.
+   */
+  int fd;
+
+  /* The page size we are using for mappings so that we can
+   * calculate how many pages to map into address space.
+   */
+  gsize page_size;
+
+  /* The result of mmap() is stored here so that we can calculate
+   * addresses when mmap_writer_advance() is called.
+   */
+  void *page_map;
+
+  /* Because special information is often stored at the head of a
+   * file, we map the head page specially so that it is always available
+   * for quick access.
+   */
+  void *head_page;
+
+  /* The first page that is mapped (the byte offset is calculated
+   * from (page*page_size).
+   */
+  goffset page;
+
+  /* The number of pages that are mapped sequentially. */
+  goffset n_pages;
+
+  /* The offset from @page_map for write position. */
+  goffset wr_offset;
+};
+
+static void
+mmap_writer_unmap_head (MmapWriter *self)
+{
+  g_assert (self != NULL);
+  g_assert (self->n_pages > 0);
+  g_assert (self->page_size > 0);
+
+  if (self->head_page != NULL)
+    {
+      msync (self->head_page, self->page_size, MS_SYNC);
+      madvise (self->head_page, self->page_size, MADV_DONTNEED);
+      munmap (self->head_page, self->page_size);
+      self->head_page = NULL;
+    }
+}
+
+static gboolean
+mmap_writer_map_head (MmapWriter *self)
+{
+  void *map;
+
+  g_assert (self != NULL);
+  g_assert (self->head_page == NULL);
+  g_assert (self->n_pages > 0);
+  g_assert (self->page_size > 0);
+  g_assert (self->fd > -1);
+
+  if (posix_fallocate (self->fd, 0, self->page_size) < 0)
+    return FALSE;
+
+  map = mmap (NULL, self->page_size, PROT_WRITE | PROT_READ, MAP_SHARED, self->fd, 0);
+  if (map == MAP_FAILED)
+    return FALSE;
+
+  madvise (map, self->page_size, MADV_WILLNEED);
+
+  self->head_page = map;
+
+  return TRUE;
+}
+
+static void
+mmap_writer_flush_page_map (MmapWriter *self)
+{
+  g_assert (self != NULL);
+
+  if (self->page_map != NULL)
+    {
+      gsize wr_offset = self->wr_offset;
+      gsize n_pages = 0;
+
+      while (wr_offset > self->page_size)
+        {
+          wr_offset -= self->page_size;
+          n_pages++;
+        }
+
+      if (wr_offset > 0)
+        n_pages++;
+
+      msync (self->page_map, n_pages * self->page_size, MS_SYNC);
+    }
+}
+
+static void
+mmap_writer_unmap (MmapWriter *self)
+{
+  g_assert (self != NULL);
+  g_assert (self->n_pages > 0);
+  g_assert (self->page_size > 0);
+
+  if (self->page_map != NULL)
+    {
+      mmap_writer_flush_page_map (self);
+      madvise (self->page_map, self->page_size * self->n_pages, MADV_DONTNEED);
+      munmap (self->page_map, self->page_size * self->n_pages);
+      self->page_map = NULL;
+    }
+}
+
+static gboolean
+mmap_writer_map (MmapWriter *self)
+{
+  void *map;
+
+  g_assert (self != NULL);
+  g_assert (self->page_map == NULL);
+  g_assert (self->n_pages > 0);
+  g_assert (self->page_size > 0);
+  g_assert (self->fd > -1);
+
+  if (posix_fallocate (self->fd,
+                       self->page * self->page_size,
+                       self->n_pages * self->page_size) < 0)
+    return FALSE;
+
+  map = mmap (NULL,
+              self->page_size * self->n_pages,
+              PROT_WRITE | PROT_READ,
+              MAP_SHARED,
+              self->fd,
+              self->page_size * self->page);
+  if (map == MAP_FAILED)
+    return FALSE;
+
+  madvise (map,
+           self->page_size * self->n_pages,
+           (MADV_NOHUGEPAGE | MADV_SEQUENTIAL));
+
+  self->page_map = map;
+
+  return TRUE;
+}
+
+static void
+normalize_buffer_size (MmapWriter *self,
+                       gsize       buffer_size)
+{
+  g_assert (self != NULL);
+  g_assert (self->page_map == NULL);
+  g_assert (self->page_size > 0);
+
+  if (buffer_size == 0)
+    {
+      self->n_pages = 16;
+      return;
+    }
+
+  self->n_pages = 0;
+
+  while (buffer_size >= self->page_size)
+    {
+      self->n_pages++;
+      buffer_size -= self->page_size;
+    }
+
+  if (buffer_size > 0)
+    self->n_pages++;
+
+  g_assert (self->n_pages > 0);
+}
+
+MmapWriter *
+mmap_writer_new_for_fd (gint  fd,
+                        gsize buffer_size)
+{
+  MmapWriter *ret;
+
+  if (fd < 0)
+    return NULL;
+
+  ret = g_atomic_rc_box_new0 (MmapWriter);
+  ret->fd = fd;
+  ret->page_size = _sysprof_getpagesize ();
+  ret->page_map = NULL;
+  ret->page = 0;
+  ret->n_pages = 16;
+  ret->wr_offset = sizeof (SysprofCaptureFileHeader);
+
+  normalize_buffer_size (ret, buffer_size);
+
+  if (!mmap_writer_map (ret) || !mmap_writer_map_head (ret))
+    {
+      close (ret->fd);
+      g_atomic_rc_box_release (ret);
+    }
+
+  return g_steal_pointer (&ret);
+}
+
+MmapWriter *
+mmap_writer_new (const gchar *filename,
+                 gsize        buffer_size)
+{
+  gint fd;
+
+  if (filename == NULL)
+    return NULL;
+
+  if ((fd = open (filename, O_RDWR | O_CLOEXEC, 0640)) == -1)
+    return NULL;
+
+  return mmap_writer_new_for_fd (fd, buffer_size);
+}
+
+void
+mmap_writer_close (MmapWriter *self)
+{
+  g_assert (self != NULL);
+
+  mmap_writer_unmap (self);
+  mmap_writer_unmap_head (self);
+
+  if (self->fd > -1)
+    {
+      close (self->fd);
+      self->fd = -1;
+    }
+
+  self->wr_offset = 0;
+  self->page = 0;
+  self->n_pages = 0;
+
+  g_assert (self->fd == -1);
+  g_assert (self->head_page == NULL);
+  g_assert (self->page_map == NULL);
+}
+
+void
+mmap_writer_destroy (MmapWriter *self)
+{
+  g_atomic_rc_box_release_full (self, (GDestroyNotify)mmap_writer_close);
+}
+
+gint
+mmap_writer_get_fd (MmapWriter *self)
+{
+  return self != NULL ? self->fd : -1;
+}
+
+static inline gboolean
+mmap_writer_has_space_for (MmapWriter *self,
+                           goffset     length)
+{
+  goffset available;
+
+  g_assert (self != NULL);
+  g_assert (self->fd > -1);
+  g_assert (self->page_map != NULL);
+  g_assert (self->page_size > 0);
+  g_assert (self->n_pages > 0);
+
+  available = (self->page_size * self->n_pages) - self->wr_offset;
+
+  return length < available;
+}
+
+gpointer
+mmap_writer_advance (MmapWriter *self,
+                     goffset     length)
+{
+  void *ret;
+
+  g_assert (self != NULL);
+  g_assert (self->fd > -1);
+  g_assert (self->page_map != NULL);
+  g_assert (self->page_size > 0);
+  g_assert (self->n_pages > 0);
+  g_assert (self->wr_offset <= (self->n_pages * self->page_size));
+
+  if G_UNLIKELY (!mmap_writer_has_space_for (self, length))
+    {
+      goffset req_pages;
+
+      mmap_writer_unmap (self);
+
+      while (self->wr_offset > self->page_size)
+        {
+          self->page++;
+          self->wr_offset -= self->page_size;
+        }
+
+      /* Determine how many pages we need loaded */
+      req_pages = (self->wr_offset + length) / self->page_size;
+      if (((self->wr_offset + length) % self->page_size) > 0)
+        req_pages++;
+
+      /* We might need to increase the buffer size */
+      if (req_pages > self->n_pages)
+        self->n_pages = req_pages;
+
+      if (!mmap_writer_map (self))
+        return NULL;
+    }
+
+  /* Stash pointer for the frame we've just added */
+  ret = (guint8 *)self->page_map + self->wr_offset;
+
+  /* Now advance our write offset */
+  self->wr_offset += length;
+
+  return ret;
+}
+
+gpointer
+mmap_writer_rewind (MmapWriter *self,
+                    goffset     length)
+{
+  g_assert (self != NULL);
+  g_assert (self->fd > -1);
+  g_assert (self->page_map != NULL);
+  g_assert (self->page_size > 0);
+  g_assert (self->n_pages > 0);
+
+  /* We only allow rewinding into the current frame, but we can short-
+   * circuit and technically allow it within the current map range so
+   * we don't have to track the current frame size.
+   */
+  if (length > self->wr_offset)
+    return NULL;
+
+  self->wr_offset -= length;
+
+  return (guint8 *)self->page_map + self->wr_offset;
+}
+
+void
+mmap_writer_flush (MmapWriter *self)
+{
+  g_return_if_fail (self != NULL);
+
+  if (self->head_page != NULL)
+    msync (self->head_page, self->page_size, MS_SYNC);
+
+  if (self->page_map != NULL)
+    mmap_writer_flush_page_map (self);
+}
+
+SysprofCaptureFileHeader *
+mmap_writer_get_file_header (MmapWriter *self)
+{
+  g_return_val_if_fail (self != NULL, GSIZE_TO_POINTER (-1));
+  g_return_val_if_fail (self->head_page != NULL, GSIZE_TO_POINTER (-1));
+
+  return (SysprofCaptureFileHeader *)self->head_page;
+}
+
+gsize
+mmap_writer_get_buffer_size (MmapWriter *self)
+{
+  g_return_val_if_fail (self != NULL, 0);
+
+  return self->n_pages * self->page_size;
+}
+
+goffset
+mmap_writer_tell (MmapWriter *self)
+{
+  g_return_val_if_fail (self->head_page != NULL, -1);
+  g_return_val_if_fail (self->page_map != NULL, -1);
+
+  return self->page * self->page_size + self->wr_offset;
+}
diff --git a/src/libsysprof-capture/mmap-writer.h b/src/libsysprof-capture/mmap-writer.h
new file mode 100644
index 0000000..de84d99
--- /dev/null
+++ b/src/libsysprof-capture/mmap-writer.h
@@ -0,0 +1,92 @@
+/* mmap-writer.h
+ *
+ * Copyright 2020 Christian Hergert <chergert redhat com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * Subject to the terms and conditions of this license, each copyright holder
+ * and contributor hereby grants to those receiving rights under this license
+ * a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
+ * irrevocable (except for failure to satisfy the conditions of this license)
+ * patent license to make, have made, use, offer to sell, sell, import, and
+ * otherwise transfer this software, where such license applies only to those
+ * patent claims, already acquired or hereafter acquired, licensable by such
+ * copyright holder or contributor that are necessarily infringed by:
+ *
+ * (a) their Contribution(s) (the licensed copyrights of copyright holders
+ *     and non-copyrightable additions of contributors, in source or binary
+ *     form) alone; or
+ *
+ * (b) combination of their Contribution(s) with the work of authorship to
+ *     which such Contribution(s) was added by such copyright holder or
+ *     contributor, if, at the time the Contribution is added, such addition
+ *     causes such combination to be necessarily infringed. The patent license
+ *     shall not apply to any other combinations which include the
+ *     Contribution.
+ *
+ * Except as expressly stated above, no rights or licenses from any copyright
+ * holder or contributor is granted under this license, whether expressly, by
+ * implication, estoppel or otherwise.
+ *
+ * DISCLAIMER
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause-Patent
+ */
+
+#pragma once
+
+#include "sysprof-capture-types.h"
+
+G_BEGIN_DECLS
+
+typedef struct _MmapWriter MmapWriter;
+
+G_GNUC_INTERNAL
+MmapWriter               *mmap_writer_new             (const gchar *path,
+                                                       gsize        buffer_size);
+G_GNUC_INTERNAL
+MmapWriter               *mmap_writer_new_for_fd      (gint         fd,
+                                                       gsize        buffer_size);
+G_GNUC_INTERNAL
+void                      mmap_writer_close           (MmapWriter  *self);
+G_GNUC_INTERNAL
+void                      mmap_writer_flush           (MmapWriter  *self);
+G_GNUC_INTERNAL
+void                      mmap_writer_destroy         (MmapWriter  *self);
+G_GNUC_INTERNAL
+gint                      mmap_writer_get_fd          (MmapWriter  *self);
+G_GNUC_INTERNAL
+gpointer                  mmap_writer_advance         (MmapWriter  *self,
+                                                       goffset      length);
+G_GNUC_INTERNAL
+gpointer                  mmap_writer_rewind          (MmapWriter  *self,
+                                                       goffset      length);
+G_GNUC_INTERNAL
+SysprofCaptureFileHeader *mmap_writer_get_file_header (MmapWriter  *self);
+G_GNUC_INTERNAL
+gsize                     mmap_writer_get_buffer_size (MmapWriter  *self);
+G_GNUC_INTERNAL
+goffset                   mmap_writer_tell            (MmapWriter  *self);
+
+G_END_DECLS
diff --git a/src/libsysprof-capture/sysprof-capture-reader.c b/src/libsysprof-capture/sysprof-capture-reader.c
index 4774e3f..bd6823d 100644
--- a/src/libsysprof-capture/sysprof-capture-reader.c
+++ b/src/libsysprof-capture/sysprof-capture-reader.c
@@ -334,7 +334,20 @@ sysprof_capture_reader_ensure_space_for (SysprofCaptureReader *self,
 {
   g_assert (self != NULL);
   g_assert (self->pos <= self->len);
-  g_assert (len > 0);
+  g_assert (len >= sizeof (SysprofCaptureFrame));
+
+  /* If we have a writer that is loading data using mmap(), then there is a
+   * chance that we could hit zero section up to the end of the file. We can
+   * retry the buffered read though in that case so that we allow reading
+   * a file (without mmap) at the same time as writing with mmap. This is
+   * not really a common case except for in unit-testing.
+   */
+  if ((self->len - self->pos) >= sizeof (guint16) &&
+      *(guint16 *)(gpointer)&self->buf[self->pos] == 0)
+    {
+      self->fd_off -= (self->len - self->pos);
+      self->len = self->pos;
+    }
 
   if ((self->len - self->pos) < len)
     {
@@ -364,7 +377,17 @@ sysprof_capture_reader_ensure_space_for (SysprofCaptureReader *self,
         }
     }
 
-  return (self->len - self->pos) >= len;
+  if ((self->len - self->pos) >= len)
+    {
+      /* Make sure we got valid frame data back from the
+       * FD or else we might be in the zero-fill section
+       * up to the end of the file.
+       */
+      if (*(guint16 *)(gpointer)&self->buf[self->pos] >= len)
+        return TRUE;
+    }
+
+  return FALSE;
 }
 
 gboolean
@@ -419,6 +442,10 @@ sysprof_capture_reader_peek_frame (SysprofCaptureReader *self,
 
   sysprof_capture_reader_bswap_frame (self, frame);
 
+  /* In case the capture did not update the end_time during normal usage,
+   * we can update our cached known end_time based on the greatest frame
+   * we come across.
+   */
   if (frame->time > self->end_time)
     self->end_time = frame->time;
 
@@ -741,6 +768,7 @@ sysprof_capture_reader_read_jitmap (SysprofCaptureReader *self)
     return NULL;
 
   jitmap = (SysprofCaptureJitmap *)(gpointer)&self->buf[self->pos];
+  g_assert (jitmap->frame.len > 0);
 
   ret = g_hash_table_new_full (NULL, NULL, NULL, g_free);
 
@@ -961,26 +989,9 @@ sysprof_capture_reader_splice (SysprofCaptureReader  *self,
                                GError               **error)
 {
   g_assert (self != NULL);
-  g_assert (self->fd != -1);
   g_assert (dest != NULL);
 
-  /* Flush before writing anything to ensure consistency */
-  if (!sysprof_capture_writer_flush (dest))
-    {
-      g_set_error (error,
-                   G_FILE_ERROR,
-                   g_file_error_from_errno (errno),
-                   "%s", g_strerror (errno));
-      return FALSE;
-    }
-
-  /*
-   * We don't need to track position because writer will
-   * track the current position to avoid reseting it.
-   */
-
-  /* Perform the splice */
-  return _sysprof_capture_writer_splice_from_fd (dest, self->fd, error);
+  return sysprof_capture_writer_cat (dest, self, error);
 }
 
 /**
diff --git a/src/libsysprof-capture/sysprof-capture-reader.h b/src/libsysprof-capture/sysprof-capture-reader.h
index 3c58524..f8cf5e9 100644
--- a/src/libsysprof-capture/sysprof-capture-reader.h
+++ b/src/libsysprof-capture/sysprof-capture-reader.h
@@ -126,7 +126,8 @@ gboolean                            sysprof_capture_reader_reset               (
 SYSPROF_AVAILABLE_IN_ALL
 gboolean                            sysprof_capture_reader_splice              (SysprofCaptureReader      
*self,
                                                                                 SysprofCaptureWriter      
*dest,
-                                                                                GError                   
**error);
+                                                                                GError                   
**error)
+  G_GNUC_DEPRECATED_FOR (sysprof_capture_writer_cat);
 SYSPROF_AVAILABLE_IN_ALL
 gboolean                            sysprof_capture_reader_save_as             (SysprofCaptureReader      
*self,
                                                                                 const gchar               
*filename,
diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c
index 5e206af..22f0bfb 100644
--- a/src/libsysprof-capture/sysprof-capture-writer.c
+++ b/src/libsysprof-capture/sysprof-capture-writer.c
@@ -71,6 +71,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include "mmap-writer.h"
+
 #include "sysprof-capture-reader.h"
 #include "sysprof-capture-util-private.h"
 #include "sysprof-capture-writer.h"
@@ -91,48 +93,34 @@ typedef struct
 
 struct _SysprofCaptureWriter
 {
-  /*
-   * This is our buffer location for incoming strings. This is used
-   * similarly to GStringChunk except there is only one-page, and after
-   * it fills, we flush to disk.
+  /* Our hashtable for JIT deduplication.
    *
-   * This is paired with a closed hash table for deduplication.
-   */
-  gchar addr_buf[4096*4];
-
-  /* Our hashtable for deduplication. */
-  SysprofCaptureJitmapBucket addr_hash[512];
-
-  /* We keep the large fields above so that our allocation will be page
-   * alinged for the write buffer. This improves the performance of large
-   * writes to the target file-descriptor.
-   */
-  volatile gint ref_count;
-
-  /*
-   * Our address sequence counter. The value that comes from
+   * Our address sequence counter is the value that comes from
    * monotonically increasing this is OR'd with JITMAP_MARK to denote
    * the address name should come from the JIT map.
-   */
-  gsize addr_seq;
-
-  /* Our position in addr_buf. */
-  gsize addr_buf_pos;
-
-  /*
-   * The number of hash table items in @addr_hash. This is an
-   * optimization so that we can avoid calculating the number of strings
+   *
+   * The addr_hash_size is the number of items in @addr_hash. This is
+   * an optimization so that we avoid calculating the number of strings
    * when flushing out the jitmap.
    */
+  guint8 addr_buf[3584];
+  SysprofCaptureJitmapBucket addr_hash[512];
+  gsize addr_buf_pos;
+  gsize addr_seq;
   guint addr_hash_size;
 
-  /* Capture file handle */
-  int fd;
-
-  /* Our write buffer for fd */
-  guint8 *buf;
-  gsize pos;
-  gsize len;
+  /* The MmapWriter abstracts writing to a memory mapped file which is
+   * persisted to underlying storage. The goal here is to ensure that as
+   * we write data, it can be retrieved in case our program crashes. This
+   * is particularly useful when the external Sysprof profiler stops the
+   * profiler but the inferior process does not exit. The profiler can
+   * read up to the known data and then skip any unfinished data when
+   * muxing the streams.
+   *
+   * To make updating the SysprofCaptureFileHeader faster the MmapWriter
+   * keeps the head page around and available.
+   */
+  MmapWriter *writer;
 
   /* GSource for periodic flush */
   GSource *periodic_flush;
@@ -152,8 +140,6 @@ sysprof_capture_writer_frame_init (SysprofCaptureFrame     *frame_,
                                    gint64                   time_,
                                    SysprofCaptureFrameType  type)
 {
-  g_assert (frame_ != NULL);
-
   frame_->len = len;
   frame_->cpu = cpu;
   frame_->pid = pid;
@@ -169,74 +155,30 @@ sysprof_capture_writer_finalize (SysprofCaptureWriter *self)
   if (self != NULL)
     {
       g_clear_pointer (&self->periodic_flush, g_source_destroy);
-
       sysprof_capture_writer_flush (self);
-
-      if (self->fd != -1)
-        {
-          close (self->fd);
-          self->fd = -1;
-        }
-
-      g_free (self->buf);
-      g_free (self);
+      g_clear_pointer (&self->writer, mmap_writer_destroy);
     }
 }
 
 SysprofCaptureWriter *
 sysprof_capture_writer_ref (SysprofCaptureWriter *self)
 {
-  g_assert (self != NULL);
-  g_assert (self->ref_count > 0);
-
-  g_atomic_int_inc (&self->ref_count);
-
-  return self;
+  return g_atomic_rc_box_acquire (self);
 }
 
 void
 sysprof_capture_writer_unref (SysprofCaptureWriter *self)
 {
-  g_assert (self != NULL);
-  g_assert (self->ref_count > 0);
-
-  if (g_atomic_int_dec_and_test (&self->ref_count))
-    sysprof_capture_writer_finalize (self);
+  g_atomic_rc_box_release_full (self, (GDestroyNotify)sysprof_capture_writer_finalize);
 }
 
 static gboolean
 sysprof_capture_writer_flush_data (SysprofCaptureWriter *self)
 {
-  const guint8 *buf;
-  gssize written;
-  gsize to_write;
-
   g_assert (self != NULL);
-  g_assert (self->pos <= self->len);
-  g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
 
-  if (self->pos == 0)
-    return TRUE;
-
-  buf = self->buf;
-  to_write = self->pos;
-
-  while (to_write > 0)
-    {
-      written = _sysprof_write (self->fd, buf, to_write);
-      if (written < 0)
-        return FALSE;
-
-      if (written == 0 && errno != EAGAIN)
-        return FALSE;
-
-      g_assert (written <= (gssize)to_write);
-
-      buf += written;
-      to_write -= written;
-    }
-
-  self->pos = 0;
+  if (self->writer != NULL)
+    mmap_writer_flush (self->writer);
 
   return TRUE;
 }
@@ -247,52 +189,23 @@ sysprof_capture_writer_realign (gsize *pos)
   *pos = (*pos + SYSPROF_CAPTURE_ALIGN - 1) & ~(SYSPROF_CAPTURE_ALIGN - 1);
 }
 
-static inline gboolean
-sysprof_capture_writer_ensure_space_for (SysprofCaptureWriter *self,
-                                         gsize                 len)
-{
-  /* Check for max frame size */
-  if (len > G_MAXUSHORT)
-    return FALSE;
-
-  if ((self->len - self->pos) < len)
-    {
-      if (!sysprof_capture_writer_flush_data (self))
-        return FALSE;
-    }
-
-  return TRUE;
-}
-
 static inline gpointer
 sysprof_capture_writer_allocate (SysprofCaptureWriter *self,
                                  gsize                *len)
 {
-  gpointer p;
-
   g_assert (self != NULL);
   g_assert (len != NULL);
-  g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
+  g_assert (self->writer != NULL);
 
   sysprof_capture_writer_realign (len);
 
-  if (!sysprof_capture_writer_ensure_space_for (self, *len))
-    return NULL;
-
-  p = (gpointer)&self->buf[self->pos];
-
-  self->pos += *len;
-
-  g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
-
-  return p;
+  return mmap_writer_advance (self->writer, *len);
 }
 
 static gboolean
 sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self)
 {
-  SysprofCaptureJitmap jitmap;
-  gssize r;
+  SysprofCaptureJitmap *jitmap;
   gsize len;
 
   g_assert (self != NULL);
@@ -302,24 +215,20 @@ sysprof_capture_writer_flush_jitmap (SysprofCaptureWriter *self)
 
   g_assert (self->addr_buf_pos > 0);
 
-  len = sizeof jitmap + self->addr_buf_pos;
-
+  len = sizeof *jitmap + self->addr_buf_pos;
   sysprof_capture_writer_realign (&len);
 
-  sysprof_capture_writer_frame_init (&jitmap.frame,
+  jitmap = mmap_writer_advance (self->writer, len);
+  sysprof_capture_writer_frame_init (&jitmap->frame,
                                      len,
                                      -1,
                                      _sysprof_getpid (),
                                      SYSPROF_CAPTURE_CURRENT_TIME,
                                      SYSPROF_CAPTURE_FRAME_JITMAP);
-  jitmap.n_jitmaps = self->addr_hash_size;
-
-  if (sizeof jitmap != _sysprof_write (self->fd, &jitmap, sizeof jitmap))
-    return FALSE;
+  jitmap->n_jitmaps = self->addr_hash_size;
 
-  r = _sysprof_write (self->fd, self->addr_buf, len - sizeof jitmap);
-  if (r < 0 || (gsize)r != (len - sizeof jitmap))
-    return FALSE;
+  /* Copy the jitmap buffer into the frame data */
+  memcpy (jitmap->data, self->addr_buf, self->addr_buf_pos);
 
   self->addr_buf_pos = 0;
   self->addr_hash_size = 0;
@@ -387,7 +296,6 @@ sysprof_capture_writer_insert_jitmap (SysprofCaptureWriter *self,
 
   g_assert (self != NULL);
   g_assert (str != NULL);
-  g_assert ((self->pos % SYSPROF_CAPTURE_ALIGN) == 0);
 
   len = sizeof addr + strlen (str) + 1;
 
@@ -466,7 +374,6 @@ sysprof_capture_writer_new_from_fd (int   fd,
   g_autoptr(GDateTime) now = NULL;
   SysprofCaptureWriter *self;
   SysprofCaptureFileHeader *header;
-  gsize header_len = sizeof(*header);
 
   if (fd < 0)
     return NULL;
@@ -480,19 +387,14 @@ sysprof_capture_writer_new_from_fd (int   fd,
   /* This is only useful on files, memfd, etc */
   if (ftruncate (fd, 0) != 0) { /* Do Nothing */ }
 
-  self = g_new0 (SysprofCaptureWriter, 1);
-  self->ref_count = 1;
-  self->fd = fd;
-  self->buf = (guint8 *)g_malloc0 (buffer_size);
-  self->len = buffer_size;
+  self = g_atomic_rc_box_new0 (SysprofCaptureWriter);
+  self->writer = mmap_writer_new_for_fd (fd, buffer_size);
   self->next_counter_id = 1;
 
   now = g_date_time_new_now_local ();
   nowstr = g_date_time_format_iso8601 (now);
 
-  header = sysprof_capture_writer_allocate (self, &header_len);
-
-  if (header == NULL)
+  if (!(header = mmap_writer_get_file_header (self->writer)))
     {
       sysprof_capture_writer_finalize (self);
       return NULL;
@@ -511,18 +413,7 @@ sysprof_capture_writer_new_from_fd (int   fd,
   header->end_time = 0;
   memset (header->suffix, 0, sizeof header->suffix);
 
-  if (!sysprof_capture_writer_flush_data (self))
-    {
-      sysprof_capture_writer_finalize (self);
-      return NULL;
-    }
-
-  g_assert (self->pos == 0);
-  g_assert (self->len > 0);
-  g_assert (self->len % _sysprof_getpagesize() == 0);
-  g_assert (self->buf != NULL);
-  g_assert (self->addr_hash_size == 0);
-  g_assert (self->fd != -1);
+  mmap_writer_flush (self->writer);
 
   return self;
 }
@@ -570,9 +461,7 @@ sysprof_capture_writer_add_map (SysprofCaptureWriter *self,
   g_assert (filename != NULL);
 
   len = sizeof *ev + strlen (filename) + 1;
-
-  ev = (SysprofCaptureMap *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -617,8 +506,7 @@ sysprof_capture_writer_add_mark (SysprofCaptureWriter *self,
   message_len = strlen (message) + 1;
 
   len = sizeof *ev + message_len;
-  ev = (SysprofCaptureMark *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -663,8 +551,7 @@ sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self,
     metadata_len = strlen (metadata);
 
   len = sizeof *ev + metadata_len + 1;
-  ev = (SysprofCaptureMetadata *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -718,9 +605,7 @@ sysprof_capture_writer_add_process (SysprofCaptureWriter *self,
   g_assert (cmdline != NULL);
 
   len = sizeof *ev + strlen (cmdline) + 1;
-
-  ev = (SysprofCaptureProcess *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -753,9 +638,7 @@ sysprof_capture_writer_add_sample (SysprofCaptureWriter        *self,
   g_assert (self != NULL);
 
   len = sizeof *ev + (n_addrs * sizeof (SysprofCaptureAddress));
-
-  ev = (SysprofCaptureSample *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -782,12 +665,12 @@ sysprof_capture_writer_add_fork (SysprofCaptureWriter *self,
                                  gint32           child_pid)
 {
   SysprofCaptureFork *ev;
-  gsize len = sizeof *ev;
+  gsize len;
 
   g_assert (self != NULL);
 
-  ev = (SysprofCaptureFork *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  len = sizeof *ev;
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -810,12 +693,12 @@ sysprof_capture_writer_add_exit (SysprofCaptureWriter *self,
                                  gint32                pid)
 {
   SysprofCaptureExit *ev;
-  gsize len = sizeof *ev;
+  gsize len;
 
   g_assert (self != NULL);
 
-  ev = (SysprofCaptureExit *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  len = sizeof *ev;
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -837,12 +720,12 @@ sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self,
                                       gint32                pid)
 {
   SysprofCaptureTimestamp *ev;
-  gsize len = sizeof *ev;
+  gsize len;
 
   g_assert (self != NULL);
 
-  ev = (SysprofCaptureTimestamp *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  len = sizeof *ev;
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -860,21 +743,12 @@ sysprof_capture_writer_add_timestamp (SysprofCaptureWriter *self,
 static gboolean
 sysprof_capture_writer_flush_end_time (SysprofCaptureWriter *self)
 {
-  gint64 end_time = SYSPROF_CAPTURE_CURRENT_TIME;
-  ssize_t ret;
+  SysprofCaptureFileHeader *header;
 
   g_assert (self != NULL);
 
-  /* This field is opportunistic, so a failure is okay. */
-
-again:
-  ret = _sysprof_pwrite (self->fd,
-                         &end_time,
-                         sizeof (end_time),
-                         G_STRUCT_OFFSET (SysprofCaptureFileHeader, end_time));
-
-  if (ret < 0 && errno == EAGAIN)
-    goto again;
+  header = mmap_writer_get_file_header (self->writer);
+  header->end_time = SYSPROF_CAPTURE_CURRENT_TIME;
 
   return TRUE;
 }
@@ -910,10 +784,11 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter  *self,
   gsize to_write;
   off_t in_off;
   off_t pos;
+  int writer_fd;
   int fd = -1;
 
   g_assert (self != NULL);
-  g_assert (self->fd != -1);
+  g_assert (self->writer != NULL);
   g_assert (filename != NULL);
 
   if (-1 == (fd = open (filename, O_CREAT | O_RDWR, 0640)))
@@ -922,8 +797,8 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter  *self,
   if (!sysprof_capture_writer_flush (self))
     goto handle_errno;
 
-  if (-1 == (pos = lseek (self->fd, 0L, SEEK_CUR)))
-    goto handle_errno;
+  writer_fd = mmap_writer_get_fd (self->writer);
+  pos = mmap_writer_tell (self->writer);
 
   to_write = pos;
   in_off = 0;
@@ -932,7 +807,7 @@ sysprof_capture_writer_save_as (SysprofCaptureWriter  *self,
     {
       gssize written;
 
-      written = _sysprof_sendfile (fd, self->fd, &in_off, pos);
+      written = _sysprof_sendfile (fd, writer_fd, &in_off, to_write);
 
       if (written < 0)
         goto handle_errno;
@@ -964,77 +839,6 @@ handle_errno:
   return FALSE;
 }
 
-/**
- * _sysprof_capture_writer_splice_from_fd:
- * @self: An #SysprofCaptureWriter
- * @fd: the fd to read from.
- * @error: A location for a #GError, or %NULL.
- *
- * This is internal API for SysprofCaptureWriter and SysprofCaptureReader to
- * communicate when splicing a reader into a writer.
- *
- * This should not be used outside of #SysprofCaptureReader or
- * #SysprofCaptureWriter.
- *
- * This will not advance the position of @fd.
- *
- * Returns: %TRUE if successful; otherwise %FALSE and @error is set.
- */
-gboolean
-_sysprof_capture_writer_splice_from_fd (SysprofCaptureWriter  *self,
-                                        int                    fd,
-                                        GError               **error)
-{
-  struct stat stbuf;
-  off_t in_off;
-  gsize to_write;
-
-  g_assert (self != NULL);
-  g_assert (self->fd != -1);
-
-  if (-1 == fstat (fd, &stbuf))
-    goto handle_errno;
-
-  if (stbuf.st_size < 256)
-    {
-      g_set_error (error,
-                   G_FILE_ERROR,
-                   G_FILE_ERROR_INVAL,
-                   "Cannot splice, possibly corrupt file.");
-      return FALSE;
-    }
-
-  in_off = 256;
-  to_write = stbuf.st_size - in_off;
-
-  while (to_write > 0)
-    {
-      gssize written;
-
-      written = _sysprof_sendfile (self->fd, fd, &in_off, to_write);
-
-      if (written < 0)
-        goto handle_errno;
-
-      if (written == 0 && errno != EAGAIN)
-        goto handle_errno;
-
-      g_assert (written <= (gssize)to_write);
-
-      to_write -= written;
-    }
-
-  return TRUE;
-
-handle_errno:
-  g_set_error (error,
-               G_FILE_ERROR,
-               g_file_error_from_errno (errno),
-               "%s", g_strerror (errno));
-
-  return FALSE;
-}
-
 /**
  * sysprof_capture_writer_splice:
  * @self: An #SysprofCaptureWriter
@@ -1053,41 +857,20 @@ sysprof_capture_writer_splice (SysprofCaptureWriter  *self,
                                SysprofCaptureWriter  *dest,
                                GError               **error)
 {
+  SysprofCaptureReader *reader;
   gboolean ret;
-  off_t pos;
 
   g_assert (self != NULL);
-  g_assert (self->fd != -1);
   g_assert (dest != NULL);
-  g_assert (dest->fd != -1);
 
-  /* Flush before writing anything to ensure consistency */
-  if (!sysprof_capture_writer_flush (self) || !sysprof_capture_writer_flush (dest))
-    goto handle_errno;
-
-  /* Track our current position so we can reset */
-  if ((off_t)-1 == (pos = lseek (self->fd, 0L, SEEK_CUR)))
-    goto handle_errno;
+  if (!(reader = sysprof_capture_writer_create_reader (self, error)))
+    return FALSE;
 
-  /* Perform the splice */
-  ret = _sysprof_capture_writer_splice_from_fd (dest, self->fd, error);
+  ret = sysprof_capture_writer_cat (dest, reader, error);
 
-  /* Now reset or file-descriptor position (it should be the same */
-  if (pos != lseek (self->fd, pos, SEEK_SET))
-    {
-      ret = FALSE;
-      goto handle_errno;
-    }
+  sysprof_capture_reader_unref (reader);
 
   return ret;
-
-handle_errno:
-  g_set_error (error,
-               G_FILE_ERROR,
-               g_file_error_from_errno (errno),
-               "%s", g_strerror (errno));
-
-  return FALSE;
 }
 
 /**
@@ -1109,25 +892,21 @@ sysprof_capture_writer_create_reader (SysprofCaptureWriter  *self,
                                       GError               **error)
 {
   SysprofCaptureReader *ret;
+  int fd;
   int copy;
 
   g_return_val_if_fail (self != NULL, NULL);
-  g_return_val_if_fail (self->fd != -1, NULL);
+  g_return_val_if_fail (self->writer != NULL, NULL);
 
-  if (!sysprof_capture_writer_flush (self))
-    {
-      g_set_error (error,
-                   G_FILE_ERROR,
-                   g_file_error_from_errno (errno),
-                   "%s", g_strerror (errno));
-      return NULL;
-    }
+  sysprof_capture_writer_flush (self);
+
+  fd = mmap_writer_get_fd (self->writer);
 
   /*
    * We don't care about the write position, since the reader
    * uses positioned reads.
    */
-  if (-1 == (copy = dup (self->fd)))
+  if (-1 == (copy = dup (fd)))
     return NULL;
 
   if ((ret = sysprof_capture_reader_new_from_fd (copy, error)))
@@ -1173,9 +952,7 @@ sysprof_capture_writer_define_counters (SysprofCaptureWriter        *self,
     return TRUE;
 
   len = sizeof *def + (sizeof *counters * n_counters);
-
-  def = (SysprofCaptureCounterDefine *)sysprof_capture_writer_allocate (self, &len);
-  if (!def)
+  if (!(def = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&def->frame,
@@ -1234,12 +1011,9 @@ sysprof_capture_writer_set_counters (SysprofCaptureWriter             *self,
 
   len = sizeof *set + (n_groups * sizeof (SysprofCaptureCounterValues));
 
-  set = (SysprofCaptureCounterSet *)sysprof_capture_writer_allocate (self, &len);
-  if (!set)
+  if (!(set = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
-  memset (set, 0, len);
-
   sysprof_capture_writer_frame_init (&set->frame,
                                      len,
                                      cpu,
@@ -1304,27 +1078,15 @@ _sysprof_capture_writer_set_time_range (SysprofCaptureWriter *self,
                                         gint64                start_time,
                                         gint64                end_time)
 {
-  ssize_t ret;
+  SysprofCaptureFileHeader *header;
 
   g_assert (self != NULL);
 
-do_start:
-  ret = _sysprof_pwrite (self->fd,
-                         &start_time,
-                         sizeof (start_time),
-                         G_STRUCT_OFFSET (SysprofCaptureFileHeader, time));
-
-  if (ret < 0 && errno == EAGAIN)
-    goto do_start;
-
-do_end:
-  ret = _sysprof_pwrite (self->fd,
-                         &end_time,
-                         sizeof (end_time),
-                         G_STRUCT_OFFSET (SysprofCaptureFileHeader, end_time));
-
-  if (ret < 0 && errno == EAGAIN)
-    goto do_end;
+  if ((header = mmap_writer_get_file_header (self->writer)))
+    {
+      header->time = start_time;
+      header->end_time = end_time;
+    }
 
   return TRUE;
 }
@@ -1356,7 +1118,7 @@ sysprof_capture_writer_get_buffer_size (SysprofCaptureWriter *self)
 {
   g_return_val_if_fail (self != NULL, 0);
 
-  return self->len;
+  return mmap_writer_get_buffer_size (self->writer);
 }
 
 gboolean
@@ -1382,8 +1144,7 @@ sysprof_capture_writer_add_log (SysprofCaptureWriter *self,
   message_len = strlen (message) + 1;
 
   len = sizeof *ev + message_len;
-  ev = (SysprofCaptureLog *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -1420,8 +1181,7 @@ sysprof_capture_writer_add_file (SysprofCaptureWriter *self,
   g_assert (self != NULL);
 
   len = sizeof *ev + data_len;
-  ev = (SysprofCaptureFileChunk *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -1531,8 +1291,7 @@ sysprof_capture_writer_add_allocation (SysprofCaptureWriter  *self,
   g_assert (backtrace_func != NULL);
 
   len = sizeof *ev + (MAX_UNWIND_DEPTH * sizeof (SysprofCaptureAddress));
-  ev = (SysprofCaptureAllocation *)sysprof_capture_writer_allocate (self, &len);
-  if (!ev)
+  if (!(ev = sysprof_capture_writer_allocate (self, &len)))
     return FALSE;
 
   sysprof_capture_writer_frame_init (&ev->frame,
@@ -1557,8 +1316,14 @@ sysprof_capture_writer_add_allocation (SysprofCaptureWriter  *self,
     {
       gsize diff = (sizeof (SysprofCaptureAddress) * (MAX_UNWIND_DEPTH - ev->n_addrs));
 
-      ev->frame.len -= diff;
-      self->pos -= diff;
+      /* We can simply rewind the length to what we used here because
+       * SYSPROF_CAPTURE_ALIGN matches sizeof(SysprofCaptureAddress).
+       */
+      if (diff > 0)
+        {
+          ev->frame.len -= diff;
+          mmap_writer_rewind (self->writer, diff);
+        }
     }
 
   self->stat.frame_count[SYSPROF_CAPTURE_FRAME_ALLOCATION]++;
@@ -1586,7 +1351,7 @@ sysprof_capture_writer_add_allocation_copy (SysprofCaptureWriter        *self,
     n_addrs = 0xFFF;
 
   len = sizeof *ev + (n_addrs * sizeof (SysprofCaptureAddress));
-  ev = (SysprofCaptureAllocation *)sysprof_capture_writer_allocate (self, &len);
+  ev = sysprof_capture_writer_allocate (self, &len);
   if (!ev)
     return FALSE;
 
diff --git a/src/libsysprof-capture/sysprof-capture-writer.h b/src/libsysprof-capture/sysprof-capture-writer.h
index 657e726..4eeb18b 100644
--- a/src/libsysprof-capture/sysprof-capture-writer.h
+++ b/src/libsysprof-capture/sysprof-capture-writer.h
@@ -237,10 +237,6 @@ gboolean              sysprof_capture_writer_cat                             (Sy
                                                                               SysprofCaptureReader           
   *reader,
                                                                               GError                         
  **error);
 G_GNUC_INTERNAL
-gboolean              _sysprof_capture_writer_splice_from_fd                 (SysprofCaptureWriter           
   *self,
-                                                                              int                            
    fd,
-                                                                              GError                         
  **error) G_GNUC_INTERNAL;
-G_GNUC_INTERNAL
 gboolean              _sysprof_capture_writer_set_time_range                 (SysprofCaptureWriter           
   *self,
                                                                               gint64                         
    start_time,
                                                                               gint64                         
    end_time) G_GNUC_INTERNAL;
diff --git a/src/tests/test-capture.c b/src/tests/test-capture.c
index 64d6f2c..a13954a 100644
--- a/src/tests/test-capture.c
+++ b/src/tests/test-capture.c
@@ -214,7 +214,7 @@ test_reader_basic (void)
     }
 
   {
-    SysprofCaptureCounter counters[10];
+    SysprofCaptureCounter counters[10] = {0};
     guint base = sysprof_capture_writer_request_counter (writer, G_N_ELEMENTS (counters));
 
     t = SYSPROF_CAPTURE_CURRENT_TIME;
@@ -239,7 +239,7 @@ test_reader_basic (void)
     const SysprofCaptureCounterDefine *def;
 
     def = sysprof_capture_reader_read_counter_define (reader);
-    g_assert (def != NULL);
+    g_assert_nonnull (def);
     g_assert_cmpint (def->n_counters, ==, 10);
 
     for (i = 0; i < def->n_counters; i++)
@@ -421,7 +421,16 @@ test_reader_basic (void)
     g_assert_cmpint (buf1len, >, 0);
     g_assert_cmpint (buf2len, >, 0);
 
-    g_assert_cmpint (buf1len, ==, buf2len);
+    /* Make sure the sizes match or else there is only zero padding
+     * at the end of our capture file that is truncated from the
+     * backup file.
+     */
+    if (buf1len != buf2len)
+      {
+        for (gsize j = buf1len; j < buf2len; j++)
+          g_assert_cmpint (buf2[j], ==, 0);
+      }
+
     g_assert_true (0 == memcmp (buf1, buf2, buf1len));
   }
 
@@ -544,7 +553,9 @@ test_reader_splice (void)
   r = sysprof_capture_reader_peek_type (reader, &type);
   g_assert_cmpint (r, ==, FALSE);
 
+  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
   r = sysprof_capture_reader_splice (reader, writer2, &error);
+  G_GNUC_END_IGNORE_DEPRECATIONS
   g_assert_no_error (error);
   g_assert_cmpint (r, ==, TRUE);
 
@@ -831,6 +842,7 @@ test_reader_writer_cat_jitmap (void)
   const SysprofCaptureSample *sample;
   GError *error = NULL;
   SysprofCaptureAddress addrs[20];
+  GHashTable *ht;
   gboolean r;
 
   writer1 = sysprof_capture_writer_new ("jitmap1.syscap", 0);
@@ -886,11 +898,13 @@ test_reader_writer_cat_jitmap (void)
   reader = sysprof_capture_writer_create_reader (res, &error);
   g_assert_no_error (error);
   g_assert_nonnull (reader);
-  g_hash_table_unref (sysprof_capture_reader_read_jitmap (reader));
   sample = sysprof_capture_reader_read_sample (reader);
   g_assert_cmpint (sample->frame.pid, ==, getpid ());
   g_assert_cmpint (sample->n_addrs, ==, G_N_ELEMENTS (addrs));
   g_assert_cmpint (sample->addrs[0], !=, sample->addrs[1]);
+  ht = sysprof_capture_reader_read_jitmap (reader);
+  g_assert_nonnull (ht);
+  g_clear_pointer (&ht, g_hash_table_unref);
   sysprof_capture_reader_unref (reader);
 
   sysprof_capture_writer_unref (res);
diff --git a/src/tools/sysprof-dump.c b/src/tools/sysprof-dump.c
index 8b50f54..d04f163 100644
--- a/src/tools/sysprof-dump.c
+++ b/src/tools/sysprof-dump.c
@@ -35,10 +35,10 @@ main (gint argc,
       gchar *argv[])
 {
   g_autoptr(GOptionContext) context = g_option_context_new ("- dump capture data");
+  g_autoptr(GHashTable) ctrtypes = NULL;
   g_autoptr(GError) error = NULL;
   SysprofCaptureReader *reader;
   SysprofCaptureFrameType type;
-  GHashTable *ctrtypes;
   gint64 begin_time;
   gint64 end_time;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]