[libdazzle] tools: add utility to dump program counters



commit 2a19d8440e2394ed64fc53e0404bd8dd82685a6f
Author: Christian Hergert <chergert redhat com>
Date:   Tue Jun 13 22:40:29 2017 -0700

    tools: add utility to dump program counters

 meson.build                  |    1 +
 meson_options.txt            |    3 +
 tools/dazzle-list-counters.c |  110 ++++++++++++++++++++++++++++++++++++++++++
 tools/meson.build            |    8 +++
 4 files changed, 122 insertions(+), 0 deletions(-)
---
diff --git a/meson.build b/meson.build
index 6cc128b..bbc054c 100644
--- a/meson.build
+++ b/meson.build
@@ -96,6 +96,7 @@ endif
 gnome = import('gnome')
 
 subdir('src')
+subdir('tools')
 subdir('tests')
 subdir('examples/app')
 
diff --git a/meson_options.txt b/meson_options.txt
index 9bb858b..0e4abf6 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -6,6 +6,9 @@ option('enable_rdtscp', type: 'boolean', value: false,
   description: 'Use intel rdtscp haswell instruction for performance counters'
 )
 
+option('enable_tools', type: 'boolean', value: true,
+       description: 'Whether helper tools should be installed')
+
 # Support for multiple languages
 option('with_introspection', type: 'boolean', value: true)
 option('with_vapi', type: 'boolean', value: true)
diff --git a/tools/dazzle-list-counters.c b/tools/dazzle-list-counters.c
new file mode 100644
index 0000000..4f88335
--- /dev/null
+++ b/tools/dazzle-list-counters.c
@@ -0,0 +1,110 @@
+/* dazzle-list-counters.c
+ *
+ * Copyright (C) 2015-2017 Christian Hergert <christian hergert me>
+ *
+ * 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/>.
+ */
+
+#include <dazzle.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static void
+foreach_cb (DzlCounter *counter,
+            gpointer    user_data)
+{
+  guint *n_counters = user_data;
+
+  (*n_counters)++;
+
+  g_print ("%-20s : %-32s : %20"G_GINT64_FORMAT" : %-s\n",
+           counter->category,
+           counter->name,
+           dzl_counter_get (counter),
+           counter->description);
+}
+
+static gboolean
+int_parse_with_range (gint        *value,
+                      gint         lower,
+                      gint         upper,
+                      const gchar *str)
+{
+  gint64 v64;
+
+  g_assert (value);
+  g_assert (lower <= upper);
+
+  v64 = g_ascii_strtoll (str, NULL, 10);
+
+  if (((v64 == G_MININT64) || (v64 == G_MAXINT64)) && (errno == ERANGE))
+    return FALSE;
+
+  if ((v64 < lower) || (v64 > upper))
+    return FALSE;
+
+  *value = (gint)v64;
+
+  return TRUE;
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  DzlCounterArena *arena;
+  guint n_counters = 0;
+  gint pid;
+
+  if (argc != 2)
+    {
+      fprintf (stderr, "usage: %s [PID | SHM_PATH]\n", argv [0]);
+      return EXIT_FAILURE;
+    }
+
+  if (g_str_has_prefix (argv [1], "/dev/shm/DzlCounters-"))
+    argv [1] += strlen ("/dev/shm/DzlCounters-");
+
+  if (!int_parse_with_range (&pid, 1, G_MAXUSHORT, argv [1]))
+    {
+      fprintf (stderr, "usage: %s <pid>\n", argv [0]);
+      return EXIT_FAILURE;
+    }
+
+  arena = dzl_counter_arena_new_for_pid (pid);
+
+  if (!arena)
+    {
+      fprintf (stderr, "Failed to access counters for process %u.\n", (int)pid);
+      return EXIT_FAILURE;
+    }
+
+  g_print ("%-20s : %-32s : %20s : %-72s\n",
+           "      Category",
+           "             Name", "Value", "Description");
+  g_print ("-------------------- : "
+           "-------------------------------- : "
+           "-------------------- : "
+           "------------------------------------------------------------------------\n");
+  dzl_counter_arena_foreach (arena, foreach_cb, &n_counters);
+  g_print ("-------------------- : "
+           "-------------------------------- : "
+           "-------------------- : "
+           "------------------------------------------------------------------------\n");
+  g_print ("Discovered %u counters\n", n_counters);
+
+  return EXIT_SUCCESS;
+}
diff --git a/tools/meson.build b/tools/meson.build
new file mode 100644
index 0000000..341d566
--- /dev/null
+++ b/tools/meson.build
@@ -0,0 +1,8 @@
+if get_option('enable_tools')
+
+dazzle_list_counters = executable('dazzle-list-counters', 'dazzle-list-counters.c',
+  dependencies: libdazzle_deps + [libdazzle_dep],
+       install: true,
+)
+
+endif


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