[mutter/gbsneto/tracing: 23/30] cogl: Add libsysprof capture based tracing



commit 8e9df75162d729a2486388f1cccd46595a80c793
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Tue May 15 16:31:29 2018 +0100

    cogl: Add libsysprof capture based tracing
    
    Add the ability to add tracing instrumentation to the code. When
    enabled, trace entries will generate a file with timing information
    that will be processable by sysprof for generating visualization of
    traces over time.
    
    While enabled by default at compile time, it is possible to disable the
    expansion of the macros completely by passing --disable-tracing to
    ./configure.
    
    Tracing is so far only actually done if actually enabled on explicitly
    specified threads.
    
    This will be used by Mutter passing the write end of a pipe, where the
    read end is sent to Sysprof itself via the D-Bus method 'Capture()'.
    
    By passing that, we have to detect EPIPE that is sent when Sysprof stops
    recording. To do that, SEGPIPE is ignored.
    
    https://gitlab.gnome.org/GNOME/mutter/merge_requests/197

 cogl/cogl-config.h.meson |   3 +
 cogl/cogl/cogl-trace.c   | 181 +++++++++++++++++++++++++++++++++++++++++++++++
 cogl/cogl/cogl-trace.h   | 138 ++++++++++++++++++++++++++++++++++++
 cogl/cogl/meson.build    |   2 +
 cogl/meson.build         |   7 ++
 config.h.meson           |   3 +
 meson.build              |   7 ++
 meson_options.txt        |   6 ++
 8 files changed, 347 insertions(+)
---
diff --git a/cogl/cogl-config.h.meson b/cogl/cogl-config.h.meson
index 56946b299..f2d46b259 100644
--- a/cogl/cogl-config.h.meson
+++ b/cogl/cogl-config.h.meson
@@ -4,6 +4,9 @@
 /* Have GLES 2.0 for rendering */
 #mesondefine HAVE_COGL_GLES2
 
+/* Building with Sysprof profiling suport */
+#mesondefine HAVE_TRACING
+
 /* Enable unit tests */
 #mesondefine ENABLE_UNIT_TESTS
 
