libsysprof-analyze: add callgraph flags for thread-ids

This allows you to set a flag to show the thread id of what was recorded.

Use this to help disassociate similar threads in a process to figure out
which thread is consuming a majority of the Total time of that process.
This commit is contained in:
Christian Hergert
2023-06-14 15:11:56 -07:00
parent 54ecdc57cd
commit 5b929b8d5d
20 changed files with 206 additions and 538 deletions

View File

@ -57,6 +57,8 @@ struct _SysprofCallgraph
GHashTable *symbol_to_summary;
GPtrArray *symbols;
SysprofCallgraphFlags flags;
gsize augment_size;
SysprofAugmentationFunc augment_func;
gpointer augment_func_data;
@ -66,6 +68,7 @@ struct _SysprofCallgraph
};
void _sysprof_callgraph_new_async (SysprofDocument *document,
SysprofCallgraphFlags flags,
GListModel *traceables,
gsize augment_size,
SysprofAugmentationFunc augment_func,

View File

@ -284,17 +284,19 @@ sysprof_callgraph_add_traceable (SysprofCallgraph *self,
guint stack_depth;
guint n_symbols;
int pid;
int tid;
g_assert (SYSPROF_IS_CALLGRAPH (self));
g_assert (SYSPROF_IS_DOCUMENT_TRACEABLE (traceable));
pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (traceable));
tid = sysprof_document_traceable_get_thread_id (traceable);
stack_depth = sysprof_document_traceable_get_stack_depth (traceable);
if (stack_depth == 0 || stack_depth > MAX_STACK_DEPTH)
return;
symbols = g_newa (SysprofSymbol *, stack_depth + 3);
symbols = g_newa (SysprofSymbol *, stack_depth + 4);
n_symbols = sysprof_document_symbolize_traceable (self->document,
traceable,
symbols,
@ -323,6 +325,13 @@ sysprof_callgraph_add_traceable (SysprofCallgraph *self,
*/
if (final_context == SYSPROF_ADDRESS_CONTEXT_KERNEL)
symbols[n_symbols++] = _sysprof_document_kernel_symbol (self->document);
/* If the user requested thread-ids within each process, then
* insert a symbol for that before the real stacks.
*/
if ((self->flags & SYSPROF_CALLGRAPH_FLAGS_INCLUDE_THREADS) != 0)
symbols[n_symbols++] = _sysprof_document_thread_symbol (self->document, pid, tid);
symbols[n_symbols++] = _sysprof_document_process_symbol (self->document, pid);
symbols[n_symbols++] = everything;
@ -364,6 +373,7 @@ sysprof_callgraph_new_worker (GTask *task,
void
_sysprof_callgraph_new_async (SysprofDocument *document,
SysprofCallgraphFlags flags,
GListModel *traceables,
gsize augment_size,
SysprofAugmentationFunc augment_func,
@ -387,6 +397,7 @@ _sysprof_callgraph_new_async (SysprofDocument *document,
summary_free = (GDestroyNotify)sysprof_callgraph_summary_free_self;
self = g_object_new (SYSPROF_TYPE_CALLGRAPH, NULL);
self->flags = flags;
self->document = g_object_ref (document);
self->traceables = g_object_ref (traceables);
self->augment_size = augment_size;

View File

@ -62,6 +62,14 @@ typedef void (*SysprofAugmentationFunc) (SysprofCallgraph *callgraph,
gboolean summarize,
gpointer user_data);
typedef enum _SysprofCallgraphFlags
{
SYSPROF_CALLGRAPH_FLAGS_NONE = 0,
SYSPROF_CALLGRAPH_FLAGS_INCLUDE_THREADS = 1 << 1,
} SysprofCallgraphFlags;
SYSPROF_AVAILABLE_IN_ALL
GType sysprof_callgraph_flags_get_type (void) G_GNUC_CONST;
SYSPROF_AVAILABLE_IN_ALL
GListModel *sysprof_callgraph_list_symbols (SysprofCallgraph *self);
SYSPROF_AVAILABLE_IN_ALL

View File

@ -41,7 +41,7 @@ enum {
PROP_IS_FREE,
PROP_SIZE,
PROP_STACK_DEPTH,
PROP_TID,
PROP_THREAD_ID,
N_PROPS
};
@ -76,12 +76,26 @@ sysprof_document_allocation_get_stack_addresses (SysprofDocumentTraceable *trace
return depth;
}
static int
sysprof_document_allocation_get_thread_id (SysprofDocumentTraceable *traceable)
{
SysprofDocumentAllocation *self = (SysprofDocumentAllocation *)traceable;
const SysprofCaptureAllocation *allocation;
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_ALLOCATION (self), 0);
allocation = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureAllocation);
return SYSPROF_DOCUMENT_FRAME_INT32 (self, allocation->tid);
}
static void
traceable_iface_init (SysprofDocumentTraceableInterface *iface)
{
iface->get_stack_depth = sysprof_document_allocation_get_stack_depth;
iface->get_stack_address = sysprof_document_allocation_get_stack_address;
iface->get_stack_addresses = sysprof_document_allocation_get_stack_addresses;
iface->get_thread_id = sysprof_document_allocation_get_thread_id;
}
G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDocumentAllocation, sysprof_document_allocation, SYSPROF_TYPE_DOCUMENT_FRAME,
@ -115,8 +129,8 @@ sysprof_document_allocation_get_property (GObject *object,
g_value_set_uint (value, sysprof_document_traceable_get_stack_depth (SYSPROF_DOCUMENT_TRACEABLE (self)));
break;
case PROP_TID:
g_value_set_int (value, sysprof_document_allocation_get_tid (self));
case PROP_THREAD_ID:
g_value_set_int (value, sysprof_document_traceable_get_thread_id (SYSPROF_DOCUMENT_TRACEABLE (self)));
break;
default:
@ -132,17 +146,17 @@ sysprof_document_allocation_class_init (SysprofDocumentAllocationClass *klass)
object_class->get_property = sysprof_document_allocation_get_property;
/**
* SysprofDocumentAllocation:tid:
* SysprofDocumentAllocation:thread-id:
*
* The task-id or thread-id of the thread which was traced.
* The thread-id where the stack was traced.
*
* On Linux, this is generally set to the value of the gettid() syscall.
* On Linux, this is generally set to the value of `gettid()`.
*
* Since: 45
*/
properties [PROP_TID] =
g_param_spec_int ("tid", NULL, NULL,
G_MININT32, G_MAXINT32, 0,
properties [PROP_THREAD_ID] =
g_param_spec_int ("thread-id", NULL, NULL,
-1, G_MAXINT32, 0,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
/**
@ -226,18 +240,6 @@ sysprof_document_allocation_get_size (SysprofDocumentAllocation *self)
return SYSPROF_DOCUMENT_FRAME_INT64 (self, allocation->alloc_size);
}
int
sysprof_document_allocation_get_tid (SysprofDocumentAllocation *self)
{
const SysprofCaptureAllocation *allocation;
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_ALLOCATION (self), 0);
allocation = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureAllocation);
return SYSPROF_DOCUMENT_FRAME_INT32 (self, allocation->tid);
}
gboolean
sysprof_document_allocation_is_free (SysprofDocumentAllocation *self)
{

View File

@ -58,6 +58,9 @@ GRefString *_sysprof_document_ref_string (SysprofDocument *self,
EggBitset *_sysprof_document_traceables (SysprofDocument *self);
SysprofSymbol *_sysprof_document_process_symbol (SysprofDocument *self,
int pid);
SysprofSymbol *_sysprof_document_thread_symbol (SysprofDocument *self,
int pid,
int tid);
SysprofSymbol *_sysprof_document_kernel_symbol (SysprofDocument *self);
G_END_DECLS

View File

@ -38,7 +38,7 @@ struct _SysprofDocumentSampleClass
enum {
PROP_0,
PROP_STACK_DEPTH,
PROP_TID,
PROP_THREAD_ID,
N_PROPS
};
@ -73,12 +73,26 @@ sysprof_document_sample_get_stack_addresses (SysprofDocumentTraceable *traceable
return depth;
}
static int
sysprof_document_sample_get_thread_id (SysprofDocumentTraceable *traceable)
{
SysprofDocumentSample *self = (SysprofDocumentSample *)traceable;
const SysprofCaptureSample *sample;
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_SAMPLE (self), -1);
sample = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureSample);
return SYSPROF_DOCUMENT_FRAME_INT32 (self, sample->tid);
}
static void
traceable_iface_init (SysprofDocumentTraceableInterface *iface)
{
iface->get_stack_depth = sysprof_document_sample_get_stack_depth;
iface->get_stack_address = sysprof_document_sample_get_stack_address;
iface->get_stack_addresses = sysprof_document_sample_get_stack_addresses;
iface->get_thread_id = sysprof_document_sample_get_thread_id;
}
G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDocumentSample, sysprof_document_sample, SYSPROF_TYPE_DOCUMENT_FRAME,
@ -100,8 +114,8 @@ sysprof_document_sample_get_property (GObject *object,
g_value_set_uint (value, sysprof_document_traceable_get_stack_depth (SYSPROF_DOCUMENT_TRACEABLE (self)));
break;
case PROP_TID:
g_value_set_int (value, sysprof_document_sample_get_tid (self));
case PROP_THREAD_ID:
g_value_set_int (value, sysprof_document_sample_get_thread_id (SYSPROF_DOCUMENT_TRACEABLE (self)));
break;
default:
@ -117,17 +131,17 @@ sysprof_document_sample_class_init (SysprofDocumentSampleClass *klass)
object_class->get_property = sysprof_document_sample_get_property;
/**
* SysprofDocumentSample:tid:
* SysprofDocumentSample:thread-id:
*
* The task-id or thread-id of the thread which was sampled.
* The thread-id where the sample occurred.
*
* On Linux, this is generally set to the value of the gettid() syscall.
* On Linux, this is generally set to the value of gettid().
*
* Since: 45
*/
properties [PROP_TID] =
g_param_spec_int ("tid", NULL, NULL,
G_MININT32, G_MAXINT32, 0,
properties [PROP_THREAD_ID] =
g_param_spec_int ("thread-id", NULL, NULL,
-1, G_MAXINT32, 0,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
/**
@ -149,15 +163,3 @@ static void
sysprof_document_sample_init (SysprofDocumentSample *self)
{
}
int
sysprof_document_sample_get_tid (SysprofDocumentSample *self)
{
const SysprofCaptureSample *sample;
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_SAMPLE (self), 0);
sample = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureSample);
return SYSPROF_DOCUMENT_FRAME_INT32 (self, sample->tid);
}

View File

@ -43,6 +43,18 @@ sysprof_document_traceable_default_init (SysprofDocumentTraceableInterface *ifac
g_param_spec_uint ("stack-depth", NULL, NULL,
0, G_MAXUINT16, 0,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
/**
* SysprofDocumentTraceable:thread-id:
*
* The "thread-id" property contains the thread identifier for the traceable.
*
* Since: 45
*/
g_object_interface_install_property (iface,
g_param_spec_int ("thread-id", NULL, NULL,
-1, G_MAXINT, -1,
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
}
guint
@ -58,6 +70,12 @@ sysprof_document_traceable_get_stack_address (SysprofDocumentTraceable *self,
return SYSPROF_DOCUMENT_TRACEABLE_GET_IFACE (self)->get_stack_address (self, position);
}
int
sysprof_document_traceable_get_thread_id (SysprofDocumentTraceable *self)
{
return SYSPROF_DOCUMENT_TRACEABLE_GET_IFACE (self)->get_thread_id (self);
}
guint
sysprof_document_traceable_get_stack_addresses (SysprofDocumentTraceable *self,
guint64 *addresses,

View File

@ -40,6 +40,7 @@ struct _SysprofDocumentTraceableInterface
guint (*get_stack_addresses) (SysprofDocumentTraceable *self,
guint64 *addresses,
guint n_addresses);
int (*get_thread_id) (SysprofDocumentTraceable *self);
};
SYSPROF_AVAILABLE_IN_ALL
@ -51,5 +52,7 @@ SYSPROF_AVAILABLE_IN_ALL
guint sysprof_document_traceable_get_stack_addresses (SysprofDocumentTraceable *self,
guint64 *addresses,
guint n_addresses);
SYSPROF_AVAILABLE_IN_ALL
int sysprof_document_traceable_get_thread_id (SysprofDocumentTraceable *self);
G_END_DECLS

View File

@ -78,6 +78,7 @@ struct _SysprofDocument
GHashTable *files_first_position;
GHashTable *pid_to_process_info;
GHashTable *tid_to_symbol;
SysprofMountNamespace *mount_namespace;
@ -269,6 +270,7 @@ sysprof_document_init (SysprofDocument *self)
self->files_first_position = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
self->pid_to_process_info = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)sysprof_process_info_unref);
self->tid_to_symbol = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify)g_object_unref);
self->mount_namespace = sysprof_mount_namespace_new ();
}
@ -1190,6 +1192,7 @@ sysprof_document_callgraph_cb (GObject *object,
/**
* sysprof_document_callgraph_async:
* @self: a #SysprofDocument
* @flags: flags for generating the callgraph
* @traceables: a list model of traceables for the callgraph
* @augment_size: the size of data to reserve for augmentation in
* the callgraph.
@ -1212,6 +1215,7 @@ sysprof_document_callgraph_cb (GObject *object,
*/
void
sysprof_document_callgraph_async (SysprofDocument *self,
SysprofCallgraphFlags flags,
GListModel *traceables,
gsize augment_size,
SysprofAugmentationFunc augment_func,
@ -1231,6 +1235,7 @@ sysprof_document_callgraph_async (SysprofDocument *self,
g_task_set_source_tag (task, sysprof_document_callgraph_async);
_sysprof_callgraph_new_async (self,
flags,
traceables,
augment_size,
augment_func,
@ -1282,6 +1287,39 @@ _sysprof_document_process_symbol (SysprofDocument *self,
return info->fallback_symbol;
}
SysprofSymbol *
_sysprof_document_thread_symbol (SysprofDocument *self,
int pid,
int tid)
{
SysprofSymbol *ret;
g_return_val_if_fail (SYSPROF_IS_DOCUMENT (self), NULL);
if (!(ret = g_hash_table_lookup (self->tid_to_symbol, GINT_TO_POINTER (tid))))
{
char pidstr[32];
char tidstr[32];
g_snprintf (pidstr, sizeof pidstr, "(%d)", pid);
if (tid == pid)
g_snprintf (tidstr, sizeof tidstr, "Thread-%d (Main)", tid);
else
g_snprintf (tidstr, sizeof tidstr, "Thread-%d", tid);
ret = _sysprof_symbol_new (g_ref_string_new (tidstr),
NULL,
g_ref_string_new (pidstr),
0, 0,
SYSPROF_SYMBOL_KIND_THREAD);
g_hash_table_insert (self->tid_to_symbol, GINT_TO_POINTER (tid), ret);
}
return ret;
}
SysprofSymbol *
_sysprof_document_kernel_symbol (SysprofDocument *self)
{

View File

@ -66,6 +66,7 @@ guint sysprof_document_symbolize_traceable (SysprofDocument
SysprofAddressContext *final_context);
SYSPROF_AVAILABLE_IN_ALL
void sysprof_document_callgraph_async (SysprofDocument *self,
SysprofCallgraphFlags flags,
GListModel *traceables,
gsize augment_size,
SysprofAugmentationFunc augment_func,

View File

@ -31,6 +31,7 @@ typedef enum _SysprofSymbolKind
{
SYSPROF_SYMBOL_KIND_ROOT = 1,
SYSPROF_SYMBOL_KIND_PROCESS,
SYSPROF_SYMBOL_KIND_THREAD,
SYSPROF_SYMBOL_KIND_CONTEXT_SWITCH,
SYSPROF_SYMBOL_KIND_USER,
SYSPROF_SYMBOL_KIND_KERNEL,

View File

@ -33,8 +33,10 @@ typedef struct _Augment
} Augment;
static char *kallsyms_path;
static gboolean include_threads;
static const GOptionEntry entries[] = {
{ "kallsyms", 'k', 0, G_OPTION_ARG_FILENAME, &kallsyms_path, "The path to kallsyms to use for decoding", "PATH" },
{ "threads", 't', 0, G_OPTION_ARG_NONE, &include_threads, "Include threads in the stack traces" },
{ 0 }
};
@ -122,6 +124,7 @@ main (int argc,
g_autoptr(SysprofMultiSymbolizer) multi = NULL;
g_autoptr(GListModel) samples = NULL;
g_autoptr(GError) error = NULL;
SysprofCallgraphFlags flags = 0;
setlocale (LC_ALL, "");
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
@ -159,7 +162,11 @@ main (int argc,
samples = sysprof_document_list_samples (document);
if (include_threads)
flags |= SYSPROF_CALLGRAPH_FLAGS_INCLUDE_THREADS;
sysprof_document_callgraph_async (document,
flags,
samples,
sizeof (Augment),
augment_cb, NULL, NULL,