blob: b5a8cf03c5ceec7c0daab56e3f58e130cf60752d [file] [log] [blame]
Doug Zongker424296a2014-09-02 08:53:09 -07001# Copyright (C) 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Doug Zongkerfc44a512014-08-26 13:10:25 -070015from __future__ import print_function
16
17from collections import deque, OrderedDict
18from hashlib import sha1
Doug Zongker2d2dd152016-02-09 08:28:09 -080019import array
Tao Bao8dcf7382015-05-21 14:09:49 -070020import common
Doug Zongker2d2dd152016-02-09 08:28:09 -080021import functools
Doug Zongker62338182014-09-08 08:29:55 -070022import heapq
Doug Zongkerfc44a512014-08-26 13:10:25 -070023import itertools
24import multiprocessing
25import os
Doug Zongkerfc44a512014-08-26 13:10:25 -070026import re
27import subprocess
Doug Zongkerfc44a512014-08-26 13:10:25 -070028import threading
Doug Zongker2d2dd152016-02-09 08:28:09 -080029import time
Doug Zongkerfc44a512014-08-26 13:10:25 -070030import tempfile
31
Dan Albert8b72aef2015-03-23 19:13:21 -070032from rangelib import RangeSet
33
Doug Zongkerfc44a512014-08-26 13:10:25 -070034
Doug Zongkerab7ca1d2014-08-26 10:40:28 -070035__all__ = ["EmptyImage", "DataImage", "BlockImageDiff"]
36
Dan Albert8b72aef2015-03-23 19:13:21 -070037
Doug Zongkerfc44a512014-08-26 13:10:25 -070038def compute_patch(src, tgt, imgdiff=False):
39 srcfd, srcfile = tempfile.mkstemp(prefix="src-")
40 tgtfd, tgtfile = tempfile.mkstemp(prefix="tgt-")
41 patchfd, patchfile = tempfile.mkstemp(prefix="patch-")
42 os.close(patchfd)
43
44 try:
45 with os.fdopen(srcfd, "wb") as f_src:
46 for p in src:
47 f_src.write(p)
48
49 with os.fdopen(tgtfd, "wb") as f_tgt:
50 for p in tgt:
51 f_tgt.write(p)
52 try:
53 os.unlink(patchfile)
54 except OSError:
55 pass
56 if imgdiff:
57 p = subprocess.call(["imgdiff", "-z", srcfile, tgtfile, patchfile],
58 stdout=open("/dev/null", "a"),
59 stderr=subprocess.STDOUT)
60 else:
61 p = subprocess.call(["bsdiff", srcfile, tgtfile, patchfile])
62
63 if p:
64 raise ValueError("diff failed: " + str(p))
65
66 with open(patchfile, "rb") as f:
67 return f.read()
68 finally:
69 try:
70 os.unlink(srcfile)
71 os.unlink(tgtfile)
72 os.unlink(patchfile)
73 except OSError:
74 pass
75
Dan Albert8b72aef2015-03-23 19:13:21 -070076
77class Image(object):
78 def ReadRangeSet(self, ranges):
79 raise NotImplementedError
80
Tao Bao68658c02015-06-01 13:40:49 -070081 def TotalSha1(self, include_clobbered_blocks=False):
Dan Albert8b72aef2015-03-23 19:13:21 -070082 raise NotImplementedError
83
84
85class EmptyImage(Image):
Doug Zongkerfc44a512014-08-26 13:10:25 -070086 """A zero-length image."""
87 blocksize = 4096
88 care_map = RangeSet()
Tao Baoff777812015-05-12 11:42:31 -070089 clobbered_blocks = RangeSet()
Tao Baoe9b61912015-07-09 17:37:49 -070090 extended = RangeSet()
Doug Zongkerfc44a512014-08-26 13:10:25 -070091 total_blocks = 0
92 file_map = {}
93 def ReadRangeSet(self, ranges):
94 return ()
Tao Bao68658c02015-06-01 13:40:49 -070095 def TotalSha1(self, include_clobbered_blocks=False):
96 # EmptyImage always carries empty clobbered_blocks, so
97 # include_clobbered_blocks can be ignored.
98 assert self.clobbered_blocks.size() == 0
Doug Zongkerab7ca1d2014-08-26 10:40:28 -070099 return sha1().hexdigest()
100
101
Dan Albert8b72aef2015-03-23 19:13:21 -0700102class DataImage(Image):
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700103 """An image wrapped around a single string of data."""
104
105 def __init__(self, data, trim=False, pad=False):
106 self.data = data
107 self.blocksize = 4096
108
109 assert not (trim and pad)
110
111 partial = len(self.data) % self.blocksize
Tao Bao7589e962015-09-05 20:35:32 -0700112 padded = False
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700113 if partial > 0:
114 if trim:
115 self.data = self.data[:-partial]
116 elif pad:
117 self.data += '\0' * (self.blocksize - partial)
Tao Bao7589e962015-09-05 20:35:32 -0700118 padded = True
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700119 else:
120 raise ValueError(("data for DataImage must be multiple of %d bytes "
121 "unless trim or pad is specified") %
122 (self.blocksize,))
123
124 assert len(self.data) % self.blocksize == 0
125
126 self.total_blocks = len(self.data) / self.blocksize
127 self.care_map = RangeSet(data=(0, self.total_blocks))
Tao Bao7589e962015-09-05 20:35:32 -0700128 # When the last block is padded, we always write the whole block even for
129 # incremental OTAs. Because otherwise the last block may get skipped if
130 # unchanged for an incremental, but would fail the post-install
131 # verification if it has non-zero contents in the padding bytes.
132 # Bug: 23828506
133 if padded:
Tao Bao42206c32015-09-08 13:39:40 -0700134 clobbered_blocks = [self.total_blocks-1, self.total_blocks]
Tao Bao7589e962015-09-05 20:35:32 -0700135 else:
Tao Bao42206c32015-09-08 13:39:40 -0700136 clobbered_blocks = []
137 self.clobbered_blocks = clobbered_blocks
Tao Baoe9b61912015-07-09 17:37:49 -0700138 self.extended = RangeSet()
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700139
140 zero_blocks = []
141 nonzero_blocks = []
142 reference = '\0' * self.blocksize
143
Tao Bao7589e962015-09-05 20:35:32 -0700144 for i in range(self.total_blocks-1 if padded else self.total_blocks):
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700145 d = self.data[i*self.blocksize : (i+1)*self.blocksize]
146 if d == reference:
147 zero_blocks.append(i)
148 zero_blocks.append(i+1)
149 else:
150 nonzero_blocks.append(i)
151 nonzero_blocks.append(i+1)
152
Tao Bao42206c32015-09-08 13:39:40 -0700153 assert zero_blocks or nonzero_blocks or clobbered_blocks
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700154
Tao Bao42206c32015-09-08 13:39:40 -0700155 self.file_map = dict()
156 if zero_blocks:
157 self.file_map["__ZERO"] = RangeSet(data=zero_blocks)
158 if nonzero_blocks:
159 self.file_map["__NONZERO"] = RangeSet(data=nonzero_blocks)
160 if clobbered_blocks:
161 self.file_map["__COPY"] = RangeSet(data=clobbered_blocks)
Tao Bao7589e962015-09-05 20:35:32 -0700162
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700163 def ReadRangeSet(self, ranges):
164 return [self.data[s*self.blocksize:e*self.blocksize] for (s, e) in ranges]
165
Tao Bao68658c02015-06-01 13:40:49 -0700166 def TotalSha1(self, include_clobbered_blocks=False):
Tao Bao7589e962015-09-05 20:35:32 -0700167 if not include_clobbered_blocks:
168 ranges = self.care_map.subtract(self.clobbered_blocks)
169 return sha1(self.ReadRangeSet(ranges)).hexdigest()
170 else:
171 return sha1(self.data).hexdigest()
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700172
Doug Zongkerfc44a512014-08-26 13:10:25 -0700173
174class Transfer(object):
175 def __init__(self, tgt_name, src_name, tgt_ranges, src_ranges, style, by_id):
176 self.tgt_name = tgt_name
177 self.src_name = src_name
178 self.tgt_ranges = tgt_ranges
179 self.src_ranges = src_ranges
180 self.style = style
181 self.intact = (getattr(tgt_ranges, "monotonic", False) and
182 getattr(src_ranges, "monotonic", False))
Tao Baob8c87172015-03-19 19:42:12 -0700183
184 # We use OrderedDict rather than dict so that the output is repeatable;
185 # otherwise it would depend on the hash values of the Transfer objects.
186 self.goes_before = OrderedDict()
187 self.goes_after = OrderedDict()
Doug Zongkerfc44a512014-08-26 13:10:25 -0700188
Doug Zongker62338182014-09-08 08:29:55 -0700189 self.stash_before = []
190 self.use_stash = []
191
Doug Zongkerfc44a512014-08-26 13:10:25 -0700192 self.id = len(by_id)
193 by_id.append(self)
194
Doug Zongker62338182014-09-08 08:29:55 -0700195 def NetStashChange(self):
196 return (sum(sr.size() for (_, sr) in self.stash_before) -
197 sum(sr.size() for (_, sr) in self.use_stash))
198
Tao Bao82c47982015-08-17 09:45:13 -0700199 def ConvertToNew(self):
200 assert self.style != "new"
201 self.use_stash = []
202 self.style = "new"
203 self.src_ranges = RangeSet()
204
Doug Zongkerfc44a512014-08-26 13:10:25 -0700205 def __str__(self):
206 return (str(self.id) + ": <" + str(self.src_ranges) + " " + self.style +
207 " to " + str(self.tgt_ranges) + ">")
208
209
Doug Zongker2d2dd152016-02-09 08:28:09 -0800210@functools.total_ordering
211class HeapItem(object):
212 def __init__(self, item):
213 self.item = item
214 # Negate the score since python's heap is a min-heap and we want
215 # the maximum score.
216 self.score = -item.score
217 def clear(self):
218 self.item = None
219 def __bool__(self):
220 return self.item is None
221 def __eq__(self, other):
222 return self.score == other.score
223 def __le__(self, other):
224 return self.score <= other.score
225
226
Doug Zongkerfc44a512014-08-26 13:10:25 -0700227# BlockImageDiff works on two image objects. An image object is
228# anything that provides the following attributes:
229#
230# blocksize: the size in bytes of a block, currently must be 4096.
231#
232# total_blocks: the total size of the partition/image, in blocks.
233#
234# care_map: a RangeSet containing which blocks (in the range [0,
235# total_blocks) we actually care about; i.e. which blocks contain
236# data.
237#
238# file_map: a dict that partitions the blocks contained in care_map
239# into smaller domains that are useful for doing diffs on.
240# (Typically a domain is a file, and the key in file_map is the
241# pathname.)
242#
Tao Baoff777812015-05-12 11:42:31 -0700243# clobbered_blocks: a RangeSet containing which blocks contain data
244# but may be altered by the FS. They need to be excluded when
245# verifying the partition integrity.
246#
Doug Zongkerfc44a512014-08-26 13:10:25 -0700247# ReadRangeSet(): a function that takes a RangeSet and returns the
248# data contained in the image blocks of that RangeSet. The data
249# is returned as a list or tuple of strings; concatenating the
250# elements together should produce the requested data.
251# Implementations are free to break up the data into list/tuple
252# elements in any way that is convenient.
253#
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700254# TotalSha1(): a function that returns (as a hex string) the SHA-1
255# hash of all the data in the image (ie, all the blocks in the
Tao Bao68658c02015-06-01 13:40:49 -0700256# care_map minus clobbered_blocks, or including the clobbered
257# blocks if include_clobbered_blocks is True).
Doug Zongkerab7ca1d2014-08-26 10:40:28 -0700258#
Doug Zongkerfc44a512014-08-26 13:10:25 -0700259# When creating a BlockImageDiff, the src image may be None, in which
260# case the list of transfers produced will never read from the
261# original image.
262
263class BlockImageDiff(object):
Tao Baob6568cd2016-06-11 12:19:23 -0700264 def __init__(self, tgt, src=None, threads=None, version=4,
265 disable_imgdiff=False):
Doug Zongkerfc44a512014-08-26 13:10:25 -0700266 if threads is None:
267 threads = multiprocessing.cpu_count() // 2
Dan Albert8b72aef2015-03-23 19:13:21 -0700268 if threads == 0:
269 threads = 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700270 self.threads = threads
Doug Zongker62338182014-09-08 08:29:55 -0700271 self.version = version
Dan Albert8b72aef2015-03-23 19:13:21 -0700272 self.transfers = []
273 self.src_basenames = {}
274 self.src_numpatterns = {}
Tao Baod8d14be2016-02-04 14:26:02 -0800275 self._max_stashed_size = 0
Tao Baod4caaae2016-04-12 15:53:16 -0700276 self.touched_src_ranges = RangeSet()
277 self.touched_src_sha1 = None
Tao Baob6568cd2016-06-11 12:19:23 -0700278 self.disable_imgdiff = disable_imgdiff
Doug Zongker62338182014-09-08 08:29:55 -0700279
Tao Baoeba409c2015-10-21 13:30:43 -0700280 assert version in (1, 2, 3, 4)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700281
282 self.tgt = tgt
283 if src is None:
284 src = EmptyImage()
285 self.src = src
286
287 # The updater code that installs the patch always uses 4k blocks.
288 assert tgt.blocksize == 4096
289 assert src.blocksize == 4096
290
291 # The range sets in each filemap should comprise a partition of
292 # the care map.
293 self.AssertPartition(src.care_map, src.file_map.values())
294 self.AssertPartition(tgt.care_map, tgt.file_map.values())
295
Tao Baod8d14be2016-02-04 14:26:02 -0800296 @property
297 def max_stashed_size(self):
298 return self._max_stashed_size
299
Doug Zongkerfc44a512014-08-26 13:10:25 -0700300 def Compute(self, prefix):
301 # When looking for a source file to use as the diff input for a
302 # target file, we try:
303 # 1) an exact path match if available, otherwise
304 # 2) a exact basename match if available, otherwise
305 # 3) a basename match after all runs of digits are replaced by
306 # "#" if available, otherwise
307 # 4) we have no source for this target.
308 self.AbbreviateSourceNames()
309 self.FindTransfers()
310
311 # Find the ordering dependencies among transfers (this is O(n^2)
312 # in the number of transfers).
313 self.GenerateDigraph()
314 # Find a sequence of transfers that satisfies as many ordering
315 # dependencies as possible (heuristically).
316 self.FindVertexSequence()
317 # Fix up the ordering dependencies that the sequence didn't
318 # satisfy.
Doug Zongker62338182014-09-08 08:29:55 -0700319 if self.version == 1:
320 self.RemoveBackwardEdges()
321 else:
322 self.ReverseBackwardEdges()
323 self.ImproveVertexSequence()
324
Tao Bao82c47982015-08-17 09:45:13 -0700325 # Ensure the runtime stash size is under the limit.
326 if self.version >= 2 and common.OPTIONS.cache_size is not None:
327 self.ReviseStashSize()
328
Doug Zongkerfc44a512014-08-26 13:10:25 -0700329 # Double-check our work.
330 self.AssertSequenceGood()
331
332 self.ComputePatches(prefix)
333 self.WriteTransfers(prefix)
334
Dan Albert8b72aef2015-03-23 19:13:21 -0700335 def HashBlocks(self, source, ranges): # pylint: disable=no-self-use
Sami Tolvanendd67a292014-12-09 16:40:34 +0000336 data = source.ReadRangeSet(ranges)
337 ctx = sha1()
338
339 for p in data:
340 ctx.update(p)
341
342 return ctx.hexdigest()
343
Doug Zongkerfc44a512014-08-26 13:10:25 -0700344 def WriteTransfers(self, prefix):
345 out = []
346
Doug Zongkerfc44a512014-08-26 13:10:25 -0700347 total = 0
Doug Zongkerfc44a512014-08-26 13:10:25 -0700348
Doug Zongker62338182014-09-08 08:29:55 -0700349 stashes = {}
350 stashed_blocks = 0
351 max_stashed_blocks = 0
352
353 free_stash_ids = []
354 next_stash_id = 0
355
Doug Zongkerfc44a512014-08-26 13:10:25 -0700356 for xf in self.transfers:
357
Doug Zongker62338182014-09-08 08:29:55 -0700358 if self.version < 2:
359 assert not xf.stash_before
360 assert not xf.use_stash
361
362 for s, sr in xf.stash_before:
363 assert s not in stashes
364 if free_stash_ids:
365 sid = heapq.heappop(free_stash_ids)
366 else:
367 sid = next_stash_id
368 next_stash_id += 1
369 stashes[s] = sid
Sami Tolvanendd67a292014-12-09 16:40:34 +0000370 if self.version == 2:
caozhiyuan21b37d82015-10-21 15:14:03 +0800371 stashed_blocks += sr.size()
Sami Tolvanendd67a292014-12-09 16:40:34 +0000372 out.append("stash %d %s\n" % (sid, sr.to_string_raw()))
373 else:
374 sh = self.HashBlocks(self.src, sr)
375 if sh in stashes:
376 stashes[sh] += 1
377 else:
378 stashes[sh] = 1
caozhiyuan21b37d82015-10-21 15:14:03 +0800379 stashed_blocks += sr.size()
Tao Baod4caaae2016-04-12 15:53:16 -0700380 self.touched_src_ranges = self.touched_src_ranges.union(sr)
Sami Tolvanendd67a292014-12-09 16:40:34 +0000381 out.append("stash %s %s\n" % (sh, sr.to_string_raw()))
Doug Zongker62338182014-09-08 08:29:55 -0700382
383 if stashed_blocks > max_stashed_blocks:
384 max_stashed_blocks = stashed_blocks
385
Jesse Zhao7b985f62015-03-02 16:53:08 -0800386 free_string = []
caozhiyuan21b37d82015-10-21 15:14:03 +0800387 free_size = 0
Jesse Zhao7b985f62015-03-02 16:53:08 -0800388
Doug Zongker62338182014-09-08 08:29:55 -0700389 if self.version == 1:
Tao Bao4fcb77e2015-10-21 13:36:01 -0700390 src_str = xf.src_ranges.to_string_raw() if xf.src_ranges else ""
Sami Tolvanendd67a292014-12-09 16:40:34 +0000391 elif self.version >= 2:
Doug Zongker62338182014-09-08 08:29:55 -0700392
393 # <# blocks> <src ranges>
394 # OR
395 # <# blocks> <src ranges> <src locs> <stash refs...>
396 # OR
397 # <# blocks> - <stash refs...>
398
399 size = xf.src_ranges.size()
Dan Albert8b72aef2015-03-23 19:13:21 -0700400 src_str = [str(size)]
Doug Zongker62338182014-09-08 08:29:55 -0700401
402 unstashed_src_ranges = xf.src_ranges
403 mapped_stashes = []
404 for s, sr in xf.use_stash:
405 sid = stashes.pop(s)
Doug Zongker62338182014-09-08 08:29:55 -0700406 unstashed_src_ranges = unstashed_src_ranges.subtract(sr)
Sami Tolvanendd67a292014-12-09 16:40:34 +0000407 sh = self.HashBlocks(self.src, sr)
Doug Zongker62338182014-09-08 08:29:55 -0700408 sr = xf.src_ranges.map_within(sr)
409 mapped_stashes.append(sr)
Sami Tolvanendd67a292014-12-09 16:40:34 +0000410 if self.version == 2:
Dan Albert8b72aef2015-03-23 19:13:21 -0700411 src_str.append("%d:%s" % (sid, sr.to_string_raw()))
Tao Baobb625d22015-08-13 14:44:15 -0700412 # A stash will be used only once. We need to free the stash
413 # immediately after the use, instead of waiting for the automatic
414 # clean-up at the end. Because otherwise it may take up extra space
415 # and lead to OTA failures.
416 # Bug: 23119955
417 free_string.append("free %d\n" % (sid,))
caozhiyuan21b37d82015-10-21 15:14:03 +0800418 free_size += sr.size()
Sami Tolvanendd67a292014-12-09 16:40:34 +0000419 else:
420 assert sh in stashes
Dan Albert8b72aef2015-03-23 19:13:21 -0700421 src_str.append("%s:%s" % (sh, sr.to_string_raw()))
Sami Tolvanendd67a292014-12-09 16:40:34 +0000422 stashes[sh] -= 1
423 if stashes[sh] == 0:
caozhiyuan21b37d82015-10-21 15:14:03 +0800424 free_size += sr.size()
Sami Tolvanendd67a292014-12-09 16:40:34 +0000425 free_string.append("free %s\n" % (sh))
426 stashes.pop(sh)
Doug Zongker62338182014-09-08 08:29:55 -0700427 heapq.heappush(free_stash_ids, sid)
428
429 if unstashed_src_ranges:
Dan Albert8b72aef2015-03-23 19:13:21 -0700430 src_str.insert(1, unstashed_src_ranges.to_string_raw())
Doug Zongker62338182014-09-08 08:29:55 -0700431 if xf.use_stash:
432 mapped_unstashed = xf.src_ranges.map_within(unstashed_src_ranges)
Dan Albert8b72aef2015-03-23 19:13:21 -0700433 src_str.insert(2, mapped_unstashed.to_string_raw())
Doug Zongker62338182014-09-08 08:29:55 -0700434 mapped_stashes.append(mapped_unstashed)
435 self.AssertPartition(RangeSet(data=(0, size)), mapped_stashes)
436 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700437 src_str.insert(1, "-")
Doug Zongker62338182014-09-08 08:29:55 -0700438 self.AssertPartition(RangeSet(data=(0, size)), mapped_stashes)
439
Dan Albert8b72aef2015-03-23 19:13:21 -0700440 src_str = " ".join(src_str)
Doug Zongker62338182014-09-08 08:29:55 -0700441
Sami Tolvanendd67a292014-12-09 16:40:34 +0000442 # all versions:
Doug Zongker62338182014-09-08 08:29:55 -0700443 # zero <rangeset>
444 # new <rangeset>
445 # erase <rangeset>
446 #
447 # version 1:
448 # bsdiff patchstart patchlen <src rangeset> <tgt rangeset>
449 # imgdiff patchstart patchlen <src rangeset> <tgt rangeset>
450 # move <src rangeset> <tgt rangeset>
451 #
452 # version 2:
Dan Albert8b72aef2015-03-23 19:13:21 -0700453 # bsdiff patchstart patchlen <tgt rangeset> <src_str>
454 # imgdiff patchstart patchlen <tgt rangeset> <src_str>
455 # move <tgt rangeset> <src_str>
Sami Tolvanendd67a292014-12-09 16:40:34 +0000456 #
457 # version 3:
Dan Albert8b72aef2015-03-23 19:13:21 -0700458 # bsdiff patchstart patchlen srchash tgthash <tgt rangeset> <src_str>
459 # imgdiff patchstart patchlen srchash tgthash <tgt rangeset> <src_str>
460 # move hash <tgt rangeset> <src_str>
Doug Zongkerfc44a512014-08-26 13:10:25 -0700461
462 tgt_size = xf.tgt_ranges.size()
463
464 if xf.style == "new":
465 assert xf.tgt_ranges
466 out.append("%s %s\n" % (xf.style, xf.tgt_ranges.to_string_raw()))
467 total += tgt_size
468 elif xf.style == "move":
Doug Zongkerfc44a512014-08-26 13:10:25 -0700469 assert xf.tgt_ranges
470 assert xf.src_ranges.size() == tgt_size
471 if xf.src_ranges != xf.tgt_ranges:
Doug Zongker62338182014-09-08 08:29:55 -0700472 if self.version == 1:
473 out.append("%s %s %s\n" % (
474 xf.style,
475 xf.src_ranges.to_string_raw(), xf.tgt_ranges.to_string_raw()))
476 elif self.version == 2:
477 out.append("%s %s %s\n" % (
478 xf.style,
Dan Albert8b72aef2015-03-23 19:13:21 -0700479 xf.tgt_ranges.to_string_raw(), src_str))
Sami Tolvanendd67a292014-12-09 16:40:34 +0000480 elif self.version >= 3:
Sami Tolvanen29f529f2015-04-17 16:28:08 +0100481 # take into account automatic stashing of overlapping blocks
482 if xf.src_ranges.overlaps(xf.tgt_ranges):
Tao Baoe9b61912015-07-09 17:37:49 -0700483 temp_stash_usage = stashed_blocks + xf.src_ranges.size()
Sami Tolvanen29f529f2015-04-17 16:28:08 +0100484 if temp_stash_usage > max_stashed_blocks:
485 max_stashed_blocks = temp_stash_usage
486
Tao Baod4caaae2016-04-12 15:53:16 -0700487 self.touched_src_ranges = self.touched_src_ranges.union(
488 xf.src_ranges)
489
Sami Tolvanendd67a292014-12-09 16:40:34 +0000490 out.append("%s %s %s %s\n" % (
491 xf.style,
492 self.HashBlocks(self.tgt, xf.tgt_ranges),
Dan Albert8b72aef2015-03-23 19:13:21 -0700493 xf.tgt_ranges.to_string_raw(), src_str))
Doug Zongkerfc44a512014-08-26 13:10:25 -0700494 total += tgt_size
495 elif xf.style in ("bsdiff", "imgdiff"):
Doug Zongkerfc44a512014-08-26 13:10:25 -0700496 assert xf.tgt_ranges
497 assert xf.src_ranges
Doug Zongker62338182014-09-08 08:29:55 -0700498 if self.version == 1:
499 out.append("%s %d %d %s %s\n" % (
500 xf.style, xf.patch_start, xf.patch_len,
501 xf.src_ranges.to_string_raw(), xf.tgt_ranges.to_string_raw()))
502 elif self.version == 2:
503 out.append("%s %d %d %s %s\n" % (
504 xf.style, xf.patch_start, xf.patch_len,
Dan Albert8b72aef2015-03-23 19:13:21 -0700505 xf.tgt_ranges.to_string_raw(), src_str))
Sami Tolvanendd67a292014-12-09 16:40:34 +0000506 elif self.version >= 3:
Sami Tolvanen29f529f2015-04-17 16:28:08 +0100507 # take into account automatic stashing of overlapping blocks
508 if xf.src_ranges.overlaps(xf.tgt_ranges):
Tao Baoe9b61912015-07-09 17:37:49 -0700509 temp_stash_usage = stashed_blocks + xf.src_ranges.size()
Sami Tolvanen29f529f2015-04-17 16:28:08 +0100510 if temp_stash_usage > max_stashed_blocks:
511 max_stashed_blocks = temp_stash_usage
512
Tao Baod4caaae2016-04-12 15:53:16 -0700513 self.touched_src_ranges = self.touched_src_ranges.union(
514 xf.src_ranges)
515
Sami Tolvanendd67a292014-12-09 16:40:34 +0000516 out.append("%s %d %d %s %s %s %s\n" % (
517 xf.style,
518 xf.patch_start, xf.patch_len,
519 self.HashBlocks(self.src, xf.src_ranges),
520 self.HashBlocks(self.tgt, xf.tgt_ranges),
Dan Albert8b72aef2015-03-23 19:13:21 -0700521 xf.tgt_ranges.to_string_raw(), src_str))
Doug Zongkerfc44a512014-08-26 13:10:25 -0700522 total += tgt_size
523 elif xf.style == "zero":
524 assert xf.tgt_ranges
525 to_zero = xf.tgt_ranges.subtract(xf.src_ranges)
526 if to_zero:
527 out.append("%s %s\n" % (xf.style, to_zero.to_string_raw()))
528 total += to_zero.size()
529 else:
Dan Albert8b72aef2015-03-23 19:13:21 -0700530 raise ValueError("unknown transfer style '%s'\n" % xf.style)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700531
Sami Tolvanendd67a292014-12-09 16:40:34 +0000532 if free_string:
533 out.append("".join(free_string))
caozhiyuan21b37d82015-10-21 15:14:03 +0800534 stashed_blocks -= free_size
Sami Tolvanendd67a292014-12-09 16:40:34 +0000535
Tao Bao575d68a2015-08-07 19:49:45 -0700536 if self.version >= 2 and common.OPTIONS.cache_size is not None:
Tao Bao8dcf7382015-05-21 14:09:49 -0700537 # Sanity check: abort if we're going to need more stash space than
538 # the allowed size (cache_size * threshold). There are two purposes
539 # of having a threshold here. a) Part of the cache may have been
540 # occupied by some recovery logs. b) It will buy us some time to deal
541 # with the oversize issue.
542 cache_size = common.OPTIONS.cache_size
543 stash_threshold = common.OPTIONS.stash_threshold
544 max_allowed = cache_size * stash_threshold
545 assert max_stashed_blocks * self.tgt.blocksize < max_allowed, \
546 'Stash size %d (%d * %d) exceeds the limit %d (%d * %.2f)' % (
547 max_stashed_blocks * self.tgt.blocksize, max_stashed_blocks,
548 self.tgt.blocksize, max_allowed, cache_size,
549 stash_threshold)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700550
Tao Baod4caaae2016-04-12 15:53:16 -0700551 if self.version >= 3:
552 self.touched_src_sha1 = self.HashBlocks(
553 self.src, self.touched_src_ranges)
554
Tao Baoe9b61912015-07-09 17:37:49 -0700555 # Zero out extended blocks as a workaround for bug 20881595.
556 if self.tgt.extended:
557 out.append("zero %s\n" % (self.tgt.extended.to_string_raw(),))
Tao Baob32d56e2015-09-09 11:55:01 -0700558 total += self.tgt.extended.size()
Tao Baoe9b61912015-07-09 17:37:49 -0700559
560 # We erase all the blocks on the partition that a) don't contain useful
561 # data in the new image and b) will not be touched by dm-verity.
Doug Zongkerfc44a512014-08-26 13:10:25 -0700562 all_tgt = RangeSet(data=(0, self.tgt.total_blocks))
Tao Baoe9b61912015-07-09 17:37:49 -0700563 all_tgt_minus_extended = all_tgt.subtract(self.tgt.extended)
564 new_dontcare = all_tgt_minus_extended.subtract(self.tgt.care_map)
565 if new_dontcare:
566 out.append("erase %s\n" % (new_dontcare.to_string_raw(),))
Doug Zongkere985f6f2014-09-09 12:38:47 -0700567
568 out.insert(0, "%d\n" % (self.version,)) # format version number
Tao Baob32d56e2015-09-09 11:55:01 -0700569 out.insert(1, "%d\n" % (total,))
Doug Zongkere985f6f2014-09-09 12:38:47 -0700570 if self.version >= 2:
571 # version 2 only: after the total block count, we give the number
572 # of stash slots needed, and the maximum size needed (in blocks)
573 out.insert(2, str(next_stash_id) + "\n")
574 out.insert(3, str(max_stashed_blocks) + "\n")
Doug Zongkerfc44a512014-08-26 13:10:25 -0700575
576 with open(prefix + ".transfer.list", "wb") as f:
577 for i in out:
578 f.write(i)
579
Doug Zongker62338182014-09-08 08:29:55 -0700580 if self.version >= 2:
Tao Baod8d14be2016-02-04 14:26:02 -0800581 self._max_stashed_size = max_stashed_blocks * self.tgt.blocksize
Tao Bao575d68a2015-08-07 19:49:45 -0700582 OPTIONS = common.OPTIONS
583 if OPTIONS.cache_size is not None:
584 max_allowed = OPTIONS.cache_size * OPTIONS.stash_threshold
585 print("max stashed blocks: %d (%d bytes), "
586 "limit: %d bytes (%.2f%%)\n" % (
Tao Baod8d14be2016-02-04 14:26:02 -0800587 max_stashed_blocks, self._max_stashed_size, max_allowed,
588 self._max_stashed_size * 100.0 / max_allowed))
Tao Bao575d68a2015-08-07 19:49:45 -0700589 else:
590 print("max stashed blocks: %d (%d bytes), limit: <unknown>\n" % (
Tao Baod8d14be2016-02-04 14:26:02 -0800591 max_stashed_blocks, self._max_stashed_size))
Doug Zongker62338182014-09-08 08:29:55 -0700592
Tao Bao82c47982015-08-17 09:45:13 -0700593 def ReviseStashSize(self):
594 print("Revising stash size...")
595 stashes = {}
596
597 # Create the map between a stash and its def/use points. For example, for a
598 # given stash of (idx, sr), stashes[idx] = (sr, def_cmd, use_cmd).
599 for xf in self.transfers:
600 # Command xf defines (stores) all the stashes in stash_before.
601 for idx, sr in xf.stash_before:
602 stashes[idx] = (sr, xf)
603
604 # Record all the stashes command xf uses.
605 for idx, _ in xf.use_stash:
606 stashes[idx] += (xf,)
607
608 # Compute the maximum blocks available for stash based on /cache size and
609 # the threshold.
610 cache_size = common.OPTIONS.cache_size
611 stash_threshold = common.OPTIONS.stash_threshold
612 max_allowed = cache_size * stash_threshold / self.tgt.blocksize
613
614 stashed_blocks = 0
Tao Bao9a5caf22015-08-25 15:10:10 -0700615 new_blocks = 0
Tao Bao82c47982015-08-17 09:45:13 -0700616
617 # Now go through all the commands. Compute the required stash size on the
618 # fly. If a command requires excess stash than available, it deletes the
619 # stash by replacing the command that uses the stash with a "new" command
620 # instead.
621 for xf in self.transfers:
622 replaced_cmds = []
623
624 # xf.stash_before generates explicit stash commands.
625 for idx, sr in xf.stash_before:
626 if stashed_blocks + sr.size() > max_allowed:
627 # We cannot stash this one for a later command. Find out the command
628 # that will use this stash and replace the command with "new".
629 use_cmd = stashes[idx][2]
630 replaced_cmds.append(use_cmd)
Tao Bao9a5caf22015-08-25 15:10:10 -0700631 print("%10d %9s %s" % (sr.size(), "explicit", use_cmd))
Tao Bao82c47982015-08-17 09:45:13 -0700632 else:
633 stashed_blocks += sr.size()
634
635 # xf.use_stash generates free commands.
636 for _, sr in xf.use_stash:
637 stashed_blocks -= sr.size()
638
639 # "move" and "diff" may introduce implicit stashes in BBOTA v3. Prior to
640 # ComputePatches(), they both have the style of "diff".
641 if xf.style == "diff" and self.version >= 3:
642 assert xf.tgt_ranges and xf.src_ranges
643 if xf.src_ranges.overlaps(xf.tgt_ranges):
644 if stashed_blocks + xf.src_ranges.size() > max_allowed:
645 replaced_cmds.append(xf)
Tao Bao9a5caf22015-08-25 15:10:10 -0700646 print("%10d %9s %s" % (xf.src_ranges.size(), "implicit", xf))
Tao Bao82c47982015-08-17 09:45:13 -0700647
648 # Replace the commands in replaced_cmds with "new"s.
649 for cmd in replaced_cmds:
650 # It no longer uses any commands in "use_stash". Remove the def points
651 # for all those stashes.
652 for idx, sr in cmd.use_stash:
653 def_cmd = stashes[idx][1]
654 assert (idx, sr) in def_cmd.stash_before
655 def_cmd.stash_before.remove((idx, sr))
656
Tianjie Xuebe39a02016-01-14 14:12:26 -0800657 # Add up blocks that violates space limit and print total number to
658 # screen later.
659 new_blocks += cmd.tgt_ranges.size()
Tao Bao82c47982015-08-17 09:45:13 -0700660 cmd.ConvertToNew()
661
Tianjie Xuebe39a02016-01-14 14:12:26 -0800662 num_of_bytes = new_blocks * self.tgt.blocksize
663 print(" Total %d blocks (%d bytes) are packed as new blocks due to "
664 "insufficient cache size." % (new_blocks, num_of_bytes))
Tao Bao9a5caf22015-08-25 15:10:10 -0700665
Doug Zongkerfc44a512014-08-26 13:10:25 -0700666 def ComputePatches(self, prefix):
667 print("Reticulating splines...")
668 diff_q = []
669 patch_num = 0
670 with open(prefix + ".new.dat", "wb") as new_f:
671 for xf in self.transfers:
672 if xf.style == "zero":
673 pass
674 elif xf.style == "new":
675 for piece in self.tgt.ReadRangeSet(xf.tgt_ranges):
676 new_f.write(piece)
677 elif xf.style == "diff":
678 src = self.src.ReadRangeSet(xf.src_ranges)
679 tgt = self.tgt.ReadRangeSet(xf.tgt_ranges)
680
681 # We can't compare src and tgt directly because they may have
682 # the same content but be broken up into blocks differently, eg:
683 #
684 # ["he", "llo"] vs ["h", "ello"]
685 #
686 # We want those to compare equal, ideally without having to
687 # actually concatenate the strings (these may be tens of
688 # megabytes).
689
690 src_sha1 = sha1()
691 for p in src:
692 src_sha1.update(p)
693 tgt_sha1 = sha1()
694 tgt_size = 0
695 for p in tgt:
696 tgt_sha1.update(p)
697 tgt_size += len(p)
698
699 if src_sha1.digest() == tgt_sha1.digest():
700 # These are identical; we don't need to generate a patch,
701 # just issue copy commands on the device.
702 xf.style = "move"
703 else:
704 # For files in zip format (eg, APKs, JARs, etc.) we would
705 # like to use imgdiff -z if possible (because it usually
706 # produces significantly smaller patches than bsdiff).
707 # This is permissible if:
708 #
Tao Baob6568cd2016-06-11 12:19:23 -0700709 # - imgdiff is not disabled, and
Doug Zongkerfc44a512014-08-26 13:10:25 -0700710 # - the source and target files are monotonic (ie, the
711 # data is stored with blocks in increasing order), and
712 # - we haven't removed any blocks from the source set.
713 #
714 # If these conditions are satisfied then appending all the
715 # blocks in the set together in order will produce a valid
716 # zip file (plus possibly extra zeros in the last block),
717 # which is what imgdiff needs to operate. (imgdiff is
718 # fine with extra zeros at the end of the file.)
Tao Baob6568cd2016-06-11 12:19:23 -0700719 imgdiff = (not self.disable_imgdiff and xf.intact and
Doug Zongkerfc44a512014-08-26 13:10:25 -0700720 xf.tgt_name.split(".")[-1].lower()
721 in ("apk", "jar", "zip"))
722 xf.style = "imgdiff" if imgdiff else "bsdiff"
723 diff_q.append((tgt_size, src, tgt, xf, patch_num))
724 patch_num += 1
725
726 else:
727 assert False, "unknown style " + xf.style
728
729 if diff_q:
730 if self.threads > 1:
731 print("Computing patches (using %d threads)..." % (self.threads,))
732 else:
733 print("Computing patches...")
734 diff_q.sort()
735
736 patches = [None] * patch_num
737
Dan Albert8b72aef2015-03-23 19:13:21 -0700738 # TODO: Rewrite with multiprocessing.ThreadPool?
Doug Zongkerfc44a512014-08-26 13:10:25 -0700739 lock = threading.Lock()
740 def diff_worker():
741 while True:
742 with lock:
Dan Albert8b72aef2015-03-23 19:13:21 -0700743 if not diff_q:
744 return
Doug Zongkerfc44a512014-08-26 13:10:25 -0700745 tgt_size, src, tgt, xf, patchnum = diff_q.pop()
746 patch = compute_patch(src, tgt, imgdiff=(xf.style == "imgdiff"))
747 size = len(patch)
748 with lock:
749 patches[patchnum] = (patch, xf)
750 print("%10d %10d (%6.2f%%) %7s %s" % (
751 size, tgt_size, size * 100.0 / tgt_size, xf.style,
752 xf.tgt_name if xf.tgt_name == xf.src_name else (
753 xf.tgt_name + " (from " + xf.src_name + ")")))
754
755 threads = [threading.Thread(target=diff_worker)
Dan Albert8b72aef2015-03-23 19:13:21 -0700756 for _ in range(self.threads)]
Doug Zongkerfc44a512014-08-26 13:10:25 -0700757 for th in threads:
758 th.start()
759 while threads:
760 threads.pop().join()
761 else:
762 patches = []
763
764 p = 0
765 with open(prefix + ".patch.dat", "wb") as patch_f:
766 for patch, xf in patches:
767 xf.patch_start = p
768 xf.patch_len = len(patch)
769 patch_f.write(patch)
770 p += len(patch)
771
772 def AssertSequenceGood(self):
773 # Simulate the sequences of transfers we will output, and check that:
774 # - we never read a block after writing it, and
775 # - we write every block we care about exactly once.
776
777 # Start with no blocks having been touched yet.
Doug Zongker2d2dd152016-02-09 08:28:09 -0800778 touched = array.array("B", "\0" * self.tgt.total_blocks)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700779
780 # Imagine processing the transfers in order.
781 for xf in self.transfers:
782 # Check that the input blocks for this transfer haven't yet been touched.
Doug Zongker62338182014-09-08 08:29:55 -0700783
784 x = xf.src_ranges
785 if self.version >= 2:
786 for _, sr in xf.use_stash:
787 x = x.subtract(sr)
788
Doug Zongker2d2dd152016-02-09 08:28:09 -0800789 for s, e in x:
Tao Baodca22002016-03-04 15:23:34 -0800790 # Source image could be larger. Don't check the blocks that are in the
791 # source image only. Since they are not in 'touched', and won't ever
792 # be touched.
793 for i in range(s, min(e, self.tgt.total_blocks)):
Doug Zongker2d2dd152016-02-09 08:28:09 -0800794 assert touched[i] == 0
795
796 # Check that the output blocks for this transfer haven't yet
797 # been touched, and touch all the blocks written by this
798 # transfer.
799 for s, e in xf.tgt_ranges:
800 for i in range(s, e):
801 assert touched[i] == 0
802 touched[i] = 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700803
804 # Check that we've written every target block.
Doug Zongker2d2dd152016-02-09 08:28:09 -0800805 for s, e in self.tgt.care_map:
806 for i in range(s, e):
807 assert touched[i] == 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700808
Doug Zongker62338182014-09-08 08:29:55 -0700809 def ImproveVertexSequence(self):
810 print("Improving vertex order...")
811
812 # At this point our digraph is acyclic; we reversed any edges that
813 # were backwards in the heuristically-generated sequence. The
814 # previously-generated order is still acceptable, but we hope to
815 # find a better order that needs less memory for stashed data.
816 # Now we do a topological sort to generate a new vertex order,
817 # using a greedy algorithm to choose which vertex goes next
818 # whenever we have a choice.
819
820 # Make a copy of the edge set; this copy will get destroyed by the
821 # algorithm.
822 for xf in self.transfers:
823 xf.incoming = xf.goes_after.copy()
824 xf.outgoing = xf.goes_before.copy()
825
826 L = [] # the new vertex order
827
828 # S is the set of sources in the remaining graph; we always choose
829 # the one that leaves the least amount of stashed data after it's
830 # executed.
831 S = [(u.NetStashChange(), u.order, u) for u in self.transfers
832 if not u.incoming]
833 heapq.heapify(S)
834
835 while S:
836 _, _, xf = heapq.heappop(S)
837 L.append(xf)
838 for u in xf.outgoing:
839 del u.incoming[xf]
840 if not u.incoming:
841 heapq.heappush(S, (u.NetStashChange(), u.order, u))
842
843 # if this fails then our graph had a cycle.
844 assert len(L) == len(self.transfers)
845
846 self.transfers = L
847 for i, xf in enumerate(L):
848 xf.order = i
849
Doug Zongkerfc44a512014-08-26 13:10:25 -0700850 def RemoveBackwardEdges(self):
851 print("Removing backward edges...")
852 in_order = 0
853 out_of_order = 0
854 lost_source = 0
855
856 for xf in self.transfers:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700857 lost = 0
858 size = xf.src_ranges.size()
859 for u in xf.goes_before:
860 # xf should go before u
861 if xf.order < u.order:
862 # it does, hurray!
Doug Zongker62338182014-09-08 08:29:55 -0700863 in_order += 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700864 else:
865 # it doesn't, boo. trim the blocks that u writes from xf's
866 # source, so that xf can go after u.
Doug Zongker62338182014-09-08 08:29:55 -0700867 out_of_order += 1
Doug Zongkerfc44a512014-08-26 13:10:25 -0700868 assert xf.src_ranges.overlaps(u.tgt_ranges)
869 xf.src_ranges = xf.src_ranges.subtract(u.tgt_ranges)
870 xf.intact = False
871
872 if xf.style == "diff" and not xf.src_ranges:
873 # nothing left to diff from; treat as new data
874 xf.style = "new"
875
876 lost = size - xf.src_ranges.size()
877 lost_source += lost
Doug Zongkerfc44a512014-08-26 13:10:25 -0700878
879 print((" %d/%d dependencies (%.2f%%) were violated; "
880 "%d source blocks removed.") %
881 (out_of_order, in_order + out_of_order,
882 (out_of_order * 100.0 / (in_order + out_of_order))
883 if (in_order + out_of_order) else 0.0,
884 lost_source))
885
Doug Zongker62338182014-09-08 08:29:55 -0700886 def ReverseBackwardEdges(self):
887 print("Reversing backward edges...")
888 in_order = 0
889 out_of_order = 0
890 stashes = 0
891 stash_size = 0
892
893 for xf in self.transfers:
Doug Zongker62338182014-09-08 08:29:55 -0700894 for u in xf.goes_before.copy():
895 # xf should go before u
896 if xf.order < u.order:
897 # it does, hurray!
898 in_order += 1
899 else:
900 # it doesn't, boo. modify u to stash the blocks that it
901 # writes that xf wants to read, and then require u to go
902 # before xf.
903 out_of_order += 1
904
905 overlap = xf.src_ranges.intersect(u.tgt_ranges)
906 assert overlap
907
908 u.stash_before.append((stashes, overlap))
909 xf.use_stash.append((stashes, overlap))
910 stashes += 1
911 stash_size += overlap.size()
912
913 # reverse the edge direction; now xf must go after u
914 del xf.goes_before[u]
915 del u.goes_after[xf]
916 xf.goes_after[u] = None # value doesn't matter
917 u.goes_before[xf] = None
918
919 print((" %d/%d dependencies (%.2f%%) were violated; "
920 "%d source blocks stashed.") %
921 (out_of_order, in_order + out_of_order,
922 (out_of_order * 100.0 / (in_order + out_of_order))
923 if (in_order + out_of_order) else 0.0,
924 stash_size))
925
Doug Zongkerfc44a512014-08-26 13:10:25 -0700926 def FindVertexSequence(self):
927 print("Finding vertex sequence...")
928
929 # This is based on "A Fast & Effective Heuristic for the Feedback
930 # Arc Set Problem" by P. Eades, X. Lin, and W.F. Smyth. Think of
931 # it as starting with the digraph G and moving all the vertices to
932 # be on a horizontal line in some order, trying to minimize the
933 # number of edges that end up pointing to the left. Left-pointing
934 # edges will get removed to turn the digraph into a DAG. In this
935 # case each edge has a weight which is the number of source blocks
936 # we'll lose if that edge is removed; we try to minimize the total
937 # weight rather than just the number of edges.
938
939 # Make a copy of the edge set; this copy will get destroyed by the
940 # algorithm.
941 for xf in self.transfers:
942 xf.incoming = xf.goes_after.copy()
943 xf.outgoing = xf.goes_before.copy()
Doug Zongker2d2dd152016-02-09 08:28:09 -0800944 xf.score = sum(xf.outgoing.values()) - sum(xf.incoming.values())
Doug Zongkerfc44a512014-08-26 13:10:25 -0700945
946 # We use an OrderedDict instead of just a set so that the output
947 # is repeatable; otherwise it would depend on the hash values of
948 # the transfer objects.
949 G = OrderedDict()
950 for xf in self.transfers:
951 G[xf] = None
952 s1 = deque() # the left side of the sequence, built from left to right
953 s2 = deque() # the right side of the sequence, built from right to left
954
Doug Zongker2d2dd152016-02-09 08:28:09 -0800955 heap = []
956 for xf in self.transfers:
957 xf.heap_item = HeapItem(xf)
958 heap.append(xf.heap_item)
959 heapq.heapify(heap)
Doug Zongkerfc44a512014-08-26 13:10:25 -0700960
Doug Zongker2d2dd152016-02-09 08:28:09 -0800961 sinks = set(u for u in G if not u.outgoing)
962 sources = set(u for u in G if not u.incoming)
963
964 def adjust_score(iu, delta):
965 iu.score += delta
966 iu.heap_item.clear()
967 iu.heap_item = HeapItem(iu)
968 heapq.heappush(heap, iu.heap_item)
969
970 while G:
Doug Zongkerfc44a512014-08-26 13:10:25 -0700971 # Put all sinks at the end of the sequence.
Doug Zongker2d2dd152016-02-09 08:28:09 -0800972 while sinks:
973 new_sinks = set()
Doug Zongkerfc44a512014-08-26 13:10:25 -0700974 for u in sinks:
Doug Zongker2d2dd152016-02-09 08:28:09 -0800975 if u not in G: continue
Doug Zongkerfc44a512014-08-26 13:10:25 -0700976 s2.appendleft(u)
977 del G[u]
978 for iu in u.incoming:
Doug Zongker2d2dd152016-02-09 08:28:09 -0800979 adjust_score(iu, -iu.outgoing.pop(u))
980 if not iu.outgoing: new_sinks.add(iu)
981 sinks = new_sinks
Doug Zongkerfc44a512014-08-26 13:10:25 -0700982
983 # Put all the sources at the beginning of the sequence.
Doug Zongker2d2dd152016-02-09 08:28:09 -0800984 while sources:
985 new_sources = set()
Doug Zongkerfc44a512014-08-26 13:10:25 -0700986 for u in sources:
Doug Zongker2d2dd152016-02-09 08:28:09 -0800987 if u not in G: continue
Doug Zongkerfc44a512014-08-26 13:10:25 -0700988 s1.append(u)
989 del G[u]
990 for iu in u.outgoing:
Doug Zongker2d2dd152016-02-09 08:28:09 -0800991 adjust_score(iu, +iu.incoming.pop(u))
992 if not iu.incoming: new_sources.add(iu)
993 sources = new_sources
Doug Zongkerfc44a512014-08-26 13:10:25 -0700994
Doug Zongker2d2dd152016-02-09 08:28:09 -0800995 if not G: break
Doug Zongkerfc44a512014-08-26 13:10:25 -0700996
997 # Find the "best" vertex to put next. "Best" is the one that
998 # maximizes the net difference in source blocks saved we get by
999 # pretending it's a source rather than a sink.
1000
Doug Zongker2d2dd152016-02-09 08:28:09 -08001001 while True:
1002 u = heapq.heappop(heap)
1003 if u and u.item in G:
1004 u = u.item
1005 break
Doug Zongkerfc44a512014-08-26 13:10:25 -07001006
Doug Zongkerfc44a512014-08-26 13:10:25 -07001007 s1.append(u)
1008 del G[u]
1009 for iu in u.outgoing:
Doug Zongker2d2dd152016-02-09 08:28:09 -08001010 adjust_score(iu, +iu.incoming.pop(u))
1011 if not iu.incoming: sources.add(iu)
1012
Doug Zongkerfc44a512014-08-26 13:10:25 -07001013 for iu in u.incoming:
Doug Zongker2d2dd152016-02-09 08:28:09 -08001014 adjust_score(iu, -iu.outgoing.pop(u))
1015 if not iu.outgoing: sinks.add(iu)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001016
1017 # Now record the sequence in the 'order' field of each transfer,
1018 # and by rearranging self.transfers to be in the chosen sequence.
1019
1020 new_transfers = []
1021 for x in itertools.chain(s1, s2):
1022 x.order = len(new_transfers)
1023 new_transfers.append(x)
1024 del x.incoming
1025 del x.outgoing
1026
1027 self.transfers = new_transfers
1028
1029 def GenerateDigraph(self):
1030 print("Generating digraph...")
Doug Zongker2d2dd152016-02-09 08:28:09 -08001031
1032 # Each item of source_ranges will be:
1033 # - None, if that block is not used as a source,
1034 # - a transfer, if one transfer uses it as a source, or
1035 # - a set of transfers.
1036 source_ranges = []
1037 for b in self.transfers:
1038 for s, e in b.src_ranges:
1039 if e > len(source_ranges):
1040 source_ranges.extend([None] * (e-len(source_ranges)))
1041 for i in range(s, e):
1042 if source_ranges[i] is None:
1043 source_ranges[i] = b
1044 else:
1045 if not isinstance(source_ranges[i], set):
1046 source_ranges[i] = set([source_ranges[i]])
1047 source_ranges[i].add(b)
1048
Doug Zongkerfc44a512014-08-26 13:10:25 -07001049 for a in self.transfers:
Doug Zongker2d2dd152016-02-09 08:28:09 -08001050 intersections = set()
1051 for s, e in a.tgt_ranges:
1052 for i in range(s, e):
1053 if i >= len(source_ranges): break
1054 b = source_ranges[i]
1055 if b is not None:
1056 if isinstance(b, set):
1057 intersections.update(b)
1058 else:
1059 intersections.add(b)
1060
1061 for b in intersections:
1062 if a is b: continue
Doug Zongkerfc44a512014-08-26 13:10:25 -07001063
1064 # If the blocks written by A are read by B, then B needs to go before A.
1065 i = a.tgt_ranges.intersect(b.src_ranges)
1066 if i:
Doug Zongkerab7ca1d2014-08-26 10:40:28 -07001067 if b.src_name == "__ZERO":
1068 # the cost of removing source blocks for the __ZERO domain
1069 # is (nearly) zero.
1070 size = 0
1071 else:
1072 size = i.size()
Doug Zongkerfc44a512014-08-26 13:10:25 -07001073 b.goes_before[a] = size
1074 a.goes_after[b] = size
1075
1076 def FindTransfers(self):
Tao Bao9a5caf22015-08-25 15:10:10 -07001077 """Parse the file_map to generate all the transfers."""
1078
1079 def AddTransfer(tgt_name, src_name, tgt_ranges, src_ranges, style, by_id,
1080 split=False):
1081 """Wrapper function for adding a Transfer().
1082
1083 For BBOTA v3, we need to stash source blocks for resumable feature.
1084 However, with the growth of file size and the shrink of the cache
1085 partition source blocks are too large to be stashed. If a file occupies
1086 too many blocks (greater than MAX_BLOCKS_PER_DIFF_TRANSFER), we split it
1087 into smaller pieces by getting multiple Transfer()s.
1088
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001089 The downside is that after splitting, we may increase the package size
1090 since the split pieces don't align well. According to our experiments,
1091 1/8 of the cache size as the per-piece limit appears to be optimal.
1092 Compared to the fixed 1024-block limit, it reduces the overall package
1093 size by 30% volantis, and 20% for angler and bullhead."""
Tao Bao9a5caf22015-08-25 15:10:10 -07001094
1095 # We care about diff transfers only.
1096 if style != "diff" or not split:
1097 Transfer(tgt_name, src_name, tgt_ranges, src_ranges, style, by_id)
1098 return
1099
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001100 pieces = 0
1101 cache_size = common.OPTIONS.cache_size
1102 split_threshold = 0.125
1103 max_blocks_per_transfer = int(cache_size * split_threshold /
1104 self.tgt.blocksize)
1105
Tao Bao9a5caf22015-08-25 15:10:10 -07001106 # Change nothing for small files.
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001107 if (tgt_ranges.size() <= max_blocks_per_transfer and
1108 src_ranges.size() <= max_blocks_per_transfer):
Tao Bao9a5caf22015-08-25 15:10:10 -07001109 Transfer(tgt_name, src_name, tgt_ranges, src_ranges, style, by_id)
1110 return
1111
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001112 while (tgt_ranges.size() > max_blocks_per_transfer and
1113 src_ranges.size() > max_blocks_per_transfer):
Tao Bao9a5caf22015-08-25 15:10:10 -07001114 tgt_split_name = "%s-%d" % (tgt_name, pieces)
1115 src_split_name = "%s-%d" % (src_name, pieces)
Tianjie Xubb86e1d2016-01-13 16:14:10 -08001116 tgt_first = tgt_ranges.first(max_blocks_per_transfer)
1117 src_first = src_ranges.first(max_blocks_per_transfer)
1118
Tao Bao9a5caf22015-08-25 15:10:10 -07001119 Transfer(tgt_split_name, src_split_name, tgt_first, src_first, style,
1120 by_id)
1121
1122 tgt_ranges = tgt_ranges.subtract(tgt_first)
1123 src_ranges = src_ranges.subtract(src_first)
1124 pieces += 1
1125
1126 # Handle remaining blocks.
1127 if tgt_ranges.size() or src_ranges.size():
1128 # Must be both non-empty.
1129 assert tgt_ranges.size() and src_ranges.size()
1130 tgt_split_name = "%s-%d" % (tgt_name, pieces)
1131 src_split_name = "%s-%d" % (src_name, pieces)
1132 Transfer(tgt_split_name, src_split_name, tgt_ranges, src_ranges, style,
1133 by_id)
1134
Doug Zongkerfc44a512014-08-26 13:10:25 -07001135 empty = RangeSet()
1136 for tgt_fn, tgt_ranges in self.tgt.file_map.items():
1137 if tgt_fn == "__ZERO":
1138 # the special "__ZERO" domain is all the blocks not contained
1139 # in any file and that are filled with zeros. We have a
1140 # special transfer style for zero blocks.
1141 src_ranges = self.src.file_map.get("__ZERO", empty)
Tao Bao9a5caf22015-08-25 15:10:10 -07001142 AddTransfer(tgt_fn, "__ZERO", tgt_ranges, src_ranges,
1143 "zero", self.transfers)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001144 continue
1145
Tao Baoff777812015-05-12 11:42:31 -07001146 elif tgt_fn == "__COPY":
1147 # "__COPY" domain includes all the blocks not contained in any
1148 # file and that need to be copied unconditionally to the target.
Tao Bao9a5caf22015-08-25 15:10:10 -07001149 AddTransfer(tgt_fn, None, tgt_ranges, empty, "new", self.transfers)
Tao Baoff777812015-05-12 11:42:31 -07001150 continue
1151
Doug Zongkerfc44a512014-08-26 13:10:25 -07001152 elif tgt_fn in self.src.file_map:
1153 # Look for an exact pathname match in the source.
Tao Bao9a5caf22015-08-25 15:10:10 -07001154 AddTransfer(tgt_fn, tgt_fn, tgt_ranges, self.src.file_map[tgt_fn],
1155 "diff", self.transfers, self.version >= 3)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001156 continue
1157
1158 b = os.path.basename(tgt_fn)
1159 if b in self.src_basenames:
1160 # Look for an exact basename match in the source.
1161 src_fn = self.src_basenames[b]
Tao Bao9a5caf22015-08-25 15:10:10 -07001162 AddTransfer(tgt_fn, src_fn, tgt_ranges, self.src.file_map[src_fn],
1163 "diff", self.transfers, self.version >= 3)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001164 continue
1165
1166 b = re.sub("[0-9]+", "#", b)
1167 if b in self.src_numpatterns:
1168 # Look for a 'number pattern' match (a basename match after
1169 # all runs of digits are replaced by "#"). (This is useful
1170 # for .so files that contain version numbers in the filename
1171 # that get bumped.)
1172 src_fn = self.src_numpatterns[b]
Tao Bao9a5caf22015-08-25 15:10:10 -07001173 AddTransfer(tgt_fn, src_fn, tgt_ranges, self.src.file_map[src_fn],
1174 "diff", self.transfers, self.version >= 3)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001175 continue
1176
Tao Bao9a5caf22015-08-25 15:10:10 -07001177 AddTransfer(tgt_fn, None, tgt_ranges, empty, "new", self.transfers)
Doug Zongkerfc44a512014-08-26 13:10:25 -07001178
1179 def AbbreviateSourceNames(self):
Doug Zongkerfc44a512014-08-26 13:10:25 -07001180 for k in self.src.file_map.keys():
1181 b = os.path.basename(k)
1182 self.src_basenames[b] = k
1183 b = re.sub("[0-9]+", "#", b)
1184 self.src_numpatterns[b] = k
1185
1186 @staticmethod
1187 def AssertPartition(total, seq):
1188 """Assert that all the RangeSets in 'seq' form a partition of the
1189 'total' RangeSet (ie, they are nonintersecting and their union
1190 equals 'total')."""
Doug Zongker2d2dd152016-02-09 08:28:09 -08001191
Doug Zongkerfc44a512014-08-26 13:10:25 -07001192 so_far = RangeSet()
1193 for i in seq:
1194 assert not so_far.overlaps(i)
1195 so_far = so_far.union(i)
1196 assert so_far == total