memory: avoid unnecessary iteration when updating ioeventfds
PATCH 1/1 memory: avoid unnecessary iteration when updating ioeventfds
From: Longpeng <[email protected]>
When updating ioeventfds, we need to iterate all address spaces and iterate all flat ranges of each address space. There is so much redundant process that a FlatView would be iterated for so many times during one commit (memory_region_transaction_commit).
We can mark a FlatView as UPDATED and then skip it in the next iteration and clear the UPDATED flag at the end of the commit. The overhead can be significantly reduced.
For example, a VM with 16 vdpa net devices and each one has 65 vectors, can reduce the time spent on memory_region_transaction_commit by 95%.
--- include/exec/memory.h | 2 ++ softmmu/memory.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-)
include/exec/memory.h +2 -0
@@ -1093,6 +1093,8 @@ struct FlatView {unsigned nr_allocated;struct AddressSpaceDispatch *dispatch;MemoryRegion *root; +#define FLATVIEW_FLAG_IOEVENTFD_UPDATED (1 << 0) +unsigned flags; };static inline FlatView *address_space_to_flatview (AddressSpace *as)
softmmu/memory.c +27 -1
@@ -815,6 +815,15 @@ FlatView *address_space_get_flatview(AddressSpace *as)return view; } +static void address_space_reset_view_flags (AddressSpace *as,unsigned mask) +{ +FlatView *view =address_space_get_flatview (as); + +if (view->flags & mask) { + view->flags &= ~mask; + } +} +static void address_space_update_ioeventfds (AddressSpace *as) {FlatView *view;
@@ -825,6 +834,12 @@ static void address_space_update_ioeventfds(AddressSpace *as)AddrRange tmp;unsigned i; + view =address_space_get_flatview (as); +if (view->flags &FLATVIEW_FLAG_IOEVENTFD_UPDATED ) { +return ; + } + view->flags |=FLATVIEW_FLAG_IOEVENTFD_UPDATED ; +/* * It is likely that the number of ioeventfds hasn't changed much, so use * the previous size as the starting value, with some headroom to avoid
@@ -833,7 +848,6 @@ static void address_space_update_ioeventfds(AddressSpace *as) ioeventfd_max =QEMU_ALIGN_UP (as->ioeventfd_nb,4 ); ioeventfds =g_new (MemoryRegionIoeventfd, ioeventfd_max); - view =address_space_get_flatview (as); FOR_EACH_FLAT_RANGE(fr, view) {for (i =0 ; i < fr->mr->ioeventfd_nb; ++i) { tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
@@ -1086,6 +1100,15 @@ void memory_region_transaction_begin(void) ++memory_region_transaction_depth; } +static inline void address_space_update_ioeventfds_finish (void ) +{ +AddressSpace *as; + + QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { +address_space_reset_view_flags (as,FLATVIEW_FLAG_IOEVENTFD_UPDATED ); + } +} +void memory_region_transaction_commit (void ) {AddressSpace *as;
@@ -1106,12 +1129,14 @@ void memory_region_transaction_commit(void) } memory_region_update_pending = false; ioeventfd_update_pending = false; +address_space_update_ioeventfds_finish ();MEMORY_LISTENER_CALL_GLOBAL (commit, Forward); }else if (ioeventfd_update_pending) { QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {address_space_update_ioeventfds (as); } ioeventfd_update_pending = false; +address_space_update_ioeventfds_finish (); } } }
@@ -3076,6 +3101,7 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) as->name =g_strdup (name ? name :"anonymous" );address_space_update_topology (as);address_space_update_ioeventfds (as); +address_space_reset_view_flags (as,FLATVIEW_FLAG_IOEVENTFD_UPDATED ); }static void do_address_space_destroy (AddressSpace *as)
Signature · 1 line
2.23.0
Quoted 38 lines · Longpeng(Mike) via
From: Longpeng <[email protected]>
When updating ioeventfds, we need to iterate all address spaces and iterate all flat ranges of each address space. There is so much redundant process that a FlatView would be iterated for so many times during one commit (
memory_region_transaction_commit).We can mark a FlatView as UPDATED and then skip it in the next iteration and clear the UPDATED flag at the end of the commit. The overhead can be significantly reduced.
For example, a VM with 16 vdpa net devices and each one has 65 vectors, can reduce the time spent on
memory_region_transaction_commitby 95%.Signed-off-by Longpeng <[email protected]>--- include/exec/memory.h | 2 ++ softmmu/memory.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-)include/exec/memory.h +2
-0@@ -1093,6 +1093,8 @@ struct FlatView {unsigned nr_allocated;struct AddressSpaceDispatch *dispatch;MemoryRegion *root; +#define FLATVIEW_FLAG_IOEVENTFD_UPDATED (1 << 0) +unsigned flags; };static inline FlatView *address_space_to_flatview (AddressSpace *as)softmmu/memory.c +0
-0
softmmu/memory.c FlatView *address_space_get_flatview(AddressSpace *as) · line 824
Quoted 10 lines
softmmu/memory.c +7
-0@@ -815,6 +815,15 @@ FlatView *address_space_get_flatview(AddressSpace *as)return view; } +static void address_space_reset_view_flags (AddressSpace *as,unsigned mask) +{ +FlatView *view =address_space_get_flatview (as); + +if (view->flags & mask) { + view->flags &= ~mask; + }
Just do it unconditionally.
Unfortunately, I cannot comment on the bigger picture, but it does look good to me.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 55 lines · Longpeng(Mike)
From: Longpeng <[email protected]>
When updating ioeventfds, we need to iterate all address spaces and iterate all flat ranges of each address space. There is so much redundant process that a FlatView would be iterated for so many times during one commit (
memory_region_transaction_commit).We can mark a FlatView as UPDATED and then skip it in the next iteration and clear the UPDATED flag at the end of the commit. The overhead can be significantly reduced.
For example, a VM with 16 vdpa net devices and each one has 65 vectors, can reduce the time spent on
memory_region_transaction_commitby 95%.Signed-off-by Longpeng <[email protected]>--- include/exec/memory.h | 2 ++ softmmu/memory.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-)include/exec/memory.h +2
-0@@ -1093,6 +1093,8 @@ struct FlatView {unsigned nr_allocated;struct AddressSpaceDispatch *dispatch;MemoryRegion *root; +#define FLATVIEW_FLAG_IOEVENTFD_UPDATED (1 << 0) +unsigned flags; };static inline FlatView *address_space_to_flatview (AddressSpace *as)softmmu/memory.c +9
-0@@ -815,6 +815,15 @@ FlatView *address_space_get_flatview(AddressSpace *as)return view; } +static void address_space_reset_view_flags (AddressSpace *as,unsigned mask) +{ +FlatView *view =address_space_get_flatview (as); + +if (view->flags & mask) { + view->flags &= ~mask; + } +} +static void address_space_update_ioeventfds (AddressSpace *as) {FlatView *view;
softmmu/memory.c static void address_space_update_ioeventfds(AddressSpace *as) · line 842
Quoted 9 lines
softmmu/memory.c +6
-0@@ -825,6 +834,12 @@ static void address_space_update_ioeventfds(AddressSpace *as)AddrRange tmp;unsigned i; + view =address_space_get_flatview (as); +if (view->flags &FLATVIEW_FLAG_IOEVENTFD_UPDATED ) { +return ; + } + view->flags |=FLATVIEW_FLAG_IOEVENTFD_UPDATED ; +
Won't we lose the listener calls if multiple address spaces have the same flatview?
Thanks
Quoted 53 lines
softmmu/memory.c +12
-2/* * It is likely that the number of ioeventfds hasn't changed much, so use * the previous size as the starting value, with some headroom to avoid @@ -833,7 +848,6 @@ static void address_space_update_ioeventfds(AddressSpace *as) ioeventfd_max =QEMU_ALIGN_UP (as->ioeventfd_nb,4 ); ioeventfds =g_new (MemoryRegionIoeventfd, ioeventfd_max); - view =address_space_get_flatview (as); FOR_EACH_FLAT_RANGE(fr, view) {for (i =0 ; i < fr->mr->ioeventfd_nb; ++i) { tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,@@ -1086,6 +1100,15 @@ void memory_region_transaction_begin(void) ++memory_region_transaction_depth; } +static inline void address_space_update_ioeventfds_finish (void ) +{ +AddressSpace *as; + + QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { +address_space_reset_view_flags (as,FLATVIEW_FLAG_IOEVENTFD_UPDATED ); + } +} +void memory_region_transaction_commit (void ) {AddressSpace *as;@@ -1106,12 +1129,14 @@ void memory_region_transaction_commit(void) } memory_region_update_pending = false; ioeventfd_update_pending = false; +address_space_update_ioeventfds_finish ();MEMORY_LISTENER_CALL_GLOBAL (commit, Forward); }else if (ioeventfd_update_pending) { QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {address_space_update_ioeventfds (as); } ioeventfd_update_pending = false; +address_space_update_ioeventfds_finish (); } } }@@ -3076,6 +3101,7 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) as->name =g_strdup (name ? name :"anonymous" );address_space_update_topology (as);address_space_update_ioeventfds (as); +address_space_reset_view_flags (as,FLATVIEW_FLAG_IOEVENTFD_UPDATED ); }static void do_address_space_destroy (AddressSpace *as) --2.23.0
Quoted 59 lines · Jason Wang
Quoted 58 lines · Longpeng(Mike)
From: Longpeng <[email protected]>
When updating ioeventfds, we need to iterate all address spaces and iterate all flat ranges of each address space. There is so much redundant process that a FlatView would be iterated for so many times during one commit (
memory_region_transaction_commit).We can mark a FlatView as UPDATED and then skip it in the next iteration and clear the UPDATED flag at the end of the commit. The overhead can be significantly reduced.
For example, a VM with 16 vdpa net devices and each one has 65 vectors, can reduce the time spent on
memory_region_transaction_commitby 95%.Signed-off-by Longpeng <[email protected]>--- include/exec/memory.h | 2 ++ softmmu/memory.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-)include/exec/memory.h +2
-0@@ -1093,6 +1093,8 @@ struct FlatView {unsigned nr_allocated;struct AddressSpaceDispatch *dispatch;MemoryRegion *root; +#define FLATVIEW_FLAG_IOEVENTFD_UPDATED (1 << 0) +unsigned flags; };static inline FlatView *address_space_to_flatview (AddressSpace *as)softmmu/memory.c +9
-0@@ -815,6 +815,15 @@ FlatView *address_space_get_flatview(AddressSpace *as)return view; } +static void address_space_reset_view_flags (AddressSpace *as,unsigned mask) +{ +FlatView *view =address_space_get_flatview (as); + +if (view->flags & mask) { + view->flags &= ~mask; + } +} +static void address_space_update_ioeventfds (AddressSpace *as) {FlatView *view;@@ -825,6 +834,12 @@ static void address_space_update_ioeventfds(AddressSpace *as)AddrRange tmp;unsigned i;
Quoted 9 lines
softmmu/memory.c static void address_space_update_ioeventfds(AddressSpace *as) · line 842
Quoted 6 lines
softmmu/memory.c +6
-0+ view =address_space_get_flatview (as); +if (view->flags &FLATVIEW_FLAG_IOEVENTFD_UPDATED ) { +return ; + } + view->flags |=FLATVIEW_FLAG_IOEVENTFD_UPDATED ; +Won't we lose the listener calls if multiple address spaces have the same flatview?
I have the same concern with Jason. I don't think it matters in reality, since only address_space_io uses it so far. but it doesn't really look reasonable and clean.
One other idea of optimizing ioeventfd update is we can add a per-AS counter (ioeventfd_notifiers), increase if any eventfd_add|del is registered in memory_listener_register(), and decrease when unregister. Then address_space_update_ioeventfds() can be skipped completely if ioeventfd_notifiers==0.
Side note: Jason, do you think we should drop vhost_eventfd_add|del? They're all no-ops right now.
Thanks,
Signature · 1 line
Peter Xu
Quoted 71 lines · Peter Xu
Quoted 59 lines · Jason Wang
Quoted 58 lines · Longpeng(Mike)
From: Longpeng <[email protected]>
When updating ioeventfds, we need to iterate all address spaces and iterate all flat ranges of each address space. There is so much redundant process that a FlatView would be iterated for so many times during one commit (
memory_region_transaction_commit).We can mark a FlatView as UPDATED and then skip it in the next iteration and clear the UPDATED flag at the end of the commit. The overhead can be significantly reduced.
For example, a VM with 16 vdpa net devices and each one has 65 vectors, can reduce the time spent on
memory_region_transaction_commitby 95%.Signed-off-by Longpeng <[email protected]>--- include/exec/memory.h | 2 ++ softmmu/memory.c | 28 +++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-)include/exec/memory.h +2
-0@@ -1093,6 +1093,8 @@ struct FlatView {unsigned nr_allocated;struct AddressSpaceDispatch *dispatch;MemoryRegion *root; +#define FLATVIEW_FLAG_IOEVENTFD_UPDATED (1 << 0) +unsigned flags; };static inline FlatView *address_space_to_flatview (AddressSpace *as)softmmu/memory.c +9
-0@@ -815,6 +815,15 @@ FlatView *address_space_get_flatview(AddressSpace *as)return view; } +static void address_space_reset_view_flags (AddressSpace *as,unsigned mask) +{ +FlatView *view =address_space_get_flatview (as); + +if (view->flags & mask) { + view->flags &= ~mask; + } +} +static void address_space_update_ioeventfds (AddressSpace *as) {FlatView *view;@@ -825,6 +834,12 @@ static void address_space_update_ioeventfds(AddressSpace *as)AddrRange tmp;unsigned i;Quoted 9 lines
softmmu/memory.c static void address_space_update_ioeventfds(AddressSpace *as) · line 842
Quoted 6 lines
softmmu/memory.c +6
-0+ view =address_space_get_flatview (as); +if (view->flags &FLATVIEW_FLAG_IOEVENTFD_UPDATED ) { +return ; + } + view->flags |=FLATVIEW_FLAG_IOEVENTFD_UPDATED ; +Won't we lose the listener calls if multiple address spaces have the same flatview?
Quoted 3 lines
I have the same concern with Jason. I don't think it matters in reality, since only
address_space_iouses it so far. but it doesn't really look reasonable and clean.
Yes, I think in the memory core it should not assume how the eventfds are used.
Quoted 9 lines
One other idea of optimizing ioeventfd update is we can add a per-AS counter (
ioeventfd_notifiers), increase if any eventfd_add|del is registered inmemory_listener_register(), and decrease when unregister. Thenaddress_space_update_ioeventfds()can be skipped completely if ioeventfd_notifiers==0.Side note: Jason, do you think we should drop vhost_eventfd_add|del? They're all no-ops right now.
I think so.
Thanks
Quoted 6 lines
Thanks,
--
Peter Xu