[gnome-builder/wip/chergert/sysprof-3: 18/18] core: trace function calls to sysprof-capture when available



commit de5c2085fd57901ffb532da881346b148c48142b
Author: Christian Hergert <chergert redhat com>
Date:   Wed May 22 16:30:52 2019 -0700

    core: trace function calls to sysprof-capture when available

 meson.build                    |  9 +++++++
 src/libide/core/ide-debug.h.in | 15 ++++++++++-
 src/libide/core/ide-global.c   | 35 ++++++++++++++++++++++++-
 src/libide/core/ide-private.h  | 39 ++++++++++++++++++++++++++++
 src/main.c                     | 58 ++++++++++++++++++++++++++++++++++++++++++
 src/meson.build                |  4 +++
 6 files changed, 158 insertions(+), 2 deletions(-)
---
diff --git a/meson.build b/meson.build
index bb8afa0a0..783fb5170 100644
--- a/meson.build
+++ b/meson.build
@@ -280,6 +280,15 @@ foreach func: check_functions
   config_h.set(func[0], cc.has_function(func[1]))
 endforeach
 
+#
+# If we have sysprof-capture-3, we can export tracing information
+# to the SYSPROF_TRACE_FD.
+#
+libsysprof_capture = dependency('sysprof-capture-3', version: '3.33.1', static: true, required: false)
+if libsysprof_capture.found()
+  config_h.set10('ENABLE_TRACING_SYSCAP', true)
+endif
+
 configure_file(output: 'config.h', configuration: config_h)
 
 gnome = import('gnome')
diff --git a/src/libide/core/ide-debug.h.in b/src/libide/core/ide-debug.h.in
index 128f96c8b..7d6167b02 100644
--- a/src/libide/core/ide-debug.h.in
+++ b/src/libide/core/ide-debug.h.in
@@ -22,6 +22,8 @@
 #include <execinfo.h>
 #include <glib.h>
 
+#include "ide-version-macros.h"
+
 G_BEGIN_DECLS
 
 /**
@@ -116,6 +118,10 @@ G_BEGIN_DECLS
 #endif
 
 #ifdef IDE_ENABLE_TRACE
+_IDE_EXTERN
+void ide_trace_function (const gchar *strfunc,
+                         gint64       begin_time_usec,
+                         gint64       end_time_usec);
 # define IDE_TRACE_MSG(fmt, ...)                                         \
    g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, "  MSG: %s():%d: " fmt,      \
          G_STRFUNC, __LINE__, ##__VA_ARGS__)
@@ -126,10 +132,14 @@ G_BEGIN_DECLS
    g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, " TODO: %s():%d: %s",        \
          G_STRFUNC, __LINE__, _msg)
 # define IDE_ENTRY                                                       \
+   gint64 __trace_begin_time = g_get_monotonic_time ();                  \
    g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, "ENTRY: %s():%d",            \
-         G_STRFUNC, __LINE__)
+           G_STRFUNC, __LINE__)
 # define IDE_EXIT                                                        \
    G_STMT_START {                                                        \
+      ide_trace_function (G_STRFUNC,                                     \
+                          __trace_begin_time,                            \
+                          g_get_monotonic_time ());                      \
       g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, " EXIT: %s():%d",         \
             G_STRFUNC, __LINE__);                                        \
       return;                                                            \
@@ -142,6 +152,9 @@ G_BEGIN_DECLS
    } G_STMT_END
 # define IDE_RETURN(_r)                                                  \
    G_STMT_START {                                                        \
+      ide_trace_function (G_STRFUNC,                                     \
+                          __trace_begin_time,                            \
+                          g_get_monotonic_time ());                      \
       g_log(G_LOG_DOMAIN, IDE_LOG_LEVEL_TRACE, " EXIT: %s():%d ",        \
             G_STRFUNC, __LINE__);                                        \
       return _r;                                                         \
diff --git a/src/libide/core/ide-global.c b/src/libide/core/ide-global.c
index 46133cc56..8d4e426df 100644
--- a/src/libide/core/ide-global.c
+++ b/src/libide/core/ide-global.c
@@ -32,8 +32,10 @@
 
 #include "../../gconstructor.h"
 
-#include "ide-macros.h"
+#include "ide-debug.h"
 #include "ide-global.h"
+#include "ide-macros.h"
+#include "ide-private.h"
 
 static GThread *main_thread;
 static const gchar *application_id = "org.gnome.Builder";
@@ -232,3 +234,34 @@ ide_gettext (const gchar *message)
     return g_dgettext (GETTEXT_PACKAGE, message);
   return NULL;
 }
+
+static IdeTraceVTable trace_vtable;
+
+void
+_ide_trace_init (IdeTraceVTable *vtable)
+{
+  trace_vtable = *vtable;
+  if (trace_vtable.load)
+    trace_vtable.load ();
+}
+
+void
+_ide_trace_shutdown (void)
+{
+  if (trace_vtable.unload)
+    trace_vtable.unload ();
+  memset (&trace_vtable, 0, sizeof trace_vtable);
+}
+
+void
+ide_trace_function (const gchar *strfunc,
+                    gint64       begin_time_usec,
+                    gint64       end_time_usec)
+{
+  /* In case our clock is not reliable */
+  if (end_time_usec < begin_time_usec)
+    end_time_usec = begin_time_usec;
+
+  if (trace_vtable.function)
+    trace_vtable.function (strfunc, begin_time_usec, end_time_usec);
+}
diff --git a/src/libide/core/ide-private.h b/src/libide/core/ide-private.h
new file mode 100644
index 000000000..1c985acf9
--- /dev/null
+++ b/src/libide/core/ide-private.h
@@ -0,0 +1,39 @@
+/* ide-private.h
+ *
+ * Copyright 2019 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
+ */
+
+#pragma once
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef struct
+{
+  void (*load)     (void);
+  void (*unload)   (void);
+  void (*function) (const gchar    *func,
+                    gint64          begin_time_usec,
+                    gint64          end_time_usec);
+} IdeTraceVTable;
+
+void _ide_trace_init     (IdeTraceVTable *vtable);
+void _ide_trace_shutdown (void);
+
+G_END_DECLS
diff --git a/src/main.c b/src/main.c
index dd9825585..04127aa39 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,13 +31,63 @@
 #include <libide-greeter.h>
 #include <libide-gui.h>
 #include <libide-threading.h>
