From: Orit Wasserman <owasserm@redhat.com>
To: qemu-devel@nongnu.org
Cc: blauwirbel@gmail.com, stefanha@gmail.com,
Orit Wasserman <owasserm@redhat.com>,
avi@redhat.com, quintela@redhat.com
Subject: [Qemu-devel] [PATCH v6 01/11] Add cache handling functions
Date: Wed, 25 Jan 2012 13:26:39 +0200 [thread overview]
Message-ID: <1327490809-21393-2-git-send-email-owasserm@redhat.com> (raw)
In-Reply-To: <1327490809-21393-1-git-send-email-owasserm@redhat.com>
Add LRU page caching mechanism.
The pages are stored in the cache ordered by their address.
Signed-off-by: Orit Wasserman <owasserm@redhat.com>
---
arch_init.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 175 insertions(+), 0 deletions(-)
diff --git a/arch_init.c b/arch_init.c
index 95ac682..34e4e60 100644
--- a/arch_init.c
+++ b/arch_init.c
@@ -28,6 +28,7 @@
#include <sys/types.h>
#include <sys/mman.h>
#endif
+#include <assert.h>
#include "config.h"
#include "monitor.h"
#include "sysemu.h"
@@ -43,6 +44,14 @@
#include "hw/smbios.h"
#include "exec-memory.h"
+#ifdef DEBUG_ARCH_INIT
+#define DPRINTF(fmt, ...) \
+ do { fprintf(stdout, "arch_init: " fmt, ## __VA_ARGS__); } while (0)
+#else
+#define DPRINTF(fmt, ...) \
+ do { } while (0)
+#endif
+
#ifdef TARGET_SPARC
int graphic_width = 1024;
int graphic_height = 768;
@@ -126,6 +135,172 @@ static int is_dup_page(uint8_t *page)
return 1;
}
+/***********************************************************/
+/* Page cache for storing previous pages as basis for XBZRLE compression */
+#define CACHE_N_WAY 2 /* 2-way assossiative cache */
+
+typedef struct CacheItem {
+ ram_addr_t it_addr;
+ unsigned long it_age;
+ uint8_t *it_data;
+} CacheItem;
+
+typedef struct CacheBucket {
+ CacheItem bkt_item[CACHE_N_WAY];
+} CacheBucket;
+
+static CacheBucket *page_cache;
+static int64_t cache_num_buckets;
+static uint64_t cache_max_item_age;
+static int64_t cache_num_items;
+
+static void cache_init(ssize_t num_buckets);
+static void cache_fini(void);
+static int cache_is_cached(ram_addr_t addr);
+static int cache_get_oldest(CacheBucket *buck);
+static int cache_get_newest(CacheBucket *buck, ram_addr_t addr);
+static void cache_insert(ram_addr_t id, uint8_t *pdata);
+static unsigned long cache_get_cache_pos(ram_addr_t address);
+static CacheItem *cache_item_get(unsigned long pos, int item);
+
+/***********************************************************/
+/* XBRLE page cache implementation */
+static CacheItem *cache_item_get(unsigned long pos, int item)
+{
+ assert(page_cache);
+ return &page_cache[pos].bkt_item[item];
+}
+
+static void cache_init(int64_t num_bytes)
+{
+ int i;
+
+ cache_num_items = 0;
+ cache_max_item_age = 0;
+ cache_num_buckets = num_bytes / (TARGET_PAGE_SIZE * CACHE_N_WAY);
+ assert(cache_num_buckets);
+ DPRINTF("Setting cache buckets to %lu\n", cache_num_buckets);
+
+ assert(!page_cache);
+ page_cache = (CacheBucket *)g_malloc((cache_num_buckets) *
+ sizeof(CacheBucket));
+
+ for (i = 0; i < cache_num_buckets; i++) {
+ int j;
+ for (j = 0; j < CACHE_N_WAY; j++) {
+ CacheItem *it = cache_item_get(i, j);
+ it->it_data = NULL;
+ it->it_age = 0;
+ it->it_addr = -1;
+ }
+ }
+}
+
+static void cache_fini(void)
+{
+ int i;
+
+ assert(page_cache);
+
+ for (i = 0; i < cache_num_buckets; i++) {
+ int j;
+ for (j = 0; j < CACHE_N_WAY; j++) {
+ CacheItem *it = cache_item_get(i, j);
+ g_free(it->it_data);
+ it->it_data = 0;
+ }
+ }
+
+ g_free(page_cache);
+ page_cache = NULL;
+}
+
+static unsigned long cache_get_cache_pos(ram_addr_t address)
+{
+ unsigned long pos;
+
+ assert(cache_num_buckets);
+ pos = (address/TARGET_PAGE_SIZE) & (cache_num_buckets - 1);
+ return pos;
+}
+
+static int cache_get_newest(CacheBucket *buck, ram_addr_t addr)
+{
+ unsigned long big = 0;
+ int big_pos = -1;
+ int j;
+
+ assert(page_cache);
+
+ for (j = 0; j < CACHE_N_WAY; j++) {
+ CacheItem *it = &buck->bkt_item[j];
+
+ if (it->it_addr != addr) {
+ continue;
+ }
+
+ if (!j || it->it_age > big) {
+ big = it->it_age;
+ big_pos = j;
+ }
+ }
+
+ return big_pos;
+}
+
+static int cache_get_oldest(CacheBucket *buck)
+{
+ unsigned long small = 0;
+ int small_pos = -1;
+ int j;
+
+ assert(page_cache);
+
+ for (j = 0; j < CACHE_N_WAY; j++) {
+ CacheItem *it = &buck->bkt_item[j];
+
+ if (!j || it->it_age < small) {
+ small = it->it_age;
+ small_pos = j;
+ }
+ }
+
+ return small_pos;
+}
+
+static int cache_is_cached(ram_addr_t addr)
+{
+ unsigned long pos = cache_get_cache_pos(addr);
+
+ assert(page_cache);
+ CacheBucket *bucket = &page_cache[pos];
+ return cache_get_newest(bucket, addr);
+}
+
+static void cache_insert(unsigned long addr, uint8_t *pdata)
+{
+ unsigned long pos;
+ int slot = -1;
+ CacheBucket *bucket;
+
+ pos = cache_get_cache_pos(addr);
+ assert(page_cache);
+ bucket = &page_cache[pos];
+ slot = cache_get_oldest(bucket); /* evict LRU */
+
+ /* actual update of entry */
+ CacheItem *it = cache_item_get(pos, slot);
+ if (!it->it_data) {
+ it->it_data = g_malloc(TARGET_PAGE_SIZE);
+ cache_num_items++;
+ }
+
+ memcpy(it->it_data, pdata, TARGET_PAGE_SIZE);
+ it->it_age = ++cache_max_item_age;
+ it->it_addr = addr;
+}
+
+
static RAMBlock *last_block;
static ram_addr_t last_offset;
--
1.7.6.5
next prev parent reply other threads:[~2012-01-25 11:29 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-01-25 11:26 [Qemu-devel] [PATCH v6 00/11] XBRLE delta for live migration of large memory app Orit Wasserman
2012-01-25 11:26 ` Orit Wasserman [this message]
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 02/11] Add uleb encoding/decoding functions Orit Wasserman
2012-01-25 11:48 ` Avi Kivity
2012-01-25 12:22 ` Orit Wasserman
2012-01-25 12:27 ` Orit Wasserman
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 03/11] Add save_block_hdr function Orit Wasserman
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 04/11] Add host_from_stream_offset_versioned function Orit Wasserman
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 05/11] Add XBZRLE to ram_save_block and ram_save_live Orit Wasserman
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 06/11] Add MigrationParams structure Orit Wasserman
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 07/11] Add XBZRLE parameters to MigrationState Orit Wasserman
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 08/11] Add migration capabilties Orit Wasserman
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 09/11] Add set_cachesize command Orit Wasserman
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 10/11] Add XBZRLE option to migrate command Orit Wasserman
2012-01-25 11:26 ` [Qemu-devel] [PATCH v6 11/11] Add XBZRLE statstics information Orit Wasserman
2012-01-25 11:53 ` [Qemu-devel] [PATCH v6 00/11] XBRLE delta for live migration of large memory app Avi Kivity
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=1327490809-21393-2-git-send-email-owasserm@redhat.com \
--to=owasserm@redhat.com \
--cc=avi@redhat.com \
--cc=blauwirbel@gmail.com \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=stefanha@gmail.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).