[polari] main: Setup GJS profiler when GJS_TRACE_FD is set



commit 6c1cb78f6570e71f01c6397e81fce473f6630916
Author: Florian Müllner <fmuellner gnome org>
Date:   Mon Jun 24 19:17:28 2019 +0200

    main: Setup GJS profiler when GJS_TRACE_FD is set
    
    This is the same environment variable that will be used in GJS to auto-
    connect Sysprof to the GJS profiler when the gjs binary is used.
    
    https://gitlab.gnome.org/GNOME/polari/merge_requests/123

 meson.build  |  2 +-
 src/polari.c | 44 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 40 insertions(+), 6 deletions(-)
---
diff --git a/meson.build b/meson.build
index cbdc263..9ae4e01 100644
--- a/meson.build
+++ b/meson.build
@@ -36,7 +36,7 @@ gio = dependency('gio-2.0', version: '>= 2.43.4')
 gtk3 = dependency('gtk+-3.0', version: '>= 3.21.6')
 telepathy_glib = dependency('telepathy-glib')
 girepository = dependency('gobject-introspection-1.0')
-gjs = dependency('gjs-1.0', version: '>= 1.53.90')
+gjs = dependency('gjs-1.0', version: '>= 1.57.3')
 
 conf = configuration_data()
 
diff --git a/src/polari.c b/src/polari.c
index e9d2837..4455b2a 100644
--- a/src/polari.c
+++ b/src/polari.c
@@ -32,6 +32,32 @@ get_js_argv (int argc, const char * const *argv)
   return strv;
 }
 
+static gboolean
+get_profiler_fd (int *fd_p)
+{
+  const char *enabled;
+  const char *fd_str;
+  int fd;
+
+  /* Sysprof uses the "GJS_TRACE_FD=N" environment variable to connect GJS
+   * profiler data to the combined Sysprof capture. Since we are in control of
+   * the GjsContext, we need to proxy this FD across to the GJS profiler.
+   */
+
+  fd_str = g_getenv ("GJS_TRACE_FD");
+  enabled = g_getenv ("GJS_ENABLE_PROFILER");
+  if (fd_str == NULL || enabled == NULL)
+    return FALSE;
+
+  fd = atoi (fd_str);
+
+  if (fd <= 2)
+    return FALSE;
+
+  *fd_p = fd;
+  return TRUE;
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -40,8 +66,9 @@ main (int argc, char *argv[])
   g_autoptr (GError) error = NULL;
   g_autoptr (GjsContext) context = NULL;
   g_auto (GStrv) js_argv = NULL;
+  GjsProfiler *profiler = NULL;
   gboolean debugger = FALSE;
-  int status;
+  int status, profiler_fd;
 
   GOptionEntry entries[] =
     {
@@ -82,12 +109,19 @@ main (int argc, char *argv[])
       return 1;
     }
 
-  if (!gjs_context_eval (context, src, -1, "<main>", &status, &error))
+  if (get_profiler_fd (&profiler_fd))
     {
-      g_message ("Execution of start() threw exception: %s", error->message);
+      profiler = gjs_context_get_profiler (context);
 
-      return status;
+      gjs_profiler_set_fd (profiler, profiler_fd);
+      gjs_profiler_start (profiler);
     }
 
-  return 0;
+  if (!gjs_context_eval (context, src, -1, "<main>", &status, &error))
+    g_message ("Execution of start() threw exception: %s", error->message);
+
+  if (profiler)
+    gjs_profiler_stop (profiler);
+
+  return status;
 }


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