+#ifdef ENABLE_TRACING_SYSCAP
+# include <sysprof-capture.h>
+#endif
+#include <sched.h>
+#include <unistd.h>
 
 #include "ide-application-private.h"
+#include "ide-debug.h"
 #include "ide-thread-private.h"
 #include "ide-terminal-private.h"
+#include "ide-private.h"
 
 #include "bug-buddy.h"
 
+#ifdef ENABLE_TRACING_SYSCAP
+static SysprofCaptureWriter *trace_writer;
+
+static void
+trace_load (void)
+{
+  sysprof_clock_init ();
+  trace_writer = sysprof_capture_writer_new_from_env (0);
+}
+
+static void
+trace_unload (void)
+{
+  if (trace_writer)
+    {
+      sysprof_capture_writer_flush (trace_writer);
+      g_clear_pointer (&trace_writer, sysprof_capture_writer_unref);
+    }
+}
+
+static void
+trace_function (const gchar    *func,
+                gint64          begin_time_usec,
+                gint64          end_time_usec)
+{
+  if (trace_writer != NULL)
+    sysprof_capture_writer_add_mark (trace_writer,
+                                     begin_time_usec * 1000L,
+                                     sched_getcpu (),
+                                     getpid (),
+                                     (end_time_usec - begin_time_usec) * 1000L,
+                                     "tracing",
+                                     "function",
+                                     func);
+}
+
+static IdeTraceVTable trace_vtable = {
+  trace_load,
+  trace_unload,
+  trace_function,
+};
+#endif
+
 static gboolean
 verbose_cb (const gchar  *option_name,
             const gchar  *value,
@@ -165,6 +215,10 @@ main (gint   argc,
   if (desktop == NULL)
     desktop = "unknown";
 
+#ifdef ENABLE_TRACING_SYSCAP
+  _ide_trace_init (&trace_vtable);
+#endif
+
   g_message ("Initializing with %s desktop and GTK+ %d.%d.%d.",
              desktop,
              gtk_get_major_version (),
@@ -194,5 +248,9 @@ main (gint   argc,
   /* Cleanup GtkSourceView singletons to improve valgrind output */
   gtk_source_finalize ();
 
+#ifdef ENABLE_TRACING_SYSCAP
+  _ide_trace_shutdown ();
+#endif
+
   return ret;
 }
diff --git a/src/meson.build b/src/meson.build
index 6e9127759..bc7f91841 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -48,6 +48,10 @@ gnome_builder_deps = [
   libide_tree_dep,
 ]
 
+if libsysprof_capture.found()
+  gnome_builder_deps += libsysprof_capture
+endif
+
 if get_option('fusermount_wrapper')
 
   executable('fusermount', ['fusermount-wrapper.c'],


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