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




commit b0ca45053fc20a41de07ce739f8ece968368b46f
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 | 30 +++++++++++++++++++++++++++++-
 gjs/profiler.h   |  4 ++++
 2 files changed, 33 insertions(+), 1 deletion(-)
---
diff --git a/gjs/profiler.cpp b/gjs/profiler.cpp
index c5113becb..f857a1b7c 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,29 @@ 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..953a288a0 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,
+                                     void        *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]