qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Pawel Zmarzly <pzmarzly0@gmail.com>
To: qemu-devel@nongnu.org
Cc: peterx@redhat.com, farosas@suse.de
Subject: [PATCH v2] scripts/analyze-migration: Support mapped-ram snapshot format
Date: Wed, 26 Nov 2025 15:50:15 +0000	[thread overview]
Message-ID: <20251126155015.941129-1-pzmarzly0@gmail.com> (raw)

The script has not been updated to read mapped-ram snapshots and is currently
crashing when trying to read such a file.

With this commit, it can now read a snapshot created with:

    (qemu) migrate_set_capability x-ignore-shared on
    (qemu) migrate_set_capability mapped-ram on
    (qemu) migrate -d file:vm.state

Signed-off-by: Pawel Zmarzly <pzmarzly0@gmail.com>
---
V2: Added support for format change in my other patch "migration: Fix writing
mapped_ram + ignore_shared snapshots". You can see whole stack at
https://gitlab.com/pzmarzly/qemu/-/commits/pzmarzly?ref_type=heads
---
 scripts/analyze-migration.py | 48 ++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/scripts/analyze-migration.py b/scripts/analyze-migration.py
index 3303c05358..b6694bbd23 100755
--- a/scripts/analyze-migration.py
+++ b/scripts/analyze-migration.py
@@ -19,6 +19,7 @@
 
 import json
 import os
+import math
 import argparse
 import collections
 import struct
@@ -127,6 +128,7 @@ def __init__(self, file, version_id, ramargs, section_key):
         self.dump_memory = ramargs['dump_memory']
         self.write_memory = ramargs['write_memory']
         self.ignore_shared = ramargs['ignore_shared']
+        self.mapped_ram = ramargs['mapped_ram']
         self.sizeinfo = collections.OrderedDict()
         self.data = collections.OrderedDict()
         self.data['section sizes'] = self.sizeinfo
@@ -170,6 +172,50 @@ def read(self):
                         self.files[self.name] = f
                     if self.ignore_shared:
                         mr_addr = self.file.read64()
+                    if self.mapped_ram:
+                        version = self.file.read32()
+                        if version != 1:
+                            raise Exception("Unsupported MappedRamHeader version %s" % version)
+                            
+                        page_size = self.file.read64()
+                        if page_size != self.TARGET_PAGE_SIZE:
+                            raise Exception("Page size mismatch in MappedRamHeader")
+
+                        bitmap_offset = self.file.read64()
+                        pages_offset = self.file.read64()
+                        
+                        if self.ignore_shared and bitmap_offset == 0 and pages_offset == 0:
+                            continue
+
+                        if self.dump_memory or self.write_memory:
+                            num_pages = len // page_size
+
+                            self.file.seek(bitmap_offset, os.SEEK_SET)
+                            bitmap_len = int(math.ceil(num_pages / 8))
+                            bitmap = self.file.readvar(size=bitmap_len)
+
+                            self.file.seek(pages_offset, os.SEEK_SET)
+                            for page_num in range(num_pages):
+                                page_addr = page_num * page_size
+
+                                is_filled = (bitmap[page_num // 8] >> page_num % 8) & 1
+                                if is_filled:
+                                    data = self.file.readvar(size=self.TARGET_PAGE_SIZE)
+                                    if self.write_memory:
+                                        self.files[self.name].seek(page_addr, os.SEEK_SET)
+                                        self.files[self.name].write(data)
+                                    if self.dump_memory:
+                                        hexdata = " ".join("{0:02x}".format(c) for c in data)
+                                        self.memory['%s (0x%016x)' % (self.name, page_addr)] = hexdata
+                                else:
+                                    self.file.seek(self.TARGET_PAGE_SIZE, os.SEEK_CUR)
+                                    if self.write_memory:
+                                        self.files[self.name].seek(page_addr, os.SEEK_SET)
+                                        self.files[self.name].write(b'\x00' * self.TARGET_PAGE_SIZE)
+                                    if self.dump_memory:
+                                        self.memory['%s (0x%016x)' % (self.name, page_addr)] = 'Filled with 0x00'
+
+                        self.file.seek(pages_offset + len, os.SEEK_SET)
                 flags &= ~self.RAM_SAVE_FLAG_MEM_SIZE
 
             if flags & self.RAM_SAVE_FLAG_COMPRESS:
@@ -663,6 +709,7 @@ def read(self, desc_only = False, dump_memory = False,
         ramargs['dump_memory'] = dump_memory
         ramargs['write_memory'] = write_memory
         ramargs['ignore_shared'] = False
+        ramargs['mapped_ram'] = False
         self.section_classes[('ram',0)][1] = ramargs
 
         while True:
@@ -674,6 +721,7 @@ def read(self, desc_only = False, dump_memory = False,
                 section = ConfigurationSection(file, config_desc)
                 section.read()
                 ramargs['ignore_shared'] = section.has_capability('x-ignore-shared')
+                ramargs['mapped_ram'] = section.has_capability('mapped-ram')
             elif section_type == self.QEMU_VM_SECTION_START or section_type == self.QEMU_VM_SECTION_FULL:
                 section_id = file.read32()
                 name = file.readstr()
-- 
2.52.0


             reply	other threads:[~2025-11-26 15:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-26 15:50 Pawel Zmarzly [this message]
2025-12-01 21:51 ` [PATCH v2] scripts/analyze-migration: Support mapped-ram snapshot format 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=20251126155015.941129-1-pzmarzly0@gmail.com \
    --to=pzmarzly0@gmail.com \
    --cc=farosas@suse.de \
    --cc=peterx@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).