[gjs/gbsneto/external-capture-writer] profiler: Support external SysprofCaptureWriters




commit 39d10deb3a6555508e55bbcecb72611181cb8e87
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Wed Sep 2 11:40:31 2020 -0300

    profiler: Support external SysprofCaptureWriters
    
    Using environment variables to control the profiler is limiting,
    and when it comes to GNOME Shell, it adds an extra level of
    complexity, since it forces us to start GNOME Shell with the
    right environment set.
    
    Add support for setting a caller-controlled SysprofCaptureWriter.
    The header file treats it as a gpointer to avoid including config.h.
    If an external capture writer is set, it takes precedence over the
    filename and the file descriptor.

 gjs/profiler.cpp | 32 +++++++++++++++++++++++++++++++-
 gjs/profiler.h   |  4 ++++
 2 files changed, 35 insertions(+), 1 deletion(-)
---
diff --git a/gjs/profiler.cpp b/gjs/profiler.cpp
index c5113becb..4cb3eee07 100644
--- a/gjs/profiler.cpp
+++ b/gjs/profiler.cpp
@@ -105,6 +105,8 @@ struct _GjsProfiler {
     /* Buffers and writes our sampled stacks */
     SysprofCaptureWriter* capture;
     GSource* periodic_flush;
+
+    SysprofCaptureWriter* target_capture;
 #endif  /* ENABLE_PROFILER */
 
     /* The filename to write to */
@@ -259,6 +261,7 @@ _gjs_profiler_free(GjsProfiler *self)
 #ifdef ENABLE_PROFILER
     g_clear_pointer(&self->capture, sysprof_capture_writer_unref);
     g_clear_pointer(&self->periodic_flush, g_source_destroy);
+    g_clear_pointer(&self->target_capture, sysprof_capture_writer_unref);
 
     if (self->fd != -1)
         close(self->fd);
@@ -429,7 +432,9 @@ gjs_profiler_start(GjsProfiler *self)
     struct itimerspec its = { 0 };
     struct itimerspec old_its;
 
-    if (self->fd != -1) {
+    if (self->target_capture) {
+        self->capture = sysprof_capture_writer_ref(self->target_capture);
+    } else if (self->fd != -1) {
         self->capture = sysprof_capture_writer_new_from_fd(self->fd, 0);
         self->fd = -1;
     } else {
@@ -672,6 +677,31 @@ gjs_profiler_chain_signal(GjsContext *context,
     return false;
 }
 
+/**
+ * gjs_profiler_set_capture_writer:
+ * @self: A #GjsProfiler
+ * @capture: (nullable): A #SysprofCaptureWriter
+ *
+ * Set the capture writer to which profiling data is written when the @self
+ * is stopped.
+ */
+void
+gjs_profiler_set_capture_writer(GjsProfiler *self,
+                                gpointer     capture)
+{
+    g_return_if_fail(self);
+    g_return_if_fail(!self->running);
+
+#ifdef ENABLE_PROFILER
+    g_clear_pointer(&self->target_capture, sysprof_capture_writer_unref);
+    self->target_capture = capture ?
+        sysprof_capture_writer_ref(reinterpret_cast<SysprofCaptureWriter*>capture) : NULL;
+#else
+    // Unused in the no-profiler case
+    (void)capture;
+#endif
+}
+
 /**
  * gjs_profiler_set_filename:
  * @self: A #GjsProfiler
diff --git a/gjs/profiler.h b/gjs/profiler.h
index d3518b877..927a32f2b 100644
--- a/gjs/profiler.h
+++ b/gjs/profiler.h
@@ -42,6 +42,10 @@ typedef struct _GjsProfiler GjsProfiler;
 GJS_EXPORT
 GType gjs_profiler_get_type(void);
 
+GJS_EXPORT
+void gjs_profiler_set_capture_writer(GjsProfiler *self,
+                                     gpointer     capture);
+
 GJS_EXPORT
 void gjs_profiler_set_filename(GjsProfiler *self,
                                const char  *filename);


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