From nobody Fri May 3 03:22:59 2024 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=1622301930; cv=none; d=zohomail.com; s=zohoarc; b=PCOedrU071IErlC/7UqKbpINAJ01M9XZKV7naIZ3N7zkwLiKYGvvkYfCVWt4yLg+E6/G7idIfyiYMpcgt1BHrVQML3a06aNufp8hdJUlHLQMwwSBDjMCbFM+IRY/uxialRx+q1c1yzYn9MILJPxz6snfbhc1PwmxGRXOIcTts40= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622301930; 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=0OrbkiY834csv3Yxp5ryuGtCuPFWhDkCpFIaiXtPwQE=; b=KAz355ltwfje+bYMFkuUkOMExc95/SIXNWkAgbtYPSWFMvjz7oCTDOsCZHw3As6JKOGAw5GXIawkhMkKfNdigiF7FU+t+FGJ7hFs3Eg3ZUSVxXsZ4phRGUnlgEOa5uPT69pPL2P63RqVffGPvDRJCmfy+UJ4oQK89zcntpa5nqw= 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 162230193014135.393170744290956; Sat, 29 May 2021 08:25:30 -0700 (PDT) Received: from localhost ([::1]:52944 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ln0q8-0006oU-UN for importer2@patchew.org; Sat, 29 May 2021 11:25:28 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60544) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ln0n9-0001M3-GQ for qemu-devel@nongnu.org; Sat, 29 May 2021 11:22:24 -0400 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]:41594) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ln0n5-0006eX-Bn for qemu-devel@nongnu.org; Sat, 29 May 2021 11:22:23 -0400 Received: by mail-wr1-x42e.google.com with SMTP id c3so6166021wrp.8 for ; Sat, 29 May 2021 08:22:18 -0700 (PDT) Received: from localhost.localdomain ([102.47.172.201]) by smtp.gmail.com with ESMTPSA id s7sm17019164wmh.35.2021.05.29.08.22.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 May 2021 08:22:17 -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=0OrbkiY834csv3Yxp5ryuGtCuPFWhDkCpFIaiXtPwQE=; b=HJt9Hxh57AcNLJCLo24k+jbbjRDIhDQNGXN2NnBWOq3HiLw2pIK+k1TR4jni977yX0 dbM9PxxGk96J/BMKuuVYGFzI8tlKuws/+LZwhkJDV1jcJWlk8/NkohTsQgVI3v4Gsx9D i/V39cvxBfak6iXMQQKWL9+evDK1/kqJSDSoTX4TneuLo56yz6dpn0S2d56umbOFxzVs orMea5B9iTe2h5fLt4RbhozUMq5UU23UvAeB02etb+2i5l7ODLkRL+wbCZ+cV3DeJDQU u3SKMbr4YkbsNrbUJpXeN3qhAtO5fxNBgnEIHl2UXA1nA+jPgi8n5zP+JSSkC71OstKb 1maQ== 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=0OrbkiY834csv3Yxp5ryuGtCuPFWhDkCpFIaiXtPwQE=; b=LMWD+nD6HnL2ZAHXOtE2YPB2xK3oZLALKzCIhwsFnKqNchNckLRzb7Tb0/plZ65gfM C/3rtHStsuNHQ6ZSYlm230OmCdqTv8rSEA1Iry/4/4DJYNujinvlOYry9saIf8I4Fzo/ ZmG6KsI0NKmpXxWb+I83HHI9uHIeeBrc9yJ3T8DqEYR9bOuDleUTKiutiHn5+dA+qgZL IRmhoZYsgEAWeLZYu2rppUiE6SvkPs6S0e6UItZoWK1owf1XAES5XuznWsl4lOOX7Jde MISPMXDbiWWOGrieH3PX2ZpYAcUGPa/HaLkv1PweDegExpivhVYCfGwGhkC+Okuplvox 4+QQ== X-Gm-Message-State: AOAM530tclCjNCQ4f1NNyTGxT7OsPfjj31J1FkWiSB80Gg4xwIfj2OGT GzyQOYVrKgVFOGSa6UY4a5HfgfeZmk4iWw== X-Google-Smtp-Source: ABdhPJwtyx3yPYuqR20eue13mZwvGeHp5ZePrBNHW+mS/eayAfV9TPLRLcRI/0FsQu+SZmPcZ6Zgxw== X-Received: by 2002:adf:efca:: with SMTP id i10mr203903wrp.139.1622301737751; Sat, 29 May 2021 08:22:17 -0700 (PDT) From: Mahmoud Mandour To: qemu-devel@nongnu.org Subject: [RFC PATCH 1/3] plugins: Added a new cache modelling plugin Date: Sat, 29 May 2021 17:22:01 +0200 Message-Id: <20210529152203.40358-2-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" Added a cache modelling plugin that uses a static configuration used in many of the commercial microprocessors and uses random eviction policy. Signed-off-by: Mahmoud Mandour --- contrib/plugins/Makefile | 1 + contrib/plugins/cache.c | 398 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 399 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..f8c15ebed2 --- /dev/null +++ b/contrib/plugins/cache.c @@ -0,0 +1,398 @@ +/* + * 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 +#include +#include +#include +#include + +#include + +QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; + +static GRand *rng; +static GHashTable *dmiss_ht; +static GHashTable *imiss_ht; + +static GMutex dmtx, imtx; + +static int limit; +static bool sys; + +static uint64_t dmem_accesses; +static uint64_t dmisses; + +static uint64_t imem_accesses; +static uint64_t imisses; + +static enum qemu_plugin_mem_rw rw =3D QEMU_PLUGIN_MEM_RW; + +enum AccessResult { + HIT =3D 0, + MISS =3D 1 +}; + +struct InsnData { + char *disas_str; + uint64_t addr; + uint64_t misses; +}; + +struct CacheBlock { + uint64_t tag; + bool valid; +}; + +struct CacheSet { + struct CacheBlock *blocks; +}; + +struct Cache { + struct CacheSet *sets; + int num_sets; + + int cachesize; + int blksize; + int assoc; + + uint64_t blk_mask; + uint64_t set_mask; + uint64_t tag_mask; +}; + +struct 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(struct Cache *cache, uint64_t addr) +{ + return (addr & cache->tag_mask) >> + (pow_of_two(cache->num_sets) + pow_of_two(cache->blksize)); +} + +static inline uint64_t extract_set(struct Cache *cache, uint64_t addr) +{ + return (addr & cache->set_mask) >> (pow_of_two(cache->blksize)); +} + +static struct Cache *cache_init(int blksize, int assoc, int cachesize) +{ + struct Cache *cache; + int i; + + cache =3D g_new(struct Cache, 1); + cache->blksize =3D blksize; + cache->assoc =3D assoc; + cache->cachesize =3D cachesize; + cache->num_sets =3D cachesize / (blksize * assoc); + cache->sets =3D g_new(struct CacheSet, cache->num_sets); + + for (i =3D 0; i < cache->num_sets; i++) { + cache->sets[i].blocks =3D g_new0(struct CacheBlock, assoc); + } + + cache->blk_mask =3D blksize - 1; + cache->set_mask =3D ((cache->num_sets - 1) << (pow_of_two(cache->blksi= ze))); + cache->tag_mask =3D ~(cache->set_mask | cache->blk_mask); + + return cache; +} + +static int get_invalid_block(struct Cache *cache, uint64_t set) +{ + int i; + + for (i =3D 0; i < cache->assoc; i++) { + if (!cache->sets[set].blocks[i].valid) { + /* conflict miss */ + return i; + } + } + + /* compulsary miss */ + return -1; +} + +static int get_replaced_block(struct Cache *cache) +{ + return g_rand_int_range(rng, 0, cache->assoc); +} + +static bool in_cache(struct 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; +} + +static enum AccessResult access_cache(struct Cache *cache, uint64_t addr) +{ + uint64_t tag, set; + int replaced_blk; + + if (in_cache(cache, addr)) { + return HIT; + } + + 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 MISS; +} + +struct InsnData *get_or_create(GHashTable *ht, struct InsnData *insn_data, + uint64_t addr) +{ + struct InsnData *insn =3D g_hash_table_lookup(ht, GUINT_TO_POINTER(add= r)); + if (!insn) { + g_hash_table_insert(ht, GUINT_TO_POINTER(addr), (gpointer) insn_da= ta); + insn =3D insn_data; + } + + return insn; +} + +static void vcpu_mem_access(unsigned int cpu_index, qemu_plugin_meminfo_t = info, + uint64_t vaddr, void *userdata) +{ + uint64_t insn_addr; + uint64_t effective_addr; + struct qemu_plugin_hwaddr *hwaddr; + + g_mutex_lock(&dmtx); + hwaddr =3D qemu_plugin_get_hwaddr(info, vaddr); + if (hwaddr && qemu_plugin_hwaddr_is_io(hwaddr)) { + g_free(userdata); + return; + } + + insn_addr =3D ((struct InsnData *) userdata)->addr; + effective_addr =3D hwaddr ? qemu_plugin_hwaddr_phys_addr(hwaddr) : vad= dr; + + if (access_cache(dcache, effective_addr) =3D=3D MISS) { + struct InsnData *insn =3D get_or_create(dmiss_ht, userdata, insn_a= ddr); + insn->misses++; + dmisses++; + } + dmem_accesses++; + g_mutex_unlock(&dmtx); +} + +static void vcpu_insn_exec(unsigned int vcpu_index, void *userdata) +{ + uint64_t addr; + + g_mutex_lock(&imtx); + addr =3D ((struct InsnData *) userdata)->addr; + + 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); +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + size_t n_insns; + size_t i; + + 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); + } + + struct InsnData *ddata =3D g_new(struct InsnData, 1); + struct InsnData *idata =3D g_new(struct InsnData, 1); + + ddata->disas_str =3D qemu_plugin_insn_disas(insn); + ddata->misses =3D 0; + ddata->addr =3D effective_addr; + + idata->disas_str =3D g_strdup(ddata->disas_str); + idata->misses =3D 0; + idata->addr =3D effective_addr; + + qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem_access, + QEMU_PLUGIN_CB_NO_REGS, + rw, ddata); + + qemu_plugin_register_vcpu_insn_exec_cb(insn, vcpu_insn_exec, + QEMU_PLUGIN_CB_NO_REGS, ida= ta); + } +} + +static void print_entry(gpointer data) +{ + struct InsnData *insn =3D (struct InsnData *) data; + g_autoptr(GString) xx =3D g_string_new(""); + g_string_append_printf(xx, "0x%" PRIx64 ": %s - misses: %lu\n", + insn->addr, insn->disas_str, insn->misses); + qemu_plugin_outs(xx->str); +} + +static void free_insn(gpointer data) +{ + struct InsnData *insn =3D (struct InsnData *) data; + g_free(insn->disas_str); + g_free(insn); +} + +static void free_cache(struct Cache *cache) +{ + for (int i =3D 0; i < cache->num_sets; i++) { + g_free(cache->sets[i].blocks); + } + + g_free(cache->sets); +} + +static int cmp(gconstpointer a, gconstpointer b) +{ + struct InsnData *insn_a =3D (struct InsnData *) a; + struct InsnData *insn_b =3D (struct InsnData *) b; + + return insn_a->misses < insn_b->misses ? 1 : -1; +} + +static void print_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 / dmem_accesses) * 100.0); + + g_string_append_printf(rep, + "Instruction accesses: %lu, Misses: %lu\nMiss rate: %lf%%\n\n", + imem_accesses, + imisses, + ((double)imisses / imem_accesses) * 100.0); + + qemu_plugin_outs(rep->str); +} + +static void plugin_exit() +{ + GList *curr; + int i; + + g_mutex_lock(&imtx); + g_mutex_lock(&dmtx); + GList *dmiss_insns =3D g_hash_table_get_values(dmiss_ht); + GList *imiss_insns =3D g_hash_table_get_values(imiss_ht); + dmiss_insns =3D g_list_sort(dmiss_insns, cmp); + imiss_insns =3D g_list_sort(imiss_insns, cmp); + + print_stats(); + + qemu_plugin_outs("Most data-missing instructions\n"); + for (curr =3D dmiss_insns, i =3D 0; curr && i < limit; i++, curr =3D c= urr->next) { + print_entry(curr->data); + } + + qemu_plugin_outs("\nMost fetch-missing instructions\n"); + for (curr =3D imiss_insns, i =3D 0; curr && i < limit; i++, curr =3D c= urr->next) { + print_entry(curr->data); + } + + free_cache(dcache); + free_cache(icache); + + g_list_free(dmiss_insns); + g_list_free(imiss_insns); + + g_hash_table_destroy(dmiss_ht); + g_hash_table_destroy(imiss_ht); + + g_mutex_unlock(&dmtx); + g_mutex_unlock(&imtx); +} + +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; + + rng =3D g_rand_new(); + + for (i =3D 0; i < argc; i++) { + char *opt =3D argv[i]; + if (g_str_has_prefix(opt, "limit=3D")) { + limit =3D g_ascii_strtoull(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); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + + dmiss_ht =3D g_hash_table_new_full(NULL, g_direct_equal, NULL, free_in= sn); + imiss_ht =3D g_hash_table_new_full(NULL, g_direct_equal, NULL, free_in= sn); + + return 0; +} --=20 2.25.1 From nobody Fri May 3 03:22:59 2024 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 From nobody Fri May 3 03:22:59 2024 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=1622301833; cv=none; d=zohomail.com; s=zohoarc; b=dHBgPurIODFObOK/ERGF4hYOy8jrZW6C8sXmAGV89uA2rjKv0lBUatROGtJjSHurqxW74A9z0QnVYsi4JRp9PlcTyQjzHX8FHT754mP7NvycjbuY8KjmUrMhBfDiLtmTiywxuXZiS6rfAH5JVHmB5y/cgrf4wbwFfpypa49tn6w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1622301833; 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=NglVq6XPSNfI2OlftVxELyprFxY+6DW+HIvyv6La3O0=; b=IXST/GzbqZnKQEchBQjz9BQbrTnM9ExNmn9ujZXYD+6HTHjvAiFIkDn+BXZuJI0uRrsjhPGGDnHVLl/PSTVsfWO5MWA5pWVLjq4obaZc35iN9/qRKsB+eTvMHV/sqVHsy+2yJDIGyhPJ1/QI8ft2Hrup7GlOhocSnCEZDirwiWE= 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 1622301833043462.54601311513386; Sat, 29 May 2021 08:23:53 -0700 (PDT) Received: from localhost ([::1]:47972 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ln0oa-0003Uj-0n for importer2@patchew.org; Sat, 29 May 2021 11:23:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60592) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ln0nE-0001OO-Db for qemu-devel@nongnu.org; Sat, 29 May 2021 11:22:29 -0400 Received: from mail-wr1-x434.google.com ([2a00:1450:4864:20::434]:35821) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ln0n9-0006f0-76 for qemu-devel@nongnu.org; Sat, 29 May 2021 11:22:28 -0400 Received: by mail-wr1-x434.google.com with SMTP id m18so6197204wrv.2 for ; Sat, 29 May 2021 08:22:22 -0700 (PDT) Received: from localhost.localdomain ([102.47.172.201]) by smtp.gmail.com with ESMTPSA id s7sm17019164wmh.35.2021.05.29.08.22.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 May 2021 08:22:20 -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=NglVq6XPSNfI2OlftVxELyprFxY+6DW+HIvyv6La3O0=; b=IvUxECjeE/ADHD8FA+rz/gtmfPdXYHSm28p0bt0qSjrG/tM3nnDmcYBeZ2T8HlNxQV jO1tyZQqlxYxN/KbTTeRz8pXXOVtFtbg8eDVSj1FtttxRpMLPrZ9U64us2dMBnAJpZuC WhjRJxpKykZU/eWbVPdRODT/RBy4+gV2Kq81Thg+qf6uVuYEtAuN39WVGLUT56p+RtLX cPtgit2eB6NlhG6Ft14AIYnvYIEumPia4p0M9TqXeb0zwi+TLYxBmNPbR9N/rqv7asVH 7ZXtgSwCJnsdKR7PXaTxN5m6gkNUintbS00Tik6T8OFFSMUpN7KuDMI9DEAA82ZV/o+y PbOw== 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=NglVq6XPSNfI2OlftVxELyprFxY+6DW+HIvyv6La3O0=; b=VCECnvoF/7E4PYrOlFupm21yZNNhJSuRKY8CPMA8yhZ3dDkALV2ZHBNbkSmG0em5ix 78b/xDgnHRROhI3g5Aa27NrwHNeIkjGCtVEIn0NwUVbzP9T3N/R/5dLzDNL4xVjttATV OFRjVYZcWioHf7yqkOBFYyOgZiJwKVoQKOZ3ZGfk0ifvmHB5VuZodzf7JmYQUVreylkI UoP+1VJUbFkD8p4xmvPMAIil0cmDTkgqRnMVDlk41OJTA9tg4MtOcZuI/VS5hSvktFvP WqbGFVUUY8izCAiLrN11iuTLMlZAVFnIoIMookxP1R1pSTCqZaYTCHo0kNixnErtw7pL +UDQ== X-Gm-Message-State: AOAM533W4rQTf7pFo99JUt2qVIQvZuLGngHoxcTt4A5hJT8x8tAm8fmW +vmI2XWwrCeKxiCiw5RcnBIlgISRSHz6HA== X-Google-Smtp-Source: ABdhPJxCaPZEu1nycBeJEyf3rERQEGgTcYt97jXFaeccqj/rDM1HvNVvFt2JMhid9lVi3Y84J5+qxA== X-Received: by 2002:a5d:6dce:: with SMTP id d14mr998056wrz.236.1622301740894; Sat, 29 May 2021 08:22:20 -0700 (PDT) From: Mahmoud Mandour To: qemu-devel@nongnu.org Subject: [RFC PATCH 3/3] plugins: cache: Added FIFO and LRU eviction policies. Date: Sat, 29 May 2021 17:22:03 +0200 Message-Id: <20210529152203.40358-4-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::434; envelope-from=ma.mandourr@gmail.com; helo=mail-wr1-x434.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" Now one of the three eviction policies can be chosen as an argument. On not specifying an argument, LRU is used by default. Signed-off-by: Mahmoud Mandour --- contrib/plugins/cache.c | 159 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 146 insertions(+), 13 deletions(-) diff --git a/contrib/plugins/cache.c b/contrib/plugins/cache.c index 7d1d185480..341cd64e41 100644 --- a/contrib/plugins/cache.c +++ b/contrib/plugins/cache.c @@ -18,6 +18,8 @@ =20 QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; =20 +static bool fifo, lru, rnd; + static GRand *rng; static GHashTable *dmiss_ht; static GHashTable *imiss_ht; @@ -55,6 +57,8 @@ struct CacheBlock { =20 struct CacheSet { struct CacheBlock *blocks; + uint16_t *priorities; + GQueue *evict_queue; }; =20 struct Cache { @@ -93,6 +97,84 @@ static inline uint64_t extract_set(struct Cache *cache, = uint64_t addr) return (addr & cache->set_mask) >> (pow_of_two(cache->blksize)); } =20 +static void lru_priorities_init(struct Cache *cache) +{ + int i, j; + + for (i =3D 0; i < cache->num_sets; i++) { + cache->sets[i].priorities =3D g_new(uint16_t, cache->assoc); + for (j =3D 0; j < cache->assoc; j++) { + cache->sets[i].priorities[j] =3D cache->assoc - j - 1; + } + } +} + +static void lru_update_on_miss(struct Cache *cache, + int set_idx, + int blk_idx) +{ + int i; + + for (i =3D 0; i < cache->assoc; i++) { + cache->sets[set_idx].priorities[i]++; + } + + cache->sets[set_idx].priorities[blk_idx] =3D 0; +} + +static void lru_update_on_hit(struct Cache *cache, + int set_idx, + int blk_idx) +{ + uint16_t blk_priority; + int i; + + blk_priority =3D cache->sets[set_idx].priorities[blk_idx]; + for (i =3D 0; i < cache->assoc; i++) { + if (cache->sets[set_idx].priorities[i] < blk_priority) { + cache->sets[set_idx].priorities[i]++; + } + } + cache->sets[set_idx].priorities[blk_idx] =3D 0; +} + +static int lru_get_lru_block(struct Cache *cache, int set_idx) +{ + int i; + + for (i =3D 0; i < cache->assoc; i++) { + if (cache->sets[set_idx].priorities[i] =3D=3D cache->assoc - 1) { + return i; + } + } + + g_assert_not_reached(); +} + +static void fifo_init(struct Cache *cache) +{ + int i; + + for (i =3D 0; i < cache->num_sets; i++) { + cache->sets[i].evict_queue =3D g_queue_new(); + } +} + +static int fifo_get_first_in_block(struct Cache *cache, int set) +{ + GQueue *q =3D cache->sets[set].evict_queue; + return GPOINTER_TO_INT(g_queue_pop_tail(q)); +} + +static void fifo_update_on_miss(struct Cache *cache, + int set, + int blk_idx) +{ + GQueue *q =3D cache->sets[set].evict_queue; + g_queue_push_head(q, GINT_TO_POINTER(blk_idx)); +} + + static struct Cache *cache_init(int blksize, int assoc, int cachesize) { struct Cache *cache; @@ -113,6 +195,12 @@ static struct Cache *cache_init(int blksize, int assoc= , int cachesize) cache->set_mask =3D ((cache->num_sets - 1) << (pow_of_two(cache->blksi= ze))); cache->tag_mask =3D ~(cache->set_mask | cache->blk_mask); =20 + if (lru) { + lru_priorities_init(cache); + } else if (fifo) { + fifo_init(cache); + } + return cache; } =20 @@ -131,12 +219,20 @@ static int get_invalid_block(struct Cache *cache, uin= t64_t set) return -1; } =20 -static int get_replaced_block(struct Cache *cache) +static int get_replaced_block(struct Cache *cache, int set) { - return g_rand_int_range(rng, 0, cache->assoc); + if (rnd) { + return g_rand_int_range(rng, 0, cache->assoc); + } else if (lru) { + return lru_get_lru_block(cache, set); + } else if (fifo) { + return fifo_get_first_in_block(cache, set); + } + + g_assert_not_reached(); } =20 -static bool in_cache(struct Cache *cache, uint64_t addr) +static int in_cache(struct Cache *cache, uint64_t addr) { int i; uint64_t tag, set; @@ -147,29 +243,39 @@ static bool in_cache(struct Cache *cache, uint64_t ad= dr) 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 i; } } =20 - return false; + return -1; } =20 static enum AccessResult access_cache(struct Cache *cache, uint64_t addr) { uint64_t tag, set; - int replaced_blk; - - if (in_cache(cache, addr)) { - return HIT; - } + int hit_blk, replaced_blk; =20 tag =3D extract_tag(cache, addr); set =3D extract_set(cache, addr); + hit_blk =3D in_cache(cache, addr); + + if (hit_blk !=3D -1) { + if (lru) { + lru_update_on_hit(cache, set, hit_blk); + } + return HIT; + } =20 replaced_blk =3D get_invalid_block(cache, set); =20 if (replaced_blk =3D=3D -1) { - replaced_blk =3D get_replaced_block(cache); + replaced_blk =3D get_replaced_block(cache, set); + } + + if (lru) { + lru_update_on_miss(cache, set, replaced_blk); + } else if (fifo) { + fifo_update_on_miss(cache, set, replaced_blk); } =20 cache->sets[set].blocks[replaced_blk].tag =3D tag; @@ -307,6 +413,11 @@ static void free_cache(struct Cache *cache) { for (int i =3D 0; i < cache->num_sets; i++) { g_free(cache->sets[i].blocks); + if (lru) { + g_free(cache->sets[i].priorities); + } else if (fifo) { + g_queue_free(cache->sets[i].evict_queue); + } } =20 g_free(cache->sets); @@ -403,8 +514,6 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qemu= _info_t *info, iblksize =3D 64; icachesize =3D iblksize * iassoc * 32; =20 - rng =3D g_rand_new(); - for (i =3D 0; i < argc; i++) { char *opt =3D argv[i]; if (g_str_has_prefix(opt, "I=3D")) { @@ -433,6 +542,22 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qem= u_info_t *info, if (!tracefile) { fprintf(stderr, "could not open: %s for writing\n", file_n= ame); } + } else if (g_str_has_prefix(opt, "evict=3D")) { + if (lru || rnd || fifo) { + fprintf(stderr, "eviction policy specified more than once\= n"); + return -1; + } + gchar *policy =3D opt + 6; + if (g_strcmp0(policy, "rand") =3D=3D 0) { + rnd =3D true; + } else if (g_strcmp0(policy, "lru") =3D=3D 0) { + lru =3D true; + } else if (g_strcmp0(policy, "fifo") =3D=3D 0) { + fifo =3D true; + } else { + fprintf(stderr, "invalid eviction policy: %s\n", opt); + return -1; + } } else { fprintf(stderr, "option parsing failed: %s\n", opt); return -1; @@ -449,6 +574,14 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qem= u_info_t *info, return -1; } =20 + if (!rnd && !lru && !fifo) { + lru =3D true; + } + + if (rnd) { + rng =3D g_rand_new(); + } + dcache =3D cache_init(dblksize, dassoc, dcachesize); icache =3D cache_init(iblksize, iassoc, icachesize); =20 --=20 2.25.1