From cf4a33d116d3f6ce66027557814e3e729c00b5be Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Mon, 6 May 2013 17:12:25 +0300 Subject: [PATCH] BmapCreate: enable scalability optimization This patch changes the layout of the bmap file a little bit. Before this change, we wrote the mapped blocks count at the very end, because we only knew at the very end. In BmapCopy we need to know the amount of mapped blocks before we start copying, and this forces us to read entire bmap file to find out the amount of mapped blocks. This is not an issue when bmap file is small, but if it gets a lot bigger, this becomes a lot slower. In this patch, we change bmap file layout a little bit and now we put the mapped block cound at the beginning of the bmap file. This makes it possible to parse it more effeciently. This also makes it more human readable. Change-Id: Id9466ed4a45678a60506d32abe17845e29b79b59 Signed-off-by: Artem Bityutskiy --- bmaptools/BmapCreate.py | 55 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/bmaptools/BmapCreate.py b/bmaptools/BmapCreate.py index 34619c6..e85c2e3 100644 --- a/bmaptools/BmapCreate.py +++ b/bmaptools/BmapCreate.py @@ -34,7 +34,7 @@ from bmaptools.BmapHelpers import human_size from bmaptools import Fiemap # The bmap format version we generate -SUPPORTED_BMAP_VERSION = "1.2" +SUPPORTED_BMAP_VERSION = "1.3" _BMAP_START_TEMPLATE = \ """ @@ -61,7 +61,7 @@ _BMAP_START_TEMPLATE = \ in case of minor backward-compatible changes. --> - + %u @@ -70,10 +70,6 @@ _BMAP_START_TEMPLATE = \ %u - - """ class Error(Exception): @@ -131,6 +127,9 @@ class BmapCreate: self.mapped_size_human = None self.mapped_percent = None + self._mapped_count_pos1 = None + self._mapped_count_pos2 = None + self._f_image_needs_close = False self._f_bmap_needs_close = False @@ -163,10 +162,35 @@ class BmapCreate: """ A helper function which generates the starting contents of the block map file: the header comment, image size, block size, etc. """ + # We do not know the amount of mapped blocks at the moment, so just put + # whitespaces instead of real numbers. Assume the longest possible + # numbers. + mapped_count = ' ' * len(str(self.image_size)) + mapped_size_human = ' ' * len(self.image_size_human) + xml = _BMAP_START_TEMPLATE \ - % (SUPPORTED_BMAP_VERSION, - self.image_size_human, self.image_size, - self.block_size, self.blocks_cnt) + % (SUPPORTED_BMAP_VERSION, self.image_size_human, + self.image_size, self.block_size, self.blocks_cnt) + xml += " \n" % (mapped_size_human, 100.0) + xml += " " + + self._f_bmap.write(xml) + self._mapped_count_pos2 = self._f_bmap.tell() + + xml = "%s \n\n" % mapped_count + + # pylint: disable=C0301 + xml += " \n" + xml += " \n" + # pylint: enable=C0301 self._f_bmap.write(xml) @@ -175,15 +199,18 @@ class BmapCreate: file: the ending tags and the information about the amount of mapped blocks. """ - xml = " \n\n" - xml += " \n" \ - % (self.mapped_size_human, self.mapped_percent) - xml += " %u \n" \ - % self.mapped_cnt + xml = " \n" xml += "\n" self._f_bmap.write(xml) + self._f_bmap.seek(self._mapped_count_pos1) + self._f_bmap.write("%s or %.1f%%" % \ + (self.mapped_size_human, self.mapped_percent)) + + self._f_bmap.seek(self._mapped_count_pos2) + self._f_bmap.write("%u" % self.mapped_cnt) + def _calculate_sha1(self, first, last): """ A helper function which calculates SHA1 checksum for the range of blocks of the image file: from block 'first' to block 'last'. """ -- 2.7.4