From f34532c4e767066f668c945e8a4f305fa07e3e52 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 29 Jan 2020 16:31:00 +0100 Subject: wip: Add sysprof marks to GMainLoop This is just a prototype. --- glib/gmain.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- glib/meson.build | 5 +++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/glib/gmain.c b/glib/gmain.c index 1b4260b11..40f8c276e 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -33,6 +33,78 @@ #include "glibconfig.h" #include "glib_trace.h" +#define HAVE_SYSPROF_CAPTURE +#ifdef HAVE_SYSPROF_CAPTURE +#include +#include + +static SysprofCaptureWriter *writer = NULL; +static gboolean running = FALSE; + +static void +profiler_atexit (void) +{ +g_print ("glib profiler atexit\n"); + if (writer) + sysprof_capture_writer_unref (writer); +} + +static void +profiler_start (int fd) +{ + if (writer) + return; + + sysprof_clock_init (); + + if (fd == -1) + { + gchar *filename; + + filename = g_strdup_printf ("glib.%d.syscap", getpid ()); + g_print ("Writing profiling data to %s\n", filename); + writer = sysprof_capture_writer_new (filename, 16*1024); + g_free (filename); + } + else if (fd > 2) + writer = sysprof_capture_writer_new_from_fd (fd, 16*1024); + + if (writer) + running = TRUE; + + atexit (profiler_atexit); +} + +static void +profiler_init (void) +{ + if (g_getenv ("GLIB_TRACE_FD")) + { + g_print ("Found GLIB_TRACE_FD, using it\n"); + profiler_start (atoi (g_getenv ("GLIB_TRACE_FD"))); + } + else if (g_getenv ("GLIB_TRACE")) + profiler_start (-1); +} + +static void +profiler_add_mark (gint64 start, + guint64 duration, + const char *name, + const char *message) +{ + if (!running) + return; + +g_print ("glib profiler add mark\n"); + sysprof_capture_writer_add_mark (writer, + start, + -1, getpid (), + duration, + "glib", name, message); +} +#endif + /* Uncomment the next line (and the corresponding line in gpoll.c) to * enable debugging printouts if the environment variable * G_MAIN_POLL_DEBUG is set to some value. @@ -620,6 +692,8 @@ g_main_context_new (void) _g_main_poll_debug = TRUE; #endif + profiler_init (); + g_once_init_leave (&initialised, TRUE); } @@ -3236,6 +3310,7 @@ g_main_dispatch (GMainContext *context) GSourceCallbackFuncs *cb_funcs; gpointer cb_data; gboolean need_destroy; + gint64 before; gboolean (*dispatch) (GSource *, GSourceFunc, @@ -3267,12 +3342,14 @@ g_main_dispatch (GMainContext *context) current->source = source; current->depth++; + before = g_get_monotonic_time (); TRACE (GLIB_MAIN_BEFORE_DISPATCH (g_source_get_name (source), source, dispatch, callback, user_data)); need_destroy = !(* dispatch) (source, callback, user_data); TRACE (GLIB_MAIN_AFTER_DISPATCH (g_source_get_name (source), source, dispatch, need_destroy)); - + profiler_add_mark (before * 1000, (g_get_monotonic_time () - before) * 1000, "dispatch source", g_source_get_name (source)); + current->source = prev_source; current->depth--; diff --git a/glib/meson.build b/glib/meson.build index aaf5f00f5..51e3ec65e 100644 --- a/glib/meson.build +++ b/glib/meson.build @@ -322,6 +322,9 @@ endif glib_sources += files('gthread-@0@.c'.format(threads_implementation)) + +profiler_dep = dependency('sysprof-capture-3', static: true, required: true) + if enable_dtrace glib_dtrace_obj = dtrace_obj_gen.process('glib_probes.d') glib_dtrace_hdr = dtrace_hdr_gen.process('glib_probes.d') @@ -356,7 +359,7 @@ libglib = library('glib-2.0', # intl.lib is not compatible with SAFESEH link_args : [noseh_link_args, glib_link_flags, win32_ldflags], include_directories : configinc, - dependencies : pcre_deps + [thread_dep, libintl, librt] + libiconv + platform_deps + gnulib_libm_dependency, + dependencies : pcre_deps + [profiler_dep, thread_dep, libintl, librt] + libiconv + platform_deps + gnulib_libm_dependency, c_args : glib_c_args, objc_args : glib_c_args, ) -- cgit v1.2.1