[sysprof/wip/chergert/mem-preload: 18/43] tests: add tool to load memory allocations into stackstash



commit 2bedbe6e00926541e8b1a4ed039e9803a389dced
Author: Christian Hergert <chergert redhat com>
Date:   Mon Feb 3 16:38:31 2020 -0800

    tests: add tool to load memory allocations into stackstash
    
    This could be useful to look at functions by allocation size rather than
    by number of samples.

 src/tests/memory-stack-stash.c | 89 ++++++++++++++++++++++++++++++++++++++++++
 src/tests/meson.build          |  6 +++
 2 files changed, 95 insertions(+)
---
diff --git a/src/tests/memory-stack-stash.c b/src/tests/memory-stack-stash.c
new file mode 100644
index 0000000..e6ec1a1
--- /dev/null
+++ b/src/tests/memory-stack-stash.c
@@ -0,0 +1,89 @@
+/* memory-stack-stash.c
+ *
+ * Copyright 2020 Christian Hergert <chergert redhat 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, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+#include <stddef.h>
+#include <rax.h>
+#include <sysprof.h>
+
+#include "../stackstash.h"
+#include "../stackstash.c"
+
+static void
+memory_stack_stash (SysprofCaptureReader *reader)
+{
+  SysprofCaptureFrameType type;
+  StackStash *stash = stack_stash_new (NULL);
+
+  while (sysprof_capture_reader_peek_type (reader, &type))
+    {
+      if (type == SYSPROF_CAPTURE_FRAME_MEMORY_ALLOC)
+        {
+          const SysprofCaptureMemoryAlloc *ev = sysprof_capture_reader_read_memory_alloc (reader);
+
+          if (ev == NULL)
+            break;
+
+          stack_stash_add_trace (stash, ev->addrs, ev->n_addrs, ev->alloc_size);
+        }
+      else
+        {
+          if (!sysprof_capture_reader_skip (reader))
+            break;
+        }
+    }
+
+  stack_stash_unref (stash);
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  SysprofCaptureReader *reader;
+  const gchar *filename = argv[1];
+  g_autoptr(GError) error = NULL;
+
+  if (argc < 2)
+    {
+      g_printerr ("usage: %s FILENAME\n", argv[0]);
+      return EXIT_FAILURE;
+    }
+
+  /* Set up gettext translations */
+  setlocale (LC_ALL, "");
+  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+  textdomain (GETTEXT_PACKAGE);
+
+  if (!(reader = sysprof_capture_reader_new (filename, &error)))
+    {
+      g_printerr ("%s\n", error->message);
+      return EXIT_FAILURE;
+    }
+
+  memory_stack_stash (reader);
+
+  sysprof_capture_reader_unref (reader);
+
+  return EXIT_SUCCESS;
+}
diff --git a/src/tests/meson.build b/src/tests/meson.build
index 3af19c1..7c41280 100644
--- a/src/tests/meson.build
+++ b/src/tests/meson.build
@@ -76,6 +76,12 @@ cross_thread_frees = executable('cross-thread-frees',
   dependencies: test_deps,
 )
 
+memory_stack_stash = executable('memory-stack-stash',
+  ['memory-stack-stash.c'],
+        c_args: test_cflags,
+  dependencies: test_deps,
+)
+
 show_page_usage = executable('show-page-usage',
   [ 'show-page-usage.c' ],
         c_args: test_cflags,


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