Merge branch 'task-trace' into 'master'

Add some tracing to GTask

See merge request GNOME/glib!1629
This commit is contained in:
Emmanuele Bassi 2020-11-18 13:53:41 +00:00
commit 6e9ed964c3
6 changed files with 119 additions and 6 deletions

View File

@ -24,6 +24,7 @@
#include "gasyncresult.h"
#include "gcancellable.h"
#include "glib-private.h"
#include "gtrace-private.h"
#include "glibintl.h"
@ -618,6 +619,9 @@ static GSource *task_pool_manager;
static guint64 task_wait_time;
static gint tasks_running;
static guint task_pool_max_counter;
static guint tasks_running_counter;
/* When the task pool fills up and blocks, and the program keeps
* queueing more tasks, we will slowly add more threads to the pool
* (in case the existing tasks are trying to queue subtasks of their
@ -1361,6 +1365,7 @@ task_pool_manager_timeout (gpointer user_data)
{
g_mutex_lock (&task_pool_mutex);
g_thread_pool_set_max_threads (task_pool, tasks_running + 1, NULL);
g_trace_set_int64_counter (task_pool_max_counter, tasks_running + 1);
g_source_set_ready_time (task_pool_manager, -1);
g_mutex_unlock (&task_pool_mutex);
@ -1374,6 +1379,8 @@ g_task_thread_setup (void)
g_mutex_lock (&task_pool_mutex);
tasks_running++;
g_trace_set_int64_counter (tasks_running_counter, tasks_running);
if (tasks_running == G_TASK_POOL_SIZE)
task_wait_time = G_TASK_WAIT_TIME_BASE;
else if (tasks_running > G_TASK_POOL_SIZE && tasks_running < G_TASK_WAIT_TIME_MAX_POOL_SIZE)
@ -1394,7 +1401,10 @@ g_task_thread_cleanup (void)
tasks_pending = g_thread_pool_unprocessed (task_pool);
if (tasks_running > G_TASK_POOL_SIZE)
g_thread_pool_set_max_threads (task_pool, tasks_running - 1, NULL);
{
g_thread_pool_set_max_threads (task_pool, tasks_running - 1, NULL);
g_trace_set_int64_counter (task_pool_max_counter, tasks_running - 1);
}
else if (tasks_running + tasks_pending < G_TASK_POOL_SIZE)
g_source_set_ready_time (task_pool_manager, -1);
@ -1402,6 +1412,9 @@ g_task_thread_cleanup (void)
task_wait_time /= G_TASK_WAIT_TIME_MULTIPLIER;
tasks_running--;
g_trace_set_int64_counter (tasks_running_counter, tasks_running);
g_mutex_unlock (&task_pool_mutex);
g_private_set (&task_private, GUINT_TO_POINTER (FALSE));
}
@ -2214,6 +2227,16 @@ g_task_class_init (GTaskClass *klass)
P_("Task completed"),
P_("Whether the task has completed yet"),
FALSE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
if (G_UNLIKELY (task_pool_max_counter == 0))
{
/* We use two counters to track characteristics of the GTask thread pool.
* task pool max size - the value of g_thread_pool_set_max_threads()
* tasks running - the number of running threads
*/
task_pool_max_counter = g_trace_define_int64_counter ("GIO", "task pool max size", "Maximum number of threads allowed in the GTask thread pool; see g_thread_pool_set_max_threads()");
tasks_running_counter = g_trace_define_int64_counter ("GIO", "tasks running", "Number of currently running tasks in the GTask thread pool");
}
}
static gpointer

View File

@ -577,6 +577,7 @@ gio_sources = files(
'gzlibdecompressor.c',
'glistmodel.c',
'gliststore.c',
'../glib/gtrace.c',
)
gio_sources += appinfo_sources
@ -791,7 +792,7 @@ libgio = library('gio-2.0',
# '$(gio_win32_res_ldflag)',
dependencies : [libz_dep, libdl_dep, libmount_dep, libglib_dep,
libgobject_dep, libgmodule_dep, selinux_dep, xattr_dep,
platform_deps, network_libs],
platform_deps, network_libs, libsysprof_capture_dep],
c_args : gio_c_args,
objc_args : gio_c_args,
# intl.lib is not compatible with SAFESEH

View File

@ -23,8 +23,7 @@
#include <sysprof-capture.h>
#endif
#include "gmem.h"
#include "gmacros.h"
#include "glib.h"
G_BEGIN_DECLS
@ -67,4 +66,15 @@ void (g_trace_mark) (gint64 begin_time_nsec,
#endif
#endif
guint (g_trace_define_int64_counter) (const char *group,
const char *name,
const char *description);
void (g_trace_set_int64_counter) (guint id,
gint64 value);
#ifndef HAVE_SYSPROF
#define g_trace_define_int64_counter(g, n, d) ((guint) -1)
#define g_trace_set_int64_counter(i,v)
#endif
G_END_DECLS

View File

@ -96,3 +96,82 @@ void
va_end (args);
#endif /* HAVE_SYSPROF */
}
/*
* g_trace_define_int64_counter:
* @group: name of the group for categorising this counter
* @name: name of the counter
* @description: description for the counter
*
* Defines a new counter with integer values.
*
* The name should be unique within all counters defined with
* the same @group. The description will be shown in the sysprof UI.
*
* To add entries for this counter to a trace, use
* g_trace_set_int64_counter().
*
* Returns: ID of the counter, for use with g_trace_set_int64_counter(),
* guaranteed to never be zero
*
* Since: 2.68
*/
guint
(g_trace_define_int64_counter) (const char *group,
const char *name,
const char *description)
{
#ifdef HAVE_SYSPROF
SysprofCaptureCounter counter;
counter.id = sysprof_collector_request_counters (1);
/* sysprof not enabled? */
if (counter.id == 0)
return (guint) -1;
counter.type = SYSPROF_CAPTURE_COUNTER_INT64;
counter.value.v64 = 0;
g_strlcpy (counter.category, group, sizeof counter.category);
g_strlcpy (counter.name, name, sizeof counter.name);
g_strlcpy (counter.description, description, sizeof counter.description);
sysprof_collector_define_counters (&counter, 1);
g_assert (counter.id != 0);
return counter.id;
#else
return (guint) -1;
#endif
}
/*
* g_trace_set_int64_counter:
* @id: ID of the counter
* @val: the value to set the counter to
*
* Adds a counter value to a trace.
*
* The ID must be obtained via g_trace_define_int64_counter()
* before using this function.
*
* Since: 2.68
*/
void
(g_trace_set_int64_counter) (guint id,
gint64 val)
{
#ifdef HAVE_SYSPROF
SysprofCaptureCounterValue value;
g_return_if_fail (id != 0);
/* Ignore setting the counter if we failed to define it in the first place. */
if (id == (guint) -1)
return;
value.v64 = val;
sysprof_collector_set_counters (&id, &value, 1);
#endif
}

View File

@ -8,7 +8,7 @@ if not use_system_pcre
endif
# libsysprof-capture support
libsysprof_capture_dep = dependency('sysprof-capture-4',
libsysprof_capture_dep = dependency('sysprof-capture-4', version: '>= 3.38.0',
required: get_option('sysprof'),
default_options: [
'enable_examples=false',

View File

@ -1,5 +1,5 @@
[wrap-git]
directory=sysprof
url=https://gitlab.gnome.org/GNOME/sysprof.git
revision=6b1cd7a722fcebae1ac392562c47957477ade8bf
revision=3.38.0
depth=1