Fix performance regression in qemu_get_ram_ptr
PATCH 1/1 Fix performance regression in qemu_get_ram_ptr
When the commit f471a17e9d869df3c6573f7ec02c4725676d6f3a converted the ram_blocks structure to QLIST, it also removed the conditional check before switching the current block at the beginning of the list.
In the common use case where ram_blocks has a few blocks with only one frequently accessed (the main RAM), this has a performance impact as it performs the useless list operations on each call (which are on a really hot path).
On my machine emulation (ARM on amd64), this patch reduces the percentage of CPU time spent in qemu_get_ram_ptr from 6.3% to 2.1% in the profiling of a full boot.
--- exec.c | 7 +++++-- 1 files changed, 5 insertions(+), 2 deletions(-)
exec.c +5 -2
@@ -2957,8 +2957,11 @@ void *qemu_get_ram_ptr(ram_addr_t addr) QLIST_FOREACH(block, &ram_list.blocks, next) {if (addr - block->offset < block->length) { -QLIST_REMOVE (block, next); -QLIST_INSERT_HEAD (&ram_list.blocks, block, next); +/* Move this entry to to start of the list. */ +if (block !=QLIST_FIRST (&ram_list.blocks)) { +QLIST_REMOVE (block, next); +QLIST_INSERT_HEAD (&ram_list.blocks, block, next); + }return block->host + (addr - block->offset); } }
Signature · 1 line
1.7.3.1
Quoted 26 lines · Vincent Palatin
When the commit
f471a17e9d869df3c6573f7ec02c4725676d6f3aconverted theram_blocksstructure to QLIST, it also removed the conditional check before switching the current block at the beginning of the list.In the common use case where
ram_blockshas a few blocks with only one frequently accessed (the main RAM), this has a performance impact as it performs the useless list operations on each call (which are on a really hot path).On my machine emulation (ARM on amd64), this patch reduces the percentage of CPU time spent in
qemu_get_ram_ptrfrom 6.3% to 2.1% in the profiling of a full boot.Signed-off-by Vincent Palatin <[email protected]>--- exec.c | 7 +++++-- 1 files changed, 5 insertions(+), 2 deletions(-)exec.c +0
-0@@ -2957,8 +2957,11 @@ void *qemu_get_ram_ptr(ram_addr_t addr) QLIST_FOREACH(block, &ram_list.blocks, next) {if (addr - block->offset < block->length) {
exec.c void *qemu_get_ram_ptr(ram_addr_t addr) · line 2967
Quoted 10 lines
exec.c +5
-2-QLIST_REMOVE (block, next); -QLIST_INSERT_HEAD (&ram_list.blocks, block, next); +/* Move this entry to to start of the list. */ +if (block !=QLIST_FIRST (&ram_list.blocks)) { +QLIST_REMOVE (block, next); +QLIST_INSERT_HEAD (&ram_list.blocks, block, next); + }return block->host + (addr - block->offset); } }
Looks good
Quoted 3 lines · * Vincent Palatin ([email protected]) wrote:
When the commit
f471a17e9d869df3c6573f7ec02c4725676d6f3aconverted theram_blocksstructure to QLIST, it also removed the conditional check before switching the current block at the beginning of the list.
Nice catch.
Quoted 8 lines
In the common use case where
ram_blockshas a few blocks with only one frequently accessed (the main RAM), this has a performance impact as it performs the useless list operations on each call (which are on a really hot path).On my machine emulation (ARM on amd64), this patch reduces the percentage of CPU time spent in
qemu_get_ram_ptrfrom 6.3% to 2.1% in the profiling of a full boot.
Hopefully this is back on par with before the QLIST switchover.
Quoted 1 line
Signed-off-by Vincent Palatin <[email protected]>
Quoted 9 lines
--- exec.c | 7 +++++-- 1 files changed, 5 insertions(+), 2 deletions(-)exec.c +0
-0
exec.c void *qemu_get_ram_ptr(ram_addr_t addr) · line 2964
Quoted 10 lines
exec.c +5
-2@@ -2957,8 +2957,11 @@ void *qemu_get_ram_ptr(ram_addr_t addr) QLIST_FOREACH(block, &ram_list.blocks, next) {if (addr - block->offset < block->length) { -QLIST_REMOVE (block, next); -QLIST_INSERT_HEAD (&ram_list.blocks, block, next); +/* Move this entry to to start of the list. */ +if (block !=QLIST_FIRST (&ram_list.blocks)) { +QLIST_REMOVE (block, next); +QLIST_INSERT_HEAD (&ram_list.blocks, block, next); + }
Pretty close to self-documenting code now. Not sure if it's subtle enough to warrant change to the comment like:
/* Move block to head of list if it's not there already */
thanks,
-chris
Anthony Liguori
AppliedQuoted 9 lines · Vincent Palatin
When the commit
f471a17e9d869df3c6573f7ec02c4725676d6f3aconverted theram_blocksstructure to QLIST, it also removed the conditional check before switching the current block at the beginning of the list.In the common use case where
ram_blockshas a few blocks with only one frequently accessed (the main RAM), this has a performance impact as it performs the useless list operations on each call (which are on a really hot path).
Quoted 5 lines
On my machine emulation (ARM on amd64), this patch reduces the percentage of CPU time spent in
qemu_get_ram_ptrfrom 6.3% to 2.1% in the profiling of a full boot.Signed-off-by Vincent Palatin<[email protected]>
Applied. Thanks.
Regards,
Anthony Liguori
Quoted 22 lines
--- exec.c | 7 +++++-- 1 files changed, 5 insertions(+), 2 deletions(-)exec.c +5
-2@@ -2957,8 +2957,11 @@ void *qemu_get_ram_ptr(ram_addr_t addr) QLIST_FOREACH(block,&ram_list.blocks, next) {if (addr - block->offset< block->length) { -QLIST_REMOVE (block, next); -QLIST_INSERT_HEAD (&ram_list.blocks, block, next); +/* Move this entry to to start of the list. */ +if (block !=QLIST_FIRST (&ram_list.blocks)) { +QLIST_REMOVE (block, next); +QLIST_INSERT_HEAD (&ram_list.blocks, block, next); + }return block->host + (addr - block->offset); } }