Received: from eggs.gnu.org ([2001:4830:134:3::10]:38060) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VYyDo-0008Ck-J4 for qemu-devel@nongnu.org; Wed, 23 Oct 2013 09:11:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VYyDi-0008K6-9p for qemu-devel@nongnu.org; Wed, 23 Oct 2013 09:11:36 -0400 Received: from cantor2.suse.de ([195.135.220.15]:38161 helo=mx2.suse.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VYyDh-0008Ix-Ql for qemu-devel@nongnu.org; Wed, 23 Oct 2013 09:11:30 -0400 From: Alexander Graf Date: Wed, 23 Oct 2013 15:11:24 +0200 Message-Id: <1382533884-18739-4-git-send-email-agraf@suse.de> In-Reply-To: <1382533884-18739-1-git-send-email-agraf@suse.de> References: <1382533884-18739-1-git-send-email-agraf@suse.de> Subject: [Qemu-devel] [PATCH 3/3] Add migration stream analyzation script List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: QEMU Developers Cc: Lucas Meneghel Rodrigues , Anthony Liguori This patch adds a python tool to the scripts directory that can read a dumped migration stream which contains the debug_migration device and construct a human readable JSON stream out of it. It's very simple to use: $ qemu-system-x86_64 -device debug_migration (qemu) migrate "exec:cat > mig" $ ./scripts/analyze_migration.py -f mig Signed-off-by: Alexander Graf --- scripts/analyze-migration.py | 483 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 483 insertions(+) create mode 100755 scripts/analyze-migration.py diff --git a/scripts/analyze-migration.py b/scripts/analyze-migration.py new file mode 100755 index 0000000..bf70749 --- /dev/null +++ b/scripts/analyze-migration.py @@ -0,0 +1,483 @@ +#!/usr/bin/env python +# +# Migration Stream Analyzer +# +# Copyright (c) 2013 Alexander Graf +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, see . + +import numpy as np +import json +import os +import argparse +import collections +import pprint + +class MigrationFile(object): + def __init__(self, filename): + self.filename = filename + self.file = open(self.filename, "rb") + + def read64(self): + return np.asscalar(np.fromfile(self.file, count=1, dtype='>i8')[0]) + + def read32(self): + return np.asscalar(np.fromfile(self.file, count=1, dtype='>i4')[0]) + + def read8(self): + return np.asscalar(np.fromfile(self.file, count=1, dtype='>i1')[0]) + + def readstr(self, len = None): + if len is None: + len = self.read8() + if len == 0: + return "" + return np.fromfile(self.file, count=1, dtype=('S%d' % len))[0] + + def readvar(self, size = None): + if size is None: + size = self.read8() + if size == 0: + return "" + value = self.file.read(size) + if len(value) != size: + raise Exception("Unexpected end of %s at 0x%x" % (self.filename, self.file.tell())) + return value + + # Search the current file from the current position onwards for a JSON + # migration descriptor. Returns the JSON string blob. + def read_migration_debug_json(self): + pos = self.file.tell() + data = self.file.read() + dbgpos = data.find("Debug Migration") + if dbgpos == -1: + raise Exception("No Debug Migration device found") + + # The full file read closed the file as well, reopen it where we were + self.file = open(self.filename, "rb") + self.file.seek(pos, 0) + + # We assume that our JSON blob starts after the "Debug Migration" magic + # and is null terminated. + return data[(dbgpos + 16):].split('\0',1)[0] + + def close(self): + self.file.close() + + +class RamSection(object): + RAM_SAVE_FLAG_COMPRESS = 0x02 + RAM_SAVE_FLAG_MEM_SIZE = 0x04 + RAM_SAVE_FLAG_PAGE = 0x08 + RAM_SAVE_FLAG_EOS = 0x10 + RAM_SAVE_FLAG_CONTINUE = 0x20 + RAM_SAVE_FLAG_XBZRLE = 0x40 + RAM_SAVE_FLAG_HOOK = 0x80 + # This can be dynamic, but all targets we care about have 4k pages + TARGET_PAGE_SIZE = 0x1000 + blocks = [] + + def __init__(self, file, version_id, device, section_key): + if version_id != 4: + raise Exception("Unknown RAM version %d" % version_id) + + self.file = file + self.section_key = section_key + + def read(self): + # Read all RAM sections + while True: + addr = self.file.read64() + flags = addr & 0xfff + addr &= 0xfffffffffffff000; + + if flags & self.RAM_SAVE_FLAG_MEM_SIZE: + while True: + namelen = self.file.read8() + # We assume that no RAM chunk is big enough to ever + # hit the first byte of the address, so when we see + # a zero here we know it has to be an address, not the + # length of the next block. + if namelen == 0: + self.file.file.seek(-1, 1) + break + name = self.file.readstr(len = namelen) + len = self.file.read64() + self.blocks.append((name, len)) + flags &= ~self.RAM_SAVE_FLAG_MEM_SIZE + + if flags & self.RAM_SAVE_FLAG_COMPRESS: + if flags & self.RAM_SAVE_FLAG_CONTINUE: + flags &= ~self.RAM_SAVE_FLAG_CONTINUE + else: + name = self.file.readstr() + fill_char = self.file.read8() + # The page in question would be filled with fill_char now + flags &= ~self.RAM_SAVE_FLAG_COMPRESS + elif flags & self.RAM_SAVE_FLAG_PAGE: + if flags & self.RAM_SAVE_FLAG_CONTINUE: + flags &= ~self.RAM_SAVE_FLAG_CONTINUE + else: + name = self.file.readstr() + # Just skip RAM data for now + self.file.file.seek(self.TARGET_PAGE_SIZE, 1) + flags &= ~self.RAM_SAVE_FLAG_PAGE + elif flags & self.RAM_SAVE_FLAG_XBZRLE: + raise Exception("XBZRLE RAM compression is not supported yet") + elif flags & self.RAM_SAVE_FLAG_HOOK: + raise Exception("RAM hooks don't make sense with files") + + # End of RAM section + if flags & self.RAM_SAVE_FLAG_EOS: + break + + if flags != 0: + raise Exception("Unknown RAM flags: %x" % flags) + + def getDict(self): + return "" + +class VMSDFieldGeneric(object): + def __init__(self, desc, file): + self.file = file + self.desc = desc + self.data = "" + + def __repr__(self): + return str(self.__str__()) + + def __str__(self): + return " ".join("{0:02x}".format(ord(c)) for c in self.data) + + def getDict(self): + return self.__str__() + + def read(self): + size = int(self.desc['size']) + self.data = self.file.readvar(size) + return self.data + +class VMSDFieldInt(VMSDFieldGeneric): + def __init__(self, desc, file): + super(VMSDFieldInt, self).__init__(desc, file) + self.size = int(desc['size']) + self.format = '0x%%0%dx' % (self.size * 2) + self.sdtype = '>i%d' % self.size + self.udtype = '>u%d' % self.size + + def __repr__(self): + if self.data < 0: + return ('%s (%d)' % ((self.format % self.udata), self.data)) + else: + return self.format % self.data + + def __str__(self): + return self.__repr__() + + def getDict(self): + return self.__str__() + + def read(self): + super(VMSDFieldInt, self).read() + self.sdata = np.fromstring(self.data, count=1, dtype=(self.sdtype))[0] + self.udata = np.fromstring(self.data, count=1, dtype=(self.udtype))[0] + self.data = self.sdata + return self.data + +class VMSDFieldUInt(VMSDFieldInt): + def __init__(self, desc, file): + super(VMSDFieldUInt, self).__init__(desc, file) + + def read(self): + super(VMSDFieldUInt, self).read() + self.data = self.udata + return self.data + +class VMSDFieldIntLE(VMSDFieldInt): + def __init__(self, desc, file): + super(VMSDFieldIntLE, self).__init__(desc, file) + self.dtype = '