mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2025-12-31 20:36:25 +00:00
libsysprof-analyze: use interval tree for symbol cache
This uses an augmented red-black tree to create an interval tree with non-interval lookups. That amounts to storing address ranges within the red-black tree, but looking up by single address.
This commit is contained in:
@ -20,23 +20,88 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
typedef struct _SysprofSymbolCacheNode SysprofSymbolCacheNode;
|
||||
static void sysprof_symbol_cache_node_augment (SysprofSymbolCacheNode *node);
|
||||
#define RB_AUGMENT(elem) sysprof_symbol_cache_node_augment(elem)
|
||||
|
||||
#include "tree.h"
|
||||
|
||||
#include "sysprof-symbol-private.h"
|
||||
#include "sysprof-symbol-cache-private.h"
|
||||
|
||||
struct _SysprofSymbolCacheNode
|
||||
{
|
||||
RB_ENTRY(_SysprofSymbolCacheNode) link;
|
||||
SysprofSymbol *symbol;
|
||||
guint64 low;
|
||||
guint64 high;
|
||||
guint64 max;
|
||||
};
|
||||
|
||||
struct _SysprofSymbolCache
|
||||
{
|
||||
GObject parent_instance;
|
||||
GSequence *symbols;
|
||||
GObject parent_instance;
|
||||
RB_HEAD(sysprof_symbol_cache, _SysprofSymbolCacheNode) head;
|
||||
};
|
||||
|
||||
G_DEFINE_FINAL_TYPE (SysprofSymbolCache, sysprof_symbol_cache, G_TYPE_OBJECT)
|
||||
|
||||
static inline int
|
||||
sysprof_symbol_cache_node_compare (SysprofSymbolCacheNode *a,
|
||||
SysprofSymbolCacheNode *b)
|
||||
{
|
||||
if (a->low < b->low)
|
||||
return -1;
|
||||
else if (a->low > b->low)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
RB_GENERATE_STATIC(sysprof_symbol_cache, _SysprofSymbolCacheNode, link, sysprof_symbol_cache_node_compare);
|
||||
|
||||
static void
|
||||
sysprof_symbol_cache_node_augment (SysprofSymbolCacheNode *node)
|
||||
{
|
||||
node->max = node->high;
|
||||
|
||||
if (RB_LEFT(node, link) && RB_LEFT(node, link)->max > node->max)
|
||||
node->max = RB_LEFT(node, link)->max;
|
||||
|
||||
if (RB_RIGHT(node, link) && RB_RIGHT(node, link)->max > node->max)
|
||||
node->max = RB_RIGHT(node, link)->max;
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_symbol_cache_node_finalize (SysprofSymbolCacheNode *node)
|
||||
{
|
||||
g_clear_object (&node->symbol);
|
||||
g_free (node);
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_symbol_cache_node_free (SysprofSymbolCacheNode *node)
|
||||
{
|
||||
SysprofSymbolCacheNode *right = RB_RIGHT(node, link);
|
||||
SysprofSymbolCacheNode *left = RB_LEFT(node, link);
|
||||
|
||||
if (left != NULL)
|
||||
sysprof_symbol_cache_node_free (left);
|
||||
|
||||
sysprof_symbol_cache_node_finalize (node);
|
||||
|
||||
if (right != NULL)
|
||||
sysprof_symbol_cache_node_free (right);
|
||||
}
|
||||
|
||||
static void
|
||||
sysprof_symbol_cache_finalize (GObject *object)
|
||||
{
|
||||
SysprofSymbolCache *self = (SysprofSymbolCache *)object;
|
||||
SysprofSymbolCacheNode *node = RB_ROOT(&self->head);
|
||||
|
||||
g_clear_pointer (&self->symbols, g_sequence_free);
|
||||
if (node != NULL)
|
||||
sysprof_symbol_cache_node_free (node);
|
||||
|
||||
G_OBJECT_CLASS (sysprof_symbol_cache_parent_class)->finalize (object);
|
||||
}
|
||||
@ -52,7 +117,7 @@ sysprof_symbol_cache_class_init (SysprofSymbolCacheClass *klass)
|
||||
static void
|
||||
sysprof_symbol_cache_init (SysprofSymbolCache *self)
|
||||
{
|
||||
self->symbols = g_sequence_new (g_object_unref);
|
||||
RB_INIT (&self->head);
|
||||
}
|
||||
|
||||
SysprofSymbolCache *
|
||||
@ -61,80 +126,105 @@ sysprof_symbol_cache_new (void)
|
||||
return g_object_new (SYSPROF_TYPE_SYMBOL_CACHE, NULL);
|
||||
}
|
||||
|
||||
static int
|
||||
sysprof_symbol_cache_compare (gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer user_data)
|
||||
#if 0
|
||||
static void
|
||||
print_tree (SysprofSymbolCacheNode *node)
|
||||
{
|
||||
const SysprofSymbol *sym_a = a;
|
||||
const SysprofSymbol *sym_b = b;
|
||||
SysprofSymbolCacheNode *left = RB_LEFT (node, link);
|
||||
SysprofSymbolCacheNode *right = RB_RIGHT (node, link);
|
||||
|
||||
if (sym_a->begin_address < sym_b->begin_address)
|
||||
return -1;
|
||||
g_print ("[%lx:%lx max=%lx];\n", node->low, node->high, node->max);
|
||||
|
||||
if (sym_a->begin_address > sym_b->end_address)
|
||||
return 1;
|
||||
if (left)
|
||||
{
|
||||
g_print ("[%lx:%lx]'L -> [%lx:%lx];\n",
|
||||
node->low, node->high,
|
||||
left->low, left->high);
|
||||
print_tree (left);
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (right)
|
||||
{
|
||||
g_print ("[%lx:%lx]'R -> [%lx:%lx];\n",
|
||||
node->low, node->high,
|
||||
right->low, right->high);
|
||||
print_tree (right);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* sysprof_symbol_cache_take:
|
||||
* @self: a #SysprofSymbolCache
|
||||
* @symbol: (transfer full): a #SysprofSymbol
|
||||
*
|
||||
*/
|
||||
void
|
||||
sysprof_symbol_cache_take (SysprofSymbolCache *self,
|
||||
SysprofSymbol *symbol)
|
||||
{
|
||||
SysprofSymbolCacheNode *node;
|
||||
SysprofSymbolCacheNode *parent;
|
||||
|
||||
g_return_if_fail (SYSPROF_IS_SYMBOL_CACHE (self));
|
||||
g_return_if_fail (SYSPROF_IS_SYMBOL (symbol));
|
||||
g_return_if_fail (symbol->end_address > symbol->begin_address);
|
||||
|
||||
if (symbol->begin_address == 0 || symbol->end_address == 0)
|
||||
return;
|
||||
/* Some symbols are not suitable for our interval tree */
|
||||
if (symbol->begin_address == 0 ||
|
||||
symbol->end_address == 0 ||
|
||||
symbol->begin_address == symbol->end_address)
|
||||
{
|
||||
g_object_unref (symbol);
|
||||
return;
|
||||
}
|
||||
|
||||
g_sequence_insert_sorted (self->symbols,
|
||||
g_object_ref (symbol),
|
||||
sysprof_symbol_cache_compare,
|
||||
NULL);
|
||||
}
|
||||
node = g_new0 (SysprofSymbolCacheNode, 1);
|
||||
node->symbol = symbol;
|
||||
node->low = symbol->begin_address;
|
||||
node->high = symbol->end_address-1;
|
||||
node->max = node->high;
|
||||
|
||||
static int
|
||||
sysprof_symbol_cache_lookup_func (gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer user_data)
|
||||
{
|
||||
const SysprofSymbol *sym_a = a;
|
||||
const gint64 *addr = b;
|
||||
RB_INSERT(sysprof_symbol_cache, &self->head, node);
|
||||
|
||||
if (*addr < sym_a->begin_address)
|
||||
return 1;
|
||||
parent = RB_PARENT(node, link);
|
||||
|
||||
if (*addr > sym_a->end_address)
|
||||
return -1;
|
||||
while (parent != NULL)
|
||||
{
|
||||
if (node->max > parent->max)
|
||||
parent->max = node->max;
|
||||
node = parent;
|
||||
parent = RB_PARENT(parent, link);
|
||||
}
|
||||
|
||||
return 0;
|
||||
#if 0
|
||||
g_print ("=====\n");
|
||||
print_tree (RB_ROOT (&self->head));
|
||||
#endif
|
||||
}
|
||||
|
||||
SysprofSymbol *
|
||||
sysprof_symbol_cache_lookup (SysprofSymbolCache *self,
|
||||
SysprofAddress address)
|
||||
{
|
||||
GSequenceIter *iter;
|
||||
SysprofSymbolCacheNode *node;
|
||||
|
||||
g_return_val_if_fail (SYSPROF_IS_SYMBOL_CACHE (self), NULL);
|
||||
|
||||
if (address == 0)
|
||||
return NULL;
|
||||
|
||||
iter = g_sequence_lookup (self->symbols,
|
||||
&address,
|
||||
sysprof_symbol_cache_lookup_func,
|
||||
NULL);
|
||||
node = RB_ROOT(&self->head);
|
||||
|
||||
if (iter != NULL)
|
||||
return g_sequence_get (iter);
|
||||
while (node != NULL)
|
||||
{
|
||||
g_assert (RB_LEFT(node, link) == NULL ||
|
||||
node->max >= RB_LEFT(node, link)->max);
|
||||
g_assert (RB_RIGHT(node, link) == NULL ||
|
||||
node->max >= RB_RIGHT(node, link)->max);
|
||||
|
||||
if (address >= node->low && address <= node->high)
|
||||
return node->symbol;
|
||||
|
||||
if (RB_LEFT(node, link) && RB_LEFT(node, link)->max >= address)
|
||||
node = RB_LEFT(node, link);
|
||||
else
|
||||
node = RB_RIGHT(node, link);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user