]> arthur.barton.de Git - bup.git/blob - cmd/margin-cmd.py
c042bf88bab8dd3795cb75d3e6f2bf2df44c3109
[bup.git] / cmd / margin-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6 # end of bup preamble
7
8 from __future__ import absolute_import
9 import sys, struct, math
10
11 from bup import options, git, _helpers
12 from bup.helpers import log
13
14 POPULATION_OF_EARTH=6.7e9  # as of September, 2010
15
16 optspec = """
17 bup margin
18 --
19 predict    Guess object offsets and report the maximum deviation
20 ignore-midx  Don't use midx files; use only plain pack idx files.
21 """
22 o = options.Options(optspec)
23 (opt, flags, extra) = o.parse(sys.argv[1:])
24
25 if extra:
26     o.fatal("no arguments expected")
27
28 git.check_repo_or_die()
29 git.ignore_midx = opt.ignore_midx
30
31 mi = git.PackIdxList(git.repo('objects/pack'))
32
33 def do_predict(ix):
34     total = len(ix)
35     maxdiff = 0
36     for count,i in enumerate(ix):
37         prefix = struct.unpack('!Q', i[:8])[0]
38         expected = prefix * total / (1<<64)
39         diff = count - expected
40         maxdiff = max(maxdiff, abs(diff))
41     print '%d of %d (%.3f%%) ' % (maxdiff, len(ix), maxdiff*100.0/len(ix))
42     sys.stdout.flush()
43     assert(count+1 == len(ix))
44
45 if opt.predict:
46     if opt.ignore_midx:
47         for pack in mi.packs:
48             do_predict(pack)
49     else:
50         do_predict(mi)
51 else:
52     # default mode: find longest matching prefix
53     last = '\0'*20
54     longmatch = 0
55     for i in mi:
56         if i == last:
57             continue
58         #assert(str(i) >= last)
59         pm = _helpers.bitmatch(last, i)
60         longmatch = max(longmatch, pm)
61         last = i
62     print longmatch
63     log('%d matching prefix bits\n' % longmatch)
64     doublings = math.log(len(mi), 2)
65     bpd = longmatch / doublings
66     log('%.2f bits per doubling\n' % bpd)
67     remain = 160 - longmatch
68     rdoublings = remain / bpd
69     log('%d bits (%.2f doublings) remaining\n' % (remain, rdoublings))
70     larger = 2**rdoublings
71     log('%g times larger is possible\n' % larger)
72     perperson = larger/POPULATION_OF_EARTH
73     log('\nEveryone on earth could have %d data sets like yours, all in one\n'
74         'repository, and we would expect 1 object collision.\n'
75         % int(perperson))