[glib: 1/6] gtrace: Add sysprof tracing support infrastructure



commit fa13c41da7fb03a710bfd8840cae4bb57cf14829
Author: Philip Withnall <withnall endlessm com>
Date:   Thu Jun 25 22:08:05 2020 +0100

    gtrace: Add sysprof tracing support infrastructure
    
    Add some internal wrappers around sysprof tracing, so that it can be
    used throughout GLib without exposing all the details of sysprof
    internally.
    
    This adds an optional dependency on `libsysprof-capture-4`. sysprof
    support is disabled without it.
    
    This depends on the GLib dependency of `libsysprof-capture` being
    dropped in https://gitlab.gnome.org/GNOME/sysprof/-/merge_requests/30,
    which has bumped the soname of `libsysprof-capture` and added subproject
    support.
    
    The next few commits will add marks that trace out each `GMainContext`
    iteration and each `GSource` `check`/`prepare`/`dispatch` call.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 docs/reference/glib/meson.build |  1 +
 glib/gtrace-private.h           | 70 +++++++++++++++++++++++++++++
 glib/gtrace.c                   | 98 +++++++++++++++++++++++++++++++++++++++++
 glib/meson.build                | 20 ++++++++-
 meson_options.txt               |  5 +++
 subprojects/sysprof.wrap        |  4 ++
 6 files changed, 197 insertions(+), 1 deletion(-)
---
diff --git a/docs/reference/glib/meson.build b/docs/reference/glib/meson.build
index 62d95f78d..43c273855 100644
--- a/docs/reference/glib/meson.build
+++ b/docs/reference/glib/meson.build
@@ -22,6 +22,7 @@ if get_option('gtk_doc')
     'gprintfint.h',
     'gmirroringtable.h',
     'gscripttable.h',
+    'gtrace-private.h',
     'glib-mirroring-tab',
     'gnulib',
     'pcre',
diff --git a/glib/gtrace-private.h b/glib/gtrace-private.h
new file mode 100644
index 000000000..24550152d
--- /dev/null
+++ b/glib/gtrace-private.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright © 2020 Endless Mobile, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Philip Withnall <withnall endlessm com>
+ */
+
+#pragma once
+
+#ifdef HAVE_SYSPROF
+#include <sysprof-capture.h>
+#endif
+
+#include "gmem.h"
+#include "gmacros.h"
+
+G_BEGIN_DECLS
+
+/*
+ * G_TRACE_CURRENT_TIME:
+ *
+ * Get the current time, in nanoseconds since the tracing epoch. This (and only
+ * this) is suitable for passing to tracing functions like g_trace_mark(). It is
+ * not suitable for other timekeeping.
+ *
+ * The tracing epoch is implementation defined, but is guaranteed to be
+ * unchanged within the lifetime of each thread. It is not comparable across
+ * threads or process instances.
+ *
+ * If tracing support is disabled, this evaluates to `0`.
+ *
+ * Since: 2.66
+ */
+#ifdef HAVE_SYSPROF
+#define G_TRACE_CURRENT_TIME SYSPROF_CAPTURE_CURRENT_TIME
+#else
+#define G_TRACE_CURRENT_TIME 0
+#endif
+
+void (g_trace_mark) (gint64       begin_time_nsec,
+                     gint64       duration_nsec,
+                     const gchar *group,
+                     const gchar *name,
+                     const gchar *message_format,
+                     ...);
+
+#ifndef HAVE_SYSPROF
+/* Optimise the whole call out */
+#if defined(G_HAVE_ISO_VARARGS)
+#define g_trace_mark(b, d, g, n, m, ...)
+#elif defined(G_HAVE_GNUC_VARARGS)
+#define g_trace_mark(b, d, g, n, m...)
+#else
+/* no varargs macro support; the call will have to be optimised out by the compiler */
+#endif
+#endif
+
+G_END_DECLS
diff --git a/glib/gtrace.c b/glib/gtrace.c
new file mode 100644
index 000000000..29726778d
--- /dev/null
+++ b/glib/gtrace.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright © 2020 Endless Mobile, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Philip Withnall <withnall endlessm com>
+ */
+
+/*
+ * SECTION:trace
+ * @Title: Performance tracing
+ * @Short_description: Functions for measuring and tracing performance
+ *
+ * The performance tracing functions allow for the performance of code using
+ * GLib to be measured by passing metrics from the current process to an
+ * external measurement process such as `sysprof-cli` or `sysprofd`.
+ *
+ * They are designed to execute quickly, especially in the common case where no
+ * measurement process is connected. They are guaranteed to not block the caller
+ * and are guaranteed to have zero runtime cost if tracing support is disabled
+ * at configure time.
+ *
+ * Tracing information can be provided as ‘marks’ with a start time and
+ * duration; or as marks with a start time and no duration. Marks with a
+ * duration are intended to show the execution time of a piece of code. Marks
+ * with no duration are intended to show an instantaneous performance problem,
+ * such as an unexpectedly large allocation, or that a slow path has been taken
+ * in some code.
+ *
+ * |[<!-- language="C" -->
+ * gint64 begin_time_nsec G_GNUC_UNUSED;
+ *
+ * begin_time_nsec = G_TRACE_CURRENT_TIME;
+ *
+ * // some code which might take a while
+ *
+ * g_trace_mark (begin_time_nsec, G_TRACE_CURRENT_TIME - begin_time_nsec,
+ *               "GLib", "GSource.dispatch",
+ *               "%s ⇒ %s", g_source_get_name (source), need_destroy ? "destroy" : "keep");
+ * ]|
+ *
+ * The tracing API is currently internal to GLib.
+ *
+ * Since: 2.66
+ */
+
+#include "config.h"
+
+#include "gtrace-private.h"
+
+#include <stdarg.h>
+
+/*
+ * g_trace_mark:
+ * @begin_time_nsec: start time of the mark, as returned by %G_TRACE_CURRENT_TIME
+ * @duration_nsec: duration of the mark, in nanoseconds
+ * @group: name of the group for categorising this mark
+ * @name: name of the mark
+ * @message_format: format for the detailed message for the mark, in `printf()` format
+ * @...: arguments to substitute into @message_format; none of these should have
+ *    side effects
+ *
+ * Add a mark to the trace, starting at @begin_time_nsec and having length
+ * @duration_nsec (which may be zero). The @group should typically be `GLib`,
+ * and the @name should concisely describe the call site.
+ *
+ * All of the arguments to this function must not have side effects, as the
+ * entire function call may be dropped if sysprof support is not available.
+ *
+ * Since: 2.66
+ */
+void
+(g_trace_mark) (gint64       begin_time_nsec,
+                gint64       duration_nsec,
+                const gchar *group,
+                const gchar *name,
+                const gchar *message_format,
+                ...)
+{
+#ifdef HAVE_SYSPROF
+  va_list args;
+
+  va_start (args, message_format);
+  sysprof_collector_mark_vprintf (begin_time_nsec, duration_nsec, group, name, message_format, args);
+  va_end (args);
+#endif  /* HAVE_SYSPROF */
+}
diff --git a/glib/meson.build b/glib/meson.build
index 149d62f01..7e0edb905 100644
--- a/glib/meson.build
+++ b/glib/meson.build
@@ -7,6 +7,22 @@ if not use_system_pcre
   subdir('pcre')
 endif
 
