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:
@ -17,11 +17,11 @@ libsysprof_analyze_testsuite = {
|
||||
'test-print-file' : {'skip': true},
|
||||
'test-list-processes' : {'skip': true},
|
||||
'test-symbolize' : {'skip': true},
|
||||
'test-symbol-cache' : {},
|
||||
}
|
||||
|
||||
libsysprof_analyze_testsuite_deps = [
|
||||
libsysprof_analyze_dep,
|
||||
libsysprof_capture_dep,
|
||||
libsysprof_analyze_static_dep,
|
||||
]
|
||||
|
||||
foreach test, params: libsysprof_analyze_testsuite
|
||||
|
||||
171
src/libsysprof-analyze/tests/test-symbol-cache.c
Normal file
171
src/libsysprof-analyze/tests/test-symbol-cache.c
Normal file
@ -0,0 +1,171 @@
|
||||
/* test-symbol-cache.c
|
||||
*
|
||||
* Copyright 2023 Christian Hergert <chergert@redhat.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <sysprof-analyze.h>
|
||||
|
||||
#include "sysprof-symbol-private.h"
|
||||
#include "sysprof-symbol-cache-private.h"
|
||||
|
||||
typedef struct _SymbolInfo
|
||||
{
|
||||
const char *name;
|
||||
guint64 begin;
|
||||
guint64 end;
|
||||
int position;
|
||||
int sort;
|
||||
SysprofSymbol *symbol;
|
||||
} SymbolInfo;
|
||||
|
||||
static SysprofSymbol *
|
||||
create_symbol (const char *name,
|
||||
guint64 begin,
|
||||
guint64 end)
|
||||
{
|
||||
g_assert (begin < end);
|
||||
|
||||
return _sysprof_symbol_new (g_ref_string_new (name), NULL, NULL, begin, end);
|
||||
}
|
||||
|
||||
static int
|
||||
sort_by_key (gconstpointer a,
|
||||
gconstpointer b)
|
||||
{
|
||||
const SymbolInfo *info_a = a;
|
||||
const SymbolInfo *info_b = b;
|
||||
|
||||
if (info_a->sort < info_b->sort)
|
||||
return -1;
|
||||
else if (info_a->sort > info_b->sort)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
sort_by_position (gconstpointer a,
|
||||
gconstpointer b)
|
||||
{
|
||||
const SymbolInfo *info_a = a;
|
||||
const SymbolInfo *info_b = b;
|
||||
|
||||
if (info_a->position < info_b->position)
|
||||
return -1;
|
||||
else if (info_a->position > info_b->position)
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
test_interval_tree (void)
|
||||
{
|
||||
SysprofSymbolCache *symbol_cache = sysprof_symbol_cache_new ();
|
||||
SymbolInfo symbols[] = {
|
||||
{ "symbol1", 0x10000, 0x20000 },
|
||||
{ "symbol2", 0x20000, 0x30000 },
|
||||
{ "symbol3", 0x30000, 0x40000 },
|
||||
{ "symbol4", 0x90000, 0xa0000 },
|
||||
{ "symbol5", 0xb0000, 0xb0001 },
|
||||
{ "symbol6", 0xb0001, 0xb0002 },
|
||||
};
|
||||
|
||||
/* Add some randomness on insertion */
|
||||
for (guint i = 0; i < G_N_ELEMENTS (symbols); i++)
|
||||
{
|
||||
symbols[i].position = i;
|
||||
symbols[i].sort = g_random_int ();
|
||||
}
|
||||
|
||||
/* Sort randomly for insertion */
|
||||
qsort (symbols, G_N_ELEMENTS (symbols), sizeof (SymbolInfo), sort_by_key);
|
||||
for (guint i = 0; i < G_N_ELEMENTS (symbols); i++)
|
||||
{
|
||||
SymbolInfo *info = &symbols[i];
|
||||
|
||||
g_assert_cmpint (info->begin, <, info->end);
|
||||
|
||||
info->symbol = create_symbol (info->name, info->begin, info->end);
|
||||
|
||||
g_assert_nonnull (info->symbol);
|
||||
g_assert_true (SYSPROF_IS_SYMBOL (info->symbol));
|
||||
|
||||
sysprof_symbol_cache_take (symbol_cache, g_object_ref (info->symbol));
|
||||
}
|
||||
|
||||
/* Now resort to do lookups with edge checking */
|
||||
qsort (symbols, G_N_ELEMENTS (symbols), sizeof (SymbolInfo), sort_by_position);
|
||||
for (guint i = 0; i < G_N_ELEMENTS (symbols); i++)
|
||||
{
|
||||
const SymbolInfo *info = &symbols[i];
|
||||
const SymbolInfo *prev = i > 0 ? &symbols[i-1] : NULL;
|
||||
const SymbolInfo *next = i + 1 < G_N_ELEMENTS (symbols) ? &symbols[i+1] : NULL;
|
||||
SysprofSymbol *lookup;
|
||||
|
||||
g_assert_cmpint (info->position, ==, i);
|
||||
|
||||
lookup = sysprof_symbol_cache_lookup (symbol_cache, info->begin-1);
|
||||
if (prev && info->begin == prev->end)
|
||||
g_assert_true (lookup == prev->symbol);
|
||||
else
|
||||
g_assert_null (lookup);
|
||||
|
||||
lookup = sysprof_symbol_cache_lookup (symbol_cache, info->begin);
|
||||
g_assert_nonnull (lookup);
|
||||
g_assert_true (lookup == info->symbol);
|
||||
|
||||
lookup = sysprof_symbol_cache_lookup (symbol_cache, info->end);
|
||||
if (next == NULL || next->begin > info->end)
|
||||
g_assert_null (lookup);
|
||||
else
|
||||
g_assert_true (lookup == next->symbol);
|
||||
|
||||
if (info->begin+1 != info->end)
|
||||
{
|
||||
lookup = sysprof_symbol_cache_lookup (symbol_cache, info->begin+1);
|
||||
g_assert_nonnull (lookup);
|
||||
g_assert_true (lookup == info->symbol);
|
||||
}
|
||||
|
||||
lookup = sysprof_symbol_cache_lookup (symbol_cache, info->end-1);
|
||||
g_assert_nonnull (lookup);
|
||||
g_assert_true (lookup == info->symbol);
|
||||
|
||||
lookup = sysprof_symbol_cache_lookup (symbol_cache, info->begin + ((info->end-info->begin)/2));
|
||||
g_assert_nonnull (lookup);
|
||||
g_assert_true (lookup == info->symbol);
|
||||
}
|
||||
|
||||
g_assert_finalize_object (symbol_cache);
|
||||
|
||||
for (guint i = 0; i < G_N_ELEMENTS (symbols); i++)
|
||||
g_assert_finalize_object (symbols[i].symbol);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
g_test_add_func ("/libsysprof-analyze/SysprofSymbolCache/interval-tree",
|
||||
test_interval_tree);
|
||||
return g_test_run ();
|
||||
}
|
||||
Reference in New Issue
Block a user