mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2025-12-31 20:36:25 +00:00
libsysprof-analyze: implement augmented callgraph
This creates a SysprofCallgraph object which is a GListModel of SysprofCallgraphFrame. The SysprofCallgraphFrame is also a GListModel of SysprofCallgraphFrame so that we can map this all into a GtkListView in the future for tree-like visibility. The augmentation allows for the same callgraph code to be used for multiple scenarios such as CPU sampling as well as memory allocation tracking. If your augmentation size is <=sizeof(void*) then you do not occur an extra allocation and you can use the inline augmentation space. The test-callgraph clearly shows that we still need to do the shuffling of -- Kernel -- and -- User -- like the old callgraph code did. But that will come soon enough.
This commit is contained in:
@ -21,36 +21,30 @@
|
||||
#include "config.h"
|
||||
|
||||
#include "sysprof-callgraph-private.h"
|
||||
#include "sysprof-callgraph-frame.h"
|
||||
#include "sysprof-callgraph-frame-private.h"
|
||||
#include "sysprof-document-private.h"
|
||||
#include "sysprof-document-traceable.h"
|
||||
#include "sysprof-symbol-private.h"
|
||||
|
||||
#define MAX_STACK_DEPTH 1024
|
||||
|
||||
struct _SysprofCallgraph
|
||||
{
|
||||
GObject parent_instance;
|
||||
SysprofDocument *document;
|
||||
GListModel *traceables;
|
||||
GObject parent_instance;
|
||||
|
||||
SysprofDocument *document;
|
||||
GListModel *traceables;
|
||||
|
||||
SysprofSymbol *everything;
|
||||
|
||||
gsize augment_size;
|
||||
SysprofAugmentationFunc augment_func;
|
||||
gpointer augment_func_data;
|
||||
GDestroyNotify augment_func_data_destroy;
|
||||
|
||||
SysprofCallgraphNode root;
|
||||
};
|
||||
|
||||
typedef struct _SysprofCallgraphNode
|
||||
{
|
||||
SysprofSymbol *symbol;
|
||||
|
||||
struct _SysprofCallgraphNode *next;
|
||||
struct _SysprofCallgraphNode *prev;
|
||||
|
||||
struct _SysprofCallgraphNode *parent;
|
||||
struct _SysprofCallgraphNode *children;
|
||||
} SysprofCallgraphNode;
|
||||
|
||||
typedef struct _SysprofCallgraphTrace
|
||||
{
|
||||
guint model_position;
|
||||
guint n_nodes;
|
||||
SysprofCallgraphNode nodes[];
|
||||
} SysprofCallgraphTrace;
|
||||
|
||||
static GType
|
||||
sysprof_callgraph_get_item_type (GListModel *model)
|
||||
{
|
||||
@ -60,14 +54,19 @@ sysprof_callgraph_get_item_type (GListModel *model)
|
||||
static guint
|
||||
sysprof_callgraph_get_n_items (GListModel *model)
|
||||
{
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static gpointer
|
||||
sysprof_callgraph_get_item (GListModel *model,
|
||||
guint position)
|
||||
{
|
||||
return NULL;
|
||||
SysprofCallgraph *self = SYSPROF_CALLGRAPH (model);
|
||||
|
||||
if (position > 0)
|
||||
return NULL;
|
||||
|
||||
return _sysprof_callgraph_frame_new (self, &self->root);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -81,6 +80,24 @@ list_model_iface_init (GListModelInterface *iface)
|
||||
G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofCallgraph, sysprof_callgraph, G_TYPE_OBJECT,
|
||||
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
|
||||
|
||||
static void
|
||||
sysprof_callgraph_dispose (GObject *object)
|
||||
{
|
||||
SysprofCallgraph *self = (SysprofCallgraph *)object;
|
||||
GDestroyNotify notify = self->augment_func_data_destroy;
|
||||
gpointer notify_data = self->augment_func_data;
|
||||
|
||||
self->augment_size = 0;
|
||||
self->augment_func = NULL;
|
||||
self->augment_func_data = NULL;
|
||||
self->augment_func_data_destroy = NULL;
|
||||
|
||||
if (notify != NULL)
|
||||
notify (notify_data);
|
||||
|
||||
G_OBJECT_CLASS (sysprof_callgraph_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_callgraph_finalize (GObject *object)
|
||||
{
|
||||
@ -88,6 +105,7 @@ sysprof_callgraph_finalize (GObject *object)
|
||||
|
||||
g_clear_object (&self->document);
|
||||
g_clear_object (&self->traceables);
|
||||
g_clear_object (&self->everything);
|
||||
|
||||
G_OBJECT_CLASS (sysprof_callgraph_parent_class)->finalize (object);
|
||||
}
|
||||
@ -97,68 +115,105 @@ sysprof_callgraph_class_init (SysprofCallgraphClass *klass)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
object_class->dispose = sysprof_callgraph_dispose;
|
||||
object_class->finalize = sysprof_callgraph_finalize;
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_callgraph_init (SysprofCallgraph *self)
|
||||
{
|
||||
self->everything = _sysprof_symbol_new (g_ref_string_new_intern ("[Everything]"), NULL, NULL, 0, 0);
|
||||
self->root.symbol = self->everything;
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_callgraph_trace_free (SysprofCallgraphTrace *trace)
|
||||
static SysprofCallgraphNode *
|
||||
sysprof_callgraph_add_trace (SysprofCallgraph *self,
|
||||
SysprofSymbol **symbols,
|
||||
guint n_symbols)
|
||||
{
|
||||
g_free (trace);
|
||||
}
|
||||
SysprofCallgraphNode *parent;
|
||||
|
||||
static void
|
||||
sysprof_callgraph_add_trace (SysprofCallgraph *self,
|
||||
SysprofCallgraphTrace *trace)
|
||||
{
|
||||
g_assert (SYSPROF_IS_CALLGRAPH (self));
|
||||
g_assert (trace != NULL);
|
||||
g_assert (n_symbols > 2);
|
||||
g_assert (symbols[n_symbols-1] == self->everything);
|
||||
|
||||
sysprof_callgraph_trace_free (trace);
|
||||
parent = &self->root;
|
||||
|
||||
for (guint i = n_symbols - 1; i > 0; i--)
|
||||
{
|
||||
SysprofSymbol *symbol = symbols[i-1];
|
||||
SysprofCallgraphNode *node = NULL;
|
||||
|
||||
/* Try to find @symbol within the children of @parent */
|
||||
for (SysprofCallgraphNode *iter = parent->children;
|
||||
iter != NULL;
|
||||
iter = iter->next)
|
||||
{
|
||||
g_assert (iter != NULL);
|
||||
g_assert (iter->symbol != NULL);
|
||||
g_assert (symbol != NULL);
|
||||
|
||||
if (_sysprof_symbol_equal (iter->symbol, symbol))
|
||||
{
|
||||
node = iter;
|
||||
goto next_symbol;
|
||||
}
|
||||
}
|
||||
|
||||
/* Otherwise create a new node */
|
||||
node = g_new0 (SysprofCallgraphNode, 1);
|
||||
node->symbol = symbol;
|
||||
node->parent = parent;
|
||||
node->next = parent->children;
|
||||
if (parent->children)
|
||||
parent->children->prev = node;
|
||||
parent->children = node;
|
||||
|
||||
next_symbol:
|
||||
parent = node;
|
||||
}
|
||||
|
||||
return parent;
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_callgraph_add_traceable (SysprofCallgraph *self,
|
||||
SysprofDocumentTraceable *traceable,
|
||||
guint model_position)
|
||||
SysprofDocumentTraceable *traceable)
|
||||
{
|
||||
SysprofCallgraphTrace *trace;
|
||||
SysprofCallgraphNode *node;
|
||||
SysprofSymbol **symbols;
|
||||
guint stack_depth;
|
||||
guint n_symbols;
|
||||
int pid;
|
||||
|
||||
g_assert (SYSPROF_IS_CALLGRAPH (self));
|
||||
g_assert (SYSPROF_IS_DOCUMENT_TRACEABLE (traceable));
|
||||
|
||||
pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (traceable));
|
||||
stack_depth = sysprof_document_traceable_get_stack_depth (traceable);
|
||||
|
||||
if (stack_depth > MAX_STACK_DEPTH)
|
||||
if (stack_depth == 0 || stack_depth > MAX_STACK_DEPTH)
|
||||
return;
|
||||
|
||||
symbols = g_newa (SysprofSymbol *, stack_depth);
|
||||
symbols = g_newa (SysprofSymbol *, stack_depth + 2);
|
||||
n_symbols = sysprof_document_symbolize_traceable (self->document, traceable, symbols, stack_depth);
|
||||
|
||||
if (n_symbols == 0)
|
||||
return;
|
||||
g_assert (n_symbols > 0);
|
||||
g_assert (n_symbols <= stack_depth);
|
||||
|
||||
trace = g_malloc (sizeof *trace + (n_symbols * sizeof (SysprofCallgraphNode)));
|
||||
trace->model_position = model_position;
|
||||
trace->n_nodes = n_symbols;
|
||||
/* We saved 2 extra spaces for these above so that we can
|
||||
* tack on the "Process" symbol and the "Everything" symbol.
|
||||
*/
|
||||
symbols[n_symbols++] = _sysprof_document_process_symbol (self->document, pid);
|
||||
symbols[n_symbols++] = self->everything;
|
||||
|
||||
for (guint i = 0; i < n_symbols; i++)
|
||||
{
|
||||
trace->nodes[i].symbol = symbols[i];
|
||||
trace->nodes[i].children = NULL;
|
||||
trace->nodes[i].parent = NULL;
|
||||
trace->nodes[i].next = NULL;
|
||||
trace->nodes[i].prev = NULL;
|
||||
}
|
||||
node = sysprof_callgraph_add_trace (self, symbols, n_symbols);
|
||||
|
||||
sysprof_callgraph_add_trace (self, trace);
|
||||
if (node && self->augment_func)
|
||||
self->augment_func (self,
|
||||
node,
|
||||
SYSPROF_DOCUMENT_FRAME (traceable),
|
||||
self->augment_func_data);
|
||||
}
|
||||
|
||||
static void
|
||||
@ -181,18 +236,22 @@ sysprof_callgraph_new_worker (GTask *task,
|
||||
{
|
||||
g_autoptr(SysprofDocumentTraceable) traceable = g_list_model_get_item (self->traceables, i);
|
||||
|
||||
sysprof_callgraph_add_traceable (self, traceable, i);
|
||||
sysprof_callgraph_add_traceable (self, traceable);
|
||||
}
|
||||
|
||||
g_task_return_pointer (task, g_object_ref (self), g_object_unref);
|
||||
}
|
||||
|
||||
void
|
||||
_sysprof_callgraph_new_async (SysprofDocument *document,
|
||||
GListModel *traceables,
|
||||
GCancellable *cancellable,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data)
|
||||
_sysprof_callgraph_new_async (SysprofDocument *document,
|
||||
GListModel *traceables,
|
||||
gsize augment_size,
|
||||
SysprofAugmentationFunc augment_func,
|
||||
gpointer augment_func_data,
|
||||
GDestroyNotify augment_func_data_destroy,
|
||||
GCancellable *cancellable,
|
||||
GAsyncReadyCallback callback,
|
||||
gpointer user_data)
|
||||
{
|
||||
g_autoptr(SysprofCallgraph) self = NULL;
|
||||
g_autoptr(GTask) task = NULL;
|
||||
@ -204,6 +263,10 @@ _sysprof_callgraph_new_async (SysprofDocument *document,
|
||||
self = g_object_new (SYSPROF_TYPE_CALLGRAPH, NULL);
|
||||
self->document = g_object_ref (document);
|
||||
self->traceables = g_object_ref (traceables);
|
||||
self->augment_size = augment_size;
|
||||
self->augment_func = augment_func;
|
||||
self->augment_func_data = augment_func_data;
|
||||
self->augment_func_data_destroy = augment_func_data_destroy;
|
||||
|
||||
task = g_task_new (NULL, cancellable, callback, user_data);
|
||||
g_task_set_source_tag (task, _sysprof_callgraph_new_async);
|
||||
@ -219,3 +282,24 @@ _sysprof_callgraph_new_finish (GAsyncResult *result,
|
||||
|
||||
return g_task_propagate_pointer (G_TASK (result), error);
|
||||
}
|
||||
|
||||
gpointer
|
||||
sysprof_callgraph_get_augment (SysprofCallgraph *callgraph,
|
||||
SysprofCallgraphNode *node)
|
||||
{
|
||||
if (callgraph->augment_size == 0)
|
||||
return NULL;
|
||||
else if (callgraph->augment_size <= GLIB_SIZEOF_VOID_P)
|
||||
return &node->augment;
|
||||
|
||||
if (node->augment == NULL)
|
||||
node->augment = g_malloc0 (callgraph->augment_size);
|
||||
|
||||
return node->augment;
|
||||
}
|
||||
|
||||
SysprofCallgraphNode *
|
||||
sysprof_callgraph_node_parent (SysprofCallgraphNode *node)
|
||||
{
|
||||
return node->parent;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user