diff --git a/cogl/cogl/cogl-trace.c b/cogl/cogl/cogl-trace.c
new file mode 100644
index 000000000..7096cdddb
--- /dev/null
+++ b/cogl/cogl/cogl-trace.c
@@ -0,0 +1,181 @@
+/*
+ * Copyright 2018 Red Hat, Inc.
+ *
+ * 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 2 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 "cogl-config.h"
+
+#ifdef HAVE_TRACING
+
+#include "cogl/cogl-trace.h"
+
+#include <sysprof-capture.h>
+#include <syscall.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#define COGL_TRACE_OUTPUT_FILE "cogl-trace-sp-capture.syscap"
+
+__thread CoglTraceThreadContext *cogl_trace_thread_context;
+CoglTraceContext *cogl_trace_context;
+GMutex cogl_trace_mutex;
+
+static CoglTraceContext *
+cogl_trace_context_new (int fd)
+{
+  CoglTraceContext *context;
+  SpCaptureWriter *writer;
+
+  g_debug ("Initializing trace context with fd=%d", fd);
+
+  if (fd == -1)
+    writer = sp_capture_writer_new (COGL_TRACE_OUTPUT_FILE,
+                                    4096 * 4);
+  else
+    writer = sp_capture_writer_new_from_fd (fd, 4096 * 4);
+
+  context = g_new0 (CoglTraceContext, 1);
+  context->writer = writer;
+  return context;
+}
+
+static void
+ensure_trace_context (int fd)
+{
+  static GMutex mutex;
+
+  g_mutex_lock (&mutex);
+  if (!cogl_trace_context)
+    cogl_trace_context = cogl_trace_context_new (fd);
+  g_mutex_unlock (&mutex);
+}
+
+static void
+ensure_sigpipe_ignored (void)
+{
+  signal (SIGPIPE, SIG_IGN);
+}
+
+static CoglTraceThreadContext *
+cogl_trace_thread_context_new (void)
+{
+  CoglTraceThreadContext *thread_context;
+  pid_t tid;
+
+  tid = (pid_t) syscall (SYS_gettid);
+
+  thread_context = g_new0 (CoglTraceThreadContext, 1);
+  thread_context->cpu_id = -1;
+  thread_context->pid = getpid ();
+  thread_context->group = g_strdup_printf ("t:%d", tid);
+
+  return thread_context;
+}
+
+static gboolean
+enable_tracing_idle_callback (gpointer user_data)
+{
+  int fd = GPOINTER_TO_INT (user_data);
+
+  ensure_sigpipe_ignored ();
+  ensure_trace_context (fd);
+
+  if (cogl_trace_thread_context)
+    {
+      g_warning ("Tracing already enabled");
+      return G_SOURCE_REMOVE;
+    }
+
+  cogl_trace_thread_context = cogl_trace_thread_context_new ();
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+cogl_trace_thread_context_free (CoglTraceThreadContext *thread_context)
+{
+  g_free (thread_context->group);
+  g_free (thread_context);
+}
+
+static gboolean
+disable_tracing_idle_callback (gpointer user_data)
+{
+  CoglTraceContext *trace_context;
+
+  if (!cogl_trace_thread_context)
+    {
+      g_warning ("Tracing not enabled");
+      return G_SOURCE_REMOVE;
+    }
+
+  cogl_trace_thread_context_free (cogl_trace_thread_context);
+  cogl_trace_thread_context = NULL;
+
+  g_mutex_lock (&cogl_trace_mutex);
+  trace_context = cogl_trace_context;
+  sp_capture_writer_flush (trace_context->writer);
+  g_mutex_unlock (&cogl_trace_mutex);
+
+  return G_SOURCE_REMOVE;
+}
+
+void
+cogl_set_tracing_enabled_on_thread (GMainContext *main_context,
+                                    int           fd)
+{
+  GSource *source;
+
+  source = g_idle_source_new ();
+
+  g_source_set_callback (source, enable_tracing_idle_callback, GINT_TO_POINTER (fd), NULL);
+
+  g_source_attach (source, main_context);
+  g_source_unref (source);
+}
+
+void
+cogl_set_tracing_disabled_on_thread (GMainContext *main_context)
+{
+  GSource *source;
+
+  source = g_idle_source_new ();
+
+  g_source_set_callback (source, disable_tracing_idle_callback, NULL, NULL);
+
+  g_source_attach (source, main_context);
+  g_source_unref (source);
+}
+
+#else
+
+#include <string.h>
+#include <stdio.h>
+
+void
+cogl_set_tracing_enabled_on_thread (void *data,
+                                    int   fd)
+{
+  fprintf (stderr, "Tracing not enabled");
+}
+
+void
+cogl_set_tracing_disabled_on_thread (void *data)
+{
+  fprintf (stderr, "Tracing not enabled");
+}
+
+#endif /* HAVE_TRACING */
diff --git a/cogl/cogl/cogl-trace.h b/cogl/cogl/cogl-trace.h
new file mode 100644
index 000000000..8b2c1ad95
--- /dev/null
+++ b/cogl/cogl/cogl-trace.h
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2018 Red Hat, Inc.
+ *
+ * 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 2 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/>.
+ *
+ */
+
+#ifndef COGL_TRACE_H
+#define COGL_TRACE_H
+
+#include "cogl-config.h"
+
+#ifdef HAVE_TRACING
+
+#include <glib.h>
+#include <capture/sp-capture-writer.h>
+#include <sp-clock.h>
+#include <stdint.h>
+#include <errno.h>
+
+typedef struct _CoglTraceContext
+{
+  SpCaptureWriter *writer;
+} CoglTraceContext;
+
+typedef struct _CoglTraceThreadContext
+{
+  int cpu_id;
+  GPid pid;
+  char *group;
+} CoglTraceThreadContext;
+
+typedef struct _CoglTraceHead
+{
+  SpTimeStamp begin_time;
+  const char *name;
+} CoglTraceHead;
+
+extern __thread CoglTraceThreadContext *cogl_trace_thread_context;
+extern CoglTraceContext *cogl_trace_context;
+extern GMutex cogl_trace_mutex;
+
+void cogl_set_tracing_enabled_on_thread (GMainContext *main_context,
+                                         int           fd);
+
+void cogl_set_tracing_disabled_on_thread (GMainContext *main_context);
+
+static inline void
+cogl_trace_begin (CoglTraceHead *head,
+                  const char    *name)
+{
+  head->begin_time = g_get_monotonic_time () * 1000;
+  head->name = name;
+}
+
+static inline void
+cogl_trace_end (CoglTraceHead *head)
+{
+  SpTimeStamp end_time;
+  CoglTraceContext *trace_context;
+  CoglTraceThreadContext *trace_thread_context;
+
+  end_time = g_get_monotonic_time () * 1000;
+  trace_context = cogl_trace_context;
+  trace_thread_context = cogl_trace_thread_context;
+
+  g_mutex_lock (&cogl_trace_mutex);
+  if (!sp_capture_writer_add_mark (trace_context->writer,
+                                   head->begin_time,
+                                   trace_thread_context->cpu_id,
+                                   trace_thread_context->pid,
+                                   (uint64_t) end_time - head->begin_time,
+                                   trace_thread_context->group,
+                                   head->name,
+                                   NULL))
+    {
+      /* XXX: g_main_context_get_thread_default() might be wrong, it probably
+       * needs to store the GMainContext in CoglTraceThreadContext when creating
+       * and use it here.
+       */
+      if (errno == EPIPE)
+        cogl_set_tracing_disabled_on_thread (g_main_context_get_thread_default ());
+    }
+  g_mutex_unlock (&cogl_trace_mutex);
+}
+
+static inline void
+cogl_auto_trace_end_helper (CoglTraceHead **head)
+{
+  if (*head)
+    cogl_trace_end (*head);
+}
+
+#define COGL_TRACE_BEGIN(Name) \
+  CoglTraceHead CoglTrace##Name = { 0 }; \
+  if (cogl_trace_thread_context) \
+    cogl_trace_begin (&CoglTrace##Name, #Name); \
+
+#define COGL_TRACE_END(Name)\
+  if (cogl_trace_thread_context) \
+    cogl_trace_end (&CoglTrace##Name);
+
+#define COGL_TRACE_BEGIN_SCOPED(Name) \
+  CoglTraceHead CoglTrace##Name = { 0 }; \
+  __attribute__((cleanup (cogl_auto_trace_end_helper))) \
+    CoglTraceHead *ScopedCoglTrace##Name = NULL; \
+  if (cogl_trace_thread_context) \
+    { \
+      cogl_trace_begin (&CoglTrace##Name, #Name); \
+      ScopedCoglTrace##Name = &CoglTrace##Name; \
+    }
+
+#else /* HAVE_TRACING */
+
+#include <stdio.h>
+
+#define COGL_TRACE_BEGIN(Name) (void) 0
+#define COGL_TRACE_END(Name) (void) 0
+#define COGL_TRACE_BEGIN_SCOPED(Name) (void) 0
+
+void cogl_set_tracing_enabled_on_thread (void *data, int fd);
+
+void cogl_set_tracing_disabled_on_thread (void *data);
+
+#endif /* HAVE_TRACING */
+
+#endif /* COGL_TRACE_H */
diff --git a/cogl/cogl/meson.build b/cogl/cogl/meson.build
index 527c50528..95ba2528d 100644
--- a/cogl/cogl/meson.build
+++ b/cogl/cogl/meson.build
@@ -301,6 +301,8 @@ cogl_sources = [
   'cogl-blend-string.c',
   'cogl-blend-string.h',
   'cogl-debug.c',
+  'cogl-trace.c',
+  'cogl-trace.h',
   'cogl-sub-texture-private.h',
   'cogl-texture-private.h',
   'cogl-texture-2d-private.h',
diff --git a/cogl/meson.build b/cogl/meson.build
index 34370ef1a..85fd1d910 100644
--- a/cogl/meson.build
+++ b/cogl/meson.build
@@ -7,6 +7,7 @@ cogl_includepath = include_directories('.', 'cogl')
 cdata = configuration_data()
 cdata.set('HAVE_COGL_GL', have_gl)
 cdata.set('HAVE_COGL_GLES2', have_gles2)
+cdata.set('HAVE_TRACING', have_tracing)
 cdata.set('ENABLE_UNIT_TESTS', have_cogl_tests)
 
 cogl_config_h = configure_file(
@@ -32,6 +33,12 @@ cogl_pkg_private_deps = [
   #uprof_dep,
 ]
 
+if have_tracing
+  cogl_pkg_private_deps += [
+    sysprof_dep,
+  ]
+endif
+
 if have_wayland
   cogl_pkg_deps += [
     wayland_server_dep,
diff --git a/config.h.meson b/config.h.meson
index 70681d774..7f4f9e176 100644
--- a/config.h.meson
+++ b/config.h.meson
@@ -49,6 +49,9 @@
 /* Building with startup notification support */
 #mesondefine HAVE_STARTUP_NOTIFICATION
 
+/* Building with Sysprof profiling suport */
+#mesondefine HAVE_TRACING
+
 /* Path to Xwayland executable */
 #mesondefine XWAYLAND_PATH
 
diff --git a/meson.build b/meson.build
index 01e843d41..f6e5b3f9c 100644
--- a/meson.build
+++ b/meson.build
@@ -262,6 +262,11 @@ if have_tests
   have_installed_tests = get_option('installed_tests')
 endif
 
+have_tracing = get_option('tracing')
+if have_tracing
+  sysprof_dep = dependency('sysprof-2')
+endif
+
 required_functions = [
   'ffs',
   'clz',
@@ -341,6 +346,7 @@ cdata.set('HAVE_LIBWACOM', have_libwacom)
 cdata.set('HAVE_SM', have_sm)
 cdata.set('HAVE_STARTUP_NOTIFICATION', have_startup_notification)
 cdata.set('HAVE_INTROSPECTION', have_introspection)
+cdata.set('HAVE_TRACING', have_tracing)
 
 xkb_base = xkeyboard_config_dep.get_pkgconfig_variable('xkb_base')
 cdata.set_quoted('XKB_BASE', xkb_base)
@@ -406,6 +412,7 @@ output = [
   '        SM....................... ' + have_sm.to_string(),
   '        Startup notification..... ' + have_startup_notification.to_string(),
   '        Introspection............ ' + have_introspection.to_string(),
+  '        Tracing.................. ' + have_tracing.to_string(),
   '',
   '    Tests:',
   '',
diff --git a/meson_options.txt b/meson_options.txt
index 1e122300e..8f74a43c9 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -123,6 +123,12 @@ option('tests',
   description: 'Enable tests globally. Specific test suites can be controlled with core_tests, 
clutter_tests, and cogl_tests'
 )
 
+option('tracing',
+  type: 'boolean',
+  value: true,
+  description: 'Enable Sysprof tracing'
+)
+
 option('installed_tests',
   type: 'boolean',
   value: true,


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