From nobody Thu Apr 25 19:32:03 2024 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1624453172; cv=none; d=zohomail.com; s=zohoarc; b=PSMB5ApAVySWt4sKSArXuMYSsnWIJjYjFo0KhHXfdtpwzjO7ayEg42+HMvDx3RK2GXDvpaV6+hIPvTPbUC6IgFUVuwbNCggRv/6Cab91LUXF7+wfUsg0iYlq2aMrjXp0abWl+Igs52T2gZArOspHymYFAMNzrZG0ngNHSrHKgVw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624453172; 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=xyNCvfNYrTkVXgGVdh0npRo6e/cK40KZyM6VdpIuUzg=; b=VFMffw+1mTb9sb+sVXvK/LYO8aQMpQvby6WEn6ERTfKYGFxy0g+Qwf4ew8/2eRt7EjWhnjVvjK1NOYys6iarwv6PRM7f1E+c8LqUUkpZ+eDl2nwKS0b6sCQxBwHu0EMUYLvlBZ8WjM49hzW3fGCSfwmgV/1A1vxEqYH1ixpArOI= 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+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1624453172487309.85499285408866; Wed, 23 Jun 2021 05:59:32 -0700 (PDT) Received: from localhost ([::1]:47096 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lw2Ta-00015A-76 for importer@patchew.org; Wed, 23 Jun 2021 08:59:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54514) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lw2Pq-00035b-Bi for qemu-devel@nongnu.org; Wed, 23 Jun 2021 08:55:38 -0400 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]:33524) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lw2Po-0007qK-52 for qemu-devel@nongnu.org; Wed, 23 Jun 2021 08:55:38 -0400 Received: by mail-wr1-x436.google.com with SMTP id d11so2599352wrm.0 for ; Wed, 23 Jun 2021 05:55:35 -0700 (PDT) Received: from localhost.localdomain ([102.41.161.240]) by smtp.gmail.com with ESMTPSA id c12sm3108461wrr.90.2021.06.23.05.55.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Jun 2021 05:55:34 -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=xyNCvfNYrTkVXgGVdh0npRo6e/cK40KZyM6VdpIuUzg=; b=e5ALzj/Fdv1PsBXDC4G+E23Mrhb2YeZtuhFSSVojLPUJp3BLwGrcw5UMi7ZkHcoB/M O6jqPRo6R4GWUqhteVY5lCkLt5GJ7FdshBRpaEyTsg+WfTdYC9r2hSvEtueQfe+wImnR 0x6QxwEuj7+Nk0IECtsOI2G9SIP2hUG+dzz1/K137Va7MHMgUTZW+IdF1Pg7CTsSGyE2 B8/R39CMLrBL5iQVy3Gi9UGsN4JqnHUZ2+jEvXJ49QZsg/kbpVWCIOiyjRj045eyPySG ZCfDwB45GaDaQGED/fdtpAF6DV7FeF1bONLKBFDOgHVplse3yJHstljq7gtrhjo0lcu6 3CXg== 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=xyNCvfNYrTkVXgGVdh0npRo6e/cK40KZyM6VdpIuUzg=; b=Xrk7v9bLo9MiopkSuVq7KQ+f4/evGl19lgyvzZCWJmwQaGhVHOKHtaGRH9W8q/qHUm dgjesR/FwhtvtNPGB0kFBD2pVXnnw1UgpNjSEghgKVfjLqpeA2Ee41tNfxcxj7rK1mfP vcMAJ0GeUK1QF2RTYMgC8eRwC5fjJo2shp/k4m96rxu/JjmrhcAL4/Kw6v3wJWSY+y7n PuBKVjceyl/sdAKLXXBbKpLP64fXJCQN8haQ9kMNCSbR6vXTcKI94Sz4dShPFuP4t+I3 GNATds4WALq1w+Iect/I0XD82OyseOaWGIO5sgqYnD9sdYs7Ij7LBF++B3W/R82ldF/l yKlA== X-Gm-Message-State: AOAM530Jb2bC7MirCUtnLkiqen7BKF7PjiuIhos+3XJHXIXXPD5EJZPZ xYhIIx7nEuLWI/hZqONjApWiqwWHrmIRuA== X-Google-Smtp-Source: ABdhPJy06A7yKKwWrDAv60Ls+WPkhpXUk5Jr2wpm/JRiTw9dq9j3/OeHMPHPNFBxKwt9LvAjCDojUQ== X-Received: by 2002:a5d:4a81:: with SMTP id o1mr11635897wrq.1.1624452934545; Wed, 23 Jun 2021 05:55:34 -0700 (PDT) From: Mahmoud Mandour To: qemu-devel@nongnu.org Subject: [PATCH v4 1/5] plugins: Added a new cache modelling plugin Date: Wed, 23 Jun 2021 14:54:53 +0200 Message-Id: <20210623125458.450462-2-ma.mandourr@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210623125458.450462-1-ma.mandourr@gmail.com> References: <20210623125458.450462-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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::436; envelope-from=ma.mandourr@gmail.com; helo=mail-wr1-x436.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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @gmail.com) Content-Type: text/plain; charset="utf-8" Added a cache modelling plugin that uses a static configuration used in many of the commercial microprocessors and uses random eviction policy. The purpose of the plugin is to identify the most cache-thrashing instructions for both instruction cache and data cache. Signed-off-by: Mahmoud Mandour --- contrib/plugins/Makefile | 1 + contrib/plugins/cache.c | 419 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 420 insertions(+) create mode 100644 contrib/plugins/cache.c diff --git a/contrib/plugins/Makefile b/contrib/plugins/Makefile index b9d7935e5e..2237b47f8b 100644 --- a/contrib/plugins/Makefile +++ b/contrib/plugins/Makefile @@ -18,6 +18,7 @@ NAMES +=3D hotpages NAMES +=3D howvec NAMES +=3D lockstep NAMES +=3D hwprofile +NAMES +=3D cache =20 SONAMES :=3D $(addsuffix .so,$(addprefix lib,$(NAMES))) =20 diff --git a/contrib/plugins/cache.c b/contrib/plugins/cache.c new file mode 100644 index 0000000000..e9955cdc3a --- /dev/null +++ b/contrib/plugins/cache.c @@ -0,0 +1,419 @@ +/* + * Copyright (C) 2021, Mahmoud Mandour + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include +#include +#include + +#include + +QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; + +static enum qemu_plugin_mem_rw rw =3D QEMU_PLUGIN_MEM_RW; + +static GHashTable *miss_ht; + +static GMutex mtx; +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; + +/* + * A CacheSet is a set of cache blocks. A memory block that maps to a set = can be + * put in any of the blocks inside the set. The number of block per set is + * called the associativity (assoc). + * + * Each block contains the the stored tag and a valid bit. Since this is n= ot + * a functional simulator, the data itself is not stored. We only identify + * whether a block is in the cache or not by searching for its tag. + * + * In order to search for memory data in the cache, the set identifier and= tag + * are extracted from the address and the set is probed to see whether a t= ag + * match occur. + * + * An address is logically divided into three portions: The block offset, + * the set number, and the tag. + * + * The set number is used to identify the set in which the block may exist. + * The tag is compared against all the tags of a set to search for a match= . If a + * match is found, then the access is a hit. + */ + +typedef struct { + uint64_t tag; + bool valid; +} CacheBlock; + +typedef struct { + CacheBlock *blocks; +} CacheSet; + +typedef struct { + CacheSet *sets; + int num_sets; + int cachesize; + int assoc; + int blksize_shift; + uint64_t set_mask; + uint64_t tag_mask; +} Cache; + +typedef struct { + char *disas_str; + const char *symbol; + uint64_t addr; + uint64_t dmisses; + uint64_t imisses; +} InsnData; + +Cache *dcache, *icache; + +static int pow_of_two(int num) +{ + g_assert((num & (num - 1)) =3D=3D 0); + int ret =3D 0; + while (num /=3D 2) { + ret++; + } + return ret; +} + +static inline uint64_t extract_tag(Cache *cache, uint64_t addr) +{ + return addr & cache->tag_mask; +} + +static inline uint64_t extract_set(Cache *cache, uint64_t addr) +{ + return (addr & cache->set_mask) >> cache->blksize_shift; +} + +static Cache *cache_init(int blksize, int assoc, int cachesize) +{ + Cache *cache; + int i; + uint64_t blk_mask; + + cache =3D g_new(Cache, 1); + cache->assoc =3D assoc; + cache->cachesize =3D cachesize; + cache->num_sets =3D cachesize / (blksize * assoc); + cache->sets =3D g_new(CacheSet, cache->num_sets); + cache->blksize_shift =3D pow_of_two(blksize); + + for (i =3D 0; i < cache->num_sets; i++) { + cache->sets[i].blocks =3D g_new0(CacheBlock, assoc); + } + + blk_mask =3D blksize - 1; + cache->set_mask =3D ((cache->num_sets - 1) << cache->blksize_shift); + cache->tag_mask =3D ~(cache->set_mask | blk_mask); + return cache; +} + +static int get_invalid_block(Cache *cache, uint64_t set) +{ + int i; + + for (i =3D 0; i < cache->assoc; i++) { + if (!cache->sets[set].blocks[i].valid) { + return i; + } + } + + return -1; +} + +static int get_replaced_block(Cache *cache) +{ + return g_rand_int_range(rng, 0, cache->assoc); +} + +static bool in_cache(Cache *cache, uint64_t addr) +{ + int i; + uint64_t tag, set; + + tag =3D extract_tag(cache, addr); + set =3D extract_set(cache, addr); + + for (i =3D 0; i < cache->assoc; i++) { + if (cache->sets[set].blocks[i].tag =3D=3D tag && + cache->sets[set].blocks[i].valid) { + return true; + } + } + + return false; +} + +/** + * access_cache(): Simulate a cache access + * @cache: The cache under simulation + * @addr: The address of the requested memory location + * + * Returns true if the requsted data is hit in the cache and false when mi= ssed. + * The cache is updated on miss for the next access. + */ +static bool access_cache(Cache *cache, uint64_t addr) +{ + uint64_t tag, set; + int replaced_blk; + + if (in_cache(cache, addr)) { + return true; + } + + tag =3D extract_tag(cache, addr); + set =3D extract_set(cache, addr); + + replaced_blk =3D get_invalid_block(cache, set); + + if (replaced_blk =3D=3D -1) { + replaced_blk =3D get_replaced_block(cache); + } + + cache->sets[set].blocks[replaced_blk].tag =3D tag; + cache->sets[set].blocks[replaced_blk].valid =3D true; + + return false; +} + +static void vcpu_mem_access(unsigned int vcpu_index, qemu_plugin_meminfo_t= info, + uint64_t vaddr, void *userdata) +{ + uint64_t effective_addr; + struct qemu_plugin_hwaddr *hwaddr; + InsnData *insn; + + g_mutex_lock(&mtx); + hwaddr =3D qemu_plugin_get_hwaddr(info, vaddr); + if (hwaddr && qemu_plugin_hwaddr_is_io(hwaddr)) { + g_mutex_unlock(&mtx); + return; + } + + effective_addr =3D hwaddr ? qemu_plugin_hwaddr_phys_addr(hwaddr) : vad= dr; + + if (!access_cache(dcache, effective_addr)) { + insn =3D (InsnData *) userdata; + insn->dmisses++; + dmisses++; + } + dmem_accesses++; + g_mutex_unlock(&mtx); +} + +static void vcpu_insn_exec(unsigned int vcpu_index, void *userdata) +{ + uint64_t insn_addr; + InsnData *insn; + + g_mutex_lock(&mtx); + insn_addr =3D ((InsnData *) userdata)->addr; + + if (!access_cache(icache, insn_addr)) { + insn =3D (InsnData *) userdata; + insn->imisses++; + imisses++; + } + imem_accesses++; + g_mutex_unlock(&mtx); +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + size_t n_insns; + size_t i; + InsnData *data; + + n_insns =3D qemu_plugin_tb_n_insns(tb); + for (i =3D 0; i < n_insns; i++) { + struct qemu_plugin_insn *insn =3D qemu_plugin_tb_get_insn(tb, i); + uint64_t effective_addr; + + if (sys) { + effective_addr =3D (uint64_t) qemu_plugin_insn_haddr(insn); + } else { + effective_addr =3D (uint64_t) qemu_plugin_insn_vaddr(insn); + } + + /* + * Instructions might get translated multiple times, we do not cre= ate + * new entries for those instructions. Instead, we fetch the same + * entry from the hash table and register it for the callback agai= n. + */ + g_mutex_lock(&mtx); + data =3D g_hash_table_lookup(miss_ht, GUINT_TO_POINTER(effective_a= ddr)); + if (data =3D=3D NULL) { + data =3D g_new0(InsnData, 1); + data->disas_str =3D qemu_plugin_insn_disas(insn); + data->symbol =3D qemu_plugin_insn_symbol(insn); + data->addr =3D effective_addr; + g_hash_table_insert(miss_ht, GUINT_TO_POINTER(effective_addr), + (gpointer) data); + } + g_mutex_unlock(&mtx); + + qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem_access, + QEMU_PLUGIN_CB_NO_REGS, + rw, data); + + qemu_plugin_register_vcpu_insn_exec_cb(insn, vcpu_insn_exec, + QEMU_PLUGIN_CB_NO_REGS, dat= a); + } +} + +static void insn_free(gpointer data) +{ + InsnData *insn =3D (InsnData *) data; + g_free(insn->disas_str); + g_free(insn); +} + +static void cache_free(Cache *cache) +{ + for (int i =3D 0; i < cache->num_sets; i++) { + g_free(cache->sets[i].blocks); + } + + g_free(cache->sets); + g_free(cache); +} + +static int dcmp(gconstpointer a, gconstpointer b) +{ + InsnData *insn_a =3D (InsnData *) a; + InsnData *insn_b =3D (InsnData *) b; + + return insn_a->dmisses < insn_b->dmisses ? 1 : -1; +} + +static int icmp(gconstpointer a, gconstpointer b) +{ + InsnData *insn_a =3D (InsnData *) a; + InsnData *insn_b =3D (InsnData *) b; + + return insn_a->imisses < insn_b->imisses ? 1 : -1; +} + +static void log_stats() +{ + g_autoptr(GString) rep =3D 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); + + qemu_plugin_outs(rep->str); +} + +static void log_top_insns() +{ + int i; + GList *curr, *miss_insns; + InsnData *insn; + + miss_insns =3D g_hash_table_get_values(miss_ht); + miss_insns =3D g_list_sort(miss_insns, dcmp); + g_autoptr(GString) rep =3D g_string_new(""); + g_string_append_printf(rep, "%s", "address, data misses, instruction\n= "); + + for (curr =3D miss_insns, i =3D 0; curr && i < limit; i++, curr =3D cu= rr->next) { + insn =3D (InsnData *) curr->data; + g_string_append_printf(rep, "0x%" PRIx64, insn->addr); + if (insn->symbol) { + g_string_append_printf(rep, " (%s)", insn->symbol); + } + g_string_append_printf(rep, ", %ld, %s\n", insn->dmisses, + insn->disas_str); + } + + miss_insns =3D g_list_sort(miss_insns, icmp); + g_string_append_printf(rep, "%s", "\naddress, fetch misses, instructio= n\n"); + + for (curr =3D miss_insns, i =3D 0; curr && i < limit; i++, curr =3D cu= rr->next) { + insn =3D (InsnData *) curr->data; + g_string_append_printf(rep, "0x%" PRIx64, insn->addr); + if (insn->symbol) { + g_string_append_printf(rep, " (%s)", insn->symbol); + } + g_string_append_printf(rep, ", %ld, %s\n", insn->imisses, + insn->disas_str); + } + + qemu_plugin_outs(rep->str); + g_list_free(miss_insns); +} + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + log_stats(); + log_top_insns(); + + cache_free(dcache); + cache_free(icache); + + g_hash_table_destroy(miss_ht); +} + +QEMU_PLUGIN_EXPORT +int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, + int argc, char **argv) +{ + int i; + int iassoc, iblksize, icachesize; + int dassoc, dblksize, dcachesize; + + limit =3D 32; + sys =3D info->system_emulation; + + dassoc =3D 8; + dblksize =3D 64; + dcachesize =3D dblksize * dassoc * 32; + + iassoc =3D 8; + iblksize =3D 64; + icachesize =3D iblksize * iassoc * 32; + + + for (i =3D 0; i < argc; i++) { + char *opt =3D argv[i]; + if (g_str_has_prefix(opt, "limit=3D")) { + limit =3D g_ascii_strtoll(opt + 6, NULL, 10); + } else { + fprintf(stderr, "option parsing failed: %s\n", opt); + return -1; + } + } + + dcache =3D cache_init(dblksize, dassoc, dcachesize); + icache =3D cache_init(iblksize, iassoc, icachesize); + + rng =3D g_rand_new(); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + + miss_ht =3D g_hash_table_new_full(NULL, g_direct_equal, NULL, insn_fre= e); + + return 0; +} --=20 2.25.1