From 3a74254f73ef21f00f275a25075bfe15b072cdd2 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 27 Jul 2023 15:49:34 -0700 Subject: [PATCH] tools: use sysprof_tracefd_consumer_new This just allows us to avoid some duplicated code. --- src/sysprof-agent/sysprof-agent.c | 25 +++---------------------- src/sysprof-cli/sysprof-cli.c | 23 +++++++++++++++++++---- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/sysprof-agent/sysprof-agent.c b/src/sysprof-agent/sysprof-agent.c index 65eb39ac..b6ea1c53 100644 --- a/src/sysprof-agent/sysprof-agent.c +++ b/src/sysprof-agent/sysprof-agent.c @@ -599,6 +599,9 @@ main (int argc, } } + sysprof_profiler_add_instrument (profiler, sysprof_tracefd_consumer_new (g_steal_fd (&gjs_trace_fd))); + sysprof_profiler_add_instrument (profiler, sysprof_tracefd_consumer_new (g_steal_fd (&trace_fd))); + /* Now open the writer for our session */ if (!(writer = sysprof_capture_writer_new (capture_filename, BUFFER_SIZE))) { @@ -625,28 +628,6 @@ main (int argc, g_main_loop_run (main_loop); - if (gjs_trace_fd != -1) - { - SysprofCaptureReader *reader = NULL; - - if ((reader = sysprof_capture_reader_new_from_fd (g_steal_fd (&gjs_trace_fd)))) - { - sysprof_capture_writer_cat (writer, reader); - sysprof_capture_reader_unref (reader); - } - } - - if (trace_fd != -1) - { - SysprofCaptureReader *reader = NULL; - - if ((reader = sysprof_capture_reader_new_from_fd (g_steal_fd (&trace_fd)))) - { - sysprof_capture_writer_cat (writer, reader); - sysprof_capture_reader_unref (reader); - } - } - sysprof_capture_writer_flush (writer); sysprof_capture_writer_unref (writer); diff --git a/src/sysprof-cli/sysprof-cli.c b/src/sysprof-cli/sysprof-cli.c index da3b9715..3f910433 100644 --- a/src/sysprof-cli/sysprof-cli.c +++ b/src/sysprof-cli/sysprof-cli.c @@ -236,6 +236,23 @@ sysprof_cli_record_cb (GObject *object, g_set_object (&active_recording, recording); } +static void +add_trace_fd (SysprofProfiler *profiler, + SysprofSpawnable *spawnable, + const char *name) +{ + int trace_fd = -1; + + g_assert (SYSPROF_IS_PROFILER (profiler)); + g_assert (!spawnable || SYSPROF_IS_SPAWNABLE (spawnable)); + + if (spawnable == NULL) + return; + + trace_fd = sysprof_spawnable_add_trace_fd (spawnable, name); + sysprof_profiler_add_instrument (profiler, sysprof_tracefd_consumer_new (g_steal_fd (&trace_fd))); +} + int main (int argc, char *argv[]) @@ -272,8 +289,6 @@ main (int argc, gboolean speedtrack = FALSE; gboolean system_bus = FALSE; gboolean session_bus = FALSE; - g_autofd int gjs_trace_fd = -1; - g_autofd int trace_fd = -1; int pid = -1; int fd; int flags; @@ -473,10 +488,10 @@ Examples:\n\ sysprof_profiler_set_spawnable (profiler, spawnable); if (gjs) - gjs_trace_fd = sysprof_spawnable_add_trace_fd (spawnable, "GJS_TRACE_FD"); + add_trace_fd (profiler, spawnable, "GJS_TRACE_FD"); if (use_trace_fd) - trace_fd = sysprof_spawnable_add_trace_fd (spawnable, NULL); + add_trace_fd (profiler, spawnable, NULL); if (memprof) sysprof_spawnable_add_ld_preload (spawnable, PACKAGE_LIBDIR"/libsysprof-memory-"API_VERSION_S".so");