From nobody Tue Nov 11 14:22:54 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1563584634; cv=none; d=zoho.com; s=zohoarc; b=iyKHwPXdcITCLNUZiEAImRLhw/RcLjgPq4Ls+jzwAiwICdWIyUEVsRV9vzBxVdT76axBon66GNEwApNgD6deGdfMeijyX9Dw9vFnY8c1NRlo3M2KI5YuFc+VKkpoWe5QiOdhv0L1p+OIwcLjgfsq/sadOUnVffy9G9QFtgzqN24= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563584634; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=9s93kcx9QHi1RLJvtLWRybSgOGkahFDvEpGxVZRpbGE=; b=JKClDW/jfFRVff7Sk0Ct494yPf3ID6lN2EV3D8MjweXpZlvuw4jAVoOPYhUb9UCvkECalJphXcV0btOmh+8R3ihIe7djMrx5zR/BF7ArTmKxhSALxCTv8xwETSzJaUbHAUToBa38TCUNQy02pLQO76wyD0hrH8lVJRjEWgZxlqg= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1563584634135317.59144619343624; Fri, 19 Jul 2019 18:03:54 -0700 (PDT) Received: from localhost ([::1]:49016 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hodmx-0007fi-L8 for importer@patchew.org; Fri, 19 Jul 2019 21:03:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39958) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hodmP-00068o-LY for qemu-devel@nongnu.org; Fri, 19 Jul 2019 21:03:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hodmO-0006EH-5f for qemu-devel@nongnu.org; Fri, 19 Jul 2019 21:03:17 -0400 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]:39136) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hodmO-0006Du-1H for qemu-devel@nongnu.org; Fri, 19 Jul 2019 21:03:16 -0400 Received: by mail-qt1-x844.google.com with SMTP id l9so32929605qtu.6 for ; Fri, 19 Jul 2019 18:03:15 -0700 (PDT) Received: from vanderson-lmcad.domain.name ([177.137.96.41]) by smtp.googlemail.com with ESMTPSA id g10sm13405115qki.37.2019.07.19.18.03.13 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 19 Jul 2019 18:03:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9s93kcx9QHi1RLJvtLWRybSgOGkahFDvEpGxVZRpbGE=; b=YRgTbZ8hrgIkYUEc3Xrk2mV7iaEqRgzJurT+KkmGyS+hhqSlThakeG9WOGFj4eLmOk QrwFag4M+3DfI+cm2UPcCxD3XsWNFzGeGyQZNZQuCTL7+SlEC+RQtvIPDfhBQdV0NF4k 8Enao9xv/jbPKSpcBjXzyOr8n53y0cgUP/TFTwdXLKiFn0psbgA+MBZjrN6aL/xwH9Vx iK6z2wJqAKGVH8D1SWrSVZIR/VE5+VTK+yLXHIJfrjaOzW4/vXsLBqORTjoBJhX4SMko ums+wXgOjq7ZJaFTK2Qpe2GSwcWl4L0R+pBtU8Kty5WGxy3o6FcoiAqNRYOcFsv7CNU7 TkrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9s93kcx9QHi1RLJvtLWRybSgOGkahFDvEpGxVZRpbGE=; b=Z2vpN6KpiZBTKo16NSNKWY35NiKJwKXa19CoPZQcTDYtpQ9jy49iiELrhsGRCyDMH7 Cxj0ApIiNoN8yYN8THqOcUflPZifIEoIdwxvc0VipLlft6ieY940H2psZNNXiupx+ooc AQeoBOkNfGnUc4wIDCR8ZDcA7n+GWaThZJl02avNj+ShHihqFQcR7vl7tfZKMReNvX6X Tx+FU84OvZ0dEIIgzvBgKMVLkukiqUGKffXbVGdM9jFxHqNQC8jb0qsjvd/SXMbs5OxW W+KbY9+eqai3e4cwa1z6OJZCpunrVy9IdccL6UHx4JnDGiDVk6JhURCTzKMK4LA5eelw yxaA== X-Gm-Message-State: APjAAAUt2L5180uywy7/IPRzJaLQ6ymE62pY+GKE8Ywap2BASeCXr5yS sxIUIp4vcvqCWG8UvDRf/4umEhP8 X-Google-Smtp-Source: APXvYqxoogNAjWlWYTWcqbIUNmOCKTQMUFd+Bd1djLeHlwympBxktA6JP1G86ukCLmn40xRORmIwRA== X-Received: by 2002:a0c:92ca:: with SMTP id c10mr39805114qvc.108.1563584594884; Fri, 19 Jul 2019 18:03:14 -0700 (PDT) From: vandersonmr To: qemu-devel@nongnu.org Date: Fri, 19 Jul 2019 22:02:29 -0300 Message-Id: <20190720010235.32444-2-vandersonmr2@gmail.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190720010235.32444-1-vandersonmr2@gmail.com> References: <20190720010235.32444-1-vandersonmr2@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::844 Subject: [Qemu-devel] [PATCH v4 1/7] accel: introducing TBStatistics structure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , vandersonmr , Richard Henderson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" To store statistics for each TB we created a TBStatistics structure which is linked with the TBs. The TBStatistics can stay alive after tb_flush and be relinked to a regenerated TB. So the statistics can be accumulated even through flushes. TBStatistics will be also referred to as TBS or tbstats. Signed-off-by: Vanderson M. do Rosario --- accel/tcg/translate-all.c | 57 +++++++++++++++++++++++++++++++++++++++ include/exec/exec-all.h | 15 +++-------- include/exec/tb-context.h | 12 +++++++++ include/exec/tb-hash.h | 7 +++++ include/exec/tb-stats.h | 29 ++++++++++++++++++++ include/qemu/log.h | 6 +++++ util/log.c | 28 +++++++++++++++++++ 7 files changed, 143 insertions(+), 11 deletions(-) create mode 100644 include/exec/tb-stats.h diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 5d1e08b169..a574890a80 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -1118,6 +1118,23 @@ static inline void code_gen_alloc(size_t tb_size) } } =20 +/* + * This is the more or less the same compare, but the data persists + * over tb_flush. We also aggregate the various variations of cflags + * under one record and ignore the details of page overlap (although + * we can count it). + */ +bool tb_stats_cmp(const void *ap, const void *bp) +{ + const TBStatistics *a =3D ap; + const TBStatistics *b =3D bp; + + return a->phys_pc =3D=3D b->phys_pc && + a->pc =3D=3D b->pc && + a->cs_base =3D=3D b->cs_base && + a->flags =3D=3D b->flags; +} + static bool tb_cmp(const void *ap, const void *bp) { const TranslationBlock *a =3D ap; @@ -1137,6 +1154,9 @@ static void tb_htable_init(void) unsigned int mode =3D QHT_MODE_AUTO_RESIZE; =20 qht_init(&tb_ctx.htable, tb_cmp, CODE_GEN_HTABLE_SIZE, mode); + if (tb_stats_collection_enabled()) { + qht_init(&tb_ctx.tb_stats, tb_stats_cmp, CODE_GEN_HTABLE_SIZE, mod= e); + } } =20 /* Must be called before using the QEMU cpus. 'tb_size' is the size @@ -1666,6 +1686,32 @@ tb_link_page(TranslationBlock *tb, tb_page_addr_t ph= ys_pc, return tb; } =20 +static TBStatistics *tb_get_stats(tb_page_addr_t phys_pc, target_ulong pc, + target_ulong cs_base, uint32_t flags) +{ + TBStatistics *new_stats =3D g_new0(TBStatistics, 1); + uint32_t hash =3D tb_stats_hash_func(phys_pc, pc, flags); + void *existing_stats =3D NULL; + new_stats->phys_pc =3D phys_pc; + new_stats->pc =3D pc; + new_stats->cs_base =3D cs_base; + new_stats->flags =3D flags; + + qht_insert(&tb_ctx.tb_stats, new_stats, hash, &existing_stats); + + if (unlikely(existing_stats)) { + /* + * If there is already a TBStatistic for this TB from a previous f= lush + * then just make the new TB point to the older TBStatistic + */ + g_free(new_stats); + return existing_stats; + } else { + return new_stats; + } +} + + /* Called with mmap_lock held for user mode emulation. */ TranslationBlock *tb_gen_code(CPUState *cpu, target_ulong pc, target_ulong cs_base, @@ -1732,6 +1778,17 @@ TranslationBlock *tb_gen_code(CPUState *cpu, ti =3D profile_getclock(); #endif =20 + /* + * We want to fetch the stats structure before we start code + * generation so we can count interesting things about this + * generation. + */ + if (tb_stats_collection_enabled()) { + tb->tb_stats =3D tb_get_stats(phys_pc, pc, cs_base, flags); + } else { + tb->tb_stats =3D NULL; + } + tcg_func_start(tcg_ctx); =20 tcg_ctx->cpu =3D env_cpu(env); diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h index 16034ee651..24bd6a0a0c 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -22,21 +22,11 @@ =20 #include "exec/tb-context.h" #include "sysemu/cpus.h" +#include "exec/tb-stats.h" =20 /* allow to see translation results - the slowdown should be negligible, s= o we leave it */ #define DEBUG_DISAS =20 -/* Page tracking code uses ram addresses in system mode, and virtual - addresses in userspace mode. Define tb_page_addr_t to be an appropriate - type. */ -#if defined(CONFIG_USER_ONLY) -typedef abi_ulong tb_page_addr_t; -#define TB_PAGE_ADDR_FMT TARGET_ABI_FMT_lx -#else -typedef ram_addr_t tb_page_addr_t; -#define TB_PAGE_ADDR_FMT RAM_ADDR_FMT -#endif - #include "qemu/log.h" =20 void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_in= sns); @@ -403,6 +393,9 @@ struct TranslationBlock { uintptr_t jmp_list_head; uintptr_t jmp_list_next[2]; uintptr_t jmp_dest[2]; + + /* Pointer to a struct where statistics from the TB is stored */ + TBStatistics *tb_stats; }; =20 extern bool parallel_cpus; diff --git a/include/exec/tb-context.h b/include/exec/tb-context.h index feb585e0a7..3cfb62a338 100644 --- a/include/exec/tb-context.h +++ b/include/exec/tb-context.h @@ -23,6 +23,17 @@ #include "qemu/thread.h" #include "qemu/qht.h" =20 +/* Page tracking code uses ram addresses in system mode, and virtual + addresses in userspace mode. Define tb_page_addr_t to be an appropriate + type. */ +#if defined(CONFIG_USER_ONLY) +typedef abi_ulong tb_page_addr_t; +#define TB_PAGE_ADDR_FMT TARGET_ABI_FMT_lx +#else +typedef ram_addr_t tb_page_addr_t; +#define TB_PAGE_ADDR_FMT RAM_ADDR_FMT +#endif + #define CODE_GEN_HTABLE_BITS 15 #define CODE_GEN_HTABLE_SIZE (1 << CODE_GEN_HTABLE_BITS) =20 @@ -35,6 +46,7 @@ struct TBContext { =20 /* statistics */ unsigned tb_flush_count; + struct qht tb_stats; }; =20 extern TBContext tb_ctx; diff --git a/include/exec/tb-hash.h b/include/exec/tb-hash.h index 4f3a37d927..54c477fe79 100644 --- a/include/exec/tb-hash.h +++ b/include/exec/tb-hash.h @@ -64,4 +64,11 @@ uint32_t tb_hash_func(tb_page_addr_t phys_pc, target_ulo= ng pc, uint32_t flags, return qemu_xxhash7(phys_pc, pc, flags, cf_mask, trace_vcpu_dstate); } =20 +static inline +uint32_t tb_stats_hash_func(tb_page_addr_t phys_pc, target_ulong pc, + uint32_t flags) +{ + return qemu_xxhash5(phys_pc, pc, flags); +} + #endif diff --git a/include/exec/tb-stats.h b/include/exec/tb-stats.h new file mode 100644 index 0000000000..0913155ec3 --- /dev/null +++ b/include/exec/tb-stats.h @@ -0,0 +1,29 @@ +#ifndef TB_STATS_H + +#define TB_STATS_H + +#include "exec/cpu-common.h" +#include "exec/tb-context.h" +#include "tcg.h" + +typedef struct TBStatistics TBStatistics; + +/* + * This struct stores statistics such as execution count of the + * TranslationBlocks. Each sets of TBs for a given phys_pc/pc/flags + * has its own TBStatistics which will persist over tb_flush. + * + * We include additional counters to track number of translations as + * well as variants for compile flags. + */ +struct TBStatistics { + tb_page_addr_t phys_pc; + target_ulong pc; + uint32_t flags; + /* cs_base isn't included in the hash but we do check for matches */ + target_ulong cs_base; +}; + +bool tb_stats_cmp(const void *ap, const void *bp); + +#endif diff --git a/include/qemu/log.h b/include/qemu/log.h index b097a6cae1..e175d4d5d0 100644 --- a/include/qemu/log.h +++ b/include/qemu/log.h @@ -129,4 +129,10 @@ void qemu_log_flush(void); /* Close the log file */ void qemu_log_close(void); =20 +void enable_collect_tb_stats(void); +void disable_collect_tb_stats(void); +void pause_collect_tb_stats(void); +bool tb_stats_collection_enabled(void); +bool tb_stats_collection_paused(void); + #endif diff --git a/util/log.c b/util/log.c index 1d1b33f7d9..ab73fdc100 100644 --- a/util/log.c +++ b/util/log.c @@ -326,3 +326,31 @@ void qemu_print_log_usage(FILE *f) fprintf(f, "\nUse \"-d trace:help\" to get a list of trace events.\n\n= "); #endif } + +int tcg_collect_tb_stats; + +void enable_collect_tb_stats(void) +{ + tcg_collect_tb_stats =3D 1; +} + +void disable_collect_tb_stats(void) +{ + tcg_collect_tb_stats =3D 0; +} + +void pause_collect_tb_stats(void) +{ + tcg_collect_tb_stats =3D 2; +} + +bool tb_stats_collection_enabled(void) +{ + return tcg_collect_tb_stats; +} + +bool tb_stats_collection_paused(void) +{ + return tcg_collect_tb_stats =3D=3D 2; +} + --=20 2.22.0