All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: fam@euphon.net, berrange@redhat.com, f4bug@amsat.org,
	stefanha@redhat.com, crosa@redhat.com, pbonzini@redhat.com,
	"Mahmoud Mandour" <ma.mandourr@gmail.com>,
	"Alexandre Iooss" <erdnaxe@crans.org>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	aurelien@aurel32.net
Subject: [PATCH v1 37/39] plugins/cache: Added FIFO and LRU eviction policies
Date: Tue,  6 Jul 2021 15:58:15 +0100	[thread overview]
Message-ID: <20210706145817.24109-38-alex.bennee@linaro.org> (raw)
In-Reply-To: <20210706145817.24109-1-alex.bennee@linaro.org>

From: Mahmoud Mandour <ma.mandourr@gmail.com>

Implemented FIFO and LRU eviction policies. 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 <ma.mandourr@gmail.com>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <20210623125458.450462-4-ma.mandourr@gmail.com>
---
 contrib/plugins/cache.c | 203 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 190 insertions(+), 13 deletions(-)

diff --git a/contrib/plugins/cache.c b/contrib/plugins/cache.c
index b550ef31b0..bf0d2f6097 100644
--- a/contrib/plugins/cache.c
+++ b/contrib/plugins/cache.c
@@ -29,6 +29,14 @@ static uint64_t dmisses;
 static uint64_t imem_accesses;
 static uint64_t imisses;
 
