qemu-e2k/tests/unit/test-qtree.c

334 lines
7.0 KiB
C
Raw Normal View History

util: import GTree as QTree The only reason to add this implementation is to control the memory allocator used. Some users (e.g. TCG) cannot work reliably in multi-threaded environments (e.g. forking in user-mode) with GTree's allocator, GSlice. See https://gitlab.com/qemu-project/qemu/-/issues/285 for details. Importing GTree is a temporary workaround until GTree migrates away from GSlice. This implementation is identical to that in glib v2.75.0, except that we don't import recent additions to the API nor deprecated API calls, none of which are used in QEMU. I've imported tests from glib and added a benchmark just to make sure that performance is similar. Note: it cannot be identical because (1) we are not using GSlice, (2) we use different compilation flags (e.g. -fPIC) and (3) we're linking statically. $ cat /proc/cpuinfo| grep 'model name' | head -1 model name : AMD Ryzen 7 PRO 5850U with Radeon Graphics $ echo '0' | sudo tee /sys/devices/system/cpu/cpufreq/boost $ tests/bench/qtree-bench Tree Op 32 1024 4096 131072 1048576 ------------------------------------------------------------------------------------------------ GTree Lookup 83.23 43.08 25.31 19.40 16.22 QTree Lookup 113.42 (1.36x) 53.83 (1.25x) 28.38 (1.12x) 17.64 (0.91x) 13.04 (0.80x) GTree Insert 44.23 29.37 25.83 19.49 17.03 QTree Insert 46.87 (1.06x) 25.62 (0.87x) 24.29 (0.94x) 16.83 (0.86x) 12.97 (0.76x) GTree Remove 53.27 35.15 31.43 24.64 16.70 QTree Remove 57.32 (1.08x) 41.76 (1.19x) 38.37 (1.22x) 29.30 (1.19x) 15.07 (0.90x) GTree RemoveAll 135.44 127.52 126.72 120.11 64.34 QTree RemoveAll 127.15 (0.94x) 110.37 (0.87x) 107.97 (0.85x) 97.13 (0.81x) 55.10 (0.86x) GTree Traverse 277.71 276.09 272.78 246.72 98.47 QTree Traverse 370.33 (1.33x) 411.97 (1.49x) 400.23 (1.47x) 262.82 (1.07x) 78.52 (0.80x) ------------------------------------------------------------------------------------------------ As a sanity check, the same benchmark when Glib's version is >= $glib_dropped_gslice_version (i.e. QTree == GTree): Tree Op 32 1024 4096 131072 1048576 ------------------------------------------------------------------------------------------------ GTree Lookup 82.72 43.09 24.18 19.73 16.09 QTree Lookup 81.82 (0.99x) 43.10 (1.00x) 24.20 (1.00x) 19.76 (1.00x) 16.26 (1.01x) GTree Insert 45.07 29.62 26.34 19.90 17.18 QTree Insert 45.72 (1.01x) 29.60 (1.00x) 26.38 (1.00x) 19.71 (0.99x) 17.20 (1.00x) GTree Remove 54.48 35.36 31.77 24.97 16.95 QTree Remove 54.46 (1.00x) 35.32 (1.00x) 31.77 (1.00x) 24.91 (1.00x) 17.15 (1.01x) GTree RemoveAll 140.68 127.36 125.43 121.45 68.20 QTree RemoveAll 140.65 (1.00x) 127.64 (1.00x) 125.01 (1.00x) 121.73 (1.00x) 67.06 (0.98x) GTree Traverse 278.68 276.05 266.75 251.65 104.93 QTree Traverse 278.31 (1.00x) 275.78 (1.00x) 266.42 (1.00x) 247.89 (0.99x) 104.58 (1.00x) ------------------------------------------------------------------------------------------------ Signed-off-by: Emilio Cota <cota@braap.org> Message-Id: <20230205163758.416992-2-cota@braap.org> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
2023-02-05 17:37:57 +01:00
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* Tests for QTree.
* Original source: glib
* https://gitlab.gnome.org/GNOME/glib/-/blob/main/glib/tests/tree.c
* LGPL license.
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*/
#include "qemu/osdep.h"
#include "qemu/qtree.h"
static gint my_compare(gconstpointer a, gconstpointer b)
{
const char *cha = a;
const char *chb = b;
return *cha - *chb;
}
static gint my_compare_with_data(gconstpointer a,
gconstpointer b,
gpointer user_data)
{
const char *cha = a;
const char *chb = b;
/* just check that we got the right data */
g_assert(GPOINTER_TO_INT(user_data) == 123);
return *cha - *chb;
}
static gint my_search(gconstpointer a, gconstpointer b)
{
return my_compare(b, a);
}
static gpointer destroyed_key;
static gpointer destroyed_value;
static guint destroyed_key_count;
static guint destroyed_value_count;
static void my_key_destroy(gpointer key)
{
destroyed_key = key;
destroyed_key_count++;
}
static void my_value_destroy(gpointer value)
{
destroyed_value = value;
destroyed_value_count++;
}
static gint my_traverse(gpointer key, gpointer value, gpointer data)
{
char *ch = key;
g_assert((*ch) > 0);
if (*ch == 'd') {
return TRUE;
}
return FALSE;
}
char chars[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
char chars2[] =
"0123456789"
"abcdefghijklmnopqrstuvwxyz";
static gint check_order(gpointer key, gpointer value, gpointer data)
{
char **p = data;
char *ch = key;
g_assert(**p == *ch);
(*p)++;
return FALSE;
}
static void test_tree_search(void)
{
gint i;
QTree *tree;
gboolean removed;
gchar c;
gchar *p, *d;
tree = q_tree_new_with_data(my_compare_with_data, GINT_TO_POINTER(123));
for (i = 0; chars[i]; i++) {
q_tree_insert(tree, &chars[i], &chars[i]);
}
q_tree_foreach(tree, my_traverse, NULL);
g_assert(q_tree_nnodes(tree) == strlen(chars));
g_assert(q_tree_height(tree) == 6);
p = chars;
q_tree_foreach(tree, check_order, &p);
for (i = 0; i < 26; i++) {
removed = q_tree_remove(tree, &chars[i + 10]);
g_assert(removed);
}
c = '\0';
removed = q_tree_remove(tree, &c);
g_assert(!removed);
q_tree_foreach(tree, my_traverse, NULL);
g_assert(q_tree_nnodes(tree) == strlen(chars2));
g_assert(q_tree_height(tree) == 6);
p = chars2;
q_tree_foreach(tree, check_order, &p);
for (i = 25; i >= 0; i--) {
q_tree_insert(tree, &chars[i + 10], &chars[i + 10]);
}
p = chars;
q_tree_foreach(tree, check_order, &p);
c = '0';
p = q_tree_lookup(tree, &c);
g_assert(p && *p == c);
g_assert(q_tree_lookup_extended(tree, &c, (gpointer *)&d, (gpointer *)&p));
g_assert(c == *d && c == *p);
c = 'A';
p = q_tree_lookup(tree, &c);
g_assert(p && *p == c);
c = 'a';
p = q_tree_lookup(tree, &c);
g_assert(p && *p == c);
c = 'z';
p = q_tree_lookup(tree, &c);
g_assert(p && *p == c);
c = '!';
p = q_tree_lookup(tree, &c);
g_assert(p == NULL);
c = '=';
p = q_tree_lookup(tree, &c);
g_assert(p == NULL);
c = '|';
p = q_tree_lookup(tree, &c);
g_assert(p == NULL);
c = '0';
p = q_tree_search(tree, my_search, &c);
g_assert(p && *p == c);
c = 'A';
p = q_tree_search(tree, my_search, &c);
g_assert(p && *p == c);
c = 'a';
p = q_tree_search(tree, my_search, &c);
g_assert(p && *p == c);
c = 'z';
p = q_tree_search(tree, my_search, &c);
g_assert(p && *p == c);
c = '!';
p = q_tree_search(tree, my_search, &c);
g_assert(p == NULL);
c = '=';
p = q_tree_search(tree, my_search, &c);
g_assert(p == NULL);
c = '|';
p = q_tree_search(tree, my_search, &c);
g_assert(p == NULL);
q_tree_destroy(tree);
}
static void test_tree_remove(void)
{
QTree *tree;
char c, d;
gint i;
gboolean removed;
tree = q_tree_new_full((GCompareDataFunc)my_compare, NULL,
my_key_destroy,
my_value_destroy);
for (i = 0; chars[i]; i++) {
q_tree_insert(tree, &chars[i], &chars[i]);
}
c = '0';
q_tree_insert(tree, &c, &c);
g_assert(destroyed_key == &c);
g_assert(destroyed_value == &chars[0]);
destroyed_key = NULL;
destroyed_value = NULL;
d = '1';
q_tree_replace(tree, &d, &d);
g_assert(destroyed_key == &chars[1]);
g_assert(destroyed_value == &chars[1]);
destroyed_key = NULL;
destroyed_value = NULL;
c = '2';
removed = q_tree_remove(tree, &c);
g_assert(removed);
g_assert(destroyed_key == &chars[2]);
g_assert(destroyed_value == &chars[2]);
destroyed_key = NULL;
destroyed_value = NULL;
c = '3';
removed = q_tree_steal(tree, &c);
g_assert(removed);
g_assert(destroyed_key == NULL);
g_assert(destroyed_value == NULL);
const gchar *remove = "omkjigfedba";
for (i = 0; remove[i]; i++) {
removed = q_tree_remove(tree, &remove[i]);
g_assert(removed);
}
q_tree_destroy(tree);
}
static void test_tree_destroy(void)
{
QTree *tree;
gint i;
tree = q_tree_new(my_compare);
for (i = 0; chars[i]; i++) {
q_tree_insert(tree, &chars[i], &chars[i]);
}
g_assert(q_tree_nnodes(tree) == strlen(chars));
g_test_message("nnodes: %d", q_tree_nnodes(tree));
q_tree_ref(tree);
q_tree_destroy(tree);
g_test_message("nnodes: %d", q_tree_nnodes(tree));
g_assert(q_tree_nnodes(tree) == 0);
q_tree_unref(tree);
}
static void test_tree_insert(void)
{
QTree *tree;
gchar *p;
gint i;
gchar *scrambled;
tree = q_tree_new(my_compare);
for (i = 0; chars[i]; i++) {
q_tree_insert(tree, &chars[i], &chars[i]);
}
p = chars;
q_tree_foreach(tree, check_order, &p);
q_tree_unref(tree);
tree = q_tree_new(my_compare);
for (i = strlen(chars) - 1; i >= 0; i--) {
q_tree_insert(tree, &chars[i], &chars[i]);
}
p = chars;
q_tree_foreach(tree, check_order, &p);
q_tree_unref(tree);
tree = q_tree_new(my_compare);
scrambled = g_strdup(chars);
for (i = 0; i < 30; i++) {
gchar tmp;
gint a, b;
a = g_random_int_range(0, strlen(scrambled));
b = g_random_int_range(0, strlen(scrambled));
tmp = scrambled[a];
scrambled[a] = scrambled[b];
scrambled[b] = tmp;
}
for (i = 0; scrambled[i]; i++) {
q_tree_insert(tree, &scrambled[i], &scrambled[i]);
}
p = chars;
q_tree_foreach(tree, check_order, &p);
g_free(scrambled);
q_tree_unref(tree);
}
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/qtree/search", test_tree_search);
g_test_add_func("/qtree/remove", test_tree_remove);
g_test_add_func("/qtree/destroy", test_tree_destroy);
g_test_add_func("/qtree/insert", test_tree_insert);
return g_test_run();
}