[sysprof/wip/chergert/mmap-writer] mmap: start on mmap writing helper



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

    mmap: start on mmap writing helper
    
    The goal is for this to get used by SysprofCaptureWriter so that we can
    ensure that data is written to underlying files even in the case that
    a thread exits uncleanly (such as from a crash).

 meson.build                          |   4 +
 src/libsysprof-capture/meson.build   |   1 +
 src/libsysprof-capture/mmap-writer.c | 332 +++++++++++++++++++++++++++++++++++
 src/libsysprof-capture/mmap-writer.h |  86 +++++++++
 4 files changed, 423 insertions(+)
---
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..21a413d
--- /dev/null
+++ b/src/libsysprof-capture/mmap-writer.c
@@ -0,0 +1,332 @@
+/* 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"
+
+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;
+
+  /* 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 (MmapWriter *self)
+{
+  g_assert (self != NULL);
+  g_assert (self->n_pages > 0);
+  g_assert (self->page_size > 0);
+
+  if (self->page_map != NULL)
+    {
+      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);
+
+  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_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);
+
+  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++;
+}
+
+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 = sysconf (_SC_PAGESIZE);
+  ret->page_map = NULL;
+  ret->page = 0;
+  ret->n_pages = 16;
+  ret->wr_offset = 0;
+
+  normalize_buffer_size (ret, buffer_size);
+
+  if (!mmap_writer_map (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);
+
+  if (self->fd >= 0)
+    {
+      close (self->fd);
+      self->fd = -1;
+    }
+}
+
+void
+mmap_writer_destroy (MmapWriter *self)
+{
+  if (self == NULL)
+    return;
+
+  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 = (length / self->page_size);
+      if ((req_pages & 0xFFF) != 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_assert (self != NULL);
+
+  fdatasync (self->fd);
+}
diff --git a/src/libsysprof-capture/mmap-writer.h b/src/libsysprof-capture/mmap-writer.h
new file mode 100644
index 0000000..c54ea45
--- /dev/null
+++ b/src/libsysprof-capture/mmap-writer.h
@@ -0,0 +1,86 @@
+/* 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 <glib.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_END_DECLS


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