+enum EvictionPolicy {
+    LRU,
+    FIFO,
+    RAND,
+};
+
+enum EvictionPolicy policy;
+
 /*
  * 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
@@ -48,6 +56,8 @@ static uint64_t imisses;
  * 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.
+ *
+ * The CacheSet also contains bookkeaping information about eviction details.
  */
 
 typedef struct {
@@ -57,6 +67,9 @@ typedef struct {
 
 typedef struct {
     CacheBlock *blocks;
+    uint64_t *lru_priorities;
+    uint64_t lru_gen_counter;
+    GQueue *fifo_queue;
 } CacheSet;
 
 typedef struct {
@@ -77,6 +90,12 @@ typedef struct {
     uint64_t imisses;
 } InsnData;
 
+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 pow_of_two(int num)
@@ -89,6 +108,103 @@ static int pow_of_two(int num)
     return ret;
 }
 
+/*
+ * LRU evection policy: For each set, a generation counter is maintained
+ * alongside a priority array.
+ *
+ * On each set access, the generation counter is incremented.
+ *
+ * On a cache hit: The hit-block is assigned the current generation counter,
+ * indicating that it is the most recently used block.
+ *
+ * On a cache miss: The block with the least priority is searched and replaced
+ * with the newly-cached block, of which the priority is set to the current
+ * generation number.
+ */
+
+static void lru_priorities_init(Cache *cache)
+{
+    int i;
+
+    for (i = 0; i < cache->num_sets; i++) {
+        cache->sets[i].lru_priorities = g_new0(uint64_t, cache->assoc);
+        cache->sets[i].lru_gen_counter = 0;
+    }
+}
+
+static void lru_update_blk(Cache *cache, int set_idx, int blk_idx)
+{
+    CacheSet *set = &cache->sets[set_idx];
+    set->lru_priorities[blk_idx] = cache->sets[set_idx].lru_gen_counter;
+    set->lru_gen_counter++;
+}
+
+static int lru_get_lru_block(Cache *cache, int set_idx)
+{
+    int i, min_idx, min_priority;
+
+    min_priority = cache->sets[set_idx].lru_priorities[0];
+    min_idx = 0;
+
+    for (i = 1; i < cache->assoc; i++) {
+        if (cache->sets[set_idx].lru_priorities[i] < min_priority) {
+            min_priority = cache->sets[set_idx].lru_priorities[i];
+            min_idx = i;
+        }
+    }
+    return min_idx;
+}
+
+static void lru_priorities_destroy(Cache *cache)
+{
+    int i;
+
+    for (i = 0; i < cache->num_sets; i++) {
+        g_free(cache->sets[i].lru_priorities);
+    }
+}
+
+/*
+ * FIFO eviction policy: a FIFO queue is maintained for each CacheSet that
+ * stores accesses to the cache.
+ *
+ * On a compulsory miss: The block index is enqueued to the fifo_queue to
+ * indicate that it's the latest cached block.
+ *
+ * On a conflict miss: The first-in block is removed from the cache and the new
+ * block is put in its place and enqueued to the FIFO queue.
+ */
+
+static void fifo_init(Cache *cache)
+{
+    int i;
+
+    for (i = 0; i < cache->num_sets; i++) {
+        cache->sets[i].fifo_queue = g_queue_new();
+    }
+}
+
+static int fifo_get_first_block(Cache *cache, int set)
+{
+    GQueue *q = cache->sets[set].fifo_queue;
+    return GPOINTER_TO_INT(g_queue_pop_tail(q));
+}
+
+static void fifo_update_on_miss(Cache *cache, int set, int blk_idx)
+{
+    GQueue *q = cache->sets[set].fifo_queue;
+    g_queue_push_head(q, GINT_TO_POINTER(blk_idx));
+}
+
+static void fifo_destroy(Cache *cache)
+{
+    int i;
+
+    for (i = 0; i < cache->assoc; i++) {
+        g_queue_free(cache->sets[i].fifo_queue);
+    }
+}
+
 static inline uint64_t extract_tag(Cache *cache, uint64_t addr)
 {
     return addr & cache->tag_mask;
@@ -139,6 +255,11 @@ static Cache *cache_init(int blksize, int assoc, int cachesize)
     blk_mask = blksize - 1;
     cache->set_mask = ((cache->num_sets - 1) << cache->blksize_shift);
     cache->tag_mask = ~(cache->set_mask | blk_mask);
+
+    if (metadata_init) {
+        metadata_init(cache);
+    }
+
     return cache;
 }
 
@@ -155,12 +276,21 @@ static int get_invalid_block(Cache *cache, uint64_t set)
     return -1;
 }
 
-static int get_replaced_block(Cache *cache)
+static int get_replaced_block(Cache *cache, int set)
 {
-    return g_rand_int_range(rng, 0, cache->assoc);
+    switch (policy) {
+    case RAND:
+        return g_rand_int_range(rng, 0, cache->assoc);
+    case LRU:
+        return lru_get_lru_block(cache, set);
+    case FIFO:
+        return fifo_get_first_block(cache, set);
+    default:
+        g_assert_not_reached();
+    }
 }
 
-static bool in_cache(Cache *cache, uint64_t addr)
+static int in_cache(Cache *cache, uint64_t addr)
 {
     int i;
     uint64_t tag, set;
@@ -171,11 +301,11 @@ static bool in_cache(Cache *cache, uint64_t addr)
     for (i = 0; i < cache->assoc; i++) {
         if (cache->sets[set].blocks[i].tag == tag &&
                 cache->sets[set].blocks[i].valid) {
-            return true;
+            return i;
         }
     }
 
-    return false;
+    return -1;
 }
 
 /**
@@ -188,20 +318,28 @@ static bool in_cache(Cache *cache, uint64_t addr)
  */
 static bool access_cache(Cache *cache, uint64_t addr)
 {
+    int hit_blk, replaced_blk;
     uint64_t tag, set;
-    int replaced_blk;
-
-    if (in_cache(cache, addr)) {
-        return true;
-    }
 
     tag = extract_tag(cache, addr);
     set = extract_set(cache, addr);
 
+    hit_blk = in_cache(cache, addr);
+    if (hit_blk != -1) {
+        if (update_hit) {
+            update_hit(cache, set, hit_blk);
+        }
+        return true;
+    }
+
     replaced_blk = get_invalid_block(cache, set);
 
     if (replaced_blk == -1) {
-        replaced_blk = get_replaced_block(cache);
+        replaced_blk = get_replaced_block(cache, set);
+    }
+
+    if (update_miss) {
+        update_miss(cache, set, replaced_blk);
     }
 
     cache->sets[set].blocks[replaced_blk].tag = tag;
@@ -308,6 +446,10 @@ static void cache_free(Cache *cache)
         g_free(cache->sets[i].blocks);
     }
 
+    if (metadata_destroy) {
+        metadata_destroy(cache);
+    }
+
     g_free(cache->sets);
     g_free(cache);
 }
