qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Lieven <pl@dlhnet.de>
To: "qemu-devel@nongnu.org" <qemu-devel@nongnu.org>
Cc: Orit Wasserman <owasserm@redhat.com>
Subject: [Qemu-devel] [PATCH] page_cache: use multiplicative hash for page position calculation
Date: Fri, 01 Mar 2013 12:53:05 +0100	[thread overview]
Message-ID: <513096A1.8020008@dlhnet.de> (raw)

instead of a linear mapping we use a multiplicative hash
with the golden ratio to derive the cache bucket from the
address. this helps to reduce collisions if memory positions
are multiple of the cache size and it avoids a division
in the position calculation.

Signed-off-by: Peter Lieven <pl@kamp.de>
---
  page_cache.c |    5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/page_cache.c b/page_cache.c
index 376f1db..45d769a 100644
--- a/page_cache.c
+++ b/page_cache.c
@@ -24,6 +24,7 @@
  #include <strings.h>

  #include "qemu-common.h"
+#include "qemu/host-utils.h"
  #include "migration/page_cache.h"

  #ifdef DEBUG_CACHE
@@ -48,6 +49,7 @@ struct PageCache {
      int64_t max_num_items;
      uint64_t max_item_age;
      int64_t num_items;
+    uint64_t hash_shift_bits;
  };

  PageCache *cache_init(int64_t num_pages, unsigned int page_size)
@@ -72,6 +74,7 @@ PageCache *cache_init(int64_t num_pages, unsigned int page_size)
      cache->num_items = 0;
      cache->max_item_age = 0;
      cache->max_num_items = num_pages;
+    cache->hash_shift_bits = clz64(num_pages-1);

      DPRINTF("Setting cache buckets to %" PRId64 "\n", cache->max_num_items);

@@ -108,7 +111,7 @@ static size_t cache_get_cache_pos(const PageCache *cache,
      size_t pos;

      g_assert(cache->max_num_items);
-    pos = (address / cache->page_size) & (cache->max_num_items - 1);
+    pos = (address * 0x9e3779b97f4a7c13) >> cache->hash_shift_bits;
      return pos;
  }

-- 
1.7.9.5

             reply	other threads:[~2013-03-01 11:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-01 11:53 Peter Lieven [this message]
2013-03-01 12:50 ` [Qemu-devel] [PATCH] page_cache: use multiplicative hash for page position calculation Laszlo Ersek
2013-03-01 13:22   ` Peter Lieven

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=513096A1.8020008@dlhnet.de \
    --to=pl@dlhnet.de \
    --cc=owasserm@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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).