All of lore.kernel.org
 help / color / mirror / Atom feed
From: Denis Plotnikov <dplotnikov@virtuozzo.com>
To: dgilbert@redhat.com, quintela@redhat.com, pbonzini@redhat.com
Cc: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH v1 07/17] background snapshot: introduce page buffer
Date: Wed, 18 Jul 2018 18:41:50 +0300	[thread overview]
Message-ID: <20180718154200.26777-8-dplotnikov@virtuozzo.com> (raw)
In-Reply-To: <20180718154200.26777-1-dplotnikov@virtuozzo.com>

To limit the amount of memory used by the background snapshot
a memory limiter is used which called "page buffer".
In fact, it's a memory page counter limiting the page allocation.
Currently, the limit of pages is hardcoded but its setter is
the matter of the future work.

The background snapshot makes a copy of the page before writing it
to the snapshot destination, but it doesn't use a preallocated buffer,
because the background snapshot employs the migration infrastructure
which, in turn, uses madvice to release the buffer.
The advice issuing is hard to track, so here we rely on anonymous
memory mapping and releasing it by the existing code.

Signed-off-by: Denis Plotnikov <dplotnikov@virtuozzo.com>
---
 migration/ram.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++
 migration/ram.h |  3 ++
 2 files changed, 94 insertions(+)

diff --git a/migration/ram.c b/migration/ram.c
index 4399c31606..27d3403435 100644
--- a/migration/ram.c
+++ b/migration/ram.c
@@ -192,6 +192,16 @@ struct RAMSrcPageRequest {
     QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req;
 };
 
+/* Page buffer used for background snapshot */
+typedef struct RAMPageBuffer {
+    /* Page buffer capacity in host pages */
+    int capacity;
+    /* Current number of pages in the buffer */
+    int used;
+    /* Event to notify that buffer usage is under capacity */
+    QemuEvent used_decreased;
+} RAMPageBuffer;
+
 /* State of RAM for migration */
 struct RAMState {
     /* QEMUFile used for this migration */
@@ -230,6 +240,11 @@ struct RAMState {
     /* Queue of outstanding page requests from the destination */
     QemuMutex src_page_req_mutex;
     QSIMPLEQ_HEAD(src_page_requests, RAMSrcPageRequest) src_page_requests;
+    /* The following 2 are for background snapshot */
+    /* Buffer data to store copies of ram pages while async vm saving */
+    RAMPageBuffer page_buffer;
+    /* Event to notify that a page copying just has finished*/
+    QemuEvent page_copying_done;
 };
 typedef struct RAMState RAMState;
 
@@ -1540,6 +1555,52 @@ void ram_block_list_destroy(void)
     }
 }
 
+static void ram_page_buffer_decrease_used(void)
+{
+    qemu_event_reset(&ram_state->page_buffer.used_decreased);
+    atomic_dec(&ram_state->page_buffer.used);
+    qemu_event_set(&ram_state->page_buffer.used_decreased);
+}
+
+static void ram_page_buffer_increase_used_wait(void)
+{
+    int used, *used_ptr;
+    RAMState *rs = ram_state;
+    used_ptr = &rs->page_buffer.used;
+    do {
+        used = atomic_read(used_ptr);
+        if (rs->page_buffer.capacity > used) {
+            if (atomic_cmpxchg(used_ptr, used, used + 1) == used) {
+                return;
+            } else {
+                continue;
+            }
+        } else {
+            qemu_event_wait(&rs->page_buffer.used_decreased);
+        }
+    } while (true);
+}
+
+void *ram_page_buffer_get(void)
+{
+    void *page;
+    ram_page_buffer_increase_used_wait();
+    page = mmap(0, TARGET_PAGE_SIZE, PROT_READ | PROT_WRITE,
+                MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+    if (page == MAP_FAILED) {
+        ram_page_buffer_decrease_used();
+        page = NULL;
+    }
+    return page;
+}
+
+int ram_page_buffer_free(void *buffer)
+{
+    ram_page_buffer_decrease_used();
+    return qemu_madvise(buffer, TARGET_PAGE_SIZE, MADV_DONTNEED);
+}
+
 RAMBlock *ram_bgs_block_find(uint8_t *address, ram_addr_t *page_offset)
 {
     RAMBlock *block = NULL;
@@ -1559,6 +1620,7 @@ RAMBlock *ram_bgs_block_find(uint8_t *address, ram_addr_t *page_offset)
 
     return NULL;
 }
+
 /**
  * ram_find_and_save_block: finds a dirty page and sends it to f
  *
@@ -1651,6 +1713,13 @@ static void xbzrle_load_cleanup(void)
 
 static void ram_state_cleanup(RAMState **rsp)
 {
+    if (migrate_background_snapshot()) {
+        qemu_event_destroy(&(*rsp)->page_buffer.used_decreased);
+        /* In case somebody is still waiting for the event */
+        qemu_event_set(&(*rsp)->page_copying_done);
+        qemu_event_destroy(&(*rsp)->page_copying_done);
+    }
+
     migration_page_queue_free(*rsp);
     qemu_mutex_destroy(&(*rsp)->bitmap_mutex);
     qemu_mutex_destroy(&(*rsp)->src_page_req_mutex);
