From nobody Wed Nov 5 17:25: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 1496790840882574.9721502040425; Tue, 6 Jun 2017 16:14:00 -0700 (PDT) Received: from localhost ([::1]:40390 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dINfj-00057m-C2 for importer@patchew.org; Tue, 06 Jun 2017 19:13:59 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42239) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dINeL-0003wX-Ni for qemu-devel@nongnu.org; Tue, 06 Jun 2017 19:12:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dINeI-0002I3-IM for qemu-devel@nongnu.org; Tue, 06 Jun 2017 19:12:33 -0400 Received: from out1-smtp.messagingengine.com ([66.111.4.25]:59181) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dINeI-0002Gn-0l for qemu-devel@nongnu.org; Tue, 06 Jun 2017 19:12:30 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 7FD4220DF8; Tue, 6 Jun 2017 19:12:27 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute4.internal (MEProxy); Tue, 06 Jun 2017 19:12:27 -0400 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 32E7F7E8E5; Tue, 6 Jun 2017 19:12:27 -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=Drm 4jjtk2cr2hKOZ/otChL9zPiUmFghI3xSoDS9fWKs=; b=qX14NZzgLY7nfC/HSPN vV4AL8qtpEll6WkNWslmUk5wtS/MLOJec61UnPTWQWJmMd18AnNbtV5txhqTmiW5 fu7dCO5VEvJHTzqgFrGWm99zRW6ASi8HOWaSDu5RwiHRK4uuf0SQ5Z3VDosTD4QM 6mxA6NlRnYXqAMMIqpdt6U94= 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=Drm4jjtk2cr2hKOZ/otChL9zPiUmFghI3xSoDS9fW Ks=; b=fs3oUq4id9K8Kr057NUxMXIQultYU6EGbHHyEjmXf3CMDeEPWw5Iyf+2x Xs8vtsIUAFFX4rR2RuB+/GBmhjRP339ddUe5B/LeSHXQMd7ybWHtVgSvIedD6dTW LJLtOEHHlpYaW60cgdGC8xuPvAQHcBc+c105FflAZi8BhaDa8og3LPc/wJ+I36EV kIuc5yS8IYAM4F48BYraito87Pu43kA4JyzkrX5xHuqeFB5e8Ci1NZi513fUfUI2 bqfikoh9UMtLHGGXYLaj9YpX5Y18GFP9G+K7GHH0E0ouN7AxSZ0IgOVXh/h5KeuK hz6kXQxwLZSFRRhWA1dkJOuf6yTcw== X-ME-Sender: X-Sasl-enc: g2VzTb1256xpdYBSuUREipRtwVmUH6T/sqPfmr/B1z9p 1496790747 From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Tue, 6 Jun 2017 19:12:25 -0400 Message-Id: <1496790745-314-3-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1496790745-314-1-git-send-email-cota@braap.org> References: <1496790745-314-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 v3 2/2] 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 , Geert Martin Ijewski , Paolo Bonzini , Pranith Kumar , 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 Reviewed-by: Pranith Kumar Signed-off-by: Emilio G. Cota --- include/exec/tb-context.h | 3 ++- tcg/tcg.c | 19 +++++++++++++++++++ tcg/tcg.h | 2 +- translate-all.c | 39 ++++++++++++++++++++++++--------------- 4 files changed, 46 insertions(+), 17 deletions(-) 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..9120e7a 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -383,6 +383,25 @@ 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) +{ + size_t struct_size; + void *aligned; + + struct_size =3D ROUND_UP(sizeof(TranslationBlock), qemu_icache_linesiz= e); + aligned =3D (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, + qemu_icache_linesize); + if (unlikely(aligned + struct_size > s->code_gen_highwater)) { + return NULL; + } + s->code_gen_ptr +=3D aligned - s->code_gen_ptr + struct_size; + 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..bb094ad 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,10 @@ 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]) { + size_t struct_size =3D ROUND_UP(sizeof(*tb), qemu_icache_linesize); + + tcg_ctx.code_gen_ptr =3D tb->tc_ptr - struct_size; tcg_ctx.tb_ctx.nb_tbs--; } } @@ -1666,7 +1676,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 +1686,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 +1884,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 +1904,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