@@ -395,6 +537,28 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
     g_hash_table_destroy(miss_ht);
 }
 
+static void policy_init()
+{
+    switch (policy) {
+    case LRU:
+        update_hit = lru_update_blk;
+        update_miss = lru_update_blk;
+        metadata_init = lru_priorities_init;
+        metadata_destroy = lru_priorities_destroy;
+        break;
+    case FIFO:
+        update_miss = fifo_update_on_miss;
+        metadata_init = fifo_init;
+        metadata_destroy = fifo_destroy;
+        break;
+    case RAND:
+        rng = g_rand_new();
+        break;
+    default:
+        g_assert_not_reached();
+    }
+}
+
 QEMU_PLUGIN_EXPORT
 int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
                         int argc, char **argv)
@@ -414,6 +578,7 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
     iblksize = 64;
     icachesize = iblksize * iassoc * 32;
 
+    policy = LRU;
 
     for (i = 0; i < argc; i++) {
         char *opt = argv[i];
@@ -431,12 +596,26 @@ 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, "evict=")) {
+            gchar *p = opt + 6;
+            if (g_strcmp0(p, "rand") == 0) {
+                policy = RAND;
+            } else if (g_strcmp0(p, "lru") == 0) {
+                policy = LRU;
+            } else if (g_strcmp0(p, "fifo") == 0) {
+                policy = FIFO;
+            } else {
+                fprintf(stderr, "invalid eviction policy: %s\n", opt);
+                return -1;
+            }
         } else {
             fprintf(stderr, "option parsing failed: %s\n", opt);
             return -1;
         }
     }
 
+    policy_init();
+
     dcache = cache_init(dblksize, dassoc, dcachesize);
     if (!dcache) {
         const char *err = cache_config_error(dblksize, dassoc, dcachesize);
@@ -453,8 +632,6 @@ int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info,
         return -1;
     }
 
-    rng = g_rand_new();
-
     qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans);
     qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);
 
