unicorn/qemu
Emilio G. Cota ae3e22a689
tb hash: hash phys_pc, pc, and flags with xxhash
For some workloads such as arm bootup, tb_phys_hash is performance-critical.
The is due to the high frequency of accesses to the hash table, originated
by (frequent) TLB flushes that wipe out the cpu-private tb_jmp_cache's.
More info:
https://lists.nongnu.org/archive/html/qemu-devel/2016-03/msg05098.html

To dig further into this I modified an arm image booting debian jessie to
immediately shut down after boot. Analysis revealed that quite a bit of time
is unnecessarily spent in tb_phys_hash: the cause is poor hashing that
results in very uneven loading of chains in the hash table's buckets;
the longest observed chain had ~550 elements.

The appended addresses this with two changes:

1) Use xxhash as the hash table's hash function. xxhash is a fast,
high-quality hashing function.

2) Feed the hashing function with not just tb_phys, but also pc and flags.

This improves performance over using just tb_phys for hashing, since that
resulted in some hash buckets having many TB's, while others getting very few;
with these changes, the longest observed chain on a single hash bucket is
brought down from ~550 to ~40.

Tests show that the other element checked for in tb_find_physical,
cs_base, is always a match when tb_phys+pc+flags are a match,
so hashing cs_base is wasteful. It could be that this is an ARM-only
thing, though. UPDATE:
On Tue, Apr 05, 2016 at 08:41:43 -0700, Richard Henderson wrote:
> The cs_base field is only used by i386 (in 16-bit modes), and sparc (for a TB
> consisting of only a delay slot).
> It may well still turn out to be reasonable to ignore cs_base for hashing.

BTW, after this change the hash table should not be called "tb_hash_phys"
anymore; this is addressed later in this series.

This change gives consistent bootup time improvements. I tested two
host machines:
- Intel Xeon E5-2690: 11.6% less time
- Intel i7-4790K: 19.2% less time

Increasing the number of hash buckets yields further improvements. However,
using a larger, fixed number of buckets can degrade performance for other
workloads that do not translate as many blocks (600K+ for debian-jessie arm
bootup). This is dealt with later in this series.

Backports commit 42bd32287f3a18d823f2258b813824a39ed7c6d9 from qemu
2018-02-24 18:00:14 -05:00
..
crypto crypto: Clean up includes 2018-02-19 00:47:40 -05:00
default-configs arm64eb: add support for ARM64 big endian. 2017-04-24 23:30:01 +08:00
docs docs: clarify memory region lifecycle 2018-02-12 15:11:21 -05:00
fpu fpu: silence warnings 2018-02-22 09:52:28 -05:00
hw qemu-common: push cpu.h inclusion out of qemu-common.h 2018-02-24 01:50:56 -05:00
include tb hash: hash phys_pc, pc, and flags with xxhash 2018-02-24 18:00:14 -05:00
qapi qapi: Change visit_type_FOO() to no longer return partial objects 2018-02-23 19:53:17 -05:00
qobject util: move declarations out of qemu-common.h 2018-02-22 09:25:48 -05:00
qom tcg: Remove needless CPUState::current_tb 2018-02-23 23:45:42 -05:00
scripts qapi: Change visit_type_FOO() to no longer return partial objects 2018-02-23 19:53:17 -05:00
target-arm target-arm: Fix TTBR selecting logic on AArch32 Stage 2 translation 2018-02-24 16:54:32 -05:00
target-i386 compiler.h: add QEMU_ALIGNED() to enforce struct alignment 2018-02-24 17:32:43 -05:00
target-m68k cpu: move exec-all.h inclusion out of cpu.h 2018-02-24 02:39:08 -05:00
target-mips cpu: move exec-all.h inclusion out of cpu.h 2018-02-24 02:39:08 -05:00
target-sparc cpu: move exec-all.h inclusion out of cpu.h 2018-02-24 02:39:08 -05:00
tcg compiler.h: add QEMU_ALIGNED() to enforce struct alignment 2018-02-24 17:32:43 -05:00
util util: align memory allocations to 2M on AArch64 2018-02-23 13:56:59 -05:00
aarch64.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
aarch64eb.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
accel.c accel: make configure_accelerator return void 2018-02-24 00:31:28 -05:00
arm.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
armeb.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
CODING_STYLE import 2015-08-21 15:04:50 +08:00
configure configure: Use instead of deprecated 2018-02-24 16:59:40 -05:00
COPYING import 2015-08-21 15:04:50 +08:00
COPYING.LIB import 2015-08-21 15:04:50 +08:00
cpu-exec-common.c cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
cpu-exec.c tb hash: hash phys_pc, pc, and flags with xxhash 2018-02-24 18:00:14 -05:00
cpus.c cpu: move exec-all.h inclusion out of cpu.h 2018-02-24 02:39:08 -05:00
cputlb.c memory: split memory_region_from_host from qemu_ram_addr_from_host 2018-02-24 16:06:49 -05:00
exec.c exec: hide mr->ram_addr from qemu_get_ram_ptr users 2018-02-24 16:17:49 -05:00
gen_all_header.sh arm64eb: add support for ARM64 big endian. 2017-04-24 23:30:01 +08:00
glib_compat.c glib_compat: backport hashtable iterator interfaces 2018-02-21 13:18:44 -05:00
HACKING import 2015-08-21 15:04:50 +08:00
header_gen.py cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
ioport.c hw: remove pio_addr_t 2018-02-24 02:43:16 -05:00
LICENSE import 2015-08-21 15:04:50 +08:00
m68k.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
Makefile Makefile: Add a FORCE target 2018-02-24 17:03:51 -05:00
Makefile.objs crypto: move crypto objects out of libqemuutil.la 2018-02-17 15:23:50 -05:00
Makefile.target tcg: split tcg_op_defs to -common 2018-02-17 15:23:51 -05:00
memory.c exec: hide mr->ram_addr from qemu_get_ram_ptr users 2018-02-24 16:17:49 -05:00
memory_mapping.c include/qemu/osdep.h: Don't include qapi/error.h 2018-02-21 23:08:18 -05:00
mips.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
mips64.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
mips64el.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
mipsel.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
powerpc.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
qapi-schema.json qapi: Lazy creation of array types 2018-02-19 18:55:35 -05:00
qemu-log.c log: move qemu_log_close/qemu_log_flush from header to log.c 2018-02-22 11:13:17 -05:00
qemu-timer.c all: Clean up includes 2018-02-19 01:34:28 -05:00
rules.mak Makefile: add dependency on scripts/create_config 2018-02-24 17:05:03 -05:00
softmmu_template.h exec.c: Pass MemTxAttrs to iotlb_to_region so it uses the right AS 2018-02-17 23:19:00 -05:00
sparc.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
sparc64.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
tcg-runtime.c all: Clean up includes 2018-02-19 01:34:28 -05:00
translate-all.c tb hash: hash phys_pc, pc, and flags with xxhash 2018-02-24 18:00:14 -05:00
translate-all.h user-exec: Push resume-from-signal code out to handle_cpu_signal() 2018-02-24 17:21:06 -05:00
translate-common.c exec: Clean up includes 2018-02-19 00:49:55 -05:00
unicorn_common.h qom/cpu: Add MemoryRegion property 2018-02-18 21:54:50 -05:00
VERSION import 2015-08-21 15:04:50 +08:00
vl.c hw: explicitly include qemu/log.h 2018-02-24 02:00:45 -05:00
vl.h import 2015-08-21 15:04:50 +08:00
x86_64.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00