From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06CF6C63793 for ; Thu, 22 Jul 2021 07:03:15 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 62AFA60FE9 for ; Thu, 22 Jul 2021 07:03:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 62AFA60FE9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:34430 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m6Sjh-0008RP-3z for qemu-devel@archiver.kernel.org; Thu, 22 Jul 2021 03:03:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45028) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m6Sba-0008TC-HA for qemu-devel@nongnu.org; Thu, 22 Jul 2021 02:54:50 -0400 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]:42846) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1m6SbY-0006vj-H4 for qemu-devel@nongnu.org; Thu, 22 Jul 2021 02:54:50 -0400 Received: by mail-wr1-x42b.google.com with SMTP id r11so4699431wro.9 for ; Wed, 21 Jul 2021 23:54:48 -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=vOjUscqVzJcRPYkS7J51fxMeax0tJPb3jN+fReD0gug=; b=FzTmGIvasPIa1iHYAWUhN4P2PGxNCqu+kxdjraOK1CuSG5gPuwSSoUvubmHysvAWBu E7q8KeWPEyatkwbR052dVnqWaCLyRxo6Z87ryGgmHBlEJNl2ZKEXHpd6QMkqKpkrSMum 2o9U7TPsOC2/Jwbr7DiWwMW4Ciw4HPifp2UtTf10gLkmtUrpHSpumRXLXwnIf6zFqf3v NZ4Na/xQzBf2oV+F9tooxNwdgUpmIouzN8GcpHVBbvZRjkRGMk1+n2y2KWt03nwwjhLC xhRKqdYZSBleFg8UX2fJ7f9NHZaBvOiz6dRQXL/RT28eZeHbPoXznYvSM1vRFa634Dq2 mTow== 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=vOjUscqVzJcRPYkS7J51fxMeax0tJPb3jN+fReD0gug=; b=KLtRYOEhUuBsIv+iCVGQZJJXIMxm7CW9ZX+N8LXwNwbUZ52Upt7mBS5SQOSSrbFhQs K+ff2EUehJWo0tGYHzI8WVuaKPtKuiijDRZeX/joi43iVmh94j2m3VsWipN2nWIGCwVe nKXx3JTT7Pe63TjckEkUui0+YXq+DIBcVqaBTpomAl8PoQUQIe2Nz+xfB2mhMroPLg5R qgLZVeu3MnDrSKJHExZe466G+ttKgttwIsJMs3ANe03iFaycfDGYmfgQ7ZPvAfrKhwaK iC+Y3kwbeuLxfyeLzB3jbNSdQ2gbiIofdcNBbH9GIDm7AgMD5LZmAtr7b45Lo9QQO+fA VfOg== X-Gm-Message-State: AOAM5313qAuFrOgr3YkmWLhVZEYshsPK9KNIpRZ2Em8ERy65KGBu9XgT O+3BRWhC4uB+7wdkksgK4la4ePaGTZA= X-Google-Smtp-Source: ABdhPJxvc/3uqBIvjZTBjmxGlMCkTJZLorv013RnF0d5VkLYxJhYmUsLmlDwBtTxnzW7eI/YweYCFg== X-Received: by 2002:a5d:4e4d:: with SMTP id r13mr46679933wrt.224.1626936886893; Wed, 21 Jul 2021 23:54:46 -0700 (PDT) Received: from localhost.localdomain ([102.44.10.38]) by smtp.gmail.com with ESMTPSA id b16sm29245426wrw.46.2021.07.21.23.54.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jul 2021 23:54:46 -0700 (PDT) From: Mahmoud Mandour To: qemu-devel@nongnu.org Subject: [PATCH v3 3/5] plugins/cache: Supported multicore cache modelling Date: Thu, 22 Jul 2021 08:54:26 +0200 Message-Id: <20210722065428.134608-4-ma.mandourr@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210722065428.134608-1-ma.mandourr@gmail.com> References: <20210722065428.134608-1-ma.mandourr@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2a00:1450:4864:20::42b; envelope-from=ma.mandourr@gmail.com; helo=mail-wr1-x42b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: Mahmoud Mandour , cota@braap.org, =?UTF-8?q?Alex=20Benn=C3=A9e?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Multicore L1 cache modelling is introduced and is supported for both full system emulation and linux-user. For full-system emulation, L1 icache and dcache are maintained for each available core, since this information is exposed to the plugin through `qemu_plugin_n_vcpus()`. For linux-user, a static number of cores is assumed (default 1 core, and can be provided as a plugin argument `cores=N`). Every memory access goes through one of these caches, this approach is taken as it's somewhat akin to what happens on real setup, where a program that dispatches more threads than the available cores, they'll thrash each other Signed-off-by: Mahmoud Mandour --- contrib/plugins/cache.c | 154 +++++++++++++++++++++++++++++++--------- 1 file changed, 119 insertions(+), 35 deletions(-) diff --git a/contrib/plugins/cache.c b/contrib/plugins/cache.c index 695fb969dc..496d6e7d49 100644 --- a/contrib/plugins/cache.c +++ b/contrib/plugins/cache.c @@ -23,12 +23,6 @@ static GRand *rng; static int limit; static bool sys; -static uint64_t dmem_accesses; -static uint64_t dmisses; - -static uint64_t imem_accesses; -static uint64_t imisses; - enum EvictionPolicy { LRU, FIFO, @@ -90,13 +84,22 @@ typedef struct { uint64_t imisses; } InsnData; +typedef struct { + uint64_t dmem_accesses; + uint64_t dmisses; + uint64_t imem_accesses; + uint64_t imisses; +} CoreStats; + void (*update_hit)(Cache *cache, int set, int blk); void (*update_miss)(Cache *cache, int set, int blk); void (*metadata_init)(Cache *cache); void (*metadata_destroy)(Cache *cache); -Cache *dcache, *icache; +static int cores; +CoreStats *stats; +Cache **dcaches, **icaches; static int pow_of_two(int num) { @@ -233,14 +236,16 @@ static bool bad_cache_params(int blksize, int assoc, int cachesize) static Cache *cache_init(int blksize, int assoc, int cachesize) { - if (bad_cache_params(blksize, assoc, cachesize)) { - return NULL; - } - Cache *cache; int i; uint64_t blk_mask; + /* + * This function shall not be called directly, and hence expects suitable + * parameters. + */ + g_assert(!bad_cache_params(blksize, assoc, cachesize)); + cache = g_new(Cache, 1); cache->assoc = assoc; cache->cachesize = cachesize; @@ -263,6 +268,24 @@ static Cache *cache_init(int blksize, int assoc, int cachesize) return cache; } +static Cache **caches_init(int blksize, int assoc, int cachesize) +{ + Cache **caches; + int i; + + if (bad_cache_params(blksize, assoc, cachesize)) { + return NULL; + } + + caches = g_new(Cache *, cores); + + for (i = 0; i < cores; i++) { + caches[i] = cache_init(blksize, assoc, cachesize); + } + + return caches; +} + static int get_invalid_block(Cache *cache, uint64_t set) { int i; @@ -353,6 +376,7 @@ static void vcpu_mem_access(unsigned int vcpu_index, qemu_plugin_meminfo_t info, { uint64_t effective_addr; struct qemu_plugin_hwaddr *hwaddr; + int cache_idx; InsnData *insn; hwaddr = qemu_plugin_get_hwaddr(info, vaddr); @@ -361,14 +385,15 @@ static void vcpu_mem_access(unsigned int vcpu_index, qemu_plugin_meminfo_t info, } effective_addr = hwaddr ? qemu_plugin_hwaddr_phys_addr(hwaddr) : vaddr; + cache_idx = vcpu_index % cores; g_mutex_lock(&mtx); - if (!access_cache(dcache, effective_addr)) { + if (!access_cache(dcaches[cache_idx], effective_addr)) { insn = (InsnData *) userdata; insn->dmisses++; - dmisses++; + stats[cache_idx].dmisses++; } - dmem_accesses++; + stats[cache_idx].dmem_accesses++; g_mutex_unlock(&mtx); } @@ -376,16 +401,18 @@ static void vcpu_insn_exec(unsigned int vcpu_index, void *userdata) { uint64_t insn_addr; InsnData *insn; + int cache_idx; g_mutex_lock(&mtx); insn_addr = ((InsnData *) userdata)->addr; + cache_idx = vcpu_index % cores; - if (!access_cache(icache, insn_addr)) { + if (!access_cache(icaches[cache_idx], insn_addr)) { insn = (InsnData *) userdata; insn->imisses++; - imisses++; + stats[cache_idx].imisses++; } - imem_accesses++; + stats[cache_idx].imem_accesses++; g_mutex_unlock(&mtx); } @@ -453,6 +480,15 @@ static void cache_free(Cache *cache) g_free(cache); } +static void caches_free(Cache **caches) +{ + int i; + + for (i = 0; i < cores; i++) { + cache_free(caches[i]); + } +} + static int dcmp(gconstpointer a, gconstpointer b) { InsnData *insn_a = (InsnData *) a; @@ -469,21 +505,57 @@ static int icmp(gconstpointer a, gconstpointer b) return insn_a->imisses < insn_b->imisses ? 1 : -1; } +static void append_stats_line(GString *line, CoreStats cs) +{ + double dmiss_rate, imiss_rate; + + dmiss_rate = ((double) cs.dmisses) / (cs.dmem_accesses) * 100.0; + imiss_rate = ((double) cs.imisses) / (cs.imem_accesses) * 100.0; + + g_string_append_printf(line, "%-14lu %-12lu %9.4lf%% %-14lu %-12lu" + " %9.4lf%%\n", + cs.dmem_accesses, + cs.dmisses, + cs.dmem_accesses ? dmiss_rate : 0.0, + cs.imem_accesses, + cs.imisses, + cs.imem_accesses ? imiss_rate : 0.0); +} + +static void sum_stats(void) +{ + int i; + + g_assert(cores > 1); + for (i = 0; i < cores; i++) { + stats[cores].imisses += stats[i].imisses; + stats[cores].dmisses += stats[i].dmisses; + stats[cores].dmem_accesses += stats[i].dmem_accesses; + stats[cores].imem_accesses += stats[i].imem_accesses; + } +} + static void log_stats() { - g_autoptr(GString) rep = g_string_new(""); - g_string_append_printf(rep, - "Data accesses: %lu, Misses: %lu\nMiss rate: %lf%%\n\n", - dmem_accesses, - dmisses, - ((double) dmisses / (double) dmem_accesses) * 100.0); - - g_string_append_printf(rep, - "Instruction accesses: %lu, Misses: %lu\nMiss rate: %lf%%\n\n", - imem_accesses, - imisses, - ((double) imisses / (double) imem_accesses) * 100.0); + int i, iters; + + g_autoptr(GString) rep = g_string_new("core #, data accesses, data misses," + " dmiss rate, insn accesses," + " insn misses, imiss rate\n"); + + /* Only iterate and print a sum row if cores > 1 */ + iters = cores == 1 ? 1 : cores + 1; + for (i = 0; i < iters; i++) { + if (i == cores) { + g_string_append_printf(rep, "%-8s", "sum"); + sum_stats(); + } else { + g_string_append_printf(rep, "%-8d", i); + } + append_stats_line(rep, stats[i]); + } + g_string_append(rep, "\n"); qemu_plugin_outs(rep->str); } @@ -530,10 +602,12 @@ static void plugin_exit(qemu_plugin_id_t id, void *p) log_stats(); log_top_insns(); - cache_free(dcache); - cache_free(icache); + caches_free(dcaches); + caches_free(icaches); g_hash_table_destroy(miss_ht); + + g_free(stats); } static void policy_init() @@ -579,6 +653,8 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, policy = LRU; + cores = sys ? qemu_plugin_n_vcpus() : 1; + for (i = 0; i < argc; i++) { char *opt = argv[i]; if (g_str_has_prefix(opt, "iblksize=")) { @@ -595,6 +671,8 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, dcachesize = g_ascii_strtoll(opt + 11, NULL, 10); } else if (g_str_has_prefix(opt, "limit=")) { limit = g_ascii_strtoll(opt + 6, NULL, 10); + } else if (g_str_has_prefix(opt, "cores=")) { + cores = g_ascii_strtoll(opt + 6, NULL, 10); } else if (g_str_has_prefix(opt, "evict=")) { gchar *p = opt + 6; if (g_strcmp0(p, "rand") == 0) { @@ -615,22 +693,28 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, policy_init(); - dcache = cache_init(dblksize, dassoc, dcachesize); - if (!dcache) { + dcaches = caches_init(dblksize, dassoc, dcachesize); + if (!dcaches) { const char *err = cache_config_error(dblksize, dassoc, dcachesize); fprintf(stderr, "dcache cannot be constructed from given parameters\n"); fprintf(stderr, "%s\n", err); return -1; } - icache = cache_init(iblksize, iassoc, icachesize); - if (!icache) { + icaches = caches_init(iblksize, iassoc, icachesize); + if (!icaches) { const char *err = cache_config_error(iblksize, iassoc, icachesize); fprintf(stderr, "icache cannot be constructed from given parameters\n"); fprintf(stderr, "%s\n", err); return -1; } + /* + * plus one to save the sum in. If only one core is used then no need to + * get an auxiliary struct. + */ + stats = g_new0(CoreStats, cores == 1 ? 1 : cores + 1); + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); -- 2.25.1