+# libsysprof-capture support
+libsysprof_capture_dep = dependency('sysprof-capture-4',
+  required: get_option('sysprof'),
+  default_options: [
+    'enable_examples=false',
+    'enable_gtk=false',
+    'enable_tests=false',
+    'enable_tools=false',
+    'libsysprof=false',
+    'with_sysprofd=none',
+    'help=false',
+  ],
+  fallback: ['sysprof', 'libsysprof_capture_dep'],
+)
+glib_conf.set('HAVE_SYSPROF', libsysprof_capture_dep.found())
+
 # TODO: gnulib_objects, pcre_objects and pcre_deps are a workaround for
 # <https://github.com/mesonbuild/meson/issues/3934> and
 # <https://github.com/mesonbuild/meson/issues/3937>. When we can depend
@@ -275,6 +291,8 @@ glib_sources = files(
   'gthreadpool.c',
   'gtimer.c',
   'gtimezone.c',
+  'gtrace.c',
+  'gtrace-private.h',
   'gtranslit.c',
   'gtrashstack.c',
   'gtree.c',
@@ -357,7 +375,7 @@ libglib = library('glib-2.0',
   # intl.lib is not compatible with SAFESEH
   link_args : [noseh_link_args, glib_link_flags, win32_ldflags],
   include_directories : configinc,
-  dependencies : pcre_deps + [thread_dep, librt] + libintl_deps + libiconv + platform_deps + 
gnulib_libm_dependency,
+  dependencies : pcre_deps + [thread_dep, librt] + libintl_deps + libiconv + platform_deps + 
gnulib_libm_dependency + [libsysprof_capture_dep],
   c_args : glib_c_args,
   objc_args : glib_c_args,
 )
diff --git a/meson_options.txt b/meson_options.txt
index 80a1d9d8f..c6f228a01 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -59,6 +59,11 @@ option('tapset_install_dir',
        value : '',
        description : 'path where systemtap tapsets are installed')
 
+option('sysprof',
+       type : 'feature',
+       value : 'disabled',
+       description : 'include tracing support for sysprof')
+
 option('gtk_doc',
        type : 'boolean',
        value : false,
diff --git a/subprojects/sysprof.wrap b/subprojects/sysprof.wrap
new file mode 100644
index 000000000..14874880e
--- /dev/null
+++ b/subprojects/sysprof.wrap
@@ -0,0 +1,4 @@
+[wrap-git]
+directory=sysprof
+url=https://gitlab.gnome.org/GNOME/sysprof.git
+revision=6b1cd7a722fcebae1ac392562c47957477ade8bf


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