From nobody Wed Nov 5 15:51:26 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496703184258237.57658880316694; Mon, 5 Jun 2017 15:53:04 -0700 (PDT) Received: from localhost ([::1]:35455 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI0ru-00019Y-NS for importer@patchew.org; Mon, 05 Jun 2017 18:53:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38516) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI0ou-0007nE-Fr for qemu-devel@nongnu.org; Mon, 05 Jun 2017 18:49:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dI0or-0000GC-Bn for qemu-devel@nongnu.org; Mon, 05 Jun 2017 18:49:56 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:44383) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dI0or-0000FB-0v for qemu-devel@nongnu.org; Mon, 05 Jun 2017 18:49:53 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 79FE420AAC; Mon, 5 Jun 2017 18:49:49 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute4.internal (MEProxy); Mon, 05 Jun 2017 18:49:49 -0400 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 3C6AF7E263; Mon, 5 Jun 2017 18:49:49 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc:x-sasl-enc; s=mesmtp; bh=FLh TyJBOZersxyftd24rGuhU92rGn4BnyZ0n0thUGgA=; b=SsuU6CODnDTvIW+CLz2 ZLeuzYK4yFPYXxWAOVIladcSdpRUzayX4l8L3n/7cs4UxiNVdQ/I+lIZPeaUZzL7 C49hZ9Hy+jP+sWI8laOtnzm+9V0WyISw1iENvwk0jfK/cDN5lMg6MYq9Fqr9lVL+ YR3Qn+a5JwFNSnIIsjpmXKOA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc :x-sasl-enc; s=fm1; bh=FLhTyJBOZersxyftd24rGuhU92rGn4BnyZ0n0thUG gA=; b=gMJYAxaR/rE4G0nnAC264VAUNV2s6f9E+e3zbsjyG88tJwaeipUIp3KYo ZsMiNrikCa7Vk6CpQwtdpQT3S5JZ31GLtKi1hv5di7BLF3fLu5aTlKJ5SIIQFe4X f+5gTEJgqK28C5Ww3CCQtUv1cM7lPEd/W3WxAnMTDg2m+aPFgIgbY/CdRWsl0ivh ybd3lxhvq5sOV1JeGOFEzG2TDP2lhUI+rg4kaDrih3fVd/lMqJ5b5F+YBRw2RrFr altzDJmw7IQ5rdpa154Xu3I3TtJLDazwalCBNQW4woTINhv1sLcn7maAG/ATP0SR TpMvYMrXXYcKSvzgVcZR9sTkVKamA== X-ME-Sender: X-Sasl-enc: Pk4pFwf4v3EJDLBX5awr2UXfOjUWpvSD7Ptyy2h+Cc0y 1496702989 From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Mon, 5 Jun 2017 18:49:37 -0400 Message-Id: <1496702979-26132-2-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1496702979-26132-1-git-send-email-cota@braap.org> References: <1496702979-26132-1-git-send-email-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.111.4.25 Subject: [Qemu-devel] [PATCH v2 1/3] compiler: define QEMU_CACHELINE_SIZE X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Pranith Kumar , Paolo Bonzini , alex.bennee@linaro.org, Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This is a constant used as a hint for padding structs to hopefully avoid false cache line sharing. The constant can be set at configure time by defining QEMU_CACHELINE_SIZE via --extra-cflags. If not set there, we try to obtain the value from the machine running the configure script. If we fail, we default to reasonable values, i.e. 128 bytes for ppc64 and 64 bytes for all others. Note: the configure script only picks up the cache line size when run on Linux hosts because I have no other platforms (e.g. Windows, BSD's) to test on. Signed-off-by: Emilio G. Cota Tested-by: Geert Martin Ijewski --- configure | 38 ++++++++++++++++++++++++++++++++++++++ include/qemu/compiler.h | 17 +++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/configure b/configure index 13e040d..6a68cb2 100755 --- a/configure +++ b/configure @@ -4832,6 +4832,41 @@ EOF fi fi =20 +# Find out the size of a cache line on the host +# TODO: support more platforms +cat > $TMPC< + +#define SYSFS "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_si= ze" + +int main(int argc, char *argv[]) +{ + unsigned int size; + FILE *fp; + + fp =3D fopen(SYSFS, "r"); + if (fp =3D=3D NULL) { + return -1; + } + if (!fscanf(fp, "%u", &size)) { + return -1; + } + return size; +} +#else +#error Cannot find host cache line size +#endif +EOF + +host_cacheline_size=3D0 +if compile_prog "" "" ; then + ./$TMPE + host_cacheline_size=3D$? +fi + + ########################################## # check for _Static_assert() =20 @@ -5284,6 +5319,9 @@ fi if test "$bigendian" =3D "yes" ; then echo "HOST_WORDS_BIGENDIAN=3Dy" >> $config_host_mak fi +if test "$host_cacheline_size" -gt 0 ; then + echo "HOST_CACHELINE_SIZE=3D$host_cacheline_size" >> $config_host_mak +fi if test "$mingw32" =3D "yes" ; then echo "CONFIG_WIN32=3Dy" >> $config_host_mak rc_version=3D$(cat $source_path/VERSION) diff --git a/include/qemu/compiler.h b/include/qemu/compiler.h index 340e5fd..178d831 100644 --- a/include/qemu/compiler.h +++ b/include/qemu/compiler.h @@ -40,6 +40,23 @@ # define QEMU_PACKED __attribute__((packed)) #endif =20 +/* + * Cache line size of the host. Can be overriden. + * Note that this is just a compile-time hint to hopefully avoid false sha= ring + * of cache lines; code must be correct regardless of the constant's value. + */ +#ifndef QEMU_CACHELINE_SIZE +# ifdef HOST_CACHELINE_SIZE +# define QEMU_CACHELINE_SIZE HOST_CACHELINE_SIZE +# else +# if defined(__powerpc64__) +# define QEMU_CACHELINE_SIZE 128 +# else +# define QEMU_CACHELINE_SIZE 64 +# endif +# endif +#endif + #define QEMU_ALIGNED(X) __attribute__((aligned(X))) =20 #ifndef glue --=20 2.7.4 From nobody Wed Nov 5 15:51:26 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 149670307975472.76874473354667; Mon, 5 Jun 2017 15:51:19 -0700 (PDT) Received: from localhost ([::1]:35450 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI0qD-0008SL-KW for importer@patchew.org; Mon, 05 Jun 2017 18:51:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38517) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI0ou-0007nF-G7 for qemu-devel@nongnu.org; Mon, 05 Jun 2017 18:49:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dI0or-0000GO-DL for qemu-devel@nongnu.org; Mon, 05 Jun 2017 18:49:56 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:56041) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dI0or-0000FC-9F for qemu-devel@nongnu.org; Mon, 05 Jun 2017 18:49:53 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id A939520B65; Mon, 5 Jun 2017 18:49:49 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute4.internal (MEProxy); Mon, 05 Jun 2017 18:49:49 -0400 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 6B13B24870; Mon, 5 Jun 2017 18:49:49 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc:x-sasl-enc; s=mesmtp; bh=Nw6 5aPg8R+0vhnyHLzkpEZ60djDDRv4ewBgMRIHCUtY=; b=XqTht5YFCOtDelruaAW q2xkVKX9eJeJhDqaeCH4W54dsETMVt+za+UeZ9iRpE83w/zatL3SRm0paaWjApd9 B7G4ybp/VcuSVr7hz+HrzvcgxcdUDfLTAJT3yf4tDpOJGNvwbSq8hWkjCWKrYS8L hf5oVGd5H0RIhkXheCCs85Lc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc :x-sasl-enc; s=fm1; bh=Nw65aPg8R+0vhnyHLzkpEZ60djDDRv4ewBgMRIHCU tY=; b=HaQ0SijEVyI9DdGuE1gJJaeF8UHLmpezpdfga8w9H4bSu+SfLDapGcngr s5l4+5VwX6e1GvwyhCysodVh6No3mO4XBQXDv8gPOuvXlG8NPQEJ0xOEiJwKHo/u DuMF6Yma/6FDvC4LzV/YD2NB2+FXK8T2F6Qi0MBE50fN7vevPcyqn5hVDYiz4miB QL5mHoKgGt8r20DW7LmT01JLMuJf4FmUM5PAhLh5Q6LN7QRhBfFc0fVcQxbMi6SX CLgnPwVHD4HbLAgq/MFVet/hqqLEHXPYUdAVKealylktgTCUGPKgPX9j0Qq9hgfw meh+ptGaxlQIL5ZF3mm2QDQifsC9g== X-ME-Sender: X-Sasl-enc: Pk4mBwn9oGgeE6NB+6Yr2UXfOjUWpvSD7Ptyy2h+Cc0y 1496702989 From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Mon, 5 Jun 2017 18:49:38 -0400 Message-Id: <1496702979-26132-3-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1496702979-26132-1-git-send-email-cota@braap.org> References: <1496702979-26132-1-git-send-email-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.111.4.25 Subject: [Qemu-devel] [PATCH v2 2/3] tests: use QEMU_CACHELINE_SIZE instead of hard-coding it X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Pranith Kumar , Paolo Bonzini , alex.bennee@linaro.org, Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Emilio G. Cota Reviewed-by: Pranith Kumar --- tests/atomic_add-bench.c | 4 ++-- tests/qht-bench.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/atomic_add-bench.c b/tests/atomic_add-bench.c index caa1e8e..c219109 100644 --- a/tests/atomic_add-bench.c +++ b/tests/atomic_add-bench.c @@ -5,11 +5,11 @@ =20 struct thread_info { uint64_t r; -} QEMU_ALIGNED(64); +} QEMU_ALIGNED(QEMU_CACHELINE_SIZE); =20 struct count { unsigned long val; -} QEMU_ALIGNED(64); +} QEMU_ALIGNED(QEMU_CACHELINE_SIZE); =20 static QemuThread *threads; static struct thread_info *th_info; diff --git a/tests/qht-bench.c b/tests/qht-bench.c index 2afa09d..3f4b5eb 100644 --- a/tests/qht-bench.c +++ b/tests/qht-bench.c @@ -28,7 +28,7 @@ struct thread_info { uint64_t r; bool write_op; /* writes alternate between insertions and removals */ bool resize_down; -} QEMU_ALIGNED(64); /* avoid false sharing among threads */ +} QEMU_ALIGNED(QEMU_CACHELINE_SIZE); /* avoid false sharing among threads = */ =20 static struct qht ht; static QemuThread *rw_threads; --=20 2.7.4 From nobody Wed Nov 5 15:51:26 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1496703184396197.22673591287537; Mon, 5 Jun 2017 15:53:04 -0700 (PDT) Received: from localhost ([::1]:35456 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI0rv-00019r-2g for importer@patchew.org; Mon, 05 Jun 2017 18:53:03 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38520) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dI0ou-0007nH-Gm for qemu-devel@nongnu.org; Mon, 05 Jun 2017 18:49:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dI0or-0000GU-DY for qemu-devel@nongnu.org; Mon, 05 Jun 2017 18:49:56 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:51117) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dI0or-0000FN-8i for qemu-devel@nongnu.org; Mon, 05 Jun 2017 18:49:53 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id DB89520BBD; Mon, 5 Jun 2017 18:49:49 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute4.internal (MEProxy); Mon, 05 Jun 2017 18:49:49 -0400 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 99A797E263; Mon, 5 Jun 2017 18:49:49 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc:x-sasl-enc; s=mesmtp; bh=fwn TIPWt425pedA26gTrasY8k01JzCfvzZ2OX5N2f6k=; b=MBT8Xoi2LjlvWrqObVo P96wW9BSEqhvLGn7g7DFtdp7qQ2CfmXxZ9UMJ/Tuw/hyBAgSeHCbo0uuEcAzwPNQ fsmUZtMASVP3oK+5bBPy8pBdCyp7WGNKspq2HSz/dVsN2RcUR23rFufUgk5Uvh4e MRZ6aRyHzeirppDYCeYhotXU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc :x-sasl-enc; s=fm1; bh=fwnTIPWt425pedA26gTrasY8k01JzCfvzZ2OX5N2f 6k=; b=rMOfgZrxM24hVZkV6RGOR2DGWmxGeHjGFmvpD5XNUDYPxc6DW9J40ZqEc z+dYfV7rZdJBwABYZVXecP8D/3vfPVB5pIA/JU+MiMzmzJheJJZQWNejxuY+rUx6 pyixDQTkHbdUay0JXlLLgk05xmH0pdjCpYLCZsiTWE0KF0oXI8a+67/lSmwnNYvD M48OgUJf2trwOkKaoMzhV+Kc8MpmFjPkaxB1te+C3yGJEE8kVTM8Q8bj5BB4qZp5 Fl4XvPE0zU95fD1/fgP7/iEsRoLQvqa0Fc2LrSj3oJMHx80QQUX5CUzOUoDBtIHg 4kPnWrUDRFk8DJXaphlUQ0LeNHHqQ== X-ME-Sender: X-Sasl-enc: Pk4wFg3hsmIIDaRc/7Qr2UXfOjUWpvSD7Ptyy2h+Cc0y 1496702989 From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Mon, 5 Jun 2017 18:49:39 -0400 Message-Id: <1496702979-26132-4-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1496702979-26132-1-git-send-email-cota@braap.org> References: <1496702979-26132-1-git-send-email-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.111.4.25 Subject: [Qemu-devel] [PATCH v2 3/3] tcg: allocate TB structs before the corresponding translated code X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Pranith Kumar , Paolo Bonzini , alex.bennee@linaro.org, Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Allocating an arbitrarily-sized array of tbs results in either (a) a lot of memory wasted or (b) unnecessary flushes of the code cache when we run out of TB structs in the array. An obvious solution would be to just malloc a TB struct when needed, and keep the TB array as an array of pointers (recall that tb_find_pc() needs the TB array to run in O(log n)). Perhaps a better solution, which is implemented in this patch, is to allocate TB's right before the translated code they describe. This results in some memory waste due to padding to have code and TBs in separate cache lines--for instance, I measured 4.7% of padding in the used portion of code_gen_buffer when booting aarch64 Linux on a host with 64-byte cache lines. However, it can allow for optimizations in some host architectures, since TCG backends could safely assume that the TB and the corresponding translated code are very close to each other in memory. See this message by rth for a detailed explanation: https://lists.gnu.org/archive/html/qemu-devel/2017-03/msg05172.html Subject: Re: GSoC 2017 Proposal: TCG performance enhancements Message-ID: <1e67644b-4b30-887e-d329-1848e94c9484@twiddle.net> Suggested-by: Richard Henderson Signed-off-by: Emilio G. Cota Reviewed-by: Pranith Kumar --- include/exec/exec-all.h | 2 +- include/exec/tb-context.h | 3 ++- tcg/tcg.c | 16 ++++++++++++++++ tcg/tcg.h | 2 +- translate-all.c | 37 ++++++++++++++++++++++--------------- 5 files changed, 42 insertions(+), 18 deletions(-) diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h index 87ae10b..00c0f43 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -363,7 +363,7 @@ struct TranslationBlock { */ uintptr_t jmp_list_next[2]; uintptr_t jmp_list_first; -}; +} QEMU_ALIGNED(QEMU_CACHELINE_SIZE); =20 void tb_free(TranslationBlock *tb); void tb_flush(CPUState *cpu); diff --git a/include/exec/tb-context.h b/include/exec/tb-context.h index c7f17f2..25c2afe 100644 --- a/include/exec/tb-context.h +++ b/include/exec/tb-context.h @@ -31,8 +31,9 @@ typedef struct TBContext TBContext; =20 struct TBContext { =20 - TranslationBlock *tbs; + TranslationBlock **tbs; struct qht htable; + size_t tbs_size; int nb_tbs; /* any access to the tbs or the page table must use this lock */ QemuMutex tb_lock; diff --git a/tcg/tcg.c b/tcg/tcg.c index 564292f..f657c51 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -383,6 +383,22 @@ void tcg_context_init(TCGContext *s) } } =20 +/* + * Allocate TBs right before their corresponding translated code, making + * sure that TBs and code are on different cache lines. + */ +TranslationBlock *tcg_tb_alloc(TCGContext *s) +{ + void *aligned; + + aligned =3D (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, QEMU_CACHELIN= E_SIZE); + if (unlikely(aligned + sizeof(TranslationBlock) > s->code_gen_highwate= r)) { + return NULL; + } + s->code_gen_ptr +=3D aligned - s->code_gen_ptr + sizeof(TranslationBlo= ck); + return aligned; +} + void tcg_prologue_init(TCGContext *s) { size_t prologue_size, total_size; diff --git a/tcg/tcg.h b/tcg/tcg.h index 5ec48d1..9e37722 100644 --- a/tcg/tcg.h +++ b/tcg/tcg.h @@ -697,7 +697,6 @@ struct TCGContext { here, because there's too much arithmetic throughout that relies on addition and subtraction working on bytes. Rely on the GCC extension that allows arithmetic on void*. */ - int code_gen_max_blocks; void *code_gen_prologue; void *code_gen_epilogue; void *code_gen_buffer; @@ -756,6 +755,7 @@ static inline bool tcg_op_buf_full(void) /* tb_lock must be held for tcg_malloc_internal. */ void *tcg_malloc_internal(TCGContext *s, int size); void tcg_pool_reset(TCGContext *s); +TranslationBlock *tcg_tb_alloc(TCGContext *s); =20 void tb_lock(void); void tb_unlock(void); diff --git a/translate-all.c b/translate-all.c index b3ee876..0eb9d13 100644 --- a/translate-all.c +++ b/translate-all.c @@ -781,12 +781,13 @@ static inline void code_gen_alloc(size_t tb_size) exit(1); } =20 - /* Estimate a good size for the number of TBs we can support. We - still haven't deducted the prologue from the buffer size here, - but that's minimal and won't affect the estimate much. */ - tcg_ctx.code_gen_max_blocks - =3D tcg_ctx.code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE; - tcg_ctx.tb_ctx.tbs =3D g_new(TranslationBlock, tcg_ctx.code_gen_max_bl= ocks); + /* size this conservatively -- realloc later if needed */ + tcg_ctx.tb_ctx.tbs_size =3D + tcg_ctx.code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE / 8; + if (unlikely(!tcg_ctx.tb_ctx.tbs_size)) { + tcg_ctx.tb_ctx.tbs_size =3D 64 * 1024; + } + tcg_ctx.tb_ctx.tbs =3D g_new(TranslationBlock *, tcg_ctx.tb_ctx.tbs_si= ze); =20 qemu_mutex_init(&tcg_ctx.tb_ctx.tb_lock); } @@ -828,13 +829,20 @@ bool tcg_enabled(void) static TranslationBlock *tb_alloc(target_ulong pc) { TranslationBlock *tb; + TBContext *ctx; =20 assert_tb_locked(); =20 - if (tcg_ctx.tb_ctx.nb_tbs >=3D tcg_ctx.code_gen_max_blocks) { + tb =3D tcg_tb_alloc(&tcg_ctx); + if (unlikely(tb =3D=3D NULL)) { return NULL; } - tb =3D &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs++]; + ctx =3D &tcg_ctx.tb_ctx; + if (unlikely(ctx->nb_tbs =3D=3D ctx->tbs_size)) { + ctx->tbs_size *=3D 2; + ctx->tbs =3D g_renew(TranslationBlock *, ctx->tbs, ctx->tbs_size); + } + ctx->tbs[ctx->nb_tbs++] =3D tb; tb->pc =3D pc; tb->cflags =3D 0; tb->invalid =3D false; @@ -850,8 +858,8 @@ void tb_free(TranslationBlock *tb) Ignore the hard cases and just back up if this TB happens to be the last one generated. */ if (tcg_ctx.tb_ctx.nb_tbs > 0 && - tb =3D=3D &tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs - 1]) { - tcg_ctx.code_gen_ptr =3D tb->tc_ptr; + tb =3D=3D tcg_ctx.tb_ctx.tbs[tcg_ctx.tb_ctx.nb_tbs - 1]) { + tcg_ctx.code_gen_ptr =3D tb->tc_ptr - sizeof(TranslationBlock); tcg_ctx.tb_ctx.nb_tbs--; } } @@ -1666,7 +1674,7 @@ static TranslationBlock *tb_find_pc(uintptr_t tc_ptr) m_max =3D tcg_ctx.tb_ctx.nb_tbs - 1; while (m_min <=3D m_max) { m =3D (m_min + m_max) >> 1; - tb =3D &tcg_ctx.tb_ctx.tbs[m]; + tb =3D tcg_ctx.tb_ctx.tbs[m]; v =3D (uintptr_t)tb->tc_ptr; if (v =3D=3D tc_ptr) { return tb; @@ -1676,7 +1684,7 @@ static TranslationBlock *tb_find_pc(uintptr_t tc_ptr) m_min =3D m + 1; } } - return &tcg_ctx.tb_ctx.tbs[m_max]; + return tcg_ctx.tb_ctx.tbs[m_max]; } =20 #if !defined(CONFIG_USER_ONLY) @@ -1874,7 +1882,7 @@ void dump_exec_info(FILE *f, fprintf_function cpu_fpr= intf) direct_jmp_count =3D 0; direct_jmp2_count =3D 0; for (i =3D 0; i < tcg_ctx.tb_ctx.nb_tbs; i++) { - tb =3D &tcg_ctx.tb_ctx.tbs[i]; + tb =3D tcg_ctx.tb_ctx.tbs[i]; target_code_size +=3D tb->size; if (tb->size > max_target_code_size) { max_target_code_size =3D tb->size; @@ -1894,8 +1902,7 @@ void dump_exec_info(FILE *f, fprintf_function cpu_fpr= intf) cpu_fprintf(f, "gen code size %td/%zd\n", tcg_ctx.code_gen_ptr - tcg_ctx.code_gen_buffer, tcg_ctx.code_gen_highwater - tcg_ctx.code_gen_buffer); - cpu_fprintf(f, "TB count %d/%d\n", - tcg_ctx.tb_ctx.nb_tbs, tcg_ctx.code_gen_max_blocks); + cpu_fprintf(f, "TB count %d\n", tcg_ctx.tb_ctx.nb_tbs); cpu_fprintf(f, "TB avg target size %d max=3D%d bytes\n", tcg_ctx.tb_ctx.nb_tbs ? target_code_size / tcg_ctx.tb_ctx.nb_tbs : 0, --=20 2.7.4