-- 
2.20.1



  parent reply	other threads:[~2021-07-06 15:08 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-06 14:57 [PATCH v1 00/39] final pre-PR for 6.1 (testing and plugins) Alex Bennée
2021-07-06 14:57 ` [PATCH v1 01/39] Jobs based on custom runners: documentation and configuration placeholder Alex Bennée
2021-07-06 14:57 ` [PATCH v1 02/39] Jobs based on custom runners: build environment docs and playbook Alex Bennée
2021-07-06 14:57 ` [PATCH v1 03/39] Jobs based on custom runners: docs and gitlab-runner setup playbook Alex Bennée
2021-07-06 14:57 ` [PATCH v1 04/39] Jobs based on custom runners: add job definitions for QEMU's machines Alex Bennée
2021-07-06 14:57 ` [PATCH v1 05/39] tests/tcg: generalise the disabling of the signals test Alex Bennée
2021-07-08 15:29   ` Peter Maydell
2021-07-06 14:57 ` [PATCH v1 06/39] build: validate that system capstone works before using it Alex Bennée
2021-07-06 14:57 ` [PATCH v1 07/39] gitlab: support for FreeBSD 12, 13 and macOS 11 via cirrus-run Alex Bennée
2021-07-06 14:57 ` [PATCH v1 08/39] cirrus: delete FreeBSD and macOS jobs Alex Bennée
2021-07-06 14:57 ` [PATCH v1 09/39] hw/usb/ccid: remove references to NSS Alex Bennée
2021-07-06 14:57 ` [PATCH v1 10/39] tests/docker: don't use BUILDKIT in GitLab either Alex Bennée
2021-07-06 14:57 ` [PATCH v1 11/39] tests/docker: use project specific container registries Alex Bennée
2021-07-06 14:57 ` [PATCH v1 12/39] tests/docker: use explicit docker.io registry Alex Bennée
2021-07-06 14:57 ` [PATCH v1 13/39] tests/docker: remove FEATURES env var from templates Alex Bennée
2021-07-06 14:57 ` [PATCH v1 14/39] tests/docker: fix sorting in package lists Alex Bennée
2021-07-06 14:57 ` [PATCH v1 15/39] tests/docker: fix mistakes in centos " Alex Bennée
2021-07-06 14:57 ` [PATCH v1 16/39] tests/docker: fix mistakes in fedora package list Alex Bennée
2021-07-06 14:57 ` [PATCH v1 17/39] tests/docker: fix mistakes in ubuntu package lists Alex Bennée
2021-07-07 14:41   ` Philippe Mathieu-Daudé
2021-07-07 14:49     ` Daniel P. Berrangé
2021-07-06 14:57 ` [PATCH v1 18/39] tests/docker: remove mingw packages from Fedora Alex Bennée
2021-07-06 14:57 ` [PATCH v1 19/39] tests/docker: expand centos8 package list Alex Bennée
2021-07-06 14:57 ` [PATCH v1 20/39] tests/docker: expand fedora " Alex Bennée
2021-07-06 14:57 ` [PATCH v1 21/39] tests/docker: expand ubuntu1804 " Alex Bennée
2021-07-06 14:58 ` [PATCH v1 22/39] tests/docker: expand ubuntu2004 " Alex Bennée
2021-07-06 14:58 ` [PATCH v1 23/39] tests/docker: expand opensuse-leap " Alex Bennée
2021-07-06 14:58 ` [PATCH v1 24/39] tests/vm: update NetBSD to 9.2 Alex Bennée
2021-07-06 14:58 ` [PATCH v1 25/39] tests/vm: update openbsd to release 6.9 Alex Bennée
2021-07-06 14:58 ` [PATCH v1 26/39] tests/tcg: make test-mmap a little less aggressive Alex Bennée
2021-07-06 14:58 ` [PATCH v1 27/39] plugins: fix-up handling of internal hostaddr for 32 bit Alex Bennée
2021-07-06 14:58 ` [PATCH v1 28/39] meson.build: move TCG plugin summary output Alex Bennée
2021-07-06 14:58 ` [PATCH v1 29/39] configure: don't allow plugins to be enabled for a non-TCG build Alex Bennée
2021-07-07  4:17   ` Thomas Huth
2021-07-06 14:58 ` [PATCH v1 30/39] configure: stop user enabling plugins on Windows for now Alex Bennée
2021-07-07  4:24   ` Thomas Huth
2021-07-06 14:58 ` [PATCH v1 31/39] tcg/plugins: enable by default for TCG builds Alex Bennée
2021-07-07  4:32   ` Thomas Huth
2021-07-06 14:58 ` [PATCH v1 32/39] contrib/plugins: enable -Wall for building plugins Alex Bennée
2021-07-07  4:36   ` Thomas Huth
2021-07-06 14:58 ` [PATCH v1 33/39] contrib/plugins: add execlog to log instruction execution and memory access Alex Bennée
2021-07-06 14:58 ` [PATCH v1 34/39] docs/devel: tcg-plugins: add execlog plugin description Alex Bennée
2021-07-06 14:58 ` [PATCH v1 35/39] plugins: Added a new cache modelling plugin Alex Bennée
2021-07-06 14:58 ` [PATCH v1 36/39] plugins/cache: Enable cache parameterization Alex Bennée
2021-07-06 14:58 ` Alex Bennée [this message]
2021-07-06 14:58 ` [PATCH v1 38/39] docs/devel: Added cache plugin to the plugins docs Alex Bennée
2021-07-06 14:58 ` [PATCH v1 39/39] MAINTAINTERS: Added myself as a reviewer for TCG Plugins Alex Bennée
2021-07-06 15:35   ` Peter Maydell
2021-07-07  8:33 ` [PATCH v1 00/39] final pre-PR for 6.1 (testing and plugins) Philippe Mathieu-Daudé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210706145817.24109-38-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=aurelien@aurel32.net \
    --cc=berrange@redhat.com \
    --cc=crosa@redhat.com \
    --cc=erdnaxe@crans.org \
    --cc=f4bug@amsat.org \
    --cc=fam@euphon.net \
    --cc=ma.mandourr@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.