@@ -1689,6 +1758,13 @@ static void ram_save_cleanup(void *opaque)
         block->bmap = NULL;
         g_free(block->unsentmap);
         block->unsentmap = NULL;
+
+        if (migrate_background_snapshot()) {
+            g_free(block->touched_map);
+            block->touched_map = NULL;
+            g_free(block->copied_map);
+            block->copied_map = NULL;
+        }
     }
 
     xbzrle_cleanup();
@@ -1703,6 +1779,10 @@ static void ram_state_reset(RAMState *rs)
     rs->last_page = 0;
     rs->last_version = ram_list.version;
     rs->ram_bulk_stage = true;
+
+    /* page buffer capacity in number of pages */
+    rs->page_buffer.capacity = 1000;
+    rs->page_buffer.used = 0;
 }
 
 #define MAX_WAIT 50 /* ms, half buffered_file limit */
@@ -2180,6 +2260,11 @@ static int ram_state_init(RAMState **rsp)
      */
     (*rsp)->migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS;
 
+    if (migrate_background_snapshot()) {
+        qemu_event_init(&ram_state->page_buffer.used_decreased, false);
+        qemu_event_init(&ram_state->page_copying_done, false);
+    }
+
     ram_state_reset(*rsp);
 
     return 0;
@@ -2196,10 +2281,16 @@ static void ram_list_init_bitmaps(void)
             pages = block->max_length >> TARGET_PAGE_BITS;
             block->bmap = bitmap_new(pages);
             bitmap_set(block->bmap, 0, pages);
+
             if (migrate_postcopy_ram()) {
                 block->unsentmap = bitmap_new(pages);
                 bitmap_set(block->unsentmap, 0, pages);
             }
+
+            if (migrate_background_snapshot()) {
+                block->touched_map = bitmap_new(pages);
+                block->copied_map = bitmap_new(pages);
+            }
         }
     }
 }
diff --git a/migration/ram.h b/migration/ram.h
index 385579cae9..7c802c1d7f 100644
--- a/migration/ram.h
+++ b/migration/ram.h
@@ -68,4 +68,7 @@ void ram_block_list_destroy(void);
 
 RAMBlock *ram_bgs_block_find(uint8_t *address, ram_addr_t *page_offset);
 
+void *ram_page_buffer_get(void);
+int ram_page_buffer_free(void *buffer);
+
 #endif
-- 
2.17.0

  parent reply	other threads:[~2018-07-18 15:42 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-18 15:41 [Qemu-devel] [PATCH v1 00/17] Background snapshots Denis Plotnikov
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 01/17] migration: add background snapshot capability Denis Plotnikov
2018-07-20  5:14   ` Peter Xu
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 02/17] bitops: add some atomic versions of bitmap operations Denis Plotnikov
2018-07-20  5:09   ` Peter Xu
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 03/17] threads: add infrastructure to process sigsegv Denis Plotnikov
2018-07-20  4:58   ` Peter Xu
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 04/17] background snapshot: make a dedicated type for ram block list Denis Plotnikov
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 05/17] ram: extend the data structures for background snapshotting Denis Plotnikov
2018-07-20  7:59   ` Peter Xu
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 06/17] background snapshot: add helpers to manage a copy of ram block list Denis Plotnikov
2018-07-20  7:57   ` Peter Xu
2018-07-18 15:41 ` Denis Plotnikov [this message]
2018-07-20  9:22   ` [Qemu-devel] [PATCH v1 07/17] background snapshot: introduce page buffer Peter Xu
2018-07-20 10:34   ` Paolo Bonzini
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 08/17] migration: add helpers to change VM memory protection rights Denis Plotnikov
2018-07-20 11:28   ` Dr. David Alan Gilbert
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 09/17] background snapshot: extend RAM request for holding a page copy pointer Denis Plotnikov
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 10/17] background snapshots: adapt the page queueing code for using page copies Denis Plotnikov
2018-07-20  8:39   ` Peter Xu
2018-07-20 11:46   ` Peter Xu
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 11/17] background snapshot: add a memory page copying function Denis Plotnikov
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 12/17] ram: add background snapshot support in ram page saving part of migration Denis Plotnikov
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 13/17] background snapshot: add write-protected page access handler function Denis Plotnikov
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 14/17] kvm: add failed memeory access exit reason Denis Plotnikov
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 15/17] kvm: add vCPU failed memeory access processing Denis Plotnikov
2018-07-20  9:44   ` Peter Xu
2018-07-18 15:41 ` [Qemu-devel] [PATCH v1 16/17] migration: move the device state saving logic to a separate function Denis Plotnikov
2018-07-18 15:42 ` [Qemu-devel] [PATCH v1 17/17] background snapshot: enable background snapshot Denis Plotnikov
2018-07-20  9:27 ` [Qemu-devel] [PATCH v1 00/17] Background snapshots Peter Xu
2018-09-04 13:00   ` Denis Plotnikov
2018-09-05  3:32     ` Peter Xu
2018-09-05  9:18       ` Denis Plotnikov
2018-09-05  9:30         ` Peter Xu

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=20180718154200.26777-8-dplotnikov@virtuozzo.com \
    --to=dplotnikov@virtuozzo.com \
    --cc=dgilbert@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=quintela@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.