Merge remote-tracking branch 'remotes/kraxel/tags/pull-vnc-20150312-1' into staging
[qemu/qmp-unstable.git] / scripts / analyze-migration.py
blob0c8b22f2aa9ec5240fe7735c37f955df8613e85d
1 #!/usr/bin/env python
3 # Migration Stream Analyzer
5 # Copyright (c) 2015 Alexander Graf <agraf@suse.de>
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 import numpy as np
21 import json
22 import os
23 import argparse
24 import collections
25 import pprint
27 def mkdir_p(path):
28 try:
29 os.makedirs(path)
30 except OSError:
31 pass
33 class MigrationFile(object):
34 def __init__(self, filename):
35 self.filename = filename
36 self.file = open(self.filename, "rb")
38 def read64(self):
39 return np.asscalar(np.fromfile(self.file, count=1, dtype='>i8')[0])
41 def read32(self):
42 return np.asscalar(np.fromfile(self.file, count=1, dtype='>i4')[0])
44 def read16(self):
45 return np.asscalar(np.fromfile(self.file, count=1, dtype='>i2')[0])
47 def read8(self):
48 return np.asscalar(np.fromfile(self.file, count=1, dtype='>i1')[0])
50 def readstr(self, len = None):
51 if len is None:
52 len = self.read8()
53 if len == 0:
54 return ""
55 return np.fromfile(self.file, count=1, dtype=('S%d' % len))[0]
57 def readvar(self, size = None):
58 if size is None:
59 size = self.read8()
60 if size == 0:
61 return ""
62 value = self.file.read(size)
63 if len(value) != size:
64 raise Exception("Unexpected end of %s at 0x%x" % (self.filename, self.file.tell()))
65 return value
67 def tell(self):
68 return self.file.tell()
70 # The VMSD description is at the end of the file, after EOF. Look for
71 # the last NULL byte, then for the beginning brace of JSON.
72 def read_migration_debug_json(self):
73 QEMU_VM_VMDESCRIPTION = 0x06
75 # Remember the offset in the file when we started
76 entrypos = self.file.tell()
78 # Read the last 10MB
79 self.file.seek(0, os.SEEK_END)
80 endpos = self.file.tell()
81 self.file.seek(max(-endpos, -10 * 1024 * 1024), os.SEEK_END)
82 datapos = self.file.tell()
83 data = self.file.read()
84 # The full file read closed the file as well, reopen it
85 self.file = open(self.filename, "rb")
87 # Find the last NULL byte, then the first brace after that. This should
88 # be the beginning of our JSON data.
89 nulpos = data.rfind("\0")
90 jsonpos = data.find("{", nulpos)
92 # Check backwards from there and see whether we guessed right
93 self.file.seek(datapos + jsonpos - 5, 0)
94 if self.read8() != QEMU_VM_VMDESCRIPTION:
95 raise Exception("No Debug Migration device found")
97 jsonlen = self.read32()
99 # Seek back to where we were at the beginning
100 self.file.seek(entrypos, 0)
102 return data[jsonpos:jsonpos + jsonlen]
104 def close(self):
105 self.file.close()
107 class RamSection(object):
108 RAM_SAVE_FLAG_COMPRESS = 0x02
109 RAM_SAVE_FLAG_MEM_SIZE = 0x04
110 RAM_SAVE_FLAG_PAGE = 0x08
111 RAM_SAVE_FLAG_EOS = 0x10
112 RAM_SAVE_FLAG_CONTINUE = 0x20
113 RAM_SAVE_FLAG_XBZRLE = 0x40
114 RAM_SAVE_FLAG_HOOK = 0x80
116 def __init__(self, file, version_id, ramargs, section_key):
117 if version_id != 4:
118 raise Exception("Unknown RAM version %d" % version_id)
120 self.file = file
121 self.section_key = section_key
122 self.TARGET_PAGE_SIZE = ramargs['page_size']
123 self.dump_memory = ramargs['dump_memory']
124 self.write_memory = ramargs['write_memory']
125 self.sizeinfo = collections.OrderedDict()
126 self.data = collections.OrderedDict()
127 self.data['section sizes'] = self.sizeinfo
128 self.name = ''
129 if self.write_memory:
130 self.files = { }
131 if self.dump_memory:
132 self.memory = collections.OrderedDict()
133 self.data['memory'] = self.memory
135 def __repr__(self):
136 return self.data.__repr__()
138 def __str__(self):
139 return self.data.__str__()
141 def getDict(self):
142 return self.data
144 def read(self):
145 # Read all RAM sections
146 while True:
147 addr = self.file.read64()
148 flags = addr & (self.TARGET_PAGE_SIZE - 1)
149 addr &= ~(self.TARGET_PAGE_SIZE - 1)
151 if flags & self.RAM_SAVE_FLAG_MEM_SIZE:
152 while True:
153 namelen = self.file.read8()
154 # We assume that no RAM chunk is big enough to ever
155 # hit the first byte of the address, so when we see
156 # a zero here we know it has to be an address, not the
157 # length of the next block.
158 if namelen == 0:
159 self.file.file.seek(-1, 1)
160 break
161 self.name = self.file.readstr(len = namelen)
162 len = self.file.read64()
163 self.sizeinfo[self.name] = '0x%016x' % len
164 if self.write_memory:
165 print self.name
166 mkdir_p('./' + os.path.dirname(self.name))
167 f = open('./' + self.name, "wb")
168 f.truncate(0)
169 f.truncate(len)
170 self.files[self.name] = f
171 flags &= ~self.RAM_SAVE_FLAG_MEM_SIZE
173 if flags & self.RAM_SAVE_FLAG_COMPRESS:
174 if flags & self.RAM_SAVE_FLAG_CONTINUE:
175 flags &= ~self.RAM_SAVE_FLAG_CONTINUE
176 else:
177 self.name = self.file.readstr()
178 fill_char = self.file.read8()
179 # The page in question is filled with fill_char now
180 if self.write_memory and fill_char != 0:
181 self.files[self.name].seek(addr, os.SEEK_SET)
182 self.files[self.name].write(chr(fill_char) * self.TARGET_PAGE_SIZE)
183 if self.dump_memory:
184 self.memory['%s (0x%016x)' % (self.name, addr)] = 'Filled with 0x%02x' % fill_char
185 flags &= ~self.RAM_SAVE_FLAG_COMPRESS
186 elif flags & self.RAM_SAVE_FLAG_PAGE:
187 if flags & self.RAM_SAVE_FLAG_CONTINUE:
188 flags &= ~self.RAM_SAVE_FLAG_CONTINUE
189 else:
190 self.name = self.file.readstr()
192 if self.write_memory or self.dump_memory:
193 data = self.file.readvar(size = self.TARGET_PAGE_SIZE)
194 else: # Just skip RAM data
195 self.file.file.seek(self.TARGET_PAGE_SIZE, 1)
197 if self.write_memory:
198 self.files[self.name].seek(addr, os.SEEK_SET)
199 self.files[self.name].write(data)
200 if self.dump_memory:
201 hexdata = " ".join("{0:02x}".format(ord(c)) for c in data)
202 self.memory['%s (0x%016x)' % (self.name, addr)] = hexdata
204 flags &= ~self.RAM_SAVE_FLAG_PAGE
205 elif flags & self.RAM_SAVE_FLAG_XBZRLE:
206 raise Exception("XBZRLE RAM compression is not supported yet")
207 elif flags & self.RAM_SAVE_FLAG_HOOK:
208 raise Exception("RAM hooks don't make sense with files")
210 # End of RAM section
211 if flags & self.RAM_SAVE_FLAG_EOS:
212 break
214 if flags != 0:
215 raise Exception("Unknown RAM flags: %x" % flags)
217 def __del__(self):
218 if self.write_memory:
219 for key in self.files:
220 self.files[key].close()
223 class HTABSection(object):
224 HASH_PTE_SIZE_64 = 16
226 def __init__(self, file, version_id, device, section_key):
227 if version_id != 1:
228 raise Exception("Unknown HTAB version %d" % version_id)
230 self.file = file
231 self.section_key = section_key
233 def read(self):
235 header = self.file.read32()
237 if (header > 0):
238 # First section, just the hash shift
239 return
241 # Read until end marker
242 while True:
243 index = self.file.read32()
244 n_valid = self.file.read16()
245 n_invalid = self.file.read16()
247 if index == 0 and n_valid == 0 and n_invalid == 0:
248 break
250 self.file.readvar(n_valid * self.HASH_PTE_SIZE_64)
252 def getDict(self):
253 return ""
255 class VMSDFieldGeneric(object):
256 def __init__(self, desc, file):
257 self.file = file
258 self.desc = desc
259 self.data = ""
261 def __repr__(self):
262 return str(self.__str__())
264 def __str__(self):
265 return " ".join("{0:02x}".format(ord(c)) for c in self.data)
267 def getDict(self):
268 return self.__str__()
270 def read(self):
271 size = int(self.desc['size'])
272 self.data = self.file.readvar(size)
273 return self.data
275 class VMSDFieldInt(VMSDFieldGeneric):
276 def __init__(self, desc, file):
277 super(VMSDFieldInt, self).__init__(desc, file)
278 self.size = int(desc['size'])
279 self.format = '0x%%0%dx' % (self.size * 2)
280 self.sdtype = '>i%d' % self.size
281 self.udtype = '>u%d' % self.size
283 def __repr__(self):
284 if self.data < 0:
285 return ('%s (%d)' % ((self.format % self.udata), self.data))
286 else:
287 return self.format % self.data
289 def __str__(self):
290 return self.__repr__()
292 def getDict(self):
293 return self.__str__()
295 def read(self):
296 super(VMSDFieldInt, self).read()
297 self.sdata = np.fromstring(self.data, count=1, dtype=(self.sdtype))[0]
298 self.udata = np.fromstring(self.data, count=1, dtype=(self.udtype))[0]
299 self.data = self.sdata
300 return self.data
302 class VMSDFieldUInt(VMSDFieldInt):
303 def __init__(self, desc, file):
304 super(VMSDFieldUInt, self).__init__(desc, file)
306 def read(self):
307 super(VMSDFieldUInt, self).read()
308 self.data = self.udata
309 return self.data
311 class VMSDFieldIntLE(VMSDFieldInt):
312 def __init__(self, desc, file):
313 super(VMSDFieldIntLE, self).__init__(desc, file)
314 self.dtype = '<i%d' % self.size
316 class VMSDFieldBool(VMSDFieldGeneric):
317 def __init__(self, desc, file):
318 super(VMSDFieldBool, self).__init__(desc, file)
320 def __repr__(self):
321 return self.data.__repr__()
323 def __str__(self):
324 return self.data.__str__()
326 def getDict(self):
327 return self.data
329 def read(self):
330 super(VMSDFieldBool, self).read()
331 if self.data[0] == 0:
332 self.data = False
333 else:
334 self.data = True
335 return self.data
337 class VMSDFieldStruct(VMSDFieldGeneric):
338 QEMU_VM_SUBSECTION = 0x05
340 def __init__(self, desc, file):
341 super(VMSDFieldStruct, self).__init__(desc, file)
342 self.data = collections.OrderedDict()
344 # When we see compressed array elements, unfold them here
345 new_fields = []
346 for field in self.desc['struct']['fields']:
347 if not 'array_len' in field:
348 new_fields.append(field)
349 continue
350 array_len = field.pop('array_len')
351 field['index'] = 0
352 new_fields.append(field)
353 for i in xrange(1, array_len):
354 c = field.copy()
355 c['index'] = i
356 new_fields.append(c)
358 self.desc['struct']['fields'] = new_fields
360 def __repr__(self):
361 return self.data.__repr__()
363 def __str__(self):
364 return self.data.__str__()
366 def read(self):
367 for field in self.desc['struct']['fields']:
368 try:
369 reader = vmsd_field_readers[field['type']]
370 except:
371 reader = VMSDFieldGeneric
373 field['data'] = reader(field, self.file)
374 field['data'].read()
376 if 'index' in field:
377 if field['name'] not in self.data:
378 self.data[field['name']] = []
379 a = self.data[field['name']]
380 if len(a) != int(field['index']):
381 raise Exception("internal index of data field unmatched (%d/%d)" % (len(a), int(field['index'])))
382 a.append(field['data'])
383 else:
384 self.data[field['name']] = field['data']
386 if 'subsections' in self.desc['struct']:
387 for subsection in self.desc['struct']['subsections']:
388 if self.file.read8() != self.QEMU_VM_SUBSECTION:
389 raise Exception("Subsection %s not found at offset %x" % ( subsection['vmsd_name'], self.file.tell()))
390 name = self.file.readstr()
391 version_id = self.file.read32()
392 self.data[name] = VMSDSection(self.file, version_id, subsection, (name, 0))
393 self.data[name].read()
395 def getDictItem(self, value):
396 # Strings would fall into the array category, treat
397 # them specially
398 if value.__class__ is ''.__class__:
399 return value
401 try:
402 return self.getDictOrderedDict(value)
403 except:
404 try:
405 return self.getDictArray(value)
406 except:
407 try:
408 return value.getDict()
409 except:
410 return value
412 def getDictArray(self, array):
413 r = []
414 for value in array:
415 r.append(self.getDictItem(value))
416 return r
418 def getDictOrderedDict(self, dict):
419 r = collections.OrderedDict()
420 for (key, value) in dict.items():
421 r[key] = self.getDictItem(value)
422 return r
424 def getDict(self):
425 return self.getDictOrderedDict(self.data)
427 vmsd_field_readers = {
428 "bool" : VMSDFieldBool,
429 "int8" : VMSDFieldInt,
430 "int16" : VMSDFieldInt,
431 "int32" : VMSDFieldInt,
432 "int32 equal" : VMSDFieldInt,
433 "int32 le" : VMSDFieldIntLE,
434 "int64" : VMSDFieldInt,
435 "uint8" : VMSDFieldUInt,
436 "uint16" : VMSDFieldUInt,
437 "uint32" : VMSDFieldUInt,
438 "uint32 equal" : VMSDFieldUInt,
439 "uint64" : VMSDFieldUInt,
440 "int64 equal" : VMSDFieldInt,
441 "uint8 equal" : VMSDFieldInt,
442 "uint16 equal" : VMSDFieldInt,
443 "float64" : VMSDFieldGeneric,
444 "timer" : VMSDFieldGeneric,
445 "buffer" : VMSDFieldGeneric,
446 "unused_buffer" : VMSDFieldGeneric,
447 "bitmap" : VMSDFieldGeneric,
448 "struct" : VMSDFieldStruct,
449 "unknown" : VMSDFieldGeneric,
452 class VMSDSection(VMSDFieldStruct):
453 def __init__(self, file, version_id, device, section_key):
454 self.file = file
455 self.data = ""
456 self.vmsd_name = ""
457 self.section_key = section_key
458 desc = device
459 if 'vmsd_name' in device:
460 self.vmsd_name = device['vmsd_name']
462 # A section really is nothing but a FieldStruct :)
463 super(VMSDSection, self).__init__({ 'struct' : desc }, file)
465 ###############################################################################
467 class MigrationDump(object):
468 QEMU_VM_FILE_MAGIC = 0x5145564d
469 QEMU_VM_FILE_VERSION = 0x00000003
470 QEMU_VM_EOF = 0x00
471 QEMU_VM_SECTION_START = 0x01
472 QEMU_VM_SECTION_PART = 0x02
473 QEMU_VM_SECTION_END = 0x03
474 QEMU_VM_SECTION_FULL = 0x04
475 QEMU_VM_SUBSECTION = 0x05
476 QEMU_VM_VMDESCRIPTION = 0x06
478 def __init__(self, filename):
479 self.section_classes = { ( 'ram', 0 ) : [ RamSection, None ],
480 ( 'spapr/htab', 0) : ( HTABSection, None ) }
481 self.filename = filename
482 self.vmsd_desc = None
484 def read(self, desc_only = False, dump_memory = False, write_memory = False):
485 # Read in the whole file
486 file = MigrationFile(self.filename)
488 # File magic
489 data = file.read32()
490 if data != self.QEMU_VM_FILE_MAGIC:
491 raise Exception("Invalid file magic %x" % data)
493 # Version (has to be v3)
494 data = file.read32()
495 if data != self.QEMU_VM_FILE_VERSION:
496 raise Exception("Invalid version number %d" % data)
498 self.load_vmsd_json(file)
500 # Read sections
501 self.sections = collections.OrderedDict()
503 if desc_only:
504 return
506 ramargs = {}
507 ramargs['page_size'] = self.vmsd_desc['page_size']
508 ramargs['dump_memory'] = dump_memory
509 ramargs['write_memory'] = write_memory
510 self.section_classes[('ram',0)][1] = ramargs
512 while True:
513 section_type = file.read8()
514 if section_type == self.QEMU_VM_EOF:
515 break
516 elif section_type == self.QEMU_VM_SECTION_START or section_type == self.QEMU_VM_SECTION_FULL:
517 section_id = file.read32()
518 name = file.readstr()
519 instance_id = file.read32()
520 version_id = file.read32()
521 section_key = (name, instance_id)
522 classdesc = self.section_classes[section_key]
523 section = classdesc[0](file, version_id, classdesc[1], section_key)
524 self.sections[section_id] = section
525 section.read()
526 elif section_type == self.QEMU_VM_SECTION_PART or section_type == self.QEMU_VM_SECTION_END:
527 section_id = file.read32()
528 self.sections[section_id].read()
529 else:
530 raise Exception("Unknown section type: %d" % section_type)
531 file.close()
533 def load_vmsd_json(self, file):
534 vmsd_json = file.read_migration_debug_json()
535 self.vmsd_desc = json.loads(vmsd_json, object_pairs_hook=collections.OrderedDict)
536 for device in self.vmsd_desc['devices']:
537 key = (device['name'], device['instance_id'])
538 value = ( VMSDSection, device )
539 self.section_classes[key] = value
541 def getDict(self):
542 r = collections.OrderedDict()
543 for (key, value) in self.sections.items():
544 key = "%s (%d)" % ( value.section_key[0], key )
545 r[key] = value.getDict()
546 return r
548 ###############################################################################
550 class JSONEncoder(json.JSONEncoder):
551 def default(self, o):
552 if isinstance(o, VMSDFieldGeneric):
553 return str(o)
554 return json.JSONEncoder.default(self, o)
556 parser = argparse.ArgumentParser()
557 parser.add_argument("-f", "--file", help='migration dump to read from', required=True)
558 parser.add_argument("-m", "--memory", help='dump RAM contents as well', action='store_true')
559 parser.add_argument("-d", "--dump", help='what to dump ("state" or "desc")', default='state')
560 parser.add_argument("-x", "--extract", help='extract contents into individual files', action='store_true')
561 args = parser.parse_args()
563 jsonenc = JSONEncoder(indent=4, separators=(',', ': '))
565 if args.extract:
566 dump = MigrationDump(args.file)
568 dump.read(desc_only = True)
569 print "desc.json"
570 f = open("desc.json", "wb")
571 f.truncate()
572 f.write(jsonenc.encode(dump.vmsd_desc))
573 f.close()
575 dump.read(write_memory = True)
576 dict = dump.getDict()
577 print "state.json"
578 f = open("state.json", "wb")
579 f.truncate()
580 f.write(jsonenc.encode(dict))
581 f.close()
582 elif args.dump == "state":
583 dump = MigrationDump(args.file)
584 dump.read(dump_memory = args.memory)
585 dict = dump.getDict()
586 print jsonenc.encode(dict)
587 elif args.dump == "desc":
588 dump = MigrationDump(args.file)
589 dump.read(desc_only = True)
590 print jsonenc.encode(dump.vmsd_desc)
591 else:
592 raise Exception("Please specify either -x, -d state or -d dump")