From nobody Thu Jul 3 22:52:40 2025 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1622301862; cv=none; d=zohomail.com; s=zohoarc; b=j9Prbe156HNqwsDjuVmF50BBzmvf2E18IXFhAF259SKvHnL1WucyxJH97iZDq1eySUvsw4RWkEM3RNFmFW5DPL0imx6vycpJueMxdFe9ElAr1Pg5kKlz8WOFlLzJcgbzg3xOzSQgdswu0/kSqUy9I7h5qRaWXdYA2+f2t3VYo2w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622301862; 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; bh=SykQW2VZCed5IFbt4dsj/M5Blr8ToABeANkngq/IOEY=; b=NNX760G37TFwqjOxzEa+d67GP02hfhPJkbGE4boF1uNA8sWqzjM8DFQhwHoVwz6sMzMPmqqOd8qMISGFloCD/kV6+RBDRtsZfsJZu0r7eqh/B+ORVroW1x5mbkgSWb+GdQiNAgwE83fxmszvplUjDxh6wUAO2/uNu46vsZP+4hI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass 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 1622301862954730.4425327885353; Sat, 29 May 2021 08:24:22 -0700 (PDT) Received: from localhost ([::1]:49944 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ln0p4-0004oC-1M for importer2@patchew.org; Sat, 29 May 2021 11:24:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60586) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ln0nD-0001NM-9G for qemu-devel@nongnu.org; Sat, 29 May 2021 11:22:27 -0400 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]:46760) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ln0n8-0006ee-0Q for qemu-devel@nongnu.org; Sat, 29 May 2021 11:22:27 -0400 Received: by mail-wr1-x42e.google.com with SMTP id g17so6124774wrs.13 for ; Sat, 29 May 2021 08:22:20 -0700 (PDT) Received: from localhost.localdomain ([102.47.172.201]) by smtp.gmail.com with ESMTPSA id s7sm17019164wmh.35.2021.05.29.08.22.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 May 2021 08:22:18 -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=SykQW2VZCed5IFbt4dsj/M5Blr8ToABeANkngq/IOEY=; b=n6ghKfmeEOoxdfzW2/HOs89gT8Cl7B66gGBp8uV15rX3fzUhcmySfXpkg25rM/FsPF FQ99MJLPLFKklvJCNtIKNcVi+6sKSy9qRueS22aTNxF4nAiXK3tASGRU8gxgL5dWxSm4 /2UjA6TC/rgvA3ebAAOeJIyeydpsMrME/BrDKkRaZ/GWoyRT7N8rRXeiyKhUA3OTNIL2 4IfU2oXHa8kuaVNTIKOmL/NKiwb85Sz/0bKeK3d4/S6ovIThnudEtycln6jCdraHmUsb 0jS5VBLrgAoOS8J2rqwYNgRpGxPePwj8z6G8RN0ShgSaHmbj6AYRZbG9F3TdVJW1bq5P 9CRw== 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=SykQW2VZCed5IFbt4dsj/M5Blr8ToABeANkngq/IOEY=; b=KxU9y4Hk5xh5JrZqVP0C8uhcyF/LaPJpXr6AODaOU0QiuLY7ZH66+WFKgTtYtACSb3 2OSlnvtVpafqbR/y44FEeql4a/QFPffHx28yIB5BxBqEbcjEk+B8skaqi3JAcJIB+c/n SWywrq/MC6/KK4QNeIVMDvKZYisi/81rrev7K82vAtzlx1MwNyV9arxhV0vWEf7PH6WA uGiqEU9/ESBn7jvooVrxWsFQlqmpIgoSpo81WbQa1794KKSjSklDInAspQwiW+NAH6yl BDokkM3YsuJgc5tc21AjpYp+rdAfT5Q+LERs11uW09cMzscuDr2qGgmWv342HT4mFSe6 gf1g== X-Gm-Message-State: AOAM531VZvIqBdU/Ec0wXfrj85AIAcf0vZ+RRQf5orXY2rRRZrTFZHY2 UwMl5PMF+O6w3MA+qKYT5aT/iDfXzcbk1g== X-Google-Smtp-Source: ABdhPJyZcyZee2tXuskcpmYVw/KGdddzFr9rYuAz9A3uFzrtVQ5SFRmA/FSXxttsovBkxOb4aj2ctw== X-Received: by 2002:a5d:46cb:: with SMTP id g11mr2043533wrs.418.1622301738983; Sat, 29 May 2021 08:22:18 -0700 (PDT) From: Mahmoud Mandour To: qemu-devel@nongnu.org Subject: [RFC PATCH 2/3] plugins: cache: Enabled parameterization and added trace printing Date: Sat, 29 May 2021 17:22:02 +0200 Message-Id: <20210529152203.40358-3-ma.mandourr@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210529152203.40358-1-ma.mandourr@gmail.com> References: <20210529152203.40358-1-ma.mandourr@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::42e; envelope-from=ma.mandourr@gmail.com; helo=mail-wr1-x42e.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 , =?UTF-8?q?Alex=20Benn=C3=A9e?= Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @gmail.com) Content-Type: text/plain; charset="utf-8" Made both icache and dcache configurable through plugin arguments and added memory trace printing in a separate file. Signed-off-by: Mahmoud Mandour --- contrib/plugins/cache.c | 68 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/contrib/plugins/cache.c b/contrib/plugins/cache.c index f8c15ebed2..7d1d185480 100644 --- a/contrib/plugins/cache.c +++ b/contrib/plugins/cache.c @@ -22,7 +22,7 @@ static GRand *rng; static GHashTable *dmiss_ht; static GHashTable *imiss_ht; =20 -static GMutex dmtx, imtx; +static GMutex dmtx, imtx, fmtx; =20 static int limit; static bool sys; @@ -33,6 +33,8 @@ static uint64_t dmisses; static uint64_t imem_accesses; static uint64_t imisses; =20 +FILE *tracefile; + static enum qemu_plugin_mem_rw rw =3D QEMU_PLUGIN_MEM_RW; =20 enum AccessResult { @@ -205,6 +207,16 @@ static void vcpu_mem_access(unsigned int cpu_index, qe= mu_plugin_meminfo_t info, insn_addr =3D ((struct InsnData *) userdata)->addr; effective_addr =3D hwaddr ? qemu_plugin_hwaddr_phys_addr(hwaddr) : vad= dr; =20 + if (tracefile) { + g_mutex_lock(&fmtx); + g_autoptr(GString) rep =3D g_string_new(""); + bool is_store =3D qemu_plugin_mem_is_store(info); + g_string_append_printf(rep, "%c: 0x%" PRIx64, + is_store ? 'S' : 'L', effective_addr); + fprintf(tracefile, "%s\n", rep->str); + g_mutex_unlock(&fmtx); + } + if (access_cache(dcache, effective_addr) =3D=3D MISS) { struct InsnData *insn =3D get_or_create(dmiss_ht, userdata, insn_a= ddr); insn->misses++; @@ -221,11 +233,20 @@ static void vcpu_insn_exec(unsigned int vcpu_index, v= oid *userdata) g_mutex_lock(&imtx); addr =3D ((struct InsnData *) userdata)->addr; =20 + if (tracefile) { + g_mutex_lock(&fmtx); + g_autoptr(GString) rep =3D g_string_new(""); + g_string_append_printf(rep, "I: 0x%" PRIx64, addr); + fprintf(tracefile, "%s\n", rep->str); + g_mutex_unlock(&fmtx); + } + if (access_cache(icache, addr) =3D=3D MISS) { struct InsnData *insn =3D get_or_create(imiss_ht, userdata, addr); insn->misses++; imisses++; } + imem_accesses++; g_mutex_unlock(&imtx); } @@ -352,6 +373,15 @@ static void plugin_exit() =20 g_mutex_unlock(&dmtx); g_mutex_unlock(&imtx); + + if (tracefile) { + fclose(tracefile); + } +} + +static bool bad_cache_params(int blksize, int assoc, int cachesize) +{ + return (cachesize % blksize) !=3D 0 || (cachesize % (blksize * assoc) = !=3D 0); } =20 QEMU_PLUGIN_EXPORT @@ -377,14 +407,48 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qe= mu_info_t *info, =20 for (i =3D 0; i < argc; i++) { char *opt =3D argv[i]; - if (g_str_has_prefix(opt, "limit=3D")) { + if (g_str_has_prefix(opt, "I=3D")) { + gchar **toks =3D g_strsplit(opt + 2, " ", -1); + if (g_strv_length(toks) !=3D 3) { + fprintf(stderr, "option parsing failed: %s\n", opt); + return -1; + } + icachesize =3D g_ascii_strtoull(toks[0], NULL, 10); + iassoc =3D g_ascii_strtoull(toks[1], NULL, 10); + iblksize =3D g_ascii_strtoull(toks[2], NULL, 10); + } else if (g_str_has_prefix(opt, "D=3D")) { + gchar **toks =3D g_strsplit(opt + 2, " ", -1); + if (g_strv_length(toks) !=3D 3) { + fprintf(stderr, "option parsing failed: %s\n", opt); + return -1; + } + dcachesize =3D g_ascii_strtoull(toks[0], NULL, 10); + dassoc =3D g_ascii_strtoull(toks[1], NULL, 10); + dblksize =3D g_ascii_strtoull(toks[2], NULL, 10); + } else if (g_str_has_prefix(opt, "limit=3D")) { limit =3D g_ascii_strtoull(opt + 6, NULL, 10); + } else if (g_str_has_prefix(opt, "tracefile=3D")) { + char *file_name =3D opt + 10; + tracefile =3D fopen(file_name, "w"); + if (!tracefile) { + fprintf(stderr, "could not open: %s for writing\n", file_n= ame); + } } else { fprintf(stderr, "option parsing failed: %s\n", opt); return -1; } } =20 + if (bad_cache_params(iblksize, iassoc, icachesize)) { + fprintf(stderr, "icache cannot be constructed from given parameter= s\n"); + return -1; + } + + if (bad_cache_params(dblksize, dassoc, dcachesize)) { + fprintf(stderr, "dcache cannot be constructed from given parameter= s\n"); + return -1; + } + dcache =3D cache_init(dblksize, dassoc, dcachesize); icache =3D cache_init(iblksize, iassoc, icachesize); =20 --=20 2.25.1