fix for #285
cover fix for #285
Context:
https://gitlab.com/qemu-project/qemu/-/issues/285
So far the only fix that we have had posted on the list is
https://lists.gnu.org/archive/html/qemu-devel/2022-10/msg00391.html
by Daniel. The approach that I'm following here should have the same outcome, except that it doesn't change the guest's environment. The approach is to import GTree (sans GSlice) into QEMU, and use that for TCG.
Daniel: what is the testing that you're using? Could you test these patches to confirm they fix the issue?
Regarding performance, it looks like GSlice does buy us something, which might explain why GLib's maintainers don't want to change it. But I'd put correctness over performance any day. Furthermore, we could use an alternative tree implementation; I've tried CCAN's AVL and the performance impact is lower (I believe due to faster traversals), although I'm going with a straight import of GTree here to keep the API identical (and also avoid any potential correctness concerns).
Thanks,
Emilio
PATCH 1/2 util: import GTree as QTree
The only reason to add this tree 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. I've imported tests from glib and added a benchmark just to make sure that performance is similar (Note: it cannot be identical because we are not using GSlice).
$ taskset -c 2 tests/bench/qtree-bench
- With libc's allocator:
Tree Op 32 1024 4096 131072 1048576
------------------------------------------------------------------------------------------------
GTree Lookup 14.01 15.17 24.93 18.99 15.28 QTree Lookup 22.50 (1.61x) 32.49 (2.14x) 29.84 (1.20x) 16.77 (0.88x) 12.21 (0.80x) GTree Insert 19.24 15.72 25.24 17.87 16.55 QTree Insert 15.07 (0.78x) 26.70 (1.70x) 25.68 (1.02x) 17.20 (0.96x) 12.49 (0.75x) GTree Remove 11.57 31.44 29.77 20.88 16.60 QTree Remove 14.01 (1.21x) 34.54 (1.10x) 33.52 (1.13x) 26.64 (1.28x) 14.95 (0.90x) GTree RemoveAll 57.97 119.13 118.16 112.82 61.63 QTree RemoveAll 46.31 (0.80x) 108.04 (0.91x) 113.85 (0.96x) 77.88 (0.69x) 41.69 (0.68x) GTree Traverse 72.56 232.83 243.20 254.22 97.44 QTree Traverse 66.53 (0.92x) 394.76 (1.70x) 357.07 (1.47x) 289.09 (1.14x) 45.64 (0.47x)
------------------------------------------------------------------------------------------------
- With tcmalloc:
Tree Op 32 1024 4096 131072 1048576
------------------------------------------------------------------------------------------------
GTree Lookup 24.56 27.69 25.78 17.14 15.90 QTree Lookup 40.92 (1.67x) 34.04 (1.23x) 30.15 (1.17x) 22.93 (1.34x) 20.31 (1.28x) GTree Insert 33.97 28.22 25.66 17.37 16.07 QTree Insert 34.01 (1.00x) 36.35 (1.29x) 32.29 (1.26x) 22.32 (1.28x) 17.62 (1.10x) GTree Remove 20.61 32.42 30.80 16.96 15.93 QTree Remove 20.61 (1.00x) 43.60 (1.35x) 41.71 (1.35x) 25.04 (1.48x) 16.33 (1.03x) GTree RemoveAll 106.31 125.72 126.49 70.89 54.60 QTree RemoveAll 83.99 (0.79x) 207.75 (1.65x) 206.17 (1.63x) 53.35 (0.75x) 46.38 (0.85x) GTree Traverse 128.00 243.93 255.20 140.39 90.94 QTree Traverse 110.34 (0.86x) 325.49 (1.33x) 376.82 (1.48x) 118.22 (0.84x) 62.25 (0.68x)
------------------------------------------------------------------------------------------------
--- include/qemu/qtree.h | 119 +++ tests/bench/meson.build | 4 + tests/bench/qtree-bench.c | 273 ++++++ tests/unit/meson.build | 1 + tests/unit/test-qtree.c | 701 +++++++++++++++ util/meson.build | 1 + util/qtree.c | 1776 +++++++++++++++++++++++++++++++++++++ 7 files changed, 2875 insertions(+) create mode 100644 include/qemu/qtree.h create mode 100644 tests/bench/qtree-bench.c create mode 100644 tests/unit/test-qtree.c create mode 100644 util/qtree.c
include/qemu/qtree.h new file +119 -0
@@ -0,0 +1,119 @@ +/* +* GLIB - Library of useful routines for C programming +* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald +* +* SPDX-License-Identifier: LGPL-2.1-or-later +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library 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 +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, see <http://www.gnu.org/licenses/>. +*/ + +/* +* Modified by the GLib Team and others 1997-2000. See the AUTHORS +* file for a list of people on the GLib Team. See the ChangeLog +* files for a list of changes. These files are distributed with +* GLib at ftp://ftp.gtk.org/pub/gtk/. +*/ + +#ifndef QEMU_QTREE_H +#define QEMU_QTREE_H + +typedef struct _QTree QTree ; + +typedef struct _QTreeNode QTreeNode ; + +typedef gboolean (*QTraverseNodeFunc )(QTreeNode *node, +gpointer user_data); + +/* +* Balanced binary trees +*/ +QTree *q_tree_new (GCompareFunc key_compare_func); +QTree *q_tree_new_with_data (GCompareDataFunc key_compare_func, +gpointer key_compare_data); +QTree *q_tree_new_full (GCompareDataFunc key_compare_func, +gpointer key_compare_data, +GDestroyNotify key_destroy_func, +GDestroyNotify value_destroy_func); +QTreeNode *q_tree_node_first (QTree *tree); + +QTreeNode *q_tree_node_last (QTree *tree); + +QTreeNode *q_tree_node_previous (QTreeNode *node); + +QTreeNode *q_tree_node_next (QTreeNode *node); + +QTree *q_tree_ref (QTree *tree); + +void q_tree_unref (QTree *tree); + +void q_tree_destroy (QTree *tree); + +QTreeNode *q_tree_insert_node (QTree *tree, +gpointer key, +gpointer value); + +void q_tree_insert (QTree *tree, +gpointer key, +gpointer value); + +QTreeNode *q_tree_replace_node (QTree *tree, +gpointer key, +gpointer value); + +void q_tree_replace (QTree *tree, +gpointer key, +gpointer value); +gboolean q_tree_remove (QTree *tree, +gconstpointer key); + +void q_tree_remove_all (QTree *tree); + +gboolean q_tree_steal (QTree *tree, +gconstpointer key); +gpointer q_tree_node_key (QTreeNode *node); +gpointer q_tree_node_value (QTreeNode *node); +QTreeNode *q_tree_lookup_node (QTree *tree, +gconstpointer key); +gpointer q_tree_lookup (QTree *tree, +gconstpointer key); +gboolean q_tree_lookup_extended (QTree *tree, +gconstpointer lookup_key, +gpointer *orig_key, +gpointer *value); +void q_tree_foreach (QTree *tree, +GTraverseFunc func, +gpointer user_data); +void q_tree_foreach_node (QTree *tree, +QTraverseNodeFunc func, +gpointer user_data); + +void q_tree_traverse (QTree *tree, +GTraverseFunc traverse_func, +GTraverseType traverse_type, +gpointer user_data); + +QTreeNode *q_tree_search_node (QTree *tree, +GCompareFunc search_func, +gconstpointer user_data); +gpointer q_tree_search (QTree *tree, +GCompareFunc search_func, +gconstpointer user_data); +QTreeNode *q_tree_lower_bound (QTree *tree, +gconstpointer key); +QTreeNode *q_tree_upper_bound (QTree *tree, +gconstpointer key); +gint q_tree_height (QTree *tree); +gint q_tree_nnodes (QTree *tree); + +#endif /* QEMU_QTREE_H */
tests/bench/meson.build +4 -0
@@ -3,6 +3,10 @@ qht_bench = executable('qht-bench', sources: 'qht-bench.c', dependencies: [qemuutil]) +qtree_bench = executable('qtree-bench', + sources: 'qtree-bench.c', + dependencies: [qemuutil]) + executable('atomic_add-bench', sources: files('atomic_add-bench.c'), dependencies: [qemuutil],
tests/bench/qtree-bench.c new file +273 -0
273 lines not on this page · raw message
tests/unit/meson.build +1 -0
@@ -35,6 +35,7 @@ tests = { 'test-rcu-slist': [], 'test-qdist': [], 'test-qht': [], + 'test-qtree': [], 'test-bitops': [], 'test-bitcnt': [], 'test-qgraph': ['../qtest/libqos/qgraph.c'],
tests/unit/test-qtree.c new file +701 -0
701 lines not on this page · raw message
util/meson.build +1 -0
@@ -26,6 +26,7 @@ util_ss.add(when: 'CONFIG_WIN32', if_true: files('oslib-win32.c')) util_ss.add(when: 'CONFIG_WIN32', if_true: files('qemu-thread-win32.c')) util_ss.add(when: 'CONFIG_WIN32', if_true: winmm) util_ss.add(when: 'CONFIG_WIN32', if_true: pathcch) +util_ss.add(files('qtree.c')) util_ss.add(files('envlist.c', 'path.c', 'module.c')) util_ss.add(files('host-utils.c')) util_ss.add(files('bitmap.c', 'bitops.c'))
util/qtree.c new file +1776 -0
1,776 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH 1/2] util: import GTree as QTree
Quoted 29 lines · Emilio Cota
The only reason to add this tree 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. I've imported tests from glib and added a benchmark just to make sure that performance is similar (Note: it cannot be identical because we are not using GSlice).
$ taskset -c 2 tests/bench/qtree-bench- With libc's allocator:
Tree Op 32 1024 4096 131072 1048576
------------------------------------------------------------------------------------------------GTree Lookup 14.01 15.17 24.93 18.99 15.28 QTree Lookup 22.50 (1.61x) 32.49 (2.14x) 29.84 (1.20x) 16.77 (0.88x) 12.21 (0.80x) GTree Insert 19.24 15.72 25.24 17.87 16.55 QTree Insert 15.07 (0.78x) 26.70 (1.70x) 25.68 (1.02x) 17.20 (0.96x) 12.49 (0.75x) GTree Remove 11.57 31.44 29.77 20.88 16.60 QTree Remove 14.01 (1.21x) 34.54 (1.10x) 33.52 (1.13x) 26.64 (1.28x) 14.95 (0.90x) GTree RemoveAll 57.97 119.13 118.16 112.82 61.63 QTree RemoveAll 46.31 (0.80x) 108.04 (0.91x) 113.85 (0.96x) 77.88 (0.69x) 41.69 (0.68x) GTree Traverse 72.56 232.83 243.20 254.22 97.44 QTree Traverse 66.53 (0.92x) 394.76 (1.70x) 357.07 (1.47x) 289.09 (1.14x) 45.64 (0.47x)
Quoted 1 line
------------------------------------------------------------------------------------------------
Well this is rather strange, as it doesn't really match what I see when running your test benchmark !
First, I find the test to be a little unreliable the first few times it is ran. I ran it in a loop 20 times and it got more stable results. Looking at just the QTree lines I get something typically like:
QTree Lookup 21.43 (1.33x) 17.99 (1.03x) 16.71 (1.07x) 10.01 (0.75x) 4.51 (0.40x) QTree Insert 12.65 (0.81x) 12.65 (0.76x) 11.94 (0.76x) 7.71 (0.59x) 4.30 (0.39x) QTree Remove 12.77 (1.09x) 18.34 (1.09x) 17.68 (1.07x) 13.65 (1.00x) 8.65 (0.76x) QTree RemoveAll 35.05 (1.01x) 40.17 (1.10x) 30.70 (0.88x) 42.06 (1.25x) 27.13 (1.14x) QTree Traverse 72.40 (1.12x) 180.95 (1.24x) 138.17 (1.09x) 146.29 (1.21x) 51.62 (1.29x)
So it is slower on small Lookup, and slower on Traverse. On large Lookup and Insert malloc is massively faster.
One thing to bear in mind is that if setting G_SLICE=always-malloc, we should in theory see the exact same results for GTree and QTree. So for a sanity check, I tried the test with that env set and get:
QTree Lookup 21.72 (1.31x) 19.04 (1.05x) 16.65 (1.01x) 9.94 (1.06x) 7.19 (1.06x) QTree Insert 14.71 (1.25x) 12.59 (1.07x) 11.83 (1.04x) 7.48 (0.99x) 5.72 (0.96x) QTree Remove 12.48 (1.02x) 18.58 (1.01x) 17.89 (1.01x) 11.68 (0.97x) 8.96 (1.11x) QTree RemoveAll 31.47 (1.04x) 39.71 (1.16x) 37.84 (1.13x) 37.11 (1.15x) 24.56 (1.04x) QTree Traverse 74.77 (1.47x) 179.21 (1.28x) 164.88 (1.15x) 126.17 (1.07x) 42.18 (1.00x)
That's odd - all values ought to be 1.00 or very close.
This tells me that the nelements=32 data is not to be trusted.
There's also something wierd going on with Traverse - it is always slower in QTree, even when both QTree and GTree are using malloc. There must be some wierd cache effects from the locally linked vs .so lib executed calls IMHO.
So overall if I ignore the unreliable results, my take away is that malloc is pretty much always a win over gslice, sometimes massively so, but at least shouldn't be worse.
NB, I'm using Fedora 37 with glibc. Mileage may vary with different libc impls.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 1/2] util: import GTree as QTree
Quoted 4 lines · Daniel P. Berrangé
First, I find the test to be a little unreliable the first few times it is ran. I ran it in a loop 20 times and it got more stable results. Looking at just the QTree lines I get something typically like:
Agreed, this is a problem in the benchmarks as written. I've changed them now to run for at least 200ms, which seems to stabilise results on my machine. (See the appended patch.)
Quoted 2 lines
One thing to bear in mind is that if setting G_SLICE=always-malloc, we should in theory see the exact same results for GTree and QTree.
Not quite exact same results.
There are a couple of differences that matter in this context:
- We're comparing a shared library, compiled most likely with -fPIC, against a static library without fPIC. I've done some tests and indeed using fPIC on qtree makes a difference on the generated code and in the resulting benchmark performance.
- With G_SLICE=always-malloc we are still going through g_slice to then defer to the system's malloc. Should not matter much except in microbenchmarks like these.
That said, I have not been able to get a 1:1 match of perf results between qtree and gtree, even after compiling fPIC on qtree, compiling Glib myself, and modifying qtree to use gslice.
I've also tried tcmalloc and glibc.
One thing I didn't try due to lack of time is to make qtree into a shared library and benchmark that -- I think that would finally give us identical results.
Quoted 6 lines
So overall if I ignore the unreliable results, my take away is that malloc is pretty much always a win over gslice, sometimes massively so, but at least shouldn't be worse.
NB, I'm using Fedora 37 with glibc. Mileage may vary with different libc impls.
I have to agree, I just wanted to be honest by sharing the numbers I had, but in fairness I didn't put enough time in getting those numbers for them to be useful, which is tricky when dealing with microbenchmarks.
Thanks,
E.
---
tests/bench/qtree-bench.c +18 -5
@@ -118,9 +118,9 @@ static inline void remove_all(void *tree, enum impl_type impl) } } -static double run_benchmark (const struct benchmark *bench, -enum impl_type impl, -size_t n_elems) +static int64_t run_benchmark (const struct benchmark *bench, +enum impl_type impl, +size_t n_elems) {void *tree;size_t *keys;
@@ -212,7 +212,7 @@ static double run_benchmark(const struct benchmark *bench, }g_free (keys); -return (double )n_elems / ns *1e3 ; +return ns; }int main (int argc,char *argv[])
@@ -232,7 +232,20 @@ int main(int argc, char *argv[])const struct tree_implementation *impl = &impls[j];for (int k =0 ; k <ARRAY_SIZE (benchmarks); k++) {const struct benchmark *bench = &benchmarks[k]; - res[k][j][i] =run_benchmark (bench, impl->type, size); + +/* warm-up run */ +run_benchmark (bench, impl->type, size); + +int64_t total_ns =0 ; +int64_t n_runs =0 ; +while (total_ns <2e8 || n_runs <3 ) { + total_ns +=run_benchmark (bench, impl->type, size); + n_runs++; + } +double ns_per_run = (double )total_ns / n_runs; + +// Throughput, in Mops/s. + res[k][j][i] = size / ns_per_run *1e3 ; } } }
PATCH 2/2 tcg: use QTree instead of GTree
In reply to Emilio Cotaqemu-user can hang in a multi-threaded fork. One common reason is that when creating a TB, between fork and exec we manipulate a GTree whose memory allocator (GSlice) is not fork-safe.
Although POSIX does not mandate it, the system's allocator (e.g. tcmalloc, libc malloc) is probably fork-safe.
Fix some of these hangs by using QTree, which uses the system's allocator.
For more details, see:
https://gitlab.com/qemu-project/qemu/-/issues/285
Performance impact on linux-user:
- ~2% slowdown in spec06
- 1.05% slowdown in Nbench-int
- 4.51% slowdown in Nbench-fp
x86_64 spec06
Host: AMD Ryzen 7 PRO 5850U with Radeon Graphics
1.04 +--------------------------------------------------------------------------------------------------+
| | |
| qtree-gcc1|.int |
| +-+ | |
1.02 |-+...............................................|..................................|...........+-|
| | | |
| +-+ | | |
1 |-+..........+-+............|............+-+......|..........................+-+.....|...........+-|
| **|** | *|** | *+-+ | | |
| *+-+* | +-+* | * * +-+ **|** | |
| * * +-+ *|** * * **|* * * | *+-+* | +-+ |
0.98 |-+.........*...*....|.....*|.*..........*..*...*.|*...*..*...**|*..........*...*....|....**|**..+-|
| * * **|** *| * +-+ * * * |* * * *+-+ +-+ * * **|** *+-+* |
| *+-+* * * * | * +-+* | * * * |* * * * * | * * * | * * * |
| *+-+* * * *+-+* * * | * * * |* * * * * | * * * | * * * |
0.96 |-+..*...*..*...*..*...*...*..*...*|**...*..*...*.|*...*..*...*..*.....|....*...*..*.|.*..*...*..+-|
| * * * * * * * * *| * * * * |* * * * * **|* * * * | * * * |
| * * * * * * * * +-+* * * *+-+ * * * * * |* * * * | * * * |
| * * * * * * * * * * * * * * * * * * * |* * * * | * * * |
0.94 |-+..*...*..*...*..*...*...*..*...*..*...*..*...*..*...*..*...*..*...*.|*...*...*..*.|.*..*...*..+-|
| * * * * * * * * * * * * * * * * * * * |* * * * | * * * |
| * * * * * * * * * * * * * * * * * * *+-+ * * * | * * * |
0.92 |-+..*...*..*...*..*...*...*..*...*..*...*..*...*..*...*..*...*..*...*..*...*...*..*.|.*..*...*..+-|
| * * * * * * * * * * * * * * * * * * * * * * *+-+* * * |
| * * * * * * * * * * * * * * * * * * * * * * * * * * |
| * * * * * * * * * * * * * * * * * * * * * * * * * * | 0.9 +--------------------------------------------------------------------------------------------------+
400.perlben401.bzip2403.gcc429.m445.gob456.hmme45462.libqua464.h26471.omnet473483.xalancbmkgeomean
aarch64 NBench Integer Performance
Host: AMD Ryzen 7 PRO 5850U with Radeon Graphics
81.2 +----------------------------------------------------------------+
| + + |
| *** |
81 |-+ B +-|
| **## |
80.8 |-+ ## +-|
| ## |
| ## |
80.6 |-+ ## +-|
| ## |
| ## |
80.4 |-+ ## +-|
| ## |
80.2 |-+ ##** +-|
| B |
| + * |
80 +----------------------------------------------------------------+
master-gcc12 qtree-gcc12
QEMU version
aarch64 NBench Floating Point Performance
Host: AMD Ryzen 7 PRO 5850U with Radeon Graphics
13.8 +----------------------------------------------------------------+
| *B* + |
13.7 |-+ **## +-|
| ## |
13.6 |-+ # +-|
| ## |
13.5 |-+ # +-|
| ## |
13.4 |-+ ## +-|
| # |
13.3 |-+ ## +-|
| # |
13.2 |-+ ## +-|
| ##** |
13.1 |-+ B +-|
| + *** |
13 +----------------------------------------------------------------+
master-gcc12 qtree-gcc12
QEMU version
--- accel/tcg/tb-maint.c | 17 +++++++++-------- tcg/region.c | 19 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-)
accel/tcg/tb-maint.c +9 -8
@@ -19,6 +19,7 @@#include "qemu/osdep.h" #include "qemu/interval-tree.h" +#include "qemu/qtree.h" #include "exec/cputlb.h" #include "exec/log.h" #include "exec/exec-all.h"
@@ -313,7 +314,7 @@ struct page_entry {* See also: page_collection_lock(). */ struct page_collection { -GTree *tree; +QTree *tree;struct page_entry *max; };
@@ -466,7 +467,7 @@ static bool page_trylock_add(struct page_collection *set, tb_page_addr_t addr)struct page_entry *pe;PageDesc *pd; - pe =g_tree_lookup (set->tree, &index); + pe =q_tree_lookup (set->tree, &index);if (pe) {return false; }
@@ -477,7 +478,7 @@ static bool page_trylock_add(struct page_collection *set, tb_page_addr_t addr) } pe =page_entry_new (pd, index); -g_tree_insert (set->tree, &pe->index, pe); +q_tree_insert (set->tree, &pe->index, pe);/* * If this is either (1) the first insertion or (2) a page whose index
@@ -524,13 +525,13 @@ static struct page_collection *page_collection_lock(tb_page_addr_t start, end >>=TARGET_PAGE_BITS ;g_assert (start <= end); - set->tree =g_tree_new_full (tb_page_addr_cmp,NULL ,NULL , + set->tree =q_tree_new_full (tb_page_addr_cmp,NULL ,NULL , page_entry_destroy); set->max =NULL ;assert_no_pages_locked (); retry: -g_tree_foreach (set->tree, page_entry_lock,NULL ); +q_tree_foreach (set->tree, page_entry_lock,NULL );for (index = start; index <= end; index++) {TranslationBlock *tb;
@@ -541,7 +542,7 @@ static struct page_collection *page_collection_lock(tb_page_addr_t start,continue ; }if (page_trylock_add (set, index <<TARGET_PAGE_BITS )) { -g_tree_foreach (set->tree, page_entry_unlock,NULL ); +q_tree_foreach (set->tree, page_entry_unlock,NULL ); goto retry; }assert_page_locked (pd);
@@ -550,7 +551,7 @@ static struct page_collection *page_collection_lock(tb_page_addr_t start, (tb_page_addr1(tb) != -1 && page_trylock_add(set, tb_page_addr1(tb)))) {/* drop all locks, and reacquire in order */ -g_tree_foreach (set->tree, page_entry_unlock,NULL ); +q_tree_foreach (set->tree, page_entry_unlock,NULL ); goto retry; } }
@@ -561,7 +562,7 @@ static struct page_collection *page_collection_lock(tb_page_addr_t start,static void page_collection_unlock (struct page_collection *set) {/* entries are unlocked and freed via page_entry_destroy */ -g_tree_destroy (set->tree); +q_tree_destroy (set->tree);g_free (set); }
tcg/region.c +10 -9
@@ -28,6 +28,7 @@#include "qemu/mprotect.h" #include "qemu/memalign.h" #include "qemu/cacheinfo.h" +#include "qemu/qtree.h" #include "qapi/error.h" #include "exec/exec-all.h" #include "tcg/tcg.h"
@@ -36,7 +37,7 @@struct tcg_region_tree {QemuMutex lock; -GTree *tree; +QTree *tree;/* padding to avoid false sharing is computed at run-time */ };
@@ -163,7 +164,7 @@ static void tcg_region_trees_init(void)struct tcg_region_tree *rt = region_trees + i * tree_size;qemu_mutex_init (&rt->lock); - rt->tree =g_tree_new_full (tb_tc_cmp,NULL ,NULL , tb_destroy); + rt->tree =q_tree_new_full (tb_tc_cmp,NULL ,NULL , tb_destroy); } }
@@ -202,7 +203,7 @@ void tcg_tb_insert(TranslationBlock *tb)g_assert (rt !=NULL );qemu_mutex_lock (&rt->lock); -g_tree_insert (rt->tree, &tb->tc, tb); +q_tree_insert (rt->tree, &tb->tc, tb);qemu_mutex_unlock (&rt->lock); }
@@ -212,7 +213,7 @@ void tcg_tb_remove(TranslationBlock *tb)g_assert (rt !=NULL );qemu_mutex_lock (&rt->lock); -g_tree_remove (rt->tree, &tb->tc); +q_tree_remove (rt->tree, &tb->tc);qemu_mutex_unlock (&rt->lock); }
@@ -232,7 +233,7 @@ TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr) }qemu_mutex_lock (&rt->lock); - tb =g_tree_lookup (rt->tree, &s); + tb =q_tree_lookup (rt->tree, &s);qemu_mutex_unlock (&rt->lock);return tb; }
@@ -267,7 +268,7 @@ void tcg_tb_foreach(GTraverseFunc func, gpointer user_data)for (i =0 ; i < region.n; i++) {struct tcg_region_tree *rt = region_trees + i * tree_size; -g_tree_foreach (rt->tree, func, user_data); +q_tree_foreach (rt->tree, func, user_data); }tcg_region_tree_unlock_all (); }
@@ -281,7 +282,7 @@ size_t tcg_nb_tbs(void)for (i =0 ; i < region.n; i++) {struct tcg_region_tree *rt = region_trees + i * tree_size; - nb_tbs +=g_tree_nnodes (rt->tree); + nb_tbs +=q_tree_nnodes (rt->tree); }tcg_region_tree_unlock_all ();return nb_tbs;
@@ -296,8 +297,8 @@ static void tcg_region_tree_reset_all(void)struct tcg_region_tree *rt = region_trees + i * tree_size;/* Increment the refcount first so that destroy acts as a reset */ -g_tree_ref (rt->tree); -g_tree_destroy (rt->tree); +q_tree_ref (rt->tree); +q_tree_destroy (rt->tree); }tcg_region_tree_unlock_all (); }
Signature · 1 line
2.34.1
Re: [PATCH 2/2] tcg: use QTree instead of GTree
Quoted 11 lines · Emilio Cota
qemu-user can hang in a multi-threaded fork. One common reason is that when creating a TB, between fork and exec we manipulate a GTree whose memory allocator (GSlice) is not fork-safe.
Although POSIX does not mandate it, the system's allocator (e.g. tcmalloc, libc malloc) is probably fork-safe.
Fix some of these hangs by using QTree, which uses the system's allocator.
Quoted 7 lines
For more details, see:
https://gitlab.com/qemu-project/qemu/-/issues/285Performance impact on linux-user:
- ~2% slowdown in spec06
- 1.05% slowdown in Nbench-int
- 4.51% slowdown in Nbench-fp
What do you get *before* applying this patch, if you just run linux-user with G_SLICE=always-malloc set ?
Also what libc impl were you testing with ? glibc or musl or something else ?
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 2/2] tcg: use QTree instead of GTree
Quoted 11 lines · Daniel P. Berrangé
Quoted 4 lines · Emilio Cota
Performance impact on linux-user:
- ~2% slowdown in spec06
- 1.05% slowdown in Nbench-int
- 4.51% slowdown in Nbench-fpWhat do you get *before* applying this patch, if you just run linux-user with G_SLICE=always-malloc set ?
Also what libc impl were you testing with ? glibc or musl or something else ?
I've now done a few runs of nbench and the measurements I'm getting are within noise. So I'd say no perf difference.
For SPEC I don't have time right now although I could do more runs if you think they're needed.
I'm tempted for v2 to just remove these macrobenchmark numbers, since with perf we can see that little time is spent on gtree anyway, and at the moment I don't have time to do proper benchmarking.
Thanks,
E.
Re: [PATCH 2/2] tcg: use QTree instead of GTree
In reply to Emilio CotaQuoted 4 lines · Emilio Cota
qemu-user can hang in a multi-threaded fork. One common reason is that when creating a TB, between fork and exec we manipulate a GTree whose memory allocator (GSlice) is not fork-safe.
BTW, I just checked latest glib status
https://gitlab.gnome.org/GNOME/glib/-/issues/1079
it appears they're pretty close to deciding to delete the GSlice impl and always use system malloc.
So if we do take this patch series it'll hopefully be a time limited thing to carry.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 2/2] tcg: use QTree instead of GTree
Quoted 6 lines · Daniel P. Berrangé
Quoted 4 lines · Emilio Cota
qemu-user can hang in a multi-threaded fork. One common reason is that when creating a TB, between fork and exec we manipulate a GTree whose memory allocator (GSlice) is not fork-safe.
Quoted 6 lines
BTW, I just checked latest glib status
https://gitlab.gnome.org/GNOME/glib/-/issues/1079
it appears they're pretty close to deciding to delete the GSlice impl and always use system malloc.
They have now merged the code to delete the GSlice custom allocator.
So glib >= 2.76.0 should not exhibit a hang
Quoted 2 lines
So if we do take this patch series it'll hopefully be a time limited thing to carry.
So the question is whether the issue is critical enough that we want to carry a workaround for a while, vs telling users to upgrade to newer glib (once 2.76 actually gets released)
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 2/2] tcg: use QTree instead of GTree
Quoted 16 lines · Daniel P. Berrangé
Quoted 6 lines · Daniel P. Berrangé
Quoted 4 lines · Emilio Cota
qemu-user can hang in a multi-threaded fork. One common reason is that when creating a TB, between fork and exec we manipulate a GTree whose memory allocator (GSlice) is not fork-safe.
Quoted 6 lines
BTW, I just checked latest glib status
https://gitlab.gnome.org/GNOME/glib/-/issues/1079
it appears they're pretty close to deciding to delete the GSlice impl and always use system malloc.
They have now merged the code to delete the GSlice custom allocator.
Quoted 8 lines
So glib >= 2.76.0 should not exhibit a hang
Quoted 2 lines
So if we do take this patch series it'll hopefully be a time limited thing to carry.
So the question is whether the issue is critical enough that we want to carry a workaround for a while, vs telling users to upgrade to newer glib (once 2.76 actually gets released)
That is great news!
Since this is a correctness issue, I think we should ship with qtree and use it when configuring with glib <2.76.0. For later glib versions we would just use gtree, e.g. via typedef + inline functions.
Once the minimum glib required by the configure script is >= 2.76.0, then we'd remove qtree.
If that sounds like a good plan, I can send a v2.
Thanks,
Emilio
Re: [PATCH 2/2] tcg: use QTree instead of GTree
Quoted 28 lines · Emilio Cota
Quoted 16 lines · Daniel P. Berrangé
Quoted 6 lines · Daniel P. Berrangé
Quoted 4 lines · Emilio Cota
qemu-user can hang in a multi-threaded fork. One common reason is that when creating a TB, between fork and exec we manipulate a GTree whose memory allocator (GSlice) is not fork-safe.
Quoted 6 lines
BTW, I just checked latest glib status
https://gitlab.gnome.org/GNOME/glib/-/issues/1079
it appears they're pretty close to deciding to delete the GSlice impl and always use system malloc.
They have now merged the code to delete the GSlice custom allocator.
Quoted 8 lines
So glib >= 2.76.0 should not exhibit a hang
Quoted 2 lines
So if we do take this patch series it'll hopefully be a time limited thing to carry.
So the question is whether the issue is critical enough that we want to carry a workaround for a while, vs telling users to upgrade to newer glib (once 2.76 actually gets released)
That is great news!
Quoted 8 lines
Since this is a correctness issue, I think we should ship with qtree and use it when configuring with glib <2.76.0. For later glib versions we would just use gtree, e.g. via typedef + inline functions.
Once the minimum glib required by the configure script is >= 2.76.0, then we'd remove qtree.
If that sounds like a good plan, I can send a v2.
I'm fine with it, but be good to have an opinion here from the TCG subsystem maintainers, CC'ing them
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH 2/2] tcg: use QTree instead of GTree
Quoted 5 lines · Daniel P. Berrangé
Quoted 4 lines · Emilio Cota
Since this is a correctness issue, I think we should ship with qtree and use it when configuring with glib <2.76.0. For later glib versions we would just use gtree, e.g. via typedef + inline functions.
Quoted 7 lines
Quoted 4 lines
Once the minimum glib required by the configure script is >= 2.76.0, then we'd remove qtree.
If that sounds like a good plan, I can send a v2.
I'm fine with it, but be good to have an opinion here from the TCG subsystem maintainers, CC'ing them
I agree the correctness issue wants the fix now, and typedef + inlines sounds like a good way moving forward.
r~
Re: [PATCH 2/2] tcg: use QTree instead of GTree
Quoted 9 lines · Richard Henderson
Quoted 8 lines · Daniel P. Berrangé
Quoted 7 lines · Emilio Cota
Since this is a correctness issue, I think we should ship with qtree and use it when configuring with glib <2.76.0. For later glib versions we would just use gtree, e.g. via typedef + inline functions.
Once the minimum glib required by the configure script is >= 2.76.0, then we'd remove qtree.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
If that sounds like a good plan, I can send a v2.
I'm fine with it, but be good to have an opinion here from the TCG subsystem maintainers, CC'ing them
I agree the correctness issue wants the fix now, and typedef + inlines sounds like a good way moving forward.
Thanks, just sent a v2 implementing the above.
Emilio
Quoted 10 lines · Emilio Cota
Context:
https://gitlab.com/qemu-project/qemu/-/issues/285So far the only fix that we have had posted on the list is
https://lists.gnu.org/archive/html/qemu-devel/2022-10/msg00391.html
by Daniel. The approach that I'm following here should have the same outcome, except that it doesn't change the guest's environment. The approach is to import GTree (sans GSlice) into QEMU, and use that for TCG.
Quoted 2 lines
Daniel: what is the testing that you're using? Could you test these patches to confirm they fix the issue?
I used the demo program in the bug report. With your patches applied, that appears to run without hangs for a decent amount of time.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|