qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: Alberto Garcia <berto@igalia.com>, qemu-devel@nongnu.org
Cc: Stefan Hajnoczi <stefanha@redhat.com>, qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [Qemu-block] [PATCH 3/7] qcow2: use an LRU algorithm to replace entries from the L2 cache
Date: Fri, 08 May 2015 17:22:34 +0200	[thread overview]
Message-ID: <554CD4BA.2020805@redhat.com> (raw)
In-Reply-To: <97f3d8a1ff7cbb2922a1d1c2007d7ccf44859294.1430919406.git.berto@igalia.com>

On 06.05.2015 15:39, Alberto Garcia wrote:
> The current algorithm to evict entries from the cache gives always
> preference to those in the lowest positions. As the size of the cache
> increases, the chances of the later elements of being removed decrease
> exponentially.
>
> In a scenario with random I/O and lots of cache misses, entries in
> positions 8 and higher are rarely (if ever) evicted. This can be seen
> even with the default cache size, but with larger caches the problem
> becomes more obvious.
>
> Using an LRU algorithm makes the chances of being removed from the
> cache independent from the position.
>
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>   block/qcow2-cache.c | 31 +++++++++++++++----------------
>   1 file changed, 15 insertions(+), 16 deletions(-)
>
> diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c
> index 6e78c8f..786c10a 100644
> --- a/block/qcow2-cache.c
> +++ b/block/qcow2-cache.c
> @@ -28,10 +28,10 @@
>   #include "trace.h"
>   
>   typedef struct Qcow2CachedTable {
> -    int64_t offset;
> -    bool    dirty;
> -    int     cache_hits;
> -    int     ref;
> +    int64_t  offset;
> +    bool     dirty;
> +    uint64_t lru_counter;
> +    int      ref;
>   } Qcow2CachedTable;
>   
>   struct Qcow2Cache {
> @@ -40,6 +40,7 @@ struct Qcow2Cache {
>       int                     size;
>       bool                    depends_on_flush;
>       void                   *table_array;
> +    uint64_t                lru_counter;
>   };
>   
>   static inline void *qcow2_cache_get_table_addr(BlockDriverState *bs,
> @@ -233,16 +234,18 @@ int qcow2_cache_empty(BlockDriverState *bs, Qcow2Cache *c)
>       for (i = 0; i < c->size; i++) {
>           assert(c->entries[i].ref == 0);
>           c->entries[i].offset = 0;
> -        c->entries[i].cache_hits = 0;
> +        c->entries[i].lru_counter = 0;
>       }
>   
> +    c->lru_counter = 0;
> +
>       return 0;
>   }
>   
>   static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c)
>   {
>       int i;
> -    int min_count = INT_MAX;
> +    uint64_t min_lru_counter = UINT64_MAX;
>       int min_index = -1;
>   
>   
> @@ -251,15 +254,9 @@ static int qcow2_cache_find_entry_to_replace(Qcow2Cache *c)
>               continue;
>           }
>   
> -        if (c->entries[i].cache_hits < min_count) {
> +        if (c->entries[i].lru_counter < min_lru_counter) {
>               min_index = i;
> -            min_count = c->entries[i].cache_hits;
> -        }
> -
> -        /* Give newer hits priority */
> -        /* TODO Check how to optimize the replacement strategy */
> -        if (c->entries[i].cache_hits > 1) {
> -            c->entries[i].cache_hits /= 2;
> +            min_lru_counter = c->entries[i].lru_counter;
>           }
>       }
>   
> @@ -318,12 +315,10 @@ static int qcow2_cache_do_get(BlockDriverState *bs, Qcow2Cache *c,
>   
>       /* Give the table some hits for the start so that it won't be replaced
>        * immediately. The number 32 is completely arbitrary. */
> -    c->entries[i].cache_hits = 32;
>       c->entries[i].offset = offset;

With the coment removed:

Reviewed-by: Max Reitz <mreitz@redhat.com>

>       /* And return the right table */
>   found:
> -    c->entries[i].cache_hits++;
>       c->entries[i].ref++;
>       *table = qcow2_cache_get_table_addr(bs, c, i);
>   
> @@ -356,6 +351,10 @@ int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table)
>       c->entries[i].ref--;
>       *table = NULL;
>   
> +    if (c->entries[i].ref == 0) {
> +        c->entries[i].lru_counter = ++c->lru_counter;
> +    }
> +
>       assert(c->entries[i].ref >= 0);
>       return 0;
>   }

  parent reply	other threads:[~2015-05-08 15:23 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-06 13:39 [Qemu-devel] [PATCH v2 0/7] qcow2 L2/refcount cache improvements Alberto Garcia
2015-05-06 13:39 ` [Qemu-devel] [PATCH 1/7] qcow2: use one single memory block for the L2/refcount cache tables Alberto Garcia
2015-05-07 10:14   ` Stefan Hajnoczi
2015-05-08 15:03   ` [Qemu-devel] [Qemu-block] " Max Reitz
2015-05-06 13:39 ` [Qemu-devel] [PATCH 2/7] qcow2: simplify qcow2_cache_put() and qcow2_cache_entry_mark_dirty() Alberto Garcia
2015-05-07 10:15   ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-05-08 15:12   ` Max Reitz
2015-05-06 13:39 ` [Qemu-devel] [PATCH 3/7] qcow2: use an LRU algorithm to replace entries from the L2 cache Alberto Garcia
2015-05-07 10:16   ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-05-07 14:55   ` [Qemu-devel] " Eric Blake
2015-05-07 15:01     ` Alberto Garcia
2015-05-08 15:22   ` Max Reitz [this message]
2015-05-06 13:39 ` [Qemu-devel] [PATCH 4/7] qcow2: remove qcow2_cache_find_entry_to_replace() Alberto Garcia
2015-05-07 10:17   ` Stefan Hajnoczi
2015-05-08 15:27   ` [Qemu-devel] [Qemu-block] " Max Reitz
2015-05-06 13:39 ` [Qemu-devel] [PATCH 5/7] qcow2: use a hash to look for entries in the L2 cache Alberto Garcia
2015-05-07 10:18   ` [Qemu-devel] [Qemu-block] " Stefan Hajnoczi
2015-05-08 15:46   ` Max Reitz
2015-05-08 16:48     ` Alberto Garcia
2015-05-06 13:39 ` [Qemu-devel] [PATCH 6/7] qcow2: make qcow2_cache_put() a void function Alberto Garcia
2015-05-07 10:20   ` Stefan Hajnoczi
2015-05-08 15:51   ` [Qemu-devel] [Qemu-block] " Max Reitz
2015-05-08 16:59     ` Alberto Garcia
2015-05-06 13:39 ` [Qemu-devel] [PATCH 7/7] qcow2: style fixes in qcow2-cache.c Alberto Garcia
2015-05-07 10:20   ` Stefan Hajnoczi
2015-05-08 15:52   ` [Qemu-devel] [Qemu-block] " Max Reitz

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=554CD4BA.2020805@redhat.com \
    --to=mreitz@redhat.com \
    --cc=berto@igalia.com \
    --cc=qemu-block@nongnu.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).