unicorn/qemu/include/exec
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
..
address-spaces.h import 2015-08-21 15:04:50 +08:00
cpu_ldst_template.h softmmu: add helper function to pass through retaddr 2018-02-17 15:23:38 -05:00
cpu_ldst.h softmmu: remove now unused functions 2018-02-17 15:23:38 -05:00
cpu-all.h qemu-common.h: Drop WORDS_ALIGNED define 2018-02-24 17:01:55 -05:00
cpu-common.h memory: split memory_region_from_host from qemu_ram_addr_from_host 2018-02-24 16:06:49 -05:00
cpu-defs.h cpu_defs: Simplify CPUTLB padding logic 2018-02-17 15:23:27 -05:00
cputlb.h include/exec: Move cputlb exec.c defs out 2018-02-23 10:52:25 -05:00
exec-all.h cpu-exec: Rename cpu_resume_from_signal() to cpu_loop_exit_noexc() 2018-02-24 17:25:28 -05:00
gen-icount.h gen-icount: Use tcg_set_insn_param 2018-02-23 20:01:17 -05:00
helper-gen.h import 2015-08-21 15:04:50 +08:00
helper-head.h import 2015-08-21 15:04:50 +08:00
helper-proto.h import 2015-08-21 15:04:50 +08:00
helper-tcg.h This code should now build the x86_x64-softmmu part 2. 2017-01-19 22:50:28 +11:00
hwaddr.h qemu-common: push cpu.h inclusion out of qemu-common.h 2018-02-24 01:50:56 -05:00
ioport.h hw: remove pio_addr_t 2018-02-24 02:43:16 -05:00
memattrs.h target-i386: introduce cpu_get_mem_attrs 2018-02-13 11:33:39 -05:00
memory-internal.h import 2015-08-21 15:04:50 +08:00
memory.h exec: hide mr->ram_addr from qemu_get_ram_ptr users 2018-02-24 16:17:49 -05:00
ram_addr.h memory: remove qemu_get_ram_fd, qemu_set_ram_fd, qemu_ram_block_host_ptr 2018-02-24 03:34:44 -05:00
ramlist.h memory: RCU ram_list.dirty_memory[] for safe RAM hotplug 2018-02-22 15:38:03 -05:00
semihost.h exec: Add semihosting stubs 2018-02-17 15:23:33 -05:00
tb-context.h exec: extract exec/tb-context.h 2018-02-24 02:09:58 -05:00
tb-hash-xx.h exec: add tb_hash_func5, derived from xxhash 2018-02-24 17:36:35 -05:00
tb-hash.h tb hash: hash phys_pc, pc, and flags with xxhash 2018-02-24 18:00:14 -05:00